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  
 /**
 2  
  * Copyright 2005-2011 The Kuali Foundation
 3  
  *
 4  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  * http://www.opensource.org/licenses/ecl2.php
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 package org.kuali.rice.core.api.util.xml;
 17  
 
 18  
 import org.jdom.output.Format;
 19  
 import org.jdom.output.XMLOutputter;
 20  
 
 21  
 import javax.xml.transform.OutputKeys;
 22  
 import javax.xml.transform.Result;
 23  
 import javax.xml.transform.Source;
 24  
 import javax.xml.transform.Transformer;
 25  
 import javax.xml.transform.TransformerException;
 26  
 import javax.xml.transform.TransformerFactory;
 27  
 import javax.xml.transform.dom.DOMSource;
 28  
 import javax.xml.transform.stream.StreamResult;
 29  
 import java.io.IOException;
 30  
 import java.io.StringWriter;
 31  
 
 32  
 /**
 33  
  * Utility class that helps retrieve string representations of org.jdom & org.w3c
 34  
  * xml Objects
 35  
  */
 36  
 public final class XmlJotter {
 37  
 
 38  0
     private XmlJotter() {
 39  0
         throw new UnsupportedOperationException("do not call");
 40  
     }
 41  
 
 42  
     /**
 43  
      * Retrieves a string representation of a document in a indented format.
 44  
      *
 45  
      * @param document the document
 46  
      * @return document as a string
 47  
      */
 48  
     public static String jotDocument(org.jdom.Document document) {
 49  0
         return jotDocument(document, true);
 50  
     }
 51  
 
 52  
     /**
 53  
      * Retrieves a string representation of a document in either indented or raw format.
 54  
      *
 55  
      * @param document the document
 56  
      * @param indent   whether to use indented or raw format
 57  
      * @return document as a string
 58  
      */
 59  
     public static String jotDocument(org.jdom.Document document, boolean indent) {
 60  0
         XMLOutputter outputer = new XMLOutputter(getJdomFormat(indent));
 61  0
         StringWriter writer = new StringWriter();
 62  
         try {
 63  0
             outputer.output(document, writer);
 64  0
         } catch (IOException e) {
 65  0
             throw new XmlException("Could not write XML data export.", e);
 66  0
         }
 67  0
         return writer.toString();
 68  
     }
 69  
 
 70  
     /**
 71  
      * Retrieves a string representation of a document in a indented format.
 72  
      *
 73  
      * @param document the document
 74  
      * @return document as a string
 75  
      */
 76  
     public static String jotDocument(org.w3c.dom.Document document) {
 77  0
         return jotNode(document.getDocumentElement());
 78  
     }
 79  
 
 80  
     /**
 81  
      * Retrieves a string representation of a document in either indented or raw format.
 82  
      *
 83  
      * @param document the document
 84  
      * @param indent   whether to use indented or raw format
 85  
      * @return document as a string
 86  
      */
 87  
     public static String jotDocument(org.w3c.dom.Document document, boolean indent) {
 88  0
         return jotNode(document.getDocumentElement(), indent);
 89  
     }
 90  
 
 91  
     /**
 92  
      * Retrieves a string representation of a node in a indented format.
 93  
      *
 94  
      * @param node the node
 95  
      * @return node as a string
 96  
      */
 97  
     public static String jotNode(org.jdom.Element node) {
 98  0
         return jotNode(node, true);
 99  
     }
 100  
 
 101  
     /**
 102  
      * Retrieves a string representation of a node in either indented or raw format.
 103  
      *
 104  
      * @param node   the node
 105  
      * @param indent whether to use indented or raw format
 106  
      * @return node as a string
 107  
      */
 108  
     public static String jotNode(org.jdom.Element node, boolean indent) {
 109  0
         XMLOutputter outputer = new XMLOutputter(getJdomFormat(indent));
 110  0
         StringWriter writer = new StringWriter();
 111  
         try {
 112  0
             outputer.output(node, writer);
 113  0
         } catch (IOException e) {
 114  0
             throw new XmlException("Could not write XML data export.", e);
 115  0
         }
 116  0
         return writer.toString();
 117  
     }
 118  
 
 119  
     /**
 120  
      * Retrieves a string representation of a node in a indented format.
 121  
      *
 122  
      * @param node the node
 123  
      * @return node as a string
 124  
      */
 125  
     public static String jotNode(org.w3c.dom.Node node) {
 126  0
         return jotNode(node, true);
 127  
     }
 128  
 
 129  
     /**
 130  
      * Retrieves 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  
      */
 136  
     public static String jotNode(org.w3c.dom.Node node, boolean indent) {
 137  
         try {
 138  0
             return nodeToString(node, indent);
 139  0
         } catch (TransformerException te) {
 140  0
             throw new XmlException(te);
 141  
         }
 142  
     }
 143  
 
 144  
     /**
 145  
      * Internal function to retrieve a string representation of a node in either indented or raw format.
 146  
      *
 147  
      * @param node   the node
 148  
      * @param indent whether to use indented or raw format
 149  
      * @return node as a string
 150  
      * @throws TransformerException if the node cannnot be transformed to a string
 151  
      */
 152  
     private static String nodeToString(org.w3c.dom.Node node, boolean indent) throws TransformerException {
 153  0
         Source source = new DOMSource(node);
 154  0
         StringWriter writer = new StringWriter();
 155  0
         Result result = new StreamResult(writer);
 156  0
         Transformer transformer = TransformerFactory.newInstance().newTransformer();
 157  0
         transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
 158  0
         if (indent) {
 159  0
             transformer.setOutputProperty(OutputKeys.INDENT, "yes");
 160  
         }
 161  0
         transformer.transform(source, result);
 162  0
         return writer.toString();
 163  
     }
 164  
 
 165  
     /**
 166  
      * Internal function to choose a format based on a boolean flag.
 167  
      *
 168  
      * @param indent whether to use indented or raw format
 169  
      * @return The format
 170  
      */
 171  
     private static Format getJdomFormat(boolean indent) {
 172  0
         return indent ? Format.getPrettyFormat() : Format.getRawFormat();
 173  
     }
 174  
 }