LCOV - code coverage report
Current view: top level - src/utils/iodevices - PlainXMLFormatter.h (source / functions) Coverage Total Hit
Test: lcov.info Lines: 100.0 % 12 12
Test Date: 2026-07-25 16:16:11 Functions: 92.7 % 82 76

            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      1833184 :     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              :      */
      69              :     void openTag(std::ostream& into, const std::string& xmlElement);
      70              : 
      71              :     /** @brief Opens an XML tag
      72              :      *
      73              :      * Helper method which finds the correct string before calling openTag.
      74              :      *
      75              :      * @param[in] into The output stream to use
      76              :      * @param[in] xmlElement Id of the element to open
      77              :      */
      78              :     void openTag(std::ostream& into, const SumoXMLTag& xmlElement);
      79              : 
      80              :     /** @brief Closes the most recently opened tag
      81              :      *
      82              :      * @param[in] into The output stream to use
      83              :      * @return Whether a further element existed in the stack and could be closed
      84              :      */
      85              :     bool closeTag(std::ostream& into, const std::string& comment = "");
      86              : 
      87              :     /** @brief writes a preformatted tag to the device but ensures that any
      88              :      * pending tags are closed
      89              :      * @param[in] into The output stream to use
      90              :      * @param[in] val The preformatted data
      91              :      */
      92              :     void writePreformattedTag(std::ostream& into, const std::string& val);
      93              : 
      94              :     /** @brief writes arbitrary padding
      95              :      */
      96              :     void writePadding(std::ostream& into, const std::string& val);
      97              : 
      98              :     /** @brief writes an arbitrary attribute
      99              :      *
     100              :      * @param[in] into The output stream to use
     101              :      * @param[in] attr The attribute (name)
     102              :      * @param[in] val The attribute value
     103              :      * @param[in] escape Whether the value should be processed by StringUtils::escapeXML (only used in the string variant below)
     104              :      */
     105              :     template <class T>
     106     23497973 :     static void writeAttr(std::ostream& into, const std::string& attr, const T& val, const bool escape) {
     107              :         UNUSED_PARAMETER(escape);
     108     46994899 :         into << " " << attr << "=\"" << toString(val, into.precision()) << "\"";
     109     23497973 :     }
     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              :      * @param[in] escape Whether the value should be processed by StringUtils::escapeXML (only used in the string variant below)
     117              :      */
     118              :     template <class T>
     119     60755041 :     static void writeAttr(std::ostream& into, const SumoXMLAttr attr, const T& val, const bool escape) {
     120              :         UNUSED_PARAMETER(escape);
     121    242861621 :         into << " " << toString(attr) << "=\"" << toString(val, into.precision()) << "\"";
     122     60755041 :     }
     123              : 
     124              :     /// @brief typed overloads (non-template) -- picked by overload resolution over the template
     125              :     static void writeAttr(std::ostream& into, const std::string& attr, const std::string& val, const bool escape);
     126              :     static void writeAttr(std::ostream& into, const SumoXMLAttr attr, const std::string& val, const bool escape);
     127              : 
     128      8231166 :     void writeTime(std::ostream& into, const SumoXMLAttr attr, const SUMOTime val) {
     129     32924664 :         into << " " << toString(attr) << "=\"" << time2string(val) << "\"";
     130      8231166 :     }
     131              : 
     132       271990 :     bool wroteHeader() const {
     133       271990 :         return !myXMLStack.empty();
     134              :     }
     135              : 
     136              : private:
     137              :     /// @brief The stack of begun xml elements
     138              :     std::vector<std::string> myXMLStack;
     139              : 
     140              :     /// @brief The initial indentation level
     141              :     int myDefaultIndentation;
     142              : 
     143              :     /// @brief whether a closing ">" might be missing
     144              :     bool myHavePendingOpener;
     145              : };
        

Generated by: LCOV version 2.0-1