|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| |
|
17 |
| |
|
18 |
| |
|
19 |
| |
|
20 |
| package org.apache.xindice.webadmin.viewer.components; |
|
21 |
| |
|
22 |
| import java.io.IOException; |
|
23 |
| import java.io.Writer; |
|
24 |
| |
|
25 |
| import javax.servlet.ServletException; |
|
26 |
| import javax.servlet.http.HttpServletRequest; |
|
27 |
| import javax.servlet.http.HttpServletResponse; |
|
28 |
| |
|
29 |
| import org.w3c.dom.Node; |
|
30 |
| |
|
31 |
| import org.apache.xindice.xml.NamespaceMap; |
|
32 |
| import org.apache.xindice.core.Collection; |
|
33 |
| import org.apache.xindice.core.DBException; |
|
34 |
| import org.apache.xindice.core.data.NodeSet; |
|
35 |
| import org.apache.xindice.webadmin.viewer.*; |
|
36 |
| import org.apache.xindice.webadmin.viewer.HtmlViewerComponent; |
|
37 |
| import org.apache.xindice.xml.Xml2HtmlWriter; |
|
38 |
| import org.apache.commons.logging.Log; |
|
39 |
| import org.apache.commons.logging.LogFactory; |
|
40 |
| |
|
41 |
| |
|
42 |
| |
|
43 |
| |
|
44 |
| |
|
45 |
| |
|
46 |
| |
|
47 |
| public class XPathSearchCollectionViewer extends HtmlViewerComponent implements HtmlCollectionViewer { |
|
48 |
| |
|
49 |
| private static final Log log = LogFactory.getLog(XPathSearchCollectionViewer.class); |
|
50 |
| |
|
51 |
0
| public void execute(HttpServletRequest req, HttpServletResponse res, Collection col)
|
|
52 |
| throws ServletException, IOException { |
|
53 |
| |
|
54 |
0
| String query = req.getParameter("query");
|
|
55 |
0
| if (query == null) {
|
|
56 |
0
| query = "";
|
|
57 |
| } |
|
58 |
| |
|
59 |
0
| String path = col.getCanonicalName();
|
|
60 |
0
| String title = "XPATH Query for " + path;
|
|
61 |
0
| Writer output = startViewer(title, path, res);
|
|
62 |
| |
|
63 |
| |
|
64 |
0
| String[] ns = {"", ""};
|
|
65 |
0
| int nsSize = 1;
|
|
66 |
0
| String rawNsSize = req.getParameter("nssize");
|
|
67 |
0
| if (rawNsSize != null) {
|
|
68 |
0
| nsSize = Integer.parseInt(rawNsSize);
|
|
69 |
0
| ns = new String[nsSize * 2];
|
|
70 |
0
| int index = 0;
|
|
71 |
0
| for (int i = 1; i <= nsSize; i++) {
|
|
72 |
0
| String prefix = req.getParameter("prefix" + i);
|
|
73 |
0
| String uri = req.getParameter("uri" + i);
|
|
74 |
0
| if (prefix != null) {
|
|
75 |
0
| ns[index] = prefix;
|
|
76 |
| } else { |
|
77 |
0
| ns[index] = "";
|
|
78 |
| } |
|
79 |
0
| index++;
|
|
80 |
0
| if (uri != null) {
|
|
81 |
0
| ns[index] = uri;
|
|
82 |
| } else { |
|
83 |
0
| ns[index] = "";
|
|
84 |
| } |
|
85 |
0
| index++;
|
|
86 |
| } |
|
87 |
| } |
|
88 |
| |
|
89 |
0
| printQueryForm(output, query, path, ns);
|
|
90 |
0
| output.write("<br />\n");
|
|
91 |
0
| output.flush();
|
|
92 |
| |
|
93 |
0
| if (query.length() > 0) {
|
|
94 |
0
| NamespaceMap nsMap = new NamespaceMap();
|
|
95 |
0
| int index = 0;
|
|
96 |
0
| for (int i = 0; i < nsSize; i++) {
|
|
97 |
0
| nsMap.setNamespace(ns[index++], ns[index++]);
|
|
98 |
| } |
|
99 |
0
| try {
|
|
100 |
0
| NodeSet result = col.queryCollection("XPath", query, nsMap);
|
|
101 |
0
| printQuery(result, output, query, path);
|
|
102 |
| } catch (DBException e) { |
|
103 |
0
| log.error(e);
|
|
104 |
0
| printStartTable("Error while XPath Query", output);
|
|
105 |
0
| printStartSingleTableBox(output);
|
|
106 |
0
| output.write("<b>Error while XPath Query:</b><br />");
|
|
107 |
0
| output.write(e.getMessage());
|
|
108 |
0
| printEndSingleTableBox(output);
|
|
109 |
0
| printEndTable(output);
|
|
110 |
| } |
|
111 |
| } |
|
112 |
| |
|
113 |
0
| finishViewer(output);
|
|
114 |
| } |
|
115 |
| |
|
116 |
0
| protected String getName() {
|
|
117 |
0
| return "xpath";
|
|
118 |
| } |
|
119 |
| |
|
120 |
0
| private void printQuery(NodeSet result, Writer output, String query, String path)
|
|
121 |
| throws IOException { |
|
122 |
| |
|
123 |
0
| String resultTitle = "Result for Query " + query;
|
|
124 |
0
| printStartTable(resultTitle, output);
|
|
125 |
| |
|
126 |
0
| if (!result.hasMoreNodes()) {
|
|
127 |
0
| printStartSingleTableBox(output);
|
|
128 |
0
| output.write("No result for query " + query + "");
|
|
129 |
0
| printEndSingleTableBox(output);
|
|
130 |
| } |
|
131 |
| |
|
132 |
0
| while (result.hasMoreNodes()) {
|
|
133 |
0
| Object node = result.getNextNode();
|
|
134 |
| |
|
135 |
0
| printStartSingleTableBox(output);
|
|
136 |
0
| if (node instanceof Node) {
|
|
137 |
0
| Xml2HtmlWriter.write((Node) node, output);
|
|
138 |
| } else { |
|
139 |
| |
|
140 |
0
| output.write("Node is no Node");
|
|
141 |
| } |
|
142 |
0
| printEndSingleTableBox(output);
|
|
143 |
| } |
|
144 |
0
| printEndTable(output);
|
|
145 |
| } |
|
146 |
| |
|
147 |
0
| private void printQueryForm(Writer output, String query, String path, String[] ns)
|
|
148 |
| throws IOException { |
|
149 |
0
| String formTitle = "XPath Query for Collection " + path;
|
|
150 |
0
| printStartTable(formTitle, output);
|
|
151 |
0
| printSearchFromRow("xpath", query, ns, output);
|
|
152 |
0
| printEndTable(output);
|
|
153 |
| } |
|
154 |
| |
|
155 |
| } |