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
103template <class T>
105
106public:
107
108
109 typedef std::map<typename T::node_data_t::xml_tag_t, short unsigned int> tag_selector_t;
110
111 tag_selector_t tagSelector;
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
123};
124
125
126template <class T>
127int TreePruner<T>::visitPostfix(T & tree, const typename T::path_t & path){
128 drain::Logger mout(__FILE__, __FUNCTION__);
129
130 T & current = tree(path);
131
132 std::list<typename T::path_elem_t> elemsToDelete;
133
134 for (const auto & entry: current.getChildren()){
135
136 const T & child = entry.second;
137
138 // Is current tag type mentioned in selector?
139 typename tag_selector_t::const_iterator it = tagSelector.find(child->getNativeType());
140 if (it != tagSelector.end()){
141
142 // mout.pending<LOG_WARNING>("found : ", child->getType(), " -> " , ElemPrinter<typename T::node_data_t>(child).str());
143
144
145 if (it->second == 0){
146 // Unconditioned deletion of this tag type: the type is mentioned without selector value.
147 elemsToDelete.push_back(entry.first);
148 }
149 else {
150
152
153 if (child.getChildren().empty())
154 emptiness.add(CHILDREN);
155
156 if (child.data.getAttributes().empty())
157 emptiness.add(ATTRIBUTES);
158
159 if (child.data.getText().empty())
160 emptiness.add(TEXT);
161
162 // Note: checks rule if-ALL-set
163 if (emptiness.isSet(it->second)){
164 elemsToDelete.push_back(entry.first);
165 }
166 else if ((it->second & XmlEmptiness::ANY) && emptiness.isAnySet(it->second)){
167 elemsToDelete.push_back(entry.first);
168 }
169 }
170
171 }
172
173 }
174
175 for (const typename T::path_elem_t & elem: elemsToDelete){
176 // mout.reject<LOG_WARNING>("erasing: ", elem, " -> " , ElemPrinter<typename T::node_data_t>(current[elem]).str());
177 current.erase(elem);
178 }
179
180 return 0;
181
182}
183
184
185
186} // drain::
187
188DRAIN_ENUM_DICT(drain::XmlEmptiness);
189
190#endif // DRAIN_TREE_UTILS_XML
191
Definition TreeUtilsXML.h:51
Flagger accepting values of enum type E.
Definition EnumUtils.h:294
LogSourc e is the means for a function or any program segment to "connect" to a Log.
Definition Log.h:313
Definition TreeUtilsXML.h:104
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:127
Default implementation of a tree visitor (concept) compatible TreeUtils::traverser()
Definition TreeUtils.h:270
Definition DataSelector.cpp:1277