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