TreeHTML.h
1 /*
2 
3 MIT License
4 
5 Copyright (c) 2017 FMI Open Development / Markus Peura, first.last@fmi.fi
6 
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software and associated documentation files (the "Software"), to deal
9 in the Software without restriction, including without limitation the rights
10 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 copies of the Software, and to permit persons to whom the Software is
12 furnished to do so, subject to the following conditions:
13 
14 The above copyright notice and this permission notice shall be included in all
15 copies or substantial portions of the Software.
16 
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 SOFTWARE.
24 
25 */
26 /*
27 Part of Rack development has been done in the BALTRAD projects part-financed
28 by the European Union (European Regional Development Fund and European
29 Neighbourhood Partnership Instrument, Baltic Sea Region Programme 2007-2013)
30 */
31 /*
32  * TreeXML.h
33  *
34  * Created on: Jun 24, 2012
35  * Author: mpeura
36  */
37 
38 
39 
40 #ifndef TREE_HTML
41 #define TREE_HTML
42 
43 #include <ostream>
44 
45 #include "FileInfo.h"
46 #include "ReferenceMap.h"
47 #include "TreeXML.h"
48 
49 namespace drain {
50 
51 
52 class NodeHTML;
53 
54 // typedef drain::UnorderedMultiTree<NodeHTML,false, NodeXML<>::path_t> TreeHTML;
55 
56 struct BaseHTML {
57 
58  enum tag_t {
59  UNDEFINED=NodeXML<>::UNDEFINED,
60  COMMENT=NodeXML<>::COMMENT,
61  CTEXT=NodeXML<>::CTEXT,
62  SCRIPT=NodeXML<>::SCRIPT,
63  STYLE=NodeXML<>::STYLE,
64  STYLE_SELECT=NodeXML<>::STYLE_SELECT,
65  HTML,
66  HEAD, BASE, LINK, META, TITLE,
67  BODY, A, BR, CAPTION, DIV, H1, H2, H3, HR, IMG, LI, OL, P, SPAN, TABLE, TR, TH, TD, UL};
68 
69  // check CTEXT, maybe implement in XML
70  // typedef NodeHTML xml_node_t;
71  // typedef TreeHTML tree_t;
72 
73 };
74 
75 
79 //class NodeHTML : public BaseHTML, public NodeXML<BaseHTML::tag_t> {
80 class NodeHTML : public BaseHTML, public NodeXML<BaseHTML::tag_t> {
81 //class NodeHTML : public NodeXML<BaseHTML::tag_t> {
82 
83 
84 public:
85 
86  // typedef int tag_t;
87 
89  NodeHTML(const tag_t & t = tag_t(0));
90 
92  NodeHTML(const NodeHTML & node);
93 
94  inline
95  ~NodeHTML(){};
96 
97 
98 
99  /*
100  inline
101  NodeHTML & operator=(const std::initializer_list<std::pair<const char *,const drain::Variable> > &l){
102  set(l);
103  return *this;
104  }
105  */
106  inline
107  NodeHTML & operator=(const NodeHTML & node){
108  return XML::xmlAssignNode(*this, node);
109  }
110 
111 
112  template <class T>
113  inline
114  NodeHTML & operator=(const T & s){
115  set(s); // XML
116  return *this;
117  }
118 
119 
120  /*
121  virtual
122  bool isSelfClosing() const;
123  */
124 
125  static inline
126  std::ostream & docToStream(std::ostream &ostr, const xml_tree_t & tree){
127  ostr << "<!DOCTYPE html>\n";
128  toStream(ostr, tree);
129  return ostr;
130  }
131 
132 
133  static
134  const FileInfo fileInfo;
135 
136 protected:
137 
138  virtual
139  void handleType(const tag_t &t) override final;
140 
141 };
142 
143 
145 
148 typedef NodeHTML::xml_tree_t TreeHTML;
149 
150 template <>
151 inline
152 //TreeHTML & TreeHTML::operator=(std::initializer_list<std::pair<const char *,const char *> > l){
153 TreeHTML & TreeHTML::operator=(std::initializer_list<std::pair<const char *,const Variable> > l){
154  return XML::xmlAssign(*this, l);
155 }
156 
157 template <>
158 template <class T>
159 inline
160 TreeHTML & TreeHTML::operator=(const T & arg){
161  return XML::xmlAssign(*this, arg);
162 }
163 
164 
165 inline
166 std::ostream & operator<<(std::ostream &ostr, const NodeHTML & node){
167  return drain::Sprinter::toStream(ostr, node.getAttributes());
168 }
169 
170 
171 /*
172 inline
173 std::ostream & operator<<(std::ostream &ostr, const TreeHTML & tree){
174  //ostr << "<!DOCTYPE html>\n";
175  //return drain::NodeXML<>::docToStream(ostr, tree);
176  drain::NodeHTML::toStream(ostr, tree);
177  return ostr;
178 }
179 */
180 
181 
182 
183 template <>
184 bool NodeXML<BaseHTML::tag_t>::isSelfClosing() const;
185 
186 
187 DRAIN_TYPENAME(NodeHTML);
188 DRAIN_TYPENAME(BaseHTML::tag_t);
189 
190 
193 template <>
194 inline
195 TreeHTML & TreeHTML::addChild(const TreeHTML::key_t & key){
196  return XML::xmlAddChild(*this, key);
197 }
198 
199 template <>
200 const NodeXML<BaseHTML::tag_t>::xml_default_elem_map_t NodeXML<BaseHTML::tag_t>::xml_default_elems;
201 
202 
203 template <>
204 template <>
205 inline
206 TreeHTML & TreeHTML::operator()(const BaseHTML::tag_t & type){
207  // this->data.setType(type);
208  // return *this;
209  return XML::xmlSetType(*this, type);
210 }
211 
212 
213 // UTILS... consider to XML ?
214 
216 
217 public:
218 
220 
223  static
224  drain::TreeHTML & initHtml(drain::TreeHTML & html, const std::string & key = "");
225 
226 
227 
228  static inline
229  drain::TreeHTML & getFirstElem(drain::TreeHTML & elem, drain::BaseHTML::tag_t tagType){
230  if (elem.hasChildren()){
231  return elem.getChildren().begin()->second; // last
232  }
233  else {
234  //
235  return elem.addChild()(tagType); // addChild(elem, tagType);
236  }
237  }
238 
239 
240 
242 
246  static // compare with TreeHTML::addChild( - is needed?
247  drain::TreeHTML & addChild(drain::TreeHTML & elem, drain::BaseHTML::tag_t tagType, const std::string & key);
248 
249  template <class T>
250  static inline
251  drain::TreeHTML & appendElem(drain::TreeHTML & elem, drain::BaseHTML::tag_t tagType, const T & arg){
252  drain::TreeHTML & child = elem.addChild()(tagType); // addChild(elem,tagType);
253  child = arg;
254  return child;
255  };
256 
257 
258  template <class T, class ...TT>
259  static inline
260  drain::TreeHTML & appendElem(drain::TreeHTML & elem, drain::BaseHTML::tag_t tagType, const T & arg, const TT & ...args) {
261  appendElem(elem, tagType, arg);
262  return appendElem(elem, tagType, args...);
263  }
264 
265  static inline
266  drain::TreeHTML & createTable(drain::TreeHTML & body, const std::list<std::string> & columnTitles){ // "var", "ref", no whitespace.
267 
268  drain::TreeHTML & table = body.addChild()(drain::NodeHTML::TABLE); // drain::TreeUtilsHTML::addChild(body, drain::NodeHTML::TABLE);
269 
270  drain::TreeHTML & tr = table["header"](drain::NodeHTML::TR);
271 
272  for (const auto & title: columnTitles){
273  drain::TreeHTML & th = tr[title](drain::NodeHTML::TH);
274  th = title;
275  }
276 
277  return table;
278  }
279 
281 
284  template <class T>
285  static
286  // drain::TreeHTML & fillTableRow(drain::TreeHTML & table, drain::TreeHTML & tr, const std::string value = "");
287  drain::TreeHTML & fillTableRow(drain::TreeHTML & table, drain::TreeHTML & tr, const T & value){
288 
289  for (const auto & entry: table.getChildren()){
290  // Using keys of the first row, create a new row. Often, it is the title row (TH elements).
291  for (const auto & e: entry.second.getChildren()){
292  tr[e.first]->setType(drain::NodeHTML::TD);
293  tr[e.first] = value;
294  }
295  // Return after investigating the first row:
296  return tr;
297  }
298 
299  // If table is empty, also tr is.
300  return tr;
301 
302  }
303 
304  static
305  drain::TreeHTML & addTableRow(drain::TreeHTML & table, const std::string value = ""){
306  drain::TreeHTML & tr = table.addChild()(BaseHTML::TR); // addChild(table, BaseHTML::TR);
307  return fillTableRow(table, tr, value);
308  }
309 
310 
311 protected:
312 
313  // Dummy end... TODO: redesign logic, perhaps addChild();
314  template <class T>
315  static inline
316  drain::TreeHTML & appendElem(drain::TreeHTML & elem, drain::BaseHTML::tag_t tagType){
317  if (elem.hasChildren()){
318  return elem.getChildren().rbegin()->second; // last
319  }
320  else {
321  //
322  return elem;
323  }
324  }
325 
326 };
327 
328 /*
329 template <>
330 template <>
331 inline
332 TreeHTML & TreeHTML::operator()(const std::string & text){
333  this->data.ctext = text;
334  return *this;
335 }
336 */
337 
338 
339 
340 
341 } // drain::
342 
343 #endif /* TREEXML_H_ */
344 
Definition: FileInfo.h:48
Definition: TreeHTML.h:80
NodeHTML(const tag_t &t=tag_t(0))
Default constructor.
Definition: TreeHTML.cpp:165
virtual void handleType(const tag_t &t) override final
Internal function called after setType()
Definition: TreeHTML.cpp:180
Definition: TreeXML.h:341
static std::ostream & toStream(std::ostream &ostr, const V &t, const std::string &defaultTag="", int indent=0)
"Forward definition" of Tree::toOstream
static std::ostream & toStream(std::ostream &ostr, const std::initializer_list< T > &x, const SprinterLayout &layout=defaultLayout)
New (experimental)
Definition: Sprinter.h:420
Definition: TreeHTML.h:215
static drain::TreeHTML & addChild(drain::TreeHTML &elem, drain::BaseHTML::tag_t tagType, const std::string &key)
Add element of given type. The path key is generated automatically, unless given.
Definition: TreeHTML.cpp:287
static drain::TreeHTML & initHtml(drain::TreeHTML &html, const std::string &key="")
Initialize a HTML object with "head" (including "title", "style") and "body" elements.
Definition: TreeHTML.cpp:262
static drain::TreeHTML & fillTableRow(drain::TreeHTML &table, drain::TreeHTML &tr, const T &value)
Creates a new table row (TD) using first row as a template.
Definition: TreeHTML.h:287
static X & xmlAssignNode(X &dst, const X &src)
Assign another tree structure to another.
Definition: XML.h:497
static TX & xmlSetType(TX &tree, const typename TX::node_data_t::xml_tag_t &type)
Definition: XML.h:559
static T & xmlAddChild(T &tree, const std::string &key)
Definition: XML.h:573
static TX & xmlAssign(TX &dst, const TX &src)
Assign another tree structure to another.
Definition: XML.h:458
Definition: DataSelector.cpp:1277
NodeHTML::xml_tree_t TreeHTML
The HTML data structure.
Definition: TreeHTML.h:148
Definition: TreeHTML.h:56