NCBI C++ ToolKit
demo_html_template.cpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 /* $Id: demo_html_template.cpp 70170 2015-12-09 16:36:40Z ivanov $
2  * ===========================================================================
3  *
4  * PUBLIC DOMAIN NOTICE
5  * National Center for Biotechnology Information
6  *
7  * This software/database is a "United States Government Work" under the
8  * terms of the United States Copyright Act. It was written as part of
9  * the author's official duties as a United States Government employee and
10  * thus cannot be copyrighted. This software/database is freely available
11  * to the public for use. The National Library of Medicine and the U.S.
12  * Government have not placed any restriction on its use or reproduction.
13  *
14  * Although all reasonable efforts have been taken to ensure the accuracy
15  * and reliability of the software and data, the NLM and the U.S.
16  * Government do not and cannot warrant the performance or results that
17  * may be obtained by using this software or data. The NLM and the U.S.
18  * Government disclaim all warranties, express or implied, including
19  * warranties of performance, merchantability or fitness for any particular
20  * purpose.
21  *
22  * Please cite the author in any work or product based on this material.
23  *
24  * ===========================================================================
25  *
26  * Author: Vladimir Ivanov
27  *
28  * File Description: Sample of usage the HTML library templates.
29  *
30  */
31 
32 #include <ncbi_pch.hpp>
33 #include <corelib/ncbiapp.hpp>
34 #include <corelib/ncbienv.hpp>
35 #include <html/page.hpp>
36 #include <stdlib.h>
37 
39 
40 
41 //----------------------------------------------------------------------------
42 //
43 // The hook procedure to generate table rows.
44 //
45 //----------------------------------------------------------------------------
46 //
47 
48 // Simple data source.
49 // You can use here any other: files, databases...
50 
51 struct SPerson {
52  const char* name;
53  const char* phone;
54  const char* email;
55 };
56 static const SPerson s_Persons[] = {
57  { "Username 1", "111-111-1111", "name1@server" },
58  { "Username 2", "222-222-2222", "name2@server" },
59  { "Username 3", "333-333-3333", "name3@server" },
60  { "Username 4", "444-444-4444", "name4@server" },
61  { "Username 5", "555-555-5555", "name5@server" },
62  { "Username 6", "666-666-6666", "name6@server" },
63  { "Username 7", "777-777-7777", "name7@server" },
64  { "Username 8", "888-888-8888", "name8@server" },
65  { "Username 9", "999-999-9999", "name9@server" },
66  { 0, 0, 0}
67 };
68 
70 {
71  STableRowHook_Ctx(const SPerson *const x_persons)
72  : persons(x_persons), current(0) {}
73 
74  const SPerson *const persons;
75  size_t current;
76 };
77 
78 
79 // You can use hook functions with other parameters like next:
80 //
81 // static CNCBINode* s_TableRowHook(void)
82 // static CNCBINode* s_TableRowHook(const string& name)
83 // static CNCBINode* s_TableRowHook(void* data)
84 // static CNCBINode* s_TableRowHook(void* data, const string& name)
85 // static CNCBINode* s_TableRowHook(CAnyNodeClass* node)
86 // static CNCBINode* s_TableRowHook(CAnyNodeClass* node, const string& name)
87 // static CNCBINode* s_TableRowHook(CAnyNodeClass* node, TAnyType data)
88 // static CNCBINode* s_TableRowHook(CAnyNodeClass* node, TAnyType data,
89 // const string& name)
90 //
91 // <node> - contains a pointer to node called the function.
92 // <data> - allows to pass into hook-function some data.
93 // <name> - contains a name of the tag for which the functons is called.
94 // so the one mapper function can be set for few different tags.
95 //
96 // (see file nodemap.hpp, CreateTagMapper() for details)
97 //
98 
99 static CNCBINode* s_TableRowHook(CHTMLPage* page /*never NULL*/,
101 {
102  SPerson person;
103  person = ctx->persons[ctx->current];
104 
105  // Do we have something to print out?
106  if ( !person.name ) {
107  // No more data. Stop the table row dublication.
108  return 0;
109  }
110 
111  // Here we are constructing a node using an already existent
112  // template. But you can construct it yourself, using a full set
113  // of CNCBINode and its derived classes, and also an AppendChild()
114  // method.
115 
116  CNCBINode* node = new CNCBINode();
117  if ( !node ) {
118  return 0;
119  }
120  // For example, we add a HTML comment before each table row.
121  // We add CHTMLText("\n") nodes only for good look in the text mode,
122  // they will be ignored by Internet browsers.
123  node->AppendChild(new CHTMLText("\n"));
124  node->AppendChild(new CHTMLComment("Table row #"
125  + NStr::NumericToString(ctx->current+1)));
126  node->AppendChild(new CHTMLText("\n"));
127  node->AppendChild(new CHTMLTagNode("table_row_template"));
128 
129  // Define variables for the new row.
130  page->AddTagMap("name", new CHTMLText(person.name));
131  page->AddTagMap("phone", new CHTMLText(person.phone));
132  page->AddTagMap("email", new CHTMLText(person.email));
133 
134  // Decorate table rows.
135  string css_class = "colored";
136  if (ctx->current != 5) {
137  css_class = ctx->current %2 ? "even" : "odd";
138  }
139  page->AddTagMap("class", new CHTMLText(css_class));
140 
141  // Increment the hook's context row counter.
142  ctx->current++;
143 
144  // Instruct to call this hook again after printing already prepared data.
145  // By default repetition is disabled.
146  node->RepeatTag();
147 
148  // Return generated node.
149  return node;
150 }
151 
152 
153 //----------------------------------------------------------------------------
154 //
155 // The class to provide random numbers
156 //
157 //----------------------------------------------------------------------------
158 
160 {
161 public:
162  // Constructor.
164  : m_Counter(0), m_Sum(0), m_Page(page)
165  { srand((unsigned int)time(0)); }
166 
167  // Generate random number by module 10.
169  {
170  int n = rand() % 10;
171  m_Counter++;
172  m_Sum += n;
173  return n;
174  }
175 
176  // Return current counter value
178  { return m_Counter; };
179 
180  // Return accumulated sum.
181  int GetSum()
182  { return m_Sum; };
183 
184  // Return accumulated sum.
186  { return m_Page; }
187 private:
188  int m_Counter; // Just a counter.
189  int m_Sum; // Accumulated sum.
190  CHTMLPage* m_Page; // Reference to the main page.
191 };
192 
193 
194 //----------------------------------------------------------------------------
195 //
196 // Demo application class
197 //
198 //----------------------------------------------------------------------------
199 
201 {
202 public:
203  virtual int Run (void);
204 
205  // Function to generate data for number addition example.
206  //
207  // You can use hook functions with other parameters like next:
208  //
209  // CNCBINode* NumAdderHook(void)
210  // CNCBINode* NumAdderHook(const string& name)
211  // CNCBINode* NumAdderHook(TAnyType data)
212  // CNCBINode* NumAdderHook(TAnyType data, const string& name)
213  //
214  // (see file nodemap.hpp, CreateTagMapper() for details)
215  //
216 
218 };
219 
220 
222 {
223  // Generate random numbers until it sum is less 50.
224  if ( ctx->GetSum() >= 50 ) {
225  return 0;
226  }
227 
228  // Generate node to represent a generated number.
229  int n = ctx->GetRandomNumber();
230  int i = ctx->GetCounter();
231  CNCBINode* node = new CHTMLText(NStr::IntToString(i) +
232  ". Add number: " +
233  NStr::IntToString(n) + " <br>\n");
234  // Get sum value.
235  int sum = ctx->GetSum();
236 
237  // Add result if sum is above 50.
238  if ( sum >= 50) {
239  CNCBINode* sep = new CHTMLPlainText("-");
240  sep->SetRepeatCount(25);
241  node->AppendChild(sep);
242  node->AppendChild(new CHTML_br());
243  ctx->GetPage()->AddTagMap("num_sum",
244  new CHTMLText(NStr::IntToString(sum)));
245  }
246 
247  // Enable to call this hook again after printing already prepared data.
248  // By default repetition is disabled.
249  node->RepeatTag();
250 
251  // Return generated node.
252  return node;
253 }
254 
255 
257 {
258  // Create main page, that is used to compose and write out the HTML code.
259  CHTMLPage page("HTML library template demo page");
260 
261  // Set used template. The main template can be only one.
262  // Each next call of the SetTemplate*() redefine previous template.
263 
264  page.SetTemplateFile("demo_html_template.html");
265  // page.SetTemplateString(...);
266  // page.SetTemplateStream(...);
267  // page.SetTemplateBuffer(...);
268 
269  // Load template libraries. The number of loaded libraries is
270  // limited only with amount of available memory.
271 
272  page.LoadTemplateLibFile("demo_html_template.inc");
273  // page.LoadTemplateLibString(...);
274  // page.LoadTemplateLibStream(...);
275  // page.LoadTemplateLibBuffer(...);
276 
277  // Redefine some definition from already loaded template library.
278  page.AddTagMap("TITLE",
279  new CHTMLText("Sample of usage the HTML library templates"));
280 
281  // Create other necessary tags.
282  page.AddTagMap("HEADLINE",
283  new CHTMLText("Phone browser"));
284  page.AddTagMap("DATE",
285  new CHTMLText(CTime(CTime::eCurrent).AsString("M B Y, h:m")));
286 
287  // Setup static hook procedure to generate table rows
288  STableRowHook_Ctx table_ctx(s_Persons);
289  page.AddTagMap("table_row_hook", CreateTagMapper(s_TableRowHook,
290  &table_ctx));
291 
292  // We also can use any other tag mappers, such as a function with
293  // tag name parameter, or some class method as shown below.
294  CNumAdderCtx adder_ctx(&page);
296  &adder_ctx));
297 
298  // Print out the results.
299  page.Print(cout);
300 
301  // Next line added to better output look in the text mode only
302  cout << endl;
303 
304  // All done.
305  return 0;
306 }
307 
308 
309 //----------------------------------------------------------------------------
310 //
311 // Main function
312 //
313 //----------------------------------------------------------------------------
314 
315 int main(int argc, const char* argv[])
316 {
317  return CDemoApplication().AppMain(argc, argv);
318 }
virtual int Run(void)
Run the application.
CNCBINode * NumAdderHook(CNumAdderCtx *ctx)
CHTMLPage –.
Definition: page.hpp:161
CNumAdderCtx(CHTMLPage *page)
CHTMLPage * GetPage()
CTime –.
Definition: ncbitime.hpp:296
static const SPerson s_Persons[]
int main(int argc, const char *argv[])
USING_NCBI_SCOPE
static CNCBINode * s_TableRowHook(CHTMLPage *page, STableRowHook_Ctx *ctx)
CS_CONTEXT * ctx
Definition: t0006.c:12
int AppMain(int argc, const char *const *argv, const char *const *envp=0, EAppDiagStream diag=eDS_Default, const char *conf=NcbiEmptyCStr, const string &name=NcbiEmptyString)
Main function (entry point) for the NCBI application.
Definition: ncbiapp.cpp:819
virtual void AddTagMap(const string &name, BaseTagMapper *mapper)
Tag mappers.
Definition: page.cpp:289
void RepeatTag(bool enable=true)
CNCBINode * AppendChild(CNCBINode *child)
void LoadTemplateLibFile(const string &template_file, TTemplateLibFilter *filter=NULL)
Load template library.
Definition: page.cpp:748
virtual CNcbiOstream & Print(CNcbiOstream &out, TMode mode=eHTML)
Definition: node.cpp:296
void SetRepeatCount(size_t count=0)
void SetTemplateFile(const string &template_file)
Set source which contains the template.
Definition: page.cpp:459
static string IntToString(int value, TNumToStringFlags flags=0, int base=10)
Convert int to string.
Definition: ncbistr.hpp:5084
static enable_if< is_arithmetic< TNumeric >::value||is_convertible< TNumeric, Int8 >::value, string >::type NumericToString(TNumeric value, TNumToStringFlags flags=0, int base=10)
Convert numeric value to string.
Definition: ncbistr.hpp:673
BaseTagMapper * CreateTagMapper(CNCBINode *node)
Definition: nodemap.hpp:243
@ eCurrent
Use current time. See also CCurrentTime.
Definition: ncbitime.hpp:300
int i
yy_size_t n
Defines the CNcbiApplication and CAppException classes for creating NCBI applications.
Defines unified interface to application:
The HTML page.
const char * name
const char * email
const char * phone
STableRowHook_Ctx(const SPerson *const x_persons)
const SPerson *const persons
Modified on Tue Jun 18 13:40:38 2024 by modify_doxy.py rev. 669887