Skip to content

Commit dbb4431

Browse files
committed
half of XPath
1 parent 26d485f commit dbb4431

File tree

9 files changed

+190
-147
lines changed

9 files changed

+190
-147
lines changed

xquery-engine/.idea/workspace.xml

+100-137
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Binary file not shown.
Binary file not shown.
Binary file not shown.

xquery-engine/out/production/xquery-engine/src/main/resources/test.xml xquery-engine/out/production/xquery-engine/test.xml

+4
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,8 @@
1818
<description>Two eggs, bacon or sausage, toast, and our ever-popular hash browns</description>
1919
<calories>950</calories>
2020
</food>
21+
<drink>
22+
<name>Coco</name>
23+
<price>$1.09</price>
24+
</drink>
2125
</breakfast_menu>

xquery-engine/src/main/java/Contex.java

+4
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ public int getLength(){
2525
return nodeList.size();
2626
}
2727

28+
public Node item(int i){
29+
return nodeList.get(i);
30+
}
31+
2832
public Node getCurrentNode(){
2933
return nodeList.peek();
3034
}

xquery-engine/src/main/java/Main.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616
public class Main {
1717

18-
public static String query = "document(\"pom.xml\")/*";
18+
public static String query = "document(\"test.xml\")/*/drink/name/text()";
1919

2020
public static void main(String[] args) throws Exception{
2121

@@ -29,8 +29,8 @@ public static void main(String[] args) throws Exception{
2929
MyVisitor myVisitor = new MyVisitor();
3030
LinkedList<Node> res = (LinkedList<Node>) myVisitor.visit(tree);
3131

32-
for (int i=0; i<res.size(); i++) {
33-
System.out.println(res.get(i).getNodeName());
32+
for (Node node : res) {
33+
System.out.println("query node list: " + node.getNodeValue());
3434
}
3535

3636
}

xquery-engine/src/main/java/MyVisitor.java

+75-7
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@
1010
import javax.xml.parsers.DocumentBuilder;
1111
import javax.xml.parsers.DocumentBuilderFactory;
1212
import java.io.File;
13+
import java.util.HashMap;
1314
import java.util.LinkedList;
1415

16+
1517
/**
1618
* Created by wilsonli on 5/10/16.
1719
*/
@@ -20,11 +22,23 @@ public class MyVisitor extends XqueryBaseVisitor<Object> {
2022
/* use this stack to store the context for pass */
2123
private LinkedList<Contex> stack = new LinkedList<Contex>();
2224

25+
private static LinkedList<Node> unique(LinkedList<Node> nodeList){
26+
System.out.println("uniquing.");
27+
HashMap<Node, Boolean> hash = new HashMap<>();
28+
LinkedList<Node> res = new LinkedList<>();
29+
for (Node node : nodeList){
30+
if (!hash.containsKey(node))
31+
res.add(node);
32+
}
33+
return res;
34+
}
35+
2336
@Override public Object visitAPChildren(XqueryParser.APChildrenContext ctx) {
2437
/*
2538
for rule [doc(fileName)/rp]A:
2639
get the root node, which is fname and pass it to rp
2740
*/
41+
2842
String fname = (String) visit(ctx.doc());
2943
File xmlFile = new File(fname);
3044
// open the xml file //
@@ -38,11 +52,9 @@ public class MyVisitor extends XqueryBaseVisitor<Object> {
3852
} catch (Exception ex) {
3953
ex.printStackTrace();
4054
}
41-
4255
return visit(ctx.rp());
4356
}
4457

45-
4658
@Override public Object visitAPAll(XqueryParser.APAllContext ctx) {
4759
/* get the */
4860

@@ -74,17 +86,73 @@ public class MyVisitor extends XqueryBaseVisitor<Object> {
7486
Contex current = stack.peek();
7587
LinkedList<Node> res = new LinkedList<>();
7688
for (int i = 0; i < current.getLength(); i++){
77-
for (int j = 0; j < current.item(i).getChildNodes().getLength(); j++)
78-
res.add(current.item(i).getChildNodes().item(j));
89+
NodeList tmp = current.item(i).getChildNodes();
90+
for (int j = 0; j < tmp.getLength(); j++)
91+
res.add(tmp.item(j));
7992
}
8093
return res;
8194
}
8295

83-
84-
8596
@Override public Object visitTagName(XqueryParser.TagNameContext ctx) {
97+
System.out.println("TagName running.");
8698
LinkedList<Node> res = new LinkedList<>();
99+
String tagName = (String) ctx.getText();
100+
101+
for (Node node : stack.peek().getChildNodes()){
102+
if (tagName.equals(node.getNodeName()))
103+
res.add(node);
104+
}
105+
System.out.println("TagName stop.");
106+
return res;
107+
}
108+
109+
@Override public Object visitRPwithPar(XqueryParser.RPwithParContext ctx) {
110+
System.out.println("rp with parameter running.");
111+
return visit(ctx.rp());
112+
}
113+
114+
@Override public Object visitRPChildren(XqueryParser.RPChildrenContext ctx) {
115+
System.out.println("RPChildren running.");
116+
LinkedList<Node> x = (LinkedList<Node>) visit(ctx.rp(0));
117+
Contex middle = new Contex();
118+
for (Node node : x)
119+
middle.add(node);
120+
stack.push(middle);
121+
LinkedList<Node> y = (LinkedList<Node>) visit(ctx.rp(1));
122+
System.out.println(y.size());
123+
LinkedList<Node> res = unique(y);
124+
System.out.println(res.size());
125+
stack.pop();
126+
return res;
127+
}
128+
129+
// i don't think this is correct. //
130+
@Override public Object visitRPCondition(XqueryParser.RPConditionContext ctx) {
131+
System.out.println("[rp[f]]r(n) running.");
132+
LinkedList<Node> x = (LinkedList<Node>) visit(ctx.rp());
133+
Contex middle = new Contex();
134+
for (Node node : x)
135+
middle.add(node);
136+
stack.push(middle);
137+
LinkedList<Node> res = (LinkedList<Node>) visit(ctx.fltr());
138+
return res;
139+
}
140+
141+
@Override public Object visitTwoRP(XqueryParser.TwoRPContext ctx) {
142+
System.out.println("[rp1,rp2]r(n) running.");
143+
LinkedList<Node> x = (LinkedList<Node>) visit(ctx.rp(0));
144+
System.out.println(x.size());
145+
LinkedList<Node> y = (LinkedList<Node>) visit(ctx.rp(1));
146+
x.addLast(y.getFirst());
147+
System.out.println(x.size());
148+
149+
return x;
150+
}
151+
152+
@Override public Object visitText(XqueryParser.TextContext ctx) {
153+
System.out.println("text() running.");
154+
Contex current = stack.peek();
155+
return current.getChildNodes();
87156

88-
return visitChildren(ctx);
89157
}
90158
}

xquery-engine/src/main/resources/test.xml xquery-engine/test.xml

+4
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,8 @@
1818
<description>Two eggs, bacon or sausage, toast, and our ever-popular hash browns</description>
1919
<calories>950</calories>
2020
</food>
21+
<drink>
22+
<name>Coco</name>
23+
<price>$1.09</price>
24+
</drink>
2125
</breakfast_menu>

0 commit comments

Comments
 (0)