SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PlainXMLFormatter.cpp
Go to the documentation of this file.
1 /****************************************************************************/
7 // Static storage of an output device and its base (abstract) implementation
8 /****************************************************************************/
9 // SUMO, Simulation of Urban MObility; see http://sumo-sim.org/
10 // Copyright (C) 2001-2013 DLR (http://www.dlr.de/) and contributors
11 /****************************************************************************/
12 //
13 // This file is part of SUMO.
14 // SUMO is free software: you can redistribute it and/or modify
15 // it under the terms of the GNU General Public License as published by
16 // the Free Software Foundation, either version 3 of the License, or
17 // (at your option) any later version.
18 //
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 #ifdef CHECK_MEMORY_LEAKS
36 #include <foreign/nvwa/debug_new.h>
37 #endif // CHECK_MEMORY_LEAKS
38 
39 
40 // ===========================================================================
41 // member method definitions
42 // ===========================================================================
43 PlainXMLFormatter::PlainXMLFormatter(const unsigned int defaultIndentation)
44  : myDefaultIndentation(defaultIndentation), myHavePendingOpener(false) {
45 }
46 
47 
48 bool
49 PlainXMLFormatter::writeHeader(std::ostream& into, const SumoXMLTag& rootElement) {
50  if (myXMLStack.empty()) {
52  openTag(into, rootElement);
53  return true;
54  }
55  return false;
56 }
57 
58 
59 bool
60 PlainXMLFormatter::writeXMLHeader(std::ostream& into, const std::string& rootElement,
61  const std::string& attrs, const std::string& comment) {
62  if (myXMLStack.empty()) {
64  if (comment != "") {
65  into << comment << "\n";
66  }
67  openTag(into, rootElement);
68  if (attrs != "") {
69  into << " " << attrs;
70  }
71  into << ">\n";
72  myHavePendingOpener = false;
73  return true;
74  }
75  return false;
76 }
77 
78 
79 void
80 PlainXMLFormatter::openTag(std::ostream& into, const std::string& xmlElement) {
81  if (myHavePendingOpener) {
82  into << ">\n";
83  }
84  myHavePendingOpener = true;
85  into << std::string(4 * (myXMLStack.size() + myDefaultIndentation), ' ') << "<" << xmlElement;
86  myXMLStack.push_back(xmlElement);
87 }
88 
89 
90 void
91 PlainXMLFormatter::openTag(std::ostream& into, const SumoXMLTag& xmlElement) {
92  openTag(into, toString(xmlElement));
93 }
94 
95 
96 bool
97 PlainXMLFormatter::closeTag(std::ostream& into) {
98  if (!myXMLStack.empty()) {
99  if (myHavePendingOpener) {
100  into << "/>\n";
101  myHavePendingOpener = false;
102  } else {
103  const std::string indent(4 * (myXMLStack.size() + myDefaultIndentation - 1), ' ');
104  into << indent << "</" << myXMLStack.back() << ">\n";
105  }
106  myXMLStack.pop_back();
107  return true;
108  }
109  return false;
110 }
111 
112 
113 /****************************************************************************/
114