2 * Copyright 2001-2006 Internet2
\r
4 * Licensed under the Apache License, Version 2.0 (the "License");
\r
5 * you may not use this file except in compliance with the License.
\r
6 * You may obtain a copy of the License at
\r
8 * http://www.apache.org/licenses/LICENSE-2.0
\r
10 * Unless required by applicable law or agreed to in writing, software
\r
11 * distributed under the License is distributed on an "AS IS" BASIS,
\r
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
13 * See the License for the specific language governing permissions and
\r
14 * limitations under the License.
\r
20 * A helper class for working with W3C DOM objects.
\r
23 #include "internal.h"
\r
24 #include "exceptions.h"
\r
25 #include "util/XMLHelper.h"
\r
26 #include "util/XMLConstants.h"
\r
28 #include <xercesc/framework/MemBufFormatTarget.hpp>
\r
29 #include <xercesc/util/XMLUniDefs.hpp>
\r
31 using namespace xmltooling;
\r
33 static const XMLCh type[]={chLatin_t, chLatin_y, chLatin_p, chLatin_e, chNull };
\r
35 bool XMLHelper::hasXSIType(const DOMElement* e)
\r
38 if (e->hasAttributeNS(XMLConstants::XSI_NS, type)) {
\r
46 QName* XMLHelper::getXSIType(const DOMElement* e)
\r
48 DOMAttr* attribute = e->getAttributeNodeNS(XMLConstants::XSI_NS, type);
\r
51 const XMLCh* attributeValue = attribute->getTextContent();
\r
52 if (attributeValue && (i=XMLString::indexOf(attributeValue,chColon))>0) {
\r
53 XMLCh* prefix=new XMLCh[i+1];
\r
54 XMLString::subString(prefix,attributeValue,0,i);
\r
56 QName* ret=new QName(e->lookupNamespaceURI(prefix), attributeValue + i + 1, prefix);
\r
65 DOMAttr* XMLHelper::getIdAttribute(const DOMElement* domElement)
\r
67 if(!domElement->hasAttributes()) {
\r
71 DOMNamedNodeMap* attributes = domElement->getAttributes();
\r
73 for(XMLSize_t i = 0; i < attributes->getLength(); i++) {
\r
74 attribute = static_cast<DOMAttr*>(attributes->item(i));
\r
75 if(attribute->isId()) {
\r
83 QName* XMLHelper::getNodeQName(const DOMNode* domNode)
\r
86 return new QName(domNode->getNamespaceURI(), domNode->getLocalName(), domNode->getPrefix());
\r
90 QName* XMLHelper::getAttributeValueAsQName(const DOMAttr* attribute)
\r
96 const XMLCh* attributeValue=attribute->getTextContent();
\r
97 if (attributeValue && (i=XMLString::indexOf(attributeValue,chColon))>0) {
\r
98 XMLCh* prefix=new XMLCh[i+1];
\r
99 XMLString::subString(prefix,attributeValue,0,i);
\r
101 QName* ret=new QName(attribute->lookupNamespaceURI(prefix), attributeValue + i + 1, prefix);
\r
106 return new QName(attribute->lookupNamespaceURI(NULL), attributeValue);
\r
109 DOMElement* XMLHelper::appendChildElement(DOMElement* parentElement, DOMElement* childElement)
\r
111 DOMDocument* parentDocument = parentElement->getOwnerDocument();
\r
112 if (childElement->getOwnerDocument() != parentDocument) {
\r
113 childElement = static_cast<DOMElement*>(parentDocument->importNode(childElement, true));
\r
116 parentElement->appendChild(childElement);
\r
117 return childElement;
\r
120 const XMLCh* XMLHelper::getTextContent(const DOMElement* e)
\r
122 DOMNode* child=e->getFirstChild();
\r
124 if (child->getNodeType()==DOMNode::TEXT_NODE)
\r
125 return child->getNodeValue();
\r
126 child=child->getNextSibling();
\r
131 void XMLHelper::serialize(const DOMElement* e, std::string& buf)
\r
133 static const XMLCh impltype[] = { chLatin_L, chLatin_S, chNull };
\r
134 static const XMLCh UTF8[]={ chLatin_U, chLatin_T, chLatin_F, chDigit_8, chNull };
\r
135 DOMImplementation* impl=DOMImplementationRegistry::getDOMImplementation(impltype);
\r
136 DOMWriter* serializer=(static_cast<DOMImplementationLS*>(impl))->createDOMWriter();
\r
137 serializer->setEncoding(UTF8);
\r
139 MemBufFormatTarget target;
\r
140 if (!serializer->writeNode(&target,*e))
\r
141 throw XMLParserException("unable to serialize XML");
\r
143 buf.append(reinterpret_cast<const char*>(target.getRawBuffer()),target.getLen());
\r
144 serializer->release();
\r
147 serializer->release();
\r