1 /*
2  * Copyright 2006-2011 The FLWOR Foundation.
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 #pragma once
17 #ifndef ZORBA_MODULE_VERSION_H
18 #define ZORBA_MODULE_VERSION_H
19 
20 #include <zorbatypes/zstring.h>
21 
22 namespace zorba {
23 
24 /**
25  * The ModuleVersion class is a pair of a module namespace URI and a version
26  * definition. A definition is one of the following:
27  *
28  *  A.B[.p] - represents major version A and minor version *at least* B
29  *  A.B[.p]! - represents exactly major version A and minor version B, no other
30  *  A.B[.p]-C.0 - represents any major version between A and C; if it is A, then
31  *            the minor version must be at least B
32  *
33  * In all of the above, [.p] represents an optional patch version number. If
34  * patch is not specified, it is assumed to be 0.
35  *
36  * A ModuleVersion may represent an explicit version (eg., the version of a
37  * module which has actually been imported), or a requested version range (eg.,
38  * a URI from an "import module" statement).
39  */
40 class ModuleVersion {
41 public:
42   /**
43    * Construct a ModuleVersion from a string of the form "uri#ver"; that is, a
44    * URI with the fragment being a version definition. If the URI does not have
45    * a valid version definition, "#0.0.0-1000000.0.0" will be assumed - that is,
46    * any available version. (In that case, namespace_uri() will return the
47    * complete original URI passed to the constructor.)
48    */
49   ModuleVersion(zstring const& aUri);
50 
51   /**
52    * Construct a ModuleVersion from a URI (presumed to not have a version
53    * definition fragment) and a separate version definition string.
54    */
55   ModuleVersion(zstring const& aUri, zstring const& aVersionDef);
56 
57   /**
58    * Returns whether the given ModuleVersion is "satisfied" by this
59    * ModuleVersion, which means:
60    *   1. The namespace URIs must be equal.
61    *   2. The minimum major version of this ModuleVersion must be in the
62    *      range [min_major, max_major] of the given ModuleVersion.
63    *   3. If the minimum major of this ModuleVersion is equal to the given
64    *      ModuleVersion's min_major, then the minimum minor version of this
65    *      ModuleVersion must be in the range [min_minor, infinity), and the
66    *      minimum patch version of this ModuleVersion must be in the range
67    *      [min_patch, infinity).
68    *   4. If the given ModuleVersion is "exact", then this ModuleVersion must
69    *      have exactly the same min_major, max_major, and min_minor. If the
70    *      given ModuleVersion has a non-zero min_patch, then this ModuleVersion
71    *      must also have exactly the same min_patch.
72    */
73   bool satisfies(ModuleVersion const& aModuleVersion) const;
74 
75   /**
76    * Return the namespace URI.
77    */
namespace_uri()78   zstring const& namespace_uri() const {
79     return theNamespaceURI;
80   }
81 
82   /**
83    * Return the minimum major version that satisifies the version definition.
84    */
min_major()85   int min_major() const {
86     return theMinMajor;
87   }
88 
89   /**
90    * Return the maximum major version that satisifies the version definition.
91    */
max_major()92   int max_major() const {
93     return theMaxMajor;
94   }
95 
96   /**
97    * Return the minimum minor version (for the minimum major version) that
98    * satisfies the version definition.
99    */
min_minor()100   int min_minor() const {
101     return theMinMinor;
102   }
103 
104   /**
105    * Return the minimum patch version (for the minimum major version) that
106    * satisfies the version definition.
107    */
min_patch()108   int min_patch() const {
109     return theMinPatch;
110   }
111 
112   /**
113    * Returns whether this version definition is exact.
114    */
is_exact()115   bool is_exact() const {
116     return theIsExact;
117   }
118 
119   /**
120    * Returns whether this ModuleVersion was initialized with a valid version
121    * specification or not.
122    */
is_valid_version()123   bool is_valid_version() const {
124     return theValidVersion;
125   }
126 
127   /**
128    * Returns the full URI, complete with version definition.
129    */
versioned_uri()130   zstring const& versioned_uri() const {
131     return theVersionedURI;
132   }
133 
134   /**
135    * Returns the full versioned URI as a C string (so this class can be used as
136    * a key in a zorba::hashmap).
137    */
c_str()138   const char* c_str() const {
139     return theVersionedURI.c_str();
140   }
141 
142 private:
143 
144   bool initValues(zstring const& aVersionDef);
145 
146   int theMinMajor;
147   int theMaxMajor;
148   int theMinMinor;
149   int theMinPatch;
150   bool theIsExact;
151   bool theValidVersion;
152   zstring theNamespaceURI;
153   zstring theVersionedURI;
154 };
155 
156 } // namespace zorba
157 
158 #endif // ZORBA_MODULE_VERSION_H
159 /* vim:set et sw=2 ts=2: */
160