1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.xmlhammer.gui.xpath;
23
24 import java.util.ArrayList;
25 import java.util.List;
26
27 import javax.xml.XMLConstants;
28 import javax.xml.xpath.XPathFactory;
29
30 import org.apache.log4j.Logger;
31 import org.xmlhammer.PreferencesHandler;
32 import org.xmlhammer.gui.Page;
33 import org.xmlhammer.gui.ProjectView;
34 import org.xmlhammer.gui.actions.PagePropertiesAction;
35 import org.xmlhammer.gui.output.SourceBuilder;
36 import org.xmlhammer.gui.overview.OverviewNode;
37 import org.xmlhammer.gui.preferences.PropertiesDialog;
38 import org.xmlhammer.gui.util.JAXPSettings;
39 import org.xmlhammer.gui.util.TitleBar;
40 import org.xmlhammer.model.jaxp.Feature;
41 import org.xmlhammer.model.jaxp.JAXPXPathFactory;
42 import org.xmlhammer.model.jaxp.Mapping;
43 import org.xmlhammer.model.jaxp.Settings;
44 import org.xmlhammer.model.tools.xpath.XPath;
45
46 /***
47 * Input Panel.
48 *
49 * Allows to select either one URI, multiple URIs or
50 * a range of files.
51 *
52 * @version $Revision: 1.19 $, $Date: 2007/03/07 21:56:23 $
53 * @author Edwin Dankert <edankert@gmail.com>
54 */
55
56 public class XPathPage extends Page {
57 private static final long serialVersionUID = -6021755010826846525L;
58
59 private PropertiesDialog propertiesDialog = null;
60 private XPathPanel xpathPanel = null;
61
62 /***
63 * @param view the underlying view.
64 */
65 public XPathPage( ProjectView view) {
66 super(view);
67
68 xpathPanel = new XPathPanel(this);
69 setCenterPanel(xpathPanel);
70
71 TitleBar titleBar = new TitleBar("XPath:");
72 titleBar.add(new PagePropertiesAction(this));
73
74 setTitleBar(titleBar);
75 }
76
77 /***
78 * @return the XPath settings.
79 */
80 public XPath getXPath() {
81 return xpathPanel.getXPath();
82 }
83
84 /***
85 * Shows the properties dialog.
86 */
87 public void showPropertiesDialog() {
88 Settings settings = getProjectView().getProject(null).getJAXPSettings();
89
90 if ( propertiesDialog == null) {
91 propertiesDialog = new PropertiesDialog( getProjectView().getProjectsView().getRoot(), false, false, true, false, false);
92 }
93
94 propertiesDialog.setXPathFactory(settings.getJAXPXPathFactory());
95
96 if ( propertiesDialog.open() == PropertiesDialog.OK_OPTION) {
97 settings.setJAXPXPathFactory( propertiesDialog.getXPathFactory());
98 getProjectView().getFieldManager().setPropertiesChanged(true);
99
100 firePreferencesUpdated();
101 }
102 }
103
104 public void setXPath(XPath xpath) {
105 Logger.getLogger(this.getClass()).debug( "setXPath( "+xpath+")");
106 xpathPanel.setXPath(xpath);
107 }
108
109 public void firePreferencesUpdated() {
110 xpathPanel.preferencesUpdated();
111 }
112
113 public String getShortName() {
114 return "XPath";
115 }
116
117 public String getNodeName() {
118 return "XPath";
119 }
120
121
122
123
124 public ArrayList<OverviewNode> getChildNodes() {
125 return xpathPanel.getChildNodes();
126 }
127
128 public String getError() {
129 return null;
130 }
131
132 public void dispose() {
133 xpathPanel.dispose();
134 }
135
136 public void appendSource(StringBuilder builder) {
137 if ( getProjectView().getProject().getJAXPSettings() != null) {
138 builder.append("\n");
139
140 JAXPSettings jaxp = new JAXPSettings(PreferencesHandler.getInstance().getPreferences().getJAXPSettings(), getProjectView().getProject().getJAXPSettings());
141
142 String objectModel = XPathFactory.DEFAULT_OBJECT_MODEL_URI;
143 JAXPXPathFactory.Settings settings = jaxp.getXPathFactorySettings();
144
145 if ( settings.getValue() != null) {
146 builder.append(" System.setProperty(");
147 SourceBuilder.appendConstant(builder, "\""+XPathFactory.DEFAULT_PROPERTY_NAME+"\"");
148 builder.append(", ");
149 builder.append( settings.getValue());
150 builder.append(");\n\n");
151 }
152
153 if ( settings.getObjectModel() != null) {
154 objectModel = settings.getObjectModel();
155 }
156
157 SourceBuilder.appendReserved(builder, " try ");
158 builder.append("{\n");
159 builder.append(" XPathFactory xpathFactory = XPathFactory.newInstance(");
160 SourceBuilder.appendConstant(builder, "\""+objectModel+"\"");
161 builder.append(");\n\n");
162
163 List<Feature> features = jaxp.getXPathFactoryFeatures();
164
165 for ( Feature feature : features) {
166 builder.append(" xpathFactory.setFeature(");
167 SourceBuilder.appendConstant(builder, "\""+feature.getName()+"\"");
168 builder.append(", ");
169 SourceBuilder.appendConstant(builder, feature.isEnabled());
170 builder.append(");\n");
171 }
172
173 builder.append(" XPath xpath = xpathFactory.newXPath();\n\n");
174 builder.append(" NamespaceContextMap mappings = new NamespaceContextMap();\n");
175
176 List<Mapping> mappings = jaxp.getXPathFactoryMappings();
177
178 builder.append(" mappings.put(");
179 SourceBuilder.appendConstant(builder, "\""+XMLConstants.XML_NS_PREFIX+"\"");
180 builder.append(", ");
181 SourceBuilder.appendConstant(builder, "\""+XMLConstants.XML_NS_URI+"\"");
182 builder.append(");\n");
183
184 builder.append(" mappings.put(");
185 SourceBuilder.appendConstant(builder, "\""+XMLConstants.XMLNS_ATTRIBUTE+"\"");
186 builder.append(", ");
187 SourceBuilder.appendConstant(builder, "\""+XMLConstants.XMLNS_ATTRIBUTE_NS_URI+"\"");
188 builder.append(");\n");
189
190 for ( Mapping mapping : mappings) {
191 builder.append(" mappings.put(");
192 SourceBuilder.appendConstant(builder, "\""+mapping.getPrefix()+"\"");
193 builder.append(", ");
194 SourceBuilder.appendConstant(builder, "\""+mapping.getUri()+"\"");
195 builder.append(");\n");
196 }
197
198 builder.append("\n");
199
200 builder.append(" xpath.setNamespaceContext(mappings);\n");
201 builder.append(" XPathExpression expression = xpath.compile(");
202 SourceBuilder.appendConstant(builder, "\""+xpathPanel.getXPathValue()+"\"");
203 builder.append(");\n\n");
204
205 String type = xpathPanel.getReturnType();
206
207 builder.append(" Object node = expression.evaluate(document, XPathConstants.");
208
209 if ( "BOOLEAN".equals( type)) {
210 SourceBuilder.appendConstant(builder, "BOOLEAN");
211 builder.append(");\n");
212 builder.append(" System.");
213 SourceBuilder.appendConstant(builder, "out");
214 builder.append(".println(node.toString());\n");
215
216 } else if ( "NODE".equals( type)) {
217 SourceBuilder.appendConstant(builder, "NODE");
218 builder.append(");\n");
219 builder.append(" System.");
220 SourceBuilder.appendConstant(builder, "out");
221 builder.append(".println(node.toString());\n");
222 } else if ( "NUMBER".equals( type)) {
223 SourceBuilder.appendConstant(builder, "NUMBER");
224 builder.append(");\n");
225 builder.append(" System.");
226 SourceBuilder.appendConstant(builder, "out");
227 builder.append(".println(node.toString());\n");
228 } else if ( "STRING".equals( type)) {
229 SourceBuilder.appendConstant(builder, "STRING");
230 builder.append(");\n");
231 builder.append(" System.");
232 SourceBuilder.appendConstant(builder, "out");
233 builder.append(".println(node.toString());\n");
234 } else {
235 SourceBuilder.appendConstant(builder, "NODESET");
236 builder.append(");\n");
237
238 SourceBuilder.appendReserved(builder, " for ");
239 builder.append("(");
240 SourceBuilder.appendReserved(builder, "int ");
241 builder.append("i = 0; i < ((NodeList)node).getLength(); i++) {\n");
242 builder.append(" System.");
243 SourceBuilder.appendConstant(builder, "out");
244 builder.append(".println(((NodeList)node).item( i).toString());\n");
245 builder.append(" }\n");
246 }
247
248 SourceBuilder.appendCatch(builder, " ", "XPathFactoryConfigurationException");
249 SourceBuilder.appendCatch(builder, " ", "XPathExpressionException");
250 builder.append(" }\n");
251 }
252 }
253
254 public void appendJavaImports(StringBuilder builder) {
255 SourceBuilder.appendImport(builder, "java.util.ArrayList");
256 SourceBuilder.appendImport(builder, "java.util.HashMap");
257 SourceBuilder.appendImport(builder, "java.util.Iterator");
258 SourceBuilder.appendImport(builder, "java.util.List");
259 }
260
261 public void appendJavaxImports(StringBuilder builder) {
262 SourceBuilder.appendImport(builder, "javax.xml.XMLConstants");
263 builder.append("\n");
264 SourceBuilder.appendImport(builder, "javax.xml.namespace.NamespaceContext");
265 builder.append("\n");
266 SourceBuilder.appendImport(builder, "javax.xml.xpath.XPath");
267 SourceBuilder.appendImport(builder, "javax.xml.xpath.XPathConstants");
268 SourceBuilder.appendImport(builder, "javax.xml.xpath.XPathExpression");
269 SourceBuilder.appendImport(builder, "javax.xml.xpath.XPathExpressionException");
270 SourceBuilder.appendImport(builder, "javax.xml.xpath.XPathFactory");
271 SourceBuilder.appendImport(builder, "javax.xml.xpath.XPathFactoryConfigurationException");
272 }
273
274 public void appendDOMImports(StringBuilder builder) {
275 SourceBuilder.appendImport(builder, "org.w3c.dom.NodeList");
276 }
277
278 public void appendNamespaceContextMap(StringBuilder builder) {
279 SourceBuilder.appendReserved(builder, " private ");
280 SourceBuilder.appendReserved(builder, "class");
281 builder.append(" NamespaceContextMap ");
282 SourceBuilder.appendReserved(builder, "extends");
283 builder.append(" HashMap<String,String> ");
284 SourceBuilder.appendReserved(builder, "implements");
285 builder.append(" NamespaceContext {\n");
286
287 SourceBuilder.appendReserved(builder, " public ");
288 builder.append("String getNamespaceURI(String prefix) {\n");
289 builder.append(" String uri = get(prefix);\n");
290 SourceBuilder.appendReserved(builder, " if ");
291 builder.append("(uri != ");
292 SourceBuilder.appendReserved(builder, "null");
293 builder.append(") {\n");
294 SourceBuilder.appendReserved(builder, " return ");
295 builder.append("uri;\n");
296 builder.append(" }\n\n");
297 SourceBuilder.appendReserved(builder, " return");
298 builder.append(" XMLConstants.");
299 SourceBuilder.appendConstant(builder, "NULL_NS_URI");
300 builder.append(";\n");
301 builder.append(" }\n\n");
302
303 SourceBuilder.appendReserved(builder, " public ");
304 builder.append("String getPrefix(String namespaceURI) {\n");
305 SourceBuilder.appendReserved(builder, " for ");
306 builder.append("(String prefix : keySet()) {\n");
307 SourceBuilder.appendReserved(builder, " if ");
308 builder.append("(get(prefix).equals(namespaceURI)) {\n");
309 SourceBuilder.appendReserved(builder, " return ");
310 builder.append("prefix;\n");
311 builder.append(" }\n");
312 builder.append(" }\n\n");
313 SourceBuilder.appendReserved(builder, " return null");
314 builder.append(";\n");
315 builder.append(" }\n\n");
316
317 SourceBuilder.appendReserved(builder, " public");
318 builder.append(" Iterator getPrefixes(String namespaceURI) {\n");
319 builder.append(" List<String> prefixes = ");
320 SourceBuilder.appendReserved(builder, "new");
321 builder.append(" ArrayList<String>();\n\n");
322 SourceBuilder.appendReserved(builder, " for ");
323 builder.append("(String prefix : keySet()) {\n");
324 SourceBuilder.appendReserved(builder, " if ");
325 builder.append("(get(prefix).equals(namespaceURI)) {\n");
326 builder.append(" prefixes.add(prefix);\n");
327 builder.append(" }\n");
328 builder.append(" }\n\n");
329 SourceBuilder.appendReserved(builder, " return ");
330 builder.append("prefixes.iterator();\n");
331 builder.append(" }\n");
332
333 builder.append(" }\n");
334 }
335
336 @Override
337 public String getHelpID() {
338 return getProjectView().getHelpID()+".specification";
339 }
340 }