|
|||||||||||||||||||
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
ConfigValidator.java | 92.9% | 93.9% | 100% | 94.2% |
|
1 |
/*
|
|
2 |
* Copyright (C) The Spice Group. All rights reserved.
|
|
3 |
*
|
|
4 |
* This software is published under the terms of the Spice
|
|
5 |
* Software License version 1.1, a copy of which has been included
|
|
6 |
* with this distribution in the LICENSE.txt file.
|
|
7 |
*/
|
|
8 |
package org.codehaus.spice.configkit;
|
|
9 |
|
|
10 |
import java.io.IOException;
|
|
11 |
import java.io.InputStream;
|
|
12 |
import java.util.ArrayList;
|
|
13 |
import java.util.List;
|
|
14 |
import javax.xml.parsers.ParserConfigurationException;
|
|
15 |
import javax.xml.parsers.SAXParser;
|
|
16 |
import javax.xml.parsers.SAXParserFactory;
|
|
17 |
import org.iso_relax.verifier.Schema;
|
|
18 |
import org.iso_relax.verifier.Verifier;
|
|
19 |
import org.iso_relax.verifier.VerifierConfigurationException;
|
|
20 |
import org.iso_relax.verifier.VerifierFilter;
|
|
21 |
import org.w3c.dom.Node;
|
|
22 |
import org.xml.sax.ContentHandler;
|
|
23 |
import org.xml.sax.EntityResolver;
|
|
24 |
import org.xml.sax.ErrorHandler;
|
|
25 |
import org.xml.sax.InputSource;
|
|
26 |
import org.xml.sax.SAXException;
|
|
27 |
import org.xml.sax.XMLReader;
|
|
28 |
|
|
29 |
/**
|
|
30 |
* The ConfigValidator is responsible for validating an XML configuration source
|
|
31 |
* according to a specified Schema. The schema is defined when the validator is
|
|
32 |
* created. Note that the user should get a reference to the validator via the
|
|
33 |
* ConfigValidatorFactory.
|
|
34 |
*
|
|
35 |
* @author Peter Donald
|
|
36 |
* @version $Revision: 1.1 $ $Date: 2003/12/03 03:19:28 $
|
|
37 |
*/
|
|
38 |
public class ConfigValidator |
|
39 |
{ |
|
40 |
/** The schema that used when validating configuration. */
|
|
41 |
private Schema m_schema;
|
|
42 |
|
|
43 |
/** The entity resolver used by validator if any. */
|
|
44 |
private EntityResolver m_resolver;
|
|
45 |
|
|
46 |
/**
|
|
47 |
* Create a validator for specified schema.
|
|
48 |
*
|
|
49 |
* @param schema the schema
|
|
50 |
*/
|
|
51 | 13 |
ConfigValidator( final Schema schema, |
52 |
final EntityResolver resolver ) |
|
53 |
{ |
|
54 | 13 |
if( null == schema ) |
55 |
{ |
|
56 | 1 |
throw new NullPointerException( "schema" ); |
57 |
} |
|
58 | 12 |
m_schema = schema; |
59 | 12 |
m_resolver = resolver; |
60 |
} |
|
61 |
|
|
62 |
/**
|
|
63 |
* Validate input stream according to shcema.
|
|
64 |
*
|
|
65 |
* @param inputStream the inputStream to validate
|
|
66 |
* @param contentHandler the handler to send to after validation
|
|
67 |
* @param errorHandler that will be passed any errors. May be null.
|
|
68 |
* @throws ValidateException if unable to validate the input
|
|
69 |
*/
|
|
70 | 37 |
public void validate( final InputStream inputStream, |
71 |
final ContentHandler contentHandler, |
|
72 |
final ErrorHandler errorHandler ) |
|
73 |
throws ValidateException
|
|
74 |
{ |
|
75 | 37 |
if( null == inputStream ) |
76 |
{ |
|
77 | 9 |
throw new NullPointerException( "inputStream" ); |
78 |
} |
|
79 | 28 |
validate( new InputSource( inputStream ),
|
80 |
contentHandler, |
|
81 |
errorHandler ); |
|
82 |
} |
|
83 |
|
|
84 |
/**
|
|
85 |
* Validate input stream according to shcema.
|
|
86 |
*
|
|
87 |
* @param inputStream the inputStream to validate
|
|
88 |
* @param errorHandler that will be passed any errors. May be null.
|
|
89 |
* @throws ValidateException if unable to validate the input
|
|
90 |
*/
|
|
91 | 36 |
public void validate( final InputStream inputStream, |
92 |
final ErrorHandler errorHandler ) |
|
93 |
throws ValidateException
|
|
94 |
{ |
|
95 | 36 |
if( null == inputStream ) |
96 |
{ |
|
97 | 9 |
throw new NullPointerException( "inputStream" ); |
98 |
} |
|
99 | 27 |
validate( new InputSource( inputStream ), errorHandler );
|
100 |
} |
|
101 |
|
|
102 |
/**
|
|
103 |
* Validate InputStream according to shcema.
|
|
104 |
*
|
|
105 |
* @param inputStream the inputStream to validate
|
|
106 |
* @return the ValidationResults
|
|
107 |
*/
|
|
108 | 37 |
public ValidationResult validate( final InputStream inputStream )
|
109 |
{ |
|
110 | 37 |
if( null == inputStream ) |
111 |
{ |
|
112 | 9 |
throw new NullPointerException( "inputStream" ); |
113 |
} |
|
114 | 28 |
return validate( new InputSource( inputStream ) ); |
115 |
} |
|
116 |
|
|
117 |
/**
|
|
118 |
* Validate InputStream according to shcema.
|
|
119 |
*
|
|
120 |
* @param inputStream the inputStream to validate
|
|
121 |
* @param contentHandler the handler to send to after validation
|
|
122 |
* @return the ValidationResults
|
|
123 |
*/
|
|
124 | 36 |
public ValidationResult validate( final InputStream inputStream,
|
125 |
final ContentHandler contentHandler ) |
|
126 |
{ |
|
127 | 36 |
if( null == inputStream ) |
128 |
{ |
|
129 | 9 |
throw new NullPointerException( "inputStream" ); |
130 |
} |
|
131 | 27 |
return validate( new InputSource( inputStream ), contentHandler ); |
132 |
} |
|
133 |
|
|
134 |
/**
|
|
135 |
* Validate InputSource according to shcema.
|
|
136 |
*
|
|
137 |
* @param source the InputSource to validate
|
|
138 |
* @param errorHandler that will be passed any errors. May be null.
|
|
139 |
* @throws ValidateException if unable to validate the input
|
|
140 |
*/
|
|
141 | 63 |
public void validate( final InputSource source, |
142 |
final ErrorHandler errorHandler ) |
|
143 |
throws ValidateException
|
|
144 |
{ |
|
145 | 63 |
if( null == source ) |
146 |
{ |
|
147 | 9 |
throw new NullPointerException( "source" ); |
148 |
} |
|
149 | 54 |
doValidate( source, errorHandler ); |
150 |
} |
|
151 |
|
|
152 |
/**
|
|
153 |
* Validate InputSource according to shcema.
|
|
154 |
*
|
|
155 |
* @param source the InputSource to validate
|
|
156 |
* @param contentHandler the handler to send to after validation
|
|
157 |
* @param errorHandler that will be passed any errors. May be null.
|
|
158 |
* @throws ValidateException if unable to validate the input
|
|
159 |
*/
|
|
160 | 64 |
public void validate( final InputSource source, |
161 |
final ContentHandler contentHandler, |
|
162 |
final ErrorHandler errorHandler ) |
|
163 |
throws ValidateException
|
|
164 |
{ |
|
165 | 64 |
if( null == source ) |
166 |
{ |
|
167 | 9 |
throw new NullPointerException( "source" ); |
168 |
} |
|
169 | 55 |
doValidate( source, contentHandler, errorHandler ); |
170 |
} |
|
171 |
|
|
172 |
/**
|
|
173 |
* Validate InputSource according to shcema.
|
|
174 |
*
|
|
175 |
* @param source the source to validate
|
|
176 |
* @return the ValidationResults
|
|
177 |
*/
|
|
178 | 64 |
public ValidationResult validate( final InputSource source )
|
179 |
{ |
|
180 | 64 |
if( null == source ) |
181 |
{ |
|
182 | 9 |
throw new NullPointerException( "source" ); |
183 |
} |
|
184 | 55 |
return doValidate( source );
|
185 |
} |
|
186 |
|
|
187 |
/**
|
|
188 |
* Validate InputSource according to shcema.
|
|
189 |
*
|
|
190 |
* @param source the source to validate
|
|
191 |
* @param contentHandler the handler to send to after validation
|
|
192 |
* @return the ValidationResults
|
|
193 |
*/
|
|
194 | 63 |
public ValidationResult validate( final InputSource source,
|
195 |
final ContentHandler contentHandler ) |
|
196 |
{ |
|
197 | 63 |
if( null == source ) |
198 |
{ |
|
199 | 9 |
throw new NullPointerException( "source" ); |
200 |
} |
|
201 | 54 |
return doValidate( source, contentHandler );
|
202 |
} |
|
203 |
|
|
204 |
/**
|
|
205 |
* Validate Node according to shcema.
|
|
206 |
*
|
|
207 |
* @param node the node to validate
|
|
208 |
* @param errorHandler that will be passed any errors. May be null.
|
|
209 |
* @throws ValidateException if unable to validate the input
|
|
210 |
*/
|
|
211 | 18 |
public void validate( final Node node, final ErrorHandler errorHandler ) |
212 |
throws ValidateException
|
|
213 |
{ |
|
214 | 18 |
if( null == node ) |
215 |
{ |
|
216 | 9 |
throw new NullPointerException( "node" ); |
217 |
} |
|
218 | 9 |
doValidate( node, errorHandler ); |
219 |
} |
|
220 |
|
|
221 |
/**
|
|
222 |
* Validate Node according to shcema.
|
|
223 |
*
|
|
224 |
* @param node the node to validate
|
|
225 |
* @return the ValidationResults
|
|
226 |
*/
|
|
227 | 18 |
public ValidationResult validate( final Node node )
|
228 |
{ |
|
229 | 18 |
if( null == node ) |
230 |
{ |
|
231 | 9 |
throw new NullPointerException( "node" ); |
232 |
} |
|
233 | 9 |
return doValidate( node );
|
234 |
} |
|
235 |
|
|
236 |
/**
|
|
237 |
* Perform the validation and collect the results.
|
|
238 |
*
|
|
239 |
* @param data either a DOM Node or an InputSource to validate
|
|
240 |
* @return the ValidationResults
|
|
241 |
*/
|
|
242 | 64 |
private ValidationResult doValidate( final Object data )
|
243 |
{ |
|
244 | 64 |
final List issueSet = new ArrayList();
|
245 | 64 |
ValidateException exception = null;
|
246 | 64 |
try
|
247 |
{ |
|
248 | 64 |
doValidate( data, new IssueCollector( issueSet ) );
|
249 |
} |
|
250 |
catch( final ValidateException ve )
|
|
251 |
{ |
|
252 | 45 |
exception = ve; |
253 |
} |
|
254 |
|
|
255 | 64 |
final ValidationIssue[] issues = (ValidationIssue[])issueSet. |
256 |
toArray( new ValidationIssue[ issueSet.size() ] );
|
|
257 | 64 |
return new ValidationResult( exception, issues ); |
258 |
} |
|
259 |
|
|
260 |
/**
|
|
261 |
* Actually perform the validation.
|
|
262 |
*
|
|
263 |
* @param data either a DOM Node or an InputSource to validate
|
|
264 |
* @param errorHandler the error handler that receives error messages
|
|
265 |
* @throws ValidateException if unable to validate the input
|
|
266 |
*/
|
|
267 | 127 |
private void doValidate( final Object data, |
268 |
final ErrorHandler errorHandler ) |
|
269 |
throws ValidateException
|
|
270 |
{ |
|
271 | 127 |
try
|
272 |
{ |
|
273 | 127 |
final Verifier verifier = m_schema.newVerifier(); |
274 | 127 |
if( null != errorHandler ) |
275 |
{ |
|
276 | 127 |
verifier.setErrorHandler( errorHandler ); |
277 |
} |
|
278 | 127 |
if( null != m_resolver ) |
279 |
{ |
|
280 | 71 |
verifier.setEntityResolver( m_resolver ); |
281 |
} |
|
282 | 127 |
boolean valid;
|
283 | 127 |
if( data instanceof InputSource ) |
284 |
{ |
|
285 | 109 |
valid = verifier.verify( (InputSource)data ); |
286 |
} |
|
287 |
else
|
|
288 |
{ |
|
289 | 18 |
valid = verifier.verify( (Node)data ); |
290 |
} |
|
291 |
|
|
292 | 82 |
if( !valid )
|
293 |
{ |
|
294 | 45 |
final String message = "Unable to validate input according to schema";
|
295 | 45 |
throw new ValidateException( message ); |
296 |
} |
|
297 |
} |
|
298 |
catch( final VerifierConfigurationException vce )
|
|
299 |
{ |
|
300 | 0 |
final String message = "Unable to locate verifier for schema";
|
301 | 0 |
throw new ValidateException( message, vce ); |
302 |
} |
|
303 |
catch( final SAXException se )
|
|
304 |
{ |
|
305 | 36 |
final String message = "Malformed input XML.";
|
306 | 36 |
throw new ValidateException( message, se ); |
307 |
} |
|
308 |
catch( final IOException ioe )
|
|
309 |
{ |
|
310 | 9 |
final String message = "Error reading input data XML.";
|
311 | 9 |
throw new ValidateException( message, ioe ); |
312 |
} |
|
313 |
} |
|
314 |
|
|
315 |
/**
|
|
316 |
* Perform the validation via filtering and collect the results.
|
|
317 |
*
|
|
318 |
* @param input an InputSource to validate
|
|
319 |
* @param contentHandler the handler to send to after validation
|
|
320 |
* @return the ValidationResults
|
|
321 |
*/
|
|
322 | 54 |
private ValidationResult doValidate( final InputSource input,
|
323 |
final ContentHandler contentHandler ) |
|
324 |
{ |
|
325 | 54 |
final List issueSet = new ArrayList();
|
326 | 54 |
ValidateException exception = null;
|
327 | 54 |
try
|
328 |
{ |
|
329 | 54 |
doValidate( input, |
330 |
contentHandler, |
|
331 |
new IssueCollector( issueSet ) );
|
|
332 |
} |
|
333 |
catch( final ValidateException ve )
|
|
334 |
{ |
|
335 | 36 |
exception = ve; |
336 |
} |
|
337 |
|
|
338 | 54 |
final ValidationIssue[] issues = (ValidationIssue[])issueSet. |
339 |
toArray( new ValidationIssue[ issueSet.size() ] );
|
|
340 | 54 |
return new ValidationResult( exception, issues ); |
341 |
} |
|
342 |
|
|
343 |
/**
|
|
344 |
* Actually perform the validation via filtering.
|
|
345 |
*
|
|
346 |
* @param input an InputSource to validate
|
|
347 |
* @param contentHandler the handler to send to after validation
|
|
348 |
* @param errorHandler the error handler that receives error messages
|
|
349 |
* @throws ValidateException if unable to validate the input
|
|
350 |
*/
|
|
351 | 109 |
private void doValidate( final InputSource input, |
352 |
final ContentHandler contentHandler, |
|
353 |
final ErrorHandler errorHandler ) |
|
354 |
throws ValidateException
|
|
355 |
{ |
|
356 | 109 |
if( null == contentHandler ) |
357 |
{ |
|
358 | 1 |
throw new NullPointerException( "contentHandler" ); |
359 |
} |
|
360 | 108 |
final SAXParserFactory saxParserFactory = SAXParserFactory.newInstance(); |
361 | 108 |
saxParserFactory.setNamespaceAware( true );
|
362 |
|
|
363 | 108 |
try
|
364 |
{ |
|
365 | 108 |
final SAXParser saxParser = saxParserFactory.newSAXParser(); |
366 | 108 |
final XMLReader reader = saxParser.getXMLReader(); |
367 |
|
|
368 | 108 |
final Verifier verifier = m_schema.newVerifier(); |
369 | 108 |
if( null != errorHandler ) |
370 |
{ |
|
371 | 108 |
verifier.setErrorHandler( errorHandler ); |
372 |
} |
|
373 | 108 |
if( null != m_resolver ) |
374 |
{ |
|
375 | 60 |
verifier.setEntityResolver( m_resolver ); |
376 |
} |
|
377 | 108 |
final VerifierFilter filter = verifier.getVerifierFilter(); |
378 | 108 |
filter.setParent( reader ); |
379 | 108 |
filter.setContentHandler( contentHandler ); |
380 | 108 |
if( null != errorHandler ) |
381 |
{ |
|
382 | 108 |
filter.setErrorHandler( errorHandler ); |
383 |
} |
|
384 | 108 |
if( null != m_resolver ) |
385 |
{ |
|
386 | 60 |
filter.setEntityResolver( m_resolver ); |
387 |
} |
|
388 | 108 |
filter.parse( input ); |
389 |
|
|
390 | 63 |
if( !filter.isValid() )
|
391 |
{ |
|
392 | 27 |
final String message = "Unable to validate input according to schema";
|
393 | 27 |
throw new ValidateException( message ); |
394 |
} |
|
395 |
} |
|
396 |
catch( final VerifierConfigurationException vce )
|
|
397 |
{ |
|
398 | 0 |
final String message = "Unable to locate verifier for schema";
|
399 | 0 |
throw new ValidateException( message, vce ); |
400 |
} |
|
401 |
catch( final SAXException se )
|
|
402 |
{ |
|
403 | 36 |
final String message = "Malformed input XML.";
|
404 | 36 |
throw new ValidateException( message, se ); |
405 |
} |
|
406 |
catch( final IOException ioe )
|
|
407 |
{ |
|
408 | 9 |
final String message = "Error reading input input XML.";
|
409 | 9 |
throw new ValidateException( message, ioe ); |
410 |
} |
|
411 |
catch( final ParserConfigurationException pce )
|
|
412 |
{ |
|
413 | 0 |
final String message = "Unable to get parser.";
|
414 | 0 |
throw new ValidateException( message, pce ); |
415 |
} |
|
416 |
} |
|
417 |
} |
|
418 |
|
|