2 * Copyright 2001-2006 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.
18 * AbstractXMLObjectUnmarshaller.cpp
20 * A thread-safe abstract unmarshaller.
24 #include "exceptions.h"
25 #include "XMLObjectBuilder.h"
26 #include "io/AbstractXMLObjectUnmarshaller.h"
28 #include "util/XMLConstants.h"
29 #include "util/XMLHelper.h"
31 #include <xercesc/util/XMLUniDefs.hpp>
32 #include <log4cpp/Category.hh>
34 using namespace xmltooling;
35 using namespace log4cpp;
38 #define XT_log (*static_cast<Category*>(m_log))
40 XMLObject* AbstractXMLObjectUnmarshaller::unmarshall(DOMElement* element, bool bindDocument)
43 xmltooling::NDC ndc("unmarshall");
46 if (getDOM() || hasParent())
47 throw UnmarshallingException("Object already contains data, it cannot be unmarshalled at this stage.");
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.");
54 if (XT_log.isDebugEnabled()) {
55 auto_ptr_char dname(element->getNodeName());
56 XT_log.debug("unmarshalling DOM element (%s)", dname.get());
59 if (element->hasAttributes()) {
60 unmarshallAttributes(element);
63 unmarshallChildElements(element);
65 setDOM(element,bindDocument);
69 void AbstractXMLObjectUnmarshaller::unmarshallAttributes(const DOMElement* domElement)
72 xmltooling::NDC ndc("unmarshallAttributes");
75 if (XT_log.isDebugEnabled()) {
76 auto_ptr_char dname(domElement->getNodeName());
77 XT_log.debug("unmarshalling attributes for DOM element (%s)", dname.get());
80 DOMNamedNodeMap* attributes = domElement->getAttributes();
82 XT_log.debug("no attributes to unmarshall");
88 for (XMLSize_t i=0; i<attributes->getLength(); i++) {
89 childNode = attributes->item(i);
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());
97 attribute = static_cast<DOMAttr*>(childNode);
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));
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));
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");
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());
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()));
132 XT_log.debug("processing generic attribute");
133 processAttribute(attribute);
137 void AbstractXMLObjectUnmarshaller::unmarshallChildElements(const DOMElement* domElement)
140 xmltooling::NDC ndc("unmarshallChildElements");
143 if (XT_log.isDebugEnabled()) {
144 auto_ptr_char dname(domElement->getNodeName());
145 XT_log.debug("unmarshalling child elements of DOM element (%s)", dname.get());
148 DOMNodeList* childNodes = domElement->getChildNodes();
150 if (!childNodes || childNodes->getLength()==0) {
151 XT_log.debug("element had no children");
155 for (XMLSize_t i = 0; i < childNodes->getLength(); i++) {
156 childNode = childNodes->item(i);
157 if (childNode->getNodeType() == DOMNode::ELEMENT_NODE) {
158 const XMLObjectBuilder* builder = XMLObjectBuilder::getBuilder(static_cast<DOMElement*>(childNode));
160 auto_ptr<QName> cname(XMLHelper::getNodeQName(childNode));
161 XT_log.error("no default builder installed, found unknown child element (%s)", cname->toString().c_str());
162 throw UnmarshallingException("Unmarshaller found unknown child element, but no default builder was found.");
165 if (XT_log.isDebugEnabled()) {
166 auto_ptr<QName> cname(XMLHelper::getNodeQName(childNode));
167 XT_log.debug("unmarshalling child element (%s)", cname->toString().c_str());
170 // Retain ownership of the unmarshalled child until it's processed by the parent.
171 auto_ptr<XMLObject> childObject(builder->buildFromElement(static_cast<DOMElement*>(childNode)));
172 processChildElement(childObject.get(), static_cast<DOMElement*>(childNode));
173 childObject.release();
175 else if (childNode->getNodeType() == DOMNode::TEXT_NODE && !XMLString::isAllWhiteSpace(childNode->getNodeValue())) {
176 XT_log.debug("processing element content");
177 processElementContent(childNode->getNodeValue());
182 void AbstractXMLObjectUnmarshaller::processChildElement(XMLObject* child, const DOMElement* childRoot)
184 throw UnmarshallingException("Invalid child element: $1",params(1,child->getElementQName().toString().c_str()));
187 void AbstractXMLObjectUnmarshaller::processAttribute(const DOMAttr* attribute)
189 auto_ptr<QName> q(XMLHelper::getNodeQName(attribute));
190 throw UnmarshallingException("Invalid attribute: $1",params(1,q->toString().c_str()));
193 void AbstractXMLObjectUnmarshaller::processElementContent(const XMLCh* elementContent)
195 throw UnmarshallingException("Invalid text content in element.");