LCOV - code coverage report
Current view: top level - src/utils/options - OptionsLoader.cpp (source / functions) Coverage Total Hit
Test: lcov.info Lines: 80.3 % 66 53
Test Date: 2026-06-15 15:46:12 Functions: 75.0 % 12 9

            Line data    Source code
       1              : /****************************************************************************/
       2              : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
       3              : // Copyright (C) 2001-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    OptionsLoader.cpp
      15              : /// @author  Daniel Krajzewicz
      16              : /// @author  Jakob Erdmann
      17              : /// @author  Michael Behrisch
      18              : /// @date    Mon, 17 Dec 2001
      19              : ///
      20              : // A SAX-Handler for loading options
      21              : /****************************************************************************/
      22              : #include <config.h>
      23              : 
      24              : #include <algorithm>
      25              : #include <string>
      26              : #include <vector>
      27              : #include <xercesc/sax/HandlerBase.hpp>
      28              : #include <xercesc/sax/AttributeList.hpp>
      29              : #include <xercesc/sax/SAXParseException.hpp>
      30              : #include <xercesc/sax/SAXException.hpp>
      31              : #include <utils/common/StringUtils.h>
      32              : #include <utils/xml/XMLSubSys.h>
      33              : #include <utils/common/StringTokenizer.h>
      34              : #include <utils/common/UtilExceptions.h>
      35              : #include <utils/common/FileHelpers.h>
      36              : #include <utils/common/MsgHandler.h>
      37              : #include <utils/common/ToString.h>
      38              : #include "OptionsIO.h"
      39              : #include "OptionsCont.h"
      40              : #include "OptionsLoader.h"
      41              : 
      42              : 
      43              : // ===========================================================================
      44              : // method definitions
      45              : // ===========================================================================
      46              : 
      47        13724 : OptionsLoader::OptionsLoader(OptionsCont& customOptions, const bool rootOnly) :
      48        13724 :     myRootOnly(rootOnly),
      49        13724 :     myOptions(customOptions),
      50        13724 :     myItem() {
      51        13724 : }
      52              : 
      53              : 
      54        13724 : OptionsLoader::~OptionsLoader() {}
      55              : 
      56              : 
      57       104771 : void OptionsLoader::startElement(const XMLCh* const name, XERCES_CPP_NAMESPACE::AttributeList& attributes) {
      58       104771 :     myFoundValue = false;
      59       104771 :     myItem = XMLSubSys::transcode(name);
      60       104771 :     if (!myRootOnly) {
      61       196483 :         for (int i = 0; i < (int)attributes.getLength(); i++) {
      62        91712 :             const std::string& key = XMLSubSys::transcode(attributes.getName(i));
      63        91712 :             const std::string& value = XMLSubSys::transcode(attributes.getValue(i));
      64        91712 :             if (key == "value" || key == "v") {
      65        65213 :                 setValue(myItem, value);
      66        65213 :                 myFoundValue = true;
      67              :             } else if (key != "xmlns:xsi"
      68        13256 :                        && key != "xsi:noNamespaceSchemaLocation"
      69           13 :                        && key != "synonymes"
      70           13 :                        && key != "deprecated"
      71           13 :                        && key != "type"
      72           13 :                        && key != "help"
      73              :                        // parsing a network file as single argument
      74        26512 :                        && (key != "version" && myItem != "net")) {
      75           36 :                 WRITE_WARNINGF(TL("Ignoring attribute '%' for option '%'"), key, myItem);
      76              :             }
      77              :         }
      78       104771 :         myValue = "";
      79              :     }
      80       104771 : }
      81              : 
      82              : 
      83        65213 : void OptionsLoader::setValue(const std::string& key, const std::string& value) {
      84        65213 :     if (value.length() > 0) {
      85              :         // try to add value in option container
      86              :         try {
      87        65209 :             if (!setSecure(myOptions, key, value)) {
      88            0 :                 WRITE_ERRORF(TL("Could not set option '%' (probably defined twice)."), key);
      89            0 :                 myError = true;
      90              :             }
      91            6 :         } catch (ProcessError& e) {
      92            6 :             WRITE_ERROR(e.what());
      93            6 :             myError = true;
      94            6 :         }
      95              :     }
      96        65213 : }
      97              : 
      98              : 
      99       130004 : void OptionsLoader::characters(const XMLCh* const chars, const XERCES3_SIZE_t length) {
     100       130004 :     myValue = myValue + XMLSubSys::transcode(chars, (int) length);
     101       130004 : }
     102              : 
     103              : 
     104              : bool
     105        65209 : OptionsLoader::setSecure(OptionsCont& options, const std::string& name, const std::string& value) const {
     106        65209 :     if (options.isWriteable(name)) {
     107        65203 :         options.set(name, value);
     108        65203 :         return true;
     109              :     }
     110              :     return false;
     111              : }
     112              : 
     113              : 
     114              : void
     115       104754 : OptionsLoader::endElement(const XMLCh* const /*name*/) {
     116       104754 :     if (myItem.length() == 0 || myValue.length() == 0) {
     117        65425 :         if (!myFoundValue) {
     118           36 :             WRITE_ERRORF(TL("Could not set option '%' because attribute 'value' is missing."), myItem);
     119              :         }
     120        65425 :         return;
     121              :     }
     122        39329 :     if (myValue.find_first_not_of("\n\t \a") == std::string::npos) {
     123              :         return;
     124              :     }
     125            0 :     setValue(myItem, myValue);
     126              :     myItem = "";
     127              :     myValue = "";
     128              : }
     129              : 
     130              : 
     131              : void
     132            0 : OptionsLoader::warning(const XERCES_CPP_NAMESPACE::SAXParseException& exception) {
     133            0 :     WRITE_WARNING(XMLSubSys::transcode(exception.getMessage()));
     134            0 :     WRITE_WARNING(" (At line/column " \
     135              :                   + toString(exception.getLineNumber() + 1) + '/' \
     136              :                   + toString(exception.getColumnNumber()) + ").");
     137            0 :     myError = true;
     138            0 : }
     139              : 
     140              : 
     141              : void
     142            0 : OptionsLoader::error(const XERCES_CPP_NAMESPACE::SAXParseException& exception) {
     143            0 :     WRITE_ERROR(XMLSubSys::transcode(exception.getMessage()));
     144            0 :     WRITE_ERROR(" (At line/column "
     145              :                 + toString(exception.getLineNumber() + 1) + '/'
     146              :                 + toString(exception.getColumnNumber()) + ").");
     147            0 :     myError = true;
     148            0 : }
     149              : 
     150              : 
     151              : void
     152            1 : OptionsLoader::fatalError(const XERCES_CPP_NAMESPACE::SAXParseException& exception) {
     153            1 :     WRITE_ERROR(XMLSubSys::transcode(exception.getMessage()));
     154            4 :     WRITE_ERROR(" (At line/column "
     155              :                 + toString(exception.getLineNumber() + 1) + '/'
     156              :                 + toString(exception.getColumnNumber()) + ").");
     157            1 :     myError = true;
     158            1 : }
     159              : 
     160              : 
     161              : bool
     162        13724 : OptionsLoader::errorOccurred() const {
     163        13724 :     return myError;
     164              : }
     165              : 
     166              : /****************************************************************************/
        

Generated by: LCOV version 2.0-1