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.dlr.de/
11 // Copyright (C) 2012-2017 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 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::map<SumoXMLAttr, std::string>& attrs) {
63  if (myXMLStack.empty()) {
65  openTag(into, rootElement);
66  for (std::map<SumoXMLAttr, std::string>::const_iterator it = attrs.begin(); it != attrs.end(); ++it) {
67  writeAttr(into, it->first, it->second);
68  }
69  into << ">\n";
70  myHavePendingOpener = false;
71  return true;
72  }
73  return false;
74 }
75 
76 
77 void
78 PlainXMLFormatter::openTag(std::ostream& into, const std::string& xmlElement) {
79  if (myHavePendingOpener) {
80  into << ">\n";
81  }
82  myHavePendingOpener = true;
83  into << std::string(4 * (myXMLStack.size() + myDefaultIndentation), ' ') << "<" << xmlElement;
84  myXMLStack.push_back(xmlElement);
85 }
86 
87 
88 void
89 PlainXMLFormatter::openTag(std::ostream& into, const SumoXMLTag& xmlElement) {
90  openTag(into, toString(xmlElement));
91 }
92 
93 
94 bool
95 PlainXMLFormatter::closeTag(std::ostream& into) {
96  if (!myXMLStack.empty()) {
97  if (myHavePendingOpener) {
98  into << "/>\n";
99  myHavePendingOpener = false;
100  } else {
101  const std::string indent(4 * (myXMLStack.size() + myDefaultIndentation - 1), ' ');
102  into << indent << "</" << myXMLStack.back() << ">\n";
103  }
104  myXMLStack.pop_back();
105  return true;
106  }
107  return false;
108 }
109 
110 
111 void
112 PlainXMLFormatter::writePreformattedTag(std::ostream& into, const std::string& val) {
113  if (myHavePendingOpener) {
114  into << ">\n";
115  myHavePendingOpener = false;
116  }
117  into << val;
118 }
119 
120 /****************************************************************************/
121 
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:69
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 ...