StartElement.h
1/*
2 * Copyright (C) 2012 Marc Boris Duerner
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * As a special exception, you may use this file as part of a free
10 * software library without restriction. Specifically, if other files
11 * instantiate templates or use macros or inline functions from this
12 * file, or you compile this file and link it with other files to
13 * produce an executable, this file does not by itself cause the
14 * resulting executable to be covered by the GNU General Public
15 * License. This exception does not however invalidate any other
16 * reasons why the executable file might be covered by the GNU Library
17 * General Public License.
18 *
19 * This library is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * Lesser General Public License for more details.
23 *
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 */
28
29#ifndef Pt_Xml_StartElement_h
30#define Pt_Xml_StartElement_h
31
32#include <Pt/Xml/Api.h>
33#include <Pt/Xml/Node.h>
34#include <Pt/Xml/QName.h>
35#include <Pt/Xml/Namespace.h>
36#include <Pt/NonCopyable.h>
37#include <Pt/String.h>
38#include <vector>
39
40namespace Pt {
41
42namespace Xml {
43
44class NamespaceContext;
45
49{
50 public:
54 : _name(0)
55 , _namespace(0)
56 { }
57
60 const QName& name() const
61 { return *_name; }
62
65 void setName(const QName& name, const Namespace& ns)
66 {
67 _name = &name;
68 _namespace = &ns;
69 }
70
73 const String& namespaceUri() const
74 { return _namespace->namespaceUri(); }
75
78 void setNamespace(const Namespace& ns)
79 { _namespace = &ns; }
80
83 const String& value() const
84 { return _value; }
85
89 { return _value; }
90
93 void clear()
94 { _value.clear(); }
95
96 private:
97 const QName* _name;
98 const Namespace* _namespace;
99 String _value;
100};
101
104class PT_XML_API AttributeList : private NonCopyable
105{
106 public:
110
113 typedef const Attribute* ConstIterator;
114
115 public:
118 explicit AttributeList(NamespaceContext& nsctx)
119 : _begin(0)
120 , _end(0)
121 , _size(0)
122 , _nsctx(&nsctx)
123 { }
124
127 void clear()
128 {
129 _begin = 0;
130 _end = 0;
131 _size = 0;
132 }
133
139 Attribute& append(const QName& name, const Namespace& ns);
140
147 const Pt::String& get(const String& localName) const;
148
155 const Pt::String& get(const Char* localName) const;
156
163 const Pt::String& get(const String& nsUri, const String& localName) const;
164
171 const Pt::String& get(const Char* nsUri, const Char* localName) const;
172
179 Iterator find(const String& localName);
180
187 Iterator find(const Char* localName);
188
196 Iterator find(const String& nsUri, const String& localName);
197
205 Iterator find(const Char* nsUri, const Char* localName);
206
213 ConstIterator find(const String& localName) const;
214
221 ConstIterator find(const Char* localName) const;
222
230 ConstIterator find(const String& nsUri, const String& localName) const;
231
239 ConstIterator find(const Char* nsUri, const Char* localName) const;
240
243 bool has(const String& localName) const
244 {
245 return find(localName) != end();
246 }
247
250 bool has(const Char* localName) const
251 {
252 return find(localName) != end();
253 }
254
257 bool has(const String& nsUri, const String& localName) const
258 {
259 return find(nsUri, localName) != end();
260 }
261
264 bool has(const Char* nsUri, const Char* localName) const
265 {
266 return find(nsUri, localName) != end();
267 }
268
272 { return _begin; }
273
277 { return _end; }
278
282 { return _begin; }
283
287 { return _end; }
288
291 bool empty() const
292 { return _size == 0; }
293
296 std::size_t size() const
297 { return _size; }
298
301 NamespaceContext& namespaceContext()
302 { return *_nsctx; }
303
304 private:
305 std::vector<Attribute> _container;
306 Attribute* _begin;
307 Attribute* _end;
308 std::size_t _size;
309 NamespaceContext* _nsctx;
310};
311
318class StartElement : public Node
319 , private NonCopyable
320{
321 public:
324 StartElement(NamespaceContext& nsctx)
326 , _name(0)
327 , _namespace(0)
328 , _attributes(nsctx)
329 { }
330
333 void clear()
334 {
335 _name = 0;
336 _namespace = 0;
337 _nsmap.clear();
338 }
339
342 const QName& name() const
343 { return *_name; }
344
347 void setName(const QName& n, const Namespace& ns)
348 {
349 _name = &n;
350 _namespace = &ns;
351 }
352
355 const String& namespaceUri() const
356 { return _namespace->namespaceUri(); }
357
360 void setNamespace(const Namespace& ns)
361 { _namespace = &ns; }
362
366 { return _attributes; }
367
371 { return _attributes; }
372
373 NamespaceMapping& namespaceMapping()
374 { return _nsmap; }
375
376 const NamespaceMapping& namespaceMapping() const
377 { return _nsmap; }
378
380 inline static const Node::Type nodeId()
381 { return Node::StartElement; }
382
383 private:
384 const QName* _name;
385 const Namespace* _namespace;
386 AttributeList _attributes;
387 NamespaceMapping _nsmap;
388};
389
395{
396 return nodeCast<StartElement>(node);
397}
398
403inline const StartElement* toStartElement(const Node* node)
404{
405 return nodeCast<StartElement>(node);
406}
407
413{
414 return nodeCast<StartElement>(node);
415}
416
421inline const StartElement& toStartElement(const Node& node)
422{
423 return nodeCast<StartElement>(node);
424}
425
426} // namespace Xml
427
428} // namespace Pt
429
430#endif // Pt_Xml_StartElement_h
NonCopyable()
Default constructor.
Definition NonCopyable.h:63
Unicode capable basic_string.
Definition Api-String.h:67
An attribute list of an XML element.
Definition StartElement.h:105
bool has(const Char *nsUri, const Char *localName) const
Checks if an attribute is present.
Definition StartElement.h:264
const Attribute * ConstIterator
Attribute list const iterator.
Definition StartElement.h:113
bool has(const Char *localName) const
Checks if an attribute is present.
Definition StartElement.h:250
NamespaceContext & namespaceContext()
Returns the namespace context for the attributes.
Definition StartElement.h:301
const Pt::String & get(const Char *localName) const
Gets the value of an attribute.
Iterator begin()
An iterator to the begin of the sequence.
Definition StartElement.h:271
AttributeList(NamespaceContext &nsctx)
Construct with namespace context.
Definition StartElement.h:118
ConstIterator find(const String &nsUri, const String &localName) const
Finds an attribute by name.
Attribute * Iterator
Attribute list iterator.
Definition StartElement.h:109
const Pt::String & get(const String &nsUri, const String &localName) const
Gets the value of an attribute.
Iterator find(const String &nsUri, const String &localName)
Finds an attribute by name.
const Pt::String & get(const Char *nsUri, const Char *localName) const
Gets the value of an attribute.
Iterator find(const Char *nsUri, const Char *localName)
Finds an attribute by name.
bool empty() const
Returns true if the list is empty.
Definition StartElement.h:291
Iterator find(const Char *localName)
Finds an attribute by name.
ConstIterator end() const
An iterator to the end of the sequence.
Definition StartElement.h:286
ConstIterator find(const String &localName) const
Finds an attribute by name.
Attribute & append(const QName &name, const Namespace &ns)
Appends a new element to the end of the list.
const Pt::String & get(const String &localName) const
Gets the value of an attribute.
Iterator end()
An iterator to the end of the sequence.
Definition StartElement.h:276
bool has(const String &nsUri, const String &localName) const
Checks if an attribute is present.
Definition StartElement.h:257
ConstIterator find(const Char *nsUri, const Char *localName) const
Finds an attribute by name.
void clear()
Clears all content.
Definition StartElement.h:127
ConstIterator begin() const
An iterator to the begin of the sequence.
Definition StartElement.h:281
Iterator find(const String &localName)
Finds an attribute by name.
std::size_t size() const
Returns the size of the list.
Definition StartElement.h:296
bool has(const String &localName) const
Checks if an attribute is present.
Definition StartElement.h:243
ConstIterator find(const Char *localName) const
Finds an attribute by name.
A single attribute of a start element.
Definition StartElement.h:49
Attribute()
Default constructor.
Definition StartElement.h:53
const String & namespaceUri() const
Returns the namespaceUri.
Definition StartElement.h:73
void setName(const QName &name, const Namespace &ns)
Sets the qualified name and namespace.
Definition StartElement.h:65
const QName & name() const
Returns the qualified name.
Definition StartElement.h:60
void setNamespace(const Namespace &ns)
Sets the namespace.
Definition StartElement.h:78
String & value()
Returns the value of this attribute.
Definition StartElement.h:88
const String & value() const
Returns the value of this attribute.
Definition StartElement.h:83
void clear()
Clears the value of this attribute.
Definition StartElement.h:93
A namespace used in an XML document.
Definition Namespace.h:47
Node(Type type)
Constructs a new Node object with the specified node type.
Definition Node.h:80
A qualified XML name.
Definition QName.h:47
StartElement(NamespaceContext &nsctx)
Constructs an empty StartElement.
Definition StartElement.h:324
const StartElement & toStartElement(const Node &node)
Casts a generic node to a StartElement node.
Definition StartElement.h:421
StartElement & toStartElement(Node &node)
Casts a generic node to a StartElement node.
Definition StartElement.h:412
const String & namespaceUri() const
Returns the namespace Uri for this element name.
Definition StartElement.h:355
const QName & name() const
Returns the qualified name.
Definition StartElement.h:342
const StartElement * toStartElement(const Node *node)
Casts a generic node to a StartElement node.
Definition StartElement.h:403
void setNamespace(const Namespace &ns)
Sets the namespace.
Definition StartElement.h:360
AttributeList & attributes()
Returns the attributes of the element.
Definition StartElement.h:370
StartElement * toStartElement(Node *node)
Casts a generic node to a StartElement node.
Definition StartElement.h:394
void setName(const QName &n, const Namespace &ns)
Sets the qualified name.
Definition StartElement.h:347
void clear()
Clears all content.
Definition StartElement.h:333
const AttributeList & attributes() const
Returns the attributes of the element.
Definition StartElement.h:365
Read and write XML ducuments.
Core module.
Definition Allocator.h:33
Unicode character type.
Definition String.h:67