Add pretty print option.
authorScott Cantor <cantor.2@osu.edu>
Wed, 5 Sep 2007 20:49:44 +0000 (20:49 +0000)
committerScott Cantor <cantor.2@osu.edu>
Wed, 5 Sep 2007 20:49:44 +0000 (20:49 +0000)
xmltooling/util/XMLHelper.cpp
xmltooling/util/XMLHelper.h

index 2ef4ecb..4ff6535 100644 (file)
@@ -232,7 +232,7 @@ DOMElement* XMLHelper::getPreviousSiblingElement(const DOMNode* n, const XMLCh*
     return e;
 }
 
-void XMLHelper::serialize(const DOMNode* n, std::string& buf)
+void XMLHelper::serialize(const DOMNode* n, std::string& buf, bool pretty)
 {
     static const XMLCh impltype[] = { chLatin_L, chLatin_S, chNull };
     static const XMLCh UTF8[]={ chLatin_U, chLatin_T, chLatin_F, chDigit_8, chNull };
@@ -240,6 +240,8 @@ void XMLHelper::serialize(const DOMNode* n, std::string& buf)
     DOMWriter* serializer=(static_cast<DOMImplementationLS*>(impl))->createDOMWriter();
     XercesJanitor<DOMWriter> janitor(serializer);
     serializer->setEncoding(UTF8);
+    if (pretty && serializer->canSetFeature(XMLUni::fgDOMWRTFormatPrettyPrint, pretty))
+        serializer->setFeature(XMLUni::fgDOMWRTFormatPrettyPrint, pretty);
     MemBufFormatTarget target;
     if (!serializer->writeNode(&target,*n))
         throw XMLParserException("unable to serialize XML");
@@ -267,7 +269,7 @@ namespace {
     };
 };
 
-ostream& XMLHelper::serialize(const DOMNode* n, ostream& out)
+ostream& XMLHelper::serialize(const DOMNode* n, ostream& out, bool pretty)
 {
     static const XMLCh impltype[] = { chLatin_L, chLatin_S, chNull };
     static const XMLCh UTF8[]={ chLatin_U, chLatin_T, chLatin_F, chDigit_8, chNull };
@@ -275,6 +277,8 @@ ostream& XMLHelper::serialize(const DOMNode* n, ostream& out)
     DOMWriter* serializer=(static_cast<DOMImplementationLS*>(impl))->createDOMWriter();
     XercesJanitor<DOMWriter> janitor(serializer);
     serializer->setEncoding(UTF8);
+    if (pretty && serializer->canSetFeature(XMLUni::fgDOMWRTFormatPrettyPrint, pretty))
+        serializer->setFeature(XMLUni::fgDOMWRTFormatPrettyPrint, pretty);
     StreamFormatTarget target(out);
     if (!serializer->writeNode(&target,*n))
         throw XMLParserException("unable to serialize XML");
index 3aacf24..99173c7 100644 (file)
@@ -249,20 +249,22 @@ namespace xmltooling {
          * Serializes the DOM node provided into a buffer using UTF-8 encoding and
          * the default XML serializer available. No manipulation or formatting is applied.
          * 
-         * @param n     node to serialize
-         * @param buf   buffer to serialize element into
+         * @param n         node to serialize
+         * @param buf       buffer to serialize element into
+         * @param pretty    enable pretty printing if supported
          */
-        static void serialize(const xercesc::DOMNode* n, std::string& buf);
+        static void serialize(const xercesc::DOMNode* n, std::string& buf, bool pretty=false);
 
         /**
          * Serializes the DOM node provided to a stream using UTF-8 encoding and
          * the default XML serializer available. No manipulation or formatting is applied.
          * 
-         * @param n     node to serialize
-         * @param out   stream to serialize element into
+         * @param n         node to serialize
+         * @param out       stream to serialize element into
+         * @param pretty    enable pretty printing if supported
          * @return reference to output stream
          */
-        static std::ostream& serialize(const xercesc::DOMNode* n, std::ostream& out);
+        static std::ostream& serialize(const xercesc::DOMNode* n, std::ostream& out, bool pretty=false);
     };
 
     /**