c26e900b4104ca4835f956eeafc6593280c90664
[shibboleth/cpp-xmltooling.git] / xmltooling / QName.h
1 /*\r
2  *  Copyright 2001-2006 Internet2\r
3  * \r
4  * Licensed under the Apache License, Version 2.0 (the "License");\r
5  * you may not use this file except in compliance with the License.\r
6  * You may obtain a copy of the License at\r
7  *\r
8  *     http://www.apache.org/licenses/LICENSE-2.0\r
9  *\r
10  * Unless required by applicable law or agreed to in writing, software\r
11  * distributed under the License is distributed on an "AS IS" BASIS,\r
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
13  * See the License for the specific language governing permissions and\r
14  * limitations under the License.\r
15  */\r
16 \r
17 /**\r
18  * @file QName.h\r
19  * \r
20  * Representing XML QNames \r
21  */\r
22 \r
23 #if !defined(__xmltooling_qname_h__)\r
24 #define __xmltooling_qname_h__\r
25 \r
26 #include <algorithm>\r
27 #include <xmltooling/unicode.h>\r
28 \r
29 namespace xmltooling {\r
30     \r
31     /**\r
32      * A data structure for encapsulating XML QNames.\r
33      * The Xerces class is too limited to use at the moment.\r
34      */\r
35     class XMLTOOL_API QName\r
36     {\r
37     public:\r
38         /**\r
39          * Constructor\r
40          * @param uri       namespace URI\r
41          * @param localPart local name\r
42          * @param prefix    namespace prefix (without the colon)\r
43          */\r
44         QName(const XMLCh* uri=NULL, const XMLCh* localPart=NULL, const XMLCh* prefix=NULL);\r
45         \r
46         ~QName();\r
47 #ifndef HAVE_GOOD_STL\r
48         /**\r
49          * Deep copy constructor\r
50          */\r
51         QName(const QName& src);\r
52 \r
53         /**\r
54          * Deep assignment operator\r
55          */\r
56         QName& operator=(const QName& src);\r
57 #endif\r
58         \r
59 #ifdef HAVE_GOOD_STL\r
60         /**\r
61          * Indicates whether the QName has a prefix.\r
62          * @return  true iff the prefix is non-empty\r
63          */\r
64         bool hasPrefix() const { return !m_prefix.empty(); }\r
65 \r
66         /**\r
67          * Indicates whether the QName has a non-empty namespace.\r
68          * @return  true iff the namespace is non-empty\r
69          */\r
70         bool hasNamespaceURI() const { return !m_uri.empty(); }\r
71 \r
72         /**\r
73          * Indicates whether the QName has a non-empty local name.\r
74          * @return  true iff the local name is non-empty\r
75          */\r
76         bool hasLocalPart() const { return !m_local.empty(); }\r
77 \r
78         /**\r
79          * Returns the namespace prefix\r
80          * @return  Null-terminated Unicode string containing the prefix, without the colon\r
81          */\r
82         const XMLCh* getPrefix() const { return m_prefix.c_str(); }\r
83 \r
84         /**\r
85          * Returns the namespace URI\r
86          * @return  Null-terminated Unicode string containing the URI\r
87          */\r
88         const XMLCh* getNamespaceURI() const { return m_uri.c_str(); }\r
89 \r
90         /**\r
91          * Returns the local part of the name\r
92          * @return  Null-terminated Unicode string containing the local name\r
93          */\r
94         const XMLCh* getLocalPart() const { return m_local.c_str(); }\r
95 #else\r
96         /**\r
97          * Indicates whether the QName has a prefix.\r
98          * @return  true iff the prefix is non-empty\r
99          */\r
100         bool hasPrefix() const { return m_prefix && *m_prefix; }\r
101 \r
102         /**\r
103          * Indicates whether the QName has a non-empty namespace.\r
104          * @return  true iff the namespace is non-empty\r
105          */\r
106         bool hasNamespaceURI() const { return m_uri && *m_uri; }\r
107 \r
108         /**\r
109          * Indicates whether the QName has a non-empty local name.\r
110          * @return  true iff the local name is non-empty\r
111          */\r
112         bool hasLocalPart() const { return m_local && *m_local; }\r
113 \r
114         /**\r
115          * Returns the namespace prefix\r
116          * @return  Null-terminated Unicode string containing the prefix, without the colon\r
117          */\r
118         const XMLCh* getPrefix() const { return m_prefix; }\r
119 \r
120         /**\r
121          * Returns the namespace URI\r
122          * @return  Null-terminated Unicode string containing the URI\r
123          */\r
124         const XMLCh* getNamespaceURI() const { return m_uri; }\r
125 \r
126         /**\r
127          * Returns the local part of the name\r
128          * @return  Null-terminated Unicode string containing the local name\r
129          */\r
130         const XMLCh* getLocalPart() const { return m_local; }\r
131 #endif\r
132 \r
133         /**\r
134          * Sets the namespace prefix\r
135          * @param prefix    Null-terminated Unicode string containing the prefix, without the colon\r
136          */\r
137         void setPrefix(const XMLCh* prefix);\r
138 \r
139         /**\r
140          * Sets the namespace URI\r
141          * @param uri  Null-terminated Unicode string containing the URI\r
142          */\r
143         void setNamespaceURI(const XMLCh* uri);\r
144         \r
145         /**\r
146          * Sets the local part of the name\r
147          * @param localPart  Null-terminated Unicode string containing the local name\r
148          */\r
149         void setLocalPart(const XMLCh* localPart);\r
150         \r
151         /**\r
152          * Gets a string representation of the QName for logging, etc.\r
153          * Format is prefix:localPart or {namespaceURI}localPart if no prefix.\r
154          * \r
155          * @return the string representation\r
156          */\r
157         std::string toString() const;\r
158         \r
159     private:\r
160 #ifdef HAVE_GOOD_STL\r
161         xstring m_uri;\r
162         xstring m_local;\r
163         xstring m_prefix;\r
164 #else\r
165         XMLCh* m_uri;\r
166         XMLCh* m_local;\r
167         XMLCh* m_prefix;\r
168 #endif\r
169     };\r
170 \r
171     /**\r
172      * Returns true iff op1's namespace lexically compares less than op2's namespace,\r
173      * or if equal, iff op1's prefix lexically compares less than op2's prefix.\r
174      * \r
175      * Needed for use with sorted STL containers.\r
176      * \r
177      * @param op1   First qname to compare\r
178      * @param op2   Second qname to compare\r
179      */\r
180     extern XMLTOOL_API bool operator<(const QName& op1, const QName& op2);\r
181 \r
182     /**\r
183      * Returns true iff op1's components are equal to op2's components, excluding prefix.\r
184      * @param op1   First qname to compare\r
185      * @param op2   Second qname to compare\r
186      */\r
187     extern XMLTOOL_API bool operator==(const QName& op1, const QName& op2);\r
188 \r
189     /**\r
190      * Returns true iff op1's components are not equal to op2's components, excluding prefix.\r
191      * @param op1   First qname to compare\r
192      * @param op2   Second qname to compare\r
193      */\r
194     extern XMLTOOL_API bool operator!=(const QName& op1, const QName& op2);\r
195 \r
196 };\r
197 \r
198 #endif /* __xmltooling_qname_h__ */\r