1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
package net.mtu.eggplant.xml; |
29 | |
|
30 | |
import java.io.IOException; |
31 | |
import java.io.InputStream; |
32 | |
import java.io.Reader; |
33 | |
import java.io.StringReader; |
34 | |
import java.io.StringWriter; |
35 | |
import java.io.Writer; |
36 | |
import java.text.DateFormat; |
37 | |
import java.text.SimpleDateFormat; |
38 | |
|
39 | |
import javax.xml.parsers.DocumentBuilder; |
40 | |
import javax.xml.parsers.DocumentBuilderFactory; |
41 | |
import javax.xml.parsers.ParserConfigurationException; |
42 | |
import javax.xml.transform.OutputKeys; |
43 | |
import javax.xml.transform.Transformer; |
44 | |
import javax.xml.transform.TransformerException; |
45 | |
import javax.xml.transform.TransformerFactory; |
46 | |
import javax.xml.transform.dom.DOMSource; |
47 | |
import javax.xml.transform.stream.StreamResult; |
48 | |
import javax.xml.validation.Schema; |
49 | |
|
50 | |
import org.apache.commons.io.IOUtils; |
51 | |
import org.custommonkey.xmlunit.Diff; |
52 | |
import org.slf4j.Logger; |
53 | |
import org.slf4j.LoggerFactory; |
54 | |
import org.w3c.dom.Document; |
55 | |
import org.w3c.dom.Element; |
56 | |
import org.xml.sax.ErrorHandler; |
57 | |
import org.xml.sax.InputSource; |
58 | |
import org.xml.sax.SAXException; |
59 | |
import org.xml.sax.SAXParseException; |
60 | |
|
61 | |
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; |
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | 0 | public class XMLUtils { |
67 | |
|
68 | 1 | private static final Logger LOGGER = LoggerFactory.getLogger(XMLUtils.class); |
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | 1 | public static final ThreadLocal<DateFormat> XML_TIME_FORMAT = new ThreadLocal<DateFormat>() { |
74 | |
@Override |
75 | |
protected DateFormat initialValue() { |
76 | 0 | return new SimpleDateFormat("HH:mm:ss"); |
77 | |
} |
78 | |
}; |
79 | |
|
80 | |
public static final DocumentBuilder DOCUMENT_BUILDER; |
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | 1 | public static final ErrorHandler STANDARD_ERROR_HANDLER = new ErrorHandler() { |
87 | |
public void error(final SAXParseException spe) throws SAXParseException { |
88 | 0 | throw spe; |
89 | |
} |
90 | |
|
91 | |
public void fatalError(final SAXParseException spe) throws SAXParseException { |
92 | 0 | throw spe; |
93 | |
} |
94 | |
|
95 | |
public void warning(final SAXParseException spe) throws SAXParseException { |
96 | 0 | LOGGER.warn(spe.getMessage(), spe); |
97 | 0 | } |
98 | |
}; |
99 | |
|
100 | |
|
101 | |
static { |
102 | |
try { |
103 | 1 | final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); |
104 | 1 | factory.setNamespaceAware(true); |
105 | 1 | DOCUMENT_BUILDER = factory.newDocumentBuilder(); |
106 | 1 | DOCUMENT_BUILDER.setErrorHandler(STANDARD_ERROR_HANDLER); |
107 | 0 | } catch (final ParserConfigurationException pce) { |
108 | 0 | throw new RuntimeException(pce.getMessage(), pce); |
109 | 1 | } |
110 | 1 | } |
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
public static Document parse(final Reader stream, |
126 | |
final Schema schema) |
127 | |
throws IOException, SAXException { |
128 | |
try { |
129 | 0 | final DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); |
130 | 0 | builderFactory.setNamespaceAware(true); |
131 | 0 | builderFactory.setSchema(schema); |
132 | 0 | builderFactory.setIgnoringComments(true); |
133 | 0 | builderFactory.setIgnoringElementContentWhitespace(true); |
134 | 0 | final DocumentBuilder parser = builderFactory.newDocumentBuilder(); |
135 | |
|
136 | 0 | parser.setErrorHandler(STANDARD_ERROR_HANDLER); |
137 | |
|
138 | |
|
139 | 0 | final StringWriter writer = new StringWriter(); |
140 | 0 | IOUtils.copy(stream, writer); |
141 | 0 | final String content = writer.toString(); |
142 | 0 | final Document document = parser.parse(new InputSource(new StringReader(content))); |
143 | |
|
144 | 0 | return document; |
145 | 0 | } catch (ParserConfigurationException e) { |
146 | 0 | throw new RuntimeException("Error configuring the XML parser", e); |
147 | |
} |
148 | |
} |
149 | |
|
150 | |
|
151 | |
|
152 | |
|
153 | |
|
154 | |
|
155 | |
|
156 | |
|
157 | |
|
158 | |
|
159 | |
|
160 | |
public static Document parseXMLDocument(final InputStream xmlDocStream) throws SAXException, IOException { |
161 | 0 | return parseXMLDocument(new InputSource(xmlDocStream)); |
162 | |
} |
163 | |
|
164 | |
|
165 | |
|
166 | |
|
167 | |
|
168 | |
|
169 | |
|
170 | |
|
171 | |
|
172 | |
|
173 | |
|
174 | |
public static Document parseXMLDocument(final Reader xmlDocStream) throws SAXException, IOException { |
175 | 0 | return parseXMLDocument(new InputSource(xmlDocStream)); |
176 | |
} |
177 | |
|
178 | |
|
179 | |
|
180 | |
|
181 | |
|
182 | |
|
183 | |
|
184 | |
|
185 | |
|
186 | |
|
187 | |
|
188 | |
public static Document parseXMLDocument(final InputSource xmlDocStream) throws SAXException, IOException { |
189 | |
try { |
190 | 0 | final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); |
191 | 0 | factory.setNamespaceAware(true); |
192 | 0 | factory.setIgnoringComments(true); |
193 | 0 | factory.setIgnoringElementContentWhitespace(true); |
194 | |
|
195 | 0 | final DocumentBuilder parser = factory.newDocumentBuilder(); |
196 | 0 | parser.setErrorHandler(STANDARD_ERROR_HANDLER); |
197 | |
|
198 | 0 | final Document document = parser.parse(xmlDocStream); |
199 | 0 | return document; |
200 | 0 | } catch (final ParserConfigurationException pce) { |
201 | 0 | throw new RuntimeException("Error configuring the XML parser", pce); |
202 | |
} |
203 | |
} |
204 | |
|
205 | |
|
206 | |
|
207 | |
|
208 | |
public static String getStringAttributeValue(final Element element, |
209 | |
final String attributeName) { |
210 | 0 | if (null == element) { |
211 | 0 | return null; |
212 | |
} |
213 | 0 | final String str = element.getAttribute(attributeName); |
214 | 0 | return str; |
215 | |
} |
216 | |
|
217 | |
|
218 | |
|
219 | |
|
220 | |
@SuppressFBWarnings(value = { "NP_BOOLEAN_RETURN_NULL" }, justification = "Need to return Null so that we can determine when there is no score") |
221 | |
public static Boolean getBooleanAttributeValue(final Element element, |
222 | |
final String attributeName) { |
223 | 0 | if (null == element) { |
224 | 0 | return null; |
225 | |
} |
226 | 0 | final String str = element.getAttribute(attributeName); |
227 | 0 | if (str.isEmpty()) { |
228 | 0 | return null; |
229 | |
} else { |
230 | 0 | return Boolean.valueOf(str); |
231 | |
} |
232 | |
} |
233 | |
|
234 | |
|
235 | |
|
236 | |
|
237 | |
|
238 | |
|
239 | |
|
240 | |
|
241 | |
|
242 | |
public static Double getDoubleAttributeValue(final Element element, |
243 | |
final String attributeName) { |
244 | 0 | if (null == element) { |
245 | 0 | return null; |
246 | |
} |
247 | 0 | final String str = element.getAttribute(attributeName); |
248 | 0 | if (str.isEmpty()) { |
249 | 0 | return null; |
250 | |
} else { |
251 | 0 | return Double.valueOf(str); |
252 | |
} |
253 | |
} |
254 | |
|
255 | |
|
256 | |
|
257 | |
|
258 | |
|
259 | |
|
260 | |
|
261 | |
public static void writeXML(final Document doc, |
262 | |
final Writer writer) { |
263 | 0 | writeXML(doc, writer, null); |
264 | 0 | } |
265 | |
|
266 | |
|
267 | |
|
268 | |
|
269 | |
|
270 | |
|
271 | |
|
272 | |
|
273 | |
|
274 | |
public static void writeXML(final Document doc, |
275 | |
final Writer writer, |
276 | |
final String encoding) { |
277 | |
try { |
278 | 0 | final TransformerFactory transformerFactory = TransformerFactory.newInstance(); |
279 | 0 | final Transformer transformer = transformerFactory.newTransformer(); |
280 | 0 | transformer.setOutputProperty(OutputKeys.INDENT, "yes"); |
281 | 0 | transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); |
282 | 0 | if (null != encoding) { |
283 | 0 | transformer.setOutputProperty(OutputKeys.ENCODING, encoding); |
284 | |
} |
285 | 0 | final DOMSource source = new DOMSource(doc); |
286 | 0 | final StreamResult result = new StreamResult(writer); |
287 | 0 | transformer.transform(source, result); |
288 | 0 | } catch (final TransformerException e) { |
289 | 0 | throw new RuntimeException("Internal error writing xml", e); |
290 | 0 | } |
291 | 0 | } |
292 | |
|
293 | |
|
294 | |
|
295 | |
|
296 | |
|
297 | |
|
298 | |
|
299 | |
|
300 | |
|
301 | |
public static boolean compareDocuments(final Document controlDoc, |
302 | |
final Document testDoc) { |
303 | 0 | final Diff xmldiff = new Diff(controlDoc, testDoc); |
304 | 0 | return xmldiff.similar(); |
305 | |
} |
306 | |
|
307 | |
} |