0f75b823bee882e453f3209aeddca92d424e4029
[shibboleth/cpp-xmltooling.git] / xmltooling / QName.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/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=nullptr, const XMLCh* localPart=nullptr, const XMLCh* prefix=nullptr);
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=nullptr);
60         
61         ~QName();
62         
63         /**
64          * Indicates whether the QName has a prefix.
65          * @return  true iff the prefix is non-empty
66          */
67         bool hasPrefix() const { return !m_prefix.empty(); }
68
69         /**
70          * Indicates whether the QName has a non-empty namespace.
71          * @return  true iff the namespace is non-empty
72          */
73         bool hasNamespaceURI() const { return !m_uri.empty(); }
74
75         /**
76          * Indicates whether the QName has a non-empty local name.
77          * @return  true iff the local name is non-empty
78          */
79         bool hasLocalPart() const { return !m_local.empty(); }
80
81         /**
82          * Returns the namespace prefix
83          * @return  Null-terminated Unicode string containing the prefix, without the colon
84          */
85         const XMLCh* getPrefix() const { return m_prefix.c_str(); }
86
87         /**
88          * Returns the namespace URI
89          * @return  Null-terminated Unicode string containing the URI
90          */
91         const XMLCh* getNamespaceURI() const { return m_uri.c_str(); }
92
93         /**
94          * Returns the local part of the name
95          * @return  Null-terminated Unicode string containing the local name
96          */
97         const XMLCh* getLocalPart() const { return m_local.c_str(); }
98
99         /**
100          * Sets the namespace prefix
101          * @param prefix    Null-terminated Unicode string containing the prefix, without the colon
102          */
103         void setPrefix(const XMLCh* prefix);
104
105         /**
106          * Sets the namespace URI
107          * @param uri  Null-terminated Unicode string containing the URI
108          */
109         void setNamespaceURI(const XMLCh* uri);
110         
111         /**
112          * Sets the local part of the name
113          * @param localPart  Null-terminated Unicode string containing the local name
114          */
115         void setLocalPart(const XMLCh* localPart);
116         
117         /**
118          * Sets the namespace prefix
119          * @param prefix    Null-terminated ASCII string containing the prefix, without the colon
120          */
121         void setPrefix(const char* prefix);
122
123         /**
124          * Sets the namespace URI
125          * @param uri  Null-terminated ASCII string containing the URI
126          */
127         void setNamespaceURI(const char* uri);
128         
129         /**
130          * Sets the local part of the name
131          * @param localPart  Null-terminated ASCII string containing the local name
132          */
133         void setLocalPart(const char* localPart);
134         
135         /**
136          * Gets a string representation of the QName for logging, etc.
137          * Format is prefix:localPart or {namespaceURI}localPart if no prefix.
138          * 
139          * @return the string representation
140          */
141         std::string toString() const;
142         
143     private:
144         xstring m_uri;
145         xstring m_local;
146         xstring m_prefix;
147     };
148
149 #if defined (_MSC_VER)
150     #pragma warning( pop )
151 #endif
152
153     /**
154      * Returns true iff op1's namespace lexically compares less than op2's namespace,
155      * or if equal, iff op1's prefix lexically compares less than op2's prefix.
156      * 
157      * Needed for use with sorted STL containers.
158      * 
159      * @param op1   First qname to compare
160      * @param op2   Second qname to compare
161      */
162     extern XMLTOOL_API bool operator<(const QName& op1, const QName& op2);
163
164     /**
165      * Returns true iff op1's components are equal to op2's components, excluding prefix.
166      * @param op1   First qname to compare
167      * @param op2   Second qname to compare
168      */
169     extern XMLTOOL_API bool operator==(const QName& op1, const QName& op2);
170
171     /**
172      * Returns true iff op1's components are not equal to op2's components, excluding prefix.
173      * @param op1   First qname to compare
174      * @param op2   Second qname to compare
175      */
176     extern XMLTOOL_API bool operator!=(const QName& op1, const QName& op2);
177
178 };
179
180 #endif /* __xmltooling_qname_h__ */