/*
- * Copyright 2001-2006 Internet2
+ * Copyright 2001-2009 Internet2
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
#include "internal.h"
#include "exceptions.h"
+#include "QName.h"
+#include "XMLObject.h"
#include "util/XMLHelper.h"
#include "util/XMLConstants.h"
#include <xercesc/util/XMLUniDefs.hpp>
using namespace xmltooling;
+using namespace xercesc;
+using namespace std;
static const XMLCh type[]={chLatin_t, chLatin_y, chLatin_p, chLatin_e, chNull };
bool XMLHelper::hasXSIType(const DOMElement* e)
{
- if (e) {
- if (e->hasAttributeNS(XMLConstants::XSI_NS, type)) {
- return true;
- }
- }
-
- return false;
+ return (e && e->hasAttributeNS(xmlconstants::XSI_NS, type));
}
-QName* XMLHelper::getXSIType(const DOMElement* e)
+xmltooling::QName* XMLHelper::getXSIType(const DOMElement* e)
{
- DOMAttr* attribute = e->getAttributeNodeNS(XMLConstants::XSI_NS, type);
+ DOMAttr* attribute = e->getAttributeNodeNS(xmlconstants::XSI_NS, type);
if (attribute) {
const XMLCh* attributeValue = attribute->getTextContent();
if (attributeValue && *attributeValue) {
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);
}
}
}
return NULL;
}
-QName* XMLHelper::getNodeQName(const DOMNode* domNode)
+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;
+}
+
+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;
+}
+
+void XMLHelper::getNonVisiblyUsedPrefixes(const XMLObject& tree, set<xstring>& prefixes)
+{
+ set<xstring> child_prefixes;
+ const list<XMLObject*>& children = tree.getOrderedChildren();
+ for (list<XMLObject*>::const_iterator i = children.begin(); i != children.end(); ++i) {
+ if (*i)
+ getNonVisiblyUsedPrefixes(*(*i), child_prefixes);
+ }
+ const set<Namespace>& nsset = tree.getNamespaces();
+ for (set<Namespace>::const_iterator ns = nsset.begin(); ns != nsset.end(); ++ns) {
+ // Check for xmlns:xml.
+ if (XMLString::equals(ns->getNamespacePrefix(), xmlconstants::XML_PREFIX) && XMLString::equals(ns->getNamespaceURI(), xmlconstants::XML_NS))
+ continue;
+ switch (ns->usage()) {
+ case Namespace::Indeterminate:
+ break;
+ case Namespace::VisiblyUsed:
+ child_prefixes.erase(ns->getNamespacePrefix() ? ns->getNamespacePrefix() : &chNull);
+ break;
+ case Namespace::NonVisiblyUsed:
+ prefixes.insert(ns->getNamespacePrefix() ? ns->getNamespacePrefix() : &chNull);
+ break;
+ }
+ }
+ prefixes.insert(child_prefixes.begin(), child_prefixes.end());
+}
+
+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)
+{
+ return getNodeValueAsQName(attribute);
+}
+
+xmltooling::QName* XMLHelper::getNodeValueAsQName(const DOMNode* domNode)
{
- if (!attribute)
+ if (!domNode)
return NULL;
int i;
- const XMLCh* attributeValue=attribute->getTextContent();
- if (attributeValue && (i=XMLString::indexOf(attributeValue,chColon))>0) {
+ const XMLCh* value=domNode->getTextContent();
+ if (value && (i=XMLString::indexOf(value,chColon))>0) {
XMLCh* prefix=new XMLCh[i+1];
- XMLString::subString(prefix,attributeValue,0,i);
+ XMLString::subString(prefix,value,0,i);
prefix[i]=chNull;
- QName* ret=new QName(attribute->lookupNamespaceURI(prefix), attributeValue + i + 1, prefix);
+ xmltooling::QName* ret=new xmltooling::QName(domNode->lookupNamespaceURI(prefix), value + i + 1, prefix);
delete[] prefix;
return ret;
}
- return new QName(attribute->lookupNamespaceURI(NULL), attributeValue);
+ return new xmltooling::QName(domNode->lookupNamespaceURI(NULL), value);
}
DOMElement* XMLHelper::appendChildElement(DOMElement* parentElement, DOMElement* childElement)
return childElement;
}
+bool XMLHelper::isNodeNamed(const xercesc::DOMNode* n, const XMLCh* ns, const XMLCh* local)
+{
+ return (n && XMLString::equals(local,n->getLocalName()) && XMLString::equals(ns,n->getNamespaceURI()));
+}
+
const XMLCh* XMLHelper::getTextContent(const DOMElement* e)
{
DOMNode* child=e->getFirstChild();
return e;
}
-void XMLHelper::serialize(const DOMElement* e, 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 };
+ static const XMLCh UTF8[]={ chLatin_U, chLatin_T, chLatin_F, chDash, 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();
+ XercesJanitor<DOMLSOutput> j_theOutput(theOutput);
+ theOutput->setEncoding(UTF8);
+ 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,*e))
+ 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());
}
+
+namespace {
+ class StreamFormatTarget : public XMLFormatTarget
+ {
+ public:
+ StreamFormatTarget(std::ostream& out) : m_out(out) {}
+ ~StreamFormatTarget() {}
+
+ void writeChars(const XMLByte *const toWrite, const xsecsize_t 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, bool pretty)
+{
+ static const XMLCh impltype[] = { chLatin_L, chLatin_S, chNull };
+ static const XMLCh UTF8[]={ chLatin_U, chLatin_T, chLatin_F, chDash, 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();
+ XercesJanitor<DOMLSOutput> j_theOutput(theOutput);
+ theOutput->setEncoding(UTF8);
+ 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);
+ 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)
+{
+ return XMLHelper::serialize(&node, ostr);
+}
+
+ostream& xmltooling::operator<<(ostream& ostr, const XMLObject& obj)
+{
+ return ostr << *(obj.marshall());
+}