Eclipse SUMO - Simulation of Urban MObility
Loading...
Searching...
No Matches
PlainXMLFormatter.cpp
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// Static storage of an output device and its base (abstract) implementation
20/****************************************************************************/
21#include <config.h>
22
26#include "PlainXMLFormatter.h"
27
28
29// ===========================================================================
30// member method definitions
31// ===========================================================================
32PlainXMLFormatter::PlainXMLFormatter(const int defaultIndentation)
33 : OutputFormatter(OutputFormatterType::XML), myDefaultIndentation(defaultIndentation), myHavePendingOpener(false) {
34}
35
36
37bool
38PlainXMLFormatter::writeXMLHeader(std::ostream& into, const std::string& rootElement,
39 const std::map<SumoXMLAttr, std::string>& attrs,
40 bool writeMetadata, bool includeConfig) {
41 if (myXMLStack.empty()) {
43 oc.writeXMLHeader(into, includeConfig);
44 openTag(into, rootElement);
45 for (std::map<SumoXMLAttr, std::string>::const_iterator it = attrs.begin(); it != attrs.end(); ++it) {
46 writeAttr(into, it->first, it->second);
47 }
48 into << ">\n";
49 if (writeMetadata) {
50 into << " <metadata created_at=\"" << StringUtils::isoTimeString() << "\" created_by=\"" << oc.getFullName() << "\">\n";
51 oc.writeConfiguration(into, true, false, false, "", false, true, " ");
52 into << " </metadata>\n";
53 }
54 myHavePendingOpener = false;
55 return true;
56 }
57 return false;
58}
59
60
61void
62PlainXMLFormatter::openTag(std::ostream& into, const std::string& xmlElement) {
64 into << ">\n";
65 }
67 into << std::string(4 * (myXMLStack.size() + myDefaultIndentation), ' ') << "<" << xmlElement;
68 myXMLStack.push_back(xmlElement);
69}
70
71
72void
73PlainXMLFormatter::openTag(std::ostream& into, const SumoXMLTag& xmlElement) {
74 openTag(into, toString(xmlElement));
75}
76
77
78bool
79PlainXMLFormatter::closeTag(std::ostream& into, const std::string& comment) {
80 if (!myXMLStack.empty()) {
82 into << "/>" << comment << "\n";
83 myHavePendingOpener = false;
84 } else {
85 const std::string indent(4 * (myXMLStack.size() + myDefaultIndentation - 1), ' ');
86 into << indent << "</" << myXMLStack.back() << ">" << comment << "\n";
87 }
88 myXMLStack.pop_back();
89 return true;
90 }
91 return false;
92}
93
94
95void
96PlainXMLFormatter::writePreformattedTag(std::ostream& into, const std::string& val) {
98 into << ">\n";
99 myHavePendingOpener = false;
100 }
101 into << val;
102}
103
104
105void
106PlainXMLFormatter::writePadding(std::ostream& into, const std::string& val) {
107 into << val;
108}
109
110
111/****************************************************************************/
OutputFormatterType
SumoXMLTag
Numbers representing SUMO-XML - element names.
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition ToString.h:49
A storage for options typed value containers)
Definition OptionsCont.h:89
void writeConfiguration(std::ostream &os, const bool filled, const bool complete, const bool addComments, const std::string &relativeTo="", const bool forceRelative=false, const bool inComment=false, const std::string &indent="") const
Writes the configuration.
void writeXMLHeader(std::ostream &os, const bool includeConfig=true) const
Writes a standard XML header, including the configuration.
const std::string & getFullName() const
get options full name
static OptionsCont & getOptions()
Retrieves the options.
Abstract base class for output formatters.
bool closeTag(std::ostream &into, const std::string &comment="")
Closes the most recently opened tag.
bool myHavePendingOpener
whether a closing ">" might be missing
PlainXMLFormatter(const int defaultIndentation=0)
Constructor.
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.
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
void openTag(std::ostream &into, const std::string &xmlElement)
Opens an XML tag.
static std::string isoTimeString(const std::chrono::time_point< std::chrono::system_clock > *const timeRef=nullptr)
Returns an ISO8601 formatted time string with microsecond precision.