Xerces 3 revisions.
[shibboleth/xmltooling.git] / xmltooling / util / ParserPool.cpp
index a06e07f..abe019c 100644 (file)
@@ -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>
 #include <xercesc/framework/LocalFileInputSource.hpp>
 #include <xercesc/framework/Wrapper4InputSource.hpp>
 
+using namespace xmltooling::logging;
 using namespace xmltooling;
+using namespace xercesc;
 using namespace std;
-using namespace log4cpp;
 
 ParserPool::ParserPool(bool namespaceAware, bool schemaAware)
-    : m_namespaceAware(namespaceAware), m_schemaAware(schemaAware), m_lock(Mutex::create()) {}
+    : m_namespaceAware(namespaceAware), m_schemaAware(schemaAware), m_lock(Mutex::create()), m_security(new SecurityManager()) {}
 
 ParserPool::~ParserPool()
 {
@@ -52,6 +53,7 @@ ParserPool::~ParserPool()
         m_pool.pop();
     }
     delete m_lock;
+    delete m_security;
 }
 
 DOMDocument* ParserPool::newDocument()
@@ -59,13 +61,26 @@ DOMDocument* ParserPool::newDocument()
     return DOMImplementationRegistry::getDOMImplementation(NULL)->createDocument();
 }
 
-DOMDocument* ParserPool::parse(DOMInputSource& domsrc)
+DOMDocument* ParserPool::parse(
+#ifdef XMLTOOLING_XERCESC_COMPLIANT_DOMLS
+   DOMLSInput& domsrc
+   )
+{
+    DOMLSParser* parser=checkoutBuilder();
+    XercesJanitor<DOMLSParser> janitor(parser);
+    try {
+        DOMDocument* doc=parser->parse(&domsrc);
+        parser->getDomConfig()->setParameter(XMLUni::fgXercesUserAdoptsDOMDocument,true);
+#else
+   DOMInputSource& domsrc
+   )
 {
     DOMBuilder* parser=checkoutBuilder();
     XercesJanitor<DOMBuilder> janitor(parser);
     try {
         DOMDocument* doc=parser->parse(domsrc);
         parser->setFeature(XMLUni::fgXercesUserAdoptsDOMDocument,true);
+#endif
         checkinBuilder(janitor.release());
         return doc;
     }
@@ -91,7 +106,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 +156,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 +192,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,15 +214,27 @@ 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;
     }
 
     return true;
 }
 
-DOMInputSource* ParserPool::resolveEntity(const XMLCh* const publicId, const XMLCh* const systemId, const XMLCh* const baseURI)
+#ifdef XMLTOOLING_XERCESC_COMPLIANT_DOMLS
+DOMLSInput* ParserPool::resolveResource(
+            const XMLCh *const resourceType,
+            const XMLCh *const namespaceUri,
+            const XMLCh *const publicId,
+            const XMLCh *const systemId,
+            const XMLCh *const baseURI
+            )
+#else
+DOMInputSource* ParserPool::resolveEntity(
+    const XMLCh* const publicId, const XMLCh* const systemId, const XMLCh* const baseURI
+    )
+#endif
 {
 #if _DEBUG
     xmltooling::NDC ndc("resolveEntity");
@@ -279,24 +307,28 @@ 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()
+            log.errorStream() << "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"));
 }
 
+#ifdef XMLTOOLING_XERCESC_COMPLIANT_DOMLS
+
+#else
+
 DOMBuilder* ParserPool::createBuilder()
 {
     static const XMLCh impltype[] = { chLatin_L, chLatin_S, chNull };
@@ -319,6 +351,7 @@ DOMBuilder* ParserPool::createBuilder()
         parser->setProperty(XMLUni::fgXercesSchemaExternalSchemaLocation,const_cast<XMLCh*>(temp.get()));
 #endif
     }
+    parser->setProperty(XMLUni::fgXercesSecurityManager, m_security);
     parser->setFeature(XMLUni::fgXercesUserAdoptsDOMDocument,true);
     parser->setEntityResolver(this);
     parser->setErrorHandler(this);
@@ -353,10 +386,12 @@ void ParserPool::checkinBuilder(DOMBuilder* builder)
     }
 }
 
-unsigned int StreamInputSource::StreamBinInputStream::readBytes(XMLByte* const toFill, const unsigned int maxToRead)
+#endif
+
+xsecsize_t StreamInputSource::StreamBinInputStream::readBytes(XMLByte* const toFill, const xsecsize_t maxToRead)
 {
     XMLByte* target=toFill;
-    unsigned int bytes_read=0,request=maxToRead;
+    xsecsize_t bytes_read=0,request=maxToRead;
 
     // Fulfill the rest by reading from the stream.
     if (request && !m_is.eof() && !m_is.fail()) {
@@ -368,7 +403,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;
         }