1 | // |
---|
2 | // NamespaceSupport.cpp |
---|
3 | // |
---|
4 | // $Id: //poco/1.3/XML/src/NamespaceSupport.cpp#1 $ |
---|
5 | // |
---|
6 | // Library: XML |
---|
7 | // Package: SAX |
---|
8 | // Module: SAX |
---|
9 | // |
---|
10 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
---|
11 | // and Contributors. |
---|
12 | // |
---|
13 | // Permission is hereby granted, free of charge, to any person or organization |
---|
14 | // obtaining a copy of the software and accompanying documentation covered by |
---|
15 | // this license (the "Software") to use, reproduce, display, distribute, |
---|
16 | // execute, and transmit the Software, and to prepare derivative works of the |
---|
17 | // Software, and to permit third-parties to whom the Software is furnished to |
---|
18 | // do so, all subject to the following: |
---|
19 | // |
---|
20 | // The copyright notices in the Software and this entire statement, including |
---|
21 | // the above license grant, this restriction and the following disclaimer, |
---|
22 | // must be included in all copies of the Software, in whole or in part, and |
---|
23 | // all derivative works of the Software, unless such copies or derivative |
---|
24 | // works are solely in the form of machine-executable object code generated by |
---|
25 | // a source language processor. |
---|
26 | // |
---|
27 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
28 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
29 | // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT |
---|
30 | // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE |
---|
31 | // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, |
---|
32 | // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
33 | // DEALINGS IN THE SOFTWARE. |
---|
34 | // |
---|
35 | |
---|
36 | |
---|
37 | #include <Poco/SAX/NamespaceSupport.h> |
---|
38 | #include <Poco/XML/Name.h> |
---|
39 | |
---|
40 | |
---|
41 | namespace Poco { |
---|
42 | namespace XML { |
---|
43 | |
---|
44 | |
---|
45 | const XMLString NamespaceSupport::EMPTY_STRING; |
---|
46 | const XMLString NamespaceSupport::XML_NAMESPACE = toXMLString("http://www.w3.org/XML/1998/namespace"); |
---|
47 | const XMLString NamespaceSupport::XML_NAMESPACE_PREFIX = toXMLString("xml"); |
---|
48 | const XMLString NamespaceSupport::XMLNS_NAMESPACE = toXMLString("http://www.w3.org/xmlns/2000/"); |
---|
49 | const XMLString NamespaceSupport::XMLNS_NAMESPACE_PREFIX = toXMLString("xmlns"); |
---|
50 | |
---|
51 | |
---|
52 | NamespaceSupport::NamespaceSupport() |
---|
53 | { |
---|
54 | reset(); |
---|
55 | } |
---|
56 | |
---|
57 | |
---|
58 | NamespaceSupport::~NamespaceSupport() |
---|
59 | { |
---|
60 | } |
---|
61 | |
---|
62 | |
---|
63 | bool NamespaceSupport::declarePrefix(const XMLString& prefix, const XMLString& namespaceURI) |
---|
64 | { |
---|
65 | poco_assert (_contexts.size() > 0); |
---|
66 | |
---|
67 | Context& ctx = _contexts.back(); |
---|
68 | if (ctx.find(prefix) == ctx.end()) |
---|
69 | { |
---|
70 | ctx.insert(Context::value_type(prefix, namespaceURI)); |
---|
71 | return true; |
---|
72 | } |
---|
73 | else return false; |
---|
74 | } |
---|
75 | |
---|
76 | |
---|
77 | bool NamespaceSupport::undeclarePrefix(const XMLString& prefix) |
---|
78 | { |
---|
79 | for (ContextVec::reverse_iterator rit = _contexts.rbegin(); rit != _contexts.rend(); ++rit) |
---|
80 | { |
---|
81 | Context::iterator it = rit->find(prefix); |
---|
82 | if (it != rit->end()) |
---|
83 | { |
---|
84 | rit->erase(it); |
---|
85 | return true; |
---|
86 | } |
---|
87 | } |
---|
88 | return false; |
---|
89 | } |
---|
90 | |
---|
91 | |
---|
92 | void NamespaceSupport::getDeclaredPrefixes(PrefixSet& prefixes) const |
---|
93 | { |
---|
94 | prefixes.clear(); |
---|
95 | const Context& ctx = _contexts.back(); |
---|
96 | for (Context::const_iterator it = ctx.begin(); it != ctx.end(); ++it) |
---|
97 | prefixes.insert(it->first); |
---|
98 | } |
---|
99 | |
---|
100 | |
---|
101 | const XMLString& NamespaceSupport::getPrefix(const XMLString& namespaceURI) const |
---|
102 | { |
---|
103 | for (ContextVec::const_reverse_iterator rit = _contexts.rbegin(); rit != _contexts.rend(); ++rit) |
---|
104 | { |
---|
105 | for (Context::const_iterator it = rit->begin(); it != rit->end(); ++it) |
---|
106 | { |
---|
107 | if (it->second == namespaceURI) |
---|
108 | return it->first; |
---|
109 | } |
---|
110 | } |
---|
111 | return EMPTY_STRING; |
---|
112 | } |
---|
113 | |
---|
114 | |
---|
115 | bool NamespaceSupport::isMapped(const XMLString& namespaceURI) const |
---|
116 | { |
---|
117 | for (ContextVec::const_reverse_iterator rit = _contexts.rbegin(); rit != _contexts.rend(); ++rit) |
---|
118 | { |
---|
119 | for (Context::const_iterator it = rit->begin(); it != rit->end(); ++it) |
---|
120 | { |
---|
121 | if (it->second == namespaceURI) |
---|
122 | return true; |
---|
123 | } |
---|
124 | } |
---|
125 | return false; |
---|
126 | } |
---|
127 | |
---|
128 | |
---|
129 | void NamespaceSupport::getPrefixes(PrefixSet& prefixes) const |
---|
130 | { |
---|
131 | prefixes.clear(); |
---|
132 | for (ContextVec::const_reverse_iterator rit = _contexts.rbegin(); rit != _contexts.rend(); ++rit) |
---|
133 | { |
---|
134 | for (Context::const_iterator it = rit->begin(); it != rit->end(); ++it) |
---|
135 | { |
---|
136 | const XMLString& prefix = it->first; |
---|
137 | if (!prefix.empty() && prefixes.find(prefix) == prefixes.end()) |
---|
138 | prefixes.insert(it->first); |
---|
139 | } |
---|
140 | } |
---|
141 | } |
---|
142 | |
---|
143 | |
---|
144 | void NamespaceSupport::getPrefixes(const XMLString& namespaceURI, PrefixSet& prefixes) const |
---|
145 | { |
---|
146 | prefixes.clear(); |
---|
147 | for (ContextVec::const_reverse_iterator rit = _contexts.rbegin(); rit != _contexts.rend(); ++rit) |
---|
148 | { |
---|
149 | for (Context::const_iterator it = rit->begin(); it != rit->end(); ++it) |
---|
150 | { |
---|
151 | const XMLString& prefix = it->first; |
---|
152 | if (it->second == namespaceURI && !prefix.empty() && prefixes.find(prefix) == prefixes.end()) |
---|
153 | prefixes.insert(it->first); |
---|
154 | } |
---|
155 | } |
---|
156 | } |
---|
157 | |
---|
158 | |
---|
159 | const XMLString& NamespaceSupport::getURI(const XMLString& prefix) const |
---|
160 | { |
---|
161 | for (ContextVec::const_reverse_iterator rit = _contexts.rbegin(); rit != _contexts.rend(); ++rit) |
---|
162 | { |
---|
163 | Context::const_iterator it = rit->find(prefix); |
---|
164 | if (it != rit->end()) |
---|
165 | return it->second; |
---|
166 | } |
---|
167 | return EMPTY_STRING; |
---|
168 | } |
---|
169 | |
---|
170 | |
---|
171 | void NamespaceSupport::pushContext() |
---|
172 | { |
---|
173 | _contexts.push_back(Context()); |
---|
174 | } |
---|
175 | |
---|
176 | |
---|
177 | void NamespaceSupport::popContext() |
---|
178 | { |
---|
179 | _contexts.pop_back(); |
---|
180 | } |
---|
181 | |
---|
182 | |
---|
183 | bool NamespaceSupport::processName(const XMLString& qname, XMLString& namespaceURI, XMLString& localName, bool isAttribute) const |
---|
184 | { |
---|
185 | XMLString prefix; |
---|
186 | Name::split(qname, prefix, localName); |
---|
187 | if (prefix.empty() && isAttribute) |
---|
188 | { |
---|
189 | namespaceURI.clear(); |
---|
190 | return true; |
---|
191 | } |
---|
192 | else |
---|
193 | { |
---|
194 | namespaceURI = getURI(prefix); |
---|
195 | return !namespaceURI.empty() || prefix.empty(); |
---|
196 | } |
---|
197 | } |
---|
198 | |
---|
199 | |
---|
200 | void NamespaceSupport::reset() |
---|
201 | { |
---|
202 | _contexts.clear(); |
---|
203 | pushContext(); |
---|
204 | declarePrefix(XML_NAMESPACE_PREFIX, XML_NAMESPACE); |
---|
205 | declarePrefix(XMLNS_NAMESPACE_PREFIX, XMLNS_NAMESPACE); |
---|
206 | } |
---|
207 | |
---|
208 | |
---|
209 | } } // namespace Poco::XML |
---|