Initial parser APIs
[shibboleth/cpp-xmltooling.git] / xmltooling / util / XMLHelper.cpp
1 /*\r
2  *  Copyright 2001-2006 Internet2\r
3  * \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
7  *\r
8  *     http://www.apache.org/licenses/LICENSE-2.0\r
9  *\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
15  */\r
16 \r
17 /**\r
18  * XMLHelper.cpp\r
19  * \r
20  * A helper class for working with W3C DOM objects. \r
21  */\r
22 \r
23 #include "internal.h"\r
24 #include "util/XMLHelper.h"\r
25 #include "util/XMLConstants.h"\r
26 \r
27 #include <xercesc/util/XMLUniDefs.hpp>\r
28 \r
29 using namespace xmltooling;\r
30 \r
31 static const XMLCh type[]={chLatin_t, chLatin_y, chLatin_p, chLatin_e, chNull };\r
32     \r
33 bool XMLHelper::hasXSIType(DOMElement* e)\r
34 {\r
35     if (e) {\r
36         if (e->hasAttributeNS(XMLConstants::XSI_NS, type)) {\r
37             return true;\r
38         }\r
39     }\r
40 \r
41     return false;\r
42 }\r
43 \r
44 QName* XMLHelper::getXSIType(DOMElement* e)\r
45 {\r
46     DOMAttr* attribute = e->getAttributeNodeNS(XMLConstants::XSI_NS, type);\r
47     if (attribute) {\r
48         int i;\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
53             prefix[i]=chNull;\r
54             QName* ret=new QName(e->lookupNamespaceURI(prefix), attributeValue + i + 1, prefix);\r
55             delete[] prefix;\r
56             return ret;\r
57         }\r
58     }\r
59 \r
60     return NULL;\r
61 }\r
62 \r
63 DOMAttr* XMLHelper::getIdAttribute(DOMElement* domElement)\r
64 {\r
65     if(!domElement->hasAttributes()) {\r
66         return NULL;\r
67     }\r
68     \r
69     DOMNamedNodeMap* attributes = domElement->getAttributes();\r
70     DOMAttr* attribute;\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
74             return attribute;\r
75         }\r
76     }\r
77     \r
78     return NULL;\r
79 }\r
80 \r
81 QName* XMLHelper::getNodeQName(DOMNode* domNode)\r
82 {\r
83     if (domNode)\r
84         return new QName(domNode->getNamespaceURI(), domNode->getLocalName(), domNode->getPrefix());\r
85     return NULL; \r
86 }\r
87 \r
88 QName* XMLHelper::getAttributeValueAsQName(DOMAttr* attribute)\r
89 {\r
90     if (!attribute)\r
91         return NULL;\r
92     \r
93     int i;\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
98         prefix[i]=chNull;\r
99         QName* ret=new QName(attribute->lookupNamespaceURI(prefix), attributeValue + i + 1, prefix);\r
100         delete[] prefix;\r
101         return ret;\r
102     }\r
103     \r
104     return new QName(attribute->lookupNamespaceURI(NULL), attributeValue);\r
105 }\r
106 \r
107 DOMElement* XMLHelper::appendChildElement(DOMElement* parentElement, DOMElement* childElement)\r
108 {\r
109     DOMDocument* parentDocument = parentElement->getOwnerDocument();\r
110     if (childElement->getOwnerDocument() != parentDocument) {\r
111         childElement = static_cast<DOMElement*>(parentDocument->importNode(childElement, true));\r
112     }\r
113 \r
114     parentElement->appendChild(childElement);\r
115     return childElement;\r
116 }\r