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

Revision 205, 4.8 KB checked in by jdalbey, 6 years ago (diff)

XMLBuilder.java remove diagnostic print.

Line 
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        Document xmlDoc = null;
105        String formattedXML = "";
106        try {
107            xmlDoc = toXmlDocument(xmlString);
108            formattedXML = prettyprintdoc(xmlDoc);
109        } catch (ParserConfigurationException | SAXException | IOException
110                | TransformerException e) {
111            e.printStackTrace();
112        }
113        return formattedXML;
114    }
115   
116 
117    /**
118     * Prettyprint an XML document.
119     * @param document an "ugly" XML document
120     * @return String nicely formatted XML with indentation
121     * @throws TransformerException
122     */
123    public static String prettyprintdoc(Document document)
124            throws TransformerException {
125        TransformerFactory transformerFactory = TransformerFactory
126                .newInstance();
127        Transformer transformer = transformerFactory.newTransformer();
128        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
129        transformer.setOutputProperty(
130                "{http://xml.apache.org/xslt}indent-amount", "2");
131        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
132        DOMSource source = new DOMSource(document);
133        StringWriter strWriter = new StringWriter();
134        StreamResult result = new StreamResult(strWriter);
135 
136        transformer.transform(source, result);
137 
138        return strWriter.getBuffer().toString();
139 
140    }
141 
142    /** Create an XML Document from a string that is in xml format.
143     *
144     * @param str xml format content
145     * @return Document containing the specified content.
146     * @throws ParserConfigurationException
147     * @throws SAXException
148     * @throws IOException
149     */
150    public static Document toXmlDocument(String str)
151            throws ParserConfigurationException, SAXException, IOException {
152 
153        DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory
154                .newInstance();
155        DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
156        Document document = docBuilder.parse(new InputSource(new StringReader(
157                str)));
158 
159        return document;
160    }
161}
Note: See TracBrowser for help on using the repository browser.