From 0ed92f33848d3f83e685364fafca1b1417dbd53c Mon Sep 17 00:00:00 2001 From: Scott Cantor Date: Tue, 2 Jan 2007 02:31:13 +0000 Subject: [PATCH] Improve parser entity handling, clean up logging categories. --- xmltooling/AbstractDOMCachingXMLObject.cpp | 13 +++---- xmltooling/AbstractXMLObject.cpp | 2 +- xmltooling/AbstractXMLObject.h | 6 ++-- xmltooling/XMLObjectBuilder.cpp | 2 +- xmltooling/impl/AnyElement.cpp | 2 -- xmltooling/impl/UnknownElement.cpp | 12 +++---- xmltooling/io/AbstractXMLObjectMarshaller.cpp | 32 ++++++++---------- xmltooling/io/AbstractXMLObjectUnmarshaller.cpp | 41 +++++++++++------------ xmltooling/signature/impl/XMLSecSignatureImpl.cpp | 6 ++-- xmltooling/util/ParserPool.cpp | 35 ++++++++++++++++--- 10 files changed, 83 insertions(+), 68 deletions(-) diff --git a/xmltooling/AbstractDOMCachingXMLObject.cpp b/xmltooling/AbstractDOMCachingXMLObject.cpp index 4966156..3381d8e 100644 --- a/xmltooling/AbstractDOMCachingXMLObject.cpp +++ b/xmltooling/AbstractDOMCachingXMLObject.cpp @@ -28,10 +28,8 @@ #include #include -#include using namespace xmltooling; -using namespace log4cpp; using namespace std; AbstractDOMCachingXMLObject::~AbstractDOMCachingXMLObject() @@ -53,10 +51,9 @@ void AbstractDOMCachingXMLObject::setDOM(DOMElement* dom, bool bindDocument) con void AbstractDOMCachingXMLObject::releaseDOM() const { if (m_dom) { - Category& log=Category::getInstance(XMLTOOLING_LOGCAT".DOM"); - if (log.isDebugEnabled()) { + if (m_log.isDebugEnabled()) { string qname=getElementQName().toString(); - log.debug("releasing cached DOM representation for (%s)", qname.empty() ? "unknown" : qname.c_str()); + m_log.debug("releasing cached DOM representation for (%s)", qname.empty() ? "unknown" : qname.c_str()); } setDOM(NULL); } @@ -65,7 +62,7 @@ void AbstractDOMCachingXMLObject::releaseDOM() const void AbstractDOMCachingXMLObject::releaseParentDOM(bool propagateRelease) const { if (getParent() && getParent()->getDOM()) { - Category::getInstance(XMLTOOLING_LOGCAT".DOM").debug( + m_log.debug( "releasing cached DOM representation for parent object with propagation set to %s", propagateRelease ? "true" : "false" ); @@ -89,7 +86,7 @@ public: void AbstractDOMCachingXMLObject::releaseChildrenDOM(bool propagateRelease) const { if (hasChildren()) { - Category::getInstance(XMLTOOLING_LOGCAT".DOM").debug( + m_log.debug( "releasing cached DOM representation for children with propagation set to %s", propagateRelease ? "true" : "false" ); @@ -117,7 +114,7 @@ XMLObject* AbstractDOMCachingXMLObject::clone() const const XMLObjectBuilder* b=XMLObjectBuilder::getBuilder(domCopy); if (!b) { auto_ptr q(XMLHelper::getNodeQName(domCopy)); - Category::getInstance(XMLTOOLING_LOGCAT".DOM").error( + m_log.error( "DOM clone failed, unable to locate builder for element (%s)", q->toString().c_str() ); domCopy->getOwnerDocument()->release(); diff --git a/xmltooling/AbstractXMLObject.cpp b/xmltooling/AbstractXMLObject.cpp index 21ec488..ba98002 100644 --- a/xmltooling/AbstractXMLObject.cpp +++ b/xmltooling/AbstractXMLObject.cpp @@ -30,7 +30,7 @@ using namespace xmltooling; AbstractXMLObject::AbstractXMLObject(const XMLCh* nsURI, const XMLCh* localName, const XMLCh* prefix, const QName* schemaType) - : m_log(&log4cpp::Category::getInstance(XMLTOOLING_LOGCAT".XMLObject")), m_schemaLocation(NULL), + : m_log(log4cpp::Category::getInstance(XMLTOOLING_LOGCAT".XMLObject")), m_schemaLocation(NULL), m_parent(NULL), m_elementQname(nsURI, localName, prefix), m_typeQname(NULL) { addNamespace(Namespace(nsURI, prefix)); diff --git a/xmltooling/AbstractXMLObject.h b/xmltooling/AbstractXMLObject.h index 80f3871..aa962e1 100644 --- a/xmltooling/AbstractXMLObject.h +++ b/xmltooling/AbstractXMLObject.h @@ -20,12 +20,14 @@ * An abstract implementation of XMLObject. */ -#if !defined(__xmltooling_abstractxmlobj_h__) +#ifndef __xmltooling_abstractxmlobj_h__ #define __xmltooling_abstractxmlobj_h__ #include #include +#include + #if defined (_MSC_VER) #pragma warning( push ) #pragma warning( disable : 4250 4251 ) @@ -192,7 +194,7 @@ namespace xmltooling { /** * Logging object. */ - void* m_log; + log4cpp::Category& m_log; /** * Stores off xsi:schemaLocation attribute. diff --git a/xmltooling/XMLObjectBuilder.cpp b/xmltooling/XMLObjectBuilder.cpp index 38d739b..838fbee 100644 --- a/xmltooling/XMLObjectBuilder.cpp +++ b/xmltooling/XMLObjectBuilder.cpp @@ -39,7 +39,7 @@ const XMLObjectBuilder* XMLObjectBuilder::getBuilder(const DOMElement* domElemen #ifdef _DEBUG xmltooling::NDC ndc("getBuilder"); #endif - Category& log=Category::getInstance(XMLTOOLING_LOGCAT".XMLObjectBuilder"); + Category& log=Category::getInstance(XMLTOOLING_LOGCAT".XMLObject.Builder"); auto_ptr schemaType(XMLHelper::getXSIType(domElement)); const XMLObjectBuilder* xmlObjectBuilder = schemaType.get() ? getBuilder(*(schemaType.get())) : NULL; diff --git a/xmltooling/impl/AnyElement.cpp b/xmltooling/impl/AnyElement.cpp index 5e813a5..c90726a 100644 --- a/xmltooling/impl/AnyElement.cpp +++ b/xmltooling/impl/AnyElement.cpp @@ -26,11 +26,9 @@ #include "util/NDC.h" #include "util/XMLHelper.h" -#include #include using namespace xmltooling; -using namespace log4cpp; using namespace std; XMLObject* AnyElementImpl::clone() const { diff --git a/xmltooling/impl/UnknownElement.cpp b/xmltooling/impl/UnknownElement.cpp index 00b234e..938bb09 100644 --- a/xmltooling/impl/UnknownElement.cpp +++ b/xmltooling/impl/UnknownElement.cpp @@ -40,7 +40,7 @@ void UnknownElementImpl::releaseDOM() const #ifdef _DEBUG xmltooling::NDC ndc("releaseDOM"); #endif - Category& log=Category::getInstance(XMLTOOLING_LOGCAT".UnknownElementImpl"); + Category& log=Category::getInstance(XMLTOOLING_LOGCAT".XMLObject"); log.debug("releasing DOM for unknown content, preserving current DOM in XML form"); // We're losing our DOM, so assuming we have one, we preserve it. @@ -81,7 +81,7 @@ DOMElement* UnknownElementImpl::marshall( xmltooling::NDC ndc("marshall"); #endif - Category& log=Category::getInstance(XMLTOOLING_LOGCAT".Marshaller"); + Category& log=Category::getInstance(XMLTOOLING_LOGCAT".XMLObject"); log.debug("marshalling unknown content"); DOMElement* cachedDOM=getDOM(); @@ -148,7 +148,7 @@ DOMElement* UnknownElementImpl::marshall( xmltooling::NDC ndc("marshall"); #endif - Category& log=Category::getInstance(XMLTOOLING_LOGCAT".Marshaller"); + Category& log=Category::getInstance(XMLTOOLING_LOGCAT".XMLObject"); log.debug("marshalling unknown content"); DOMElement* cachedDOM=getDOM(); @@ -199,8 +199,8 @@ XMLObject* UnknownElementImpl::unmarshall(DOMElement* element, bool bindDocument } XMLObject* UnknownElementBuilder::buildObject( - const XMLCh* nsURI, const XMLCh* localName, const XMLCh* prefix, const QName* schemaType - ) const { - return new UnknownElementImpl(nsURI,localName,prefix); + const XMLCh* nsURI, const XMLCh* localName, const XMLCh* prefix, const QName* schemaType + ) const { + return new UnknownElementImpl(nsURI,localName,prefix); } diff --git a/xmltooling/io/AbstractXMLObjectMarshaller.cpp b/xmltooling/io/AbstractXMLObjectMarshaller.cpp index 4822fbe..3cc8f3f 100644 --- a/xmltooling/io/AbstractXMLObjectMarshaller.cpp +++ b/xmltooling/io/AbstractXMLObjectMarshaller.cpp @@ -33,18 +33,14 @@ #include #include #include -#include #ifndef XMLTOOLING_NO_XMLSEC using namespace xmlsignature; #endif using namespace xmlconstants; using namespace xmltooling; -using namespace log4cpp; using namespace std; -#define XT_log (*static_cast(m_log)) - DOMElement* AbstractXMLObjectMarshaller::marshall( DOMDocument* document #ifndef XMLTOOLING_NO_XMLSEC @@ -56,14 +52,14 @@ DOMElement* AbstractXMLObjectMarshaller::marshall( xmltooling::NDC ndc("marshall"); #endif - if (XT_log.isDebugEnabled()) { - XT_log.debug("starting to marshal %s", getElementQName().toString().c_str()); + if (m_log.isDebugEnabled()) { + m_log.debug("starting to marshal %s", getElementQName().toString().c_str()); } DOMElement* cachedDOM=getDOM(); if (cachedDOM) { if (!document || document==cachedDOM->getOwnerDocument()) { - XT_log.debug("XMLObject has a usable cached DOM, reusing it"); + m_log.debug("XMLObject has a usable cached DOM, reusing it"); if (document) setDocumentElement(cachedDOM->getOwnerDocument(),cachedDOM); releaseParentDOM(true); @@ -89,7 +85,7 @@ DOMElement* AbstractXMLObjectMarshaller::marshall( XercesJanitor janitor(bindDocument ? document : NULL); - XT_log.debug("creating root element to marshall"); + m_log.debug("creating root element to marshall"); DOMElement* domElement = document->createElementNS( getElementQName().getNamespaceURI(), getElementQName().getLocalPart() ); @@ -100,7 +96,7 @@ DOMElement* AbstractXMLObjectMarshaller::marshall( marshallInto(domElement); #endif //Recache the DOM. - XT_log.debug("caching DOM for XMLObject (document is %sbound)", bindDocument ? "" : "not "); + m_log.debug("caching DOM for XMLObject (document is %sbound)", bindDocument ? "" : "not "); setDOM(domElement, bindDocument); janitor.release(); // safely transferred releaseParentDOM(true); @@ -119,14 +115,14 @@ DOMElement* AbstractXMLObjectMarshaller::marshall( xmltooling::NDC ndc("marshall"); #endif - if (XT_log.isDebugEnabled()) { - XT_log.debug("starting to marshalling %s", getElementQName().toString().c_str()); + if (m_log.isDebugEnabled()) { + m_log.debug("starting to marshalling %s", getElementQName().toString().c_str()); } DOMElement* cachedDOM=getDOM(); if (cachedDOM) { if (parentElement->getOwnerDocument()==cachedDOM->getOwnerDocument()) { - XT_log.debug("XMLObject has a usable cached DOM, reusing it"); + m_log.debug("XMLObject has a usable cached DOM, reusing it"); if (parentElement!=cachedDOM->getParentNode()) { parentElement->appendChild(cachedDOM); releaseParentDOM(true); @@ -144,7 +140,7 @@ DOMElement* AbstractXMLObjectMarshaller::marshall( } // If we get here, we didn't have a usable DOM (and/or we released the one we had). - XT_log.debug("creating root element to marshall"); + m_log.debug("creating root element to marshall"); DOMElement* domElement = parentElement->getOwnerDocument()->createElementNS( getElementQName().getNamespaceURI(), getElementQName().getLocalPart() ); @@ -156,7 +152,7 @@ DOMElement* AbstractXMLObjectMarshaller::marshall( #endif //Recache the DOM. - XT_log.debug("caching DOM for XMLObject"); + m_log.debug("caching DOM for XMLObject"); setDOM(domElement, false); releaseParentDOM(true); @@ -195,7 +191,7 @@ void AbstractXMLObjectMarshaller::marshallElementType(DOMElement* domElement) co { const QName* type = getSchemaType(); if (type) { - XT_log.debug("setting xsi:type attribute for XMLObject"); + m_log.debug("setting xsi:type attribute for XMLObject"); const XMLCh* typeLocalName = type->getLocalPart(); if (!typeLocalName || !*typeLocalName) { @@ -220,7 +216,7 @@ void AbstractXMLObjectMarshaller::marshallElementType(DOMElement* domElement) co if (xsivalue != typeLocalName) XMLString::release(&xsivalue); - XT_log.debug("Adding XSI namespace to list of namespaces used by XMLObject"); + m_log.debug("Adding XSI namespace to list of namespaces used by XMLObject"); addNamespace(Namespace(XSI_NS, XSI_PREFIX)); } } @@ -285,14 +281,14 @@ public: void AbstractXMLObjectMarshaller::marshallNamespaces(DOMElement* domElement) const { - XT_log.debug("marshalling namespace attributes for XMLObject"); + m_log.debug("marshalling namespace attributes for XMLObject"); const set& namespaces = getNamespaces(); for_each(namespaces.begin(),namespaces.end(),bind1st(_addns(),domElement)); } void AbstractXMLObjectMarshaller::marshallContent(DOMElement* domElement) const { - XT_log.debug("marshalling text and child elements for XMLObject"); + m_log.debug("marshalling text and child elements for XMLObject"); const XMLCh* val; unsigned int pos=0; diff --git a/xmltooling/io/AbstractXMLObjectUnmarshaller.cpp b/xmltooling/io/AbstractXMLObjectUnmarshaller.cpp index 5470c3a..7230fda 100644 --- a/xmltooling/io/AbstractXMLObjectUnmarshaller.cpp +++ b/xmltooling/io/AbstractXMLObjectUnmarshaller.cpp @@ -29,14 +29,11 @@ #include "util/XMLHelper.h" #include -#include using namespace xmlconstants; using namespace xmltooling; -using namespace log4cpp; using namespace std; -#define XT_log (*static_cast(m_log)) XMLObject* AbstractXMLObjectUnmarshaller::unmarshall(DOMElement* element, bool bindDocument) { @@ -52,9 +49,9 @@ 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()) { @@ -73,14 +70,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; } @@ -91,7 +88,7 @@ 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; } @@ -100,12 +97,12 @@ void AbstractXMLObjectUnmarshaller::unmarshallAttributes(const DOMElement* domEl const XMLCh* nsuri=attribute->getNamespaceURI(); if (XMLString::equals(nsuri,XMLNS_NS)) { if (XMLString::equals(attribute->getLocalName(),XMLNS_PREFIX)) { - XT_log.debug("found default namespace declaration, adding it to the list of namespaces on the XMLObject"); + m_log.debug("found default namespace declaration, adding it to the list of namespaces on the XMLObject"); addNamespace(Namespace(attribute->getValue(), NULL, true)); continue; } 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; } @@ -114,11 +111,11 @@ void AbstractXMLObjectUnmarshaller::unmarshallAttributes(const DOMElement* domEl 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); 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()); @@ -126,11 +123,11 @@ void AbstractXMLObjectUnmarshaller::unmarshallAttributes(const DOMElement* domEl } } else if (nsuri && !XMLString::equals(nsuri,XML_NS)) { - XT_log.debug("found namespace-qualified attribute, adding prefix to the list of namespaces on the XMLObject"); + m_log.debug("found namespace-qualified attribute, adding prefix to the list of namespaces on the XMLObject"); addNamespace(Namespace(nsuri, attribute->getPrefix())); } - XT_log.debug("processing generic attribute"); + m_log.debug("processing generic attribute"); processAttribute(attribute); } } @@ -141,14 +138,14 @@ void AbstractXMLObjectUnmarshaller::unmarshallContent(const DOMElement* domEleme xmltooling::NDC ndc("unmarshallContent"); #endif - if (XT_log.isDebugEnabled()) { + if (m_log.isDebugEnabled()) { auto_ptr_char dname(domElement->getNodeName()); - XT_log.debug("unmarshalling child nodes of DOM element (%s)", dname.get()); + m_log.debug("unmarshalling child nodes of DOM element (%s)", dname.get()); } DOMNode* childNode = domElement->getFirstChild(); if (!childNode) { - XT_log.debug("element had no children"); + m_log.debug("element had no children"); return; } @@ -158,13 +155,13 @@ void AbstractXMLObjectUnmarshaller::unmarshallContent(const DOMElement* domEleme 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. @@ -176,7 +173,7 @@ void AbstractXMLObjectUnmarshaller::unmarshallContent(const DOMElement* domEleme ++position; } else if (childNode->getNodeType() == DOMNode::TEXT_NODE) { - XT_log.debug("processing text content at position (%d)", position); + m_log.debug("processing text content at position (%d)", position); setTextContent(childNode->getNodeValue(), position); } diff --git a/xmltooling/signature/impl/XMLSecSignatureImpl.cpp b/xmltooling/signature/impl/XMLSecSignatureImpl.cpp index 6199613..6d82a0a 100644 --- a/xmltooling/signature/impl/XMLSecSignatureImpl.cpp +++ b/xmltooling/signature/impl/XMLSecSignatureImpl.cpp @@ -209,7 +209,7 @@ DOMElement* XMLSecSignatureImpl::marshall(DOMDocument* document, const vectornewSignatureFromDOM( diff --git a/xmltooling/util/ParserPool.cpp b/xmltooling/util/ParserPool.cpp index c471335..e57b118 100644 --- a/xmltooling/util/ParserPool.cpp +++ b/xmltooling/util/ParserPool.cpp @@ -221,23 +221,48 @@ DOMInputSource* ParserPool::resolveEntity(const XMLCh* const publicId, const XML log.debug("asked to resolve %s with baseURI %s",sysId.get(),base.get() ? base.get() : "(null)"); } - // Find well-known schemas in the specified location. #ifdef HAVE_GOOD_STL + // Find well-known schemas in the specified location. map::const_iterator i=m_schemaLocMap.find(systemId); if (i!=m_schemaLocMap.end()) - return new Wrapper4InputSource(new LocalFileInputSource(NULL,i->second.c_str())); + return new Wrapper4InputSource(new LocalFileInputSource(baseURI,i->second.c_str())); + + // We'll allow anything without embedded slashes. + if (XMLString::indexOf(systemId, chForwardSlash)==-1) + return new Wrapper4InputSource(new LocalFileInputSource(baseURI,systemId)); + + // Check for entity as a value in the map. + for (i=m_schemaLocMap.begin(); i!=m_schemaLocMap.end(); ++i) { + if (XMLString::endsWith(i->second.c_str(), systemId)) + return new Wrapper4InputSource(new LocalFileInputSource(baseURI,i->second.c_str())); + } #else + // Find well-known schemas in the specified location. auto_ptr_char temp(systemId); map::const_iterator i=m_schemaLocMap.find(temp.get()); if (i!=m_schemaLocMap.end()) { auto_ptr_XMLCh temp2(i->second.c_str()); - return new Wrapper4InputSource(new LocalFileInputSource(NULL,temp2.get())); + return new Wrapper4InputSource(new LocalFileInputSource(baseURI,temp2.get())); + } + + // We'll allow anything without embedded slashes. + if (XMLString::indexOf(systemId, chForwardSlash)==-1) + return new Wrapper4InputSource(new LocalFileInputSource(baseURI,systemId)); + + // Check for entity as a value in the map. + for (i=m_schemaLocMap.begin(); i!=m_schemaLocMap.end(); ++i) { + if (XMLString::endsWith(i->second.c_str(), temp.get())) { + auto_ptr_XMLCh temp2(i->second.c_str()); + return new Wrapper4InputSource(new LocalFileInputSource(baseURI,temp2.get())); + } } #endif // Shortcircuit the request. - auto_ptr_char sysId(systemId); - log.warn("unauthorized entity request (%s), blocking it", sysId.get() ? sysId.get() : "no systemId"); +#ifdef HAVE_GOOD_STL + auto_ptr_char temp(systemId); +#endif + log.warn("unauthorized entity request (%s), blocking it", temp.get()); static const XMLByte nullbuf[] = {0}; return new Wrapper4InputSource(new MemBufInputSource(nullbuf,0,systemId)); } -- 2.1.4