1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.openpermis.policy.io.xmlchecking; |
12 | |
|
13 | |
import java.io.File; |
14 | |
import java.io.FileReader; |
15 | |
import java.io.IOException; |
16 | |
import java.io.InputStream; |
17 | |
import java.io.Reader; |
18 | |
import java.io.StringReader; |
19 | |
import java.util.ResourceBundle; |
20 | |
|
21 | |
import javax.xml.XMLConstants; |
22 | |
import javax.xml.transform.Source; |
23 | |
import javax.xml.transform.dom.DOMSource; |
24 | |
import javax.xml.transform.stream.StreamSource; |
25 | |
import javax.xml.validation.Schema; |
26 | |
import javax.xml.validation.SchemaFactory; |
27 | |
|
28 | |
import org.slf4j.Logger; |
29 | |
import org.slf4j.LoggerFactory; |
30 | |
import org.w3c.dom.Node; |
31 | |
import org.xml.sax.SAXException; |
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
public class W3CXMLSchemaValidator { |
41 | |
|
42 | |
|
43 | |
|
44 | 1 | private static final Logger LOGGER = |
45 | |
LoggerFactory.getLogger(W3CXMLSchemaValidator.class); |
46 | |
|
47 | |
private static final String SCHEMA_LOCATION = |
48 | |
"org.openpermis.policy.io.xmlchecking/xml_checking"; |
49 | |
|
50 | |
private static final String SCHEMA_LANGUAGE = |
51 | |
XMLConstants.W3C_XML_SCHEMA_NS_URI; |
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
public static String validate (File file) { |
60 | 15 | String message = null; |
61 | |
try { |
62 | 15 | W3CXMLSchemaValidator validator = new W3CXMLSchemaValidator(); |
63 | 15 | validator.validate(new FileReader(file)); |
64 | 0 | } catch (org.xml.sax.SAXException ex) { |
65 | 0 | message = "XML Schema validation failed: " + ex.getMessage(); |
66 | 0 | } catch (java.io.IOException ex) { |
67 | 0 | message = "IOException when parsing the policy: " + ex.getMessage(); |
68 | 0 | } catch (Exception ex) { |
69 | 0 | message = "Exception when parsing the policy: " + ex.getMessage(); |
70 | 15 | } |
71 | 15 | return message; |
72 | |
} |
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
private Schema schema; |
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | 15 | public W3CXMLSchemaValidator () throws SAXException, IOException { |
89 | |
|
90 | 15 | SchemaFactory schemaFactory = SchemaFactory.newInstance(SCHEMA_LANGUAGE); |
91 | |
|
92 | 15 | ResourceBundle schemaResourceBundle = |
93 | |
ResourceBundle.getBundle(SCHEMA_LOCATION); |
94 | 15 | String schemaLocation = schemaResourceBundle.getString("schemaLocation"); |
95 | |
|
96 | 15 | LOGGER.debug("W3CXMLSchemaValidator: found schema location " + schemaLocation); |
97 | |
|
98 | 15 | if (new File(schemaLocation).isAbsolute()) { |
99 | 0 | File schemaFile = new File(schemaLocation); |
100 | 0 | this.schema = schemaFactory.newSchema(new StreamSource(schemaFile)); |
101 | 0 | } else { |
102 | 15 | InputStream schemaInputStream = getClass().getResourceAsStream(schemaLocation); |
103 | 15 | this.schema = schemaFactory.newSchema(new StreamSource(schemaInputStream)); |
104 | |
} |
105 | |
|
106 | 15 | LOGGER.debug("W3CXMLSchemaValidator: succesfully constructed using " |
107 | |
+ "schema " + schemaLocation); |
108 | 15 | } |
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | 0 | public W3CXMLSchemaValidator (File schemaFile) throws SAXException, IOException { |
117 | 0 | SchemaFactory schemaFactory = SchemaFactory.newInstance(SCHEMA_LANGUAGE); |
118 | 0 | this.schema = schemaFactory.newSchema(new StreamSource(schemaFile)); |
119 | 0 | } |
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
public void validate (String s) throws SAXException, IOException { |
131 | 0 | validate(new StringReader(s)); |
132 | 0 | } |
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
public void validate (Reader r) throws SAXException, IOException { |
141 | 15 | LOGGER.debug("validate with Reader argument called"); |
142 | 15 | this.schema.newValidator().validate(new StreamSource(r)); |
143 | 15 | LOGGER.debug("validate ok."); |
144 | 15 | } |
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | |
|
151 | |
|
152 | |
public void validate (Node r) throws SAXException, IOException { |
153 | 0 | LOGGER.debug("validate with Node argument called"); |
154 | 0 | this.schema.newValidator().validate(new DOMSource(r)); |
155 | 0 | LOGGER.debug("validate ok."); |
156 | 0 | } |
157 | |
|
158 | |
|
159 | |
|
160 | |
|
161 | |
|
162 | |
|
163 | |
|
164 | |
public void validate (Source source) throws SAXException, IOException { |
165 | 0 | LOGGER.debug("validate with Source argument called"); |
166 | 0 | this.schema.newValidator().validate(source); |
167 | 0 | LOGGER.debug("validate ok."); |
168 | 0 | } |
169 | |
|
170 | |
} |