de17cb074f961a4eea31c40589ec5393fe5f5038
[shibboleth/cpp-xmltooling.git] / xmltooling / Namespace.h
1 /*
2  *  Copyright 2001-2010 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/Namespace.h
19  * 
20  * Representing XML namespace attributes.
21  */
22
23 #if !defined(__xmltooling_namespace_h__)
24 #define __xmltooling_namespace_h__
25
26 #include <xmltooling/unicode.h>
27
28 namespace xmltooling {
29
30 #if defined (_MSC_VER)
31     #pragma warning( push )
32     #pragma warning( disable : 4251 )
33 #endif
34
35     /**
36      * A data structure for encapsulating XML Namespace attributes.
37      */
38     class XMLTOOL_API Namespace
39     {
40     public:
41         /**
42          * Tri-state indicator of namespace usage.
43          */
44         enum namespace_usage_t {
45             Indeterminate,
46             NonVisiblyUsed,
47             VisiblyUsed
48         };
49
50         /**
51          * Constructor
52          * @param uri               namespace URI
53          * @param prefix            namespace prefix (without the colon)
54          * @param alwaysDeclare     true iff the namespace should always be declared regardless of in-scope declarations
55          * @param usage             indicates usage of namespace in the context of an XMLObject
56          */
57         Namespace(const XMLCh* uri=nullptr, const XMLCh* prefix=nullptr, bool alwaysDeclare=false, namespace_usage_t usage=Indeterminate);
58         
59         ~Namespace();
60         
61         /**
62          * Returns the namespace prefix
63          * @return  Null-terminated Unicode string containing the prefix, without the colon
64          */
65         const XMLCh* getNamespacePrefix() const { return m_prefix.c_str(); }
66
67         /**
68          * Returns the namespace URI
69          * @return  Null-terminated Unicode string containing the URI
70          */
71         const XMLCh* getNamespaceURI() const { return m_uri.c_str(); }
72
73         /**
74          * Returns true iff the namespace should always be declared regardless of in-scope declarations
75          * @return the alwaysDeclared setting
76          */
77         const bool alwaysDeclare() const { return m_pinned; }
78
79         /**
80          * Returns the usage of the namespace by an XMLObject
81          * @return the usage setting
82          */
83         const namespace_usage_t usage() const { return m_usage; }
84
85         /**
86          * Sets the namespace prefix
87          * @param prefix    Null-terminated Unicode string containing the prefix, without the colon
88          */
89         void setNamespacePrefix(const XMLCh* prefix);
90
91         /**
92          * Sets the namespace URI
93          * @param uri  Null-terminated Unicode string containing the URI
94          */
95         void setNamespaceURI(const XMLCh* uri);
96
97         /**
98          * Sets the alwaysDeclared property
99          * @param alwaysDeclare     true iff the namespace should always be declared regardless of in-scope declarations
100          */
101         void setAlwaysDeclare(bool alwaysDeclare) { m_pinned = alwaysDeclare; } 
102         
103         /**
104          * Sets the usage property
105          * @param usage usage of the namespace by an XMLObject
106          */
107         void setUsage(namespace_usage_t usage) { m_usage = usage; }
108
109     private:
110         bool m_pinned;
111         namespace_usage_t m_usage;
112         xstring m_uri;
113         xstring m_prefix;
114     };
115
116 #if defined (_MSC_VER)
117     #pragma warning( pop )
118 #endif
119
120     /**
121      * Returns true iff op1's namespace lexically compares less than op2's namespace,
122      * or if equal, iff op1's prefix lexically compares less than op2's prefix.
123      * 
124      * Needed for use with sorted STL containers.
125      * 
126      * @param op1   First namspace to compare
127      * @param op2   Second namespace to compare
128      */
129     extern XMLTOOL_API bool operator<(const Namespace& op1, const Namespace& op2);
130
131     /**
132      * Returns true iff op1's namespace and prefix are equal to op2's namespace and prefix.
133      * @param op1   First namspace to compare
134      * @param op2   Second namespace to compare
135      */
136     extern XMLTOOL_API bool operator==(const Namespace& op1, const Namespace& op2);
137
138 };
139
140 #endif /* __xmltooling_namespace_h__ */