From c435af531f8de0446bcf71b359ab90caf813f34a Mon Sep 17 00:00:00 2001 From: cantor Date: Tue, 29 Jan 2008 22:03:07 +0000 Subject: [PATCH] Path resolver class for supporting installation-independent relative paths. git-svn-id: https://svn.middleware.georgetown.edu/cpp-xmltooling/trunk@459 de75baf8-a10c-0410-a50a-987c0e22f00f --- xmltooling/Makefile.am | 2 + xmltooling/XMLToolingConfig.cpp | 11 +++++ xmltooling/XMLToolingConfig.h | 29 +++++++++++-- xmltooling/util/PathResolver.cpp | 73 +++++++++++++++++++++++++++++++ xmltooling/util/PathResolver.h | 91 +++++++++++++++++++++++++++++++++++++++ xmltooling/xmltooling-lite.vcproj | 8 ++++ xmltooling/xmltooling.vcproj | 8 ++++ 7 files changed, 219 insertions(+), 3 deletions(-) create mode 100644 xmltooling/util/PathResolver.cpp create mode 100644 xmltooling/util/PathResolver.h diff --git a/xmltooling/Makefile.am b/xmltooling/Makefile.am index efb133f..d61b2ec 100644 --- a/xmltooling/Makefile.am +++ b/xmltooling/Makefile.am @@ -103,6 +103,7 @@ utilinclude_HEADERS = \ util/DateTime.h \ util/NDC.h \ util/ParserPool.h \ + util/PathResolver.h \ util/Predicates.h \ util/ReloadableXMLFile.h \ util/ReplayCache.h \ @@ -181,6 +182,7 @@ common_sources = \ util/DateTime.cpp \ util/NDC.cpp \ util/ParserPool.cpp \ + util/PathResolver.cpp \ util/ReloadableXMLFile.cpp \ util/TemplateEngine.cpp \ util/URLEncoder.cpp \ diff --git a/xmltooling/XMLToolingConfig.cpp b/xmltooling/XMLToolingConfig.cpp index 2c06eac..f2671c7 100644 --- a/xmltooling/XMLToolingConfig.cpp +++ b/xmltooling/XMLToolingConfig.cpp @@ -37,6 +37,7 @@ #include "soap/SOAP.h" #include "soap/SOAPTransport.h" #include "util/NDC.h" +#include "util/PathResolver.h" #include "util/ReplayCache.h" #include "util/StorageService.h" #include "util/TemplateEngine.h" @@ -185,6 +186,12 @@ void XMLToolingConfig::setReplayCache(ReplayCache* replayCache) } #endif +void XMLToolingConfig::setPathResolver(PathResolver* pathResolver) +{ + delete m_pathResolver; + m_pathResolver = pathResolver; +} + void XMLToolingConfig::setTemplateEngine(TemplateEngine* templateEngine) { delete m_templateEngine; @@ -274,6 +281,7 @@ bool XMLToolingInternalConfig::init() m_keyInfoResolver = KeyInfoResolverManager.newPlugin(INLINE_KEYINFO_RESOLVER,NULL); #endif + m_pathResolver = new PathResolver(); m_urlEncoder = new URLEncoder(); // Register xml:id as an ID attribute. @@ -331,6 +339,9 @@ void XMLToolingInternalConfig::term() m_replayCache = NULL; #endif + delete m_pathResolver; + m_pathResolver = NULL; + delete m_templateEngine; m_templateEngine = NULL; diff --git a/xmltooling/XMLToolingConfig.h b/xmltooling/XMLToolingConfig.h index bd49bc5..0f8ae43 100644 --- a/xmltooling/XMLToolingConfig.h +++ b/xmltooling/XMLToolingConfig.h @@ -44,6 +44,7 @@ namespace xmltooling { namespace xmltooling { + class XMLTOOL_API PathResolver; class XMLTOOL_API TemplateEngine; class XMLTOOL_API URLEncoder; #ifndef XMLTOOLING_LITE @@ -63,7 +64,8 @@ namespace xmltooling { MAKE_NONCOPYABLE(XMLToolingConfig); protected: #ifndef XMLTOOLING_NO_XMLSEC - XMLToolingConfig() : m_keyInfoResolver(NULL), m_replayCache(NULL), m_templateEngine(NULL), m_urlEncoder(NULL), clock_skew_secs(180) {} + XMLToolingConfig() : m_keyInfoResolver(NULL), m_replayCache(NULL), + m_pathResolver(NULL), m_templateEngine(NULL), m_urlEncoder(NULL), clock_skew_secs(180) {} /** Global KeyInfoResolver instance. */ KeyInfoResolver* m_keyInfoResolver; @@ -71,9 +73,12 @@ namespace xmltooling { /** Global ReplayCache instance. */ ReplayCache* m_replayCache; #else - XMLToolingConfig() : m_templateEngine(NULL), m_urlEncoder(NULL), clock_skew_secs(180) {} + XMLToolingConfig() : m_pathResolver(NULL), m_templateEngine(NULL), m_urlEncoder(NULL), clock_skew_secs(180) {} #endif + /** Global PathResolver instance. */ + PathResolver* m_pathResolver; + /** Global TemplateEngine instance. */ TemplateEngine* m_templateEngine; @@ -225,7 +230,25 @@ namespace xmltooling { TemplateEngine* getTemplateEngine() const { return m_templateEngine; } - + + /** + * Sets the global PathResolver instance. + * This method must be externally synchronized with any code that uses the object. + * Any previously set object is destroyed. + * + * @param pathResolver new PathResolver instance to store + */ + void setPathResolver(PathResolver* pathResolver); + + /** + * Returns the global PathResolver instance. + * + * @return global PathResolver or NULL + */ + PathResolver* getPathResolver() const { + return m_pathResolver; + } + /** * List of catalog files to load into validating parser pool at initialization time. * Like other path settings, the separator depends on the platform diff --git a/xmltooling/util/PathResolver.cpp b/xmltooling/util/PathResolver.cpp new file mode 100644 index 0000000..2d521bb --- /dev/null +++ b/xmltooling/util/PathResolver.cpp @@ -0,0 +1,73 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * 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. + */ + +/** + * PathResolver.cpp + * + * Resolves local filenames into absolute pathnames. + */ + +#include "internal.h" +#include "exceptions.h" +#include "util/PathResolver.h" + +using namespace xmltooling; +using namespace std; + +const string& PathResolver::resolve(string& s, file_type_t filetype, const char* pkgname, const char* prefix) const +{ +#ifdef WIN32 + static const char sep = '\\'; +#else + static const char sep = '/'; +#endif + if (!isAbsolute(s.c_str())) { + switch (filetype) { + case XMLTOOLING_LIB_FILE: + s = string(prefix ? prefix : m_defaultPrefix) + sep + "lib" + sep + (pkgname ? pkgname : m_defaultPackage) + sep + s; + break; + + case XMLTOOLING_LOG_FILE: + if (prefix || m_defaultPrefix != "/usr") + s = string(prefix ? prefix : m_defaultPrefix) + sep + "var" + sep + "log" + sep + (pkgname ? pkgname : m_defaultPackage) + sep + s; + else + s = string(sep,1) + "var" + sep + "log" + sep + (pkgname ? pkgname : m_defaultPackage) + sep + s; + break; + + case XMLTOOLING_XML_FILE: + s = string(prefix ? prefix : m_defaultPrefix) + sep + "share" + sep + "xml" + (pkgname ? pkgname : m_defaultPackage) + sep + s; + break; + + case XMLTOOLING_RUN_FILE: + if (prefix || m_defaultPrefix != "/usr") + s = string(prefix ? prefix : m_defaultPrefix) + sep + "var" + sep + "run" + sep + (pkgname ? pkgname : m_defaultPackage) + sep + s; + else + s = string(sep,1) + "var" + sep + "run" + sep + (pkgname ? pkgname : m_defaultPackage) + sep + s; + break; + + case XMLTOOLING_CFG_FILE: + if (prefix || m_defaultPrefix != "/usr") + s = string(prefix ? prefix : m_defaultPrefix) + sep + "etc" + sep + (pkgname ? pkgname : m_defaultPackage) + sep + s; + else + s = string(sep,1) + "etc" + sep + (pkgname ? pkgname : m_defaultPackage) + sep + s; + break; + + default: + throw XMLToolingException("Unknown file type to resolve."); + } + } + return s; +} diff --git a/xmltooling/util/PathResolver.h b/xmltooling/util/PathResolver.h new file mode 100644 index 0000000..c8ec580 --- /dev/null +++ b/xmltooling/util/PathResolver.h @@ -0,0 +1,91 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * 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. + */ + +/** + * @file xmltooling/util/PathResolver.h + * + * Resolves local filenames into absolute pathnames. + */ + +#if !defined(__xmltooling_pathres_h__) +#define __xmltooling_pathres_h__ + +#include + +#include + +namespace xmltooling { + + /** + * Resolves local filenames into absolute pathnames. + */ + class XMLTOOL_API PathResolver + { + MAKE_NONCOPYABLE(PathResolver); + public: + PathResolver() {} + + virtual ~PathResolver() {} + + /** Types of file resources to resolve. */ + enum file_type_t { + XMLTOOLING_LIB_FILE, + XMLTOOLING_LOG_FILE, + XMLTOOLING_XML_FILE, + XMLTOOLING_RUN_FILE, + XMLTOOLING_CFG_FILE + }; + + /** + * Set the default package to use when resolving files. + * + * @param pkgname name of default package to use + */ + virtual void setDefaultPackageName(const char* pkgname) { + m_defaultPackage = pkgname; + } + + /** + * Set the default istallation prefix to use when resolving files. + * + * @param prefix name of default prefix to use + */ + virtual void setDefaultPrefix(const char* prefix) { + m_defaultPrefix = prefix; + } + + /** + * Changes the input filename into an absolute pathname to the same file. + * + * @param s filename to resolve + * @param filetype type of file being resolved + * @param pkgname application package name to use in resolving the file (or NULL for the default) + * @param prefix installation prefix to use in resolving the file (or NULL for the default) + * + * @return a const reference to the input string + */ + virtual const std::string& resolve(std::string& s, file_type_t filetype, const char* pkgname=NULL, const char* prefix=NULL) const; + + private: + bool isAbsolute(const char* s) const { + return (*s == '/' || *s == '\\' || *(s+1) == ':'); + } + + std::string m_defaultPackage,m_defaultPrefix; + }; +}; + +#endif /* __xmltooling_pathres_h__ */ diff --git a/xmltooling/xmltooling-lite.vcproj b/xmltooling/xmltooling-lite.vcproj index 489992b..a13eaf8 100644 --- a/xmltooling/xmltooling-lite.vcproj +++ b/xmltooling/xmltooling-lite.vcproj @@ -244,6 +244,10 @@ > + + @@ -466,6 +470,10 @@ > + + diff --git a/xmltooling/xmltooling.vcproj b/xmltooling/xmltooling.vcproj index 9b148c4..04c8bbc 100644 --- a/xmltooling/xmltooling.vcproj +++ b/xmltooling/xmltooling.vcproj @@ -251,6 +251,10 @@ > + + @@ -581,6 +585,10 @@ > + + -- 2.1.4