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.h
15 : /// @author Daniel Krajzewicz
16 : /// @author Michael Behrisch
17 : /// @date 2012
18 : ///
19 : // Output formatter for plain XML output
20 : /****************************************************************************/
21 : #pragma once
22 : #include <config.h>
23 :
24 : #include "OutputFormatter.h"
25 :
26 :
27 : // ===========================================================================
28 : // class definitions
29 : // ===========================================================================
30 : /**
31 : * @class PlainXMLFormatter
32 : * @brief Output formatter for plain XML output
33 : *
34 : * PlainXMLFormatter format XML like output into the output stream.
35 : */
36 : class PlainXMLFormatter : public OutputFormatter {
37 : public:
38 : /// @brief Constructor
39 : PlainXMLFormatter(const int defaultIndentation = 0);
40 :
41 : /// @brief Destructor
42 1748788 : virtual ~PlainXMLFormatter() { }
43 :
44 : /** @brief Writes an XML header with optional configuration
45 : *
46 : * If something has been written (myXMLStack is not empty), nothing
47 : * is written and false returned.
48 : *
49 : * @param[in] into The output stream to use
50 : * @param[in] rootElement The root element to use
51 : * @param[in] attrs Additional attributes to save within the rootElement
52 : * @param[in] includeConfig whether the current config should be included as XML comment
53 : * @return whether something has been written
54 : * @todo Describe what is saved
55 : */
56 : bool writeXMLHeader(std::ostream& into, const std::string& rootElement,
57 : const std::map<SumoXMLAttr, std::string>& attrs, bool writeMetadata,
58 : bool includeConfig);
59 :
60 : /** @brief Opens an XML tag
61 : *
62 : * An indentation, depending on the current xml-element-stack size, is written followed
63 : * by the given xml element ("<" + xmlElement)
64 : * The xml element is added to the stack, then.
65 : *
66 : * @param[in] into The output stream to use
67 : * @param[in] xmlElement Name of element to open
68 : * @return The OutputDevice for further processing
69 : */
70 : void openTag(std::ostream& into, const std::string& xmlElement);
71 :
72 : /** @brief Opens an XML tag
73 : *
74 : * Helper method which finds the correct string before calling openTag.
75 : *
76 : * @param[in] into The output stream to use
77 : * @param[in] xmlElement Id of the element to open
78 : */
79 : void openTag(std::ostream& into, const SumoXMLTag& xmlElement);
80 :
81 : /** @brief Closes the most recently opened tag
82 : *
83 : * @param[in] into The output stream to use
84 : * @return Whether a further element existed in the stack and could be closed
85 : * @todo it is not verified that the topmost element was closed
86 : */
87 : bool closeTag(std::ostream& into, const std::string& comment = "");
88 :
89 : /** @brief writes a preformatted tag to the device but ensures that any
90 : * pending tags are closed
91 : * @param[in] into The output stream to use
92 : * @param[in] val The preformatted data
93 : */
94 : void writePreformattedTag(std::ostream& into, const std::string& val);
95 :
96 : /** @brief writes arbitrary padding
97 : */
98 : void writePadding(std::ostream& into, const std::string& val);
99 :
100 : /** @brief writes an arbitrary attribute
101 : *
102 : * @param[in] into The output stream to use
103 : * @param[in] attr The attribute (name)
104 : * @param[in] val The attribute value
105 : */
106 : template <class T>
107 28406013 : static void writeAttr(std::ostream& into, const std::string& attr, const T& val) {
108 51808182 : into << " " << attr << "=\"" << toString(val, into.precision()) << "\"";
109 28406013 : }
110 :
111 : /** @brief writes a named attribute
112 : *
113 : * @param[in] into The output stream to use
114 : * @param[in] attr The attribute (name)
115 : * @param[in] val The attribute value
116 : */
117 : template <class T>
118 88234754 : static void writeAttr(std::ostream& into, const SumoXMLAttr attr, const T& val) {
119 319092832 : into << " " << toString(attr) << "=\"" << toString(val, into.precision()) << "\"";
120 88234754 : }
121 :
122 5140025 : void writeTime(std::ostream& into, const SumoXMLAttr attr, const SUMOTime val) {
123 20560100 : into << " " << toString(attr) << "=\"" << time2string(val) << "\"";
124 5140025 : }
125 :
126 262180 : bool wroteHeader() const {
127 262180 : return !myXMLStack.empty();
128 : }
129 :
130 : private:
131 : /// @brief The stack of begun xml elements
132 : std::vector<std::string> myXMLStack;
133 :
134 : /// @brief The initial indentation level
135 : int myDefaultIndentation;
136 :
137 : /// @brief whether a closing ">" might be missing
138 : bool myHavePendingOpener;
139 : };
|