b47588e79e24ffbb1dba2a3975b236c0d9e34c94
[shibboleth/cpp-xmltooling.git] / xmltooling / QName.cpp
1 /*\r
2  *  Copyright 2001-2006 Internet2\r
3  * \r
4  * Licensed under the Apache License, Version 2.0 (the "License");\r
5  * you may not use this file except in compliance with the License.\r
6  * You may obtain a copy of the License at\r
7  *\r
8  *     http://www.apache.org/licenses/LICENSE-2.0\r
9  *\r
10  * Unless required by applicable law or agreed to in writing, software\r
11  * distributed under the License is distributed on an "AS IS" BASIS,\r
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
13  * See the License for the specific language governing permissions and\r
14  * limitations under the License.\r
15  */\r
16 \r
17 /**\r
18  * QName.cpp\r
19  * \r
20  * Representing XML QNames \r
21  */\r
22 \r
23 #include "internal.h"\r
24 #include "QName.h"\r
25 \r
26 using namespace xmltooling;\r
27 using namespace std;\r
28 \r
29 QName::QName(const XMLCh* uri, const XMLCh* localPart, const XMLCh* prefix)\r
30 {\r
31 #ifndef HAVE_GOOD_STL\r
32     m_uri=m_prefix=m_local=NULL;\r
33 #endif\r
34     setNamespaceURI(uri);\r
35     setLocalPart(localPart);\r
36     setPrefix(prefix);\r
37 }\r
38 \r
39 QName::~QName()\r
40 {\r
41 #ifndef HAVE_GOOD_STL\r
42     XMLString::release(&m_uri);\r
43     XMLString::release(&m_prefix);\r
44     XMLString::release(&m_local);\r
45 #endif\r
46 }\r
47 \r
48 void QName::setPrefix(const XMLCh* prefix)\r
49 {\r
50 #ifdef HAVE_GOOD_STL\r
51     if (prefix)\r
52         m_prefix=prefix;\r
53     else\r
54         m_prefix.erase();\r
55 #else\r
56     if (m_prefix)\r
57         XMLString::release(&m_prefix);\r
58     m_prefix=XMLString::replicate(prefix);\r
59 #endif\r
60 }\r
61 \r
62 void QName::setNamespaceURI(const XMLCh* uri)\r
63 {\r
64 #ifdef HAVE_GOOD_STL\r
65     if (uri)\r
66         m_uri=uri;\r
67     else\r
68         m_uri.erase();\r
69 #else\r
70     if (m_uri)\r
71         XMLString::release(&m_uri);\r
72     m_uri=XMLString::replicate(uri);\r
73 #endif\r
74 }\r
75 \r
76 void QName::setLocalPart(const XMLCh* localPart)\r
77 {\r
78 #ifdef HAVE_GOOD_STL\r
79     if (localPart)\r
80         m_local=localPart;\r
81     else\r
82         m_local.erase();\r
83 #else\r
84     if (m_local)\r
85         XMLString::release(&m_local);\r
86     m_local=XMLString::replicate(localPart);\r
87 #endif\r
88 }\r
89 \r
90 #ifndef HAVE_GOOD_STL\r
91 QName::QName(const QName& src)\r
92 {\r
93     m_uri=XMLString::replicate(src.getNamespaceURI());\r
94     m_prefix=XMLString::replicate(src.getPrefix());\r
95     m_local=XMLString::replicate(src.getLocalPart());\r
96 }\r
97 \r
98 QName& QName::operator=(const QName& src)\r
99 {\r
100     m_uri=XMLString::replicate(src.getNamespaceURI());\r
101     m_prefix=XMLString::replicate(src.getPrefix());\r
102     m_local=XMLString::replicate(src.getLocalPart());\r
103     return *this;\r
104 }\r
105 #endif\r
106 \r
107 bool xmltooling::operator==(const QName& op1, const QName& op2)\r
108 {\r
109     return (!XMLString::compareString(op1.getNamespaceURI(),op2.getNamespaceURI()) &&\r
110             !XMLString::compareString(op1.getLocalPart(),op2.getLocalPart()));\r
111 }\r
112 \r
113 bool xmltooling::operator<(const QName& op1, const QName& op2)\r
114 {\r
115     int i=XMLString::compareString(op1.getNamespaceURI(),op2.getNamespaceURI());\r
116     if (i<0)\r
117         return true;\r
118     else if (i==0)\r
119         return (XMLString::compareString(op1.getLocalPart(),op2.getLocalPart())<0);\r
120     else\r
121         return false;\r
122 }\r
123 \r
124 string QName::toString() const\r
125 {\r
126     if (!getLocalPart())\r
127         return "";\r
128     auto_ptr_char local(getLocalPart());\r
129     if (getPrefix()) {\r
130         auto_ptr_char pre(getPrefix());\r
131         return string(pre.get()) + ':' + local.get(); \r
132     }\r
133     else if (getNamespaceURI()) {\r
134         auto_ptr_char ns(getNamespaceURI());\r
135         return string("{") + ns.get() + '}' + local.get(); \r
136     }\r
137     else\r
138         return local.get();\r
139 }\r