| 1 | package scriptbuilder.structures; |
|---|
| 2 | |
|---|
| 3 | import java.io.IOException; |
|---|
| 4 | import java.io.StringReader; |
|---|
| 5 | import java.io.StringWriter; |
|---|
| 6 | import javax.xml.parsers.DocumentBuilder; |
|---|
| 7 | import javax.xml.parsers.DocumentBuilderFactory; |
|---|
| 8 | import javax.xml.parsers.ParserConfigurationException; |
|---|
| 9 | import javax.xml.transform.OutputKeys; |
|---|
| 10 | import javax.xml.transform.Transformer; |
|---|
| 11 | import javax.xml.transform.TransformerException; |
|---|
| 12 | import javax.xml.transform.TransformerFactory; |
|---|
| 13 | import javax.xml.transform.dom.DOMSource; |
|---|
| 14 | import javax.xml.transform.stream.StreamResult; |
|---|
| 15 | import org.w3c.dom.Document; |
|---|
| 16 | import org.xml.sax.InputSource; |
|---|
| 17 | import org.xml.sax.SAXException; |
|---|
| 18 | |
|---|
| 19 | /** |
|---|
| 20 | * Utility methods that build XML elements from content strings. |
|---|
| 21 | * @author Bryan McGuffin |
|---|
| 22 | */ |
|---|
| 23 | public 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 | * @return the xml string with the link to external DTD for XML script files. |
|---|
| 87 | */ |
|---|
| 88 | public static String externalDTD() |
|---|
| 89 | { |
|---|
| 90 | return "<!DOCTYPE TMC_SCRIPT SYSTEM \"incident_script.dtd\">\n"; |
|---|
| 91 | } |
|---|
| 92 | /** |
|---|
| 93 | * @return The xml string that starts the document. |
|---|
| 94 | */ |
|---|
| 95 | public static String xmlHeader() |
|---|
| 96 | { |
|---|
| 97 | return "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n"; |
|---|
| 98 | } |
|---|
| 99 | /** |
|---|
| 100 | * Prettyprint an XML string. |
|---|
| 101 | * @param xmlString (works best if it contains no newlines) |
|---|
| 102 | * @return String that has been nicely formatted with indentation |
|---|
| 103 | */ |
|---|
| 104 | public static String prettyPrintXML(String xmlString) |
|---|
| 105 | { |
|---|
| 106 | Document xmlDoc = null; |
|---|
| 107 | String formattedXML = ""; |
|---|
| 108 | try { |
|---|
| 109 | xmlDoc = toXmlDocument(xmlString); |
|---|
| 110 | formattedXML = prettyprintdoc(xmlDoc); |
|---|
| 111 | } catch (ParserConfigurationException | SAXException | IOException |
|---|
| 112 | | TransformerException e) { |
|---|
| 113 | e.printStackTrace(); |
|---|
| 114 | } |
|---|
| 115 | // Prepend the header and externalDTD lines. (This must be done after |
|---|
| 116 | // prettyprinting, which tries to read the externalDTD if the link is present.) |
|---|
| 117 | // Fixes #239. |
|---|
| 118 | formattedXML = xmlHeader() + externalDTD() + formattedXML; |
|---|
| 119 | return formattedXML; |
|---|
| 120 | } |
|---|
| 121 | |
|---|
| 122 | |
|---|
| 123 | /** |
|---|
| 124 | * Prettyprint an XML document. |
|---|
| 125 | * @param document an "ugly" XML document |
|---|
| 126 | * @return String nicely formatted XML with indentation |
|---|
| 127 | * @throws TransformerException |
|---|
| 128 | */ |
|---|
| 129 | public static String prettyprintdoc(Document document) |
|---|
| 130 | throws TransformerException { |
|---|
| 131 | TransformerFactory transformerFactory = TransformerFactory |
|---|
| 132 | .newInstance(); |
|---|
| 133 | Transformer transformer = transformerFactory.newTransformer(); |
|---|
| 134 | transformer.setOutputProperty(OutputKeys.INDENT, "yes"); |
|---|
| 135 | transformer.setOutputProperty( |
|---|
| 136 | "{http://xml.apache.org/xslt}indent-amount", "2"); |
|---|
| 137 | transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); |
|---|
| 138 | DOMSource source = new DOMSource(document); |
|---|
| 139 | StringWriter strWriter = new StringWriter(); |
|---|
| 140 | StreamResult result = new StreamResult(strWriter); |
|---|
| 141 | |
|---|
| 142 | transformer.transform(source, result); |
|---|
| 143 | |
|---|
| 144 | return strWriter.getBuffer().toString(); |
|---|
| 145 | |
|---|
| 146 | } |
|---|
| 147 | |
|---|
| 148 | /** Create an XML Document from a string that is in xml format. |
|---|
| 149 | * |
|---|
| 150 | * @param str xml format content |
|---|
| 151 | * @return Document containing the specified content. |
|---|
| 152 | * @throws ParserConfigurationException |
|---|
| 153 | * @throws SAXException |
|---|
| 154 | * @throws IOException |
|---|
| 155 | */ |
|---|
| 156 | public static Document toXmlDocument(String str) |
|---|
| 157 | throws ParserConfigurationException, SAXException, IOException { |
|---|
| 158 | |
|---|
| 159 | DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory |
|---|
| 160 | .newInstance(); |
|---|
| 161 | DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); |
|---|
| 162 | Document document = docBuilder.parse(new InputSource(new StringReader( |
|---|
| 163 | str))); |
|---|
| 164 | |
|---|
| 165 | return document; |
|---|
| 166 | } |
|---|
| 167 | } |
|---|