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: 2025-12-06 15:35:27 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-2025 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/common/StringTokenizer.h>
      33              : #include <utils/common/UtilExceptions.h>
      34              : #include <utils/common/FileHelpers.h>
      35              : #include <utils/common/MsgHandler.h>
      36              : #include <utils/common/ToString.h>
      37              : #include "OptionsIO.h"
      38              : #include "OptionsCont.h"
      39              : #include "OptionsLoader.h"
      40              : 
      41              : 
      42              : // ===========================================================================
      43              : // method definitions
      44              : // ===========================================================================
      45              : 
      46        11707 : OptionsLoader::OptionsLoader(OptionsCont& customOptions, const bool rootOnly) :
      47        11707 :     myRootOnly(rootOnly),
      48        11707 :     myOptions(customOptions),
      49        11707 :     myItem() {
      50        11707 : }
      51              : 
      52              : 
      53        11707 : OptionsLoader::~OptionsLoader() {}
      54              : 
      55              : 
      56       137179 : void OptionsLoader::startElement(const XMLCh* const name, XERCES_CPP_NAMESPACE::AttributeList& attributes) {
      57       137179 :     myFoundValue = false;
      58       137179 :     myItem = StringUtils::transcode(name);
      59       137179 :     if (!myRootOnly) {
      60       246636 :         for (int i = 0; i < (int)attributes.getLength(); i++) {
      61       109457 :             const std::string& key = StringUtils::transcode(attributes.getName(i));
      62       109457 :             const std::string& value = StringUtils::transcode(attributes.getValue(i));
      63       109457 :             if (key == "value" || key == "v") {
      64        87198 :                 setValue(myItem, value);
      65        87198 :                 myFoundValue = true;
      66              :             } else if (key != "xmlns:xsi"
      67        11136 :                     && key != "xsi:noNamespaceSchemaLocation"
      68           13 :                     && key != "synonymes"
      69           13 :                     && key != "deprecated"
      70           13 :                     && key != "type"
      71           13 :                     && key != "help"
      72              :                     // parsing a network file as single argument
      73        22272 :                     && (key != "version" && myItem != "net")) {
      74           36 :                 WRITE_WARNINGF(TL("Ignoring attribute '%' for option '%'"), key, myItem);
      75              :             }
      76              :         }
      77       137179 :         myValue = "";
      78              :     }
      79       137179 : }
      80              : 
      81              : 
      82        87198 : void OptionsLoader::setValue(const std::string& key, const std::string& value) {
      83        87198 :     if (value.length() > 0) {
      84              :         // try to add value in option container
      85              :         try {
      86        87192 :             if (!setSecure(myOptions, key, value)) {
      87            0 :                 WRITE_ERRORF(TL("Could not set option '%' (probably defined twice)."), key);
      88            0 :                 myError = true;
      89              :             }
      90            6 :         } catch (ProcessError& e) {
      91            6 :             WRITE_ERROR(e.what());
      92            6 :             myError = true;
      93            6 :         }
      94              :     }
      95        87198 : }
      96              : 
      97              : 
      98       174808 : void OptionsLoader::characters(const XMLCh* const chars, const XERCES3_SIZE_t length) {
      99       174808 :     myValue = myValue + StringUtils::transcode(chars, (int) length);
     100       174808 : }
     101              : 
     102              : 
     103              : bool
     104        87192 : OptionsLoader::setSecure(OptionsCont& options, const std::string& name, const std::string& value) const {
     105        87192 :     if (options.isWriteable(name)) {
     106        87186 :         options.set(name, value);
     107        87186 :         return true;
     108              :     }
     109              :     return false;
     110              : }
     111              : 
     112              : 
     113              : void
     114       137145 : OptionsLoader::endElement(const XMLCh* const /*name*/) {
     115       137145 :     if (myItem.length() == 0 || myValue.length() == 0) {
     116        87410 :         if (!myFoundValue) {
     117           36 :             WRITE_ERRORF(TL("Could not set option '%' because attribute 'value' is missing."), myItem);
     118              :         }
     119        87410 :         return;
     120              :     }
     121        49735 :     if (myValue.find_first_not_of("\n\t \a") == std::string::npos) {
     122              :         return;
     123              :     }
     124            0 :     setValue(myItem, myValue);
     125              :     myItem = "";
     126              :     myValue = "";
     127              : }
     128              : 
     129              : 
     130              : void
     131            0 : OptionsLoader::warning(const XERCES_CPP_NAMESPACE::SAXParseException& exception) {
     132            0 :     WRITE_WARNING(StringUtils::transcode(exception.getMessage()));
     133            0 :     WRITE_WARNING(" (At line/column " \
     134              :                   + toString(exception.getLineNumber() + 1) + '/' \
     135              :                   + toString(exception.getColumnNumber()) + ").");
     136            0 :     myError = true;
     137            0 : }
     138              : 
     139              : 
     140              : void
     141            0 : OptionsLoader::error(const XERCES_CPP_NAMESPACE::SAXParseException& exception) {
     142            0 :     WRITE_ERROR(StringUtils::transcode(exception.getMessage()));
     143            0 :     WRITE_ERROR(" (At line/column "
     144              :                 + toString(exception.getLineNumber() + 1) + '/'
     145              :                 + toString(exception.getColumnNumber()) + ").");
     146            0 :     myError = true;
     147            0 : }
     148              : 
     149              : 
     150              : void
     151            1 : OptionsLoader::fatalError(const XERCES_CPP_NAMESPACE::SAXParseException& exception) {
     152            1 :     WRITE_ERROR(StringUtils::transcode(exception.getMessage()));
     153            4 :     WRITE_ERROR(" (At line/column "
     154              :                 + toString(exception.getLineNumber() + 1) + '/'
     155              :                 + toString(exception.getColumnNumber()) + ").");
     156            1 :     myError = true;
     157            1 : }
     158              : 
     159              : 
     160              : bool
     161        11707 : OptionsLoader::errorOccurred() const {
     162        11707 :     return myError;
     163              : }
     164              : 
     165              : /****************************************************************************/
        

Generated by: LCOV version 2.0-1