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-2024 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// member method definitions
30// ===========================================================================
31PlainXMLFormatter::PlainXMLFormatter(const int defaultIndentation)
32 : myDefaultIndentation(defaultIndentation), myHavePendingOpener(false) {
33}
34
35
36bool
37PlainXMLFormatter::writeHeader(std::ostream& into, const SumoXMLTag& rootElement) {
38 if (myXMLStack.empty()) {
40 openTag(into, rootElement);
41 return true;
42 }
43 return false;
44}
45
46
47bool
48PlainXMLFormatter::writeXMLHeader(std::ostream& into, const std::string& rootElement,
49 const std::map<SumoXMLAttr, std::string>& attrs, bool includeConfig) {
50 if (myXMLStack.empty()) {
51 OptionsCont::getOptions().writeXMLHeader(into, includeConfig);
52 openTag(into, rootElement);
53 for (std::map<SumoXMLAttr, std::string>::const_iterator it = attrs.begin(); it != attrs.end(); ++it) {
54 writeAttr(into, it->first, it->second);
55 }
56 into << ">\n";
57 myHavePendingOpener = false;
58 return true;
59 }
60 return false;
61}
62
63
64void
65PlainXMLFormatter::openTag(std::ostream& into, const std::string& xmlElement) {
67 into << ">\n";
68 }
70 into << std::string(4 * (myXMLStack.size() + myDefaultIndentation), ' ') << "<" << xmlElement;
71 myXMLStack.push_back(xmlElement);
72}
73
74
75void
76PlainXMLFormatter::openTag(std::ostream& into, const SumoXMLTag& xmlElement) {
77 openTag(into, toString(xmlElement));
78}
79
80
81bool
82PlainXMLFormatter::closeTag(std::ostream& into, const std::string& comment) {
83 if (!myXMLStack.empty()) {
85 into << "/>" << comment << "\n";
86 myHavePendingOpener = false;
87 } else {
88 const std::string indent(4 * (myXMLStack.size() + myDefaultIndentation - 1), ' ');
89 into << indent << "</" << myXMLStack.back() << ">" << comment << "\n";
90 }
91 myXMLStack.pop_back();
92 return true;
93 }
94 return false;
95}
96
97
98void
99PlainXMLFormatter::writePreformattedTag(std::ostream& into, const std::string& val) {
101 into << ">\n";
102 myHavePendingOpener = false;
103 }
104 into << val;
105}
106
107void
108PlainXMLFormatter::writePadding(std::ostream& into, const std::string& val) {
109 into << val;
110}
111
112
113/****************************************************************************/
SumoXMLTag
Numbers representing SUMO-XML - element names.
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition ToString.h:46
void writeXMLHeader(std::ostream &os, const bool includeConfig=true) const
Writes a standard XML header, including the configuration.
static OptionsCont & getOptions()
Retrieves the options.
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.
int myDefaultIndentation
The initial indentation level.
static void writeAttr(std::ostream &into, const std::string &attr, const T &val)
writes an arbitrary attribute
bool writeHeader(std::ostream &into, const SumoXMLTag &rootElement)
Writes an XML header with optional configuration.
std::vector< std::string > myXMLStack
The stack of begun xml elements.
void writePadding(std::ostream &into, const std::string &val)
writes arbitrary padding
bool writeXMLHeader(std::ostream &into, const std::string &rootElement, const std::map< SumoXMLAttr, std::string > &attrs, bool includeConfig=true)
Writes an XML header with optional configuration.
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.