4f12ee8c0985addaaca6143342c174bcbd40da0c
[shibboleth/cpp-xmltooling.git] / xmltooling / util / PathResolver.h
1 /*
2  *  Copyright 2001-2009 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      * Resolves local filenames into absolute pathnames.
33      */
34     class XMLTOOL_API PathResolver
35     {
36         MAKE_NONCOPYABLE(PathResolver);
37     public:
38         PathResolver();
39         virtual ~PathResolver() {}
40
41         /** Types of file resources to resolve. */
42         enum file_type_t {
43             XMLTOOLING_LIB_FILE,
44             XMLTOOLING_LOG_FILE,
45             XMLTOOLING_XML_FILE,
46             XMLTOOLING_RUN_FILE,
47             XMLTOOLING_CFG_FILE
48         };
49
50         /**
51          * Set the default package to use when resolving files.
52          *
53          * @param pkgname name of default package to use
54          */
55         virtual void setDefaultPackageName(const char* pkgname) {
56             m_defaultPackage = pkgname;
57         }
58
59         /**
60          * Set the default installation prefix to use when resolving files.
61          *
62          * @param prefix name of default prefix to use
63          */
64         virtual void setDefaultPrefix(const char* prefix) {
65             m_defaultPrefix = prefix;
66         }
67
68         /**
69          * Set the lib directory to use when resolving files.
70          * <p>If relative, the default prefix will be prepended.
71          *
72          * @param dir    the library directory to use
73          */
74         virtual void setLibDir(const char* dir) {
75             m_lib = dir;
76         }
77
78         /**
79          * Set the log directory to use when resolving files.
80          * <p>If relative, the default prefix will be prepended.
81          *
82          * @param dir    the log directory to use
83          */
84         virtual void setLogDir(const char* dir) {
85             m_log = dir;
86         }
87
88         /**
89          * Set the XML directory to use when resolving files.
90          * <p>If relative, the default prefix will be prepended.
91          *
92          * @param dir    the XML directory to use
93          */
94         virtual void setXMLDir(const char* dir) {
95             m_xml = dir;
96         }
97
98         /**
99          * Set the run directory to use when resolving files.
100          * <p>If relative, the default prefix will be prepended.
101          *
102          * @param dir    the run directory to use
103          */
104         virtual void setRunDir(const char* dir) {
105             m_run = dir;
106         }
107
108         /**
109          * Set the config directory to use when resolving files.
110          * <p>If relative, the default prefix will be prepended.
111          *
112          * @param dir    the config directory to use
113          */
114         virtual void setCfgDir(const char* dir) {
115             m_cfg = dir;
116         }
117
118         /**
119          * Changes the input filename into an absolute pathname to the same file.
120          *
121          * @param s         filename to resolve
122          * @param filetype  type of file being resolved
123          * @param pkgname   application package name to use in resolving the file (or NULL for the default)
124          * @param prefix    installation prefix to use in resolving the file (or NULL for the default)
125          *
126          * @return a const reference to the input string
127          */
128         virtual const std::string& resolve(std::string& s, file_type_t filetype, const char* pkgname=NULL, const char* prefix=NULL) const;
129
130     private:
131         bool isAbsolute(const char* s) const {
132             switch (*s) {
133                 case '/':
134                 case '\\':
135                     return true;
136                 case '.':
137                     return (*(s+1) == '.' || *(s+1) == '/' || *(s+1) == '\\');
138             }
139             return *(s+1) == ':';
140         }
141
142         std::string m_defaultPackage,m_defaultPrefix,m_lib,m_log,m_xml,m_run,m_cfg;
143     };
144 };
145
146 #endif /* __xmltooling_pathres_h__ */