Loading...
Searching...
No Matches
JavaScriptXML.h
1/*
2
3MIT License
4
5Copyright (c) 2026 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 * UtilsXML.h
33 *
34 * Author: mpeura
35 */
36
37#ifndef DRAIN_JAVASCRIPT_XML
38#define DRAIN_JAVASCRIPT_XML
39
40// #include "XML.h"
41
42#include "TreeXML.h"
43#include "UtilsXML.h"
44
45namespace drain {
46
47
50
51public:
52
53
54 /*
55 template <typename N>
56 static
57 DRAIN_XML_TREE(N) & getHeaderObject(DRAIN_XML_TREE(N) & root, typename N::xml_tag_t tag,
58 const typename DRAIN_XML_TREE(N)::path_elem_t & key = typename DRAIN_XML_TREE(N)::path_elem_t()){
59
60 const typename DRAIN_XML_TREE(N)::path_elem_t & finalKey = !key.empty() ? key : Enum<typename N::xml_tag_t>::getKey(tag);
61
62 if (!root.hasChild(finalKey)){
63 DRAIN_XML_TREE(N) & child = root.prependChild(finalKey); // consider path type! getDefaultObject
64 child->setType(tag);
65 return child;
66 }
67 else {
68 return root[finalKey];
69 }
70 };
71
72 template <typename N>
73 static
74 DRAIN_XML_TREE(N) & appendHeaderObject(DRAIN_XML_TREE(N) & root, typename N::xml_tag_t tag,
75 const typename DRAIN_XML_TREE(N)::path_elem_t & key = typename DRAIN_XML_TREE(N)::path_elem_t()){
76
77 const typename DRAIN_XML_TREE(N)::path_elem_t & finalKey = !key.empty() ? key : Enum<typename N::xml_tag_t>::getKey(tag);
78
79 if (!root.hasChild(finalKey)){
80 DRAIN_XML_TREE(N) & child = root.add(finalKey); // consider path type! getDefaultObject
81 child->setType(tag);
82 return child;
83 }
84 else {
85 return root[finalKey];
86 }
87 };
88 */
89
90
91 template <class N>
92 static
93 DRAIN_XML_TREE(N) & getOnLoadScript(DRAIN_XML_TREE(N) & root);
94
95
96 template <class N>
97 static inline
98 DRAIN_XML_TREE(N) & ensureScript(DRAIN_XML_TREE(N) & root, const std::string & label, const std::string & code){
99 DRAIN_XML_TREE(N) & scriptElem = UtilsXML::getHeaderObject(root, N::xml_tag_t::SCRIPT, label);
100 scriptElem = code;
101 return scriptElem;
102 }
103
104 // See StringTools::getSafeKey void getSafeVariableName();
105
107
118 template <typename N, typename ...TT>
119 static
120 DRAIN_XML_TREE(N) & ensureJavaScriptFunction(DRAIN_XML_TREE(N) & root, const std::string & name, const TT & ...args){
121
122 // typedef UnorderedMultiTree<N> T;
123 // T & jsFunction = getHeaderObject(root, T::node_data_t::xml_tag_t::SCRIPT, name);
124 // T & jsFunctionScope = jsFunction[N::xml_tag_t::SCOPE_CURLY](N::xml_tag_t::SCOPE_CURLY);
125 DRAIN_XML_TREE(N) & jsFunction = UtilsXML::getHeaderObject(root, N::xml_tag_t::SCRIPT);
126
127 DRAIN_XML_TREE(N) & jsFunctionScope = jsFunction[name](N::xml_tag_t::SCOPE_CURLY);
128
129 if (jsFunctionScope.empty()){
130 jsFunctionScope->setText("function ", name, '(', StringBuilder<','>(args...), ')');
131 // jsFunction.addChild()->setComment("end of ", name);
132 jsFunction.addChild()->setText('\n');
133 }
134
135
136 return jsFunctionScope; // [XML::SCOPE_CURLY];
137 }
138
139 template <typename N>
140 static
141 DRAIN_XML_TREE(N) & ensureJavaScriptUrl(DRAIN_XML_TREE(N) & root, const std::string & url){
142
143 drain::Logger mout(__FILE__, __FUNCTION__);
144
145 // typedef DRAIN_XML_TREE(N) T;
146
147 std::string id(url);
148 for (auto & c : id){
149 // drain::PathSeparatorPolicy
150 if (c == DRAIN_XML_TREE(N)::path_t::separator.character){
151 c = '|';
152 }
153 }
154 mout.note("mapped id:", id);
155
156 DRAIN_XML_TREE(N) & treeJS = UtilsXML::getHeaderObject(root, N::xml_tag_t::SCRIPT, id);
157
158 treeJS->setUrl(url);
159
160 return treeJS; // [XML::SCOPE_CURLY];
161 }
162
163
164};
165
166template <class N>
167DRAIN_XML_TREE(N) & JavaScriptXML::getOnLoadScript(DRAIN_XML_TREE(N) & root){
168
169 drain::Logger mout(__FILE__, __FUNCTION__);
170
171 static
172 const std::string onload_fnc_name("drain_onload");
173
174 mout.experimental<LOG_INFO>("Adding mouse interaction: SVG onload=", onload_fnc_name, "()");
175
176 root->setAttribute("onload", onload_fnc_name, "()"); // perhaps repeatedly
177
178 return ensureJavaScriptFunction(root, onload_fnc_name); // [svg::SCOPE_CURLY](svg::SCOPE_CURLY);
179 // return drain::UtilsXML::ensureJavaScriptFunctionScope(ctx.getSVG(), onload_fnc_name);
180
181
182}
183
184} // drain::
185
186#endif /* DRAIN_XML */
187
188
Functions for manipulating XML structures.
Definition JavaScriptXML.h:49
static DRAIN_XML_TREE(N) &ensureJavaScriptFunction(DRAIN_XML_TREE(N) &root
LogSourc e is the means for a function or any program segment to "connect" to a Log.
Definition Log.h:326
Definition StringBuilder.h:58
Definition Data.h:1473