Warning: Can't use blame annotator:
svn blame failed on trunk/src/scriptbuilder/structures/XMLBuilder.java: ("Can't find a temporary directory: Internal error", 20014)

source: tmcsimulator-scriptbuilder/trunk/src/scriptbuilder/structures/XMLBuilder.java @ 203

Revision 203, 4.9 KB checked in by jdalbey, 6 years ago (diff)

XMLWriter.java name changed to XMLBuilder. Add pretty printing methods (ticket #237).

RevLine 
1package scriptbuilder.structures;
2
3import java.io.IOException;
4import java.io.StringReader;
5import java.io.StringWriter;
6import javax.xml.parsers.DocumentBuilder;
7import javax.xml.parsers.DocumentBuilderFactory;
8import javax.xml.parsers.ParserConfigurationException;
9import javax.xml.transform.OutputKeys;
10import javax.xml.transform.Transformer;
11import javax.xml.transform.TransformerException;
12import javax.xml.transform.TransformerFactory;
13import javax.xml.transform.dom.DOMSource;
14import javax.xml.transform.stream.StreamResult;
15import org.w3c.dom.Document;
16import org.xml.sax.InputSource;
17import org.xml.sax.SAXException;
18
19/**
20 * Utility methods that build XML elements from content strings.
21 * @author Bryan McGuffin
22 */
23public class XMLBuilder
24{
25
26    /**
27     * XML-style opening tag. Example: if given string "my_tag", returns
28     * "<my_tag>".
29     *
30     * @param s the XML element to be included in the tag.
31     * @return the properly formatted tag
32     */
33    public static String openTag(String s)
34    {
35        return "<" + s + ">";
36    }
37
38    /**
39     * XML-style closing tag. Example: if given string "my_tag", returns
40     * "</my_tag>".
41     *
42     * @param s the XML element to be included in the tag.
43     * @return the properly formatted tag
44     */
45    public static String closeTag(String s)
46    {
47        return "</" + s + ">\n";
48    }
49
50    /**
51     * XML-style empty tag. Example: if given string "my_tag", returns
52     * "<my_tag/>".
53     *
54     * @param s the XML element to be included in the tag.
55     * @return the properly formatted tag
56     */
57    public static String emptyTag(String s)
58    {
59        return "<" + s + "/>\n";
60    }
61
62    /**
63     * Creates a pair of XML open and close tags to wrap a simple line of data.
64     * Useful if only one element need be enclosed in this particular tag.
65     *
66     * @param s the data to be wrapped
67     * @param e the XML element represented by the data
68     * @return an XML string of the format <my_tag>some_data_goes_here</my_tag>
69     */
70    public static String simpleTag(String s, ELEMENT e)
71    {
72        String output = "";
73        if (s == null)
74        {
75            s = "";
76        }
77
78        output += openTag(e.tag);
79        output += s;
80        output += closeTag(e.tag);
81
82        return output;
83    }
84
85    /**
86     * Generates the link to external DTD for XML script files.
87     *
88     * @return
89     */
90    public static String externalDTD()
91    {
92        String output = "";
93        output += "<!DOCTYPE TMC_SCRIPT SYSTEM \"incident_script.dtd\">\n";
94        return output;
95    }
96   
97    /**
98     * Prettyprint an XML string.
99     * @param xmlString (works best if it contains no newlines)
100     * @return String that has been nicely formatted with indentation
101     */
102    public static String prettyPrintXML(String xmlString)
103    {
104        System.out.println("prettyprint start: " + xmlString);
105        Document xmlDoc = null;
106        String formattedXML = "";
107        try {
108            xmlDoc = toXmlDocument(xmlString);
109            formattedXML = prettyprintdoc(xmlDoc);
110        } catch (ParserConfigurationException | SAXException | IOException
111                | TransformerException e) {
112            e.printStackTrace();
113        }
114        return formattedXML;
115    }
116   
117 
118    /**
119     * Prettyprint an XML document.
120     * @param document an "ugly" XML document
121     * @return String nicely formatted XML with indentation
122     * @throws TransformerException
123     */
124    public static String prettyprintdoc(Document document)
125            throws TransformerException {
126        TransformerFactory transformerFactory = TransformerFactory
127                .newInstance();
128        Transformer transformer = transformerFactory.newTransformer();
129        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
130        transformer.setOutputProperty(
131                "{http://xml.apache.org/xslt}indent-amount", "2");
132        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
133        DOMSource source = new DOMSource(document);
134        StringWriter strWriter = new StringWriter();
135        StreamResult result = new StreamResult(strWriter);
136 
137        transformer.transform(source, result);
138 
139        return strWriter.getBuffer().toString();
140 
141    }
142 
143    /** Create an XML Document from a string that is in xml format.
144     *
145     * @param str xml format content
146     * @return Document containing the specified content.
147     * @throws ParserConfigurationException
148     * @throws SAXException
149     * @throws IOException
150     */
151    public static Document toXmlDocument(String str)
152            throws ParserConfigurationException, SAXException, IOException {
153 
154        DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory
155                .newInstance();
156        DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
157        Document document = docBuilder.parse(new InputSource(new StringReader(
158                str)));
159 
160        return document;
161    }
162}
Note: See TracBrowser for help on using the repository browser.