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