LCOV - code coverage report
Current view: top level - src/utils/iodevices - PlainXMLFormatter.cpp (source / functions) Coverage Total Hit
Test: lcov.info Lines: 100.0 % 42 42
Test Date: 2026-06-15 15:46:12 Functions: 100.0 % 7 7

            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      1743448 : PlainXMLFormatter::PlainXMLFormatter(const int defaultIndentation)
      33      1743448 :     : OutputFormatter(OutputFormatterType::XML), myDefaultIndentation(defaultIndentation), myHavePendingOpener(false) {
      34      1743448 : }
      35              : 
      36              : 
      37              : bool
      38        97562 : PlainXMLFormatter::writeXMLHeader(std::ostream& into, const std::string& rootElement,
      39              :                                   const std::map<SumoXMLAttr, std::string>& attrs,
      40              :                                   bool writeMetadata, bool includeConfig) {
      41        97562 :     if (myXMLStack.empty()) {
      42        87086 :         const OptionsCont& oc = OptionsCont::getOptions();
      43        87086 :         oc.writeXMLHeader(into, includeConfig);
      44        87086 :         openTag(into, rootElement);
      45       261857 :         for (std::map<SumoXMLAttr, std::string>::const_iterator it = attrs.begin(); it != attrs.end(); ++it) {
      46       174771 :             writeAttr(into, it->first, it->second);
      47              :         }
      48        87086 :         into << ">\n";
      49        87086 :         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        87086 :         myHavePendingOpener = false;
      55        87086 :         return true;
      56              :     }
      57              :     return false;
      58              : }
      59              : 
      60              : 
      61              : void
      62     24958891 : PlainXMLFormatter::openTag(std::ostream& into, const std::string& xmlElement) {
      63     24958891 :     if (myHavePendingOpener) {
      64      8556566 :         into << ">\n";
      65              :     }
      66     24958891 :     myHavePendingOpener = true;
      67     24958891 :     into << std::string(4 * (myXMLStack.size() + myDefaultIndentation), ' ') << "<" << xmlElement;
      68     24958891 :     myXMLStack.push_back(xmlElement);
      69     24958891 : }
      70              : 
      71              : 
      72              : void
      73     15777790 : PlainXMLFormatter::openTag(std::ostream& into, const SumoXMLTag& xmlElement) {
      74     15777790 :     openTag(into, toString(xmlElement));
      75     15777790 : }
      76              : 
      77              : 
      78              : bool
      79     25261045 : PlainXMLFormatter::closeTag(std::ostream& into, const std::string& comment) {
      80     25261045 :     if (!myXMLStack.empty()) {
      81     24958855 :         if (myHavePendingOpener) {
      82     16315222 :             into << "/>" << comment << "\n";
      83     16315222 :             myHavePendingOpener = false;
      84              :         } else {
      85      8643633 :             const std::string indent(4 * (myXMLStack.size() + myDefaultIndentation - 1), ' ');
      86      8643633 :             into << indent << "</" << myXMLStack.back() << ">" << comment << "\n";
      87              :         }
      88              :         myXMLStack.pop_back();
      89     24958855 :         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       135810 : PlainXMLFormatter::writePadding(std::ostream& into, const std::string& val) {
     107              :     into << val;
     108       135810 : }
     109              : 
     110              : 
     111              : /****************************************************************************/
        

Generated by: LCOV version 2.0-1