Path: blob/main/src/utils/iodevices/PlainXMLFormatter.cpp
169678 views
/****************************************************************************/1// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo2// Copyright (C) 2012-2025 German Aerospace Center (DLR) and others.3// This program and the accompanying materials are made available under the4// terms of the Eclipse Public License 2.0 which is available at5// https://www.eclipse.org/legal/epl-2.0/6// This Source Code may also be made available under the following Secondary7// Licenses when the conditions for such availability set forth in the Eclipse8// Public License 2.0 are satisfied: GNU General Public License, version 29// or later which is available at10// https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html11// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later12/****************************************************************************/13/// @file PlainXMLFormatter.cpp14/// @author Daniel Krajzewicz15/// @author Michael Behrisch16/// @date 201217///18// Static storage of an output device and its base (abstract) implementation19/****************************************************************************/20#include <config.h>2122#include <utils/common/ToString.h>23#include <utils/options/OptionsCont.h>24#include "PlainXMLFormatter.h"252627// ===========================================================================28// member method definitions29// ===========================================================================30PlainXMLFormatter::PlainXMLFormatter(const int defaultIndentation)31: OutputFormatter(OutputFormatterType::XML), myDefaultIndentation(defaultIndentation), myHavePendingOpener(false) {32}333435bool36PlainXMLFormatter::writeXMLHeader(std::ostream& into, const std::string& rootElement,37const std::map<SumoXMLAttr, std::string>& attrs,38bool writeMetadata, bool includeConfig) {39if (myXMLStack.empty()) {40const OptionsCont& oc = OptionsCont::getOptions();41oc.writeXMLHeader(into, includeConfig);42openTag(into, rootElement);43for (std::map<SumoXMLAttr, std::string>::const_iterator it = attrs.begin(); it != attrs.end(); ++it) {44writeAttr(into, it->first, it->second);45}46into << ">\n";47if (writeMetadata) {48into << " <metadata created_at=\"" << StringUtils::isoTimeString() << "\" created_by=\"" << oc.getFullName() << "\">\n";49oc.writeConfiguration(into, true, false, false, "", false, true, " ");50into << " </metadata>\n";51}52myHavePendingOpener = false;53return true;54}55return false;56}575859void60PlainXMLFormatter::openTag(std::ostream& into, const std::string& xmlElement) {61if (myHavePendingOpener) {62into << ">\n";63}64myHavePendingOpener = true;65into << std::string(4 * (myXMLStack.size() + myDefaultIndentation), ' ') << "<" << xmlElement;66myXMLStack.push_back(xmlElement);67}686970void71PlainXMLFormatter::openTag(std::ostream& into, const SumoXMLTag& xmlElement) {72openTag(into, toString(xmlElement));73}747576bool77PlainXMLFormatter::closeTag(std::ostream& into, const std::string& comment) {78if (!myXMLStack.empty()) {79if (myHavePendingOpener) {80into << "/>" << comment << "\n";81myHavePendingOpener = false;82} else {83const std::string indent(4 * (myXMLStack.size() + myDefaultIndentation - 1), ' ');84into << indent << "</" << myXMLStack.back() << ">" << comment << "\n";85}86myXMLStack.pop_back();87return true;88}89return false;90}919293void94PlainXMLFormatter::writePreformattedTag(std::ostream& into, const std::string& val) {95if (myHavePendingOpener) {96into << ">\n";97myHavePendingOpener = false;98}99into << val;100}101102103void104PlainXMLFormatter::writePadding(std::ostream& into, const std::string& val) {105into << val;106}107108109/****************************************************************************/110111112