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.net.URI;
25 import java.net.URISyntaxException;
26
27 import javax.swing.Icon;
28 import javax.swing.ImageIcon;
29 import javax.xml.parsers.ParserConfigurationException;
30 import javax.xml.xpath.XPathExpressionException;
31
32 import org.bounce.image.ImageLoader;
33 import org.bounce.util.URIUtils;
34 import org.xml.sax.SAXException;
35 import org.xmlhammer.Module;
36 import org.xmlhammer.PreferencesHandler;
37 import org.xmlhammer.ResultModel;
38 import org.xmlhammer.gui.ProjectView;
39 import org.xmlhammer.gui.ProjectsView;
40 import org.xmlhammer.gui.output.SourceBuilder;
41 import org.xmlhammer.gui.parser.ParserPage;
42 import org.xmlhammer.gui.preferences.PropertiesDialog;
43 import org.xmlhammer.model.jaxp.JAXPDocumentBuilderFactory;
44 import org.xmlhammer.model.jaxp.JAXPSAXParserFactory;
45 import org.xmlhammer.model.jaxp.JAXPSchemaFactory;
46 import org.xmlhammer.model.jaxp.JAXPXPathFactory;
47 import org.xmlhammer.model.jaxp.Settings;
48 import org.xmlhammer.model.project.Filter;
49 import org.xmlhammer.model.project.Project;
50
51 /***
52 * The UI representation of a Project.
53 *
54 * @version $Revision$, $Date$
55 * @author Edwin Dankert <edankert@gmail.com>
56 */
57 public class XPathSearchProjectView extends ProjectView {
58 private static final long serialVersionUID = 4121138017445755189L;
59
60 private static final ImageIcon ICON = ImageLoader.get().getImage("/org/xmlhammer/gui/icons/etool16/xpathsearch.gif");
61
62 private PropertiesDialog propertiesDialog = null;
63
64 private XPathPage xpathPage = null;
65 private SourceBuilder source = null;
66
67 /***
68 * @param parent the projects view parent.
69 * @param uri the uri for the project.
70 */
71 public XPathSearchProjectView( ProjectsView parent, URI uri) {
72 super( parent, uri);
73
74 source = new SourceBuilder();
75
76 initPages();
77 }
78
79 protected ParserPage createParserPage() {
80 parserPage = new ParserPage(this, false);
81
82 return parserPage;
83 }
84
85 protected void createPages() {
86 addPage(createInputPage());
87 addPage(createParserPage());
88 addPage(createXPathPage());
89 }
90
91 private XPathPage createXPathPage() {
92 xpathPage = new XPathPage( this);
93
94 return xpathPage;
95 }
96
97 private XPathPage getXPathPage() {
98 return xpathPage;
99 }
100
101 /***
102 * @return the underlying project.
103 */
104 public Project getProject(URI base) {
105 Project project = super.getProject(base);
106
107 if (getXPathPage() != null) {
108 project.setXPath(getXPathPage().getXPath());
109 }
110
111 return project;
112 }
113
114 public Icon getIcon() {
115 return ICON;
116 }
117
118 /***
119 * Show the properties dialog.
120 */
121 public void showPropertiesDialog() {
122 Settings settings = getProject(null).getJAXPSettings();
123
124 if ( propertiesDialog == null) {
125 propertiesDialog = new PropertiesDialog( getProjectsView().getRoot(), true, true, true, false, true);
126 }
127
128 JAXPSAXParserFactory saxFactory = settings.getJAXPSAXParserFactory();
129 if ( saxFactory == null) {
130 saxFactory = new JAXPSAXParserFactory();
131 }
132
133 propertiesDialog.setSAXParserFactory(saxFactory);
134
135 JAXPDocumentBuilderFactory documentBuilderFactory = settings.getJAXPDocumentBuilderFactory();
136 if ( documentBuilderFactory == null) {
137 documentBuilderFactory = new JAXPDocumentBuilderFactory();
138 }
139
140 propertiesDialog.setDocumentBuilderFactory(documentBuilderFactory);
141
142 JAXPXPathFactory xpathFactory = settings.getJAXPXPathFactory();
143 if ( xpathFactory == null) {
144 xpathFactory = new JAXPXPathFactory();
145 }
146
147 propertiesDialog.setXPathFactory(xpathFactory);
148
149 JAXPSchemaFactory schemaFactory = settings.getJAXPSchemaFactory();
150 if ( schemaFactory == null) {
151 schemaFactory = new JAXPSchemaFactory();
152 }
153
154 propertiesDialog.setSchemaFactory(schemaFactory);
155
156 if ( propertiesDialog.open() == PropertiesDialog.OK_OPTION) {
157 settings.setJAXPSAXParserFactory( propertiesDialog.getSAXParserFactory());
158 settings.setJAXPDocumentBuilderFactory( propertiesDialog.getDocumentBuilderFactory());
159 settings.setJAXPXPathFactory( propertiesDialog.getXPathFactory());
160 settings.setJAXPSchemaFactory( propertiesDialog.getSchemaFactory());
161
162 getXPathPage().firePreferencesUpdated();
163
164 updateSchemaLanguages();
165 }
166 }
167
168 public void firePreferencesUpdated() {
169 super.firePreferencesUpdated();
170 getXPathPage().firePreferencesUpdated();
171 }
172
173 public SourceBuilder getSource() {
174 StringBuilder builder = new StringBuilder();
175
176 if (getXPathPage() != null && getParserPage() != null) {
177 builder.append("\n\n");
178 getParserPage().appendJavaImports(builder);
179 getXPathPage().appendJavaImports(builder);
180 builder.append("\n");
181 getParserPage().appendJavaxImports(builder);
182 getXPathPage().appendJavaxImports(builder);
183 builder.append("\n");
184 getParserPage().appendSAXImports(builder);
185 builder.append("\n");
186 getXPathPage().appendDOMImports(builder);
187 getParserPage().appendDOMImports(builder);
188 SourceBuilder.appendReserved(builder, "public class ");
189 builder.append("XPathSearch {\n\n");
190 SourceBuilder.appendReserved(builder, " public void ");
191 builder.append("search(URI uri) {\n");
192 getParserPage().appendSource(builder);
193 getXPathPage().appendSource(builder);
194 builder.append(" }\n\n");
195 ParserPage.appendErrorHandler(builder);
196 builder.append("\n");
197 getXPathPage().appendNamespaceContextMap(builder);
198 builder.append("}\n");
199 }
200
201 source.reset();
202 source.addCode(builder.toString());
203 source.appendDisclaimer();
204
205 return source;
206 }
207
208 /***
209 * @return the result model.
210 */
211 public ResultModel getResult() {
212 URI dir = null;
213
214 Filter filter = getInputPage().getInput(null).getFilter();
215 if ( filter != null) {
216 try {
217 dir = new URI("file", URIUtils.createURI(filter.getDir()).getSchemeSpecificPart(), null);
218 } catch (URISyntaxException e) {
219
220 e.printStackTrace();
221 }
222 }
223
224 return getResultPanel().createModel( dir);
225 }
226
227 /***
228 * @return the module to run.
229 */
230 public Module getModule() {
231 XPathSearchModule module = null;
232 try {
233 module = new XPathSearchModule(PreferencesHandler.getInstance().getPreferences(), getProject(null), getLogger());
234 } catch ( XPathExpressionException e) {
235 showInitializationErrorMessage(e);
236 } catch (ParserConfigurationException e) {
237 showInitializationErrorMessage(e);
238 } catch (SAXException e) {
239 showInitializationErrorMessage(e);
240 }
241
242 return module;
243 }
244
245 public void dispose() {
246 getInputPage().dispose();
247 getParserPage().dispose();
248 getXPathPage().dispose();
249 }
250
251 /***
252 * Set the project.
253 *
254 * @param project the project.
255 */
256 public void setProject( Project project) {
257 this.project = project;
258
259 if ( project != null) {
260 getXPathPage().setXPath( project.getXPath());
261 } else {
262 getXPathPage().setXPath( null);
263 }
264
265 super.setProject( project);
266 }
267
268 @Override
269 public String getHelpID() {
270 return "project.xpath";
271 }
272 }