Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
xmlutils.hpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2016 Quaternion Risk Management Ltd
3 All rights reserved.
4
5 This file is part of ORE, a free-software/open-source library
6 for transparent pricing and risk analysis - http://opensourcerisk.org
7
8 ORE is free software: you can redistribute it and/or modify it
9 under the terms of the Modified BSD License. You should have received a
10 copy of the license along with this program.
11 The license is also available online at <http://opensourcerisk.org>
12
13 This program is distributed on the basis that it will form a useful
14 contribution to risk analytics and model standardisation, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the license for more details.
17*/
18
19/*! \file ored/utilities/xmlutils.hpp
20 \brief XML utility functions
21 \ingroup utilities
22*/
23
24#pragma once
25
26#include <ql/errors.hpp>
27#include <ql/time/period.hpp>
28#include <ql/time/calendar.hpp>
29#include <ql/time/businessdayconvention.hpp>
30#include <ql/types.hpp>
31
32#include <map>
33#include <sstream> // std::ostringstream
34#include <string>
35#include <vector>
36
37// Forward declarations and typedefs
38// so we don't need to #include rapidxml everywhere.
39namespace rapidxml {
40//! XML Node
41/*! \ingroup utilities
42 */
43template <class Ch> class xml_node;
44//! XML Document
45/*! \ingroup utilities
46 */
47template <class Ch> class xml_document;
48} // namespace rapidxml
49
50namespace ore {
51namespace data {
52using QuantLib::Period;
53using QuantLib::Real;
54using QuantLib::Size;
55using std::map;
56using std::pair;
57using std::string;
58using std::vector;
59
61
62//! Small XML Document wrapper class.
63/*! \ingroup utilities
64 */
66public:
67 //! create an empty doc.
69 //! load an xml doc from the given file
70 XMLDocument(const string& filename);
71 //! destructor
73
74 //! load a document from a hard-coded string
75 void fromXMLString(const string& xmlString);
76
77 //! save the XML Document to the given file.
78 void toFile(const string& filename) const;
79
80 //! return the XML Document as a string.
81 std::string toString() const;
82
83 XMLNode* getFirstNode(const string& name) const;
84 void appendNode(XMLNode*);
85
86 // TODO: take these inside cpp, not exposed to clients
87 //! util functions that wrap rapidxml
88 XMLNode* allocNode(const string& nodeName);
89 XMLNode* allocNode(const string& nodeName, const string& nodeValue);
90 char* allocString(const string& str);
92
93private:
95 char* _buffer;
96};
97
98//! Base class for all serializable classes
99/*! \ingroup utilities
100 */
102public:
103 virtual ~XMLSerializable() {}
104 virtual void fromXML(XMLNode* node) = 0;
105 virtual XMLNode* toXML(XMLDocument& doc) const = 0;
106
107 void fromFile(const std::string& filename);
108 void toFile(const std::string& filename) const;
109
110 //! Parse from XML string
111 void fromXMLString(const std::string& xml);
112 //! Parse from XML string
113 std::string toXMLString() const;
114};
115
116//! XML Utilities Class
117/*! \ingroup utilities
118 */
119class XMLUtils {
120public:
121 static void checkNode(XMLNode* n, const string& expectedName);
122
123 static XMLNode* addChild(XMLDocument& doc, XMLNode* n, const string& name);
124 static void addChild(XMLDocument& doc, XMLNode* n, const string& name, const string& value);
125 static void addChildAsCdata(XMLDocument& doc, XMLNode* n, const string& name, const string& value);
126 static void addChild(XMLDocument& doc, XMLNode* n, const string& name, const string& value, const string& attrName,
127 const string& attr);
128 static void addChild(XMLDocument& doc, XMLNode* n, const string& name, const string& value,
129 const vector<string>& attrNames, const vector<string>& attrs);
130 static void addChild(XMLDocument& doc, XMLNode* n, const string& name, const char* value);
131 static void addChild(XMLDocument& doc, XMLNode* n, const string& name, Real value);
132 static void addChild(XMLDocument& doc, XMLNode* n, const string& name, int value);
133 static void addChild(XMLDocument& doc, XMLNode* n, const string& name, bool value);
134 static void addChild(XMLDocument& doc, XMLNode* n, const string& name, const Period& value);
135
136 //! Adds <code><Name>p1,p2,p3</Name></code>
137 template <class T> static void addGenericChild(XMLDocument& doc, XMLNode* n, const char* name, const T& value) {
138 std::ostringstream oss;
139 oss << value;
140 addChild(doc, n, name, oss.str());
141 }
142
143 template <class T>
144 static void addGenericChildAsList(XMLDocument& doc, XMLNode* n, const string& name, const vector<T>& values,
145 const string& attrName = "", const string& attr = "") {
146 std::ostringstream oss;
147 if (values.size() == 0) {
148 oss << "";
149 } else {
150 oss << values[0];
151 for (Size i = 1; i < values.size(); i++) {
152 oss << ", " << values[i];
153 }
154 }
155 addChild(doc, n, name, oss.str(), attrName, attr);
156 }
157
158 template <class T = string>
159 static void addChildren(XMLDocument& doc, XMLNode* n, const string& names, const string& name,
160 const vector<T>& values);
161 //! Adds <code><Name>v1,v2,v3</Name></code> - the inverse of getChildrenValuesAsDoublesCompact
162 static void addChild(XMLDocument& doc, XMLNode* n, const string& name, const vector<Real>& values);
163 template <class T = string>
164
165 static void addChildrenWithAttributes(XMLDocument& doc, XMLNode* n, const string& names, const string& name,
166 const vector<T>& values, const string& attrName,
167 const vector<string>& attrs); // one attribute (convenience function)
168 template <class T = string>
169 static void addChildrenWithAttributes(XMLDocument& doc, XMLNode* n, const string& names, const string& name,
170 const vector<T>& values, const vector<string>& attrNames,
171 const vector<vector<string>>& attrs); // n attributes
172 template <class T = string>
173 static void addChildrenWithOptionalAttributes(XMLDocument& doc, XMLNode* n, const string& names, const string& name,
174 const vector<T>& values, const string& attrName,
175 const vector<string>& attrs); // one attribute (convenience function)
176 template <class T = string>
177 static void addChildrenWithOptionalAttributes(XMLDocument& doc, XMLNode* n, const string& names, const string& name,
178 const vector<T>& values, const vector<string>& attrNames,
179 const vector<vector<string>>& attrs); // n attributes
180
181 static void addChildren(XMLDocument& doc, XMLNode* n, const string& names, const string& name,
182 const string& firstName, const string& secondName, const map<string, string>& values);
183
184 // If mandatory == true, we throw if the node is not present, otherwise we return a default vale.
185 static string getChildValue(XMLNode* node, const string& name, bool mandatory = false, const string& defaultValue = string());
186 static Real getChildValueAsDouble(XMLNode* node, const string& name, bool mandatory = false, double defaultValue = 0.0);
187 static int getChildValueAsInt(XMLNode* node, const string& name, bool mandatory = false, int defaultValue = 0);
188 static bool getChildValueAsBool(XMLNode* node, const string& name, bool mandatory = false, bool defaultValue = true);
189 static Period getChildValueAsPeriod(XMLNode* node, const string& name, bool mandatory = false,
190 const QuantLib::Period& defaultValue = 0 * QuantLib::Days);
191 static vector<string> getChildrenValues(XMLNode* node, const string& names, const string& name,
192 bool mandatory = false);
193 static vector<string>
194 getChildrenValuesWithAttributes(XMLNode* node, const string& names, const string& name, const string& attrName,
195 vector<string>& attrs,
196 bool mandatory = false); // one attribute (convenience function)
197
198 static vector<string> getChildrenValuesWithAttributes(XMLNode* node, const string& names, const string& name,
199 const vector<string>& attrNames,
200 const vector<std::reference_wrapper<vector<string>>>& attrs,
201 bool mandatory = false); // n attributes
202 template <class T>
203 static vector<T> getChildrenValuesWithAttributes(XMLNode* node, const string& names, const string& name,
204 const string& attrName, vector<string>& attrs,
205 const std::function<T(string)> parser,
206 bool mandatory = false); // one attribute (convenience function)
207
208 template <class T>
209 static vector<T> getChildrenValuesWithAttributes(XMLNode* node, const string& names, const string& name,
210 const vector<string>& attrNames,
211 const vector<std::reference_wrapper<vector<string>>>& attrs,
212 const std::function<T(string)> parser,
213 bool mandatory = false); // n attributes
214
215 static vector<Real> getChildrenValuesAsDoubles(XMLNode* node, const string& names, const string& name,
216 bool mandatory = false);
217 static vector<Real> getChildrenValuesAsDoublesCompact(XMLNode* node, const string& name, bool mandatory = false);
218
219 static vector<Period> getChildrenValuesAsPeriods(XMLNode* node, const string& name, bool mandatory = false);
220 static vector<string> getChildrenValuesAsStrings(XMLNode* node, const string& name, bool mandatory = false);
221
222 static map<string, string> getChildrenValues(XMLNode* node, const string& names, const string& name,
223 const string& firstName, const string& secondName,
224 bool mandatory = false);
225
226 static map<string, string> getChildrenAttributesAndValues(XMLNode* parent, const string& names,
227 const string& attributeName, bool mandatory = false);
228
229 // returns first child node
230 static XMLNode* getChildNode(XMLNode* n, const string& name = "");
231 // return first node in the hierarchy of n that matches name, maybe n itself
232 static XMLNode* locateNode(XMLNode* n, const string& name = "");
233 // append child to parent
234 static void appendNode(XMLNode* parent, XMLNode* child);
235
236 static void addAttribute(XMLDocument& doc, XMLNode* node, const string& attrName, const string& attrValue);
237 static string getAttribute(XMLNode* node, const string& attrName);
238
239 //! Returns all the children with a given name
240 // To get all children, set name equal to ""
241 static vector<XMLNode*> getChildrenNodes(XMLNode* node, const string& name);
242
243 static vector<XMLNode*> getChildrenNodesWithAttributes(XMLNode* node, const string& names, const string& name,
244 const string& attrName, vector<string>& attrs,
245 bool mandatory = false);
246 static vector<XMLNode*> getChildrenNodesWithAttributes(XMLNode* node, const string& names, const string& name,
247 const vector<string>& attrNames,
248 const vector<std::reference_wrapper<vector<string>>>& attrs,
249 bool mandatory = false);
250
251 //! Get and set a node's name
252 static string getNodeName(XMLNode* n);
253 static void setNodeName(XMLDocument& doc, XMLNode* node, const string& name);
254
255 //! Get a node's next sibling node
256 static XMLNode* getNextSibling(XMLNode* node, const string& name = "");
257
258 //! Get a node's value
259 static string getNodeValue(XMLNode* node);
260
261 //! Get a node's compact values as vector of doubles
262 static vector<Real> getNodeValueAsDoublesCompact(XMLNode* node);
263
264 //! Write a node out as a string
265 static string toString(XMLNode* node);
266
267 // helper routine to convert a value of an arbitrary type to string
268 static string convertToString(const Real value);
269
270 template <class T> static string convertToString(const T& value);
271
272};
273
274} // namespace data
275} // namespace ore
Small XML Document wrapper class.
Definition: xmlutils.hpp:65
void appendNode(XMLNode *)
Definition: xmlutils.cpp:118
XMLDocument()
create an empty doc.
Definition: xmlutils.cpp:75
std::string toString() const
return the XML Document as a string.
Definition: xmlutils.cpp:126
~XMLDocument()
destructor
Definition: xmlutils.cpp:96
rapidxml::xml_document< char > * doc()
Definition: xmlutils.hpp:91
XMLNode * allocNode(const string &nodeName)
util functions that wrap rapidxml
Definition: xmlutils.cpp:132
void toFile(const string &filename) const
save the XML Document to the given file.
Definition: xmlutils.cpp:120
void fromXMLString(const string &xmlString)
load a document from a hard-coded string
Definition: xmlutils.cpp:103
rapidxml::xml_document< char > * _doc
Definition: xmlutils.hpp:94
XMLNode * getFirstNode(const string &name) const
Definition: xmlutils.cpp:116
char * allocString(const string &str)
Definition: xmlutils.cpp:144
Base class for all serializable classes.
Definition: xmlutils.hpp:101
std::string toXMLString() const
Parse from XML string.
Definition: xmlutils.cpp:168
void fromXMLString(const std::string &xml)
Parse from XML string.
Definition: xmlutils.cpp:162
virtual XMLNode * toXML(XMLDocument &doc) const =0
virtual void fromXML(XMLNode *node)=0
void fromFile(const std::string &filename)
Definition: xmlutils.cpp:150
void toFile(const std::string &filename) const
Definition: xmlutils.cpp:155
XML Utilities Class.
Definition: xmlutils.hpp:119
static void addChildrenWithAttributes(XMLDocument &doc, XMLNode *n, const string &names, const string &name, const vector< T > &values, const string &attrName, const vector< string > &attrs)
Definition: xmlutils.cpp:510
static void addAttribute(XMLDocument &doc, XMLNode *node, const string &attrName, const string &attrValue)
Definition: xmlutils.cpp:412
static vector< Real > getChildrenValuesAsDoubles(XMLNode *node, const string &names, const string &name, bool mandatory=false)
Definition: xmlutils.cpp:319
static void addChildren(XMLDocument &doc, XMLNode *n, const string &names, const string &name, const vector< T > &values)
Definition: xmlutils.cpp:502
static string getAttribute(XMLNode *node, const string &attrName)
Definition: xmlutils.cpp:419
static void addGenericChildAsList(XMLDocument &doc, XMLNode *n, const string &name, const vector< T > &values, const string &attrName="", const string &attr="")
Definition: xmlutils.hpp:144
static void checkNode(XMLNode *n, const string &expectedName)
Definition: xmlutils.cpp:175
static void addGenericChild(XMLDocument &doc, XMLNode *n, const char *name, const T &value)
Adds <Name>p1,p2,p3</Name>
Definition: xmlutils.hpp:137
static vector< XMLNode * > getChildrenNodes(XMLNode *node, const string &name)
Returns all the children with a given name.
Definition: xmlutils.cpp:428
static Real getChildValueAsDouble(XMLNode *node, const string &name, bool mandatory=false, double defaultValue=0.0)
Definition: xmlutils.cpp:286
static XMLNode * locateNode(XMLNode *n, const string &name="")
Definition: xmlutils.cpp:393
static void addChildAsCdata(XMLDocument &doc, XMLNode *n, const string &name, const string &value)
Definition: xmlutils.cpp:202
static string getNodeName(XMLNode *n)
Get and set a node's name.
Definition: xmlutils.cpp:473
static map< string, string > getChildrenAttributesAndValues(XMLNode *parent, const string &names, const string &attributeName, bool mandatory=false)
Definition: xmlutils.cpp:365
static string getChildValue(XMLNode *node, const string &name, bool mandatory=false, const string &defaultValue=string())
Definition: xmlutils.cpp:277
static bool getChildValueAsBool(XMLNode *node, const string &name, bool mandatory=false, bool defaultValue=true)
Definition: xmlutils.cpp:296
static XMLNode * getChildNode(XMLNode *n, const string &name="")
Definition: xmlutils.cpp:387
static void addChildrenWithOptionalAttributes(XMLDocument &doc, XMLNode *n, const string &names, const string &name, const vector< T > &values, const string &attrName, const vector< string > &attrs)
Definition: xmlutils.cpp:542
static string getNodeValue(XMLNode *node)
Get a node's value.
Definition: xmlutils.cpp:489
static int getChildValueAsInt(XMLNode *node, const string &name, bool mandatory=false, int defaultValue=0)
Definition: xmlutils.cpp:291
static Period getChildValueAsPeriod(XMLNode *node, const string &name, bool mandatory=false, const QuantLib::Period &defaultValue=0 *QuantLib::Days)
Definition: xmlutils.cpp:301
static XMLNode * getNextSibling(XMLNode *node, const string &name="")
Get a node's next sibling node.
Definition: xmlutils.cpp:484
static vector< string > getChildrenValuesAsStrings(XMLNode *node, const string &name, bool mandatory=false)
Definition: xmlutils.cpp:342
static vector< string > getChildrenValuesWithAttributes(XMLNode *node, const string &names, const string &name, const string &attrName, vector< string > &attrs, bool mandatory=false)
Definition: xmlutils.cpp:563
static vector< Real > getChildrenValuesAsDoublesCompact(XMLNode *node, const string &name, bool mandatory=false)
Definition: xmlutils.cpp:327
static string convertToString(const Real value)
Definition: xmlutils.cpp:620
static vector< string > getChildrenValues(XMLNode *node, const string &names, const string &name, bool mandatory=false)
Definition: xmlutils.cpp:306
static void setNodeName(XMLDocument &doc, XMLNode *node, const string &name)
Definition: xmlutils.cpp:478
static XMLNode * addChild(XMLDocument &doc, XMLNode *n, const string &name)
Definition: xmlutils.cpp:181
static string toString(XMLNode *node)
Write a node out as a string.
Definition: xmlutils.cpp:714
static void appendNode(XMLNode *parent, XMLNode *child)
Definition: xmlutils.cpp:406
static vector< Period > getChildrenValuesAsPeriods(XMLNode *node, const string &name, bool mandatory=false)
Definition: xmlutils.cpp:337
static vector< XMLNode * > getChildrenNodesWithAttributes(XMLNode *node, const string &names, const string &name, const string &attrName, vector< string > &attrs, bool mandatory=false)
Definition: xmlutils.cpp:437
static vector< Real > getNodeValueAsDoublesCompact(XMLNode *node)
Get a node's compact values as vector of doubles.
Definition: xmlutils.cpp:332
XML Document.
Definition: xmlutils.hpp:47
SafeStack< ValueType > value
@ data
Definition: log.hpp:77
rapidxml::xml_node< char > XMLNode
Definition: xmlutils.hpp:60
Serializable Credit Default Swap.
Definition: namespaces.docs:23
string name