-/*\r
- * Copyright 2001-2006 Internet2\r
- * \r
- * Licensed under the Apache License, Version 2.0 (the "License");\r
- * you may not use this file except in compliance with the License.\r
- * You may obtain a copy of the License at\r
- *\r
- * http://www.apache.org/licenses/LICENSE-2.0\r
- *\r
- * Unless required by applicable law or agreed to in writing, software\r
- * distributed under the License is distributed on an "AS IS" BASIS,\r
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- * See the License for the specific language governing permissions and\r
- * limitations under the License.\r
- */\r
-\r
-/**\r
- * XMLHelper.cpp\r
- * \r
- * A helper class for working with W3C DOM objects. \r
- */\r
-\r
-#include "internal.h"\r
-#include "exceptions.h"\r
-#include "util/XMLHelper.h"\r
-#include "util/XMLConstants.h"\r
-\r
-#include <xercesc/framework/MemBufFormatTarget.hpp>\r
-#include <xercesc/util/XMLUniDefs.hpp>\r
-\r
-using namespace xmltooling;\r
-\r
-static const XMLCh type[]={chLatin_t, chLatin_y, chLatin_p, chLatin_e, chNull };\r
- \r
-bool XMLHelper::hasXSIType(const DOMElement* e)\r
-{\r
- if (e) {\r
- if (e->hasAttributeNS(XMLConstants::XSI_NS, type)) {\r
- return true;\r
- }\r
- }\r
-\r
- return false;\r
-}\r
-\r
-QName* XMLHelper::getXSIType(const DOMElement* e)\r
-{\r
- DOMAttr* attribute = e->getAttributeNodeNS(XMLConstants::XSI_NS, type);\r
- if (attribute) {\r
- const XMLCh* attributeValue = attribute->getTextContent();\r
- if (attributeValue && *attributeValue) {\r
- int i;\r
- if ((i=XMLString::indexOf(attributeValue,chColon))>0) {\r
- XMLCh* prefix=new XMLCh[i+1];\r
- XMLString::subString(prefix,attributeValue,0,i);\r
- prefix[i]=chNull;\r
- QName* ret=new QName(e->lookupNamespaceURI(prefix), attributeValue + i + 1, prefix);\r
- delete[] prefix;\r
- return ret;\r
- }\r
- else {\r
- return new QName(e->lookupNamespaceURI(&chNull), attributeValue);\r
- }\r
- }\r
- }\r
-\r
- return NULL;\r
-}\r
-\r
-DOMAttr* XMLHelper::getIdAttribute(const DOMElement* domElement)\r
-{\r
- if(!domElement->hasAttributes()) {\r
- return NULL;\r
- }\r
- \r
- DOMNamedNodeMap* attributes = domElement->getAttributes();\r
- DOMAttr* attribute;\r
- for(XMLSize_t i = 0; i < attributes->getLength(); i++) {\r
- attribute = static_cast<DOMAttr*>(attributes->item(i));\r
- if(attribute->isId()) {\r
- return attribute;\r
- }\r
- }\r
- \r
- return NULL;\r
-}\r
-\r
-QName* XMLHelper::getNodeQName(const DOMNode* domNode)\r
-{\r
- if (domNode)\r
- return new QName(domNode->getNamespaceURI(), domNode->getLocalName(), domNode->getPrefix());\r
- return NULL; \r
-}\r
-\r
-QName* XMLHelper::getAttributeValueAsQName(const DOMAttr* attribute)\r
-{\r
- if (!attribute)\r
- return NULL;\r
- \r
- int i;\r
- const XMLCh* attributeValue=attribute->getTextContent();\r
- if (attributeValue && (i=XMLString::indexOf(attributeValue,chColon))>0) {\r
- XMLCh* prefix=new XMLCh[i+1];\r
- XMLString::subString(prefix,attributeValue,0,i);\r
- prefix[i]=chNull;\r
- QName* ret=new QName(attribute->lookupNamespaceURI(prefix), attributeValue + i + 1, prefix);\r
- delete[] prefix;\r
- return ret;\r
- }\r
- \r
- return new QName(attribute->lookupNamespaceURI(NULL), attributeValue);\r
-}\r
-\r
-DOMElement* XMLHelper::appendChildElement(DOMElement* parentElement, DOMElement* childElement)\r
-{\r
- DOMDocument* parentDocument = parentElement->getOwnerDocument();\r
- if (childElement->getOwnerDocument() != parentDocument) {\r
- childElement = static_cast<DOMElement*>(parentDocument->importNode(childElement, true));\r
- }\r
-\r
- parentElement->appendChild(childElement);\r
- return childElement;\r
-}\r
-\r
-const XMLCh* XMLHelper::getTextContent(const DOMElement* e)\r
-{\r
- DOMNode* child=e->getFirstChild();\r
- while (child) {\r
- if (child->getNodeType()==DOMNode::TEXT_NODE)\r
- return child->getNodeValue();\r
- child=child->getNextSibling();\r
- }\r
- return NULL;\r
-}\r
-\r
-DOMElement* XMLHelper::getFirstChildElement(const DOMNode* n)\r
-{\r
- DOMNode* child = n->getFirstChild();\r
- while (child && child->getNodeType() != DOMNode::ELEMENT_NODE)\r
- child = child->getNextSibling();\r
- if (child)\r
- return static_cast<DOMElement*>(child);\r
- return NULL;\r
-} \r
-\r
-DOMElement* XMLHelper::getLastChildElement(const DOMNode* n)\r
-{\r
- DOMNode* child = n->getLastChild();\r
- while (child && child->getNodeType() != DOMNode::ELEMENT_NODE)\r
- child = child->getPreviousSibling();\r
- if (child)\r
- return static_cast<DOMElement*>(child);\r
- return NULL;\r
-} \r
-\r
-DOMElement* XMLHelper::getFirstChildElement(const DOMNode* n, const XMLCh* ns, const XMLCh* localName)\r
-{\r
- DOMElement* e = getFirstChildElement(n);\r
- while (e && !isNodeNamed(e, ns, localName))\r
- e = getNextSiblingElement(e);\r
- return e;\r
-}\r
-\r
-DOMElement* XMLHelper::getLastChildElement(const DOMNode* n, const XMLCh* ns, const XMLCh* localName)\r
-{\r
- DOMElement* e = getLastChildElement(n);\r
- while (e && !isNodeNamed(e, ns, localName))\r
- e = getPreviousSiblingElement(e);\r
- return e;\r
-}\r
-\r
-DOMElement* XMLHelper::getNextSiblingElement(const DOMNode* n)\r
-{\r
- DOMNode* sib = n->getNextSibling();\r
- while (sib && sib->getNodeType() != DOMNode::ELEMENT_NODE)\r
- sib = sib->getNextSibling();\r
- if (sib)\r
- return static_cast<DOMElement*>(sib);\r
- return NULL;\r
-}\r
-\r
-DOMElement* XMLHelper::getPreviousSiblingElement(const DOMNode* n)\r
-{\r
- DOMNode* sib = n->getPreviousSibling();\r
- while (sib && sib->getNodeType() != DOMNode::ELEMENT_NODE)\r
- sib = sib->getPreviousSibling();\r
- if (sib)\r
- return static_cast<DOMElement*>(sib);\r
- return NULL;\r
-}\r
-\r
-DOMElement* XMLHelper::getNextSiblingElement(const DOMNode* n, const XMLCh* ns, const XMLCh* localName)\r
-{\r
- DOMElement* e = getNextSiblingElement(n);\r
- while (e && !isNodeNamed(e, ns, localName))\r
- e = getNextSiblingElement(e);\r
- return e;\r
-}\r
-\r
-DOMElement* XMLHelper::getPreviousSiblingElement(const DOMNode* n, const XMLCh* ns, const XMLCh* localName)\r
-{\r
- DOMElement* e = getPreviousSiblingElement(n);\r
- while (e && !isNodeNamed(e, ns, localName))\r
- e = getPreviousSiblingElement(e);\r
- return e;\r
-}\r
-\r
-void XMLHelper::serialize(const DOMElement* e, std::string& buf)\r
-{\r
- static const XMLCh impltype[] = { chLatin_L, chLatin_S, chNull };\r
- static const XMLCh UTF8[]={ chLatin_U, chLatin_T, chLatin_F, chDigit_8, chNull };\r
- DOMImplementation* impl=DOMImplementationRegistry::getDOMImplementation(impltype);\r
- DOMWriter* serializer=(static_cast<DOMImplementationLS*>(impl))->createDOMWriter();\r
- XercesJanitor<DOMWriter> janitor(serializer);\r
- serializer->setEncoding(UTF8);\r
- MemBufFormatTarget target;\r
- if (!serializer->writeNode(&target,*e))\r
- throw XMLParserException("unable to serialize XML");\r
- buf.erase();\r
- buf.append(reinterpret_cast<const char*>(target.getRawBuffer()),target.getLen());\r
-}\r
+/*
+ * Copyright 2001-2006 Internet2
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * XMLHelper.cpp
+ *
+ * A helper class for working with W3C DOM objects.
+ */
+
+#include "internal.h"
+#include "exceptions.h"
+#include "util/XMLHelper.h"
+#include "util/XMLConstants.h"
+
+#include <xercesc/framework/MemBufFormatTarget.hpp>
+#include <xercesc/util/XMLUniDefs.hpp>
+
+using namespace xmltooling;
+
+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;
+}
+
+QName* XMLHelper::getXSIType(const DOMElement* e)
+{
+ DOMAttr* attribute = e->getAttributeNodeNS(xmlconstants::XSI_NS, type);
+ if (attribute) {
+ const XMLCh* attributeValue = attribute->getTextContent();
+ if (attributeValue && *attributeValue) {
+ int i;
+ if ((i=XMLString::indexOf(attributeValue,chColon))>0) {
+ 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);
+ delete[] prefix;
+ return ret;
+ }
+ else {
+ return new QName(e->lookupNamespaceURI(&chNull), attributeValue);
+ }
+ }
+ }
+
+ return NULL;
+}
+
+DOMAttr* XMLHelper::getIdAttribute(const DOMElement* domElement)
+{
+ if(!domElement->hasAttributes()) {
+ return NULL;
+ }
+
+ DOMNamedNodeMap* attributes = domElement->getAttributes();
+ DOMAttr* attribute;
+ for(XMLSize_t i = 0; i < attributes->getLength(); i++) {
+ attribute = static_cast<DOMAttr*>(attributes->item(i));
+ if(attribute->isId()) {
+ return attribute;
+ }
+ }
+
+ return NULL;
+}
+
+QName* XMLHelper::getNodeQName(const DOMNode* domNode)
+{
+ if (domNode)
+ return new QName(domNode->getNamespaceURI(), domNode->getLocalName(), domNode->getPrefix());
+ return NULL;
+}
+
+QName* XMLHelper::getAttributeValueAsQName(const DOMAttr* attribute)
+{
+ if (!attribute)
+ return NULL;
+
+ int i;
+ const XMLCh* attributeValue=attribute->getTextContent();
+ if (attributeValue && (i=XMLString::indexOf(attributeValue,chColon))>0) {
+ 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);
+ delete[] prefix;
+ return ret;
+ }
+
+ return new QName(attribute->lookupNamespaceURI(NULL), attributeValue);
+}
+
+DOMElement* XMLHelper::appendChildElement(DOMElement* parentElement, DOMElement* childElement)
+{
+ DOMDocument* parentDocument = parentElement->getOwnerDocument();
+ if (childElement->getOwnerDocument() != parentDocument) {
+ childElement = static_cast<DOMElement*>(parentDocument->importNode(childElement, true));
+ }
+
+ parentElement->appendChild(childElement);
+ return childElement;
+}
+
+const XMLCh* XMLHelper::getTextContent(const DOMElement* e)
+{
+ DOMNode* child=e->getFirstChild();
+ while (child) {
+ if (child->getNodeType()==DOMNode::TEXT_NODE)
+ return child->getNodeValue();
+ child=child->getNextSibling();
+ }
+ return NULL;
+}
+
+DOMElement* XMLHelper::getFirstChildElement(const DOMNode* n, const XMLCh* localName)
+{
+ DOMNode* child = n->getFirstChild();
+ while (child && child->getNodeType() != DOMNode::ELEMENT_NODE)
+ child = child->getNextSibling();
+ if (child && localName) {
+ if (!XMLString::equals(localName,child->getLocalName()))
+ return getNextSiblingElement(child, localName);
+ }
+ return static_cast<DOMElement*>(child);
+}
+
+DOMElement* XMLHelper::getLastChildElement(const DOMNode* n, const XMLCh* localName)
+{
+ DOMNode* child = n->getLastChild();
+ while (child && child->getNodeType() != DOMNode::ELEMENT_NODE)
+ child = child->getPreviousSibling();
+ if (child && localName) {
+ if (!XMLString::equals(localName,child->getLocalName()))
+ return getPreviousSiblingElement(child, localName);
+ }
+ return static_cast<DOMElement*>(child);
+}
+
+DOMElement* XMLHelper::getFirstChildElement(const DOMNode* n, const XMLCh* ns, const XMLCh* localName)
+{
+ DOMElement* e = getFirstChildElement(n, localName);
+ while (e && !XMLString::equals(e->getNamespaceURI(),ns))
+ e = getNextSiblingElement(e, localName);
+ return e;
+}
+
+DOMElement* XMLHelper::getLastChildElement(const DOMNode* n, const XMLCh* ns, const XMLCh* localName)
+{
+ DOMElement* e = getLastChildElement(n, localName);
+ while (e && !XMLString::equals(e->getNamespaceURI(),ns))
+ e = getPreviousSiblingElement(e, localName);
+ return e;
+}
+
+DOMElement* XMLHelper::getNextSiblingElement(const DOMNode* n, const XMLCh* localName)
+{
+ DOMNode* sib = n->getNextSibling();
+ while (sib && sib->getNodeType() != DOMNode::ELEMENT_NODE)
+ sib = sib->getNextSibling();
+ if (sib && localName) {
+ if (!XMLString::equals(localName,sib->getLocalName()))
+ return getNextSiblingElement(sib, localName);
+ }
+ return static_cast<DOMElement*>(sib);
+}
+
+DOMElement* XMLHelper::getPreviousSiblingElement(const DOMNode* n, const XMLCh* localName)
+{
+ DOMNode* sib = n->getPreviousSibling();
+ while (sib && sib->getNodeType() != DOMNode::ELEMENT_NODE)
+ sib = sib->getPreviousSibling();
+ if (sib && localName) {
+ if (!XMLString::equals(localName,sib->getLocalName()))
+ return getPreviousSiblingElement(sib, localName);
+ }
+ return static_cast<DOMElement*>(sib);
+}
+
+DOMElement* XMLHelper::getNextSiblingElement(const DOMNode* n, const XMLCh* ns, const XMLCh* localName)
+{
+ DOMElement* e = getNextSiblingElement(n, localName);
+ while (e && !XMLString::equals(e->getNamespaceURI(),ns))
+ e = getNextSiblingElement(e, localName);
+ return e;
+}
+
+DOMElement* XMLHelper::getPreviousSiblingElement(const DOMNode* n, const XMLCh* ns, const XMLCh* localName)
+{
+ DOMElement* e = getPreviousSiblingElement(n, localName);
+ while (e && !XMLString::equals(e->getNamespaceURI(),ns))
+ e = getPreviousSiblingElement(e, localName);
+ return e;
+}
+
+void XMLHelper::serialize(const DOMElement* e, 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 };
+ DOMImplementation* impl=DOMImplementationRegistry::getDOMImplementation(impltype);
+ DOMWriter* serializer=(static_cast<DOMImplementationLS*>(impl))->createDOMWriter();
+ XercesJanitor<DOMWriter> janitor(serializer);
+ serializer->setEncoding(UTF8);
+ MemBufFormatTarget target;
+ if (!serializer->writeNode(&target,*e))
+ throw XMLParserException("unable to serialize XML");
+ buf.erase();
+ buf.append(reinterpret_cast<const char*>(target.getRawBuffer()),target.getLen());
+}