Grid 0.7.0
XmlIO.cc
Go to the documentation of this file.
1 /*************************************************************************************
2
3 Grid physics library, www.github.com/paboyle/Grid
4
5 Source file: ./lib/serialisation/XmlIO.cc
6
7 Copyright (C) 2015
8
9Author: Antonin Portelli <antonin.portelli@me.com>
10Author: paboyle <paboyle@ph.ed.ac.uk>
11
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
16
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
21
22 You should have received a copy of the GNU General Public License along
23 with this program; if not, write to the Free Software Foundation, Inc.,
24 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25
26 See the full license in the file "LICENSE" in the top level distribution directory
27 *************************************************************************************/
28 /* END LEGAL */
29#include <Grid/GridCore.h>
30
32
33void xmlCheckParse(const pugi::xml_parse_result &result, const std::string name)
34{
35 if (!result)
36 {
37 std::cerr << "XML parsing error for " << name << std::endl;
38 std::cerr << "XML error description: " << result.description() << std::endl;
39 std::cerr << "XML error offset : " << result.offset << std::endl;
40 abort();
41 }
42}
43
44// Writer implementation ///////////////////////////////////////////////////////
45XmlWriter::XmlWriter(const std::string &fileName, std::string toplev) : fileName_(fileName)
46{
47 if ( toplev == std::string("") ) {
48 node_=doc_;
49 } else {
50 node_=doc_.append_child();
51 node_.set_name(toplev.c_str());
52 }
53}
54
56{
57 if ( fileName_ != std::string("") ) {
58 doc_.save_file(fileName_.c_str(), indent_.c_str());
59 }
60}
61
62void XmlWriter::push(const std::string &s)
63{
64 node_ = node_.append_child(s.c_str());
65}
66
67void XmlWriter::pushXmlString(const std::string &s)
68{
69 pugi::xml_document doc;
70 auto result = doc.load_buffer(s.c_str(), s.size());
71
72 xmlCheckParse(result, "fragment\n'" + s +"'");
73 for (pugi::xml_node child = doc.first_child(); child; child = child.next_sibling())
74 {
75 node_ = node_.append_copy(child);
76 }
77 pop();
78}
79
81{
82 node_ = node_.parent();
83}
84
85std::string XmlWriter::docString(void)
86{
87 std::ostringstream oss;
88 doc_.save(oss, indent_.c_str());
89 return oss.str();
90}
91
92std::string XmlWriter::string(void)
93{
94 std::ostringstream oss;
95 doc_.save(oss, indent_.c_str(), pugi::format_default | pugi::format_no_declaration);
96 return oss.str();
97}
98
99// Reader implementation ///////////////////////////////////////////////////////
100XmlReader::XmlReader(const std::string &s, const bool isBuffer,
101 std::string toplev)
102{
103 pugi::xml_parse_result result;
104
105 if (isBuffer)
106 {
107 fileName_ = "<string>";
108 result = doc_.load_string(s.c_str());
109 xmlCheckParse(result, "string\n'" + s + "'");
110 }
111 else
112 {
113 fileName_ = s;
114 result = doc_.load_file(s.c_str());
115 xmlCheckParse(result, "file '" + fileName_ + "'");
116 }
117 if ( toplev == std::string("") ) {
118 node_ = doc_;
119 } else {
120 node_ = doc_.child(toplev.c_str());
121 }
122}
123
124#define XML_SAFE_NODE(expr)\
125if (expr)\
126{\
127 node_ = expr;\
128 return true;\
129}\
130else\
131{\
132 return false;\
133}
134
135bool XmlReader::push(const std::string &s)
136{
137 if (s.empty())
138 {
139 XML_SAFE_NODE(node_.first_child());
140 }
141 else
142 {
143 XML_SAFE_NODE(node_.child(s.c_str()));
144 }
145}
146
148{
149 node_ = node_.parent();
150}
151
152bool XmlReader::nextElement(const std::string &s)
153{
154 if (s.empty())
155 {
156 XML_SAFE_NODE(node_.next_sibling());
157 }
158 else
159 {
160 XML_SAFE_NODE(node_.next_sibling(s.c_str()));
161 }
162}
163
165{
166 std::ostringstream oss;
167 pugi::xml_document doc;
168
169 doc.append_copy(node_);
170 doc.save(oss, indent_.c_str(), pugi::format_default | pugi::format_no_declaration);
171 s = oss.str();
172}
173
174template <>
175void XmlReader::readDefault(const std::string &s, std::string &output)
176{
177 if (node_.child(s.c_str()))
178 {
179 output = node_.child(s.c_str()).first_child().value();
180 }
181 else
182 {
183 std::cout << GridLogWarning << "XML: cannot open node '" << s << "'";
184 std::cout << std::endl;
185
186 output = "";
187 }
188}
GridLogger GridLogWarning(1, "Warning", GridLogColours, "YELLOW")
#define NAMESPACE_BEGIN(A)
Definition Namespace.h:35
#define NAMESPACE_END(A)
Definition Namespace.h:36
void xmlCheckParse(const pugi::xml_parse_result &result, const std::string name)
Definition XmlIO.cc:33
#define XML_SAFE_NODE(expr)
Definition XmlIO.cc:124
pugi::xml_document doc_
Definition XmlIO.h:91
pugi::xml_node node_
Definition XmlIO.h:92
XmlReader(const std::string &fileName, const bool isBuffer=false, std::string toplev=std::string("grid"))
Definition XmlIO.cc:100
bool push(const std::string &s="")
Definition XmlIO.cc:135
void readCurrentSubtree(std::string &s)
Definition XmlIO.cc:164
void readDefault(const std::string &s, U &output)
Definition XmlIO.h:171
const std::string indent_
Definition XmlIO.h:90
bool nextElement(const std::string &s="")
Definition XmlIO.cc:152
void pop(void)
Definition XmlIO.cc:147
std::string fileName_
Definition XmlIO.h:93
std::string docString(void)
Definition XmlIO.cc:85
void pop(void)
Definition XmlIO.cc:80
XmlWriter(const std::string &fileName, std::string toplev=std::string("grid"))
Definition XmlIO.cc:45
virtual ~XmlWriter(void)
Definition XmlIO.cc:55
std::string string(void)
Definition XmlIO.cc:92
std::string fileName_
Definition XmlIO.h:68
void pushXmlString(const std::string &s)
Definition XmlIO.cc:67
pugi::xml_node node_
Definition XmlIO.h:67
void push(const std::string &s)
Definition XmlIO.cc:62
pugi::xml_document doc_
Definition XmlIO.h:66
const std::string indent_
Definition XmlIO.h:65
void xmlCheckParse(const pugi::xml_parse_result &result, const std::string name)