1//
2// NamespaceStrategy.cpp
3//
4// Library: XML
5// Package: XML
6// Module: NamespaceStrategy
7//
8// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
9// and Contributors.
10//
11// SPDX-License-Identifier: BSL-1.0
12//
13
14
15#include "Poco/XML/NamespaceStrategy.h"
16#include "Poco/SAX/AttributesImpl.h"
17#include "Poco/SAX/ContentHandler.h"
18#include "Poco/XML/XMLException.h"
19#include "Poco/XML/Name.h"
20
21
22namespace Poco {
23namespace XML {
24
25
26const XMLString NamespaceStrategy::NOTHING;
27
28
29NamespaceStrategy::~NamespaceStrategy()
30{
31}
32
33
34void NamespaceStrategy::splitName(const XMLChar* qname, XMLString& uri, XMLString& localName)
35{
36 for (const XMLChar* p = qname; *p; ++p)
37 {
38 if (*p == '\t')
39 {
40 uri.assign(qname, p - qname);
41 localName.assign(p + 1);
42 return;
43 }
44 }
45 localName = qname;
46}
47
48
49void NamespaceStrategy::splitName(const XMLChar* qname, XMLString& uri, XMLString& localName, XMLString& prefix)
50{
51 const XMLChar* p = qname;
52 while (*p && *p != '\t') ++p;
53 if (*p)
54 {
55 uri.assign(qname, p - qname);
56 ++p;
57 const XMLChar* loc = p;
58 while (*p && *p != '\t') ++p;
59 localName.assign(loc, p - loc);
60 if (*p)
61 prefix.assign(++p);
62 else
63 prefix.assign(XML_LIT(""));
64 }
65 else
66 {
67 uri.assign(XML_LIT(""));
68 localName = qname;
69 prefix.assign(XML_LIT(""));
70 }
71}
72
73
74NoNamespacesStrategy::NoNamespacesStrategy()
75{
76 _attrs.reserve(32);
77}
78
79
80NoNamespacesStrategy::~NoNamespacesStrategy()
81{
82}
83
84
85void NoNamespacesStrategy::startElement(const XMLChar* name, const XMLChar** atts, int specifiedCount, ContentHandler* pContentHandler)
86{
87 poco_assert_dbg (name && atts && pContentHandler);
88
89 _attrs.clear();
90 for (int i = 0; *atts; ++i)
91 {
92 AttributesImpl::Attribute& attr = _attrs.addAttribute();
93 attr.qname.assign(*atts++);
94 attr.value.assign(*atts++);
95 attr.specified = i < specifiedCount;
96 }
97 _name.assign(name);
98 pContentHandler->startElement(NOTHING, NOTHING, _name, _attrs);
99}
100
101
102void NoNamespacesStrategy::endElement(const XMLChar* name, ContentHandler* pContentHandler)
103{
104 poco_assert_dbg (name && pContentHandler);
105
106 _name.assign(name);
107 pContentHandler->endElement(NOTHING, NOTHING, _name);
108}
109
110
111NoNamespacePrefixesStrategy::NoNamespacePrefixesStrategy()
112{
113 _attrs.reserve(32);
114}
115
116
117NoNamespacePrefixesStrategy::~NoNamespacePrefixesStrategy()
118{
119}
120
121
122void NoNamespacePrefixesStrategy::startElement(const XMLChar* name, const XMLChar** atts, int specifiedCount, ContentHandler* pContentHandler)
123{
124 poco_assert_dbg (name && atts && pContentHandler);
125
126 _attrs.clear();
127 for (int i = 0; *atts; ++i)
128 {
129 const XMLChar* attrName = *atts++;
130 const XMLChar* attrValue = *atts++;
131 AttributesImpl::Attribute& attr = _attrs.addAttribute();
132 splitName(attrName, attr.namespaceURI, attr.localName);
133 attr.value.assign(attrValue);
134 attr.specified = i < specifiedCount;
135 }
136 splitName(name, _uri, _local);
137 pContentHandler->startElement(_uri, _local, NOTHING, _attrs);
138}
139
140
141void NoNamespacePrefixesStrategy::endElement(const XMLChar* name, ContentHandler* pContentHandler)
142{
143 poco_assert_dbg (name && pContentHandler);
144
145 splitName(name, _uri, _local);
146 pContentHandler->endElement(_uri, _local, NOTHING);
147}
148
149
150NamespacePrefixesStrategy::NamespacePrefixesStrategy()
151{
152 _attrs.reserve(32);
153}
154
155
156NamespacePrefixesStrategy::~NamespacePrefixesStrategy()
157{
158}
159
160
161void NamespacePrefixesStrategy::startElement(const XMLChar* name, const XMLChar** atts, int specifiedCount, ContentHandler* pContentHandler)
162{
163 poco_assert_dbg (name && atts && pContentHandler);
164
165 _attrs.clear();
166 for (int i = 0; *atts; ++i)
167 {
168 const XMLChar* attrName = *atts++;
169 const XMLChar* attrValue = *atts++;
170 AttributesImpl::Attribute& attr = _attrs.addAttribute();
171 splitName(attrName, attr.namespaceURI, attr.localName, attr.qname);
172 if (!attr.qname.empty()) attr.qname += ':';
173 attr.qname.append(attr.localName);
174 attr.value.assign(attrValue);
175 attr.specified = i < specifiedCount;
176 }
177 splitName(name, _uri, _local, _qname);
178 if (!_qname.empty()) _qname += ':';
179 _qname.append(_local);
180 pContentHandler->startElement(_uri, _local, _qname, _attrs);
181}
182
183
184void NamespacePrefixesStrategy::endElement(const XMLChar* name, ContentHandler* pContentHandler)
185{
186 poco_assert_dbg (name && pContentHandler);
187
188 splitName(name, _uri, _local, _qname);
189 if (!_qname.empty()) _qname += ':';
190 _qname.append(_local);
191 pContentHandler->endElement(_uri, _local, _qname);
192}
193
194
195} } // namespace Poco::XML
196