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
25#include "PlainXMLFormatter.h"
26
27
28// ===========================================================================
29// static member definitions
30// ===========================================================================
31std::function<void(std::ostream&, bool)> PlainXMLFormatter::myHeaderWriter;
32std::function<void(std::ostream&)> PlainXMLFormatter::myMetadataWriter;
33
34
35// ===========================================================================
36// member method definitions
37// ===========================================================================
38PlainXMLFormatter::PlainXMLFormatter(const int defaultIndentation)
39 : OutputFormatter(OutputFormatterType::XML), myDefaultIndentation(defaultIndentation), myHavePendingOpener(false) {
40}
41
42
43bool
44PlainXMLFormatter::writeXMLHeader(std::ostream& into, const std::string& rootElement,
45 const std::map<SumoXMLAttr, std::string>& attrs,
46 bool includeConfig) {
47 if (myXMLStack.empty()) {
48 if (myHeaderWriter) {
49 myHeaderWriter(into, includeConfig);
50 }
51 openTag(into, rootElement);
52 for (std::map<SumoXMLAttr, std::string>::const_iterator it = attrs.begin(); it != attrs.end(); ++it) {
53 writeAttr(into, it->first, it->second, false);
54 }
55 into << ">\n";
56 if (myMetadataWriter) {
57 myMetadataWriter(into);
58 }
59 myHavePendingOpener = false;
60 return true;
61 }
62 return false;
63}
64
65
66void
67PlainXMLFormatter::openTag(std::ostream& into, const std::string& xmlElement) {
69 into << ">\n";
70 }
72 into << std::string(4 * (myXMLStack.size() + myDefaultIndentation), ' ') << "<" << xmlElement;
73 myXMLStack.push_back(xmlElement);
74}
75
76
77void
78PlainXMLFormatter::openTag(std::ostream& into, const SumoXMLTag& xmlElement) {
79 openTag(into, toString(xmlElement));
80}
81
82
83bool
84PlainXMLFormatter::closeTag(std::ostream& into, const std::string& comment) {
85 if (!myXMLStack.empty()) {
87 into << "/>" << comment << "\n";
88 myHavePendingOpener = false;
89 } else {
90 const std::string indent(4 * (myXMLStack.size() + myDefaultIndentation - 1), ' ');
91 into << indent << "</" << myXMLStack.back() << ">" << comment << "\n";
92 }
93 myXMLStack.pop_back();
94 return true;
95 }
96 return false;
97}
98
99
100void
101PlainXMLFormatter::writePreformattedTag(std::ostream& into, const std::string& val) {
103 into << ">\n";
104 myHavePendingOpener = false;
105 }
106 into << val;
107}
108
109
110void
111PlainXMLFormatter::writePadding(std::ostream& into, const std::string& val) {
112 into << val;
113}
114
115
116void
117PlainXMLFormatter::writeAttr(std::ostream& into, const std::string& attr, const std::string& val, const bool escape) {
118 into << " " << attr << "=\"" << (escape ? StringUtils::escapeXML(val) : val) << "\"";
119}
120
121
122void
123PlainXMLFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const std::string& val, const bool escape) {
124 into << " " << toString(attr) << "=\"" << (escape ? StringUtils::escapeXML(val) : val) << "\"";
125}
126
127
128/****************************************************************************/
OutputFormatterType
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.
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
PlainXMLFormatter(const int defaultIndentation=0)
Constructor.
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.
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
void openTag(std::ostream &into, const std::string &xmlElement)
Opens an XML tag.
static std::string escapeXML(const std::string &orig, const bool maskDoubleHyphen=false)
Replaces the standard escapes by their XML entities.