Line data Source code
1 : /****************************************************************************/
2 : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3 : // Copyright (C) 2012-2026 German Aerospace Center (DLR) and others.
4 : // This program and the accompanying materials are made available under the
5 : // terms of the Eclipse Public License 2.0 which is available at
6 : // https://www.eclipse.org/legal/epl-2.0/
7 : // This Source Code may also be made available under the following Secondary
8 : // Licenses when the conditions for such availability set forth in the Eclipse
9 : // Public License 2.0 are satisfied: GNU General Public License, version 2
10 : // or later which is available at
11 : // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12 : // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13 : /****************************************************************************/
14 : /// @file PlainXMLFormatter.cpp
15 : /// @author Daniel Krajzewicz
16 : /// @author Michael Behrisch
17 : /// @date 2012
18 : ///
19 : // Static storage of an output device and its base (abstract) implementation
20 : /****************************************************************************/
21 : #include <config.h>
22 :
23 : #include <utils/common/ToString.h>
24 : #include <utils/common/StringUtils.h>
25 : #include <utils/options/OptionsCont.h>
26 : #include "PlainXMLFormatter.h"
27 :
28 :
29 : // ===========================================================================
30 : // member method definitions
31 : // ===========================================================================
32 1833507 : PlainXMLFormatter::PlainXMLFormatter(const int defaultIndentation)
33 1833507 : : OutputFormatter(OutputFormatterType::XML), myDefaultIndentation(defaultIndentation), myHavePendingOpener(false) {
34 1833507 : }
35 :
36 :
37 : bool
38 99491 : PlainXMLFormatter::writeXMLHeader(std::ostream& into, const std::string& rootElement,
39 : const std::map<SumoXMLAttr, std::string>& attrs,
40 : bool writeMetadata, bool includeConfig) {
41 99491 : if (myXMLStack.empty()) {
42 87593 : const OptionsCont& oc = OptionsCont::getOptions();
43 87593 : oc.writeXMLHeader(into, includeConfig);
44 87593 : openTag(into, rootElement);
45 263418 : for (std::map<SumoXMLAttr, std::string>::const_iterator it = attrs.begin(); it != attrs.end(); ++it) {
46 175825 : writeAttr(into, it->first, it->second, false);
47 : }
48 87593 : into << ">\n";
49 87593 : if (writeMetadata) {
50 3 : into << " <metadata created_at=\"" << StringUtils::isoTimeString() << "\" created_by=\"" << oc.getFullName() << "\">\n";
51 2 : oc.writeConfiguration(into, true, false, false, "", false, true, " ");
52 1 : into << " </metadata>\n";
53 : }
54 87593 : myHavePendingOpener = false;
55 87593 : return true;
56 : }
57 : return false;
58 : }
59 :
60 :
61 : void
62 25633437 : PlainXMLFormatter::openTag(std::ostream& into, const std::string& xmlElement) {
63 25633437 : if (myHavePendingOpener) {
64 8875979 : into << ">\n";
65 : }
66 25633437 : myHavePendingOpener = true;
67 25633437 : into << std::string(4 * (myXMLStack.size() + myDefaultIndentation), ' ') << "<" << xmlElement;
68 25633437 : myXMLStack.push_back(xmlElement);
69 25633437 : }
70 :
71 :
72 : void
73 16118663 : PlainXMLFormatter::openTag(std::ostream& into, const SumoXMLTag& xmlElement) {
74 16118663 : openTag(into, toString(xmlElement));
75 16118663 : }
76 :
77 :
78 : bool
79 25938029 : PlainXMLFormatter::closeTag(std::ostream& into, const std::string& comment) {
80 25938029 : if (!myXMLStack.empty()) {
81 25633397 : if (myHavePendingOpener) {
82 16669848 : into << "/>" << comment << "\n";
83 16669848 : myHavePendingOpener = false;
84 : } else {
85 8963549 : const std::string indent(4 * (myXMLStack.size() + myDefaultIndentation - 1), ' ');
86 8963549 : into << indent << "</" << myXMLStack.back() << ">" << comment << "\n";
87 : }
88 : myXMLStack.pop_back();
89 25633397 : return true;
90 : }
91 : return false;
92 : }
93 :
94 :
95 : void
96 1503 : PlainXMLFormatter::writePreformattedTag(std::ostream& into, const std::string& val) {
97 1503 : if (myHavePendingOpener) {
98 9 : into << ">\n";
99 9 : myHavePendingOpener = false;
100 : }
101 : into << val;
102 1503 : }
103 :
104 :
105 : void
106 138565 : PlainXMLFormatter::writePadding(std::ostream& into, const std::string& val) {
107 : into << val;
108 138565 : }
109 :
110 :
111 : void
112 5065889 : PlainXMLFormatter::writeAttr(std::ostream& into, const std::string& attr, const std::string& val, const bool escape) {
113 10131778 : into << " " << attr << "=\"" << (escape ? StringUtils::escapeXML(val) : val) << "\"";
114 5065889 : }
115 :
116 :
117 : void
118 39338180 : PlainXMLFormatter::writeAttr(std::ostream& into, const SumoXMLAttr attr, const std::string& val, const bool escape) {
119 157352720 : into << " " << toString(attr) << "=\"" << (escape ? StringUtils::escapeXML(val) : val) << "\"";
120 39338180 : }
121 :
122 :
123 : /****************************************************************************/
|