Xerces 3 revisions.
[shibboleth/cpp-xmltooling.git] / xmltooling / util / XMLHelper.cpp
index 50f450e..77f8d0f 100644 (file)
@@ -1,5 +1,5 @@
 /*
- *  Copyright 2001-2006 Internet2
+ *  Copyright 2001-2007 Internet2
  * 
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -29,8 +29,8 @@
 #include <xercesc/util/XMLUniDefs.hpp>
 
 using namespace xmltooling;
-using std::ostream;
-using std::list;
+using namespace xercesc;
+using namespace std;
 
 static const XMLCh type[]={chLatin_t, chLatin_y, chLatin_p, chLatin_e, chNull };
     
@@ -45,7 +45,7 @@ bool XMLHelper::hasXSIType(const DOMElement* e)
     return false;
 }
 
-QName* XMLHelper::getXSIType(const DOMElement* e)
+xmltooling::QName* XMLHelper::getXSIType(const DOMElement* e)
 {
     DOMAttr* attribute = e->getAttributeNodeNS(xmlconstants::XSI_NS, type);
     if (attribute) {
@@ -56,12 +56,12 @@ QName* XMLHelper::getXSIType(const DOMElement* e)
                 XMLCh* prefix=new XMLCh[i+1];
                 XMLString::subString(prefix,attributeValue,0,i);
                 prefix[i]=chNull;
-                QName* ret=new QName(e->lookupNamespaceURI(prefix), attributeValue + i + 1, prefix);
+                xmltooling::QName* ret=new xmltooling::QName(e->lookupNamespaceURI(prefix), attributeValue + i + 1, prefix);
                 delete[] prefix;
                 return ret;
             }
             else {
-                return new QName(e->lookupNamespaceURI(&chNull), attributeValue);
+                return new xmltooling::QName(e->lookupNamespaceURI(NULL), attributeValue);
             }
         }
     }
@@ -105,14 +105,32 @@ const XMLObject* XMLHelper::getXMLObjectById(const XMLObject& tree, const XMLCh*
     return NULL;
 }
 
-QName* XMLHelper::getNodeQName(const DOMNode* domNode)
+XMLObject* XMLHelper::getXMLObjectById(XMLObject& tree, const XMLCh* id)
+{
+    if (XMLString::equals(id, tree.getXMLID()))
+        return &tree;
+    
+    XMLObject* ret;
+    const list<XMLObject*>& children = tree.getOrderedChildren();
+    for (list<XMLObject*>::const_iterator i=children.begin(); i!=children.end(); ++i) {
+        if (*i) {
+            ret = getXMLObjectById(*(*i), id);
+            if (ret)
+                return ret;
+        }
+    }
+    
+    return NULL;
+}
+
+xmltooling::QName* XMLHelper::getNodeQName(const DOMNode* domNode)
 {
     if (domNode)
-        return new QName(domNode->getNamespaceURI(), domNode->getLocalName(), domNode->getPrefix());
+        return new xmltooling::QName(domNode->getNamespaceURI(), domNode->getLocalName(), domNode->getPrefix());
     return NULL; 
 }
 
-QName* XMLHelper::getAttributeValueAsQName(const DOMAttr* attribute)
+xmltooling::QName* XMLHelper::getAttributeValueAsQName(const DOMAttr* attribute)
 {
     if (!attribute)
         return NULL;
@@ -123,12 +141,12 @@ QName* XMLHelper::getAttributeValueAsQName(const DOMAttr* attribute)
         XMLCh* prefix=new XMLCh[i+1];
         XMLString::subString(prefix,attributeValue,0,i);
         prefix[i]=chNull;
-        QName* ret=new QName(attribute->lookupNamespaceURI(prefix), attributeValue + i + 1, prefix);
+        xmltooling::QName* ret=new xmltooling::QName(attribute->lookupNamespaceURI(prefix), attributeValue + i + 1, prefix);
         delete[] prefix;
         return ret;
     }
     
-    return new QName(attribute->lookupNamespaceURI(NULL), attributeValue);
+    return new xmltooling::QName(attribute->lookupNamespaceURI(NULL), attributeValue);
 }
 
 DOMElement* XMLHelper::appendChildElement(DOMElement* parentElement, DOMElement* childElement)
@@ -233,17 +251,35 @@ 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 };
+
+    MemBufFormatTarget target;
     DOMImplementation* impl=DOMImplementationRegistry::getDOMImplementation(impltype);
-    DOMWriter* serializer=(static_cast<DOMImplementationLS*>(impl))->createDOMWriter();
+
+#ifdef XMLTOOLING_XERCESC_COMPLIANT_DOMLS
+    DOMLSSerializer* serializer = static_cast<DOMImplementationLS*>(impl)->createLSSerializer();
+    XercesJanitor<DOMLSSerializer> janitor(serializer);
+    if (pretty && serializer->getDomConfig()->canSetParameter(XMLUni::fgDOMWRTFormatPrettyPrint, pretty))
+        serializer->getDomConfig()->setParameter(XMLUni::fgDOMWRTFormatPrettyPrint, pretty);
+    DOMLSOutput *theOutput = static_cast<DOMImplementationLS*>(impl)->createLSOutput();\r
+    XercesJanitor<DOMLSOutput> j_theOutput(theOutput);\r
+    theOutput->setEncoding(UTF8);\r
+    theOutput->setByteStream(&target);
+    if (!serializer->write(n, theOutput))
+        throw XMLParserException("unable to serialize XML");
+#else
+    DOMWriter* serializer = static_cast<DOMImplementationLS*>(impl)->createDOMWriter();
     XercesJanitor<DOMWriter> janitor(serializer);
     serializer->setEncoding(UTF8);
-    MemBufFormatTarget target;
-    if (!serializer->writeNode(&target,*n))
+    if (pretty && serializer->canSetFeature(XMLUni::fgDOMWRTFormatPrettyPrint, pretty))
+        serializer->setFeature(XMLUni::fgDOMWRTFormatPrettyPrint, pretty);
+    if (!serializer->writeNode(&target, *n))
         throw XMLParserException("unable to serialize XML");
+#endif
+
     buf.erase();
     buf.append(reinterpret_cast<const char*>(target.getRawBuffer()),target.getLen());
 }
@@ -255,7 +291,7 @@ namespace {
         StreamFormatTarget(std::ostream& out) : m_out(out) {}
         ~StreamFormatTarget() {}
 
-        void writeChars(const XMLByte *const toWrite, const unsigned int count, XMLFormatter *const formatter) {
+        void writeChars(const XMLByte *const toWrite, const xsecsize_t count, XMLFormatter *const formatter) {
             m_out.write(reinterpret_cast<const char*>(toWrite),count);
         }
 
@@ -268,26 +304,44 @@ namespace {
     };
 };
 
-void XMLHelper::serialize(const DOMNode* n, std::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 };
+
+    StreamFormatTarget target(out);
     DOMImplementation* impl=DOMImplementationRegistry::getDOMImplementation(impltype);
+
+#ifdef XMLTOOLING_XERCESC_COMPLIANT_DOMLS
+    DOMLSSerializer* serializer = static_cast<DOMImplementationLS*>(impl)->createLSSerializer();
+    XercesJanitor<DOMLSSerializer> janitor(serializer);
+    if (pretty && serializer->getDomConfig()->canSetParameter(XMLUni::fgDOMWRTFormatPrettyPrint, pretty))
+        serializer->getDomConfig()->setParameter(XMLUni::fgDOMWRTFormatPrettyPrint, pretty);
+    DOMLSOutput *theOutput = static_cast<DOMImplementationLS*>(impl)->createLSOutput();\r
+    XercesJanitor<DOMLSOutput> j_theOutput(theOutput);\r
+    theOutput->setEncoding(UTF8);\r
+    theOutput->setByteStream(&target);
+    if (!serializer->write(n, theOutput))
+        throw XMLParserException("unable to serialize XML");
+#else
     DOMWriter* serializer=(static_cast<DOMImplementationLS*>(impl))->createDOMWriter();
     XercesJanitor<DOMWriter> janitor(serializer);
     serializer->setEncoding(UTF8);
-    StreamFormatTarget target(out);
+    if (pretty && serializer->canSetFeature(XMLUni::fgDOMWRTFormatPrettyPrint, pretty))
+        serializer->setFeature(XMLUni::fgDOMWRTFormatPrettyPrint, pretty);
     if (!serializer->writeNode(&target,*n))
         throw XMLParserException("unable to serialize XML");
+#endif
+
+    return out;
 }
 
 ostream& xmltooling::operator<<(ostream& ostr, const DOMNode& node)
 {
-    XMLHelper::serialize(&node, ostr);
-    return ostr;
+    return XMLHelper::serialize(&node, ostr);
 }
 
 ostream& xmltooling::operator<<(ostream& ostr, const XMLObject& obj)
 {
-    return ostr << obj.marshall();
+    return ostr << *(obj.marshall());
 }