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-2026 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#include <functional>
25#include "OutputFormatter.h"
26
27
28// ===========================================================================
29// class definitions
30// ===========================================================================
38public:
40 PlainXMLFormatter(const int defaultIndentation = 0);
41
43 virtual ~PlainXMLFormatter() { }
44
52 static void setHeaderWriters(std::function<void(std::ostream&, bool)> headerWriter,
53 std::function<void(std::ostream&)> metadataWriter) {
54 myHeaderWriter = headerWriter;
55 myMetadataWriter = metadataWriter;
56 }
57
70 bool writeXMLHeader(std::ostream& into, const std::string& rootElement,
71 const std::map<SumoXMLAttr, std::string>& attrs, bool includeConfig);
72
82 void openTag(std::ostream& into, const std::string& xmlElement);
83
91 void openTag(std::ostream& into, const SumoXMLTag& xmlElement);
92
98 bool closeTag(std::ostream& into, const std::string& comment = "");
99
105 void writePreformattedTag(std::ostream& into, const std::string& val);
106
109 void writePadding(std::ostream& into, const std::string& val);
110
118 template <class T>
119 static void writeAttr(std::ostream& into, const std::string& attr, const T& val, const bool escape) {
120 UNUSED_PARAMETER(escape);
121 into << " " << attr << "=\"" << toString(val, into.precision()) << "\"";
122 }
123
131 template <class T>
132 static void writeAttr(std::ostream& into, const SumoXMLAttr attr, const T& val, const bool escape) {
133 UNUSED_PARAMETER(escape);
134 into << " " << toString(attr) << "=\"" << toString(val, into.precision()) << "\"";
135 }
136
138 static void writeAttr(std::ostream& into, const std::string& attr, const std::string& val, const bool escape);
139 static void writeAttr(std::ostream& into, const SumoXMLAttr attr, const std::string& val, const bool escape);
140
141 void writeTime(std::ostream& into, const SumoXMLAttr attr, const SUMOTime val) {
142 into << " " << toString(attr) << "=\"" << time2string(val) << "\"";
143 }
144
145 bool wroteHeader() const {
146 return !myXMLStack.empty();
147 }
148
149private:
151 static std::function<void(std::ostream&, bool)> myHeaderWriter;
152
154 static std::function<void(std::ostream&)> myMetadataWriter;
155
157 std::vector<std::string> myXMLStack;
158
161
164};
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:107
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:57
Abstract base class for output formatters.
Output formatter for plain XML output.
bool writeXMLHeader(std::ostream &into, const std::string &rootElement, const std::map< SumoXMLAttr, std::string > &attrs, bool includeConfig)
Writes an XML header with optional configuration.
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 std::string &attr, const T &val, const bool escape)
writes an arbitrary attribute
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 setHeaderWriters(std::function< void(std::ostream &, bool)> headerWriter, std::function< void(std::ostream &)> metadataWriter)
Sets the functions which write the file header and the metadata block.
static std::function< void(std::ostream &, bool)> myHeaderWriter
writes the XML declaration and optionally the configuration comment
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
static std::function< void(std::ostream &)> myMetadataWriter
writes the metadata block
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.
static void writeAttr(std::ostream &into, const SumoXMLAttr attr, const T &val, const bool escape)
writes a named attribute
#define UNUSED_PARAMETER(x)