View Javadoc

1   package pl.psnc.dl.ege.validator.xml;
2   
3   import java.io.IOException;
4   import java.io.InputStream;
5   import java.net.URL;
6   
7   import javax.xml.parsers.ParserConfigurationException;
8   import javax.xml.parsers.SAXParser;
9   import javax.xml.parsers.SAXParserFactory;
10  import javax.xml.transform.stream.StreamSource;
11  import javax.xml.validation.Schema;
12  import javax.xml.validation.SchemaFactory;
13  
14  import org.apache.log4j.Logger;
15  import org.xml.sax.ErrorHandler;
16  import org.xml.sax.InputSource;
17  import org.xml.sax.SAXException;
18  import org.xml.sax.XMLReader;
19  
20  import pl.psnc.dl.ege.validator.StandardErrorHandler;
21  
22  /**
23   * Performs XML document validation over specified XML schema
24   * (according to - W3 XML Schema, specification).
25   * 
26   * @author mariuszs
27   * 
28   */
29  public class SchemaValidator implements XmlValidator
30  {
31  	
32  	private static final Logger LOGGER = Logger.getLogger(SchemaValidator.class);
33  	
34  	private final String schemeUrl;
35  	
36  	private String defaultUrl = null;
37  	
38  	/**
39  	 * Default constructor.<br> 
40  	 * Sets {@link StandardErrorHandler} as default error handler.
41  	 * 
42  	 * @param schemeUrl - URL reference to XML Scheme
43  	 */
44  	public SchemaValidator(String schemeUrl){
45  		if(schemeUrl == null){
46  			throw new IllegalArgumentException();
47  		}
48  		this.schemeUrl = schemeUrl;
49  	}
50  	
51  	
52  	public SchemaValidator(String schemeUrl, String defaultUrl){
53  		this(schemeUrl);
54  		this.defaultUrl = defaultUrl;	
55  	}
56  	
57  	
58  	/**
59  	 * Validates streamed XML data according to XML scheme rules.<br>
60  	 * Validation result and messages can be stored by error handler
61  	 * (StandardErrorHandler by default). <br>
62  	 * 
63  	 * @param inputData - streamed XML data.
64  	 * @throws SAXException
65  	 * @throws {@link IOException}
66  	 */
67  	public void validateXml(InputStream inputData, ErrorHandler errorHandler) throws SAXException, IOException, Exception 
68  	{
69  		SAXParserFactory spf = SAXParserFactory.newInstance();
70  		spf.setValidating(false);
71  		spf.setNamespaceAware(true);
72  		try {
73  			SchemaFactory schemaFactory = SchemaFactory
74  			.newInstance("http://www.w3.org/2001/XMLSchema");
75  			URL schemaURL = new URL(schemeUrl);
76  			//try to download schema by external URL 
77  			InputStream urlStream = null;
78  			try{
79  				urlStream = schemaURL.openStream();
80  			}catch(IOException ex){
81  				// in case of any problem use default schema
82  				if(defaultUrl != null){
83  					schemaURL = new URL(defaultUrl); 
84  					urlStream = schemaURL.openStream();
85  				}else{
86  					throw ex;
87  				}
88  			}
89  			LOGGER.debug("Uses schema url : " + schemaURL);
90  			StreamSource sss = new StreamSource(urlStream);
91  			Schema schema =  schemaFactory.newSchema(sss);
92  			spf.setSchema(schema);
93  			SAXParser parser = spf.newSAXParser();
94  			XMLReader reader = parser.getXMLReader();
95  			reader.setErrorHandler(errorHandler);
96  			reader.parse(new InputSource(inputData));
97  		}
98  		catch (ParserConfigurationException e) {
99  			throw new SAXException(e);
100 		}
101 	}
102 	
103 	
104 	
105 }