2 * Copyright 2001-2005 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.
17 #include <cxxtest/TestSuite.h>
18 #include <xmltooling/AbstractAttributeExtensibleXMLObject.h>
19 #include <xmltooling/AbstractElementProxy.h>
20 #include <xmltooling/exceptions.h>
21 #include <xmltooling/XMLObjectBuilder.h>
22 #include <xmltooling/XMLToolingConfig.h>
23 #include <xmltooling/io/AbstractXMLObjectMarshaller.h>
24 #include <xmltooling/io/AbstractXMLObjectUnmarshaller.h>
25 #ifndef XMLTOOLING_NO_XMLSEC
26 #include <xmltooling/signature/Signature.h>
28 #include <xmltooling/util/ParserPool.h>
29 #include <xmltooling/util/XMLConstants.h>
30 #include <xmltooling/util/XMLHelper.h>
31 #include <xmltooling/util/XMLObjectChildrenList.h>
33 using namespace xmltooling;
36 extern ParserPool* validatingPool;
37 extern ParserPool* nonvalidatingPool;
38 extern string data_path;
40 #if defined (_MSC_VER)
41 #pragma warning( push )
42 #pragma warning( disable : 4250 4251 )
45 class SimpleXMLObject : public AbstractXMLObjectMarshaller, public AbstractXMLObjectUnmarshaller
48 static const XMLCh NAMESPACE[];
49 static const XMLCh NAMESPACE_PREFIX[];
50 static const XMLCh LOCAL_NAME[];
51 static const XMLCh ID_ATTRIB_NAME[];
53 SimpleXMLObject() : AbstractDOMCachingXMLObject(NAMESPACE, LOCAL_NAME, NAMESPACE_PREFIX), m_id(NULL), m_value(NULL) {
54 #ifndef XMLTOOLING_NO_XMLSEC
55 m_children.push_back(NULL);
56 m_signature=m_children.begin();
60 virtual ~SimpleXMLObject() {
61 XMLString::release(&m_id);
62 XMLString::release(&m_value);
65 const XMLCh* getId() const { return m_id; }
66 void setId(const XMLCh* id) { m_id=prepareForAssignment(m_id,id); }
68 const XMLCh* getValue() const { return m_value; }
69 void setValue(const XMLCh* value) { m_value=prepareForAssignment(m_value,value); }
71 #ifndef XMLTOOLING_NO_XMLSEC
72 Signature* getSignature() const {
73 return dynamic_cast<Signature*>(*m_signature);
76 void setSignature(Signature* sig) {
77 *m_signature=prepareForAssignment(*m_signature,sig);
81 VectorOf(SimpleXMLObject) getSimpleXMLObjects() {
82 return VectorOf(SimpleXMLObject)(this, m_simples, &m_children, m_children.end());
85 SimpleXMLObject* clone() const {
86 auto_ptr<XMLObject> domClone(AbstractDOMCachingXMLObject::clone());
87 SimpleXMLObject* ret=dynamic_cast<SimpleXMLObject*>(domClone.get());
93 ret=new SimpleXMLObject();
94 ret->m_namespaces=m_namespaces;
96 ret->setValue(m_value);
97 xmltooling::clone(m_children, ret->m_children);
101 void marshallAttributes(DOMElement* domElement) const {
103 domElement->setAttributeNS(NULL, SimpleXMLObject::ID_ATTRIB_NAME, getId());
104 domElement->setIdAttributeNS(NULL, SimpleXMLObject::ID_ATTRIB_NAME);
108 void marshallElementContent(DOMElement* domElement) const {
110 domElement->setTextContent(getValue());
114 void processChildElement(XMLObject* childXMLObject, const DOMElement* root) {
115 if (XMLHelper::isNodeNamed(root, SimpleXMLObject::NAMESPACE, SimpleXMLObject::LOCAL_NAME))
116 getSimpleXMLObjects().push_back(dynamic_cast<SimpleXMLObject*>(childXMLObject));
117 #ifndef XMLTOOLING_NO_XMLSEC
118 else if (XMLHelper::isNodeNamed(root, XMLConstants::XMLSIG_NS, Signature::LOCAL_NAME))
119 setSignature(dynamic_cast<Signature*>(childXMLObject));
122 throw UnmarshallingException("Unknown child element cannot be added to parent object.");
125 void processAttribute(const DOMAttr* attribute) {
126 if (XMLHelper::isNodeNamed(attribute, NULL, SimpleXMLObject::ID_ATTRIB_NAME))
127 setId(attribute->getValue());
129 throw UnmarshallingException("Unknown attribute cannot be processed by parent object.");
132 void processElementContent(const XMLCh* elementContent) {
133 setValue(elementContent);
139 vector<SimpleXMLObject*> m_simples;
140 #ifndef XMLTOOLING_NO_XMLSEC
141 list<XMLObject*>::iterator m_signature;
145 class SimpleXMLObjectBuilder : public XMLObjectBuilder
148 SimpleXMLObject* buildObject(const DOMElement* e=NULL) const {
149 return new SimpleXMLObject();
153 class WildcardXMLObject : public AbstractElementProxy, public AbstractAttributeExtensibleXMLObject,
154 public AbstractXMLObjectMarshaller, public AbstractXMLObjectUnmarshaller
157 WildcardXMLObject(const XMLCh* nsURI, const XMLCh* localName, const XMLCh* prefix)
158 : AbstractDOMCachingXMLObject(nsURI, localName, prefix),
159 AbstractElementProxy(nsURI, localName, prefix),
160 AbstractAttributeExtensibleXMLObject(nsURI, localName, prefix) {}
161 virtual ~WildcardXMLObject() {}
163 WildcardXMLObject* clone() const {
164 auto_ptr<XMLObject> domClone(AbstractDOMCachingXMLObject::clone());
165 WildcardXMLObject* ret=dynamic_cast<WildcardXMLObject*>(domClone.get());
171 ret=new WildcardXMLObject(
172 getElementQName().getNamespaceURI(),getElementQName().getLocalPart(),getElementQName().getPrefix()
174 ret->m_namespaces=m_namespaces;
175 for (map<QName,XMLCh*>::const_iterator i=m_attributeMap.begin(); i!=m_attributeMap.end(); i++) {
176 ret->m_attributeMap[i->first]=XMLString::replicate(i->second);
178 ret->setTextContent(getTextContent());
179 xmltooling::clone(m_children, ret->m_children);
183 void marshallAttributes(DOMElement* domElement) const {
184 for (map<QName,XMLCh*>::const_iterator i=m_attributeMap.begin(); i!=m_attributeMap.end(); i++) {
185 DOMAttr* attr=domElement->getOwnerDocument()->createAttributeNS(i->first.getNamespaceURI(),i->first.getLocalPart());
186 if (i->first.hasPrefix())
187 attr->setPrefix(i->first.getPrefix());
188 attr->setNodeValue(i->second);
189 domElement->setAttributeNode(attr);
193 void marshallElementContent(DOMElement* domElement) const {
194 if(getTextContent()) {
195 domElement->appendChild(domElement->getOwnerDocument()->createTextNode(getTextContent()));
199 void processChildElement(XMLObject* childXMLObject, const DOMElement* root) {
200 getXMLObjects().push_back(childXMLObject);
203 void processAttribute(const DOMAttr* attribute) {
204 QName q(attribute->getNamespaceURI(),attribute->getLocalName(),attribute->getPrefix());
205 setAttribute(q,attribute->getNodeValue());
208 void processElementContent(const XMLCh* elementContent) {
209 setTextContent(elementContent);
213 class WildcardXMLObjectBuilder : public XMLObjectBuilder
216 WildcardXMLObject* buildObject(const DOMElement* e=NULL) const {
217 return new WildcardXMLObject(e->getNamespaceURI(),e->getLocalName(),e->getPrefix());
221 #if defined (_MSC_VER)
222 #pragma warning( pop )