Multi-line svn commit, see body.
[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/AbstractComplexElement.h>
19 #include <xmltooling/ElementProxy.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 #include <xmltooling/impl/AnyElement.h>
26 #include <xmltooling/impl/UnknownElement.h>
27 #include <xmltooling/util/ParserPool.h>
28 #include <xmltooling/util/XMLConstants.h>
29 #include <xmltooling/util/XMLHelper.h>
30 #include <xmltooling/util/XMLObjectChildrenList.h>
31
32 #ifndef XMLTOOLING_NO_XMLSEC
33     #include <xmltooling/signature/Signature.h>
34     using namespace xmlsignature;
35 #endif
36
37 using namespace xmltooling;
38 using namespace std;
39
40 extern string data_path;
41
42 #if defined (_MSC_VER)
43     #pragma warning( push )
44     #pragma warning( disable : 4250 4251 )
45 #endif
46
47 class SimpleXMLObject
48     : public AbstractComplexElement,
49         public AbstractDOMCachingXMLObject,
50         public AbstractXMLObjectMarshaller,
51         public AbstractXMLObjectUnmarshaller
52 {
53 protected:
54     SimpleXMLObject(const SimpleXMLObject& src)
55             : AbstractXMLObject(src), AbstractComplexElement(src), AbstractDOMCachingXMLObject(src),
56                 m_id(XMLString::replicate(src.m_id)) {
57 #ifndef XMLTOOLING_NO_XMLSEC
58         m_children.push_back(NULL);
59         m_signature=m_children.begin();
60 #endif
61         VectorOf(SimpleXMLObject) mine=getSimpleXMLObjects();
62         for (vector<SimpleXMLObject*>::const_iterator i=src.m_simples.begin(); i!=src.m_simples.end(); i++) {
63             mine.push_back((*i) ? (*i)->clone() : NULL);
64         }
65     }
66
67 public:
68     static const XMLCh NAMESPACE[];
69     static const XMLCh NAMESPACE_PREFIX[];
70     static const XMLCh LOCAL_NAME[];
71     static const XMLCh DERIVED_NAME[];
72     static const XMLCh TYPE_NAME[];
73     static const XMLCh ID_ATTRIB_NAME[];
74
75     SimpleXMLObject(
76         const XMLCh* nsURI=NULL, const XMLCh* localName=NULL, const XMLCh* prefix=NULL, const QName* schemaType=NULL
77         ) : AbstractXMLObject(nsURI, localName, prefix, schemaType), m_id(NULL) {
78 #ifndef XMLTOOLING_NO_XMLSEC
79         m_children.push_back(NULL);
80         m_signature=m_children.begin();
81 #endif
82     }
83
84     virtual ~SimpleXMLObject() {
85         XMLString::release(&m_id);
86     }
87
88     SimpleXMLObject* clone() const {
89         auto_ptr<XMLObject> domClone(AbstractDOMCachingXMLObject::clone());
90         SimpleXMLObject* ret=dynamic_cast<SimpleXMLObject*>(domClone.get());
91         if (ret) {
92             domClone.release();
93             return ret;
94         }
95
96         return new SimpleXMLObject(*this);
97     }
98
99     const XMLCh* getXMLID() const { return getId(); }
100     const XMLCh* getId() const { return m_id; }
101     void setId(const XMLCh* id) { m_id=prepareForAssignment(m_id,id); }
102
103     const XMLCh* getValue() const { return getTextContent(); }
104     void setValue(const XMLCh* value) { setTextContent(value); }
105
106 #ifndef XMLTOOLING_NO_XMLSEC    
107     Signature* getSignature() const {
108         return dynamic_cast<Signature*>(*m_signature);
109     }
110
111     void setSignature(Signature* sig) {
112         *m_signature=prepareForAssignment(*m_signature,sig);
113     }
114 #endif
115
116     VectorOf(SimpleXMLObject) getSimpleXMLObjects() {
117         return VectorOf(SimpleXMLObject)(this, m_simples, &m_children, m_children.end());
118     }
119     
120     const std::vector<SimpleXMLObject*>& getSimpleXMLObjects() const {
121         return m_simples;
122     }
123
124 protected:
125     void marshallAttributes(DOMElement* domElement) const {
126         if(getId()) {
127             domElement->setAttributeNS(NULL, SimpleXMLObject::ID_ATTRIB_NAME, getId());
128             domElement->setIdAttributeNS(NULL, SimpleXMLObject::ID_ATTRIB_NAME);
129         }
130     }
131
132     void processChildElement(XMLObject* childXMLObject, const DOMElement* root) {
133         SimpleXMLObject* simple=dynamic_cast<SimpleXMLObject*>(childXMLObject);
134         if (simple) {
135             getSimpleXMLObjects().push_back(simple);
136             return;
137         }
138         
139 #ifndef XMLTOOLING_NO_XMLSEC
140         Signature* sig=dynamic_cast<Signature*>(childXMLObject);
141         if (sig) {
142             setSignature(sig);
143             return;
144         }
145 #endif
146
147         throw UnmarshallingException("Unknown child element cannot be added to parent object.");
148     }
149
150     void processAttribute(const DOMAttr* attribute) {
151         if (XMLHelper::isNodeNamed(attribute, NULL, SimpleXMLObject::ID_ATTRIB_NAME))
152             setId(attribute->getValue());
153         else
154             throw UnmarshallingException("Unknown attribute cannot be processed by parent object.");
155     }
156
157 private:
158     XMLCh* m_id;
159     vector<SimpleXMLObject*> m_simples;
160 #ifndef XMLTOOLING_NO_XMLSEC
161     list<XMLObject*>::iterator m_signature;
162 #endif
163 };
164
165 class SimpleXMLObjectBuilder : public XMLObjectBuilder
166 {
167 public:
168     SimpleXMLObject* buildObject() const {
169         return buildObject(SimpleXMLObject::NAMESPACE, SimpleXMLObject::LOCAL_NAME, SimpleXMLObject::NAMESPACE_PREFIX);
170     }
171
172     SimpleXMLObject* buildObject(
173         const XMLCh* nsURI, const XMLCh* localName, const XMLCh* prefix=NULL, const QName* schemaType=NULL
174         ) const {
175         return new SimpleXMLObject(nsURI, localName, prefix, schemaType);
176     }
177
178     static SimpleXMLObject* newSimpleXMLObject() {
179         const SimpleXMLObjectBuilder* b = dynamic_cast<const SimpleXMLObjectBuilder*>(
180             XMLObjectBuilder::getBuilder(QName(SimpleXMLObject::NAMESPACE,SimpleXMLObject::LOCAL_NAME))
181             );
182         if (b)
183             return b->buildObject();
184         throw XMLObjectException("Unable to obtain typed builder for SimpleXMLObject.");
185     }
186 };
187
188 #if defined (_MSC_VER)
189     #pragma warning( pop )
190 #endif