View Javadoc

1   /*
2    * $Id$
3    *
4    * The contents of this file are subject to the Mozilla Public License 
5    * Version 1.1 (the "License"); you may not use this file except in 
6    * compliance with the License. You may obtain a copy of the License at 
7    * http://www.mozilla.org/MPL/ 
8    *
9    * Software distributed under the License is distributed on an "AS IS" basis, 
10   * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
11   * for the specific language governing rights and limitations under the License.
12   *
13   * The Original Code is XML Hammer code. (org.xmlhammer.*)
14   *
15   * The Initial Developer of the Original Code is Edwin Dankert. Portions created 
16   * by the Initial Developer are Copyright (C) 2005 - 2006 the Initial Developer. 
17   * All Rights Reserved.
18   *
19   * Contributor(s): Edwin Dankert <edankert@gmail.com>
20   */
21  
22  package org.xmlhammer.gui.schemavalidator;
23  
24  import java.net.URI;
25  import java.util.ArrayList;
26  import java.util.List;
27  
28  import javax.swing.Icon;
29  import javax.swing.ImageIcon;
30  
31  import org.bounce.image.ImageLoader;
32  import org.bounce.util.URIUtils;
33  import org.xmlhammer.Module;
34  import org.xmlhammer.PreferencesHandler;
35  import org.xmlhammer.ResultModel;
36  import org.xmlhammer.gui.ProjectView;
37  import org.xmlhammer.gui.ProjectsView;
38  import org.xmlhammer.gui.output.SourceBuilder;
39  import org.xmlhammer.gui.parser.ParserPage;
40  import org.xmlhammer.gui.preferences.PropertiesDialog;
41  import org.xmlhammer.gui.status.StatusBar;
42  import org.xmlhammer.gui.status.ValidationStatusBar;
43  import org.xmlhammer.gui.util.JAXPSettings;
44  import org.xmlhammer.model.jaxp.Feature;
45  import org.xmlhammer.model.jaxp.JAXPSAXParserFactory;
46  import org.xmlhammer.model.jaxp.JAXPSchemaFactory;
47  import org.xmlhammer.model.jaxp.Property;
48  import org.xmlhammer.model.jaxp.SchemaFactoryProperty;
49  import org.xmlhammer.model.jaxp.Settings;
50  import org.xmlhammer.model.project.Filter;
51  import org.xmlhammer.model.project.Project;
52  
53  /***
54   * The UI representation of a Project.
55   * 
56   * @version $Revision$, $Date$
57   * @author Edwin Dankert <edankert@gmail.com>s
58   */
59  public class SchemaValidatorProjectView extends ProjectView {
60  	private static final long serialVersionUID = 4121138017445755189L;
61      private static final ImageIcon ICON = ImageLoader.get().getImage("/org/xmlhammer/gui/icons/etool16/schemavalidator.gif");
62  	
63  	private PropertiesDialog propertiesDialog = null;
64      private SourceBuilder source = null;
65      private SchemaPage schemaPage = null;
66  	
67  	/***
68  	 * @param parent the projects view parent.
69  	 * @param uri the uri for the project.
70  	 */
71  	public SchemaValidatorProjectView( ProjectsView parent, URI uri) {
72  		super( parent, uri);
73          
74          source = new SourceBuilder();
75  
76          initPages();
77  	}
78  	
79      protected void createPages() {
80          addPage(createInputPage());
81          addPage(createSchemaPage());
82      }
83  
84      protected SchemaPage createSchemaPage() {
85          schemaPage = new SchemaPage(this);
86   
87          return schemaPage;
88      }
89  
90      protected SchemaPage getSchemaPage() {
91          return schemaPage;
92      }
93  
94      /***
95  	 * Show the properties dialog.
96  	 */
97  	public void showPropertiesDialog() {
98  		Settings settings = getProject(null).getJAXPSettings();
99  
100 		if (propertiesDialog == null) {
101 			propertiesDialog = new PropertiesDialog(getProjectsView().getRoot(), false, false, false, false, true);
102 		}
103 
104 //		JAXPSAXParserFactory saxFactory = settings.getJAXPSAXParserFactory();
105 //		if (saxFactory == null) {
106 //			saxFactory = new JAXPSAXParserFactory();
107 //		}
108 //
109 //		propertiesDialog.setSAXParserFactory(saxFactory);
110 		
111 		JAXPSchemaFactory schemaFactory = settings.getJAXPSchemaFactory();
112 		if (schemaFactory == null) {
113 			schemaFactory = new JAXPSchemaFactory();
114 		}
115 
116 		propertiesDialog.setSchemaFactory(schemaFactory);
117 
118 		if ( propertiesDialog.open() == PropertiesDialog.OK_OPTION) {
119 // 			settings.setJAXPSAXParserFactory(propertiesDialog.getSAXParserFactory());
120  			settings.setJAXPSchemaFactory(propertiesDialog.getSchemaFactory());
121 
122             updateSchemaLanguages();
123  		}
124 	}
125 
126     /***
127      * Returns the status bar.
128      * 
129      * @return the status bar.
130      */
131     public StatusBar getStatusBar() {
132         if ( statusBar  == null) {
133             statusBar = new ValidationStatusBar();
134         }
135         
136         return statusBar;
137     }
138 
139     public void setProject( Project project) {
140         this.project = project;
141 
142         updateSchemaLanguages();
143 
144         if (getSchemaPage() != null && getInputPage() != null) {
145             if (project != null) {
146                 getInputPage().setInput(getURI(), project.getInput());
147                 getSchemaPage().setSchemaValidator(project.getSchemaValidator());
148             } else {
149                 getInputPage().setInput(getURI(), null);
150                 getSchemaPage().setSchemaValidator(null);
151             }
152         }
153         
154         getFieldManager().setChanged(false);
155         getUndoManager().discardAllEdits();
156     }
157 
158     /***
159      * @return the underlying project, setting input and parser values .
160      */
161     public Project getProject(URI base) {
162         if ( project == null) {
163             project = new Project();
164         }
165 
166         if (getSchemaPage() != null && getInputPage() != null) {
167             project.setInput(getInputPage().getInput(base));
168             project.setSchemaValidator(getSchemaPage().getSchemaValidator());
169         }
170         
171         return project;
172     }
173 
174     /***
175      * @return the result model.
176      */
177     public ResultModel getResult() {
178         URI dir = null;
179 
180         if (getInputPage() != null) {
181             Filter filter = getInputPage().getInput(null).getFilter();
182             if ( filter != null) {
183                 dir = URIUtils.createURI( filter.getDir());
184             }
185         }
186 
187         return getResultPanel().createModel( dir);
188 	}
189 	
190     public SourceBuilder getSource() {
191         StringBuilder builder = new StringBuilder();
192         
193         if (getSchemaPage() != null && getInputPage() != null) {
194             builder.append("\n\n");
195             SourceBuilder.appendImport(builder, "java.net.URI");
196             builder.append("\n");
197             SourceBuilder.appendImport(builder, "javax.xml.transform.stream.StreamSource");
198             builder.append("\n");
199             SourceBuilder.appendImport(builder, "javax.xml.validation.SchemaFactory");
200             builder.append("\n");
201             SourceBuilder.appendImport(builder, "org.xml.sax.ErrorHandler");
202             SourceBuilder.appendImport(builder, "org.xml.sax.SAXException");
203             SourceBuilder.appendImport(builder, "org.xml.sax.SAXParseException");
204             builder.append("\n");
205             SourceBuilder.appendReserved(builder, "public class ");
206             builder.append("SchemaValidator {\n\n");
207             SourceBuilder.appendReserved(builder, "    public void ");
208             builder.append("validate(URI uri) {\n");
209             appendSchema(builder);
210             builder.append("    }\n\n");
211             ParserPage.appendErrorHandler(builder);
212             builder.append("}\n");
213         }
214 
215         source.reset();
216         source.addCode(builder.toString());
217         source.appendDisclaimer();
218 
219         return source;
220     }
221     
222     private void appendSchema(StringBuilder builder) {
223         JAXPSettings settings = new JAXPSettings(PreferencesHandler.getInstance().getPreferences().getJAXPSettings(), getProject().getJAXPSettings());
224 
225         String schemaLanguage = getSchemaPage().getSchemaLanguage();
226 
227         List<SchemaFactoryProperty> languages = settings.getSchemaFactoryLanguageProperties();
228         
229         for ( SchemaFactoryProperty language : languages) {
230             String factory = language.getFactory();
231             String lang = language.getLanguage();
232             
233             if ( lang != null && lang.equals(schemaLanguage) && factory != null && factory.trim().length() > 0) {
234                 builder.append("        System.setProperty(");
235                 SourceBuilder.appendConstant(builder, "\"javax.xml.validation.SchemaFactory:"+lang+"\"");
236                 builder.append(", ");
237                 SourceBuilder.appendConstant(builder, "\""+factory+"\"");
238                 builder.append(");\n\n");
239             }
240         }
241         
242         JAXPSAXParserFactory.Settings saxParserSettings = settings.getSAXParserSettings();
243         
244         if (saxParserSettings.getValue() != null) {
245             builder.append("        System.setProperty(");
246             SourceBuilder.appendConstant(builder, "\"javax.xml.parsers.SAXParserFactory\"");
247             builder.append(", ");
248             SourceBuilder.appendConstant(builder, "\""+saxParserSettings.getValue()+"\"");
249             builder.append(");\n\n");
250         }
251 
252         builder.append("        SchemaFactory schemaFactory = SchemaFactory.newInstance(");
253         SourceBuilder.appendConstant(builder, "\""+schemaLanguage+"\"");
254         builder.append(");\n\n");
255 
256         List<Feature> features = settings.getSchemaFactoryFeatures();
257 
258         for ( Feature feature : features) {
259             builder.append("        schemaFactory.setFeature(");
260             SourceBuilder.appendConstant(builder, "\""+feature.getName()+"\"");
261             builder.append(", ");
262             SourceBuilder.appendConstant(builder, feature.isEnabled());
263             builder.append(");\n");
264         }
265         
266         if (features.size() > 0) {
267             builder.append("\n");
268         }
269         
270         List<Property> properties = settings.getSchemaFactoryProperties();
271 
272         for ( Property property : properties) {
273             builder.append("        schemaFactory.setProperty(");
274             SourceBuilder.appendConstant(builder, "\""+property.getName()+"\"");
275             builder.append(", ");
276             SourceBuilder.appendConstant(builder, "\""+property.getValue()+"\"");
277             builder.append(");\n");
278         }
279 
280         if (properties.size() > 0) {
281             builder.append("\n");
282         }
283 
284         builder.append("        schemaFactory.setErrorHandler(");
285         SourceBuilder.appendReserved(builder, "new ");
286         builder.append("SimpleErrorHandler());\n\n");
287 
288         SourceBuilder.appendReserved(builder, "        try ");
289         builder.append("{\n");
290         builder.append("            schemaFactory.newSchema(");
291         SourceBuilder.appendReserved(builder, "new");
292         builder.append(" StreamSource(uri.toString()));\n");
293         SourceBuilder.appendCatch(builder, "        ", "SAXException");
294         builder.append("        }\n\n");
295     }
296     
297 	/***
298 	 * @return the module to run.
299 	 */
300 	public Module getModule() {
301         return new SchemaValidatorModule(PreferencesHandler.getInstance().getPreferences(), getProject(null), getLogger());
302 	}
303     
304     public void dispose() {
305         if (getInputPage() != null) {
306             getInputPage().dispose();
307         }
308 
309         if (getSchemaPage() != null) {
310             getSchemaPage().dispose();
311         }
312     }
313     
314     protected void updateSchemaLanguages() {
315         Settings settings = getProject().getJAXPSettings();
316         ArrayList<String> languages = new ArrayList<String>();
317 
318         if (settings.getJAXPSchemaFactory().getSchemaFactoryProperties() != null) {
319             List<SchemaFactoryProperty> properties = settings.getJAXPSchemaFactory().getSchemaFactoryProperties().getSchemaFactoryProperty();
320             for ( SchemaFactoryProperty property : properties) {
321                 languages.add( property.getLanguage());
322             }
323         } else {
324             settings = PreferencesHandler.getInstance().getPreferences().getJAXPSettings();
325 
326             List<SchemaFactoryProperty> properties = settings.getJAXPSchemaFactory().getSchemaFactoryProperties().getSchemaFactoryProperty();
327             for (SchemaFactoryProperty property : properties) {
328                 languages.add( property.getLanguage());
329             }
330         }
331 
332         getSchemaPage().setSchemaLanguages(languages);
333     }
334 
335     public Icon getIcon() {
336         return ICON;
337     }
338 
339     @Override
340     public String getHelpID() {
341         return "project.schema";
342     }
343 }