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-2018 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 /****************************************************************************/
16 // Static storage of an output device and its base (abstract) implementation
17 /****************************************************************************/
18 
19 
20 // ===========================================================================
21 // included modules
22 // ===========================================================================
23 #include <config.h>
24 
25 #include <utils/common/ToString.h>
27 #include "PlainXMLFormatter.h"
28 
29 
30 // ===========================================================================
31 // member method definitions
32 // ===========================================================================
33 PlainXMLFormatter::PlainXMLFormatter(const int defaultIndentation)
34  : myDefaultIndentation(defaultIndentation), myHavePendingOpener(false) {
35 }
36 
37 
38 bool
39 PlainXMLFormatter::writeHeader(std::ostream& into, const SumoXMLTag& rootElement) {
40  if (myXMLStack.empty()) {
42  openTag(into, rootElement);
43  return true;
44  }
45  return false;
46 }
47 
48 
49 bool
50 PlainXMLFormatter::writeXMLHeader(std::ostream& into, const std::string& rootElement,
51  const std::map<SumoXMLAttr, std::string>& attrs) {
52  if (myXMLStack.empty()) {
54  openTag(into, rootElement);
55  for (std::map<SumoXMLAttr, std::string>::const_iterator it = attrs.begin(); it != attrs.end(); ++it) {
56  writeAttr(into, it->first, it->second);
57  }
58  into << ">\n";
59  myHavePendingOpener = false;
60  return true;
61  }
62  return false;
63 }
64 
65 
66 void
67 PlainXMLFormatter::openTag(std::ostream& into, const std::string& xmlElement) {
68  if (myHavePendingOpener) {
69  into << ">\n";
70  }
71  myHavePendingOpener = true;
72  into << std::string(4 * (myXMLStack.size() + myDefaultIndentation), ' ') << "<" << xmlElement;
73  myXMLStack.push_back(xmlElement);
74 }
75 
76 
77 void
78 PlainXMLFormatter::openTag(std::ostream& into, const SumoXMLTag& xmlElement) {
79  openTag(into, toString(xmlElement));
80 }
81 
82 
83 bool
84 PlainXMLFormatter::closeTag(std::ostream& into, const std::string& comment) {
85  if (!myXMLStack.empty()) {
86  if (myHavePendingOpener) {
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 
100 void
101 PlainXMLFormatter::writePreformattedTag(std::ostream& into, const std::string& val) {
102  if (myHavePendingOpener) {
103  into << ">\n";
104  myHavePendingOpener = false;
105  }
106  into << val;
107 }
108 
109 void
110 PlainXMLFormatter::writePadding(std::ostream& into, const std::string& val) {
111  into << val;
112 }
113 
114 /****************************************************************************/
115 
SumoXMLTag
Numbers representing SUMO-XML - element names.
bool writeXMLHeader(std::ostream &into, const std::string &rootElement, const std::map< SumoXMLAttr, std::string > &attrs)
Writes an XML header with optional configuration.
void openTag(std::ostream &into, const std::string &xmlElement)
Opens an XML tag.
void writeXMLHeader(std::ostream &os, const bool includeConfig=true) const
Writes a standard XML header, including the configuration.
bool myHavePendingOpener
whether a closing ">" might be missing
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:58
static void writeAttr(std::ostream &into, const std::string &attr, const T &val)
writes an arbitrary attribute
std::string toString(const T &t, std::streamsize accuracy=gPrecision)
Definition: ToString.h:49
bool closeTag(std::ostream &into, const std::string &comment="")
Closes the most recently opened tag.
PlainXMLFormatter(const int defaultIndentation=0)
Constructor.
int myDefaultIndentation
The initial indentation level.
std::vector< std::string > myXMLStack
The stack of begun xml elements.
bool writeHeader(std::ostream &into, const SumoXMLTag &rootElement)
Writes an XML header with optional configuration.
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 ...