Merged marshalling/unmarshalling methods into core interface.
[shibboleth/cpp-xmltooling.git] / xmltoolingtest / XMLObjectBaseTestCase.h
1 /*
2  *  Copyright 2001-2005 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 #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>
27 #endif
28 #include <xmltooling/util/ParserPool.h>
29 #include <xmltooling/util/XMLConstants.h>
30 #include <xmltooling/util/XMLHelper.h>
31 #include <xmltooling/util/XMLObjectChildrenList.h>
32
33 using namespace xmltooling;
34 using namespace std;
35
36 extern ParserPool* validatingPool;
37 extern ParserPool* nonvalidatingPool;
38 extern string data_path;
39
40 #if defined (_MSC_VER)
41     #pragma warning( push )
42     #pragma warning( disable : 4250 4251 )
43 #endif
44
45 class SimpleXMLObject : public AbstractXMLObjectMarshaller, public AbstractXMLObjectUnmarshaller
46 {
47 public:
48     static const XMLCh NAMESPACE[];
49     static const XMLCh NAMESPACE_PREFIX[];
50     static const XMLCh LOCAL_NAME[];
51     static const XMLCh ID_ATTRIB_NAME[];
52
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();
57 #endif
58     }
59
60     virtual ~SimpleXMLObject() {
61         XMLString::release(&m_id);
62         XMLString::release(&m_value);
63     }
64     
65     const XMLCh* getId() const { return m_id; }
66     void setId(const XMLCh* id) { m_id=prepareForAssignment(m_id,id); }
67
68     const XMLCh* getValue() const { return m_value; }
69     void setValue(const XMLCh* value) { m_value=prepareForAssignment(m_value,value); }
70
71 #ifndef XMLTOOLING_NO_XMLSEC    
72     Signature* getSignature() const {
73         return dynamic_cast<Signature*>(*m_signature);
74     }
75
76     void setSignature(Signature* sig) {
77         *m_signature=prepareForAssignment(*m_signature,sig);
78     }
79 #endif
80
81     VectorOf(SimpleXMLObject) getSimpleXMLObjects() {
82         return VectorOf(SimpleXMLObject)(this, m_simples, &m_children, m_children.end());
83     }
84     
85     SimpleXMLObject* clone() const {
86         auto_ptr<XMLObject> domClone(AbstractDOMCachingXMLObject::clone());
87         SimpleXMLObject* ret=dynamic_cast<SimpleXMLObject*>(domClone.get());
88         if (ret) {
89             domClone.release();
90             return ret;
91         }
92
93         ret=new SimpleXMLObject();
94         ret->m_namespaces=m_namespaces;
95         ret->setId(m_id);
96         ret->setValue(m_value);
97         xmltooling::clone(m_children, ret->m_children);
98         return ret;
99     }
100
101     void marshallAttributes(DOMElement* domElement) const {
102         if(getId()) {
103             domElement->setAttributeNS(NULL, SimpleXMLObject::ID_ATTRIB_NAME, getId());
104             domElement->setIdAttributeNS(NULL, SimpleXMLObject::ID_ATTRIB_NAME);
105         }
106     }
107
108     void marshallElementContent(DOMElement* domElement) const {
109         if(getValue()) {
110             domElement->setTextContent(getValue());
111         }
112     }
113
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));
120 #endif
121         else
122             throw UnmarshallingException("Unknown child element cannot be added to parent object.");
123     }
124
125     void processAttribute(const DOMAttr* attribute) {
126         if (XMLHelper::isNodeNamed(attribute, NULL, SimpleXMLObject::ID_ATTRIB_NAME))
127             setId(attribute->getValue());
128         else
129             throw UnmarshallingException("Unknown attribute cannot be processed by parent object.");
130     }
131
132     void processElementContent(const XMLCh* elementContent) {
133         setValue(elementContent);
134     }
135
136 private:
137     XMLCh* m_id;
138     XMLCh* m_value;
139     vector<SimpleXMLObject*> m_simples;
140 #ifndef XMLTOOLING_NO_XMLSEC
141     list<XMLObject*>::iterator m_signature;
142 #endif
143 };
144
145 class SimpleXMLObjectBuilder : public XMLObjectBuilder
146 {
147 public:
148     SimpleXMLObject* buildObject(const DOMElement* e=NULL) const {
149         return new SimpleXMLObject();
150     }
151 };
152
153 class WildcardXMLObject : public AbstractElementProxy, public AbstractAttributeExtensibleXMLObject,
154     public AbstractXMLObjectMarshaller, public AbstractXMLObjectUnmarshaller
155 {
156 public:
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() {}
162     
163     WildcardXMLObject* clone() const {
164         auto_ptr<XMLObject> domClone(AbstractDOMCachingXMLObject::clone());
165         WildcardXMLObject* ret=dynamic_cast<WildcardXMLObject*>(domClone.get());
166         if (ret) {
167             domClone.release();
168             return ret;
169         }
170
171         ret=new WildcardXMLObject(
172             getElementQName().getNamespaceURI(),getElementQName().getLocalPart(),getElementQName().getPrefix()
173             );
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);
177         }
178         ret->setTextContent(getTextContent());
179         xmltooling::clone(m_children, ret->m_children);
180         return ret;
181     }
182
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);
190         }
191     }
192
193     void marshallElementContent(DOMElement* domElement) const {
194         if(getTextContent()) {
195             domElement->appendChild(domElement->getOwnerDocument()->createTextNode(getTextContent()));
196         }
197     }
198
199     void processChildElement(XMLObject* childXMLObject, const DOMElement* root) {
200         getXMLObjects().push_back(childXMLObject);
201     }
202
203     void processAttribute(const DOMAttr* attribute) {
204         QName q(attribute->getNamespaceURI(),attribute->getLocalName(),attribute->getPrefix()); 
205         setAttribute(q,attribute->getNodeValue());
206     }
207
208     void processElementContent(const XMLCh* elementContent) {
209         setTextContent(elementContent);
210     }
211 };
212
213 class WildcardXMLObjectBuilder : public XMLObjectBuilder
214 {
215 public:
216     WildcardXMLObject* buildObject(const DOMElement* e=NULL) const {
217         return new WildcardXMLObject(e->getNamespaceURI(),e->getLocalName(),e->getPrefix());
218     }
219 };
220
221 #if defined (_MSC_VER)
222     #pragma warning( pop )
223 #endif