Multi-line svn commit, see body.
[shibboleth/cpp-xmltooling.git] / xmltooling / util / ParserPool.cpp
index c471335..693cef4 100644 (file)
@@ -1,5 +1,5 @@
 /*
- *  Copyright 2001-2006 Internet2
+ *  Copyright 2001-2007 Internet2
  * 
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -22,6 +22,7 @@
 
 #include "internal.h"
 #include "exceptions.h"
+#include "logging.h"
 #include "util/NDC.h"
 #include "util/ParserPool.h"
 #include "util/XMLHelper.h"
@@ -30,7 +31,6 @@
 #include <functional>
 #include <sys/types.h>
 #include <sys/stat.h>
-#include <log4cpp/Category.hh>
 #include <xercesc/util/PlatformUtils.hpp>
 #include <xercesc/util/XMLUniDefs.hpp>
 #include <xercesc/sax/SAXException.hpp>
@@ -38,9 +38,9 @@
 #include <xercesc/framework/LocalFileInputSource.hpp>
 #include <xercesc/framework/Wrapper4InputSource.hpp>
 
+using namespace xmltooling::logging;
 using namespace xmltooling;
 using namespace std;
-using namespace log4cpp;
 
 ParserPool::ParserPool(bool namespaceAware, bool schemaAware)
     : m_namespaceAware(namespaceAware), m_schemaAware(schemaAware), m_lock(Mutex::create()) {}
@@ -91,7 +91,7 @@ template <class T> class doubleit
 {
 public:
     doubleit(T& t, const typename T::value_type& s) : temp(t), sep(s) {}
-    void operator() (const pair<T,T>& s) { temp += s.first + sep + s.first + sep; }
+    void operator() (const pair<const T,T>& s) { temp += s.first + sep + s.first + sep; }
     T& temp;
     const typename T::value_type& sep;
 };
@@ -141,9 +141,10 @@ bool ParserPool::loadCatalog(const XMLCh* pathname)
     Category& log=Category::getInstance(XMLTOOLING_LOGCAT".ParserPool");
 
     // XML constants
-    static const XMLCh catalog[] = { chLatin_c, chLatin_a, chLatin_t, chLatin_a, chLatin_l, chLatin_o, chLatin_g, chNull };
-    static const XMLCh uri[] = { chLatin_u, chLatin_r, chLatin_i, chNull };
-    static const XMLCh name[] = { chLatin_n, chLatin_a, chLatin_m, chLatin_e, chNull };
+    static const XMLCh catalog[] =  UNICODE_LITERAL_7(c,a,t,a,l,o,g);
+    static const XMLCh system[] =   UNICODE_LITERAL_6(s,y,s,t,e,m);
+    static const XMLCh systemId[] = UNICODE_LITERAL_8(s,y,s,t,e,m,I,d);
+    static const XMLCh uri[] =      UNICODE_LITERAL_3(u,r,i);
     static const XMLCh CATALOG_NS[] = {
         chLatin_u, chLatin_r, chLatin_n, chColon,
         chLatin_o, chLatin_a, chLatin_s, chLatin_i, chLatin_s, chColon,
@@ -176,12 +177,12 @@ bool ParserPool::loadCatalog(const XMLCh* pathname)
             return false;
         }
         
-        // Fetch all the <uri> elements.
-        DOMNodeList* mappings=root->getElementsByTagNameNS(CATALOG_NS,uri);
+        // Fetch all the <system> elements.
+        DOMNodeList* mappings=root->getElementsByTagNameNS(CATALOG_NS,system);
         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,name);
+            const XMLCh* from=root->getAttributeNS(NULL,systemId);
             const XMLCh* to=root->getAttributeNS(NULL,uri);
 #ifdef HAVE_GOOD_STL
             m_schemaLocMap[from]=to;
@@ -198,8 +199,8 @@ bool ParserPool::loadCatalog(const XMLCh* pathname)
         for_each(m_schemaLocMap.begin(),m_schemaLocMap.end(),doubleit<string>(m_schemaLocations,' '));
 #endif
     }
-    catch (XMLParserException& e) {
-        log.error("catalog loader caught XMLParserException: %s", e.what());
+    catch (exception& e) {
+        log.error("catalog loader caught exception: %s", e.what());
         return false;
     }
 
@@ -221,23 +222,47 @@ 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<xstring,xstring>::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()));
+
+    // 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()));
+    }
+
+    // We'll allow anything without embedded slashes.
+    if (XMLString::indexOf(systemId, chForwardSlash)==-1)
+        return new Wrapper4InputSource(new LocalFileInputSource(baseURI,systemId));
 #else
+    // Find well-known schemas in the specified location.
     auto_ptr_char temp(systemId);
     map<string,string>::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()));
     }
+
+    // Check for entity as a value in the map.
+    for (i=m_schemaLocMap.begin(); i!=m_schemaLocMap.end(); ++i) {
+        auto_ptr_XMLCh temp2(i->second.c_str());
+        if (XMLString::endsWith(temp2.get(), systemId))
+            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));
 #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.debug("unauthorized entity request (%s), blocking it", temp.get());
     static const XMLByte nullbuf[] = {0};
     return new Wrapper4InputSource(new MemBufInputSource(nullbuf,0,systemId));
 }
@@ -255,19 +280,19 @@ bool ParserPool::handleError(const DOMError& e)
         case DOMError::DOM_SEVERITY_WARNING:
             log.warnStream() << "warning on line " << locator->getLineNumber()
                 << ", column " << locator->getColumnNumber()
-                << ", message: " << temp.get() << CategoryStream::ENDLINE;
+                << ", message: " << temp.get() << logging::eol;
             return true;
 
         case DOMError::DOM_SEVERITY_ERROR:
             log.errorStream() << "error on line " << locator->getLineNumber()
                 << ", column " << locator->getColumnNumber()
-                << ", message: " << temp.get() << CategoryStream::ENDLINE;
+                << ", message: " << temp.get() << logging::eol;
             throw XMLParserException(string("error during XML parsing: ") + (temp.get() ? temp.get() : "no message"));
 
         case DOMError::DOM_SEVERITY_FATAL_ERROR:
             log.critStream() << "fatal error on line " << locator->getLineNumber()
                 << ", column " << locator->getColumnNumber()
-                << ", message: " << temp.get() << CategoryStream::ENDLINE;
+                << ", message: " << temp.get() << logging::eol;
             throw XMLParserException(string("fatal error during XML parsing: ") + (temp.get() ? temp.get() : "no message"));
     }
     throw XMLParserException(string("unclassified error during XML parsing: ") + (temp.get() ? temp.get() : "no message"));
@@ -344,7 +369,7 @@ unsigned int StreamInputSource::StreamBinInputStream::readBytes(XMLByte* const t
         catch(ios_base::failure& e) {
             Category::getInstance(XMLTOOLING_LOGCAT".StreamInputSource").critStream()
                 << "XML::StreamInputSource::StreamBinInputStream::readBytes caught an exception: " << e.what()
-                << CategoryStream::ENDLINE;
+                << logging::eol;
             *toFill=0;
             return 0;
         }