Path resolver class for supporting installation-independent relative paths.
[shibboleth/xmltooling.git] / xmltooling / util / PathResolver.h
1 /*
2  *  Copyright 2001-2007 Internet2
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 /**
18  * @file xmltooling/util/PathResolver.h
19  * 
20  * Resolves local filenames into absolute pathnames.
21  */
22
23 #if !defined(__xmltooling_pathres_h__)
24 #define __xmltooling_pathres_h__
25
26 #include <xmltooling/base.h>
27
28 #include <string>
29
30 namespace xmltooling {
31
32     /**
33      * Resolves local filenames into absolute pathnames.
34      */
35     class XMLTOOL_API PathResolver
36     {
37         MAKE_NONCOPYABLE(PathResolver);
38     public:
39         PathResolver() {}
40
41         virtual ~PathResolver() {}
42         
43         /** Types of file resources to resolve. */
44         enum file_type_t {
45             XMLTOOLING_LIB_FILE,
46             XMLTOOLING_LOG_FILE,
47             XMLTOOLING_XML_FILE,
48             XMLTOOLING_RUN_FILE,
49             XMLTOOLING_CFG_FILE
50         };
51         
52         /**
53          * Set the default package to use when resolving files.
54          *
55          * @param pkgname name of default package to use 
56          */
57         virtual void setDefaultPackageName(const char* pkgname) {
58             m_defaultPackage = pkgname;
59         }
60
61         /**
62          * Set the default istallation prefix to use when resolving files.
63          *
64          * @param prefix name of default prefix to use 
65          */
66         virtual void setDefaultPrefix(const char* prefix) {
67             m_defaultPrefix = prefix;
68         }
69         
70         /**
71          * Changes the input filename into an absolute pathname to the same file.
72          * 
73          * @param s         filename to resolve
74          * @param filetype  type of file being resolved
75          * @param pkgname   application package name to use in resolving the file (or NULL for the default)
76          * @param prefix    installation prefix to use in resolving the file (or NULL for the default)
77          * 
78          * @return a const reference to the input string
79          */
80         virtual const std::string& resolve(std::string& s, file_type_t filetype, const char* pkgname=NULL, const char* prefix=NULL) const;
81
82     private:
83         bool isAbsolute(const char* s) const {
84             return (*s == '/' || *s == '\\' || *(s+1) == ':');
85         }
86
87         std::string m_defaultPackage,m_defaultPrefix;
88     };
89 };
90
91 #endif /* __xmltooling_pathres_h__ */