Optimize self-comparison of static constants.
[shibboleth/xmltooling.git] / xmltooling / QName.cpp
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  * QName.cpp
19  * 
20  * Representing XML QNames 
21  */
22
23 #include "internal.h"
24 #include "QName.h"
25
26 using namespace xmltooling;
27 using namespace std;
28
29 QName::QName(const XMLCh* uri, const XMLCh* localPart, const XMLCh* prefix)
30 {
31 #ifndef HAVE_GOOD_STL
32     m_uri=m_prefix=m_local=NULL;
33 #endif
34     setNamespaceURI(uri);
35     setLocalPart(localPart);
36     setPrefix(prefix);
37 }
38
39 QName::QName(const char* uri, const char* localPart, const char* prefix)
40 {
41 #ifndef HAVE_GOOD_STL
42     m_uri=m_prefix=m_local=NULL;
43 #endif
44     setNamespaceURI(uri);
45     setLocalPart(localPart);
46     setPrefix(prefix);
47 }
48
49 QName::~QName()
50 {
51 #ifndef HAVE_GOOD_STL
52     XMLString::release(&m_uri);
53     XMLString::release(&m_prefix);
54     XMLString::release(&m_local);
55 #endif
56 }
57
58 void QName::setPrefix(const XMLCh* prefix)
59 {
60 #ifdef HAVE_GOOD_STL
61     if (prefix)
62         m_prefix=prefix;
63     else
64         m_prefix.erase();
65 #else
66     if (m_prefix)
67         XMLString::release(&m_prefix);
68     m_prefix=XMLString::replicate(prefix);
69 #endif
70 }
71
72 void QName::setNamespaceURI(const XMLCh* uri)
73 {
74 #ifdef HAVE_GOOD_STL
75     if (uri)
76         m_uri=uri;
77     else
78         m_uri.erase();
79 #else
80     if (m_uri)
81         XMLString::release(&m_uri);
82     m_uri=XMLString::replicate(uri);
83 #endif
84 }
85
86 void QName::setLocalPart(const XMLCh* localPart)
87 {
88 #ifdef HAVE_GOOD_STL
89     if (localPart)
90         m_local=localPart;
91     else
92         m_local.erase();
93 #else
94     if (m_local)
95         XMLString::release(&m_local);
96     m_local=XMLString::replicate(localPart);
97 #endif
98 }
99
100 void QName::setPrefix(const char* prefix)
101 {
102 #ifdef HAVE_GOOD_STL
103     if (prefix) {
104         auto_ptr_XMLCh temp(prefix);
105         m_prefix=temp.get();
106     }
107     else
108         m_prefix.erase();
109 #else
110     if (m_prefix)
111         XMLString::release(&m_prefix);
112     m_prefix=XMLString::transcode(prefix);
113 #endif
114 }
115
116 void QName::setNamespaceURI(const char* uri)
117 {
118 #ifdef HAVE_GOOD_STL
119     if (uri) {
120         auto_ptr_XMLCh temp(uri);
121         m_uri=temp.get();
122     }
123     else
124         m_uri.erase();
125 #else
126     if (m_uri)
127         XMLString::release(&m_uri);
128     m_uri=XMLString::transcode(uri);
129 #endif
130 }
131
132 void QName::setLocalPart(const char* localPart)
133 {
134 #ifdef HAVE_GOOD_STL
135     if (localPart) {
136         auto_ptr_XMLCh temp(localPart);
137         m_local=temp.get();
138     }
139     else
140         m_local.erase();
141 #else
142     if (m_local)
143         XMLString::release(&m_local);
144     m_local=XMLString::transcode(localPart);
145 #endif
146 }
147
148 #ifndef HAVE_GOOD_STL
149 QName::QName(const QName& src)
150 {
151     m_uri=XMLString::replicate(src.getNamespaceURI());
152     m_prefix=XMLString::replicate(src.getPrefix());
153     m_local=XMLString::replicate(src.getLocalPart());
154 }
155
156 QName& QName::operator=(const QName& src)
157 {
158     m_uri=XMLString::replicate(src.getNamespaceURI());
159     m_prefix=XMLString::replicate(src.getPrefix());
160     m_local=XMLString::replicate(src.getLocalPart());
161     return *this;
162 }
163 #endif
164
165 bool xmltooling::operator==(const QName& op1, const QName& op2)
166 {
167     if (&op1 == &op2)
168         return true;
169     return (!XMLString::compareString(op1.getNamespaceURI(),op2.getNamespaceURI()) &&
170             !XMLString::compareString(op1.getLocalPart(),op2.getLocalPart()));
171 }
172
173 bool xmltooling::operator!=(const QName& op1, const QName& op2)
174 {
175     return !(op1==op2);
176 }
177
178 bool xmltooling::operator<(const QName& op1, const QName& op2)
179 {
180     int i=XMLString::compareString(op1.getNamespaceURI(),op2.getNamespaceURI());
181     if (i<0)
182         return true;
183     else if (i==0)
184         return (XMLString::compareString(op1.getLocalPart(),op2.getLocalPart())<0);
185     else
186         return false;
187 }
188
189 string QName::toString() const
190 {
191     if (!hasLocalPart())
192         return "";
193     auto_ptr_char local(getLocalPart());
194     if (hasPrefix()) {
195         auto_ptr_char pre(getPrefix());
196         return string(pre.get()) + ':' + local.get(); 
197     }
198     else if (hasNamespaceURI()) {
199         auto_ptr_char ns(getNamespaceURI());
200         return string("{") + ns.get() + '}' + local.get(); 
201     }
202     else
203         return local.get();
204 }