2 * Licensed to the University Corporation for Advanced Internet
3 * Development, Inc. (UCAID) under one or more contributor license
4 * agreements. See the NOTICE file distributed with this work for
5 * additional information regarding copyright ownership.
7 * UCAID licenses this file to you under the Apache License,
8 * Version 2.0 (the "License"); you may not use this file except
9 * in compliance with the License. You may obtain a copy of the
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
17 * either express or implied. See the License for the specific
18 * language governing permissions and limitations under the License.
22 * AbstractAttributeExtensibleXMLObject.cpp
24 * Extension of AbstractDOMCachingXMLObject that implements an AttributeExtensibleXMLObject.
28 #include "AbstractAttributeExtensibleXMLObject.h"
29 #include "ElementExtensibleXMLObject.h"
30 #include "ElementProxy.h"
34 #include <xercesc/util/XMLUniDefs.hpp>
36 using namespace xmltooling;
38 using xercesc::chColon;
40 using xercesc::DOMAttr;
41 using xercesc::DOMElement;
42 using xercesc::XMLString;
44 ElementExtensibleXMLObject::ElementExtensibleXMLObject()
48 ElementExtensibleXMLObject::~ElementExtensibleXMLObject()
52 ElementProxy::ElementProxy()
56 ElementProxy::~ElementProxy()
60 set<QName> AttributeExtensibleXMLObject::m_idAttributeSet;
62 AttributeExtensibleXMLObject::AttributeExtensibleXMLObject()
66 AttributeExtensibleXMLObject::~AttributeExtensibleXMLObject()
70 const set<QName>& AttributeExtensibleXMLObject::getRegisteredIDAttributes()
72 return m_idAttributeSet;
75 bool AttributeExtensibleXMLObject::isRegisteredIDAttribute(const QName& name)
77 return m_idAttributeSet.find(name)!=m_idAttributeSet.end();
80 void AttributeExtensibleXMLObject::registerIDAttribute(const QName& name)
82 m_idAttributeSet.insert(name);
85 void AttributeExtensibleXMLObject::deregisterIDAttribute(const QName& name)
87 m_idAttributeSet.erase(name);
90 void AttributeExtensibleXMLObject::deregisterIDAttributes()
92 m_idAttributeSet.clear();
95 AbstractAttributeExtensibleXMLObject::AbstractAttributeExtensibleXMLObject()
97 m_idAttribute = m_attributeMap.end();
100 AbstractAttributeExtensibleXMLObject::AbstractAttributeExtensibleXMLObject(const AbstractAttributeExtensibleXMLObject& src)
101 : AbstractXMLObject(src)
103 m_idAttribute = m_attributeMap.end();
104 for (map<QName,XMLCh*>::const_iterator i=src.m_attributeMap.begin(); i!=src.m_attributeMap.end(); i++) {
105 m_attributeMap[i->first] = XMLString::replicate(i->second);
107 if (src.m_idAttribute != src.m_attributeMap.end()) {
108 m_idAttribute = m_attributeMap.find(src.m_idAttribute->first);
112 AbstractAttributeExtensibleXMLObject::~AbstractAttributeExtensibleXMLObject()
114 for (map<QName,XMLCh*>::iterator i=m_attributeMap.begin(); i!=m_attributeMap.end(); i++)
115 XMLString::release(&(i->second));
118 const XMLCh* AbstractAttributeExtensibleXMLObject::getAttribute(const QName& qualifiedName) const
120 map<QName,XMLCh*>::const_iterator i=m_attributeMap.find(qualifiedName);
121 return (i==m_attributeMap.end()) ? nullptr : i->second;
124 void AbstractAttributeExtensibleXMLObject::setAttribute(const QName& qualifiedName, const XMLCh* value, bool ID)
126 map<QName,XMLCh*>::iterator i=m_attributeMap.find(qualifiedName);
127 if (i!=m_attributeMap.end()) {
128 releaseThisandParentDOM();
129 XMLString::release(&(i->second));
130 if (value && *value) {
131 i->second=XMLString::replicate(value);
136 if (m_idAttribute==i)
137 m_idAttribute=m_attributeMap.end();
138 m_attributeMap.erase(i);
141 else if (value && *value) {
142 releaseThisandParentDOM();
143 m_attributeMap[qualifiedName]=XMLString::replicate(value);
145 m_idAttribute = m_attributeMap.find(qualifiedName);
146 Namespace newNamespace(qualifiedName.getNamespaceURI(), qualifiedName.getPrefix(), false, Namespace::VisiblyUsed);
147 addNamespace(newNamespace);
151 void AttributeExtensibleXMLObject::setAttribute(const QName& qualifiedName, const QName& value)
153 if (!value.hasLocalPart())
156 if (value.hasPrefix()) {
157 xstring buf(value.getPrefix());
158 buf = buf + chColon + value.getLocalPart();
159 setAttribute(qualifiedName, buf.c_str());
162 setAttribute(qualifiedName, value.getLocalPart());
165 Namespace newNamespace(value.getNamespaceURI(), value.getPrefix(), false, Namespace::NonVisiblyUsed);
166 addNamespace(newNamespace);
169 const map<QName,XMLCh*>& AbstractAttributeExtensibleXMLObject::getExtensionAttributes() const
171 return m_attributeMap;
173 const XMLCh* AbstractAttributeExtensibleXMLObject::getXMLID() const
175 return (m_idAttribute == m_attributeMap.end()) ? nullptr : m_idAttribute->second;
178 void AbstractAttributeExtensibleXMLObject::unmarshallExtensionAttribute(const DOMAttr* attribute)
180 QName q(attribute->getNamespaceURI(),attribute->getLocalName(),attribute->getPrefix());
181 bool ID = attribute->isId() || isRegisteredIDAttribute(q);
182 setAttribute(q,attribute->getNodeValue(),ID);
184 #ifdef XMLTOOLING_XERCESC_BOOLSETIDATTRIBUTE
185 attribute->getOwnerElement()->setIdAttributeNode(attribute, true);
187 attribute->getOwnerElement()->setIdAttributeNode(attribute);
192 void AbstractAttributeExtensibleXMLObject::marshallExtensionAttributes(DOMElement* domElement) const
194 for (map<QName,XMLCh*>::const_iterator i=m_attributeMap.begin(); i!=m_attributeMap.end(); i++) {
195 DOMAttr* attr=domElement->getOwnerDocument()->createAttributeNS(i->first.getNamespaceURI(),i->first.getLocalPart());
196 if (i->first.hasPrefix())
197 attr->setPrefix(i->first.getPrefix());
198 attr->setNodeValue(i->second);
199 domElement->setAttributeNodeNS(attr);
200 if (m_idAttribute==i) {
201 #ifdef XMLTOOLING_XERCESC_BOOLSETIDATTRIBUTE
202 domElement->setIdAttributeNode(attr, true);
204 domElement->setIdAttributeNode(attr);