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 "util/XMLHelper.h"
\r
25 #include "util/XMLConstants.h"
\r
27 #include <xercesc/util/XMLUniDefs.hpp>
\r
29 using namespace xmltooling;
\r
31 static const XMLCh type[]={chLatin_t, chLatin_y, chLatin_p, chLatin_e, chNull };
\r
33 bool XMLHelper::hasXSIType(DOMElement* e)
\r
36 if (e->hasAttributeNS(XMLConstants::XSI_NS, type)) {
\r
44 QName* XMLHelper::getXSIType(DOMElement* e)
\r
46 DOMAttr* attribute = e->getAttributeNodeNS(XMLConstants::XSI_NS, type);
\r
49 const XMLCh* attributeValue = attribute->getTextContent();
\r
50 if (attributeValue && (i=XMLString::indexOf(attributeValue,chColon))>0) {
\r
51 XMLCh* prefix=new XMLCh[i+1];
\r
52 XMLString::subString(prefix,attributeValue,0,i);
\r
54 QName* ret=new QName(e->lookupNamespaceURI(prefix), attributeValue + i + 1, prefix);
\r
63 DOMAttr* XMLHelper::getIdAttribute(DOMElement* domElement)
\r
65 if(!domElement->hasAttributes()) {
\r
69 DOMNamedNodeMap* attributes = domElement->getAttributes();
\r
71 for(XMLSize_t i = 0; i < attributes->getLength(); i++) {
\r
72 attribute = static_cast<DOMAttr*>(attributes->item(i));
\r
73 if(attribute->isId()) {
\r
81 QName* XMLHelper::getNodeQName(DOMNode* domNode)
\r
84 return new QName(domNode->getNamespaceURI(), domNode->getLocalName(), domNode->getPrefix());
\r
88 QName* XMLHelper::getAttributeValueAsQName(DOMAttr* attribute)
\r
94 const XMLCh* attributeValue=attribute->getTextContent();
\r
95 if (attributeValue && (i=XMLString::indexOf(attributeValue,chColon))>0) {
\r
96 XMLCh* prefix=new XMLCh[i+1];
\r
97 XMLString::subString(prefix,attributeValue,0,i);
\r
99 QName* ret=new QName(attribute->lookupNamespaceURI(prefix), attributeValue + i + 1, prefix);
\r
104 return new QName(attribute->lookupNamespaceURI(NULL), attributeValue);
\r
107 DOMElement* XMLHelper::appendChildElement(DOMElement* parentElement, DOMElement* childElement)
\r
109 DOMDocument* parentDocument = parentElement->getOwnerDocument();
\r
110 if (childElement->getOwnerDocument() != parentDocument) {
\r
111 childElement = static_cast<DOMElement*>(parentDocument->importNode(childElement, true));
\r
114 parentElement->appendChild(childElement);
\r
115 return childElement;
\r