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.io.IOException;
25  import java.net.MalformedURLException;
26  import java.net.URI;
27  import java.util.ArrayList;
28  
29  import javax.xml.parsers.ParserConfigurationException;
30  import javax.xml.transform.Source;
31  import javax.xml.transform.stream.StreamSource;
32  import javax.xml.validation.SchemaFactory;
33  
34  import org.apache.log4j.Logger;
35  import org.bounce.util.URIUtils;
36  import org.xml.sax.SAXException;
37  import org.xml.sax.SAXParseException;
38  import org.xmlhammer.DefaultErrorHandler;
39  import org.xmlhammer.Module;
40  import org.xmlhammer.ResultModel;
41  import org.xmlhammer.DefaultErrorHandler.Error;
42  import org.xmlhammer.DefaultErrorHandler.Fatal;
43  import org.xmlhammer.DefaultErrorHandler.Problem;
44  import org.xmlhammer.DefaultErrorHandler.Warning;
45  import org.xmlhammer.gui.status.StatusModel;
46  import org.xmlhammer.gui.status.ValidationStatusModel;
47  import org.xmlhammer.model.preferences.Preferences;
48  import org.xmlhammer.model.project.Project;
49  
50  /***
51   * Put comment...
52   * 
53   * @version $Revision$, $Date$
54   * @author Edwin Dankert <edankert@gmail.com>
55   */
56  public class SchemaValidatorModule extends Module {
57      private SchemaFactory schemaFactory = null;
58  
59      /***
60       * Sets-up an XML Validator Module.
61       * 
62       * @param preferences the global preferences.
63       * @param project the project specific properties;
64       * @throws ParserConfigurationException 
65       * @throws SAXException 
66       */
67  	public SchemaValidatorModule(Preferences preferences, Project project, Logger logger) {
68          this(preferences, project, logger, true);
69  	}
70      
71      public SchemaValidatorModule(Preferences preferences, Project project, Logger logger, boolean logSettings) {
72          super(preferences, project, logger, logSettings);
73          
74          schemaFactory = getSchemaFactory(project.getSchemaValidator().getLanguage());
75      }
76  
77      /***
78       * Execute the XPath expressio.
79       * @throws MalformedURLException 
80       * @throws ParserConfigurationException 
81       */
82      @Override
83  	public void execute(StatusModel status, ResultModel result, URI uri) {
84  
85  		if ( uri != null) {
86  			// Tell that we're loading classes and parsing, so the time it
87  			// takes to do this doesn't get confused with the time to do
88  			// the actual query and serialization.
89  			getLogger().info( "validate schema: "+URIUtils.toString(uri));
90  
91              DefaultErrorHandler errorHandler = new DefaultErrorHandler(uri);
92  
93              try {
94                  Source source = new StreamSource(uri.toString());
95                  schemaFactory.setErrorHandler(errorHandler);
96                  schemaFactory.newSchema(source);
97  				
98              } catch ( SAXException e) {
99                  if ( e instanceof SAXParseException) {
100                     try {
101                         errorHandler.fatalError( (SAXParseException)e);
102                     } catch ( Exception x) {}
103                 }
104             }
105 
106             ArrayList<Problem> list = errorHandler.getProblems();
107             if ( list.size() > 0) {
108                 if (status instanceof ValidationStatusModel) {
109                     ((ValidationStatusModel)status).setValid(false);
110                 }
111                 
112     			for ( Problem problem : list) {
113                     if ( problem instanceof Warning) {
114                         logWarning(uri, (SAXParseException)problem.getException());
115                         
116                         if (result != null) {
117                             result.addWarning( uri, (SAXParseException)problem.getException());
118                         }
119                     } else if ( problem instanceof Error) {
120                         logError(uri, (SAXParseException)problem.getException());
121                         
122                         if (result != null) {
123                             result.addError(uri, (SAXParseException)problem.getException());
124                         }
125                     } else if ( problem instanceof Fatal) {
126                         logFatal(uri, problem.getException());
127                         
128                         if (result != null) {
129                             Exception exception = problem.getException();
130                             
131                             if ( exception instanceof IOException) {
132                                 result.addFatal( uri, (IOException)problem.getException());
133                             } else {
134                                 result.addFatal( uri, (SAXParseException)problem.getException());
135                             }
136                         }
137                     }
138     			}
139             } else {
140                 getLogger().info( "\t[valid] Valid Schema");
141                 
142                 if (result != null) {
143                     result.addValid( uri);
144                 }
145             }
146 		}
147 	}
148 }