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