01: package de.schlund.pfixcore.oxm.helper;
02:
03: import java.io.InputStream;
04: import java.io.StringReader;
05:
06: import javax.xml.parsers.DocumentBuilderFactory;
07: import javax.xml.transform.OutputKeys;
08: import javax.xml.transform.Transformer;
09: import javax.xml.transform.TransformerFactory;
10: import javax.xml.transform.dom.DOMSource;
11: import javax.xml.transform.stream.StreamResult;
12:
13: import org.w3c.dom.Document;
14: import org.w3c.dom.Element;
15: import org.xml.sax.InputSource;
16:
17: /**
18: * Helper class that provides static methods
19: * that are used in the OXM unit tests.
20: *
21: * @author mleidig@schlund.de
22: * @author Stephan Schmidt <schst@stubbles.net>
23: */
24: public class OxmTestHelper {
25:
26: public static Document createResultDocument() {
27: try {
28: Document doc = DocumentBuilderFactory.newInstance()
29: .newDocumentBuilder().newDocument();
30: Element root = doc.createElement("result");
31: doc.appendChild(root);
32: return doc;
33: } catch (Exception x) {
34: throw new RuntimeException("Can't create document", x);
35: }
36: }
37:
38: public static Document createDocument(String str) {
39: try {
40: StringReader reader = new StringReader(str);
41: InputSource src = new InputSource();
42: src.setCharacterStream(reader);
43: Document doc = DocumentBuilderFactory.newInstance()
44: .newDocumentBuilder().parse(src);
45: return doc;
46: } catch (Exception x) {
47: throw new RuntimeException("Can't create document", x);
48: }
49: }
50:
51: public static Document createDocument(InputStream in) {
52: try {
53: Document doc = DocumentBuilderFactory.newInstance()
54: .newDocumentBuilder().parse(in);
55: return doc;
56: } catch (Exception x) {
57: throw new RuntimeException("Can't create document", x);
58: }
59: }
60:
61: @SuppressWarnings("unused")
62: public static void printDocument(Document doc) {
63: try {
64: Transformer t = TransformerFactory.newInstance()
65: .newTransformer();
66: t.setOutputProperty(OutputKeys.INDENT, "yes");
67: t.transform(new DOMSource(doc),
68: new StreamResult(System.out));
69: } catch (Exception x) {
70: throw new RuntimeException("Can't print document", x);
71: }
72: }
73: }
|