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 AbstractXMLObjectMarshaller, public AbstractXMLObjectUnmarshaller
49 static const XMLCh NAMESPACE[];
50 static const XMLCh NAMESPACE_PREFIX[];
51 static const XMLCh LOCAL_NAME[];
52 static const XMLCh DERIVED_NAME[];
53 static const XMLCh TYPE_NAME[];
54 static const XMLCh ID_ATTRIB_NAME[];
57 const XMLCh* namespaceURI=NULL, const XMLCh* elementLocalName=NULL, const XMLCh* namespacePrefix=NULL
58 ) : AbstractXMLObject(namespaceURI, elementLocalName, namespacePrefix), m_id(NULL), m_value(NULL) {
59 #ifndef XMLTOOLING_NO_XMLSEC
60 m_children.push_back(NULL);
61 m_signature=m_children.begin();
65 virtual ~SimpleXMLObject() {
66 XMLString::release(&m_id);
67 XMLString::release(&m_value);
70 const XMLCh* getId() const { return m_id; }
71 void setId(const XMLCh* id) { m_id=prepareForAssignment(m_id,id); }
73 const XMLCh* getValue() const { return m_value; }
74 void setValue(const XMLCh* value) { m_value=prepareForAssignment(m_value,value); }
76 #ifndef XMLTOOLING_NO_XMLSEC
77 Signature* getSignature() const {
78 return dynamic_cast<Signature*>(*m_signature);
81 void setSignature(Signature* sig) {
82 *m_signature=prepareForAssignment(*m_signature,sig);
86 VectorOf(SimpleXMLObject) getSimpleXMLObjects() {
87 return VectorOf(SimpleXMLObject)(this, m_simples, &m_children, m_children.end());
90 SimpleXMLObject* clone() const {
91 auto_ptr<XMLObject> domClone(AbstractDOMCachingXMLObject::clone());
92 SimpleXMLObject* ret=dynamic_cast<SimpleXMLObject*>(domClone.get());
98 ret=new SimpleXMLObject();
99 ret->m_namespaces=m_namespaces;
101 ret->setValue(m_value);
102 xmltooling::clone(m_children, ret->m_children);
106 void marshallAttributes(DOMElement* domElement) const {
108 domElement->setAttributeNS(NULL, SimpleXMLObject::ID_ATTRIB_NAME, getId());
109 domElement->setIdAttributeNS(NULL, SimpleXMLObject::ID_ATTRIB_NAME);
113 void marshallElementContent(DOMElement* domElement) const {
115 domElement->setTextContent(getValue());
119 void processChildElement(XMLObject* childXMLObject, const DOMElement* root) {
120 SimpleXMLObject* simple=dynamic_cast<SimpleXMLObject*>(childXMLObject);
122 getSimpleXMLObjects().push_back(simple);
126 #ifndef XMLTOOLING_NO_XMLSEC
127 Signature* sig=dynamic_cast<Signature*>(childXMLObject);
134 throw UnmarshallingException("Unknown child element cannot be added to parent object.");
137 void processAttribute(const DOMAttr* attribute) {
138 if (XMLHelper::isNodeNamed(attribute, NULL, SimpleXMLObject::ID_ATTRIB_NAME))
139 setId(attribute->getValue());
141 throw UnmarshallingException("Unknown attribute cannot be processed by parent object.");
144 void processElementContent(const XMLCh* elementContent) {
145 setValue(elementContent);
151 vector<SimpleXMLObject*> m_simples;
152 #ifndef XMLTOOLING_NO_XMLSEC
153 list<XMLObject*>::iterator m_signature;
157 class SimpleXMLObjectBuilder : public XMLObjectBuilder
160 SimpleXMLObject* buildObject() const {
161 return buildObject(SimpleXMLObject::NAMESPACE, SimpleXMLObject::LOCAL_NAME, SimpleXMLObject::NAMESPACE_PREFIX);
164 SimpleXMLObject* buildObject(
165 const XMLCh* namespaceURI, const XMLCh* elementLocalName, const XMLCh* namespacePrefix=NULL
167 return new SimpleXMLObject(namespaceURI,elementLocalName,namespacePrefix);
171 #if defined (_MSC_VER)
172 #pragma warning( pop )