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