|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| |
|
17 |
| |
|
18 |
| |
|
19 |
| |
|
20 |
| package org.apache.xindice.core.query; |
|
21 |
| |
|
22 |
| import org.apache.commons.logging.Log; |
|
23 |
| import org.apache.commons.logging.LogFactory; |
|
24 |
| import org.apache.xindice.client.xmldb.XindiceCollection; |
|
25 |
| import org.apache.xindice.core.data.NodeSet; |
|
26 |
| import org.apache.xindice.util.XindiceRuntimeException; |
|
27 |
| import org.apache.xindice.xml.NamespaceMap; |
|
28 |
| import org.apache.xindice.xml.TextWriter; |
|
29 |
| import org.apache.xindice.xml.dom.DBNode; |
|
30 |
| import org.apache.xindice.xml.dom.DocumentImpl; |
|
31 |
| import org.apache.xindice.xml.dom.NodeImpl; |
|
32 |
| |
|
33 |
| import org.w3c.dom.Attr; |
|
34 |
| import org.w3c.dom.Comment; |
|
35 |
| import org.w3c.dom.Document; |
|
36 |
| import org.w3c.dom.Element; |
|
37 |
| import org.w3c.dom.Node; |
|
38 |
| import org.w3c.dom.ProcessingInstruction; |
|
39 |
| import org.w3c.dom.Text; |
|
40 |
| |
|
41 |
| import java.util.Map; |
|
42 |
| |
|
43 |
| |
|
44 |
| |
|
45 |
| |
|
46 |
| |
|
47 |
| |
|
48 |
| public class QueryUtil { |
|
49 |
| |
|
50 |
| private static final Log log = LogFactory.getLog(QueryUtil.class); |
|
51 |
| |
|
52 |
| |
|
53 |
| |
|
54 |
| |
|
55 |
| |
|
56 |
357
| public static NamespaceMap mapNamespaces(Map namespaces) {
|
|
57 |
357
| if (namespaces != null && namespaces.size() > 0) {
|
|
58 |
9
| return new NamespaceMap(namespaces);
|
|
59 |
| } |
|
60 |
| |
|
61 |
348
| return null;
|
|
62 |
| } |
|
63 |
| |
|
64 |
| |
|
65 |
| |
|
66 |
| |
|
67 |
| |
|
68 |
| |
|
69 |
357
| public static Document queryResultsToDOM(NodeSet nodeSet, boolean expandSource) {
|
|
70 |
357
| DocumentImpl doc = new DocumentImpl();
|
|
71 |
| |
|
72 |
357
| Element root = doc.createElement("result");
|
|
73 |
357
| doc.appendChild(root);
|
|
74 |
357
| int count = 0;
|
|
75 |
357
| while (nodeSet != null && nodeSet.hasMoreNodes()) {
|
|
76 |
531
| final Object element = nodeSet.getNextNode();
|
|
77 |
531
| if (element instanceof Attr) {
|
|
78 |
27
| Attr n = (Attr) element;
|
|
79 |
| |
|
80 |
27
| Element holder = doc.createElementNS(XindiceCollection.QUERY_NS, "xq:result");
|
|
81 |
27
| holder.setAttribute(NodeImpl.XMLNS_PREFIX + ":xq", XindiceCollection.QUERY_NS);
|
|
82 |
27
| holder.setAttributeNode((Attr) doc.importNode(n, true));
|
|
83 |
| |
|
84 |
27
| if (expandSource && n instanceof DBNode) {
|
|
85 |
27
| ((DBNode) holder).setSource(((DBNode) n).getSource());
|
|
86 |
27
| ((DBNode) holder).expandSource();
|
|
87 |
| } |
|
88 |
| |
|
89 |
27
| root.appendChild(holder);
|
|
90 |
504
| } else if (element instanceof Text || element instanceof Comment) {
|
|
91 |
36
| Node n = (Node) element;
|
|
92 |
| |
|
93 |
36
| Element holder = doc.createElementNS(XindiceCollection.QUERY_NS, "xq:result");
|
|
94 |
36
| holder.setAttribute(NodeImpl.XMLNS_PREFIX + ":xq", XindiceCollection.QUERY_NS);
|
|
95 |
36
| holder.appendChild(doc.importNode(n, true));
|
|
96 |
| |
|
97 |
36
| if (expandSource && n instanceof DBNode) {
|
|
98 |
36
| ((DBNode) holder).setSource(((DBNode) n).getSource());
|
|
99 |
36
| ((DBNode) holder).expandSource();
|
|
100 |
| } |
|
101 |
| |
|
102 |
36
| root.appendChild(holder);
|
|
103 |
468
| } else if (element instanceof ProcessingInstruction) {
|
|
104 |
0
| if (log.isWarnEnabled()) {
|
|
105 |
0
| log.warn("XPath query with ProcessingInstruction result is not supported");
|
|
106 |
| } |
|
107 |
468
| } else if (element instanceof Node) {
|
|
108 |
468
| Node n = (Node) element;
|
|
109 |
| |
|
110 |
468
| if (n.getNodeType() == Node.DOCUMENT_NODE) {
|
|
111 |
0
| n = ((Document) n).getDocumentElement();
|
|
112 |
| } |
|
113 |
| |
|
114 |
468
| if (expandSource && n instanceof DBNode) {
|
|
115 |
468
| ((DBNode) n).expandSource();
|
|
116 |
| } |
|
117 |
| |
|
118 |
468
| root.appendChild(doc.importNode(n, true));
|
|
119 |
| } else { |
|
120 |
0
| throw new XindiceRuntimeException("Unknown result type (" + element.getClass().getName() + ") in nodeset");
|
|
121 |
| } |
|
122 |
| |
|
123 |
531
| count++;
|
|
124 |
| } |
|
125 |
| |
|
126 |
357
| root.setAttribute("count", Integer.toString(count));
|
|
127 |
357
| return doc;
|
|
128 |
| } |
|
129 |
| |
|
130 |
| |
|
131 |
| |
|
132 |
| |
|
133 |
| |
|
134 |
238
| public static Document queryResultsToDOM(NodeSet nodeSet) {
|
|
135 |
238
| return queryResultsToDOM(nodeSet, true);
|
|
136 |
| } |
|
137 |
| |
|
138 |
| |
|
139 |
| |
|
140 |
| |
|
141 |
| |
|
142 |
| |
|
143 |
0
| public static String queryResultsToString(NodeSet nodeSet, boolean expandSource) {
|
|
144 |
0
| return TextWriter.toString(queryResultsToDOM(nodeSet, expandSource));
|
|
145 |
| } |
|
146 |
| |
|
147 |
| |
|
148 |
| |
|
149 |
| |
|
150 |
| |
|
151 |
119
| public static String queryResultsToString(NodeSet nodeSet) {
|
|
152 |
119
| return TextWriter.toString(queryResultsToDOM(nodeSet, true));
|
|
153 |
| } |
|
154 |
| } |