Eclipse SUMO - Simulation of Urban MObility
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-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 // Output formatter for plain XML output
20 /****************************************************************************/
21 #pragma once
22 #include <config.h>
23 
24 #ifdef HAVE_FMT
25 #include <fmt/ostream.h>
26 #endif
27 
28 #include "OutputFormatter.h"
29 
30 
31 // ===========================================================================
32 // class definitions
33 // ===========================================================================
41 public:
43  PlainXMLFormatter(const int defaultIndentation = 0);
44 
45 
47  virtual ~PlainXMLFormatter() { }
48 
49 
60  bool writeXMLHeader(std::ostream& into, const std::string& rootElement,
61  const std::map<SumoXMLAttr, std::string>& attrs,
62  bool includeConfig = true);
63 
64 
73  bool writeHeader(std::ostream& into, const SumoXMLTag& rootElement);
74 
75 
86  void openTag(std::ostream& into, const std::string& xmlElement);
87 
88 
96  void openTag(std::ostream& into, const SumoXMLTag& xmlElement);
97 
98 
105  bool closeTag(std::ostream& into, const std::string& comment = "");
106 
107 
113  void writePreformattedTag(std::ostream& into, const std::string& val);
114 
117  void writePadding(std::ostream& into, const std::string& val);
118 
119 
126  template <class T>
127  static void writeAttr(std::ostream& into, const std::string& attr, const T& val) {
128  into << " " << attr << "=\"" << toString(val, into.precision()) << "\"";
129  }
130 
131 
138  template <class T>
139  static void writeAttr(std::ostream& into, const SumoXMLAttr attr, const T& val) {
140  into << " " << toString(attr) << "=\"" << toString(val, into.precision()) << "\"";
141  }
142 
143  bool wroteHeader() const {
144  return !myXMLStack.empty();
145  }
146 
147 private:
149  std::vector<std::string> myXMLStack;
150 
153 
156 };
157 
158 
159 // ===========================================================================
160 // specialized template implementations (for speedup)
161 // ===========================================================================
162 template <>
163 inline void PlainXMLFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const double& val) {
164 #ifdef HAVE_FMT
165  fmt::print(into, " {}=\"{:.{}f}\"", toString(attr), val, into.precision());
166 #else
167  into << " " << toString(attr) << "=\"" << val << "\"";
168 #endif
169 }
170 
171 
172 template <>
173 inline void PlainXMLFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const std::string& val) {
174  into << " " << toString(attr) << "=\"" << val << "\"";
175 }
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:46
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
PlainXMLFormatter(const int defaultIndentation=0)
Constructor.
static void writeAttr(std::ostream &into, const SumoXMLAttr attr, const T &val)
writes a named attribute
int myDefaultIndentation
The initial indentation level.
bool wroteHeader() const
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
virtual ~PlainXMLFormatter()
Destructor.
void openTag(std::ostream &into, const std::string &xmlElement)
Opens an XML tag.