Coverage Report - org.kuali.rice.core.util.xml.XmlJotter
 
Classes in this File Line Coverage Branch Coverage Complexity
XmlJotter
0%
0/34
0%
0/4
1.909
 
 1  
 package org.kuali.rice.core.util.xml;
 2  
 
 3  
 import org.jdom.output.Format;
 4  
 import org.jdom.output.XMLOutputter;
 5  
 
 6  
 import javax.xml.transform.*;
 7  
 import javax.xml.transform.dom.DOMSource;
 8  
 import javax.xml.transform.stream.StreamResult;
 9  
 import java.io.IOException;
 10  
 import java.io.StringWriter;
 11  
 
 12  
 /**
 13  
  * Utility class that helps retrieve string representations of org.jdom & org.w3c
 14  
  * xml Objects
 15  
  */
 16  
 public final class XmlJotter {
 17  
 
 18  0
     private XmlJotter() {
 19  0
         throw new UnsupportedOperationException("do not call");
 20  
     }
 21  
 
 22  
     /**
 23  
      * Retrieves a string representation of a document in a indented format.
 24  
      *
 25  
      * @param document the document
 26  
      * @return document as a string
 27  
      */
 28  
     public static String jotDocument(org.jdom.Document document) {
 29  0
         return jotDocument(document, true);
 30  
     }
 31  
 
 32  
     /**
 33  
      * Retrieves a string representation of a document in either indented or raw format.
 34  
      *
 35  
      * @param document the document
 36  
      * @param indent   whether to use indented or raw format
 37  
      * @return document as a string
 38  
      */
 39  
     public static String jotDocument(org.jdom.Document document, boolean indent) {
 40  0
         XMLOutputter outputer = new XMLOutputter(getJdomFormat(indent));
 41  0
         StringWriter writer = new StringWriter();
 42  
         try {
 43  0
             outputer.output(document, writer);
 44  0
         } catch (IOException e) {
 45  0
             throw new XmlException("Could not write XML data export.", e);
 46  0
         }
 47  0
         return writer.toString();
 48  
     }
 49  
 
 50  
     /**
 51  
      * Retrieves a string representation of a document in a indented format.
 52  
      *
 53  
      * @param document the document
 54  
      * @return document as a string
 55  
      */
 56  
     public static String jotDocument(org.w3c.dom.Document document) {
 57  0
         return jotNode(document.getDocumentElement());
 58  
     }
 59  
 
 60  
     /**
 61  
      * Retrieves a string representation of a document in either indented or raw format.
 62  
      *
 63  
      * @param document the document
 64  
      * @param indent   whether to use indented or raw format
 65  
      * @return document as a string
 66  
      */
 67  
     public static String jotDocument(org.w3c.dom.Document document, boolean indent) {
 68  0
         return jotNode(document.getDocumentElement(), indent);
 69  
     }
 70  
 
 71  
     /**
 72  
      * Retrieves a string representation of a node in a indented format.
 73  
      *
 74  
      * @param node the node
 75  
      * @return node as a string
 76  
      */
 77  
     public static String jotNode(org.jdom.Element node) {
 78  0
         return jotNode(node, true);
 79  
     }
 80  
 
 81  
     /**
 82  
      * Retrieves a string representation of a node in either indented or raw format.
 83  
      *
 84  
      * @param node   the node
 85  
      * @param indent whether to use indented or raw format
 86  
      * @return node as a string
 87  
      */
 88  
     public static String jotNode(org.jdom.Element node, boolean indent) {
 89  0
         XMLOutputter outputer = new XMLOutputter(getJdomFormat(indent));
 90  0
         StringWriter writer = new StringWriter();
 91  
         try {
 92  0
             outputer.output(node, writer);
 93  0
         } catch (IOException e) {
 94  0
             throw new XmlException("Could not write XML data export.", e);
 95  0
         }
 96  0
         return writer.toString();
 97  
     }
 98  
 
 99  
     /**
 100  
      * Retrieves a string representation of a node in a indented format.
 101  
      *
 102  
      * @param node the node
 103  
      * @return node as a string
 104  
      */
 105  
     public static String jotNode(org.w3c.dom.Node node) {
 106  0
         return jotNode(node, true);
 107  
     }
 108  
 
 109  
     /**
 110  
      * Retrieves a string representation of a node in either indented or raw format.
 111  
      *
 112  
      * @param node   the node
 113  
      * @param indent whether to use indented or raw format
 114  
      * @return node as a string
 115  
      */
 116  
     public static String jotNode(org.w3c.dom.Node node, boolean indent) {
 117  
         try {
 118  0
             return nodeToString(node, indent);
 119  0
         } catch (TransformerException te) {
 120  0
             throw new XmlException(te);
 121  
         }
 122  
     }
 123  
 
 124  
     /**
 125  
      * Internal function to retrieve a string representation of a node in either indented or raw format.
 126  
      *
 127  
      * @param node   the node
 128  
      * @param indent whether to use indented or raw format
 129  
      * @return node as a string
 130  
      * @throws TransformerException if the node cannnot be transformed to a string
 131  
      */
 132  
     private static String nodeToString(org.w3c.dom.Node node, boolean indent) throws TransformerException {
 133  0
         Source source = new DOMSource(node);
 134  0
         StringWriter writer = new StringWriter();
 135  0
         Result result = new StreamResult(writer);
 136  0
         Transformer transformer = TransformerFactory.newInstance().newTransformer();
 137  0
         transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
 138  0
         if (indent) {
 139  0
             transformer.setOutputProperty(OutputKeys.INDENT, "yes");
 140  
         }
 141  0
         transformer.transform(source, result);
 142  0
         return writer.toString();
 143  
     }
 144  
 
 145  
     /**
 146  
      * Internal function to choose a format based on a boolean flag.
 147  
      *
 148  
      * @param indent whether to use indented or raw format
 149  
      * @return The format
 150  
      */
 151  
     private static Format getJdomFormat(boolean indent) {
 152  0
         return indent ? Format.getPrettyFormat() : Format.getRawFormat();
 153  
     }
 154  
 }