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 
37 // ===========================================================================
38 // member method definitions
39 // ===========================================================================
40 PlainXMLFormatter::PlainXMLFormatter(const int defaultIndentation)
41  : myDefaultIndentation(defaultIndentation), myHavePendingOpener(false) {
42 }
43 
44 
45 bool
46 PlainXMLFormatter::writeHeader(std::ostream& into, const SumoXMLTag& rootElement) {
47  if (myXMLStack.empty()) {
49  openTag(into, rootElement);
50  return true;
51  }
52  return false;
53 }
54 
55 
56 bool
57 PlainXMLFormatter::writeXMLHeader(std::ostream& into, const std::string& rootElement,
58  const std::map<SumoXMLAttr, std::string>& attrs) {
59  if (myXMLStack.empty()) {
61  openTag(into, rootElement);
62  for (std::map<SumoXMLAttr, std::string>::const_iterator it = attrs.begin(); it != attrs.end(); ++it) {
63  writeAttr(into, it->first, it->second);
64  }
65  into << ">\n";
66  myHavePendingOpener = false;
67  return true;
68  }
69  return false;
70 }
71 
72 
73 void
74 PlainXMLFormatter::openTag(std::ostream& into, const std::string& xmlElement) {
75  if (myHavePendingOpener) {
76  into << ">\n";
77  }
78  myHavePendingOpener = true;
79  into << std::string(4 * (myXMLStack.size() + myDefaultIndentation), ' ') << "<" << xmlElement;
80  myXMLStack.push_back(xmlElement);
81 }
82 
83 
84 void
85 PlainXMLFormatter::openTag(std::ostream& into, const SumoXMLTag& xmlElement) {
86  openTag(into, toString(xmlElement));
87 }
88 
89 
90 bool
91 PlainXMLFormatter::closeTag(std::ostream& into) {
92  if (!myXMLStack.empty()) {
93  if (myHavePendingOpener) {
94  into << "/>\n";
95  myHavePendingOpener = false;
96  } else {
97  const std::string indent(4 * (myXMLStack.size() + myDefaultIndentation - 1), ' ');
98  into << indent << "</" << myXMLStack.back() << ">\n";
99  }
100  myXMLStack.pop_back();
101  return true;
102  }
103  return false;
104 }
105 
106 
107 void
108 PlainXMLFormatter::writePreformattedTag(std::ostream& into, const std::string& val) {
109  if (myHavePendingOpener) {
110  into << ">\n";
111  myHavePendingOpener = false;
112  }
113  into << val;
114 }
115 
116 /****************************************************************************/
117 
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:65
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:56
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 ...