Solaris fixes
[shibboleth/cpp-xmltooling.git] / xmltooling / util / ParserPool.cpp
index 39d6369..712b121 100644 (file)
@@ -1,17 +1,21 @@
-/*
- *  Copyright 2001-2009 Internet2
+/**
+ * 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.
  *
- * 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
+ * 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.
  */
 
 /**
 #include "util/Threads.h"
 #include "util/XMLHelper.h"
 
-#include <algorithm>
-#include <functional>
 #include <sys/types.h>
 #include <sys/stat.h>
+#include <algorithm>
+#include <functional>
+#include <boost/algorithm/string.hpp>
+#include <boost/bind.hpp>
+#include <boost/tokenizer.hpp>
 #include <xercesc/util/PlatformUtils.hpp>
 #include <xercesc/util/XMLUniDefs.hpp>
 #include <xercesc/sax/SAXException.hpp>
@@ -43,6 +50,7 @@
 using namespace xmltooling::logging;
 using namespace xmltooling;
 using namespace xercesc;
+using namespace boost;
 using namespace std;
 
 
@@ -104,13 +112,11 @@ ParserPool::~ParserPool()
         m_pool.top()->release();
         m_pool.pop();
     }
-    delete m_lock;
-    delete m_security;
 }
 
 DOMDocument* ParserPool::newDocument()
 {
-    return DOMImplementationRegistry::getDOMImplementation(NULL)->createDocument();
+    return DOMImplementationRegistry::getDOMImplementation(nullptr)->createDocument();
 }
 
 #ifdef XMLTOOLING_XERCESC_COMPLIANT_DOMLS
@@ -128,20 +134,20 @@ DOMDocument* ParserPool::parse(DOMLSInput& domsrc)
                 doc->release();
             throw XMLParserException("XML error(s) during parsing, check log for specifics");
         }
-        parser->getDomConfig()->setParameter(XMLUni::fgDOMErrorHandler, (void*)NULL);
+        parser->getDomConfig()->setParameter(XMLUni::fgDOMErrorHandler, (void*)nullptr);
         parser->getDomConfig()->setParameter(XMLUni::fgXercesUserAdoptsDOMDocument, true);
         checkinBuilder(janitor.release());
         return doc;
     }
     catch (XMLException& ex) {
-        parser->getDomConfig()->setParameter(XMLUni::fgDOMErrorHandler, (void*)NULL);
+        parser->getDomConfig()->setParameter(XMLUni::fgDOMErrorHandler, (void*)nullptr);
         parser->getDomConfig()->setParameter(XMLUni::fgXercesUserAdoptsDOMDocument, true);
         checkinBuilder(janitor.release());
         auto_ptr_char temp(ex.getMessage());
         throw XMLParserException(string("Xerces error during parsing: ") + (temp.get() ? temp.get() : "no message"));
     }
     catch (XMLToolingException&) {
-        parser->getDomConfig()->setParameter(XMLUni::fgDOMErrorHandler, (void*)NULL);
+        parser->getDomConfig()->setParameter(XMLUni::fgDOMErrorHandler, (void*)nullptr);
         parser->getDomConfig()->setParameter(XMLUni::fgXercesUserAdoptsDOMDocument, true);
         checkinBuilder(janitor.release());
         throw;
@@ -163,20 +169,20 @@ DOMDocument* ParserPool::parse(DOMInputSource& domsrc)
                 doc->release();
             throw XMLParserException("XML error(s) during parsing, check log for specifics");
         }
-        parser->setErrorHandler(NULL);
+        parser->setErrorHandler(nullptr);
         parser->setFeature(XMLUni::fgXercesUserAdoptsDOMDocument, true);
         checkinBuilder(janitor.release());
         return doc;
     }
     catch (XMLException& ex) {
-        parser->setErrorHandler(NULL);
+        parser->setErrorHandler(nullptr);
         parser->setFeature(XMLUni::fgXercesUserAdoptsDOMDocument, true);
         checkinBuilder(janitor.release());
         auto_ptr_char temp(ex.getMessage());
         throw XMLParserException(string("Xerces error during parsing: ") + (temp.get() ? temp.get() : "no message"));
     }
     catch (XMLToolingException&) {
-        parser->setErrorHandler(NULL);
+        parser->setErrorHandler(nullptr);
         parser->setFeature(XMLUni::fgXercesUserAdoptsDOMDocument, true);
         checkinBuilder(janitor.release());
         throw;
@@ -193,8 +199,7 @@ DOMDocument* ParserPool::parse(istream& is)
 }
 
 // Functor to double its argument separated by a character and append to a buffer
-template <class T> class doubleit
-{
+template <class T> class doubleit {
 public:
     doubleit(T& t, const typename T::value_type& s) : temp(t), sep(s) {}
     void operator() (const pair<const T,T>& s) { temp += s.first + sep + s.first + sep; }
@@ -227,11 +232,29 @@ bool ParserPool::loadSchema(const XMLCh* nsURI, const XMLCh* pathname)
     Lock lock(m_lock);
     m_schemaLocMap[nsURI]=pathname;
     m_schemaLocations.erase();
-    for_each(m_schemaLocMap.begin(),m_schemaLocMap.end(),doubleit<xstring>(m_schemaLocations,chSpace));
+    for_each(m_schemaLocMap.begin(), m_schemaLocMap.end(), doubleit<xstring>(m_schemaLocations,chSpace));
 
     return true;
 }
 
+bool ParserPool::loadCatalogs(const char* pathnames)
+{
+    string temp(pathnames);
+    boost::tokenizer< char_separator<char> > catpaths(temp, char_separator<char>(PATH_SEPARATOR_STR));
+    for_each(
+        catpaths.begin(), catpaths.end(),
+        // Call loadCatalog with an inner call to s->c_str() on each entry.
+        boost::bind(static_cast<bool (ParserPool::*)(const char*)>(&ParserPool::loadCatalog), this, boost::bind(&string::c_str, _1))
+        );
+    return catpaths.begin() != catpaths.end();
+}
+
+bool ParserPool::loadCatalog(const char* pathname)
+{
+    auto_ptr_XMLCh temp(pathname);
+    return loadCatalog(temp.get());
+}
+
 bool ParserPool::loadCatalog(const XMLCh* pathname)
 {
 #if _DEBUG
@@ -262,7 +285,7 @@ bool ParserPool::loadCatalog(const XMLCh* pathname)
         log.debug("loading XML catalog from %s", temp.get());
     }
 
-    LocalFileInputSource fsrc(NULL,pathname);
+    LocalFileInputSource fsrc(nullptr,pathname);
     Wrapper4InputSource domsrc(&fsrc,false);
     try {
         DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(domsrc);
@@ -281,14 +304,14 @@ bool ParserPool::loadCatalog(const XMLCh* pathname)
         Lock lock(m_lock);
         for (XMLSize_t i=0; i<mappings->getLength(); i++) {
             root=static_cast<DOMElement*>(mappings->item(i));
-            const XMLCh* from=root->getAttributeNS(NULL,systemId);
-            const XMLCh* to=root->getAttributeNS(NULL,uri);
+            const XMLCh* from=root->getAttributeNS(nullptr,systemId);
+            const XMLCh* to=root->getAttributeNS(nullptr,uri);
             m_schemaLocMap[from]=to;
         }
         m_schemaLocations.erase();
-        for_each(m_schemaLocMap.begin(),m_schemaLocMap.end(),doubleit<xstring>(m_schemaLocations,chSpace));
+        for_each(m_schemaLocMap.begin(), m_schemaLocMap.end(), doubleit<xstring>(m_schemaLocations,chSpace));
     }
-    catch (exception& e) {
+    catch (std::exception& e) {
         log.error("catalog loader caught exception: %s", e.what());
         return false;
     }
@@ -314,7 +337,8 @@ DOMInputSource* ParserPool::resolveEntity(
     xmltooling::NDC ndc("resolveEntity");
 #endif
     if (!systemId)
-        return NULL;
+        return nullptr;
+    xstring sysId(systemId);
 
     Category& log=Category::getInstance(XMLTOOLING_LOGCAT".ParserPool");
     if (log.isDebugEnabled()) {
@@ -324,25 +348,28 @@ DOMInputSource* ParserPool::resolveEntity(
     }
 
     // Find well-known schemas in the specified location.
-    map<xstring,xstring>::const_iterator i=m_schemaLocMap.find(systemId);
-    if (i!=m_schemaLocMap.end())
-        return new Wrapper4InputSource(new LocalFileInputSource(baseURI,i->second.c_str()));
-
-    // 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()));
-    }
+    map<xstring,xstring>::const_iterator i = m_schemaLocMap.find(sysId);
+    if (i != m_schemaLocMap.end())
+        return new Wrapper4InputSource(new LocalFileInputSource(baseURI, i->second.c_str()));
+
+    // Check for entity as a suffix of a value in the map.
+    bool (*p_ends_with)(const xstring&, const xstring&) = ends_with;
+    i = find_if(
+        m_schemaLocMap.begin(), m_schemaLocMap.end(),
+        boost::bind(p_ends_with, boost::bind(&map<xstring,xstring>::value_type::second, _1), boost::ref(sysId))
+        );
+    if (i != m_schemaLocMap.end())
+        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));
+    if (XMLString::indexOf(systemId, chForwardSlash) == -1 && XMLString::indexOf(systemId, chBackSlash) == -1)
+        return new Wrapper4InputSource(new LocalFileInputSource(baseURI, systemId));
 
     // Shortcircuit the request.
     auto_ptr_char temp(systemId);
     log.debug("unauthorized entity request (%s), blocking it", temp.get());
     static const XMLByte nullbuf[] = {0};
-    return new Wrapper4InputSource(new MemBufInputSource(nullbuf,0,systemId));
+    return new Wrapper4InputSource(new MemBufInputSource(nullbuf, 0, systemId));
 }
 
 #ifdef XMLTOOLING_XERCESC_COMPLIANT_DOMLS
@@ -351,7 +378,7 @@ DOMLSParser* ParserPool::createBuilder()
 {
     static const XMLCh impltype[] = { chLatin_L, chLatin_S, chNull };
     DOMImplementation* impl=DOMImplementationRegistry::getDOMImplementation(impltype);
-    DOMLSParser* parser=static_cast<DOMImplementationLS*>(impl)->createLSParser(DOMImplementationLS::MODE_SYNCHRONOUS,NULL);
+    DOMLSParser* parser=static_cast<DOMImplementationLS*>(impl)->createLSParser(DOMImplementationLS::MODE_SYNCHRONOUS,nullptr);
     parser->getDomConfig()->setParameter(XMLUni::fgDOMNamespaces, m_namespaceAware);
     if (m_schemaAware) {
         parser->getDomConfig()->setParameter(XMLUni::fgDOMNamespaces, true);
@@ -366,7 +393,7 @@ DOMLSParser* ParserPool::createBuilder()
     parser->getDomConfig()->setParameter(XMLUni::fgXercesUserAdoptsDOMDocument, true);
     parser->getDomConfig()->setParameter(XMLUni::fgXercesDisableDefaultEntityResolution, true);
     parser->getDomConfig()->setParameter(XMLUni::fgDOMResourceResolver, dynamic_cast<DOMLSResourceResolver*>(this));
-    parser->getDomConfig()->setParameter(XMLUni::fgXercesSecurityManager, m_security);
+    parser->getDomConfig()->setParameter(XMLUni::fgXercesSecurityManager, m_security.get());
     return parser;
 }
 
@@ -410,7 +437,7 @@ DOMBuilder* ParserPool::createBuilder()
         // This ensures the entity resolver will be given the namespace as a systemId it can check.
         parser->setProperty(XMLUni::fgXercesSchemaExternalSchemaLocation,const_cast<XMLCh*>(m_schemaLocations.c_str()));
     }
-    parser->setProperty(XMLUni::fgXercesSecurityManager, m_security);
+    parser->setProperty(XMLUni::fgXercesSecurityManager, m_security.get());
     parser->setFeature(XMLUni::fgXercesUserAdoptsDOMDocument, true);
     parser->setFeature(XMLUni::fgXercesDisableDefaultEntityResolution, true);
     parser->setEntityResolver(this);
@@ -467,7 +494,7 @@ StreamInputSource::StreamBinInputStream::curPos() const
 #ifdef XMLTOOLING_XERCESC_64BITSAFE
 const XMLCh* StreamInputSource::StreamBinInputStream::getContentType() const
 {
-    return NULL;
+    return nullptr;
 }
 #endif
 
@@ -505,9 +532,9 @@ URLInputSource::URLInputSource(const DOMElement* e, const char* systemId, string
     static const XMLCh uri[] = UNICODE_LITERAL_3(u,r,i);
     static const XMLCh url[] = UNICODE_LITERAL_3(u,r,l);
 
-    const XMLCh* attr = e->getAttributeNS(NULL, url);
+    const XMLCh* attr = e->getAttributeNS(nullptr, url);
     if (!attr || !*attr) {
-        attr = e->getAttributeNS(NULL, uri);
+        attr = e->getAttributeNS(nullptr, uri);
         if (!attr || !*attr)
             throw IOException("No URL supplied via DOM to URLInputSource constructor.");
     }
@@ -524,7 +551,7 @@ BinInputStream* URLInputSource::makeStream() const
 #else
 
 URLInputSource::URLInputSource(const XMLCh* url, const char* systemId, string* cacheTag)
-    : InputSource(systemId), m_cacheTag(cacheTag), m_url(url), m_root(NULL)
+    : InputSource(systemId), m_cacheTag(cacheTag), m_url(url), m_root(nullptr)
 {
 }