View Javadoc

1   /*
2    * $Id: XMLValidatorProjectView.java,v 1.21 2007/07/04 19:42:49 edankert Exp $
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;
23  
24  import java.net.URI;
25  
26  import javax.swing.Icon;
27  import javax.swing.ImageIcon;
28  import javax.xml.parsers.ParserConfigurationException;
29  
30  import org.bounce.image.ImageLoader;
31  import org.bounce.util.URIUtils;
32  import org.xml.sax.SAXException;
33  import org.xmlhammer.Module;
34  import org.xmlhammer.PreferencesHandler;
35  import org.xmlhammer.ResultModel;
36  import org.xmlhammer.XMLValidatorModule;
37  import org.xmlhammer.gui.output.SourceBuilder;
38  import org.xmlhammer.gui.parser.ParserPage;
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.model.jaxp.JAXPDocumentBuilderFactory;
43  import org.xmlhammer.model.jaxp.JAXPSAXParserFactory;
44  import org.xmlhammer.model.jaxp.JAXPSchemaFactory;
45  import org.xmlhammer.model.jaxp.Settings;
46  import org.xmlhammer.model.project.Filter;
47  import org.xmlhammer.model.project.Project;
48  
49  /***
50   * The UI representation of a Project.
51   * 
52   * @version $Revision: 1.21 $, $Date: 2007/07/04 19:42:49 $
53   * @author Edwin Dankert <edankert@gmail.com>s
54   */
55  public class XMLValidatorProjectView extends ProjectView {
56  	private static final long serialVersionUID = 4121138017445755189L;
57      private static final ImageIcon ICON = ImageLoader.get().getImage("/org/xmlhammer/gui/icons/etool16/xmlparser.gif");
58  	
59  	private PropertiesDialog propertiesDialog = null;
60      private SourceBuilder source = null;
61  	
62  	/***
63  	 * @param parent the projects view parent.
64  	 * @param uri the uri for the project.
65  	 */
66  	public XMLValidatorProjectView( ProjectsView parent, URI uri) {
67  		super( parent, uri);
68          
69          source = new SourceBuilder();
70  
71          initPages();
72  	}
73  	
74  	/***
75  	 * @return the underlying project.
76  	 */
77      @Override
78  	public Project getProject(URI base) {
79  		return super.getProject(base);
80  	}
81  	
82      @Override
83      protected void createPages() {
84          addPage(createInputPage());
85          addPage(createParserPage());
86      }
87  
88      /***
89  	 * Show the properties dialog.
90  	 */
91      @Override
92  	public void showPropertiesDialog() {
93  		Settings settings = getProject(null).getJAXPSettings();
94  
95  		if ( propertiesDialog == null) {
96  			propertiesDialog = new PropertiesDialog( getProjectsView().getRoot(), true, true, false, false, true);
97  		}
98  
99  		JAXPSAXParserFactory saxFactory = settings.getJAXPSAXParserFactory();
100 		if ( saxFactory == null) {
101 			saxFactory = new JAXPSAXParserFactory();
102 		}
103 
104 		propertiesDialog.setSAXParserFactory(saxFactory);
105 		
106 		JAXPDocumentBuilderFactory documentBuilderFactory = settings.getJAXPDocumentBuilderFactory();
107 		if ( documentBuilderFactory == null) {
108 			documentBuilderFactory = new JAXPDocumentBuilderFactory();
109 		}
110 
111 		propertiesDialog.setDocumentBuilderFactory(documentBuilderFactory);
112 
113 		JAXPSchemaFactory schemaFactory = settings.getJAXPSchemaFactory();
114 		if ( schemaFactory == null) {
115 			schemaFactory = new JAXPSchemaFactory();
116 		}
117 
118 		propertiesDialog.setSchemaFactory(schemaFactory);
119 
120 		if ( propertiesDialog.open() == PropertiesDialog.OK_OPTION) {
121  			settings.setJAXPSAXParserFactory( propertiesDialog.getSAXParserFactory());
122  			settings.setJAXPDocumentBuilderFactory( propertiesDialog.getDocumentBuilderFactory());
123  			settings.setJAXPSchemaFactory( propertiesDialog.getSchemaFactory());
124  		}
125 	}
126 
127     /***
128      * Returns the status bar.
129      * 
130      * @return the status bar.
131      */
132     @Override
133     public StatusBar getStatusBar() {
134         if ( statusBar  == null) {
135             statusBar = new ValidationStatusBar();
136         }
137         
138         return statusBar;
139     }
140 
141     /***
142      * @return the result model.
143      */
144     @Override
145     public ResultModel getResult() {
146         URI dir = null;
147 
148         if (getInputPage() != null) {
149             Filter filter = getInputPage().getInput(null).getFilter();
150             if ( filter != null) {
151                 dir = URIUtils.createURI( filter.getDir());
152             }
153         }
154 
155         return getResultPanel().createModel( dir);
156 	}
157 	
158     @Override
159     public SourceBuilder getSource() {
160         StringBuilder builder = new StringBuilder();
161         
162         if (getParserPage() != null && getInputPage() != null) {
163             builder.append("\n\n");
164             getParserPage().appendJavaImports(builder);
165             builder.append("\n");
166             getParserPage().appendJavaxImports(builder);
167             builder.append("\n");
168             getParserPage().appendSAXImports(builder);
169             builder.append("\n");
170             getParserPage().appendDOMImports(builder);
171             SourceBuilder.appendReserved(builder, "public class ");
172             builder.append("XMLParser {\n\n");
173             SourceBuilder.appendReserved(builder, "    public void ");
174             builder.append("parse(URI uri) {\n");
175             getParserPage().appendSource(builder);
176             builder.append("    }\n\n");
177             ParserPage.appendErrorHandler(builder);
178             builder.append("}\n");
179         }
180 
181         source.reset();
182         source.addCode(builder.toString());
183         source.appendDisclaimer();
184 
185         return source;
186     }
187     
188 	/***
189 	 * @return the module to run.
190 	 */
191     @Override
192 	public Module getModule() {
193         try {
194             return new XMLValidatorModule(PreferencesHandler.getInstance().getPreferences(), getProject(null), getLogger());
195         } catch (SAXException e) {
196             showInitializationErrorMessage(e);
197         } catch (ParserConfigurationException e) {
198             showInitializationErrorMessage(e);
199         }
200         
201         return null;
202 	}
203     
204     @Override
205     public void dispose() {
206         if (getParserPage() != null && getInputPage() != null) {
207             getInputPage().dispose();
208             getParserPage().dispose();
209         }
210     }
211     
212     @Override
213     public Icon getIcon() {
214         return ICON;
215     }
216 
217     @Override
218     public String getHelpID() {
219         return "project.parser";
220     }
221 }