Changeset 203 in tmcsimulator-scriptbuilder for trunk/src/scriptbuilder/structures/XMLBuilder.java


Ignore:
Timestamp:
02/01/2020 04:18:21 PM (6 years ago)
Author:
jdalbey
Message:

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

File:
1 moved

Legend:

Unmodified
Added
Removed
  • trunk/src/scriptbuilder/structures/XMLBuilder.java

    r201 r203  
    11package scriptbuilder.structures; 
    22 
     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 
    319/** 
    4  * 
     20 * Utility methods that build XML elements from content strings. 
    521 * @author Bryan McGuffin 
    622 */ 
    7 public class XMLWriter 
     23public class XMLBuilder 
    824{ 
    925 
     
    7894        return output; 
    7995    } 
     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    } 
    80162} 
Note: See TracChangeset for help on using the changeset viewer.