Update copyright.
[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 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()
40 {
41 #ifndef HAVE_GOOD_STL
42     XMLString::release(&m_uri);
43     XMLString::release(&m_prefix);
44     XMLString::release(&m_local);
45 #endif
46 }
47
48 void QName::setPrefix(const XMLCh* prefix)
49 {
50 #ifdef HAVE_GOOD_STL
51     if (prefix)
52         m_prefix=prefix;
53     else
54         m_prefix.erase();
55 #else
56     if (m_prefix)
57         XMLString::release(&m_prefix);
58     m_prefix=XMLString::replicate(prefix);
59 #endif
60 }
61
62 void QName::setNamespaceURI(const XMLCh* uri)
63 {
64 #ifdef HAVE_GOOD_STL
65     if (uri)
66         m_uri=uri;
67     else
68         m_uri.erase();
69 #else
70     if (m_uri)
71         XMLString::release(&m_uri);
72     m_uri=XMLString::replicate(uri);
73 #endif
74 }
75
76 void QName::setLocalPart(const XMLCh* localPart)
77 {
78 #ifdef HAVE_GOOD_STL
79     if (localPart)
80         m_local=localPart;
81     else
82         m_local.erase();
83 #else
84     if (m_local)
85         XMLString::release(&m_local);
86     m_local=XMLString::replicate(localPart);
87 #endif
88 }
89
90 #ifndef HAVE_GOOD_STL
91 QName::QName(const QName& src)
92 {
93     m_uri=XMLString::replicate(src.getNamespaceURI());
94     m_prefix=XMLString::replicate(src.getPrefix());
95     m_local=XMLString::replicate(src.getLocalPart());
96 }
97
98 QName& QName::operator=(const QName& src)
99 {
100     m_uri=XMLString::replicate(src.getNamespaceURI());
101     m_prefix=XMLString::replicate(src.getPrefix());
102     m_local=XMLString::replicate(src.getLocalPart());
103     return *this;
104 }
105 #endif
106
107 bool xmltooling::operator==(const QName& op1, const QName& op2)
108 {
109     return (!XMLString::compareString(op1.getNamespaceURI(),op2.getNamespaceURI()) &&
110             !XMLString::compareString(op1.getLocalPart(),op2.getLocalPart()));
111 }
112
113 bool xmltooling::operator!=(const QName& op1, const QName& op2)
114 {
115     return !(op1==op2);
116 }
117
118 bool xmltooling::operator<(const QName& op1, const QName& op2)
119 {
120     int i=XMLString::compareString(op1.getNamespaceURI(),op2.getNamespaceURI());
121     if (i<0)
122         return true;
123     else if (i==0)
124         return (XMLString::compareString(op1.getLocalPart(),op2.getLocalPart())<0);
125     else
126         return false;
127 }
128
129 string QName::toString() const
130 {
131     if (!hasLocalPart())
132         return "";
133     auto_ptr_char local(getLocalPart());
134     if (hasPrefix()) {
135         auto_ptr_char pre(getPrefix());
136         return string(pre.get()) + ':' + local.get(); 
137     }
138     else if (hasNamespaceURI()) {
139         auto_ptr_char ns(getNamespaceURI());
140         return string("{") + ns.get() + '}' + local.get(); 
141     }
142     else
143         return local.get();
144 }