2 * Copyright 2001-2009 Internet2
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * AbstractAttributeExtensibleXMLObject.cpp
20 * Extension of AbstractDOMCachingXMLObject that implements an AttributeExtensibleXMLObject.
24 #include "AbstractAttributeExtensibleXMLObject.h"
25 #include "ElementExtensibleXMLObject.h"
26 #include "ElementProxy.h"
31 using namespace xmltooling;
34 using xercesc::DOMAttr;
35 using xercesc::DOMElement;
36 using xercesc::XMLString;
38 ElementExtensibleXMLObject::ElementExtensibleXMLObject()
42 ElementExtensibleXMLObject::~ElementExtensibleXMLObject()
46 ElementProxy::ElementProxy()
50 ElementProxy::~ElementProxy()
54 set<QName> AttributeExtensibleXMLObject::m_idAttributeSet;
56 AttributeExtensibleXMLObject::AttributeExtensibleXMLObject()
60 AttributeExtensibleXMLObject::~AttributeExtensibleXMLObject()
64 const set<QName>& AttributeExtensibleXMLObject::getRegisteredIDAttributes()
66 return m_idAttributeSet;
69 bool AttributeExtensibleXMLObject::isRegisteredIDAttribute(const QName& name)
71 return m_idAttributeSet.find(name)!=m_idAttributeSet.end();
74 void AttributeExtensibleXMLObject::registerIDAttribute(const QName& name)
76 m_idAttributeSet.insert(name);
79 void AttributeExtensibleXMLObject::deregisterIDAttribute(const QName& name)
81 m_idAttributeSet.erase(name);
84 void AttributeExtensibleXMLObject::deregisterIDAttributes()
86 m_idAttributeSet.clear();
89 AbstractAttributeExtensibleXMLObject::AbstractAttributeExtensibleXMLObject()
91 m_idAttribute = m_attributeMap.end();
94 AbstractAttributeExtensibleXMLObject::AbstractAttributeExtensibleXMLObject(const AbstractAttributeExtensibleXMLObject& src)
95 : AbstractXMLObject(src)
97 m_idAttribute = m_attributeMap.end();
98 for (map<QName,XMLCh*>::const_iterator i=src.m_attributeMap.begin(); i!=src.m_attributeMap.end(); i++) {
99 m_attributeMap[i->first] = XMLString::replicate(i->second);
101 if (src.m_idAttribute != src.m_attributeMap.end()) {
102 m_idAttribute = m_attributeMap.find(src.m_idAttribute->first);
106 AbstractAttributeExtensibleXMLObject::~AbstractAttributeExtensibleXMLObject()
108 for (map<QName,XMLCh*>::iterator i=m_attributeMap.begin(); i!=m_attributeMap.end(); i++)
109 XMLString::release(&(i->second));
112 const XMLCh* AbstractAttributeExtensibleXMLObject::getAttribute(const QName& qualifiedName) const
114 map<QName,XMLCh*>::const_iterator i=m_attributeMap.find(qualifiedName);
115 return (i==m_attributeMap.end()) ? NULL : i->second;
118 void AbstractAttributeExtensibleXMLObject::setAttribute(const QName& qualifiedName, const XMLCh* value, bool ID)
120 map<QName,XMLCh*>::iterator i=m_attributeMap.find(qualifiedName);
121 if (i!=m_attributeMap.end()) {
122 releaseThisandParentDOM();
123 XMLString::release(&(i->second));
124 if (value && *value) {
125 i->second=XMLString::replicate(value);
130 if (m_idAttribute==i)
131 m_idAttribute=m_attributeMap.end();
132 m_attributeMap.erase(i);
135 else if (value && *value) {
136 releaseThisandParentDOM();
137 m_attributeMap[qualifiedName]=XMLString::replicate(value);
139 m_idAttribute = m_attributeMap.find(qualifiedName);
143 const map<QName,XMLCh*>& AbstractAttributeExtensibleXMLObject::getExtensionAttributes() const
145 return m_attributeMap;
147 const XMLCh* AbstractAttributeExtensibleXMLObject::getXMLID() const
149 return (m_idAttribute == m_attributeMap.end()) ? NULL : m_idAttribute->second;
152 void AbstractAttributeExtensibleXMLObject::unmarshallExtensionAttribute(const DOMAttr* attribute)
154 QName q(attribute->getNamespaceURI(),attribute->getLocalName(),attribute->getPrefix());
155 bool ID = attribute->isId() || isRegisteredIDAttribute(q);
156 setAttribute(q,attribute->getNodeValue(),ID);
158 #ifdef XMLTOOLING_XERCESC_BOOLSETIDATTRIBUTE
159 attribute->getOwnerElement()->setIdAttributeNode(attribute, true);
161 attribute->getOwnerElement()->setIdAttributeNode(attribute);
166 void AbstractAttributeExtensibleXMLObject::marshallExtensionAttributes(DOMElement* domElement) const
168 for (map<QName,XMLCh*>::const_iterator i=m_attributeMap.begin(); i!=m_attributeMap.end(); i++) {
169 DOMAttr* attr=domElement->getOwnerDocument()->createAttributeNS(i->first.getNamespaceURI(),i->first.getLocalPart());
170 if (i->first.hasPrefix())
171 attr->setPrefix(i->first.getPrefix());
172 attr->setNodeValue(i->second);
173 domElement->setAttributeNodeNS(attr);
174 if (m_idAttribute==i) {
175 #ifdef XMLTOOLING_XERCESC_BOOLSETIDATTRIBUTE
176 domElement->setIdAttributeNode(attr, true);
178 domElement->setIdAttributeNode(attr);