2 * Copyright 2001-2006 Internet2
\r
4 * Licensed under the Apache License, Version 2.0 (the "License");
\r
5 * you may not use this file except in compliance with the License.
\r
6 * You may obtain a copy of the License at
\r
8 * http://www.apache.org/licenses/LICENSE-2.0
\r
10 * Unless required by applicable law or agreed to in writing, software
\r
11 * distributed under the License is distributed on an "AS IS" BASIS,
\r
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
13 * See the License for the specific language governing permissions and
\r
14 * limitations under the License.
\r
18 * AbstractXMLObjectUnmarshaller.cpp
\r
20 * A thread-safe abstract unmarshaller.
\r
23 #include "internal.h"
\r
24 #include "DOMCachingXMLObject.h"
\r
25 #include "exceptions.h"
\r
26 #include "XMLObjectBuilder.h"
\r
27 #include "io/AbstractXMLObjectUnmarshaller.h"
\r
28 #include "util/NDC.h"
\r
29 #include "util/XMLConstants.h"
\r
30 #include "util/XMLHelper.h"
\r
32 #include <xercesc/util/XMLUniDefs.hpp>
\r
33 #include <log4cpp/Category.hh>
\r
35 using namespace xmltooling;
\r
36 using namespace log4cpp;
\r
37 using namespace std;
\r
39 #define XT_log (*static_cast<Category*>(m_log))
\r
41 AbstractXMLObjectUnmarshaller::AbstractXMLObjectUnmarshaller(const XMLCh* targetNamespaceURI, const XMLCh* targetLocalName)
\r
42 : m_targetQName(targetNamespaceURI, targetLocalName),
\r
43 m_log(&Category::getInstance(XMLTOOLING_LOGCAT".Unmarshaller")) {
\r
44 if (!targetLocalName || !*targetLocalName)
\r
45 throw UnmarshallingException("targetLocalName cannot be null or empty");
\r
48 XMLObject* AbstractXMLObjectUnmarshaller::unmarshall(DOMElement* element, bool bindDocument) const
\r
51 xmltooling::NDC ndc("unmarshall");
\r
54 if (XT_log.isDebugEnabled()) {
\r
55 auto_ptr_char dname(element->getLocalName());
\r
56 XT_log.debug("unmarshalling DOM element %s", dname.get());
\r
59 XMLObject* xmlObject = buildXMLObject(element);
\r
61 if (element->hasAttributes()) {
\r
62 unmarshallAttributes(element, xmlObject);
\r
65 if (element->getTextContent()) {
\r
66 processElementContent(xmlObject, element->getTextContent());
\r
69 unmarshallChildElements(element, xmlObject);
\r
72 if (xmlObject instanceof SignableXMLObject) {
\r
73 verifySignature(domElement, xmlObject);
\r
77 DOMCachingXMLObject* dc=dynamic_cast<DOMCachingXMLObject*>(xmlObject);
\r
79 dc->setDOM(element,bindDocument);
\r
84 XMLObject* AbstractXMLObjectUnmarshaller::buildXMLObject(const DOMElement* domElement) const
\r
86 const XMLObjectBuilder* xmlObjectBuilder = XMLObjectBuilder::getBuilder(domElement);
\r
87 if (xmlObjectBuilder)
\r
88 return xmlObjectBuilder->buildObject();
\r
89 throw UnmarshallingException("Failed to locate XMLObjectBuilder for element.");
\r
92 void AbstractXMLObjectUnmarshaller::unmarshallAttributes(const DOMElement* domElement, XMLObject* xmlObject) const
\r
95 xmltooling::NDC ndc("unmarshallAttributes");
\r
97 static const XMLCh type[]={chLatin_t, chLatin_y, chLatin_p, chLatin_e, chNull};
\r
99 if (XT_log.isDebugEnabled()) {
\r
100 auto_ptr_char dname(domElement->getLocalName());
\r
101 XT_log.debug("unmarshalling attributes for DOM element %s", dname.get());
\r
104 DOMNamedNodeMap* attributes = domElement->getAttributes();
\r
106 XT_log.debug("no attributes to unmarshall");
\r
110 DOMNode* childNode;
\r
111 DOMAttr* attribute;
\r
112 for (XMLSize_t i=0; i<attributes->getLength(); i++) {
\r
113 childNode = attributes->item(i);
\r
115 // The child node should always be an attribute, but just in case
\r
116 if (childNode->getNodeType() != DOMNode::ATTRIBUTE_NODE) {
\r
117 XT_log.debug("encountered child node of type %d in attribute list, ignoring it", childNode->getNodeType());
\r
121 attribute = static_cast<DOMAttr*>(childNode);
\r
123 const XMLCh* nsuri=attribute->getNamespaceURI();
\r
124 if (!XMLString::compareString(nsuri,XMLConstants::XMLNS_NS)) {
\r
125 XT_log.debug("found namespace declaration, adding it to the list of namespaces on the XMLObject");
\r
126 xmlObject->addNamespace(Namespace(attribute->getValue(), attribute->getLocalName()));
\r
129 else if (!XMLString::compareString(nsuri,XMLConstants::XSI_NS) &&
\r
130 !XMLString::compareString(attribute->getLocalName(),type)) {
\r
131 XT_log.debug("found xsi:type declaration, setting the schema type of the XMLObject");
\r
132 auto_ptr<QName> xsitype(XMLHelper::getAttributeValueAsQName(attribute));
\r
133 xmlObject->setSchemaType(xsitype.get());
\r
137 XT_log.debug("processing generic attribute");
\r
138 processAttribute(xmlObject, attribute);
\r
142 void AbstractXMLObjectUnmarshaller::unmarshallChildElements(const DOMElement* domElement, XMLObject* xmlObject) const
\r
145 xmltooling::NDC ndc("unmarshallChildElements");
\r
148 if (XT_log.isDebugEnabled()) {
\r
149 auto_ptr_char dname(domElement->getLocalName());
\r
150 XT_log.debug("unmarshalling child elements of DOM element %s", dname.get());
\r
153 DOMNodeList* childNodes = domElement->getChildNodes();
\r
154 DOMNode* childNode;
\r
155 const Unmarshaller* unmarshaller;
\r
156 if (!childNodes || childNodes->getLength()==0) {
\r
157 XT_log.debug("element had no children");
\r
161 XMLToolingConfig& config=XMLToolingConfig::getConfig();
\r
162 for (XMLSize_t i = 0; i < childNodes->getLength(); i++) {
\r
163 childNode = childNodes->item(i);
\r
164 if (childNode->getNodeType() == DOMNode::ELEMENT_NODE) {
\r
165 unmarshaller = Unmarshaller::getUnmarshaller(static_cast<DOMElement*>(childNode));
\r
166 if (!unmarshaller) {
\r
167 if (config.ignoreUnknownElements) {
\r
168 unmarshaller=Unmarshaller::getDefaultUnmarshaller();
\r
169 if (!unmarshaller) {
\r
170 auto_ptr<QName> cname(XMLHelper::getNodeQName(childNode));
\r
172 "no default unmarshaller installed, found unknown child element %s",
\r
173 cname->toString().c_str()
\r
175 throw UnmarshallingException(
\r
176 "Unmarshaller found unknown child element, but no default unmarshaller was found."
\r
180 XT_log.debug("using default unmarshaller");
\r
184 auto_ptr<QName> cname(XMLHelper::getNodeQName(childNode));
\r
185 XT_log.error("detected unknown child element %s", cname->toString().c_str());
\r
186 throw UnknownElementException("Unmarshaller found unknown child element.");
\r
190 if (XT_log.isDebugEnabled()) {
\r
191 auto_ptr<QName> cname(XMLHelper::getNodeQName(childNode));
\r
192 XT_log.debug("unmarshalling child element %s", cname->toString().c_str());
\r
194 processChildElement(xmlObject, unmarshaller->unmarshall(static_cast<DOMElement*>(childNode)));
\r