1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57 package org.apache.maven.sitevalidator;
58
59 import java.io.File;
60 import java.io.IOException;
61
62 import org.apache.tools.ant.Project;
63 import org.dom4j.Element;
64 import org.dom4j.io.XMLWriter;
65 import org.dom4j.tree.DefaultElement;
66 import org.xml.sax.EntityResolver;
67 import org.xml.sax.ErrorHandler;
68 import org.xml.sax.InputSource;
69 import org.xml.sax.SAXParseException;
70
71 /***
72 * ValidatorHandler
73 */
74 class ValidatorHandler implements ErrorHandler, EntityResolver
75 {
76
77 /***
78 * file being validated
79 */
80 private File currentFile = null;
81
82 /***
83 * number of errors found in the current file
84 */
85 private int errorsFound = 0;
86
87 /***
88 * out
89 */
90 private XMLWriter out;
91
92 /***
93 * set XMLWriter for output
94 * @param writer XMLWriter
95 */
96 public void setOut(XMLWriter writer)
97 {
98 this.out = writer;
99 }
100
101 /***
102 * init the validation handler: set up current file and reset errors number
103 * @param file File currently validated
104 */
105 public void init(File file)
106 {
107 currentFile = file;
108 errorsFound = 0;
109 }
110
111 /***
112 * how many errors happened during last parsing?
113 * @return int number of errors
114 */
115 public int getErrors()
116 {
117 return errorsFound;
118 }
119
120 /***
121 * an error occurred: increment errors count and log exception message
122 * @param exception SAXParseException
123 */
124 public void error(SAXParseException exception)
125 {
126 errorsFound++;
127 doLog(exception, Project.MSG_ERR);
128 }
129
130 /***
131 * a fatal error occurred: redirect to error()
132 * @param exception SAXParseException
133 */
134 public final void fatalError(SAXParseException exception)
135 {
136 error(exception);
137 }
138
139 /***
140 * a warning occurred: redirect to error()
141 * @param exception SAXParseException
142 */
143 public final void warning(SAXParseException exception)
144 {
145 error(exception);
146 }
147
148 /***
149 * log event to output
150 * @param e SAXParseException
151 * @param logLevel int
152 */
153 private void doLog(SAXParseException e, int logLevel)
154 {
155 Element error = new DefaultElement("error");
156 error.addAttribute("line", "" + e.getLineNumber());
157 error.addAttribute("col", "" + e.getColumnNumber());
158 error.addAttribute("level", "" + logLevel);
159
160 error.addText(e.getMessage());
161 writeToLog(error);
162 }
163
164 /***
165 * this method is not used to resolve entities, but to log dtd in files
166 * @param publicId public ID in dtd
167 * @param systemId system ID in dtd
168 * @return always null
169 */
170 public InputSource resolveEntity(String publicId, String systemId)
171 {
172
173 if (systemId.indexOf(".dtd") != -1)
174 {
175
176 Element dtd = new DefaultElement("dtd");
177 dtd.addAttribute("publicId", publicId);
178 dtd.addAttribute("systemId", systemId);
179 writeToLog(dtd);
180 }
181 return null;
182 }
183
184 /***
185 * write an element to the xml log file
186 * @param element Element to write
187 */
188 private void writeToLog(Element element)
189 {
190 try
191 {
192 out.write(element);
193 }
194 catch (IOException e)
195 {
196 e.printStackTrace();
197 }
198 }
199 }