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.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
87
88
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 }