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.stylesheetvalidator;
23  
24  import java.net.URI;
25  import java.util.List;
26  
27  import javax.swing.Icon;
28  import javax.swing.ImageIcon;
29  
30  import org.apache.log4j.Logger;
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.preferences.PropertiesDialog;
40  import org.xmlhammer.gui.status.StatusBar;
41  import org.xmlhammer.gui.status.ValidationStatusBar;
42  import org.xmlhammer.gui.util.JAXPSettings;
43  import org.xmlhammer.model.jaxp.Attribute;
44  import org.xmlhammer.model.jaxp.Feature;
45  import org.xmlhammer.model.jaxp.JAXPTransformerFactory;
46  import org.xmlhammer.model.jaxp.Settings;
47  import org.xmlhammer.model.project.Filter;
48  import org.xmlhammer.model.project.Project;
49  
50  /***
51   * The UI representation of a Project.
52   * 
53   * @version $Revision$, $Date$
54   * @author Edwin Dankert <edankert@gmail.com>s
55   */
56  public class StylesheetValidatorProjectView extends ProjectView {
57  	private static final long serialVersionUID = 4121138017445755189L;
58      private static final ImageIcon ICON = ImageLoader.get().getImage("/org/xmlhammer/gui/icons/etool16/stylesheetvalidator.gif");
59  	
60  	private PropertiesDialog propertiesDialog = null;
61      private SourceBuilder source = null;
62  	
63  	/***
64  	 * @param parent the projects view parent.
65  	 * @param uri the uri for the project.
66  	 */
67  	public StylesheetValidatorProjectView( ProjectsView parent, URI uri) {
68  		super( parent, uri);
69          
70          source = new SourceBuilder();
71  
72          initPages();
73  	}
74  	
75      protected void createPages() {
76          addPage(createInputPage());
77      }
78  
79      /***
80  	 * Show the properties dialog.
81  	 */
82  	public void showPropertiesDialog() {
83  		Settings settings = getProject(null).getJAXPSettings();
84  
85  		if (propertiesDialog == null) {
86  			propertiesDialog = new PropertiesDialog(getProjectsView().getRoot(), false, false, false, true, false);
87  		}
88  
89          JAXPTransformerFactory transformerFactory = settings.getJAXPTransformerFactory();
90          if (transformerFactory == null) {
91              transformerFactory = new JAXPTransformerFactory();
92          }
93  
94          propertiesDialog.setTransformerFactory(transformerFactory);
95  
96  		if ( propertiesDialog.open() == PropertiesDialog.OK_OPTION) {
97              settings.setJAXPTransformerFactory(propertiesDialog.getTransformerFactory());
98  
99              updateSchemaLanguages();
100  		}
101 	}
102 
103     /***
104      * Returns the status bar.
105      * 
106      * @return the status bar.
107      */
108     public StatusBar getStatusBar() {
109         if ( statusBar  == null) {
110             statusBar = new ValidationStatusBar();
111         }
112         
113         return statusBar;
114     }
115 
116     public void setProject( Project project) {
117         this.project = project;
118 
119         if (getInputPage() != null) {
120             if (project != null) {
121                 getInputPage().setInput(getURI(), project.getInput());
122             } else {
123                 getInputPage().setInput(getURI(), null);
124             }
125         }
126         
127         getFieldManager().setChanged(false);
128         getUndoManager().discardAllEdits();
129     }
130 
131     protected void updateSchemaLanguages() {
132         Logger.getLogger(getClass()).debug("updateSchemaLanguages()");
133     }
134 
135     /***
136      * @return the underlying project, setting input and parser values .
137      */
138     public Project getProject(URI base) {
139         if ( project == null) {
140             project = new Project();
141         }
142 
143         if (getInputPage() != null) {
144             project.setInput(getInputPage().getInput(base));
145         }
146         
147         return project;
148     }
149 
150     /***
151      * @return the result model.
152      */
153     public ResultModel getResult() {
154         URI dir = null;
155 
156         if (getInputPage() != null) {
157             Filter filter = getInputPage().getInput(null).getFilter();
158             if ( filter != null) {
159                 dir = URIUtils.createURI( filter.getDir());
160             }
161         }
162 
163         return getResultPanel().createModel( dir);
164 	}
165 	
166     public SourceBuilder getSource() {
167         StringBuilder builder = new StringBuilder();
168         
169         if (getInputPage() != null) {
170             builder.append("\n\n");
171             appendJavaImports(builder);
172             builder.append("\n");
173             appendTransformerImports(builder);
174             builder.append("\n");
175 
176             SourceBuilder.appendReserved(builder, "public class ");
177             builder.append("XMLStylesheetValidator {\n\n");
178             SourceBuilder.appendReserved(builder, "    public void ");
179             builder.append("validate(URI uri) {\n");
180             
181             appendTransformerSource(builder);
182             
183             builder.append("    }\n\n");
184             appendErrorListener(builder);
185             builder.append("}\n");
186         }
187 
188         source.reset();
189         source.addCode(builder.toString());
190         source.appendDisclaimer();
191 
192         return source;
193     }
194     
195     public void appendJavaImports(StringBuilder builder) {
196         SourceBuilder.appendImport(builder, "java.net.URI");
197     }
198 
199     public void appendTransformerImports(StringBuilder builder) {
200         SourceBuilder.appendImport(builder, "javax.xml.transform.ErrorListener");
201         SourceBuilder.appendImport(builder, "javax.xml.transform.TransformerException");
202         SourceBuilder.appendImport(builder, "javax.xml.transform.TransformerFactory");
203         SourceBuilder.appendImport(builder, "javax.xml.transform.stream.StreamSource");
204     }
205 
206     public void appendTransformerSource(StringBuilder builder) {
207         if (getProject().getJAXPSettings() != null) {
208             builder.append("\n");
209 
210             JAXPSettings jaxp = new JAXPSettings(PreferencesHandler.getInstance().getPreferences().getJAXPSettings(), getProject().getJAXPSettings());
211     
212             JAXPTransformerFactory.Settings settings = jaxp.getTransformerFactorySettings();
213             
214             if (settings.getValue() != null) {
215                 builder.append("        System.setProperty(");
216                 SourceBuilder.appendConstant(builder, "\"javax.xml.transform.TransformerFactory\"");
217                 builder.append(", ");
218                 SourceBuilder.appendConstant(builder, "\""+settings.getValue()+"\"");
219                 builder.append(");\n\n");
220             }
221             
222             SourceBuilder.appendReserved(builder, "        try ");
223             builder.append("{\n");
224             builder.append("            TransformerFactory transformerFactory = TransformerFactory.newInstance();\n\n");
225             builder.append("            transformerFactory.setErrorListener(");
226             SourceBuilder.appendReserved(builder, "new ");
227             builder.append("SimpleErrorListener());\n");
228 
229             List<Feature> features = jaxp.getTransformerFactoryFeatures();
230 
231             for (Feature feature : features) {
232                 builder.append("            transformerFactory.setFeature(");
233                 SourceBuilder.appendConstant(builder, "\""+feature.getName()+"\"");
234                 builder.append(", ");
235                 SourceBuilder.appendConstant(builder, feature.isEnabled());
236                 builder.append(");\n");
237             }
238 
239             List<Attribute> attributes = jaxp.getTransformerFactoryAttributes();
240 
241             for (Attribute attribute : attributes) {
242                 builder.append("            transformerFactory.setAttribute(");
243                 SourceBuilder.appendConstant(builder, "\""+attribute.getName()+"\"");
244                 builder.append(", ");
245                 SourceBuilder.appendConstant(builder, "\""+attribute.getValue()+"\"");
246                 builder.append(");\n");
247             }
248 
249             builder.append("            transformerFactory.newTransformer(");
250             SourceBuilder.appendReserved(builder, "new");
251             builder.append(" StreamSource(uri.toString()));\n");
252             
253             SourceBuilder.appendCatch(builder, "        ", "TransformerException");
254             builder.append("        }\n");
255         }
256     }
257     
258     public static void appendErrorListener(StringBuilder builder) {
259         SourceBuilder.appendReserved(builder, "    public ");
260         SourceBuilder.appendReserved(builder, "class ");
261         builder.append("SimpleErrorListener ");
262         SourceBuilder.appendReserved(builder, "implements");
263         builder.append(" ErrorListener {\n");
264 
265         appendErrorListenerMethod(builder, "warning");
266         builder.append("\n");
267         appendErrorListenerMethod(builder, "error");
268         builder.append("\n");
269         appendErrorListenerMethod(builder, "fatalError");
270         builder.append("    }\n");
271     }
272 
273     private static void appendErrorListenerMethod(StringBuilder builder, String method) {
274         SourceBuilder.appendReserved(builder, "        public void ");
275         builder.append(method);
276         builder.append("(TransformerException e) ");
277         SourceBuilder.appendReserved(builder, "throws");
278         builder.append(" TransformerException {\n");
279         builder.append("            System.");
280         SourceBuilder.appendConstant(builder, "out");
281         builder.append(".println(e.getMessage());\n");
282         builder.append("        }\n");
283     }
284     
285 	/***
286 	 * @return the module to run.
287 	 */
288 	public Module getModule() {
289         return new StylesheetValidatorModule(PreferencesHandler.getInstance().getPreferences(), getProject(null), getLogger());
290 	}
291     
292     public void dispose() {
293         if (getInputPage() != null) {
294             getInputPage().dispose();
295         }
296     }
297     
298     public Icon getIcon() {
299         return ICON;
300     }
301 
302     @Override
303     public String getHelpID() {
304         return "project.style";
305     }
306 }