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