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