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