Line data Source code
1 : /****************************************************************************/
2 : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3 : // Copyright (C) 2012-2025 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/options/OptionsCont.h>
25 : #include "PlainXMLFormatter.h"
26 :
27 :
28 : // ===========================================================================
29 : // member method definitions
30 : // ===========================================================================
31 1567270 : PlainXMLFormatter::PlainXMLFormatter(const int defaultIndentation)
32 1567270 : : OutputFormatter(OutputFormatterType::XML), myDefaultIndentation(defaultIndentation), myHavePendingOpener(false) {
33 1567270 : }
34 :
35 :
36 : bool
37 87917 : PlainXMLFormatter::writeXMLHeader(std::ostream& into, const std::string& rootElement,
38 : const std::map<SumoXMLAttr, std::string>& attrs,
39 : bool writeMetadata, bool includeConfig) {
40 87917 : if (myXMLStack.empty()) {
41 78768 : const OptionsCont& oc = OptionsCont::getOptions();
42 78768 : oc.writeXMLHeader(into, includeConfig);
43 78768 : openTag(into, rootElement);
44 236105 : for (std::map<SumoXMLAttr, std::string>::const_iterator it = attrs.begin(); it != attrs.end(); ++it) {
45 157337 : writeAttr(into, it->first, it->second);
46 : }
47 78768 : into << ">\n";
48 78768 : if (writeMetadata) {
49 3 : into << " <metadata created_at=\"" << StringUtils::isoTimeString() << "\" created_by=\"" << oc.getFullName() << "\">\n";
50 2 : oc.writeConfiguration(into, true, false, false, "", false, true, " ");
51 1 : into << " </metadata>\n";
52 : }
53 78768 : myHavePendingOpener = false;
54 78768 : return true;
55 : }
56 : return false;
57 : }
58 :
59 :
60 : void
61 16671579 : PlainXMLFormatter::openTag(std::ostream& into, const std::string& xmlElement) {
62 16671579 : if (myHavePendingOpener) {
63 4399424 : into << ">\n";
64 : }
65 16671579 : myHavePendingOpener = true;
66 16671579 : into << std::string(4 * (myXMLStack.size() + myDefaultIndentation), ' ') << "<" << xmlElement;
67 16671579 : myXMLStack.push_back(xmlElement);
68 16671579 : }
69 :
70 :
71 : void
72 7504094 : PlainXMLFormatter::openTag(std::ostream& into, const SumoXMLTag& xmlElement) {
73 7504094 : openTag(into, toString(xmlElement));
74 7504094 : }
75 :
76 :
77 : bool
78 16945385 : PlainXMLFormatter::closeTag(std::ostream& into, const std::string& comment) {
79 16945385 : if (!myXMLStack.empty()) {
80 16671552 : if (myHavePendingOpener) {
81 12193370 : into << "/>" << comment << "\n";
82 12193370 : myHavePendingOpener = false;
83 : } else {
84 4478182 : const std::string indent(4 * (myXMLStack.size() + myDefaultIndentation - 1), ' ');
85 4478182 : into << indent << "</" << myXMLStack.back() << ">" << comment << "\n";
86 : }
87 : myXMLStack.pop_back();
88 16671552 : return true;
89 : }
90 : return false;
91 : }
92 :
93 :
94 : void
95 1497 : PlainXMLFormatter::writePreformattedTag(std::ostream& into, const std::string& val) {
96 1497 : if (myHavePendingOpener) {
97 9 : into << ">\n";
98 9 : myHavePendingOpener = false;
99 : }
100 : into << val;
101 1497 : }
102 :
103 :
104 : void
105 129148 : PlainXMLFormatter::writePadding(std::ostream& into, const std::string& val) {
106 : into << val;
107 129148 : }
108 :
109 :
110 : /****************************************************************************/
|