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