Loading...
Searching...
No Matches
TreeUtilsXML.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: 2025/10
35 * Author: mpeura
36 */
37
38
39
40#ifndef DRAIN_TREE_UTILS_XML
41#define DRAIN_TREE_UTILS_XML
42
43#include "TreeUtils.h"
44#include "TreeXML.h"
45
46
47namespace drain {
48
49
50template <class N>
52
53public:
54
55 inline
56 ElemPrinter(const N & node){
57 std::stringstream sstr;
58 sstr << "<" << node.getTag();
59 if (::atoi(node.getId().c_str())==0){
60 sstr << " id=" << node.getId();
61 }
62 if (node.getName().isValid()){
63 sstr << " name=" << node.getName();
64 }
65 if (!node.getClasses().empty()){
66 sstr << " class=[" << node.getClasses() << ']';
67 }
68 sstr << '>';
69 id = sstr.str();
70 }
71
72 const std::string & str() const {
73 return id;
74 }
75
76
77protected:
78
79 std::string id;
80
81};
82
83enum XmlEmptiness {
84 CHILDREN = 1,
85 TEXT = 2, // consider whitespace remover?
86 ATTRIBUTES = 4,
87 //DATA = 4,
88 ALL = CHILDREN|TEXT|ATTRIBUTES,
89 // require any or all?
90 ANY = 128,
91};
92
93template <class T>
94class TreePruner : public drain::TreeVisitor<T> {
95
96public:
97
98
99 typedef std::map<typename T::node_data_t::xml_tag_t, short unsigned int> tag_selector_t;
100
101 tag_selector_t tagSelector;
102 TreePruner(){
103 }
104
105 inline
106 int visitPrefix(T & tree, const typename T::path_t & path) override {
107 return 0;
108 }
109
110 int visitPostfix(T & tree, const typename T::path_t & path) override;
111
112
113};
114
115
116template <class T>
117int TreePruner<T>::visitPostfix(T & tree, const typename T::path_t & path){
118 drain::Logger mout(__FILE__, __FUNCTION__);
119
120 T & current = tree(path);
121
122 std::list<typename T::path_elem_t> empties;
123
124 for (const auto & entry: current.getChildren()){
125
127
128 const T & child = entry.second;
129
130 if (child.getChildren().empty())
131 flagger.add(CHILDREN);
132
133 if (child.data.getAttributes().empty())
134 flagger.add(ATTRIBUTES);
135
136 if (child.data.getText().empty())
137 flagger.add(TEXT);
138
139 typename tag_selector_t::const_iterator it = tagSelector.find(child->getNativeType());
140 if (it != tagSelector.end()){
141 // mout.pending<LOG_WARNING>("found : ", child->getType(), " -> " , ElemPrinter<typename T::node_data_t>(child).str());
142 // check rule
143 if (flagger.isSet(it->second)){
144 empties.push_back(entry.first);
145 }
146 }
147
148 /*
149 switch (entry.second->getType()){
150 case svg::TEXT:
151 if (entry.second.getChildren().empty() && entry.second->getText().empty()){
152 empties.push_back(entry.first);
153 // current.erase();
154 }
155 break;
156 case svg::TSPAN:
157 if (entry.second->getText().empty()){
158 empties.push_back(entry.first);
159 }
160 break;
161 default:
162 break;
163 }
164 */
165 }
166
167 for (const typename T::path_elem_t & elem: empties){
168 // mout.reject<LOG_WARNING>("erasing: ", elem, " -> " , ElemPrinter<typename T::node_data_t>(current[elem]).str());
169 current.erase(elem);
170 }
171
172 return 0;
173
174}
175
176
177
178} // drain::
179
180DRAIN_ENUM_DICT(drain::XmlEmptiness);
181
182#endif /* TREEXML_H_ */
183
Definition TreeUtilsXML.h:51
Default default value...
Definition EnumFlags.h:247
LogSourc e is the means for a function or any program segment to "connect" to a Log.
Definition Log.h:312
Definition TreeUtilsXML.h:94
int visitPrefix(T &tree, const typename T::path_t &path) override
Tasks to be executed before traversing child nodes.
Definition TreeUtilsXML.h:106
int visitPostfix(T &tree, const typename T::path_t &path) override
Tasks to be executed after traversing child nodes.
Definition TreeUtilsXML.h:117
Default implementation of a tree visitor (concept) compatible TreeUtils::traverser()
Definition TreeUtils.h:270
Definition DataSelector.cpp:1277