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 "EnumFlagger.h"
44
45#include "TreeUtils.h"
46
47
48namespace drain {
49
50
51template <class N>
53
54public:
55
56 inline
57 ElemPrinter(const N & node){
58 std::stringstream sstr;
59 sstr << "<" << node.getTag();
60 if (::atoi(node.getId().c_str())==0){
61 sstr << " id=" << node.getId();
62 }
63 if (node.getName().isValid()){
64 sstr << " name=" << node.getName();
65 }
66 if (!node.getClasses().empty()){
67 sstr << " class=[" << node.getClasses() << ']';
68 }
69 sstr << '>';
70 id = sstr.str();
71 }
72
73 const std::string & str() const {
74 return id;
75 }
76
77
78protected:
79
80 std::string id;
81
82};
83
84enum XmlEmptiness {
85 CHILDREN = 1,
86 TEXT = 2, // consider whitespace remover?
87 ATTRIBUTES = 4,
88 //DATA = 4,
89 ALL = CHILDREN|TEXT|ATTRIBUTES,
90 // require any or all?
91 ANY = 128,
92};
93
104template <class T>
106
107public:
108
109
110 typedef std::map<typename T::node_data_t::xml_tag_t, short unsigned int> tag_selector_t;
111
112 TreePruner(){
113 }
114
115 inline
116 int visitPrefix(T & tree, const typename T::path_t & path) override {
117 return 0;
118 }
119
120 int visitPostfix(T & tree, const typename T::path_t & path) override;
121
122 template <class ... TT>
123 void setEmptinessCriterion(typename T::node_data_t::xml_tag_t tag, const TT &... args){
125 flagger.set(args...);
126 tagSelector[tag] = flagger.getValue(); // check int type?
127 }
128
129protected:
130
131 tag_selector_t tagSelector;
132
133};
134
135
136template <class T>
137int TreePruner<T>::visitPostfix(T & tree, const typename T::path_t & path){
138
139 drain::Logger mout(__FILE__, __FUNCTION__);
140
141 T & current = tree(path);
142
143 std::list<typename T::path_elem_t> elemsToDelete;
144
145 for (const auto & entry: current.getChildren()){
146
147 const T & child = entry.second;
148
149 // Is current tag type mentioned in selector?
150 typename tag_selector_t::const_iterator it = tagSelector.find(child->getNativeType());
151 if (it != tagSelector.end()){
152
153 // mout.pending<LOG_WARNING>("found : ", child->getType(), " -> " , ElemPrinter<typename T::node_data_t>(child).str());
154
155
156 if (it->second == 0){
157 // Unconditioned deletion of this tag type: the type is mentioned without selector value.
158 elemsToDelete.push_back(entry.first);
159 }
160 else {
161
163
164 if (child.getChildren().empty())
165 emptiness.add(CHILDREN);
166
167 if (child.data.getAttributes().empty())
168 emptiness.add(ATTRIBUTES);
169
170 if (child.data.getText().empty())
171 emptiness.add(TEXT);
172
173 // Note: checks rule if-ALL-set
174 if (emptiness.isSet(it->second)){
175 elemsToDelete.push_back(entry.first);
176 }
177 else if ((it->second & XmlEmptiness::ANY) && emptiness.isAnySet(it->second)){
178 elemsToDelete.push_back(entry.first);
179 }
180 }
181
182 }
183
184 }
185
186 for (const typename T::path_elem_t & elem: elemsToDelete){
187 // mout.reject<LOG_WARNING>("erasing: ", elem, " -> " , ElemPrinter<typename T::node_data_t>(current[elem]).str());
188 current.erase(elem);
189 }
190
191 return 0;
192
193}
194
195
196
197} // drain::
198
199DRAIN_ENUM_DICT(drain::XmlEmptiness);
200
201#endif // DRAIN_TREE_UTILS_XML
202
Definition TreeUtilsXML.h:52
Flagger accepting values of enum type E.
Definition EnumFlagger.h:56
LogSourc e is the means for a function or any program segment to "connect" to a Log.
Definition Log.h:313
Definition TreeUtilsXML.h:105
int visitPrefix(T &tree, const typename T::path_t &path) override
Tasks to be executed before traversing child nodes.
Definition TreeUtilsXML.h:116
int visitPostfix(T &tree, const typename T::path_t &path) override
Tasks to be executed after traversing child nodes.
Definition TreeUtilsXML.h:137
Default implementation of a tree visitor (concept) compatible TreeUtils::traverser()
Definition TreeUtils.h:265
Definition DataSelector.cpp:1277