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

Go to the SVN repository for this file.

1 #ifndef HTML___PAGE__HPP
2 #define HTML___PAGE__HPP
3 
4 /* $Id: page.hpp 92931 2021-02-23 19:54:30Z 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 page.hpp
34 /// The HTML page.
35 ///
36 /// Defines class to generate HTML code from template file.
37 
38 
39 #include <corelib/ncbistd.hpp>
40 #include <corelib/ncbifile.hpp>
41 #include <corelib/ncbi_limits.hpp>
42 #include <html/html_exception.hpp>
43 #include <html/html.hpp>
44 #include <html/nodemap.hpp>
45 
46 
47 /** @addtogroup HTMLcomp
48  *
49  * @{
50  */
51 
52 
54 
55 
56 // Forward declarations.
57 class CCgiApplication;
58 
59 
60 /////////////////////////////////////////////////////////////////////////////
61 ///
62 /// CPageStat --
63 ///
64 /// Page information used for tracking and logging: hit ID, design ID etc.
65 /// Contains pairs of name+value strings.
66 /// Elements of page stat are included in the page as meta tags in place
67 /// of <@NCBI_PAGE_STAT@> tag.
68 
70 {
71 public:
72  CPageStat(void) {}
73  ~CPageStat(void) {}
74 
75  /// Get value by name. Return empty string if the name is unknown.
76  const string& GetValue(const string& name) const;
77  /// Set new value for the name. If the value is empty, delete the
78  /// element completely.
79  void SetValue(const string& name, const string& value);
80  /// Remove all entries
81  void Clear(void) { m_Data.clear(); }
82 
84 
85  /// Return the whole internal string map (read-only).
86  const TData& GetData(void) const { return m_Data; }
87 
88 private:
89  // Prohibit copying
92 
94 };
95 
96 
97 /////////////////////////////////////////////////////////////////////////////
98 ///
99 /// CHTMLBasicPage --
100 ///
101 /// The virtual base class.
102 ///
103 /// The main functionality is the turning on and off of sub HTML components
104 /// via style bits and a creation function that orders sub components on
105 /// the page. The ability to hold children and print HTML is inherited from
106 /// CHTMLNode.
107 
109 {
110  /// Parent class.
113 
114 public:
115  /// Default constructor.
116  CHTMLBasicPage(void);
117 
118  /// Constructor.
119  CHTMLBasicPage(CCgiApplication* app, int style = 0);
120 
121  /// Dectructor.
122  virtual ~CHTMLBasicPage(void);
123 
124  virtual CCgiApplication* GetApplication(void) const;
125  virtual void SetApplication(CCgiApplication* App);
126 
127  int GetStyle(void) const;
128  void SetStyle(int style);
129 
130  /// Resolve <@XXX@> tag.
131  virtual CNCBINode* MapTag(const string& name);
132 
133  /// Add tag resolver.
134  virtual void AddTagMap(const string& name, BaseTagMapper* mapper);
135  virtual void AddTagMap(const string& name, CNCBINode* node);
136 
137  /// Get CPageStat used to create meta-tags (design ID, hit ID etc.)
138  const CPageStat& GetPageStat(void) const { return m_PageStat; }
139  /// Get editable CPageStat object
140  CPageStat& SetPageStat(void) { return m_PageStat; }
141 
142 protected:
143  CCgiApplication* m_CgiApplication; ///< Pointer to runtime information
144  int m_Style;
145  TMode m_PrintMode; ///< Current print mode
146  ///< (used by RepeatHook).
147 
148  /// Tag resolvers (as registered by AddTagMap).
151 };
152 
153 
154 /////////////////////////////////////////////////////////////////////////////
155 ///
156 /// CHTMLPage --
157 ///
158 /// This is the basic 3 section NCBI page.
159 
161 {
162  /// Parent class.
164 
165 public:
166  /// Style flags.
167  enum EFlags {
168  fNoTITLE = 0x1,
169  fNoVIEW = 0x2,
170  fNoTEMPLATE = 0x4
171  };
172  /// Binary AND of "EFlags".
173  typedef int TFlags;
174 
175  /// Constructors.
176  CHTMLPage(const string& title = kEmptyStr);
177  CHTMLPage(const string& title, const string& template_file);
178  CHTMLPage(const string& title,
179  const void* template_buffer, size_t size);
180  CHTMLPage(const string& title, istream& template_stream);
181  // HINT: use SetTemplateString to read the page from '\0'-terminated string
182 
184  TFlags style = 0,
185  const string& title = kEmptyStr,
186  const string& template_file = kEmptyStr);
187 
188  static CHTMLBasicPage* New(void);
189 
190  /// Create the individual sub pages.
191  virtual void CreateSubNodes(void);
192 
193  /// Create the static part of the page
194  /// (here - read it from <m_TemplateFile>).
195  virtual CNCBINode* CreateTemplate(CNcbiOstream* out = 0,
196  TMode mode = eHTML);
197 
198  /// Tag substitution callbacks.
199  virtual CNCBINode* CreateTitle(void); // def for tag "@TITLE@" - <m_Title>
200  virtual CNCBINode* CreateView(void); // def for tag "@VIEW@" - none
201 
202  /// To set title or template outside(after) the constructor.
203  void SetTitle(const string& title);
204 
205  /// Set source which contains the template.
206  ///
207  /// Each function assign new template source and annihilate any other.
208  /// installed before.
209  void SetTemplateFile (const string& template_file);
210  void SetTemplateString(const char* template_string);
211  void SetTemplateBuffer(const void* template_buffer, size_t size);
212  void SetTemplateStream(istream& template_stream);
213 
214  /// Interface for a filter, which must be passed to one the
215  /// LoadTemplateLib methods to select relevant parts of the loaded
216  /// template library.
217  ///
218  /// The TestAttribute() method is called for each attribute test
219  /// defined in the template library.
220  typedef class CTemplateLibFilter
221  {
222  public:
223  /// This method is called by LoadTemplateLib methods to check
224  /// whether a template within library should be loaded.
225  /// If the method returns true, the template is loaded,
226  /// otherwise it's skipped.
227  virtual bool TestAttribute(
228  const string& attr_name,
229  const string& test_pattern) = 0;
230 
231  virtual ~CTemplateLibFilter() {}
232  } TTemplateLibFilter;
233 
234  /// Load template library.
235  ///
236  /// Automatically map all sub-templates from the loaded library.
237  void LoadTemplateLibFile (const string& template_file,
238  TTemplateLibFilter* filter = NULL);
239  void LoadTemplateLibString(const char* template_string,
240  TTemplateLibFilter* filter = NULL);
241  void LoadTemplateLibBuffer(const void* template_buffer, size_t size,
242  TTemplateLibFilter* filter = NULL);
243  void LoadTemplateLibStream(istream& template_stream,
244  TTemplateLibFilter* filter = NULL);
245 
246  /// Template file caching state.
248  eCTF_Enable, ///< Enable caching
249  eCTF_Disable, ///< Disable caching
250  eCTF_Default = eCTF_Disable
251  };
252 
253  /// Enable/disable template caching.
254  ///
255  /// If caching enabled that all template and template libraries
256  /// files, loaded by any object of CHTMLPage will be read from disk
257  /// only once.
258  static void CacheTemplateFiles(ECacheTemplateFiles caching);
259 
260  /// Tag mappers.
261  virtual void AddTagMap(const string& name, BaseTagMapper* mapper);
262  virtual void AddTagMap(const string& name, CNCBINode* node);
263 
264  // Overridden to reduce latency
265  CNcbiOstream& PrintChildren(CNcbiOstream& out, TMode mode);
266 
267 private:
268  void Init(void);
269 
270  /// Read template into string.
271  ///
272  /// Used by CreateTemplate() to cache templates and add
273  /// on the fly modifications into it.
274  void x_LoadTemplate(CNcbiIstream& is, string& str);
275 
276  /// Create and print template.
277  ///
278  /// Calls by CreateTemplate() only when it can create and print
279  /// template at one time, to avoid latency on large templates.
280  /// Otherwise x_ReadTemplate() will be used.
281  CNCBINode* x_PrintTemplate(CNcbiIstream& is, CNcbiOstream* out,
283 
284  bool x_ApplyFilters(TTemplateLibFilter* filter, const char* buffer);
285 
286  // Allow/disable processing of #include directives for template libraries.
287  // eAllowIncludes used by default for LoadTemplateLibFile().
289  eAllowIncludes, // process #include's
290  eSkipIncludes // do not process #include's
291  };
292 
293  /// Load template library.
294  ///
295  /// This is an internal version that works only with streams.
296  /// @param is
297  /// Input stream to read template from
298  /// @param size
299  /// Size of input, if known (0 otherwise).
300  /// @param includes
301  /// Defines to process or not #include directives.
302  /// Used only for loading template libraries from files
303  /// @param file_name
304  /// Name of the template library file.
305  /// Used only by LoadTemplateLibFile().
306  /// @sa
307  /// LoadTemplateLibFile(), LoadTemplateLibString(),
308  /// LoadTemplateLibBuffer(), LoadTemplateLibStream()
309  void x_LoadTemplateLib(CNcbiIstream& is, size_t size /*= 0*/,
310  ETemplateIncludes includes /*= eSkipIncludes*/,
311  const string& file_name /*= kEmptyStr*/,
312  TTemplateLibFilter* filter);
313 
314 private:
315  /// Generate page internal name on the base of template source.
316  /// Debug function used at output tag trace on exception.
317  void GeneratePageInternalName(const string& template_src);
318 
319 private:
320  string m_Title; ///< Page title
321 
322  /// Template sources.
323  string m_TemplateFile; ///< File name
324  istream* m_TemplateStream; ///< Stream
325  const void* m_TemplateBuffer; ///< Some buffer
326  size_t m_TemplateSize; ///< Size of input, if known (0 otherwise)
327 
329 };
330 
331 
332 /* @} */
333 
334 
335 /////////////////////////////////////////////////////////////////////////////
336 //
337 // IMPLEMENTATION of INLINE functions
338 //
339 /////////////////////////////////////////////////////////////////////////////
340 
341 
342 //
343 // CHTMLBasicPage::
344 //
345 
347 {
348  return m_CgiApplication;
349 }
350 
351 
352 inline int CHTMLBasicPage::GetStyle(void) const
353 {
354  return m_Style;
355 }
356 
357 
358 
359 //
360 // CHTMLPage::
361 //
362 
364 {
365  return new CHTMLPage;
366 }
367 
368 
369 inline void CHTMLPage::SetTitle(const string& title)
370 {
371  m_Title = title;
372 }
373 
374 
375 inline void CHTMLPage::SetTemplateString(const char* template_string)
376 {
378  m_TemplateStream = 0;
379  m_TemplateBuffer = template_string;
380  m_TemplateSize = strlen(template_string);
382 }
383 
384 
385 inline void CHTMLPage::SetTemplateBuffer(const void* template_buffer,
386  size_t size)
387 {
389  m_TemplateStream = 0;
390  m_TemplateBuffer = template_buffer;
393 }
394 
395 
396 inline void CHTMLPage::SetTemplateStream(istream& template_stream)
397 {
399  m_TemplateStream = &template_stream;
400  m_TemplateBuffer = 0;
401  m_TemplateSize = 0;
402  GeneratePageInternalName("stream");
403 }
404 
405 
406 inline void CHTMLPage::LoadTemplateLibString(const char* template_string,
407  TTemplateLibFilter* filter)
408 {
409  istringstream is(template_string);
410  size_t size = strlen(template_string);
412 }
413 
414 
415 inline void CHTMLPage::LoadTemplateLibBuffer(const void* template_buffer,
416  size_t size,
417  TTemplateLibFilter* filter)
418 {
419  istringstream is((const char*)template_buffer);
421 }
422 
423 
424 inline void CHTMLPage::LoadTemplateLibStream(istream& template_stream,
425  TTemplateLibFilter* filter)
426 {
427  x_LoadTemplateLib(template_stream, 0, eSkipIncludes, kEmptyStr, filter);
428 }
429 
430 
431 inline void CHTMLPage::GeneratePageInternalName(const string& template_src = kEmptyStr)
432 {
433  m_Name = "htmlpage";
434  if ( !template_src.empty() ) {
435  m_Name += "(" + template_src + ")";
436  }
437 }
438 
439 
441 
442 #endif /* HTML___PAGE__HPP */
CHTMLBasicPage –.
Definition: page.hpp:109
Interface for a filter, which must be passed to one the LoadTemplateLib methods to select relevant pa...
Definition: page.hpp:221
CHTMLPage –.
Definition: page.hpp:161
CPageStat –.
Definition: page.hpp:70
Include a standard set of the NCBI C++ Toolkit most basic headers.
const char * file_name[]
std::ofstream out("events_result.xml")
main entry point for tests
static void Init(void)
Definition: cursor6.c:76
static const char * str(char *buf, int n)
Definition: stats.c:84
#define NULL
Definition: ncbistd.hpp:225
int TFlags
Binary AND of "EFlags".
Definition: page.hpp:173
CPageStat(void)
Definition: page.hpp:72
class CHTMLPage::CTemplateLibFilter TTemplateLibFilter
Interface for a filter, which must be passed to one the LoadTemplateLib methods to select relevant pa...
ETemplateIncludes
Definition: page.hpp:288
ECacheTemplateFiles
Template file caching state.
Definition: page.hpp:247
map< string, BaseTagMapper * > TTagMap
Definition: page.hpp:112
void LoadTemplateLibString(const char *template_string, TTemplateLibFilter *filter=NULL)
Definition: page.hpp:406
int GetStyle(void) const
Definition: page.hpp:352
TMode m_PrintMode
Current print mode (used by RepeatHook).
Definition: page.hpp:145
size_t m_TemplateSize
Size of input, if known (0 otherwise)
Definition: page.hpp:326
void SetTitle(const string &title)
To set title or template outside(after) the constructor.
Definition: page.hpp:369
CCgiApplication * m_CgiApplication
Pointer to runtime information.
Definition: page.hpp:143
void Clear(void)
Remove all entries.
Definition: page.hpp:81
void x_LoadTemplateLib(CNcbiIstream &is, size_t size, ETemplateIncludes includes, const string &file_name, TTemplateLibFilter *filter)
Load template library.
Definition: page.cpp:546
TData m_Data
Definition: page.hpp:93
CHTMLPage(const string &title=kEmptyStr)
Constructors.
Definition: page.cpp:200
void SetTemplateBuffer(const void *template_buffer, size_t size)
Definition: page.hpp:385
static CHTMLBasicPage * New(void)
Definition: page.hpp:363
CPageStat m_PageStat
Definition: page.hpp:150
CPageStat & SetPageStat(void)
Get editable CPageStat object.
Definition: page.hpp:140
void SetTemplateStream(istream &template_stream)
Definition: page.hpp:396
const TData & GetData(void) const
Return the whole internal string map (read-only).
Definition: page.hpp:86
const void * m_TemplateBuffer
Some buffer.
Definition: page.hpp:325
TTagMap m_TagMap
Tag resolvers (as registered by AddTagMap).
Definition: page.hpp:149
void SetTemplateString(const char *template_string)
Definition: page.hpp:375
void LoadTemplateLibBuffer(const void *template_buffer, size_t size, TTemplateLibFilter *filter=NULL)
Definition: page.hpp:415
void LoadTemplateLibStream(istream &template_stream, TTemplateLibFilter *filter=NULL)
Definition: page.hpp:424
CPageStat(const CPageStat &)
void GeneratePageInternalName(const string &template_src)
Generate page internal name on the base of template source.
Definition: page.hpp:431
const CPageStat & GetPageStat(void) const
Get CPageStat used to create meta-tags (design ID, hit ID etc.)
Definition: page.hpp:138
CHTMLPage(const string &title, const void *template_buffer, size_t size)
string m_Name
Node name.
Definition: node.hpp:265
string m_Title
Page title.
Definition: page.hpp:320
virtual bool TestAttribute(const string &attr_name, const string &test_pattern)=0
This method is called by LoadTemplateLib methods to check whether a template within library should be...
~CPageStat(void)
Definition: page.hpp:73
virtual CCgiApplication * GetApplication(void) const
Definition: page.hpp:346
istream * m_TemplateStream
Stream.
Definition: page.hpp:324
CPageStat & operator=(const CPageStat &)
CHTMLBasicPage CParent
Parent class.
Definition: page.hpp:163
CNCBINode CParent
Parent class.
Definition: page.hpp:111
EFlags
Style flags.
Definition: page.hpp:167
map< string, string > TData
Definition: page.hpp:83
static ECacheTemplateFiles sm_CacheTemplateFiles
Definition: page.hpp:328
string m_TemplateFile
Template sources.
Definition: page.hpp:323
@ eAllowIncludes
Definition: page.hpp:289
@ eSkipIncludes
Definition: page.hpp:290
@ eCTF_Enable
Enable caching.
Definition: page.hpp:248
@ eCTF_Disable
Disable caching.
Definition: page.hpp:249
#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
IO_PREFIX::istream CNcbiIstream
Portable alias for istream.
Definition: ncbistre.hpp:146
#define kEmptyStr
Definition: ncbistr.hpp:123
#define NCBI_XHTML_EXPORT
Definition: ncbi_export.h:1139
@ eHTML
HTML tag or, for example, HTML entity.
Definition: unicode.hpp:57
HTML classes.
HTML library exceptions.
mdb_mode_t mode
Definition: lmdb++.h:38
const struct ncbi::grid::netcache::search::fields::SIZE size
const GenericPointer< typename T::ValueType > T2 value
Definition: pointer.h:1227
Defines classes: CDirEntry, CFile, CDir, CSymLink, CMemoryFile, CFileUtil, CFileLock,...
Various tag mappers classes.
static uint8_t * buffer
Definition: pcre2test.c:1016
static void SetTitle(CRef< CSeq_entry > entry, string title)
Modified on Fri Sep 20 14:57:20 2024 by modify_doxy.py rev. 669887