NCBI C++ ToolKit
node.hpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 #ifndef HTML___NODE__HPP
2 #define HTML___NODE__HPP
3 
4 /* $Id: node.hpp 77708 2017-05-03 12:47:52Z ivanov $
5  * ===========================================================================
6  *
7  * PUBLIC DOMAIN NOTICE
8  * National Center for Biotechnology Information
9  *
10  * This software/database is a "United States Government Work" under the
11  * terms of the United States Copyright Act. It was written as part of
12  * the author's official duties as a United States Government employee and
13  * thus cannot be copyrighted. This software/database is freely available
14  * to the public for use. The National Library of Medicine and the U.S.
15  * Government have not placed any restriction on its use or reproduction.
16  *
17  * Although all reasonable efforts have been taken to ensure the accuracy
18  * and reliability of the software and data, the NLM and the U.S.
19  * Government do not and cannot warrant the performance or results that
20  * may be obtained by using this software or data. The NLM and the U.S.
21  * Government disclaim all warranties, express or implied, including
22  * warranties of performance, merchantability or fitness for any particular
23  * purpose.
24  *
25  * Please cite the author in any work or product based on this material.
26  *
27  * ===========================================================================
28  *
29  * Author: Lewis Geer
30  *
31  */
32 
33 /// @file node.hpp
34 /// The standard node class.
35 
36 
37 #include <corelib/ncbistd.hpp>
38 #include <corelib/ncbiobj.hpp>
39 #include <map>
40 #include <list>
41 #include <memory>
42 
43 
44 /** @addtogroup HTMLcomp
45  *
46  * @{
47  */
48 
49 
51 
52 
53 class CNCBINode;
55 //#define NCBI_LIGHTWEIGHT_LIST 1
56 
57 // Base class for a graph node.
59 {
60 public:
61  friend class CRef<CNCBINode>;
62  typedef list<CNodeRef> TChildren;
63 #if NCBI_LIGHTWEIGHT_LIST
64  typedef TChildren TChildrenMember;
65 #else
66  typedef unique_ptr<TChildren> TChildrenMember;
67 #endif
69  {
71  : m_Optional(true)
72  {
73  return;
74  }
75  SAttributeValue(const string& value, bool optional)
76  : m_Value(value), m_Optional(optional)
77  {
78  return;
79  }
81  {
82  m_Value = value;
83  m_Optional = true;
84  return *this;
85  }
86  const string& GetValue(void) const
87  {
88  return m_Value;
89  }
90  operator const string&(void) const
91  {
92  return m_Value;
93  }
94  bool IsOptional(void) const
95  {
96  return m_Optional;
97  }
98  void SetOptional(bool optional = true)
99  {
100  m_Optional = optional;
101  }
102  private:
103  string m_Value;
105  };
107 
108  enum EMode {
109  eHTML = 0,
110  ePlainText = 1,
111  eXHTML = 2
112  };
113 
114  class TMode {
115  public:
117  : m_Mode(mode), m_Node(0), m_Previous(0)
118  {
119  return;
120  }
121  TMode(int mode)
122  : m_Mode(EMode(mode)), m_Node(0), m_Previous(0)
123  {
124  return;
125  }
126  TMode(const TMode* mode, CNCBINode* node)
127  : m_Mode(mode->m_Mode), m_Node(node), m_Previous(mode)
128  {
129  return;
130  }
131  operator EMode(void) const
132  {
133  return m_Mode;
134  }
135  bool operator==(EMode mode) const
136  {
137  return mode == m_Mode;
138  }
139 
140  CNCBINode* GetNode(void) const
141  {
142  return m_Node;
143  }
144  const TMode* GetPreviousContext(void) const
145  {
146  return m_Previous;
147  }
148  private:
149  // to avoid allocation in
153  };
154 
155  // 'structors
156  CNCBINode(void);
157  CNCBINode(const string& name);
158  CNCBINode(const char* name);
159  virtual ~CNCBINode();
160 
161  // Add a Node * to the end of m_Children.
162  // Returns 'this' for chained AppendChild().
165 
166  // Remove all occurencies of the child from this node's subtree
167  // (along with its subtree).
168  // Throw exception if the child is not found.
169  // Return smart pointer to the removed child node.
170  CNodeRef RemoveChild(CNCBINode* child);
171  CNodeRef RemoveChild(CNodeRef& child);
172  void RemoveAllChildren(void);
173 
174  // All child operations (except AppendChild) are valid only if
175  // have children return true
176  bool HaveChildren(void) const;
178  const TChildren& Children(void) const;
179  TChildren::iterator ChildBegin(void);
180  TChildren::iterator ChildEnd(void);
181  static CNCBINode* Node(TChildren::iterator i);
182  TChildren::const_iterator ChildBegin(void) const;
183  TChildren::const_iterator ChildEnd(void) const;
184  static const CNCBINode* Node(TChildren::const_iterator i);
185 
187  virtual CNcbiOstream& PrintBegin(CNcbiOstream& out, TMode mode);
188  virtual CNcbiOstream& PrintChildren(CNcbiOstream& out, TMode mode);
189  virtual CNcbiOstream& PrintEnd(CNcbiOstream& out, TMode mode);
190 
191  void SetRepeatCount(size_t count = 0);
192  size_t GetRepeatCount(void);
193 
194  // This method will be called once before Print().
195  virtual void CreateSubNodes(void);
196  // Call CreateSubNodes() if it's not called yet.
197  void Initialize(void);
198  // Reinitialize node, so hierarhy can be created anew.
199  // All previously set attributes remains unchanged.
200  // On the next Print() the CreateSubNodes() method
201  // will be called again.
202  void ReInitialize(void);
203 
204  // Find and replace text with a node.
205  virtual CNCBINode* MapTag(const string& tagname);
206  CNodeRef MapTagAll(const string& tagname, const TMode& mode);
207 
208  // Repeat tag node (works only inside tag node mappers)
209  void RepeatTag(bool enable = true);
210  bool NeedRepeatTag(void);
211 
212  const string& GetName(void) const;
213 
214  bool HaveAttributes(void) const;
216  const TAttributes& Attributes(void) const;
217  // Retrieve attribute.
218  bool HaveAttribute(const string& name) const;
219  const string& GetAttribute(const string& name) const;
220  bool AttributeIsOptional(const string& name) const;
221  bool AttributeIsOptional(const char* name) const;
222  void SetAttributeOptional(const string& name, bool optional = true);
223  void SetAttributeOptional(const char* name, bool optional = true);
224  const string* GetAttributeValue(const string& name) const;
225 
226  // Set attribute.
227  void SetAttribute(const string& name, const string& value);
228  void SetAttribute(const string& name);
229  void SetAttribute(const string& name, int value);
230  void SetOptionalAttribute(const string& name, const string& value);
231  void SetOptionalAttribute(const string& name, bool set);
232 
233  void SetAttribute(const char* name, const string& value);
234  void SetAttribute(const char* name);
235  void SetAttribute(const char* name, int value);
236  void SetOptionalAttribute(const char* name, const string& value);
237  void SetOptionalAttribute(const char* name, bool set);
238 
239  // Exception handling.
240 
241  /// Flags defining how to catch and process exceptions.
242  /// By default flags are unsettled.
243  /// Note that without the fCatchAll flag only CHTMLExceptions and
244  /// all derived exceptons can be traced.
246  fAddTrace = 0x1, ///< Enable tag trace.
247  fCatchAll = 0x2, ///< Catch all other exceptions and
248  ///< rethrow CHTMLException.
249  fDisableCheckRecursion = 0x4 ///< Disable to throw exception if
250  ///< nodes tree have endless recursion.
251  };
252  typedef int TExceptionFlags; ///< Binary OR of "EExceptionFlags"
253 
254  // Set/get global exception handling flags.
255  static void SetExceptionFlags(TExceptionFlags flags);
256  static TExceptionFlags GetExceptionFlags(void);
257 
258 protected:
259  virtual void DoAppendChild(CNCBINode* child);
260  virtual void DoSetAttribute(const string& name,
261  const string& value, bool optional);
262 
264  TChildrenMember m_Children; ///< Child nodes
265  string m_Name; ///< Node name
266  size_t m_RepeatCount; ///< How many times repeat node
267 
268  // Repeat tag flag (used only inside tag node mappers hooks). See RepeatTag().
269  bool m_RepeatTag;
270 
271  // Attributes, e.g. href="link.html"
272  unique_ptr<TAttributes> m_Attributes;
273 
274 private:
275  // To prevent copy constructor.
276  CNCBINode(const CNCBINode& node);
277  // To prevent assignment operator.
279 
280  // Return children list (create if needed).
282  // Return attributes map (create if needed).
284 };
285 
286 
287 // Inline functions are defined here:
288 #include <html/node.inl>
289 
290 
292 
293 
294 /* @} */
295 
296 #endif /* HTML___NODE__HPP */
CObject –.
Definition: ncbiobj.hpp:180
Definition: map.hpp:338
Definition: set.hpp:45
void Print(const CCompactSAMApplication::AlignInfo &ai)
Include a standard set of the NCBI C++ Toolkit most basic headers.
static uch flags
std::ofstream out("events_result.xml")
main entry point for tests
#define true
Definition: bool.h:35
TMode(const TMode *mode, CNCBINode *node)
Definition: node.hpp:126
EMode m_Mode
Definition: node.hpp:150
CNCBINode * m_Node
Definition: node.hpp:151
size_t m_RepeatCount
How many times repeat node.
Definition: node.hpp:266
bool HaveChildren(void) const
CNCBINode(const CNCBINode &node)
TMode(EMode mode=eHTML)
Definition: node.hpp:116
map< string, SAttributeValue, PNocase > TAttributes
Definition: node.hpp:106
const TMode * GetPreviousContext(void) const
Definition: node.hpp:144
list< CNodeRef > TChildren
Definition: node.hpp:62
SAttributeValue & operator=(const string &value)
Definition: node.hpp:80
void SetOptionalAttribute(const char *name, bool set)
TMode(int mode)
Definition: node.hpp:121
bool HaveAttributes(void) const
CNCBINode & operator=(const CNCBINode &node)
const string & GetName(void) const
TChildren::const_iterator ChildBegin(void) const
bool m_CreateSubNodesCalled
Definition: node.hpp:263
unique_ptr< TAttributes > m_Attributes
Definition: node.hpp:272
bool m_RepeatTag
Definition: node.hpp:269
void RepeatTag(bool enable=true)
void SetOptionalAttribute(const string &name, bool set)
const TChildren & Children(void) const
const TAttributes & Attributes(void) const
void SetOptional(bool optional=true)
Definition: node.hpp:98
const string & GetValue(void) const
Definition: node.hpp:86
TChildren & GetChildren(void)
CNCBINode * AppendChild(CNCBINode *child)
int TExceptionFlags
Binary OR of "EExceptionFlags".
Definition: node.hpp:252
void SetOptionalAttribute(const char *name, const string &value)
TChildren::iterator ChildEnd(void)
CRef< CNCBINode > CNodeRef
Definition: node.hpp:53
void SetOptionalAttribute(const string &name, const string &value)
TAttributes & Attributes(void)
TChildren & Children(void)
TAttributes & GetAttributes(void)
TChildren::iterator ChildBegin(void)
TChildrenMember m_Children
Child nodes.
Definition: node.hpp:264
static const CNCBINode * Node(TChildren::const_iterator i)
static CNCBINode * Node(TChildren::iterator i)
size_t GetRepeatCount(void)
void SetAttribute(const string &name, const string &value)
void SetRepeatCount(size_t count=0)
CNCBINode * GetNode(void) const
Definition: node.hpp:140
bool operator==(EMode mode) const
Definition: node.hpp:135
string m_Name
Node name.
Definition: node.hpp:265
EExceptionFlags
Flags defining how to catch and process exceptions.
Definition: node.hpp:245
const TMode * m_Previous
Definition: node.hpp:152
bool NeedRepeatTag(void)
TChildren::const_iterator ChildEnd(void) const
bool IsOptional(void) const
Definition: node.hpp:94
unique_ptr< TChildren > TChildrenMember
Definition: node.hpp:66
SAttributeValue(const string &value, bool optional)
Definition: node.hpp:75
CNCBINode * AppendChild(CNodeRef &ref)
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
IO_PREFIX::ostream CNcbiOstream
Portable alias for ostream.
Definition: ncbistre.hpp:149
#define NCBI_XHTML_EXPORT
Definition: ncbi_export.h:1139
@ eHTML
HTML tag or, for example, HTML entity.
Definition: unicode.hpp:57
int i
mdb_mode_t mode
Definition: lmdb++.h:38
const GenericPointer< typename T::ValueType > T2 value
Definition: pointer.h:1227
Portable reference counted smart and weak pointers using CWeakRef, CRef, CObject and CObjectEx.
Modified on Sun Apr 14 05:28:36 2024 by modify_doxy.py rev. 669887