Eclipse SUMO - Simulation of Urban MObility
PlainXMLFormatter.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2012-2019 German Aerospace Center (DLR) and others.
4 // This program and the accompanying materials
5 // are made available under the terms of the Eclipse Public License v2.0
6 // which accompanies this distribution, and is available at
7 // http://www.eclipse.org/legal/epl-v20.html
8 // SPDX-License-Identifier: EPL-2.0
9 /****************************************************************************/
15 // Static storage of an output device and its base (abstract) implementation
16 /****************************************************************************/
17 
18 
19 // ===========================================================================
20 // included modules
21 // ===========================================================================
22 #include <config.h>
23 
24 #include <utils/common/ToString.h>
26 #include "PlainXMLFormatter.h"
27 
28 
29 // ===========================================================================
30 // member method definitions
31 // ===========================================================================
32 PlainXMLFormatter::PlainXMLFormatter(const int defaultIndentation)
33  : myDefaultIndentation(defaultIndentation), myHavePendingOpener(false) {
34 }
35 
36 
37 bool
38 PlainXMLFormatter::writeHeader(std::ostream& into, const SumoXMLTag& rootElement) {
39  if (myXMLStack.empty()) {
41  openTag(into, rootElement);
42  return true;
43  }
44  return false;
45 }
46 
47 
48 bool
49 PlainXMLFormatter::writeXMLHeader(std::ostream& into, const std::string& rootElement,
50  const std::map<SumoXMLAttr, std::string>& attrs) {
51  if (myXMLStack.empty()) {
53  openTag(into, rootElement);
54  for (std::map<SumoXMLAttr, std::string>::const_iterator it = attrs.begin(); it != attrs.end(); ++it) {
55  writeAttr(into, it->first, it->second);
56  }
57  into << ">\n";
58  myHavePendingOpener = false;
59  return true;
60  }
61  return false;
62 }
63 
64 
65 void
66 PlainXMLFormatter::openTag(std::ostream& into, const std::string& xmlElement) {
67  if (myHavePendingOpener) {
68  into << ">\n";
69  }
70  myHavePendingOpener = true;
71  into << std::string(4 * (myXMLStack.size() + myDefaultIndentation), ' ') << "<" << xmlElement;
72  myXMLStack.push_back(xmlElement);
73 }
74 
75 
76 void
77 PlainXMLFormatter::openTag(std::ostream& into, const SumoXMLTag& xmlElement) {
78  openTag(into, toString(xmlElement));
79 }
80 
81 
82 bool
83 PlainXMLFormatter::closeTag(std::ostream& into, const std::string& comment) {
84  if (!myXMLStack.empty()) {
85  if (myHavePendingOpener) {
86  into << "/>" << comment << "\n";
87  myHavePendingOpener = false;
88  } else {
89  const std::string indent(4 * (myXMLStack.size() + myDefaultIndentation - 1), ' ');
90  into << indent << "</" << myXMLStack.back() << ">" << comment << "\n";
91  }
92  myXMLStack.pop_back();
93  return true;
94  }
95  return false;
96 }
97 
98 
99 void
100 PlainXMLFormatter::writePreformattedTag(std::ostream& into, const std::string& val) {
101  if (myHavePendingOpener) {
102  into << ">\n";
103  myHavePendingOpener = false;
104  }
105  into << val;
106 }
107 
108 void
109 PlainXMLFormatter::writePadding(std::ostream& into, const std::string& val) {
110  into << val;
111 }
112 
113 /****************************************************************************/
114 
PlainXMLFormatter::writePadding
void writePadding(std::ostream &into, const std::string &val)
writes arbitrary padding
Definition: PlainXMLFormatter.cpp:109
ToString.h
OptionsCont::writeXMLHeader
void writeXMLHeader(std::ostream &os, const bool includeConfig=true) const
Writes a standard XML header, including the configuration.
Definition: OptionsCont.cpp:897
OptionsCont.h
PlainXMLFormatter::myDefaultIndentation
int myDefaultIndentation
The initial indentation level.
Definition: PlainXMLFormatter.h:146
OptionsCont::getOptions
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:57
SumoXMLTag
SumoXMLTag
Numbers representing SUMO-XML - element names.
Definition: SUMOXMLDefinitions.h:41
PlainXMLFormatter::PlainXMLFormatter
PlainXMLFormatter(const int defaultIndentation=0)
Constructor.
Definition: PlainXMLFormatter.cpp:32
PlainXMLFormatter::myXMLStack
std::vector< std::string > myXMLStack
The stack of begun xml elements.
Definition: PlainXMLFormatter.h:143
PlainXMLFormatter::writeHeader
bool writeHeader(std::ostream &into, const SumoXMLTag &rootElement)
Writes an XML header with optional configuration.
Definition: PlainXMLFormatter.cpp:38
PlainXMLFormatter.h
toString
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:47
PlainXMLFormatter::writePreformattedTag
void writePreformattedTag(std::ostream &into, const std::string &val)
writes a preformatted tag to the device but ensures that any pending tags are closed
Definition: PlainXMLFormatter.cpp:100
PlainXMLFormatter::myHavePendingOpener
bool myHavePendingOpener
whether a closing ">" might be missing
Definition: PlainXMLFormatter.h:149
config.h
PlainXMLFormatter::writeXMLHeader
bool writeXMLHeader(std::ostream &into, const std::string &rootElement, const std::map< SumoXMLAttr, std::string > &attrs)
Writes an XML header with optional configuration.
Definition: PlainXMLFormatter.cpp:49
PlainXMLFormatter::openTag
void openTag(std::ostream &into, const std::string &xmlElement)
Opens an XML tag.
Definition: PlainXMLFormatter.cpp:66
PlainXMLFormatter::closeTag
bool closeTag(std::ostream &into, const std::string &comment="")
Closes the most recently opened tag.
Definition: PlainXMLFormatter.cpp:83
PlainXMLFormatter::writeAttr
static void writeAttr(std::ostream &into, const std::string &attr, const T &val)
writes an arbitrary attribute
Definition: PlainXMLFormatter.h:124