Characters.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_Characters_h
30 #define Pt_Xml_Characters_h
31 
32 #include <Pt/Xml/Api.h>
33 #include <Pt/Xml/Node.h>
34 #include <Pt/String.h>
35 
36 namespace Pt {
37 
38 namespace Xml {
39 
47 class Characters : public Node
48 {
49  public:
53  : Node(Node::Characters)
54  , _content()
55  , _isSpace(true)
56  , _cdata(false)
57  , _isChunk(false)
58  { }
59 
62  void clear()
63  {
64  _content.clear();
65  _isSpace = true;
66  _cdata = false;
67  _isChunk = false;
68  }
69 
72  bool empty() const
73  { return _content.empty(); }
74 
77  void setCData(bool cdata)
78  { _cdata = cdata; }
79 
82  bool isCData() const
83  { return _cdata; }
84 
87  void setChunk(bool val)
88  { _isChunk = val; }
89 
92  bool isChunk() const
93  { return _isChunk; }
94 
97  bool isSpace() const
98  { return _isSpace; }
99 
102  inline void append(Char ch)
103  {
104  if(_isSpace && ch != ' ' && ch != '\t' && ch != '\n' && ch != '\r')
105  _isSpace = false;
106 
107  _content += ch;
108  }
109 
112  const String& content() const
113  { return _content; }
114 
118  { return _content; }
119 
122  void setContent(const String& content)
123  { _content = content; }
124 
126  inline static const Node::Type nodeId()
127  { return Node::Characters; }
128 
129  private:
130  String _content;
131  bool _isSpace;
132  bool _cdata;
133  bool _isChunk;
134 };
135 
141 {
142  return nodeCast<Characters>(node);
143 }
144 
149 inline const Characters* toCharacters(const Node* node)
150 {
151  return nodeCast<Characters>(node);
152 }
153 
159 {
160  return nodeCast<Characters>(node);
161 }
162 
167 inline const Characters& toCharacters(const Node& node)
168 {
169  return nodeCast<Characters>(node);
170 }
171 
172 } // namespace Xml
173 
174 } // namespace Pt
175 
176 #endif // Pt_Xml_Characters_h
bool isSpace() const
Returns true if the text consists only of whitespace.
Definition: Characters.h:97
void append(Char ch)
Appends a character to the text.
Definition: Characters.h:102
const Characters & toCharacters(const Node &node)
Casts a generic node to a Characters node.
Definition: Characters.h:167
void setCData(bool cdata)
Indicates that text was in a CDATA section.
Definition: Characters.h:77
bool isCData() const
Indicates that text was in a CDATA section.
Definition: Characters.h:82
Characters & toCharacters(Node &node)
Casts a generic node to a Characters node.
Definition: Characters.h:158
bool empty() const
Returns true if empty.
Definition: Characters.h:72
void clear()
Clears the string.
Definition: String.h:367
const String & content() const
Returns the text.
Definition: Characters.h:112
void setContent(const String &content)
Sets the content.
Definition: Characters.h:122
A Character node represents text in an XML document.
Definition: Characters.h:47
bool empty() const
Returns true if empty.
Definition: String.h:244
Unicode character type.
Definition: String.h:66
void setChunk(bool val)
Indicates that text might be split up.
Definition: Characters.h:87
Characters()
Constructs an empty node.
Definition: Characters.h:52
void clear()
Clears all content.
Definition: Characters.h:62
Unicode capable basic_string.
Definition: String.h:42
XML document node.
Definition: Node.h:50
Characters * toCharacters(Node *node)
Casts a generic node to a Characters node.
Definition: Characters.h:140
const Characters * toCharacters(const Node *node)
Casts a generic node to a Characters node.
Definition: Characters.h:149
bool isChunk() const
Indicates that text might be split up.
Definition: Characters.h:92
String & content()
Returns the text.
Definition: Characters.h:117