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;
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.DocumentBuilder;
30 import javax.xml.parsers.DocumentBuilderFactory;
31 import javax.xml.parsers.ParserConfigurationException;
32 import javax.xml.parsers.SAXParser;
33
34 import org.apache.log4j.Logger;
35 import org.bounce.util.URIUtils;
36 import org.xml.sax.InputSource;
37 import org.xml.sax.SAXException;
38 import org.xml.sax.SAXParseException;
39 import org.xmlhammer.DefaultErrorHandler.Error;
40 import org.xmlhammer.DefaultErrorHandler.Fatal;
41 import org.xmlhammer.DefaultErrorHandler.Problem;
42 import org.xmlhammer.DefaultErrorHandler.Warning;
43 import org.xmlhammer.gui.status.StatusModel;
44 import org.xmlhammer.gui.status.ValidationStatusModel;
45 import org.xmlhammer.model.preferences.Preferences;
46 import org.xmlhammer.model.project.Project;
47
48 /***
49 * Put comment...
50 *
51 * @version $Revision: 1.17 $, $Date: 2007/07/04 19:42:48 $
52 * @author Edwin Dankert <edankert@gmail.com>
53 */
54 public class XMLValidatorModule extends Module {
55 private SAXParser parser = null;
56 private DocumentBuilder builder = null;
57
58 public XMLValidatorModule(Preferences preferences, Project project, Logger logger, boolean logSettings) throws SAXException, ParserConfigurationException {
59 super(preferences, project, logger, logSettings);
60
61 if ("dom".equals( getProject().getParser().getType())) {
62 DocumentBuilderFactory factory = getDocumentBuilderFactory();
63 builder = factory.newDocumentBuilder();
64 } else {
65 parser = getSAXParser();
66 }
67 }
68
69 /***
70 * Sets-up an XML Validator Module.
71 *
72 * @param preferences the global preferences.
73 * @param project the project specific properties;
74 * @throws ParserConfigurationException
75 * @throws SAXException
76 */
77 public XMLValidatorModule(Preferences preferences, Project project, Logger logger) throws SAXException, ParserConfigurationException {
78 this(preferences, project, logger, true);
79 }
80
81 /***
82 * Execute the XPath expressio.
83 * @throws MalformedURLException
84 * @throws ParserConfigurationException
85 */
86 @Override
87 public void execute(StatusModel status, ResultModel result, URI uri) {
88
89 if ( uri != null) {
90
91
92
93 getLogger().info( "validate: "+URIUtils.toString(uri));
94
95 DefaultErrorHandler errorHandler = new DefaultErrorHandler(uri);
96 InputSource in = new InputSource( uri.toString());
97
98 try {
99
100 if ("dom".equals(getProject().getParser().getType())) {
101 builder.setErrorHandler(errorHandler);
102 builder.setEntityResolver(getCatalogResolver());
103 builder.parse(in);
104 } else {
105 parser.getXMLReader().setErrorHandler(errorHandler);
106 parser.getXMLReader().setEntityResolver(getCatalogResolver());
107 parser.getXMLReader().parse(in);
108 }
109
110 } catch (SAXException e) {
111
112 } catch (IOException e) {
113 errorHandler.fatalError(e);
114 }
115
116 ArrayList<Problem> list = errorHandler.getProblems();
117 if ( list.size() > 0) {
118 if (status instanceof ValidationStatusModel) {
119 ((ValidationStatusModel)status).setValid(false);
120 }
121
122 for ( Problem problem : list) {
123 if ( problem instanceof Warning) {
124 logWarning(uri, (SAXParseException)problem.getException());
125
126 if (result != null) {
127 result.addWarning( uri, (SAXParseException)problem.getException());
128 }
129 } else if ( problem instanceof Error) {
130 logError(uri, (SAXParseException)problem.getException());
131
132 if (result != null) {
133 result.addError( uri, (SAXParseException)problem.getException());
134 }
135 } else if ( problem instanceof Fatal) {
136 logFatal(uri, problem.getException());
137
138 if (result != null) {
139 Exception exception = problem.getException();
140 if ( exception instanceof IOException) {
141 result.addFatal( uri, (IOException)problem.getException());
142 } else {
143 result.addFatal( uri, (SAXParseException)problem.getException());
144 }
145 }
146 }
147 }
148 } else {
149
150 getLogger().info( "\t[valid] Valid Document");
151 if (result != null) {
152 result.addValid( uri);
153 }
154 }
155 }
156 }
157 }