X-Git-Url: http://www.project-moonshot.org/gitweb/?a=blobdiff_plain;f=xmltooling%2Fio%2FAbstractXMLObjectUnmarshaller.cpp;h=ae2709e67b5fb0193b90754b0c66c306844ec63a;hb=81b488b2790e7bdeb2f43560b1d4a7d22c3dfdf5;hp=6532d53f7d3ed1b0118b78e7de48c936bd4e8ec6;hpb=662e29b6668db409b0d81da58f3d68a9ab12c20b;p=shibboleth%2Fcpp-xmltooling.git diff --git a/xmltooling/io/AbstractXMLObjectUnmarshaller.cpp b/xmltooling/io/AbstractXMLObjectUnmarshaller.cpp index 6532d53..ae2709e 100644 --- a/xmltooling/io/AbstractXMLObjectUnmarshaller.cpp +++ b/xmltooling/io/AbstractXMLObjectUnmarshaller.cpp @@ -1,17 +1,21 @@ -/* -* Copyright 2001-2006 Internet2 - * -* Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at +/** + * Licensed to the University Corporation for Advanced Internet + * Development, Inc. (UCAID) under one or more contributor license + * agreements. See the NOTICE file distributed with this work for + * additional information regarding copyright ownership. + * + * UCAID licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the + * License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, + * either express or implied. See the License for the specific + * language governing permissions and limitations under the License. */ /** @@ -29,13 +33,19 @@ #include "util/XMLHelper.h" #include -#include +using namespace xmlconstants; using namespace xmltooling; -using namespace log4cpp; +using namespace xercesc; using namespace std; -#define XT_log (*static_cast(m_log)) +AbstractXMLObjectUnmarshaller::AbstractXMLObjectUnmarshaller() +{ +} + +AbstractXMLObjectUnmarshaller::~AbstractXMLObjectUnmarshaller() +{ +} XMLObject* AbstractXMLObjectUnmarshaller::unmarshall(DOMElement* element, bool bindDocument) { @@ -51,16 +61,16 @@ XMLObject* AbstractXMLObjectUnmarshaller::unmarshall(DOMElement* element, bool b throw UnmarshallingException("Unrecognized element supplied to implementation for unmarshalling."); } - if (XT_log.isDebugEnabled()) { + if (m_log.isDebugEnabled()) { auto_ptr_char dname(element->getNodeName()); - XT_log.debug("unmarshalling DOM element (%s)", dname.get()); + m_log.debug("unmarshalling DOM element (%s)", dname.get()); } if (element->hasAttributes()) { unmarshallAttributes(element); } - unmarshallChildElements(element); + unmarshallContent(element); setDOM(element,bindDocument); return this; @@ -72,14 +82,14 @@ void AbstractXMLObjectUnmarshaller::unmarshallAttributes(const DOMElement* domEl xmltooling::NDC ndc("unmarshallAttributes"); #endif - if (XT_log.isDebugEnabled()) { + if (m_log.isDebugEnabled()) { auto_ptr_char dname(domElement->getNodeName()); - XT_log.debug("unmarshalling attributes for DOM element (%s)", dname.get()); + m_log.debug("unmarshalling attributes for DOM element (%s)", dname.get()); } DOMNamedNodeMap* attributes = domElement->getAttributes(); if (!attributes) { - XT_log.debug("no attributes to unmarshall"); + m_log.debug("no attributes to unmarshall"); return; } @@ -90,92 +100,115 @@ void AbstractXMLObjectUnmarshaller::unmarshallAttributes(const DOMElement* domEl // The child node should always be an attribute, but just in case if (childNode->getNodeType() != DOMNode::ATTRIBUTE_NODE) { - XT_log.debug("encountered child node of type %d in attribute list, ignoring it", childNode->getNodeType()); + m_log.debug("encountered child node of type %d in attribute list, ignoring it", childNode->getNodeType()); continue; } attribute = static_cast(childNode); const XMLCh* nsuri=attribute->getNamespaceURI(); - if (XMLString::equals(nsuri,XMLConstants::XMLNS_NS)) { - if (XMLString::equals(attribute->getLocalName(),XMLConstants::XMLNS_PREFIX)) { - XT_log.debug("found default namespace declaration, adding it to the list of namespaces on the XMLObject"); - addNamespace(Namespace(attribute->getValue(), NULL, true)); - continue; + if (XMLString::equals(nsuri,XMLNS_NS)) { + if (XMLString::equals(attribute->getLocalName(),XMLNS_PREFIX)) { + m_log.debug("found default namespace declaration, adding it to the list of namespaces on the XMLObject"); + addNamespace(Namespace(attribute->getValue(), nullptr, true)); + } + else if (XMLString::equals(attribute->getLocalName(),XML_PREFIX) && XMLString::equals(attribute->getNodeValue(),XML_NS)) { + m_log.debug("found standard xml prefix declaration, ignoring as superfluous"); } else { - XT_log.debug("found namespace declaration, adding it to the list of namespaces on the XMLObject"); + m_log.debug("found namespace declaration, adding it to the list of namespaces on the XMLObject"); addNamespace(Namespace(attribute->getValue(), attribute->getLocalName(), true)); - continue; } + continue; } - else if (XMLString::equals(nsuri,XMLConstants::XSI_NS)) { + else if (XMLString::equals(nsuri,XSI_NS)) { static const XMLCh type[]= UNICODE_LITERAL_4(t,y,p,e); static const XMLCh schemaLocation[]= UNICODE_LITERAL_14(s,c,h,e,m,a,L,o,c,a,t,i,o,n); + 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); + static const XMLCh _nil[]= UNICODE_LITERAL_3(n,i,l); if (XMLString::equals(attribute->getLocalName(),type)) { - XT_log.debug("skipping xsi:type declaration"); + m_log.debug("skipping xsi:type declaration"); continue; } else if (XMLString::equals(attribute->getLocalName(),schemaLocation)) { - XT_log.debug("storing off xsi:schemaLocation attribute"); + m_log.debug("storing off xsi:schemaLocation attribute"); if (m_schemaLocation) XMLString::release(&m_schemaLocation); m_schemaLocation=XMLString::replicate(attribute->getValue()); continue; } + else if (XMLString::equals(attribute->getLocalName(),noNamespaceSchemaLocation)) { + m_log.debug("storing off xsi:noNamespaceSchemaLocation attribute"); + if (m_noNamespaceSchemaLocation) + XMLString::release(&m_noNamespaceSchemaLocation); + m_schemaLocation=XMLString::replicate(attribute->getValue()); + m_noNamespaceSchemaLocation=XMLString::replicate(attribute->getValue()); + continue; + } + else if (XMLString::equals(attribute->getLocalName(), _nil)) { + m_log.debug("processing xsi:nil attribute"); + setNil(attribute->getValue()); + continue; + } + // Note that the prefix is visibly used. + addNamespace(Namespace(nsuri, attribute->getPrefix(), false, Namespace::VisiblyUsed)); } - else if (nsuri && !XMLString::equals(nsuri,XMLConstants::XML_NS)) { - XT_log.debug("found namespace-qualified attribute, adding prefix to the list of namespaces on the XMLObject"); - addNamespace(Namespace(nsuri, attribute->getPrefix())); + else if (nsuri && !XMLString::equals(nsuri,XML_NS)) { + m_log.debug("found namespace-qualified attribute, adding prefix to the list of visible namespaces on the XMLObject"); + addNamespace(Namespace(nsuri, attribute->getPrefix(), false, Namespace::VisiblyUsed)); } - XT_log.debug("processing generic attribute"); + m_log.debug("processing generic attribute"); processAttribute(attribute); } } -void AbstractXMLObjectUnmarshaller::unmarshallChildElements(const DOMElement* domElement) +void AbstractXMLObjectUnmarshaller::unmarshallContent(const DOMElement* domElement) { #ifdef _DEBUG - xmltooling::NDC ndc("unmarshallChildElements"); + xmltooling::NDC ndc("unmarshallContent"); #endif - if (XT_log.isDebugEnabled()) { + if (m_log.isDebugEnabled()) { auto_ptr_char dname(domElement->getNodeName()); - XT_log.debug("unmarshalling child elements of DOM element (%s)", dname.get()); + m_log.debug("unmarshalling child nodes of DOM element (%s)", dname.get()); } - DOMNodeList* childNodes = domElement->getChildNodes(); - DOMNode* childNode; - if (!childNodes || childNodes->getLength()==0) { - XT_log.debug("element had no children"); + DOMNode* childNode = domElement->getFirstChild(); + if (!childNode) { + m_log.debug("element had no children"); return; } - for (XMLSize_t i = 0; i < childNodes->getLength(); i++) { - childNode = childNodes->item(i); + unsigned int position = 0; + while (childNode) { if (childNode->getNodeType() == DOMNode::ELEMENT_NODE) { const XMLObjectBuilder* builder = XMLObjectBuilder::getBuilder(static_cast(childNode)); if (!builder) { auto_ptr cname(XMLHelper::getNodeQName(childNode)); - XT_log.error("no default builder installed, found unknown child element (%s)", cname->toString().c_str()); + m_log.error("no default builder installed, found unknown child element (%s)", cname->toString().c_str()); throw UnmarshallingException("Unmarshaller found unknown child element, but no default builder was found."); } - if (XT_log.isDebugEnabled()) { + if (m_log.isDebugEnabled()) { auto_ptr cname(XMLHelper::getNodeQName(childNode)); - XT_log.debug("unmarshalling child element (%s)", cname->toString().c_str()); + m_log.debug("unmarshalling child element (%s)", cname->toString().c_str()); } // Retain ownership of the unmarshalled child until it's processed by the parent. auto_ptr childObject(builder->buildFromElement(static_cast(childNode))); processChildElement(childObject.get(), static_cast(childNode)); childObject.release(); + + // Advance the text node position marker. + ++position; } - else if (childNode->getNodeType() == DOMNode::TEXT_NODE && !XMLString::isAllWhiteSpace(childNode->getNodeValue())) { - XT_log.debug("processing element content"); - processElementContent(childNode->getNodeValue()); + else if (childNode->getNodeType() == DOMNode::TEXT_NODE || childNode->getNodeType() == DOMNode::CDATA_SECTION_NODE) { + m_log.debug("processing text content at position (%d)", position); + setTextContent(childNode->getNodeValue(), position); } + + childNode = childNode->getNextSibling(); } } @@ -189,8 +222,3 @@ void AbstractXMLObjectUnmarshaller::processAttribute(const DOMAttr* attribute) auto_ptr q(XMLHelper::getNodeQName(attribute)); throw UnmarshallingException("Invalid attribute: $1",params(1,q->toString().c_str())); } - -void AbstractXMLObjectUnmarshaller::processElementContent(const XMLCh* elementContent) -{ - throw UnmarshallingException("Invalid text content in element."); -}