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 "OutputFormatter.h"
25
26
27// ===========================================================================
28// class definitions
29// ===========================================================================
37public:
39 PlainXMLFormatter(const int defaultIndentation = 0);
40
42 virtual ~PlainXMLFormatter() { }
43
56 bool writeXMLHeader(std::ostream& into, const std::string& rootElement,
57 const std::map<SumoXMLAttr, std::string>& attrs, bool writeMetadata,
58 bool includeConfig);
59
70 void openTag(std::ostream& into, const std::string& xmlElement);
71
79 void openTag(std::ostream& into, const SumoXMLTag& xmlElement);
80
87 bool closeTag(std::ostream& into, const std::string& comment = "");
88
94 void writePreformattedTag(std::ostream& into, const std::string& val);
95
98 void writePadding(std::ostream& into, const std::string& val);
99
106 template <class T>
107 static void writeAttr(std::ostream& into, const std::string& attr, const T& val) {
108 into << " " << attr << "=\"" << toString(val, into.precision()) << "\"";
109 }
110
117 template <class T>
118 static void writeAttr(std::ostream& into, const SumoXMLAttr attr, const T& val) {
119 into << " " << toString(attr) << "=\"" << toString(val, into.precision()) << "\"";
120 }
121
122 void writeTime(std::ostream& into, const SumoXMLAttr attr, const SUMOTime val) {
123 into << " " << toString(attr) << "=\"" << time2string(val) << "\"";
124 }
125
126 bool wroteHeader() const {
127 return !myXMLStack.empty();
128 }
129
130private:
132 std::vector<std::string> myXMLStack;
133
136
139};
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:49
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.