1 /**
2  * Licensed to the University Corporation for Advanced Internet
3  * Development, Inc. (UCAID) under one or more contributor license
4  * agreements. See the NOTICE file distributed with this work for
5  * additional information regarding copyright ownership.
6  *
7  * UCAID licenses this file to you under the Apache License,
8  * Version 2.0 (the "License"); you may not use this file except
9  * in compliance with the License. You may obtain a copy of the
10  * License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing,
15  * software distributed under the License is distributed on an
16  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
17  * either express or implied. See the License for the specific
18  * language governing permissions and limitations under the License.
19  */
20 
21 /**
22  * @file xmltooling/util/PathResolver.h
23  *
24  * Resolves local filenames into absolute pathnames.
25  */
26 
27 #if !defined(__xmltooling_pathres_h__)
28 #define __xmltooling_pathres_h__
29 
30 #include <xmltooling/base.h>
31 
32 #include <string>
33 
34 namespace xmltooling {
35 
36 #if defined (_MSC_VER)
37 #    pragma warning( push )
38 #    pragma warning( disable : 4251 )
39 #endif
40 
41     /**
42      * Resolves local filenames into absolute pathnames.
43      */
44     class XMLTOOL_API PathResolver
45     {
46         MAKE_NONCOPYABLE(PathResolver);
47     public:
48         PathResolver();
49         virtual ~PathResolver();
50 
51         /** Types of file resources to resolve. */
52         enum file_type_t {
53             XMLTOOLING_LIB_FILE,
54             XMLTOOLING_LOG_FILE,
55             XMLTOOLING_XML_FILE,
56             XMLTOOLING_RUN_FILE,
57             XMLTOOLING_CFG_FILE,
58             XMLTOOLING_CACHE_FILE
59         };
60 
61         /**
62          * Set the default package to use when resolving files.
63          *
64          * @param pkgname name of default package to use
65          */
66         virtual void setDefaultPackageName(const char* pkgname);
67 
68         /**
69          * Set the default installation prefix to use when resolving files.
70          *
71          * @param prefix name of default prefix to use
72          */
73         virtual void setDefaultPrefix(const char* prefix);
74 
75         /**
76          * Set the lib directory to use when resolving files.
77          * <p>If relative, the default prefix will be prepended.
78          *
79          * @param dir    the library directory to use
80          */
81         virtual void setLibDir(const char* dir);
82 
83         /**
84          * Set the log directory to use when resolving files.
85          * <p>If relative, the default prefix will be prepended.
86          *
87          * @param dir    the log directory to use
88          */
89         virtual void setLogDir(const char* dir);
90 
91         /**
92          * Set the XML directory to use when resolving files.
93          * <p>If relative, the default prefix will be prepended.
94          *
95          * @param dir    the XML directory to use
96          */
97         virtual void setXMLDir(const char* dir);
98 
99         /**
100          * Set the run directory to use when resolving files.
101          * <p>If relative, the default prefix will be prepended.
102          *
103          * @param dir    the run directory to use
104          */
105         virtual void setRunDir(const char* dir);
106 
107         /**
108          * Set the config directory to use when resolving files.
109          * <p>If relative, the default prefix will be prepended.
110          *
111          * @param dir    the config directory to use
112          */
113         virtual void setCfgDir(const char* dir);
114 
115         /**
116          * Set the cache directory to use when resolving files.
117          * <p>If relative, the default prefix will be prepended.
118          *
119          * @param dir    the cache directory to use
120          */
121         virtual void setCacheDir(const char* dir);
122 
123         /**
124          * Changes the input filename into an absolute pathname to the same file.
125          *
126          * @param s         filename to resolve
127          * @param filetype  type of file being resolved
128          * @param pkgname   application package name to use in resolving the file (or nullptr for the default)
129          * @param prefix    installation prefix to use in resolving the file (or nullptr for the default)
130          *
131          * @return a const reference to the input string
132          */
133         virtual const std::string& resolve(std::string& s, file_type_t filetype, const char* pkgname=nullptr, const char* prefix=nullptr) const;
134 
135     private:
136         bool isAbsolute(const char* s) const;
137 
138         std::string m_defaultPackage,m_defaultPrefix,m_lib,m_log,m_xml,m_run,m_cfg,m_cache;
139     };
140 
141 #if defined (_MSC_VER)
142 #   pragma warning( pop )
143 #endif
144 };
145 
146 #endif /* __xmltooling_pathres_h__ */
147