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