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.stylesheetvalidator;
23
24 import java.io.IOException;
25 import java.net.URI;
26 import java.util.ArrayList;
27
28 import javax.xml.parsers.ParserConfigurationException;
29 import javax.xml.transform.TransformerException;
30 import javax.xml.transform.TransformerFactory;
31 import javax.xml.transform.stream.StreamSource;
32
33 import org.apache.log4j.Logger;
34 import org.bounce.util.URIUtils;
35 import org.xml.sax.SAXException;
36 import org.xmlhammer.DefaultErrorHandler;
37 import org.xmlhammer.Module;
38 import org.xmlhammer.ResultModel;
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$, $Date$
52 * @author Edwin Dankert <edankert@gmail.com>
53 */
54 public class StylesheetValidatorModule extends Module {
55 private TransformerFactory factory = null;
56
57 /***
58 * Sets-up an XML Validator Module.
59 *
60 * @param preferences the global preferences.
61 * @param project the project specific properties;
62 * @throws ParserConfigurationException
63 * @throws SAXException
64 */
65 public StylesheetValidatorModule(Preferences preferences, Project project, Logger logger) {
66 this(preferences, project, logger, true);
67 }
68
69 public StylesheetValidatorModule(Preferences preferences, Project project, Logger logger, boolean logSettings) {
70 super(preferences, project, logger, logSettings);
71
72 factory = getTransformerFactory();
73 }
74
75 @Override
76 public void execute(StatusModel status, ResultModel result, URI uri) {
77 if ( uri != null) {
78 getLogger().info( "validate stylesheet: "+URIUtils.toString(uri));
79
80 DefaultErrorHandler errorHandler = new DefaultErrorHandler(uri);
81
82 try {
83 factory.setErrorListener(errorHandler);
84 factory.newTransformer(new StreamSource(uri.toString()));
85 } catch (TransformerException e) {
86 try {
87 errorHandler.error(e);
88 } catch (Exception x) {}
89 }
90
91 ArrayList<Problem> list = errorHandler.getProblems();
92 if ( list.size() > 0) {
93 if (status instanceof ValidationStatusModel) {
94 ((ValidationStatusModel)status).setValid(false);
95 }
96
97 for ( Problem problem : list) {
98 if ( problem instanceof Warning) {
99 logWarning(uri, (TransformerException)problem.getException());
100
101 if (result != null) {
102 result.addWarning( uri, (TransformerException)problem.getException());
103 }
104 } else if ( problem instanceof Error) {
105 logError(uri, (TransformerException)problem.getException());
106
107 if (result != null) {
108 result.addError( uri, (TransformerException)problem.getException());
109 }
110 } else if ( problem instanceof Fatal) {
111 logFatal(uri, problem.getException());
112
113 if (result != null) {
114 Exception exception = problem.getException();
115
116 if ( exception instanceof IOException) {
117 result.addFatal( uri, (IOException)problem.getException());
118 } else {
119 result.addFatal( uri, (TransformerException)problem.getException());
120 }
121 }
122 }
123 }
124 } else {
125
126 getLogger().info( "\t[valid] Valid Stylesheet");
127
128 if (result != null) {
129 result.addValid(uri);
130 }
131 }
132 }
133 }
134 }