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-2017 German Aerospace Center (DLR) and others.
4 /****************************************************************************/
5 //
6 // This program and the accompanying materials
7 // are made available under the terms of the Eclipse Public License v2.0
8 // which accompanies this distribution, and is available at
9 // http://www.eclipse.org/legal/epl-v20.html
10 //
11 /****************************************************************************/
18 // Static storage of an output device and its base (abstract) implementation
19 /****************************************************************************/
20 
21 
22 // ===========================================================================
23 // included modules
24 // ===========================================================================
25 #ifdef _MSC_VER
26 #include <windows_config.h>
27 #else
28 #include <config.h>
29 #endif
30 
31 #include <utils/common/ToString.h>
33 #include "PlainXMLFormatter.h"
34 
35 
36 // ===========================================================================
37 // member method definitions
38 // ===========================================================================
39 PlainXMLFormatter::PlainXMLFormatter(const int defaultIndentation)
40  : myDefaultIndentation(defaultIndentation), myHavePendingOpener(false) {
41 }
42 
43 
44 bool
45 PlainXMLFormatter::writeHeader(std::ostream& into, const SumoXMLTag& rootElement) {
46  if (myXMLStack.empty()) {
48  openTag(into, rootElement);
49  return true;
50  }
51  return false;
52 }
53 
54 
55 bool
56 PlainXMLFormatter::writeXMLHeader(std::ostream& into, const std::string& rootElement,
57  const std::map<SumoXMLAttr, std::string>& attrs) {
58  if (myXMLStack.empty()) {
60  openTag(into, rootElement);
61  for (std::map<SumoXMLAttr, std::string>::const_iterator it = attrs.begin(); it != attrs.end(); ++it) {
62  writeAttr(into, it->first, it->second);
63  }
64  into << ">\n";
65  myHavePendingOpener = false;
66  return true;
67  }
68  return false;
69 }
70 
71 
72 void
73 PlainXMLFormatter::openTag(std::ostream& into, const std::string& xmlElement) {
74  if (myHavePendingOpener) {
75  into << ">\n";
76  }
77  myHavePendingOpener = true;
78  into << std::string(4 * (myXMLStack.size() + myDefaultIndentation), ' ') << "<" << xmlElement;
79  myXMLStack.push_back(xmlElement);
80 }
81 
82 
83 void
84 PlainXMLFormatter::openTag(std::ostream& into, const SumoXMLTag& xmlElement) {
85  openTag(into, toString(xmlElement));
86 }
87 
88 
89 bool
90 PlainXMLFormatter::closeTag(std::ostream& into) {
91  if (!myXMLStack.empty()) {
92  if (myHavePendingOpener) {
93  into << "/>\n";
94  myHavePendingOpener = false;
95  } else {
96  const std::string indent(4 * (myXMLStack.size() + myDefaultIndentation - 1), ' ');
97  into << indent << "</" << myXMLStack.back() << ">\n";
98  }
99  myXMLStack.pop_back();
100  return true;
101  }
102  return false;
103 }
104 
105 
106 void
107 PlainXMLFormatter::writePreformattedTag(std::ostream& into, const std::string& val) {
108  if (myHavePendingOpener) {
109  into << ">\n";
110  myHavePendingOpener = false;
111  }
112  into << val;
113 }
114 
115 /****************************************************************************/
116 
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.
bool closeTag(std::ostream &into)
Closes the most recently opened tag.
void openTag(std::ostream &into, const std::string &xmlElement)
Opens an XML tag.
bool myHavePendingOpener
whether a closing ">" might be missing
static OptionsCont & getOptions()
Retrieves the options.
Definition: OptionsCont.cpp:64
void writeXMLHeader(std::ostream &os)
Writes a standard XML header, including the configuration.
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:55
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 writePreformattedTag(std::ostream &into, const std::string &val)
writes a preformatted tag to the device but ensures that any pending tags are closed ...