Loading...
Searching...
No Matches
TreeHTML.h
1/*
2
3MIT License
4
5Copyright (c) 2017 FMI Open Development / Markus Peura, first.last@fmi.fi
6
7Permission is hereby granted, free of charge, to any person obtaining a copy
8of this software and associated documentation files (the "Software"), to deal
9in the Software without restriction, including without limitation the rights
10to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11copies of the Software, and to permit persons to whom the Software is
12furnished to do so, subject to the following conditions:
13
14The above copyright notice and this permission notice shall be included in all
15copies or substantial portions of the Software.
16
17THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23SOFTWARE.
24
25*/
26/*
27Part of Rack development has been done in the BALTRAD projects part-financed
28by the European Union (European Regional Development Fund and European
29Neighbourhood 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 DRAIN_TREE_HTML
41#define DRAIN_TREE_HTML
42
43#include <ostream>
44
45#include "FileInfo.h"
46#include "ReferenceMap.h"
47#include "TreeXML.h"
48#include "UtilsXML.h"
49
50namespace drain {
51
52
53class NodeHTML;
54
56/*
57 typedef NodeHTML::xml_tree_t TreeHTML;
58
59 Note: the typedef defined in XML is not the same, it is
60 drain::UnorderedMultiTree<NodeXML<html::tag_t>,false, NodeXML<>::path_t> CoreTreeHTML;
61 That is, the first template is NodeXML<html::tag_t> and not the complete NodeHTML
62 */
63typedef drain::UnorderedMultiTree<NodeHTML,false, NodeXML<>::path_t> TreeHTML;
64
65
66// typedef drain::UnorderedMultiTree<NodeHTML,false, NodeXML<>::path_t> TreeHTML;
67
68struct Html {
69
70 enum tag_t {
71 UNDEFINED=NodeXML<>::UNDEFINED,
72 COMMENT=NodeXML<>::COMMENT,
73 CTEXT=NodeXML<>::CTEXT,
74 SCRIPT=NodeXML<>::SCRIPT,
75 STYLE=NodeXML<>::STYLE,
76 STYLE_SELECT=NodeXML<>::STYLE_SELECT,
77 HTML,
78 HEAD, BASE, LINK, META, TITLE,
79 BODY, A, BR, CAPTION, DIV, H1, H2, H3, HR, IMG, LI, OL, P, PRE, SPAN, TABLE, TR, TH, TD, UL};
80
81
82};
83
84
85
86// template <>
87// const drain::EnumDict<Html::tag_t>::dict_t drain::EnumDict<Html::tag_t>::dict;
88
89DRAIN_ENUM_DICT(Html::tag_t);
90
91// Note: this specialization actually applies to all XML paths of default type, that is NodeXML<int> .
92template <>
93template <>
94inline
95void drain::NodeXML<>::path_t::appendElem(const Html::tag_t & type){
96 // void TreeHTML::path_t::appendElem(const Html::tag_t & type){
97 appendElem(EnumDict<Html::tag_t>::dict.getKey(type, false));
98}
99
103//class NodeHTML : public HTML, public NodeXML<html::tag_t> {
104class NodeHTML : public Html, public NodeXML<Html::tag_t> {
105// class NodeHTML : public NodeXML<html::tag_t> {
106
107
108public:
109
110 // typedef int tag_t;
111
113 NodeHTML(const Html::tag_t & t = Html::tag_t(0));
114
116 NodeHTML(const NodeHTML & node);
117
118 inline
119 ~NodeHTML(){};
120
122 virtual
123 bool isSingular() const override final;
124
126 virtual
127 bool isExplicit() const override final;
128
129 inline
130 NodeHTML & operator=(const NodeHTML & node){
131 return XML::xmlAssignNode(*this, node);
132 }
133
134 template <class T>
135 inline
136 NodeHTML & operator=(const T & s){
137 set(s); // XML
138 return *this;
139 }
140
141 /*
142 inline
143 NodeHTML & operator=(const std::initializer_list<std::pair<const char *,const drain::Variable> > &l){
144 set(l);
145 return *this;
146 }
147 */
148
149 /*
150 static // virtual
151 inline
152 std::ostream & docTypeToStream(std::ostream &ostr){
153 ostr << "<!DOCTYPE html>\n";
154 return ostr;
155 }
156 */
157
158 static
159 const FileInfo fileInfo;
160
161 // virtual void handleType(const Html::tag_t &t) override final;
162
163protected:
164
165 virtual
166 void handleType() override final;
167
168};
169
170
171
172
173inline
174std::ostream & operator<<(std::ostream &ostr, const NodeHTML & node){
175 return node.nodeToStream(ostr);
176}
177
178
179// This is the way!
180template <> // for T (Tree class)
181inline
182std::ostream & NodeXML<Html::tag_t>::docTypeToStream(std::ostream &ostr){
183 ostr << "<!DOCTYPE html>\n";
184 return ostr;
185}
186
187
188
189
190
191// Important TAG type initialisations for elements.
192template <>
193const NodeXML<Html::tag_t>::xml_default_elem_map_t NodeXML<Html::tag_t>::xml_default_elems;
194
196
199template <>
200inline
201void TreeHTML::initChild(TreeHTML & child) const {
202 UtilsXML::initChildWithDefaultType(*this, child);
203 /*
204 const typename Html::tag_t type = UtilsXML::retrieveDefaultType(this->data);
205 if (type){
206 child->setType(type);
207 }
208 */
209}
210
211// Preferred template specification
212/*
213template <>
214inline
215TreeHTML & TreeHTML::addChild(){ // const TreeHTML::key_t & key){
216 return UtilsXML::addChild(*this);
217}
218*/
219
220// Preferred template specification
221template <>
222template <>
223inline
224bool TreeHTML::hasChild(const Html::tag_t & type) const { // const TreeHTML::key_t & key){
225 return TreeHTML::hasChild(EnumDict<Html::tag_t>::dict.getKey(type, false));
226}
227
228
229// Preferred template specification
230template <>
231template <>
232inline
233TreeHTML & TreeHTML::operator()(const Html::tag_t & type){
234 return UtilsXML::setType(*this, type);
235}
236
237
238
239template <> // for T (Tree class)
240template <> // for K (path elem arg)
241TreeHTML & TreeHTML::operator[](const Html::tag_t & type);
242
243template <> // for T (Tree class)
244template <> // for K (path elem arg)
245const TreeHTML & TreeHTML::operator[](const Html::tag_t & type) const;
246
247// Same for ClassXML (designed for children of STYLE element)
248
249template <> // for T (Tree class)
250template <> // for K (path elem arg)
251TreeHTML & TreeHTML::operator[](const ClassXML & cls);
252
254template <> // for T (Tree class)
255template <> // for K (path elem arg)
256const TreeHTML & TreeHTML::operator[](const ClassXML & cls) const ;
257
258
259template <> // for T (Tree class)
260template <> // for K (path elem arg)
261bool TreeHTML::hasChild(const ClassXML & cls) const;
262
263
264template <>
265inline
266//TreeHTML & TreeHTML::operator=(std::initializer_list<std::pair<const char *,const char *> > l){
267TreeHTML & TreeHTML::operator=(std::initializer_list<std::pair<const char *,const Variable> > l){
268 return UtilsXML::assign(*this, l);
269}
270
272template <>
273template <>
274inline
275TreeHTML & TreeHTML::operator=(const std::string & arg){
276 UtilsXML::assignString(*this, arg);
277 return *this;
278}
279
280
281template <>
282template <class T>
283inline
284TreeHTML & TreeHTML::operator=(const T & arg){
285 return UtilsXML::assign(*this, arg);
286}
287
288/*
289inline
290std::ostream & operator<<(std::ostream &ostr, const NodeHTML & node){
291 return drain::Sprinter::toStream(ostr, node.getAttributes());
292}
293*/
294
295
296inline
297std::ostream & operator<<(std::ostream &ostr, const TreeHTML & tree){
298 return NodeHTML::docToStream(ostr, tree);
299}
300
301
302
303DRAIN_TYPENAME(NodeHTML);
304DRAIN_TYPENAME(Html);
305DRAIN_TYPENAME(Html::tag_t);
306
307
308} // drain::
309
310#endif // DRAIN_TREE_HTML
311
Definition FileInfo.h:48
Definition TreeHTML.h:104
virtual bool isSingular() const override final
Tell if this element should always be a single tag, not separate opening and closing tags.
Definition TreeHTML.cpp:188
virtual bool isExplicit() const override final
Tell if this element should always have separate opening and closing tags even when empty,...
Definition TreeHTML.cpp:209
Definition TreeXML.h:341
static std::ostream & docTypeToStream(std::ostream &ostr)
Write the XML definition beginning any XML document.
Definition TreeXML.h:751
virtual std::ostream & nodeToStream(std::ostream &ostr, tag_display_mode mode=EMPTY_TAG) const override
Dumps info. Future option: outputs leading and ending tag.
Definition TreeXML.h:414
static std::ostream & docToStream(std::ostream &ostr, const V &tree)
Definition TreeXML.h:733
void appendElem(const elem_t &elem)
Main method for adding elements.
Definition Path.h:246
static T & assign(T &dst, const T &src)
Assign another tree structure to another.
Definition UtilsXML.h:57
static TX & setType(TX &tree, const typename TX::node_data_t::xml_tag_t &type)
Definition UtilsXML.h:169
static TX & assignString(TX &tree, const std::string &s)
When assigning a string, create new element unless the element itself is of type CTEXT.
Definition UtilsXML.h:131
static N & xmlAssignNode(N &dst, const N &src)
Assign tree node (data) to another.
Definition XML.h:623
Definition DataSelector.cpp:1277
drain::UnorderedMultiTree< NodeHTML, false, NodeXML<>::path_t > TreeHTML
The HTML data structure.
Definition TreeHTML.h:63
DRAIN_TYPENAME(void)
Add a specialization for each type of those you want to support.
A container for a static dictionary of enumeration values.
Definition EnumUtils.h:52
Definition TreeHTML.h:68