1 /****************************************************************************/ 2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo 3 // Copyright (C) 2001-2019 German Aerospace Center (DLR) and others. 4 // This program and the accompanying materials 5 // are made available under the terms of the Eclipse Public License v2.0 6 // which accompanies this distribution, and is available at 7 // http://www.eclipse.org/legal/epl-v20.html 8 // SPDX-License-Identifier: EPL-2.0 9 /****************************************************************************/ 10 /// @file OptionsLoader.h 11 /// @author Daniel Krajzewicz 12 /// @author Michael Behrisch 13 /// @date Mon, 17 Dec 2001 14 /// @version $Id$ 15 /// 16 // A SAX-Handler for loading options 17 /****************************************************************************/ 18 #ifndef OptionsLoader_h 19 #define OptionsLoader_h 20 21 22 // =========================================================================== 23 // included modules 24 // =========================================================================== 25 #include <config.h> 26 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 <string> 32 33 34 // =========================================================================== 35 // class declarations 36 // =========================================================================== 37 class OptionsCont; 38 39 40 // =========================================================================== 41 // class definitions 42 // =========================================================================== 43 /** 44 * @class OptionsLoader 45 * @brief A SAX-Handler for loading options 46 */ 47 class OptionsLoader : public XERCES_CPP_NAMESPACE::HandlerBase { 48 public: 49 /** @brief Constructor 50 */ 51 OptionsLoader(const bool routeOnly = false); 52 53 54 /** destructor */ 55 ~OptionsLoader(); 56 57 58 59 60 /// @name Handlers for the SAX DocumentHandler interface 61 /// @{ 62 63 /** @brief Called on the occurence of the beginning of a tag 64 * 65 * Sets the name of the last item 66 */ 67 virtual void startElement(const XMLCh* const name, 68 XERCES_CPP_NAMESPACE::AttributeList& attributes); 69 70 71 /** @brief Called on the occurence of character data 72 * 73 * If this occurs inside a single tag it sets the option named 74 * by the tag to the value given by the character data. 75 * This is considered deprecated in favor of attributes. 76 * @todo Describe better 77 */ 78 void characters(const XMLCh* const chars, const XERCES3_SIZE_t length); 79 80 81 /** @brief Called on the end of an element 82 * 83 * Resets the element name 84 */ 85 void endElement(const XMLCh* const name); 86 /// @} 87 88 89 90 91 /// @name Handlers for the SAX ErrorHandler interface 92 /// @{ 93 94 /** @brief Called on an XML-warning 95 * 96 * The warning is reported to the the warning-instance of MsgHandler 97 */ 98 void warning(const XERCES_CPP_NAMESPACE::SAXParseException& exception); 99 100 101 /** @brief Called on an XML-error 102 * 103 * The warning is reported to the the error-instance of MsgHandler 104 */ 105 void error(const XERCES_CPP_NAMESPACE::SAXParseException& exception); 106 107 108 /** @brief Called on an XML-fatal error 109 * 110 * The warning is reported to the the error-instance of MsgHandler 111 */ 112 void fatalError(const XERCES_CPP_NAMESPACE::SAXParseException& exception); 113 /// @} 114 115 116 117 /** @brief Returns the information whether an error occurred */ 118 bool errorOccurred() const; 119 120 /** @brief Returns the last item read */ getItem()121 const std::string& getItem() const { 122 return myItem; 123 } 124 125 126 private: 127 /** @brief Tries to set the named option to the given value 128 * 129 * Also evaluates whether it is a boolean or a filename option and 130 * does the relevant checks / modifications. 131 * 132 * @param[in] key The name of the option to set 133 * @param[in] value The new value for the option 134 */ 135 void setValue(const std::string& key, std::string& value); 136 137 138 /** @brief Tries to set the named option to the given value 139 * 140 * Checks the item whether it was default before setting it. 141 * Returns the information whether the item was set before (was not a default value) 142 * 143 * @param[in] name The name of the option to set 144 * @param[in] value The new value for the option 145 * @return Whether the option could be set 146 */ 147 bool setSecure(const std::string& name, const std::string& value) const; 148 149 150 private: 151 /** invalid copy constructor */ 152 OptionsLoader(const OptionsLoader& s); 153 154 155 /** invalid assignment operator */ 156 OptionsLoader& operator=(const OptionsLoader& s); 157 158 159 private: 160 /// @brief The information whether only the root element should be parsed 161 bool myRootOnly; 162 163 /// @brief The information whether an error occurred 164 bool myError; 165 166 /// @brief The options to fill 167 OptionsCont& myOptions; 168 169 /// @brief The name of the currently parsed option 170 std::string myItem; 171 172 /// @brief The currently read characters string 173 std::string myValue; 174 175 }; 176 177 178 #endif 179 180 /****************************************************************************/ 181 182