From a93d0241cb0fcbbd03ec671ec92ace2cd1519af9 Mon Sep 17 00:00:00 2001 From: Scott Cantor Date: Sun, 4 Dec 2011 06:07:45 +0000 Subject: [PATCH] More boostisms, add string tokenizer --- configure.ac | 1 + xmltooling/XMLToolingConfig.cpp | 8 ++--- xmltooling/io/HTTPRequest.cpp | 5 ++-- xmltooling/util/ParserPool.cpp | 60 ++++++++++++++++++++------------------ xmltooling/util/ParserPool.h | 4 +-- xmltooling/util/TemplateEngine.cpp | 27 +++++------------ xmltooling/util/TemplateEngine.h | 1 - 7 files changed, 49 insertions(+), 57 deletions(-) diff --git a/configure.ac b/configure.ac index 0f257ac..0a47407 100644 --- a/configure.ac +++ b/configure.ac @@ -100,6 +100,7 @@ BOOST_CONVERSION BOOST_LAMBDA BOOST_PTR_CONTAINER BOOST_STRING_ALGO +BOOST_TOKENIZER # are covariant methods allowed? AC_LINK_IFELSE( diff --git a/xmltooling/XMLToolingConfig.cpp b/xmltooling/XMLToolingConfig.cpp index 9f8478e..bcd6eba 100644 --- a/xmltooling/XMLToolingConfig.cpp +++ b/xmltooling/XMLToolingConfig.cpp @@ -55,9 +55,9 @@ #endif #include -#include #include #include +#include #if defined(XMLTOOLING_LOG4SHIB) # include @@ -425,12 +425,12 @@ bool XMLToolingInternalConfig::init() // Load catalogs from path. if (!catalog_path.empty()) { - vector catpaths; - split(catpaths, catalog_path, is_any_of(PATH_SEPARATOR_STR), algorithm::token_compress_on); + boost::tokenizer< char_separator > catpaths(catalog_path, char_separator(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(&ParserPool::loadCatalog), m_validatingPool, boost::bind(&string::c_str,_1)) + boost::bind(static_cast(&ParserPool::loadCatalog), + m_validatingPool, boost::bind(&string::c_str,_1)) ); } diff --git a/xmltooling/io/HTTPRequest.cpp b/xmltooling/io/HTTPRequest.cpp index 10bc323..af790e9 100644 --- a/xmltooling/io/HTTPRequest.cpp +++ b/xmltooling/io/HTTPRequest.cpp @@ -29,6 +29,7 @@ #include #include +#include using namespace xmltooling; using namespace boost; @@ -70,8 +71,8 @@ const char* HTTPRequest::getCookie(const char* name) const { if (m_cookieMap.empty()) { string cookies=getHeader("Cookie"); - vector nvpairs, nvpair; - split(nvpairs, cookies, is_any_of(";"), algorithm::token_compress_on); + vector nvpair; + tokenizer< char_separator > nvpairs(cookies, char_separator(";")); for_each(nvpairs.begin(), nvpairs.end(), boost::bind(handle_cookie_fn, boost::ref(m_cookieMap), boost::ref(nvpair), _1)); } map::const_iterator lookup=m_cookieMap.find(name); diff --git a/xmltooling/util/ParserPool.cpp b/xmltooling/util/ParserPool.cpp index 72156c0..6a4700d 100644 --- a/xmltooling/util/ParserPool.cpp +++ b/xmltooling/util/ParserPool.cpp @@ -33,10 +33,12 @@ #include "util/Threads.h" #include "util/XMLHelper.h" -#include -#include #include #include +#include +#include +#include +#include #include #include #include @@ -47,6 +49,7 @@ using namespace xmltooling::logging; using namespace xmltooling; using namespace xercesc; +using namespace boost; using namespace std; @@ -108,8 +111,6 @@ ParserPool::~ParserPool() m_pool.top()->release(); m_pool.pop(); } - delete m_lock; - delete m_security; } DOMDocument* ParserPool::newDocument() @@ -197,8 +198,7 @@ DOMDocument* ParserPool::parse(istream& is) } // Functor to double its argument separated by a character and append to a buffer -template class doubleit -{ +template class doubleit { public: doubleit(T& t, const typename T::value_type& s) : temp(t), sep(s) {} void operator() (const pair& s) { temp += s.first + sep + s.first + sep; } @@ -228,10 +228,10 @@ bool ParserPool::loadSchema(const XMLCh* nsURI, const XMLCh* pathname) return false; } - Lock lock(m_lock); + Lock lock(m_lock.get()); m_schemaLocMap[nsURI]=pathname; m_schemaLocations.erase(); - for_each(m_schemaLocMap.begin(),m_schemaLocMap.end(),doubleit(m_schemaLocations,chSpace)); + for_each(m_schemaLocMap.begin(), m_schemaLocMap.end(), doubleit(m_schemaLocations,chSpace)); return true; } @@ -288,7 +288,7 @@ bool ParserPool::loadCatalog(const XMLCh* pathname) // Fetch all the elements. DOMNodeList* mappings=root->getElementsByTagNameNS(CATALOG_NS,system); - Lock lock(m_lock); + Lock lock(m_lock.get()); for (XMLSize_t i=0; igetLength(); i++) { root=static_cast(mappings->item(i)); const XMLCh* from=root->getAttributeNS(nullptr,systemId); @@ -296,9 +296,9 @@ bool ParserPool::loadCatalog(const XMLCh* pathname) m_schemaLocMap[from]=to; } m_schemaLocations.erase(); - for_each(m_schemaLocMap.begin(),m_schemaLocMap.end(),doubleit(m_schemaLocations,chSpace)); + for_each(m_schemaLocMap.begin(), m_schemaLocMap.end(), doubleit(m_schemaLocations,chSpace)); } - catch (exception& e) { + catch (std::exception& e) { log.error("catalog loader caught exception: %s", e.what()); return false; } @@ -334,25 +334,27 @@ DOMInputSource* ParserPool::resolveEntity( } // 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(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::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 suffix of a value in the map. + i = find_if( + m_schemaLocMap.begin(), m_schemaLocMap.end(), + boost::bind(ends_with, boost::bind(&map::value_type::second, _1), systemId) + ); + 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 @@ -376,13 +378,13 @@ DOMLSParser* ParserPool::createBuilder() parser->getDomConfig()->setParameter(XMLUni::fgXercesUserAdoptsDOMDocument, true); parser->getDomConfig()->setParameter(XMLUni::fgXercesDisableDefaultEntityResolution, true); parser->getDomConfig()->setParameter(XMLUni::fgDOMResourceResolver, dynamic_cast(this)); - parser->getDomConfig()->setParameter(XMLUni::fgXercesSecurityManager, m_security); + parser->getDomConfig()->setParameter(XMLUni::fgXercesSecurityManager, m_security.get()); return parser; } DOMLSParser* ParserPool::checkoutBuilder() { - Lock lock(m_lock); + Lock lock(m_lock.get()); if (m_pool.empty()) { DOMLSParser* builder=createBuilder(); return builder; @@ -397,7 +399,7 @@ DOMLSParser* ParserPool::checkoutBuilder() void ParserPool::checkinBuilder(DOMLSParser* builder) { if (builder) { - Lock lock(m_lock); + Lock lock(m_lock.get()); m_pool.push(builder); } } @@ -420,7 +422,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(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); @@ -429,7 +431,7 @@ DOMBuilder* ParserPool::createBuilder() DOMBuilder* ParserPool::checkoutBuilder() { - Lock lock(m_lock); + Lock lock(m_lock.get()); if (m_pool.empty()) { DOMBuilder* builder=createBuilder(); return builder; @@ -444,7 +446,7 @@ DOMBuilder* ParserPool::checkoutBuilder() void ParserPool::checkinBuilder(DOMBuilder* builder) { if (builder) { - Lock lock(m_lock); + Lock lock(m_lock.get()); m_pool.push(builder); } } diff --git a/xmltooling/util/ParserPool.h b/xmltooling/util/ParserPool.h index 8dee873..6561329 100644 --- a/xmltooling/util/ParserPool.h +++ b/xmltooling/util/ParserPool.h @@ -176,8 +176,8 @@ namespace xmltooling { #else std::stack m_pool; #endif - Mutex* m_lock; - xercesc::SecurityManager* m_security; + std::auto_ptr m_lock; + std::auto_ptr m_security; }; /** diff --git a/xmltooling/util/TemplateEngine.cpp b/xmltooling/util/TemplateEngine.cpp index 90173c1..ca9ca71 100644 --- a/xmltooling/util/TemplateEngine.cpp +++ b/xmltooling/util/TemplateEngine.cpp @@ -28,8 +28,11 @@ #include "io/GenericRequest.h" #include "util/TemplateEngine.h" +#include + using namespace xmltooling; using namespace std; +using boost::trim; namespace { static const pair emptyPair; @@ -112,27 +115,13 @@ void TemplateEngine::html_encode(ostream& os, const char* start) const } } -void TemplateEngine::trimspace(string& s) const -{ - string::size_type end = s.size() - 1, start = 0; - - // Trim stuff on right. - while (end > 0 && !isgraph(s[end])) end--; - - // Trim stuff on left. - while (start < end && !isgraph(s[start])) start++; - - // Modify the string. - s = s.substr(start, end - start + 1); -} - void TemplateEngine::process( bool visible, const string& buf, const char*& lastpos, ostream& os, const TemplateParameters& parameters, - const std::pair& loopentry, + const pair& loopentry, const XMLToolingException* e ) const { @@ -157,7 +146,7 @@ void TemplateEngine::process( // search for the end-tag if ((thispos = strstr(lastpos, "/>")) != nullptr) { string key = buf.substr(lastpos-line, thispos-lastpos); - trimspace(key); + trim(key); if (key == "$name" && !loopentry.first.empty()) html_encode(os,loopentry.first.c_str()); @@ -185,7 +174,7 @@ void TemplateEngine::process( // search for the end of this tag if ((thispos = strchr(lastpos, '>')) != nullptr) { string key = buf.substr(lastpos-line, thispos-lastpos); - trimspace(key); + trim(key); bool cond=false; if (visible) cond = parameters.getParameter(key.c_str()) || (e && e->getProperty(key.c_str())); @@ -215,7 +204,7 @@ void TemplateEngine::process( // search for the end of this tag if ((thispos = strchr(lastpos, '>')) != nullptr) { string key = buf.substr(lastpos-line, thispos-lastpos); - trimspace(key); + trim(key); bool cond=visible; if (visible) cond = !(parameters.getParameter(key.c_str()) || (e && e->getProperty(key.c_str()))); @@ -248,7 +237,7 @@ void TemplateEngine::process( // search for the end of this tag if ((thispos = strchr(lastpos, '>')) != nullptr) { key = buf.substr(lastpos-line, thispos-lastpos); - trimspace(key); + trim(key); lastpos = thispos + 1; // strlen(">") } diff --git a/xmltooling/util/TemplateEngine.h b/xmltooling/util/TemplateEngine.h index 05960c6..4a86c86 100644 --- a/xmltooling/util/TemplateEngine.h +++ b/xmltooling/util/TemplateEngine.h @@ -131,7 +131,6 @@ namespace xmltooling { static std::string unsafe_chars; private: - void trimspace(std::string& s) const; void html_encode(std::ostream& os, const char* start) const; void process( bool visible, -- 2.1.4