• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the  "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 package org.apache.qetest;
19 
20 import java.io.IOException;
21 import java.util.ArrayList;
22 import java.util.List;
23 
24 import javax.xml.XMLConstants;
25 import javax.xml.transform.sax.SAXSource;
26 import javax.xml.validation.Schema;
27 import javax.xml.validation.SchemaFactory;
28 import javax.xml.validation.Validator;
29 
30 import org.xml.sax.ErrorHandler;
31 import org.xml.sax.InputSource;
32 import org.xml.sax.SAXException;
33 import org.xml.sax.SAXParseException;
34 
35 /**
36  * A utility class, to do XML Schema 1.0 validation.
37  *
38  * @author mukulg@apache.org
39  * @version $Id$
40  */
41 public class XSValidate implements ErrorHandler {
42 
43 	private String xmlFilePath = null;
44 	private String xsdFilePath = null;
45 	private List validationMessages = null;
46 
47 	private static final String SCHEMA_FULL_CHECKING_FEATURE_ID =
48 	                                          "http://apache.org/xml/features/validation/schema-full-checking";
49 
XSValidate(String xmlFilePath, String xsdFilePath)50 	public XSValidate(String xmlFilePath, String xsdFilePath) {
51 		this.xmlFilePath = xmlFilePath;
52 		this.xsdFilePath = xsdFilePath;
53 		validationMessages = new ArrayList();
54 	}
55 
validate()56 	public List validate() {
57 		try {
58 			SchemaFactory sfFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
59 			sfFactory.setFeature(SCHEMA_FULL_CHECKING_FEATURE_ID, true);
60 			sfFactory.setErrorHandler(this);
61 			Schema schema = sfFactory.newSchema(new SAXSource(new InputSource(xsdFilePath)));
62 			Validator validator = schema.newValidator();
63 			validator.setErrorHandler(this);
64 			validator.validate(new SAXSource(new InputSource(xmlFilePath)));
65 		}
66 		catch (SAXException ex) {
67 			validationMessages.add("SAXException: " + ex.getMessage());
68 		}
69 		catch (IOException ex) {
70 			validationMessages.add("IOException: " + ex.getMessage());
71 		}
72 
73 		return validationMessages;
74 	}
75 
error(SAXParseException saxParseEx)76 	public void error(SAXParseException saxParseEx) throws SAXException {
77 		String systemId = saxParseEx.getSystemId();
78 		String[] sysIdParts = systemId.split("/");
79 		String saxParseMesg = saxParseEx.getMessage();
80 		validationMessages.add("[Error] " + sysIdParts[sysIdParts.length - 1] + ":" + saxParseEx.getLineNumber() + ":" +
81 		                                    saxParseEx.getColumnNumber() + ":" + saxParseMesg);
82 	}
83 
fatalError(SAXParseException saxParseEx)84 	public void fatalError(SAXParseException saxParseEx) throws SAXException {
85 		String systemId = saxParseEx.getSystemId();
86 		String[] sysIdParts = systemId.split("/");
87 		validationMessages.add("[Fatal Error] " + sysIdParts[sysIdParts.length - 1] + ":" + saxParseEx.getLineNumber() +
88 		                                          ":" + saxParseEx.getColumnNumber() + ":" + saxParseEx.getMessage());
89 	}
90 
warning(SAXParseException saxParseEx)91 	public void warning(SAXParseException saxParseEx) throws SAXException {
92 	    // NO OP
93 	}
94 
95 }
96