Xerces 3 revisions.
[shibboleth/cpp-xmltooling.git] / xmltooling / AbstractAttributeExtensibleXMLObject.cpp
1 /*
2  *  Copyright 2001-2007 Internet2
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 /**
18  * AbstractAttributeExtensibleXMLObject.cpp
19  * 
20  * Extension of AbstractDOMCachingXMLObject that implements an AttributeExtensibleXMLObject. 
21  */
22
23 #include "internal.h"
24 #include "AbstractAttributeExtensibleXMLObject.h"
25
26 #include <algorithm>
27 #include <functional>
28
29 using namespace xmltooling;
30 using namespace std;
31
32 using xercesc::DOMAttr;
33 using xercesc::DOMElement;
34 using xercesc::XMLString;
35
36 set<QName> AttributeExtensibleXMLObject::m_idAttributeSet;
37
38 AbstractAttributeExtensibleXMLObject::~AbstractAttributeExtensibleXMLObject()
39 {
40     for (map<QName,XMLCh*>::iterator i=m_attributeMap.begin(); i!=m_attributeMap.end(); i++)
41         XMLString::release(&(i->second));
42 }
43
44 AbstractAttributeExtensibleXMLObject::AbstractAttributeExtensibleXMLObject(const AbstractAttributeExtensibleXMLObject& src)
45     : AbstractXMLObject(src)
46 {
47     m_idAttribute = m_attributeMap.end();
48     for (map<QName,XMLCh*>::const_iterator i=src.m_attributeMap.begin(); i!=src.m_attributeMap.end(); i++) {
49         m_attributeMap[i->first] = XMLString::replicate(i->second);
50     }
51     if (src.m_idAttribute != src.m_attributeMap.end()) {
52         m_idAttribute = m_attributeMap.find(src.m_idAttribute->first);
53     }
54 }
55
56 void AbstractAttributeExtensibleXMLObject::setAttribute(const QName& qualifiedName, const XMLCh* value, bool ID)
57 {
58     map<QName,XMLCh*>::iterator i=m_attributeMap.find(qualifiedName);
59     if (i!=m_attributeMap.end()) {
60         releaseThisandParentDOM();
61         XMLString::release(&(i->second));
62         if (value && *value) {
63             i->second=XMLString::replicate(value);
64             if (ID)
65                 m_idAttribute=i;
66         }
67         else {
68             if (m_idAttribute==i)
69                 m_idAttribute=m_attributeMap.end();
70             m_attributeMap.erase(i);
71         }
72     }
73     else if (value && *value) {
74         releaseThisandParentDOM();
75         m_attributeMap[qualifiedName]=XMLString::replicate(value);
76         if (ID)
77             m_idAttribute = m_attributeMap.find(qualifiedName);
78     }
79 }
80
81 void AbstractAttributeExtensibleXMLObject::unmarshallExtensionAttribute(const DOMAttr* attribute)
82 {
83     QName q(attribute->getNamespaceURI(),attribute->getLocalName(),attribute->getPrefix());
84     bool ID = attribute->isId() || isRegisteredIDAttribute(q);
85     setAttribute(q,attribute->getNodeValue(),ID);
86     if (ID) {
87 #ifdef XMLTOOLING_XERCESC_BOOLSETIDATTRIBUTE
88         attribute->getOwnerElement()->setIdAttributeNode(attribute, true);
89 #else
90         attribute->getOwnerElement()->setIdAttributeNode(attribute);
91 #endif
92     }
93 }
94
95 void AbstractAttributeExtensibleXMLObject::marshallExtensionAttributes(DOMElement* domElement) const
96 {
97     for (map<QName,XMLCh*>::const_iterator i=m_attributeMap.begin(); i!=m_attributeMap.end(); i++) {
98         DOMAttr* attr=domElement->getOwnerDocument()->createAttributeNS(i->first.getNamespaceURI(),i->first.getLocalPart());
99         if (i->first.hasPrefix())
100             attr->setPrefix(i->first.getPrefix());
101         attr->setNodeValue(i->second);
102         domElement->setAttributeNodeNS(attr);
103         if (m_idAttribute==i) {
104 #ifdef XMLTOOLING_XERCESC_BOOLSETIDATTRIBUTE
105             domElement->setIdAttributeNode(attr, true);
106 #else
107             domElement->setIdAttributeNode(attr);
108 #endif
109         }
110     }
111 }