/*
- * 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.
#include "internal.h"
#include "exceptions.h"
+#include "logging.h"
#include "util/NDC.h"
#include "util/ParserPool.h"
#include "util/XMLHelper.h"
#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 std;
-using namespace log4cpp;
ParserPool::ParserPool(bool namespaceAware, bool schemaAware)
- : m_namespaceAware(namespaceAware), m_schemaAware(schemaAware), m_lock(XMLPlatformUtils::makeMutex()) {}
+ : m_namespaceAware(namespaceAware), m_schemaAware(schemaAware), m_lock(Mutex::create()) {}
ParserPool::~ParserPool()
{
m_pool.top()->release();
m_pool.pop();
}
- XMLPlatformUtils::closeMutex(m_lock);
+ delete m_lock;
}
DOMDocument* ParserPool::newDocument()
DOMDocument* ParserPool::parse(DOMInputSource& domsrc)
{
DOMBuilder* parser=checkoutBuilder();
+ XercesJanitor<DOMBuilder> janitor(parser);
try {
DOMDocument* doc=parser->parse(domsrc);
parser->setFeature(XMLUni::fgXercesUserAdoptsDOMDocument,true);
- checkinBuilder(parser);
+ checkinBuilder(janitor.release());
return doc;
}
- catch (...) {
- checkinBuilder(parser);
+ catch (XMLException&) {
+ checkinBuilder(janitor.release());
+ throw;
+ }
+ catch (XMLToolingException&) {
+ checkinBuilder(janitor.release());
throw;
}
}
{
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;
};
return false;
}
- XMLPlatformUtils::lockMutex(m_lock);
+ Lock lock(m_lock);
#ifdef HAVE_GOOD_STL
m_schemaLocMap[nsURI]=pathname;
m_schemaLocations.erase();
m_schemaLocations.erase();
for_each(m_schemaLocMap.begin(),m_schemaLocMap.end(),doubleit<string>(m_schemaLocations,' '));
#endif
- XMLPlatformUtils::unlockMutex(m_lock);
return true;
}
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,
Wrapper4InputSource domsrc(&fsrc,false);
try {
DOMDocument* doc=XMLToolingConfig::getConfig().getParser().parse(domsrc);
+ XercesJanitor<DOMDocument> janitor(doc);
// Check root element.
const DOMElement* root=doc->getDocumentElement();
if (!XMLHelper::isNodeNamed(root,CATALOG_NS,catalog)) {
auto_ptr_char temp(pathname);
log.error("unknown root element, failed to load XML catalog from %s", temp.get());
- doc->release();
return false;
}
- // Fetch all the <uri> elements.
- DOMNodeList* mappings=root->getElementsByTagNameNS(CATALOG_NS,uri);
- XMLPlatformUtils::lockMutex(m_lock);
+ // 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;
#else
for_each(m_schemaLocMap.begin(),m_schemaLocMap.end(),doubleit<string>(m_schemaLocations,' '));
#endif
- XMLPlatformUtils::unlockMutex(m_lock);
- doc->release();
}
- 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;
}
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());
- auto_ptr_XMLCh temp2(i->second.c_str());
- if (i!=m_schemaLocMap.end())
- return new Wrapper4InputSource(new LocalFileInputSource(NULL,temp2.get()));
+ if (i!=m_schemaLocMap.end()) {
+ auto_ptr_XMLCh temp2(i->second.c_str());
+ 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));
}
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"));
DOMBuilder* ParserPool::checkoutBuilder()
{
- XMLPlatformUtils::lockMutex(m_lock);
- try {
- if (m_pool.empty()) {
- DOMBuilder* builder=createBuilder();
- XMLPlatformUtils::unlockMutex(m_lock);
- return builder;
- }
- DOMBuilder* p=m_pool.top();
- m_pool.pop();
- if (m_schemaAware) {
+ Lock lock(m_lock);
+ if (m_pool.empty()) {
+ DOMBuilder* builder=createBuilder();
+ return builder;
+ }
+ DOMBuilder* p=m_pool.top();
+ m_pool.pop();
+ if (m_schemaAware) {
#ifdef HAVE_GOOD_STL
- p->setProperty(XMLUni::fgXercesSchemaExternalSchemaLocation,const_cast<XMLCh*>(m_schemaLocations.c_str()));
+ p->setProperty(XMLUni::fgXercesSchemaExternalSchemaLocation,const_cast<XMLCh*>(m_schemaLocations.c_str()));
#else
- auto_ptr_XMLCh temp2(m_schemaLocations.c_str());
- p->setProperty(XMLUni::fgXercesSchemaExternalSchemaLocation,const_cast<XMLCh*>(temp2.get()));
+ auto_ptr_XMLCh temp2(m_schemaLocations.c_str());
+ p->setProperty(XMLUni::fgXercesSchemaExternalSchemaLocation,const_cast<XMLCh*>(temp2.get()));
#endif
- }
- XMLPlatformUtils::unlockMutex(m_lock);
- return p;
- }
- catch(...) {
- XMLPlatformUtils::unlockMutex(m_lock);
- throw;
}
+ return p;
}
void ParserPool::checkinBuilder(DOMBuilder* builder)
{
if (builder) {
- XMLPlatformUtils::lockMutex(m_lock);
+ Lock lock(m_lock);
m_pool.push(builder);
- XMLPlatformUtils::unlockMutex(m_lock);
}
}
unsigned int bytes_read=0,request=maxToRead;
// Fulfill the rest by reading from the stream.
- if (request && !m_is.eof()) {
+ if (request && !m_is.eof() && !m_is.fail()) {
try {
m_is.read(reinterpret_cast<char* const>(target),request);
m_pos+=m_is.gcount();
bytes_read+=m_is.gcount();
}
- catch(...) {
- Category::getInstance(XMLTOOLING_LOGCAT".StreamInputSource").critStream() <<
- "XML::StreamInputSource::StreamBinInputStream::readBytes caught an exception" << CategoryStream::ENDLINE;
+ catch(ios_base::failure& e) {
+ Category::getInstance(XMLTOOLING_LOGCAT".StreamInputSource").critStream()
+ << "XML::StreamInputSource::StreamBinInputStream::readBytes caught an exception: " << e.what()
+ << logging::eol;
*toFill=0;
return 0;
}