Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
PlainXMLFormatter.h
Go to the documentation of this file.
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/****************************************************************************/
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// ===========================================================================
41public:
43 PlainXMLFormatter(const int defaultIndentation = 0);
44
46 virtual ~PlainXMLFormatter() { }
47
60 bool writeXMLHeader(std::ostream& into, const std::string& rootElement,
61 const std::map<SumoXMLAttr, std::string>& attrs, bool writeMetadata,
62 bool includeConfig);
63
74 void openTag(std::ostream& into, const std::string& xmlElement);
75
83 void openTag(std::ostream& into, const SumoXMLTag& xmlElement);
84
91 bool closeTag(std::ostream& into, const std::string& comment = "");
92
98 void writePreformattedTag(std::ostream& into, const std::string& val);
99
102 void writePadding(std::ostream& into, const std::string& val);
103
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
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
134private:
136 std::vector<std::string> myXMLStack;
137
140
143};
144
145
146// ===========================================================================
147// specialized template implementations (for speedup)
148// ===========================================================================
149template <>
150inline 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
159template <>
160inline void PlainXMLFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const std::string& val) {
161 into << " " << toString(attr) << "=\"" << val << "\"";
162}
long long int SUMOTime
Definition GUI.h:36
std::string time2string(SUMOTime t, bool humanReadable)
convert SUMOTime to string (independently of global format setting)
Definition SUMOTime.cpp:91
SumoXMLTag
Numbers representing SUMO-XML - element names.
SumoXMLAttr
Numbers representing SUMO-XML - attributes.
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition ToString.h:46
Abstract base class for output formatters.
Output formatter for plain XML output.
bool closeTag(std::ostream &into, const std::string &comment="")
Closes the most recently opened tag.
bool myHavePendingOpener
whether a closing ">" might be missing
static void writeAttr(std::ostream &into, const SumoXMLAttr attr, const T &val)
writes a named attribute
bool writeXMLHeader(std::ostream &into, const std::string &rootElement, const std::map< SumoXMLAttr, std::string > &attrs, bool writeMetadata, bool includeConfig)
Writes an XML header with optional configuration.
int myDefaultIndentation
The initial indentation level.
bool wroteHeader() const
Returns whether a header has been written. Useful to detect whether a file is being used by multiple ...
static void writeAttr(std::ostream &into, const std::string &attr, const T &val)
writes an arbitrary attribute
std::vector< std::string > myXMLStack
The stack of begun xml elements.
void writePadding(std::ostream &into, const std::string &val)
writes arbitrary padding
void writePreformattedTag(std::ostream &into, const std::string &val)
writes a preformatted tag to the device but ensures that any pending tags are closed
virtual ~PlainXMLFormatter()
Destructor.
void writeTime(std::ostream &into, const SumoXMLAttr attr, const SUMOTime val)
void openTag(std::ostream &into, const std::string &xmlElement)
Opens an XML tag.