Entity.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_Entity_h
30 #define Pt_Xml_Entity_h
31 
32 #include <Pt/Xml/Api.h>
33 #include <Pt/Xml/Node.h>
34 #include <Pt/NonCopyable.h>
35 #include <Pt/String.h>
36 #include <map>
37 
38 namespace Pt {
39 
40 namespace Xml {
41 
44 class Entity
45 {
46  public:
49  explicit Entity(const Pt::String& name)
50  : _name(name)
51  , _ndata(false)
52  {}
53 
56  const Pt::String& name() const
57  { return _name; }
58 
61  bool isExternal() const
62  { return ! _publicId.empty() || ! _systemId.empty(); }
63 
66  bool isInternal() const
67  { return _publicId.empty() && _systemId.empty(); }
68 
71  const Pt::String& value() const
72  { return _value; }
73 
77  { return _value; }
78 
81  void setValue(const Pt::String& val)
82  { _value = val; }
83 
86  const Pt::String& publicId() const
87  { return _publicId; }
88 
91  void setPublicId(const Pt::String& pubId)
92  { _publicId = pubId; }
93 
96  const Pt::String& systemId() const
97  { return _systemId; }
98 
101  void setSystemId(const Pt::String& sysId)
102  { _systemId = sysId; }
103 
106  bool isUnparsed() const
107  { return _ndata; }
108 
111  void setUnparsed(const Pt::String& notation)
112  {
113  _ndata = true;
114  _value = notation;
115  }
116 
119  const Pt::String& notationName() const
120  { return _value; }
121 
122  private:
123  Pt::String _name;
124  Pt::String _publicId;
125  Pt::String _systemId;
126  Pt::String _value;
127  bool _ndata;
128 };
129 
136 class EntityReference : public Node
137  , private NonCopyable
138 {
139  public:
144  , _entity(0)
145  { }
146 
149  void clear()
150  {
151  _name.clear();
152  _entity = 0;
153  }
154 
157  const Pt::String& name() const
158  { return _name; }
159 
163  { return _name; }
164 
167  void setName(const Pt::String& name)
168  { _name = name; }
169 
172  const Entity* get() const
173  { return _entity; }
174 
177  void setEntity(const Entity* entity)
178  { _entity = entity; }
179 
181  inline static Node::Type nodeId()
182  { return Node::EntityReference; }
183 
184  private:
185  Pt::String _name;
186  const Entity* _entity;
187 };
188 
194 {
195  return nodeCast<EntityReference>(node);
196 }
197 
202 inline const EntityReference* toEntityReference(const Node* node)
203 {
204  return nodeCast<EntityReference>(node);
205 }
206 
212 {
213  return nodeCast<EntityReference>(node);
214 }
215 
220 inline const EntityReference& toEntityReference(const Node& node)
221 {
222  return nodeCast<EntityReference>(node);
223 }
224 
227 bool resolveDefaultEntity(String& entity);
228 
231 bool resolveCharacterEntity(String& entity);
232 
233 } // namespace Xml
234 
235 } // namespace Pt
236 
237 #endif // Pt_Xml_Entity_h
bool isUnparsed() const
Indicates if the entity is unparsed (NDATA).
Definition: Entity.h:106
const Pt::String & name() const
Returns the name of the entity this reference refers to.
Definition: Entity.h:157
void setName(const Pt::String &name)
Sets the name of the entity this reference refers to.
Definition: Entity.h:167
Entity(const Pt::String &name)
Constructs with entity name.
Definition: Entity.h:49
EntityReference & toEntityReference(Node &node)
Casts a generic node to an EntityReference node.
Definition: Entity.h:211
Protects derived classes from being copied.
Definition: NonCopyable.h:54
void setUnparsed(const Pt::String &notation)
Sets the notation of an unparsed entity (NDATA).
Definition: Entity.h:111
EntityReference * toEntityReference(Node *node)
Casts a generic node to an EntityReference node.
Definition: Entity.h:193
Pt::String & value()
Returns the value of the entity.
Definition: Entity.h:76
void setPublicId(const Pt::String &pubId)
Sets the public ID of the entity.
Definition: Entity.h:91
void clear()
Clears the string.
Definition: String.h:367
bool empty() const
Returns true if empty.
Definition: String.h:244
void setEntity(const Entity *entity)
Sets the entity this reference refers to.
Definition: Entity.h:177
const Pt::String & name() const
Returns the name of the entity.
Definition: Entity.h:56
const EntityReference * toEntityReference(const Node *node)
Casts a generic node to an EntityReference node.
Definition: Entity.h:202
const EntityReference & toEntityReference(const Node &node)
Casts a generic node to an EntityReference node.
Definition: Entity.h:220
An entity reference XML node.
Definition: Entity.h:136
const Pt::String & publicId() const
Returns the public ID of the entity.
Definition: Entity.h:86
EntityReference()
Creates an EntityReference object.
Definition: Entity.h:142
Pt::String & name()
Returns the name of the entity this reference refers to.
Definition: Entity.h:162
void setSystemId(const Pt::String &sysId)
Sets the system ID of the entity.
Definition: Entity.h:101
An entity declaration in a DTD.
Definition: Entity.h:44
Unicode capable basic_string.
Definition: String.h:42
const Pt::String & systemId() const
Returns the system ID of the entity.
Definition: Entity.h:96
void clear()
Clears all content.
Definition: Entity.h:149
const Pt::String & notationName() const
Returns the name of the notation.
Definition: Entity.h:119
XML document node.
Definition: Node.h:50
bool isExternal() const
Returns true if the entity in external.
Definition: Entity.h:61
bool isInternal() const
Returns true if the entity in internal.
Definition: Entity.h:66
const Pt::String & value() const
Returns the value of the entity.
Definition: Entity.h:71
void setValue(const Pt::String &val)
Sets the value of the entity.
Definition: Entity.h:81