Update copyright.
[shibboleth/cpp-xmltooling.git] / xmltooling / util / XMLHelper.cpp
index c876e97..d0fa6c2 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,6 +29,8 @@
 #include <xercesc/util/XMLUniDefs.hpp>
 
 using namespace xmltooling;
+using std::ostream;
+using std::list;
 
 static const XMLCh type[]={chLatin_t, chLatin_y, chLatin_p, chLatin_e, chNull };
     
@@ -85,6 +87,24 @@ DOMAttr* XMLHelper::getIdAttribute(const DOMElement* domElement)
     return NULL;
 }
 
+const XMLObject* XMLHelper::getXMLObjectById(const XMLObject& tree, const XMLCh* id)
+{
+    if (XMLString::equals(id, tree.getXMLID()))
+        return &tree;
+    
+    const 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;
+}
+
 QName* XMLHelper::getNodeQName(const DOMNode* domNode)
 {
     if (domNode)
@@ -213,7 +233,7 @@ DOMElement* XMLHelper::getPreviousSiblingElement(const DOMNode* n, const XMLCh*
     return e;
 }
 
-void XMLHelper::serialize(const DOMElement* e, std::string& buf)
+void XMLHelper::serialize(const DOMNode* n, std::string& buf)
 {
     static const XMLCh impltype[] = { chLatin_L, chLatin_S, chNull };
     static const XMLCh UTF8[]={ chLatin_U, chLatin_T, chLatin_F, chDigit_8, chNull };
@@ -222,8 +242,52 @@ void XMLHelper::serialize(const DOMElement* e, std::string& buf)
     XercesJanitor<DOMWriter> janitor(serializer);
     serializer->setEncoding(UTF8);
     MemBufFormatTarget target;
-    if (!serializer->writeNode(&target,*e))
+    if (!serializer->writeNode(&target,*n))
         throw XMLParserException("unable to serialize XML");
     buf.erase();
     buf.append(reinterpret_cast<const char*>(target.getRawBuffer()),target.getLen());
 }
+
+namespace {
+    class StreamFormatTarget : public XMLFormatTarget
+    {
+    public:
+        StreamFormatTarget(std::ostream& out) : m_out(out) {}
+        ~StreamFormatTarget() {}
+
+        void writeChars(const XMLByte *const toWrite, const unsigned int count, XMLFormatter *const formatter) {
+            m_out.write(reinterpret_cast<const char*>(toWrite),count);
+        }
+
+        void flush() {
+            m_out.flush();
+        }
+
+    private:
+        std::ostream& m_out;
+    };
+};
+
+ostream& XMLHelper::serialize(const DOMNode* n, ostream& out)
+{
+    static const XMLCh impltype[] = { chLatin_L, chLatin_S, chNull };
+    static const XMLCh UTF8[]={ chLatin_U, chLatin_T, chLatin_F, chDigit_8, chNull };
+    DOMImplementation* impl=DOMImplementationRegistry::getDOMImplementation(impltype);
+    DOMWriter* serializer=(static_cast<DOMImplementationLS*>(impl))->createDOMWriter();
+    XercesJanitor<DOMWriter> janitor(serializer);
+    serializer->setEncoding(UTF8);
+    StreamFormatTarget target(out);
+    if (!serializer->writeNode(&target,*n))
+        throw XMLParserException("unable to serialize XML");
+    return out;
+}
+
+ostream& xmltooling::operator<<(ostream& ostr, const DOMNode& node)
+{
+    return XMLHelper::serialize(&node, ostr);
+}
+
+ostream& xmltooling::operator<<(ostream& ostr, const XMLObject& obj)
+{
+    return ostr << *(obj.marshall());
+}