17d8d775d530edb081b3f091fe5d4adfba52c5dc
[shibboleth/cpp-xmltooling.git] / xmltooling / QName.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/QName.h
19  * 
20  * Representing XML QNames 
21  */
22
23 #ifndef __xmltooling_qname_h__
24 #define __xmltooling_qname_h__
25
26 #include <xmltooling/unicode.h>
27 #include <algorithm>
28
29 namespace xmltooling {
30
31 #if defined (_MSC_VER)
32     #pragma warning( push )
33     #pragma warning( disable : 4251 )
34 #endif
35
36     /**
37      * A data structure for encapsulating XML QNames.
38      * The Xerces class is too limited to use at the moment.
39      */
40     class XMLTOOL_API QName
41     {
42     public:
43         /**
44          * Constructor
45          * 
46          * @param uri       namespace URI
47          * @param localPart local name
48          * @param prefix    namespace prefix (without the colon)
49          */
50         QName(const XMLCh* uri=NULL, const XMLCh* localPart=NULL, const XMLCh* prefix=NULL);
51
52         /**
53          * Constructor
54          * 
55          * @param uri       namespace URI
56          * @param localPart local name
57          * @param prefix    namespace prefix (without the colon)
58          */
59         QName(const char* uri, const char* localPart, const char* prefix=NULL);
60         
61         ~QName();
62 #ifndef HAVE_GOOD_STL
63         /**
64          * Deep copy constructor
65          */
66         QName(const QName& src);
67
68         /**
69          * Deep assignment operator
70          */
71         QName& operator=(const QName& src);
72 #endif
73         
74 #ifdef HAVE_GOOD_STL
75         /**
76          * Indicates whether the QName has a prefix.
77          * @return  true iff the prefix is non-empty
78          */
79         bool hasPrefix() const { return !m_prefix.empty(); }
80
81         /**
82          * Indicates whether the QName has a non-empty namespace.
83          * @return  true iff the namespace is non-empty
84          */
85         bool hasNamespaceURI() const { return !m_uri.empty(); }
86
87         /**
88          * Indicates whether the QName has a non-empty local name.
89          * @return  true iff the local name is non-empty
90          */
91         bool hasLocalPart() const { return !m_local.empty(); }
92
93         /**
94          * Returns the namespace prefix
95          * @return  Null-terminated Unicode string containing the prefix, without the colon
96          */
97         const XMLCh* getPrefix() const { return m_prefix.c_str(); }
98
99         /**
100          * Returns the namespace URI
101          * @return  Null-terminated Unicode string containing the URI
102          */
103         const XMLCh* getNamespaceURI() const { return m_uri.c_str(); }
104
105         /**
106          * Returns the local part of the name
107          * @return  Null-terminated Unicode string containing the local name
108          */
109         const XMLCh* getLocalPart() const { return m_local.c_str(); }
110 #else
111         /**
112          * Indicates whether the QName has a prefix.
113          * @return  true iff the prefix is non-empty
114          */
115         bool hasPrefix() const { return m_prefix && *m_prefix; }
116
117         /**
118          * Indicates whether the QName has a non-empty namespace.
119          * @return  true iff the namespace is non-empty
120          */
121         bool hasNamespaceURI() const { return m_uri && *m_uri; }
122
123         /**
124          * Indicates whether the QName has a non-empty local name.
125          * @return  true iff the local name is non-empty
126          */
127         bool hasLocalPart() const { return m_local && *m_local; }
128
129         /**
130          * Returns the namespace prefix
131          * @return  Null-terminated Unicode string containing the prefix, without the colon
132          */
133         const XMLCh* getPrefix() const { return m_prefix; }
134
135         /**
136          * Returns the namespace URI
137          * @return  Null-terminated Unicode string containing the URI
138          */
139         const XMLCh* getNamespaceURI() const { return m_uri; }
140
141         /**
142          * Returns the local part of the name
143          * @return  Null-terminated Unicode string containing the local name
144          */
145         const XMLCh* getLocalPart() const { return m_local; }
146 #endif
147
148         /**
149          * Sets the namespace prefix
150          * @param prefix    Null-terminated Unicode string containing the prefix, without the colon
151          */
152         void setPrefix(const XMLCh* prefix);
153
154         /**
155          * Sets the namespace URI
156          * @param uri  Null-terminated Unicode string containing the URI
157          */
158         void setNamespaceURI(const XMLCh* uri);
159         
160         /**
161          * Sets the local part of the name
162          * @param localPart  Null-terminated Unicode string containing the local name
163          */
164         void setLocalPart(const XMLCh* localPart);
165         
166         /**
167          * Sets the namespace prefix
168          * @param prefix    Null-terminated ASCII string containing the prefix, without the colon
169          */
170         void setPrefix(const char* prefix);
171
172         /**
173          * Sets the namespace URI
174          * @param uri  Null-terminated ASCII string containing the URI
175          */
176         void setNamespaceURI(const char* uri);
177         
178         /**
179          * Sets the local part of the name
180          * @param localPart  Null-terminated ASCII string containing the local name
181          */
182         void setLocalPart(const char* localPart);
183         
184         /**
185          * Gets a string representation of the QName for logging, etc.
186          * Format is prefix:localPart or {namespaceURI}localPart if no prefix.
187          * 
188          * @return the string representation
189          */
190         std::string toString() const;
191         
192     private:
193 #ifdef HAVE_GOOD_STL
194         xstring m_uri;
195         xstring m_local;
196         xstring m_prefix;
197 #else
198         XMLCh* m_uri;
199         XMLCh* m_local;
200         XMLCh* m_prefix;
201 #endif
202     };
203
204 #if defined (_MSC_VER)
205     #pragma warning( pop )
206 #endif
207
208     /**
209      * Returns true iff op1's namespace lexically compares less than op2's namespace,
210      * or if equal, iff op1's prefix lexically compares less than op2's prefix.
211      * 
212      * Needed for use with sorted STL containers.
213      * 
214      * @param op1   First qname to compare
215      * @param op2   Second qname to compare
216      */
217     extern XMLTOOL_API bool operator<(const QName& op1, const QName& op2);
218
219     /**
220      * Returns true iff op1's components are equal to op2's components, excluding prefix.
221      * @param op1   First qname to compare
222      * @param op2   Second qname to compare
223      */
224     extern XMLTOOL_API bool operator==(const QName& op1, const QName& op2);
225
226     /**
227      * Returns true iff op1's components are not equal to op2's components, excluding prefix.
228      * @param op1   First qname to compare
229      * @param op2   Second qname to compare
230      */
231     extern XMLTOOL_API bool operator!=(const QName& op1, const QName& op2);
232
233 };
234
235 #endif /* __xmltooling_qname_h__ */