SUMO - Simulation of Urban MObility
PlainXMLFormatter.cpp
Go to the documentation of this file.
1 /****************************************************************************/
8 // Static storage of an output device and its base (abstract) implementation
9 /****************************************************************************/
10 // SUMO, Simulation of Urban MObility; see http://sumo-sim.org/
11 // Copyright (C) 2012-2014 DLR (http://www.dlr.de/) and contributors
12 /****************************************************************************/
13 //
14 // This file is part of SUMO.
15 // SUMO is free software: you can redistribute it and/or modify
16 // it under the terms of the GNU General Public License as published by
17 // the Free Software Foundation, either version 3 of the License, or
18 // (at your option) any later version.
19 //
20 /****************************************************************************/
21 
22 
23 // ===========================================================================
24 // included modules
25 // ===========================================================================
26 #ifdef _MSC_VER
27 #include <windows_config.h>
28 #else
29 #include <config.h>
30 #endif
31 
32 #include <utils/common/ToString.h>
34 #include "PlainXMLFormatter.h"
35 
36 #ifdef CHECK_MEMORY_LEAKS
37 #include <foreign/nvwa/debug_new.h>
38 #endif // CHECK_MEMORY_LEAKS
39 
40 
41 // ===========================================================================
42 // member method definitions
43 // ===========================================================================
44 PlainXMLFormatter::PlainXMLFormatter(const unsigned int defaultIndentation)
45  : myDefaultIndentation(defaultIndentation), myHavePendingOpener(false) {
46 }
47 
48 
49 bool
50 PlainXMLFormatter::writeHeader(std::ostream& into, const SumoXMLTag& rootElement) {
51  if (myXMLStack.empty()) {
53  openTag(into, rootElement);
54  return true;
55  }
56  return false;
57 }
58 
59 
60 bool
61 PlainXMLFormatter::writeXMLHeader(std::ostream& into, const std::string& rootElement,
62  const std::string& attrs, const std::string& comment) {
63  if (myXMLStack.empty()) {
65  if (comment != "") {
66  into << comment << "\n";
67  }
68  openTag(into, rootElement);
69  if (attrs != "") {
70  into << " " << attrs;
71  }
72  into << ">\n";
73  myHavePendingOpener = false;
74  return true;
75  }
76  return false;
77 }
78 
79 
80 void
81 PlainXMLFormatter::openTag(std::ostream& into, const std::string& xmlElement) {
82  if (myHavePendingOpener) {
83  into << ">\n";
84  }
85  myHavePendingOpener = true;
86  into << std::string(4 * (myXMLStack.size() + myDefaultIndentation), ' ') << "<" << xmlElement;
87  myXMLStack.push_back(xmlElement);
88 }
89 
90 
91 void
92 PlainXMLFormatter::openTag(std::ostream& into, const SumoXMLTag& xmlElement) {
93  openTag(into, toString(xmlElement));
94 }
95 
96 
97 bool
98 PlainXMLFormatter::closeTag(std::ostream& into) {
99  if (!myXMLStack.empty()) {
100  if (myHavePendingOpener) {
101  into << "/>\n";
102  myHavePendingOpener = false;
103  } else {
104  const std::string indent(4 * (myXMLStack.size() + myDefaultIndentation - 1), ' ');
105  into << indent << "</" << myXMLStack.back() << ">\n";
106  }
107  myXMLStack.pop_back();
108  return true;
109  }
110  return false;
111 }
112 
113 
114 void
115 PlainXMLFormatter::writePreformattedTag(std::ostream& into, const std::string& val) {
116  if (myHavePendingOpener) {
117  into << ">\n";
118  myHavePendingOpener = false;
119  }
120  into << val;
121 }
122 
123 /****************************************************************************/
124 
SumoXMLTag
Numbers representing SUMO-XML - element names.
unsigned int myDefaultIndentation
The initial indentation level.
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:67
void writeXMLHeader(std::ostream &os)
Writes a standard XML header, including the configuration.
std::string toString(const T &t, std::streamsize accuracy=OUTPUT_ACCURACY)
Definition: ToString.h:53
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.
bool writeXMLHeader(std::ostream &into, const std::string &rootElement, const std::string &attrs="", const std::string &comment="")
Writes an XML header with optional configuration.
PlainXMLFormatter(const unsigned int defaultIndentation=0)
Constructor.
void writePreformattedTag(std::ostream &into, const std::string &val)
writes a preformatted tag to the device but ensures that any pending tags are closed ...