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/ElementProxy.h>
19 #include <xmltooling/exceptions.h>
20 #include <xmltooling/XMLObjectBuilder.h>
21 #include <xmltooling/XMLToolingConfig.h>
22 #include <xmltooling/io/AbstractXMLObjectMarshaller.h>
23 #include <xmltooling/io/AbstractXMLObjectUnmarshaller.h>
24 #include <xmltooling/impl/AnyElement.h>
25 #include <xmltooling/impl/UnknownElement.h>
26 #ifndef XMLTOOLING_NO_XMLSEC
27 #include <xmltooling/signature/Signature.h>
29 #include <xmltooling/util/ParserPool.h>
30 #include <xmltooling/util/XMLConstants.h>
31 #include <xmltooling/util/XMLHelper.h>
32 #include <xmltooling/util/XMLObjectChildrenList.h>
34 using namespace xmltooling;
37 extern ParserPool* validatingPool;
38 extern ParserPool* nonvalidatingPool;
39 extern string data_path;
41 #if defined (_MSC_VER)
42 #pragma warning( push )
43 #pragma warning( disable : 4250 4251 )
46 class SimpleXMLObject : public AbstractDOMCachingXMLObject, public AbstractXMLObjectMarshaller, public AbstractXMLObjectUnmarshaller
49 SimpleXMLObject(const SimpleXMLObject& src) : AbstractXMLObject(src), AbstractDOMCachingXMLObject(src),
50 m_id(XMLString::replicate(src.m_id)), m_value(XMLString::replicate(src.m_value)) {
51 #ifndef XMLTOOLING_NO_XMLSEC
52 m_children.push_back(NULL);
53 m_signature=m_children.begin();
55 VectorOf(SimpleXMLObject) mine=getSimpleXMLObjects();
56 for (vector<SimpleXMLObject*>::const_iterator i=src.m_simples.begin(); i!=src.m_simples.end(); i++) {
57 mine.push_back((*i) ? (*i)->clone() : NULL);
62 static const XMLCh NAMESPACE[];
63 static const XMLCh NAMESPACE_PREFIX[];
64 static const XMLCh LOCAL_NAME[];
65 static const XMLCh DERIVED_NAME[];
66 static const XMLCh TYPE_NAME[];
67 static const XMLCh ID_ATTRIB_NAME[];
70 const XMLCh* nsURI=NULL, const XMLCh* localName=NULL, const XMLCh* prefix=NULL, const QName* schemaType=NULL
71 ) : AbstractXMLObject(nsURI, localName, prefix, schemaType), m_id(NULL), m_value(NULL) {
72 #ifndef XMLTOOLING_NO_XMLSEC
73 m_children.push_back(NULL);
74 m_signature=m_children.begin();
78 virtual ~SimpleXMLObject() {
79 XMLString::release(&m_id);
80 XMLString::release(&m_value);
83 SimpleXMLObject* clone() const {
84 auto_ptr<XMLObject> domClone(AbstractDOMCachingXMLObject::clone());
85 SimpleXMLObject* ret=dynamic_cast<SimpleXMLObject*>(domClone.get());
91 return new SimpleXMLObject(*this);
94 const XMLCh* getId() const { return m_id; }
95 void setId(const XMLCh* id) { m_id=prepareForAssignment(m_id,id); }
97 const XMLCh* getValue() const { return m_value; }
98 void setValue(const XMLCh* value) { m_value=prepareForAssignment(m_value,value); }
100 #ifndef XMLTOOLING_NO_XMLSEC
101 Signature* getSignature() const {
102 return dynamic_cast<Signature*>(*m_signature);
105 void setSignature(Signature* sig) {
106 *m_signature=prepareForAssignment(*m_signature,sig);
110 VectorOf(SimpleXMLObject) getSimpleXMLObjects() {
111 return VectorOf(SimpleXMLObject)(this, m_simples, &m_children, m_children.end());
114 const std::vector<SimpleXMLObject*>& getSimpleXMLObjects() const {
119 void marshallAttributes(DOMElement* domElement) const {
121 domElement->setAttributeNS(NULL, SimpleXMLObject::ID_ATTRIB_NAME, getId());
122 domElement->setIdAttributeNS(NULL, SimpleXMLObject::ID_ATTRIB_NAME);
126 void marshallElementContent(DOMElement* domElement) const {
128 domElement->setTextContent(getValue());
132 void processChildElement(XMLObject* childXMLObject, const DOMElement* root) {
133 SimpleXMLObject* simple=dynamic_cast<SimpleXMLObject*>(childXMLObject);
135 getSimpleXMLObjects().push_back(simple);
139 #ifndef XMLTOOLING_NO_XMLSEC
140 Signature* sig=dynamic_cast<Signature*>(childXMLObject);
147 throw UnmarshallingException("Unknown child element cannot be added to parent object.");
150 void processAttribute(const DOMAttr* attribute) {
151 if (XMLHelper::isNodeNamed(attribute, NULL, SimpleXMLObject::ID_ATTRIB_NAME))
152 setId(attribute->getValue());
154 throw UnmarshallingException("Unknown attribute cannot be processed by parent object.");
157 void processElementContent(const XMLCh* elementContent) {
158 setValue(elementContent);
164 vector<SimpleXMLObject*> m_simples;
165 #ifndef XMLTOOLING_NO_XMLSEC
166 list<XMLObject*>::iterator m_signature;
170 class SimpleXMLObjectBuilder : public XMLObjectBuilder
173 SimpleXMLObject* buildObject() const {
174 return buildObject(SimpleXMLObject::NAMESPACE, SimpleXMLObject::LOCAL_NAME, SimpleXMLObject::NAMESPACE_PREFIX);
177 SimpleXMLObject* buildObject(
178 const XMLCh* nsURI, const XMLCh* localName, const XMLCh* prefix=NULL, const QName* schemaType=NULL
180 return new SimpleXMLObject(nsURI, localName, prefix, schemaType);
184 #if defined (_MSC_VER)
185 #pragma warning( pop )