Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
eclipse
GitHub Repository: eclipse/sumo
Path: blob/main/src/utils/iodevices/PlainXMLFormatter.h
169678 views
1
/****************************************************************************/
2
// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3
// Copyright (C) 2012-2025 German Aerospace Center (DLR) and others.
4
// This program and the accompanying materials are made available under the
5
// terms of the Eclipse Public License 2.0 which is available at
6
// https://www.eclipse.org/legal/epl-2.0/
7
// This Source Code may also be made available under the following Secondary
8
// Licenses when the conditions for such availability set forth in the Eclipse
9
// Public License 2.0 are satisfied: GNU General Public License, version 2
10
// or later which is available at
11
// https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13
/****************************************************************************/
14
/// @file PlainXMLFormatter.h
15
/// @author Daniel Krajzewicz
16
/// @author Michael Behrisch
17
/// @date 2012
18
///
19
// Output formatter for plain XML output
20
/****************************************************************************/
21
#pragma once
22
#include <config.h>
23
24
#ifdef HAVE_FMT
25
#include <fmt/ostream.h>
26
#endif
27
28
#include "OutputFormatter.h"
29
30
31
// ===========================================================================
32
// class definitions
33
// ===========================================================================
34
/**
35
* @class PlainXMLFormatter
36
* @brief Output formatter for plain XML output
37
*
38
* PlainXMLFormatter format XML like output into the output stream.
39
*/
40
class PlainXMLFormatter : public OutputFormatter {
41
public:
42
/// @brief Constructor
43
PlainXMLFormatter(const int defaultIndentation = 0);
44
45
/// @brief Destructor
46
virtual ~PlainXMLFormatter() { }
47
48
/** @brief Writes an XML header with optional configuration
49
*
50
* If something has been written (myXMLStack is not empty), nothing
51
* is written and false returned.
52
*
53
* @param[in] into The output stream to use
54
* @param[in] rootElement The root element to use
55
* @param[in] attrs Additional attributes to save within the rootElement
56
* @param[in] includeConfig whether the current config should be included as XML comment
57
* @return whether something has been written
58
* @todo Describe what is saved
59
*/
60
bool writeXMLHeader(std::ostream& into, const std::string& rootElement,
61
const std::map<SumoXMLAttr, std::string>& attrs, bool writeMetadata,
62
bool includeConfig);
63
64
/** @brief Opens an XML tag
65
*
66
* An indentation, depending on the current xml-element-stack size, is written followed
67
* by the given xml element ("<" + xmlElement)
68
* The xml element is added to the stack, then.
69
*
70
* @param[in] into The output stream to use
71
* @param[in] xmlElement Name of element to open
72
* @return The OutputDevice for further processing
73
*/
74
void openTag(std::ostream& into, const std::string& xmlElement);
75
76
/** @brief Opens an XML tag
77
*
78
* Helper method which finds the correct string before calling openTag.
79
*
80
* @param[in] into The output stream to use
81
* @param[in] xmlElement Id of the element to open
82
*/
83
void openTag(std::ostream& into, const SumoXMLTag& xmlElement);
84
85
/** @brief Closes the most recently opened tag
86
*
87
* @param[in] into The output stream to use
88
* @return Whether a further element existed in the stack and could be closed
89
* @todo it is not verified that the topmost element was closed
90
*/
91
bool closeTag(std::ostream& into, const std::string& comment = "");
92
93
/** @brief writes a preformatted tag to the device but ensures that any
94
* pending tags are closed
95
* @param[in] into The output stream to use
96
* @param[in] val The preformatted data
97
*/
98
void writePreformattedTag(std::ostream& into, const std::string& val);
99
100
/** @brief writes arbitrary padding
101
*/
102
void writePadding(std::ostream& into, const std::string& val);
103
104
/** @brief writes an arbitrary attribute
105
*
106
* @param[in] into The output stream to use
107
* @param[in] attr The attribute (name)
108
* @param[in] val The attribute value
109
*/
110
template <class T>
111
static void writeAttr(std::ostream& into, const std::string& attr, const T& val) {
112
into << " " << attr << "=\"" << toString(val, into.precision()) << "\"";
113
}
114
115
/** @brief writes a named attribute
116
*
117
* @param[in] into The output stream to use
118
* @param[in] attr The attribute (name)
119
* @param[in] val The attribute value
120
*/
121
template <class T>
122
static void writeAttr(std::ostream& into, const SumoXMLAttr attr, const T& val) {
123
into << " " << toString(attr) << "=\"" << toString(val, into.precision()) << "\"";
124
}
125
126
void writeTime(std::ostream& into, const SumoXMLAttr attr, const SUMOTime val) {
127
into << " " << toString(attr) << "=\"" << time2string(val) << "\"";
128
}
129
130
bool wroteHeader() const {
131
return !myXMLStack.empty();
132
}
133
134
private:
135
/// @brief The stack of begun xml elements
136
std::vector<std::string> myXMLStack;
137
138
/// @brief The initial indentation level
139
int myDefaultIndentation;
140
141
/// @brief whether a closing ">" might be missing
142
bool myHavePendingOpener;
143
};
144
145
146
// ===========================================================================
147
// specialized template implementations (for speedup)
148
// ===========================================================================
149
template <>
150
inline void PlainXMLFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const double& val) {
151
#ifdef HAVE_FMT
152
fmt::print(into, " {}=\"{:.{}f}\"", toString(attr), val, into.precision());
153
#else
154
into << " " << toString(attr) << "=\"" << toString(val, into.precision()) << "\"";
155
#endif
156
}
157
158
159
template <>
160
inline void PlainXMLFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const std::string& val) {
161
into << " " << toString(attr) << "=\"" << val << "\"";
162
}
163
164