|  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 |     | 
     | 
  |  15 |     | 
     | 
  |  16 |     | 
     | 
  |  17 |     | 
   public final class XmlJotter { | 
  |  18 |     | 
     | 
  |  19 |    0 |        private XmlJotter() { | 
  |  20 |    0 |            throw new UnsupportedOperationException("do not call"); | 
  |  21 |     | 
       }  | 
  |  22 |     | 
     | 
  |  23 |     | 
         | 
  |  24 |     | 
     | 
  |  25 |     | 
     | 
  |  26 |     | 
     | 
  |  27 |     | 
     | 
  |  28 |     | 
     | 
  |  29 |     | 
       public static String jotDocument(org.jdom.Document document) { | 
  |  30 |    0 |            return jotDocument(document, true);  | 
  |  31 |     | 
       }  | 
  |  32 |     | 
     | 
  |  33 |     | 
         | 
  |  34 |     | 
     | 
  |  35 |     | 
     | 
  |  36 |     | 
     | 
  |  37 |     | 
     | 
  |  38 |     | 
     | 
  |  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 |     | 
     | 
  |  53 |     | 
     | 
  |  54 |     | 
     | 
  |  55 |     | 
     | 
  |  56 |     | 
     | 
  |  57 |     | 
       public static String jotDocument(org.w3c.dom.Document document) { | 
  |  58 |    0 |            return jotNode(document.getDocumentElement());  | 
  |  59 |     | 
       }  | 
  |  60 |     | 
     | 
  |  61 |     | 
         | 
  |  62 |     | 
     | 
  |  63 |     | 
     | 
  |  64 |     | 
     | 
  |  65 |     | 
     | 
  |  66 |     | 
     | 
  |  67 |     | 
     | 
  |  68 |     | 
       public static String jotDocument(org.w3c.dom.Document document, boolean indent) { | 
  |  69 |    0 |            return jotNode(document.getDocumentElement(), indent);  | 
  |  70 |     | 
       }  | 
  |  71 |     | 
     | 
  |  72 |     | 
         | 
  |  73 |     | 
     | 
  |  74 |     | 
     | 
  |  75 |     | 
     | 
  |  76 |     | 
     | 
  |  77 |     | 
     | 
  |  78 |     | 
       public static String jotNode(org.jdom.Element node) { | 
  |  79 |    0 |            return jotNode(node, true);  | 
  |  80 |     | 
       }  | 
  |  81 |     | 
     | 
  |  82 |     | 
         | 
  |  83 |     | 
     | 
  |  84 |     | 
     | 
  |  85 |     | 
     | 
  |  86 |     | 
     | 
  |  87 |     | 
     | 
  |  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 |     | 
     | 
  |  102 |     | 
     | 
  |  103 |     | 
     | 
  |  104 |     | 
     | 
  |  105 |     | 
     | 
  |  106 |     | 
       public static String jotNode(org.w3c.dom.Node node) { | 
  |  107 |    0 |            return jotNode(node, true);  | 
  |  108 |     | 
       }  | 
  |  109 |     | 
     | 
  |  110 |     | 
         | 
  |  111 |     | 
     | 
  |  112 |     | 
     | 
  |  113 |     | 
     | 
  |  114 |     | 
     | 
  |  115 |     | 
     | 
  |  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 |     | 
     | 
  |  127 |     | 
     | 
  |  128 |     | 
     | 
  |  129 |     | 
     | 
  |  130 |     | 
     | 
  |  131 |     | 
     | 
  |  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 |     | 
     | 
  |  148 |     | 
     | 
  |  149 |     | 
     | 
  |  150 |     | 
     | 
  |  151 |     | 
     | 
  |  152 |     | 
       private static Format getJdomFormat(boolean indent) { | 
  |  153 |    0 |            return indent ? Format.getPrettyFormat() : Format.getRawFormat();  | 
  |  154 |     | 
       }  | 
  |  155 |     | 
   }  |