Multi-line svn commit, see body.
[shibboleth/cpp-xmltooling.git] / xmltooling / io / AbstractXMLObjectUnmarshaller.cpp
1 /*
2 *  Copyright 2001-2007 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 /**
18  * AbstractXMLObjectUnmarshaller.cpp
19  * 
20  * A thread-safe abstract unmarshaller.
21  */
22
23 #include "internal.h"
24 #include "exceptions.h"
25 #include "XMLObjectBuilder.h"
26 #include "io/AbstractXMLObjectUnmarshaller.h"
27 #include "util/NDC.h"
28 #include "util/XMLConstants.h"
29 #include "util/XMLHelper.h"
30
31 #include <xercesc/util/XMLUniDefs.hpp>
32
33 using namespace xmlconstants;
34 using namespace xmltooling;
35 using namespace std;
36
37
38 XMLObject* AbstractXMLObjectUnmarshaller::unmarshall(DOMElement* element, bool bindDocument)
39 {
40 #ifdef _DEBUG
41     xmltooling::NDC ndc("unmarshall");
42 #endif
43
44     if (getDOM() || hasParent())
45         throw UnmarshallingException("Object already contains data, it cannot be unmarshalled at this stage.");
46
47     if (!XMLString::equals(element->getNamespaceURI(),getElementQName().getNamespaceURI()) ||
48         !XMLString::equals(element->getLocalName(),getElementQName().getLocalPart())) {
49         throw UnmarshallingException("Unrecognized element supplied to implementation for unmarshalling.");
50     }
51
52     if (m_log.isDebugEnabled()) {
53         auto_ptr_char dname(element->getNodeName());
54         m_log.debug("unmarshalling DOM element (%s)", dname.get());
55     }
56
57     if (element->hasAttributes()) {
58         unmarshallAttributes(element);
59     }
60
61     unmarshallContent(element);
62
63     setDOM(element,bindDocument);
64     return this;
65 }
66
67 void AbstractXMLObjectUnmarshaller::unmarshallAttributes(const DOMElement* domElement)
68 {
69 #ifdef _DEBUG
70     xmltooling::NDC ndc("unmarshallAttributes");
71 #endif
72
73     if (m_log.isDebugEnabled()) {
74         auto_ptr_char dname(domElement->getNodeName());
75         m_log.debug("unmarshalling attributes for DOM element (%s)", dname.get());
76     }
77
78     DOMNamedNodeMap* attributes = domElement->getAttributes();
79     if (!attributes) {
80         m_log.debug("no attributes to unmarshall");
81         return;
82     }
83
84     DOMNode* childNode;
85     DOMAttr* attribute;
86     for (XMLSize_t i=0; i<attributes->getLength(); i++) {
87         childNode = attributes->item(i);
88
89         // The child node should always be an attribute, but just in case
90         if (childNode->getNodeType() != DOMNode::ATTRIBUTE_NODE) {
91             m_log.debug("encountered child node of type %d in attribute list, ignoring it", childNode->getNodeType());
92             continue;
93         }
94
95         attribute = static_cast<DOMAttr*>(childNode);
96         
97         const XMLCh* nsuri=attribute->getNamespaceURI();
98         if (XMLString::equals(nsuri,XMLNS_NS)) {
99             if (XMLString::equals(attribute->getLocalName(),XMLNS_PREFIX)) {
100                 m_log.debug("found default namespace declaration, adding it to the list of namespaces on the XMLObject");
101                 addNamespace(Namespace(attribute->getValue(), NULL, true));
102                 continue;
103             }
104             else {
105                 m_log.debug("found namespace declaration, adding it to the list of namespaces on the XMLObject");
106                 addNamespace(Namespace(attribute->getValue(), attribute->getLocalName(), true));
107                 continue;
108             }
109         }
110         else if (XMLString::equals(nsuri,XSI_NS)) {
111             static const XMLCh type[]= UNICODE_LITERAL_4(t,y,p,e);
112             static const XMLCh schemaLocation[]= UNICODE_LITERAL_14(s,c,h,e,m,a,L,o,c,a,t,i,o,n);
113             static const XMLCh noNamespaceSchemaLocation[]= UNICODE_LITERAL_25(n,o,N,a,m,e,s,p,a,c,e,S,c,h,e,m,a,L,o,c,a,t,i,o,n);
114             static const XMLCh _nil[]= UNICODE_LITERAL_3(n,i,l);
115             if (XMLString::equals(attribute->getLocalName(),type)) {
116                 m_log.debug("skipping xsi:type declaration");
117                 continue;
118             }
119             else if (XMLString::equals(attribute->getLocalName(),schemaLocation)) {
120                 m_log.debug("storing off xsi:schemaLocation attribute");
121                 if (m_schemaLocation)
122                     XMLString::release(&m_schemaLocation);
123                 m_schemaLocation=XMLString::replicate(attribute->getValue());
124                 continue;
125             }
126             else if (XMLString::equals(attribute->getLocalName(),noNamespaceSchemaLocation)) {
127                 m_log.debug("storing off xsi:noNamespaceSchemaLocation attribute");
128                 if (m_noNamespaceSchemaLocation)
129                     XMLString::release(&m_noNamespaceSchemaLocation);
130                 m_schemaLocation=XMLString::replicate(attribute->getValue());
131                 m_noNamespaceSchemaLocation=XMLString::replicate(attribute->getValue());
132                 continue;
133             }
134             else if (XMLString::equals(attribute->getLocalName(), _nil)) {
135                 m_log.debug("processing xsi:nil attribute");
136                 setNil(attribute->getValue());
137                 continue;
138             }
139         }
140         else if (nsuri && !XMLString::equals(nsuri,XML_NS)) {
141             m_log.debug("found namespace-qualified attribute, adding prefix to the list of namespaces on the XMLObject");
142             addNamespace(Namespace(nsuri, attribute->getPrefix()));
143         }
144
145         m_log.debug("processing generic attribute");
146         processAttribute(attribute);
147     }
148 }
149
150 void AbstractXMLObjectUnmarshaller::unmarshallContent(const DOMElement* domElement)
151 {
152 #ifdef _DEBUG
153     xmltooling::NDC ndc("unmarshallContent");
154 #endif
155
156     if (m_log.isDebugEnabled()) {
157         auto_ptr_char dname(domElement->getNodeName());
158         m_log.debug("unmarshalling child nodes of DOM element (%s)", dname.get());
159     }
160
161     DOMNode* childNode = domElement->getFirstChild();
162     if (!childNode) {
163         m_log.debug("element had no children");
164         return;
165     }
166
167     unsigned int position = 0;
168     while (childNode) {
169         if (childNode->getNodeType() == DOMNode::ELEMENT_NODE) {
170             const XMLObjectBuilder* builder = XMLObjectBuilder::getBuilder(static_cast<DOMElement*>(childNode));
171             if (!builder) {
172                 auto_ptr<QName> cname(XMLHelper::getNodeQName(childNode));
173                 m_log.error("no default builder installed, found unknown child element (%s)", cname->toString().c_str());
174                 throw UnmarshallingException("Unmarshaller found unknown child element, but no default builder was found.");
175             }
176
177             if (m_log.isDebugEnabled()) {
178                 auto_ptr<QName> cname(XMLHelper::getNodeQName(childNode));
179                 m_log.debug("unmarshalling child element (%s)", cname->toString().c_str());
180             }
181
182             // Retain ownership of the unmarshalled child until it's processed by the parent.
183             auto_ptr<XMLObject> childObject(builder->buildFromElement(static_cast<DOMElement*>(childNode)));
184             processChildElement(childObject.get(), static_cast<DOMElement*>(childNode));
185             childObject.release();
186             
187             // Advance the text node position marker.
188             ++position;
189         }
190         else if (childNode->getNodeType() == DOMNode::TEXT_NODE || childNode->getNodeType() == DOMNode::CDATA_SECTION_NODE) {
191             m_log.debug("processing text content at position (%d)", position);
192             setTextContent(childNode->getNodeValue(), position);
193         }
194         
195         childNode = childNode->getNextSibling();
196     }
197 }
198
199 void AbstractXMLObjectUnmarshaller::processChildElement(XMLObject* child, const DOMElement* childRoot)
200 {
201     throw UnmarshallingException("Invalid child element: $1",params(1,child->getElementQName().toString().c_str()));
202 }
203
204 void AbstractXMLObjectUnmarshaller::processAttribute(const DOMAttr* attribute)
205 {
206     auto_ptr<QName> q(XMLHelper::getNodeQName(attribute));
207     throw UnmarshallingException("Invalid attribute: $1",params(1,q->toString().c_str()));
208 }