1 package es.inteco.eni.xml;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.io.StringReader;
6 import java.net.HttpURLConnection;
7 import java.net.URL;
8 import java.util.List;
9
10 import javax.xml.XMLConstants;
11 import javax.xml.parsers.ParserConfigurationException;
12 import javax.xml.parsers.SAXParser;
13 import javax.xml.parsers.SAXParserFactory;
14 import javax.xml.transform.Source;
15 import javax.xml.transform.stream.StreamSource;
16 import javax.xml.validation.Schema;
17 import javax.xml.validation.SchemaFactory;
18 import javax.xml.validation.Validator;
19 import javax.xml.validation.ValidatorHandler;
20
21 import org.xml.sax.ContentHandler;
22 import org.xml.sax.InputSource;
23 import org.xml.sax.SAXException;
24 import org.xml.sax.SAXParseException;
25 import org.xml.sax.XMLReader;
26 import org.xml.sax.ext.Locator2Impl;
27 import org.xml.sax.helpers.XMLReaderFactory;
28
29 import es.inteco.eni.commons.ApplicationPropertiesManager;
30 import es.inteco.eni.utils.FileUtils;
31
32
33
34
35
36
37
38
39 public final class XmlUtils {
40
41
42
43 private static Source[] getSchemas(String schemasPathStr){
44 File schemasPath = new File(schemasPathStr);
45 schemasPath.getAbsolutePath();
46 List<File> schemasFiles = FileUtils.getXsdFilesInFolder(schemasPath);
47 Source[] schemasSources = new Source[schemasFiles.size()];
48 int i=0;
49 for(File schema:schemasFiles){
50 schemasSources[i] = new StreamSource(schema);
51 i++;
52 }
53 return schemasSources;
54 }
55
56 private static final String SAX_FEATURES_VALIDATION = "http://xml.org/sax/features/validation";
57 private static final String XML_FEATURES_VALIDATION_SCHEMA = "http://apache.org/xml/features/validation/schema";
58
59
60
61
62
63 private XmlUtils(){
64
65 }
66
67
68
69
70
71
72
73
74
75 public static ValidationXmlResult validateWithExtXSD(String xmlContentParam, String filePath) throws IOException {
76 String xmlContent = xmlContentParam!=null ? xmlContentParam : "";
77 ValidationXmlResult result = new ValidationXmlResult();
78
79
80 result = validateXmlWithRemoteXsd(xmlContent);
81
82
83 if (result.isValidationError()) {
84
85 if(filePath!=null && !"".equals(filePath)){
86 result = validateXmlWithLocalXsd(getSchemas(filePath), xmlContent);
87 }
88
89 if (result.isValidationError()) {
90 result =validateXmlWithLocalXsd(getSchemas(ApplicationPropertiesManager.getProperty("eni.xsd.schemas.path")), xmlContent);
91 }
92 }
93
94 return result;
95 }
96
97
98 private static ValidationXmlResult validateXmlWithLocalXsd(Source[] schemasSources, String xmlContent) throws IOException{
99 ValidationXmlResult result = new ValidationXmlResult();
100 StringReader stringReader = null;
101 InputSource xmlSource = null;
102
103 XmlValidationHandler validationHandler = new XmlValidationHandler();
104
105 try{
106 stringReader = new StringReader(xmlContent);
107 xmlSource = new InputSource(stringReader);
108 SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
109 Schema schemaGrammar = schemaFactory.newSchema(schemasSources);
110 Validator schemaValidator = schemaGrammar.newValidator();
111
112
113 schemaValidator.setErrorHandler(validationHandler);
114
115 ValidatorHandler vHandler = schemaGrammar.newValidatorHandler();
116 ContentHandler cHandler = validationHandler;
117 vHandler.setContentHandler(cHandler);
118
119
120 XMLReader parser = XMLReaderFactory.createXMLReader();
121 parser.setContentHandler(vHandler);
122 parser.parse(xmlSource);
123
124
125 if (validationHandler.isValidationError()) {
126 result = new ValidationXmlResult(validationHandler.getSaxParseExceptionError());
127 }else{
128 result.setXmlTree(validationHandler.getXmlTree());
129 }
130 }catch (SAXException spex) {
131 result = new ValidationXmlResult(new SAXParseException(spex.getMessage(), new Locator2Impl()));
132 }finally {
133 if(stringReader!=null){
134 stringReader.close();
135 }
136 }
137 return result;
138 }
139
140
141 private static ValidationXmlResult validateXmlWithRemoteXsd(String xmlContent) throws IOException{
142 ValidationXmlResult result = new ValidationXmlResult();
143 StringReader stringReader = null;
144 InputSource xmlSource = null;
145
146 XmlValidationHandler validationHandler = new XmlValidationHandler();
147
148 try{
149 stringReader = new StringReader(xmlContent);
150 xmlSource = new InputSource(stringReader);
151 SAXParserFactory factory = SAXParserFactory.newInstance();
152 factory.setValidating(true);
153 factory.setNamespaceAware(true);
154
155 factory.setFeature(SAX_FEATURES_VALIDATION, true);
156 factory.setFeature(XML_FEATURES_VALIDATION_SCHEMA,true);
157
158 SAXParser parser = factory.newSAXParser();
159
160 XMLReader reader = parser.getXMLReader();
161 reader.setContentHandler(validationHandler);
162 reader.setErrorHandler(validationHandler);
163 reader.parse(xmlSource);
164
165
166
167 if (validationHandler.isValidationError()) {
168 result = new ValidationXmlResult(validationHandler.getSaxParseExceptionError());
169 }else{
170 result.setXmlTree(validationHandler.getXmlTree());
171 }
172 }catch (SAXException spex) {
173 result = new ValidationXmlResult(new SAXParseException(spex.getMessage(), new Locator2Impl()));
174 }catch (ParserConfigurationException pcex) {
175 result = new ValidationXmlResult(new SAXParseException(pcex.getMessage(), new Locator2Impl()));
176 }finally {
177 if(stringReader!=null){
178 stringReader.close();
179 }
180 }
181 return result;
182 }
183 }