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 #include "OutputFormatter.h"
25 
26 
27 // ===========================================================================
28 // class definitions
29 // ===========================================================================
37 public:
39  PlainXMLFormatter(const int defaultIndentation = 0);
40 
41 
43  virtual ~PlainXMLFormatter() { }
44 
45 
56  bool writeXMLHeader(std::ostream& into, const std::string& rootElement,
57  const std::map<SumoXMLAttr, std::string>& attrs,
58  bool includeConfig = true);
59 
60 
69  bool writeHeader(std::ostream& into, const SumoXMLTag& rootElement);
70 
71 
82  void openTag(std::ostream& into, const std::string& xmlElement);
83 
84 
92  void openTag(std::ostream& into, const SumoXMLTag& xmlElement);
93 
94 
101  bool closeTag(std::ostream& into, const std::string& comment = "");
102 
103 
109  void writePreformattedTag(std::ostream& into, const std::string& val);
110 
113  void writePadding(std::ostream& into, const std::string& val);
114 
115 
122  template <class T>
123  static void writeAttr(std::ostream& into, const std::string& attr, const T& val) {
124  into << " " << attr << "=\"" << toString(val, into.precision()) << "\"";
125  }
126 
127 
134  template <class T>
135  static void writeAttr(std::ostream& into, const SumoXMLAttr attr, const T& val) {
136  into << " " << toString(attr) << "=\"" << toString(val, into.precision()) << "\"";
137  }
138 
139  bool wroteHeader() const {
140  return !myXMLStack.empty();
141  }
142 
143 private:
145  std::vector<std::string> myXMLStack;
146 
149 
152 };
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.