SUMO - Simulation of Urban MObility
OptionsLoader.cpp
Go to the documentation of this file.
00001 /****************************************************************************/
00009 // A SAX-Handler for loading options
00010 /****************************************************************************/
00011 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
00012 // Copyright (C) 2001-2012 DLR (http://www.dlr.de/) and contributors
00013 /****************************************************************************/
00014 //
00015 //   This file is part of SUMO.
00016 //   SUMO is free software: you can redistribute it and/or modify
00017 //   it under the terms of the GNU General Public License as published by
00018 //   the Free Software Foundation, either version 3 of the License, or
00019 //   (at your option) any later version.
00020 //
00021 /****************************************************************************/
00022 
00023 
00024 // ===========================================================================
00025 // included modules
00026 // ===========================================================================
00027 #ifdef _MSC_VER
00028 #include <windows_config.h>
00029 #else
00030 #include <config.h>
00031 #endif
00032 
00033 #include <algorithm>
00034 #include <string>
00035 #include <vector>
00036 #include <xercesc/sax/HandlerBase.hpp>
00037 #include <xercesc/sax/AttributeList.hpp>
00038 #include <xercesc/sax/SAXParseException.hpp>
00039 #include <xercesc/sax/SAXException.hpp>
00040 #include <utils/common/TplConvert.h>
00041 #include <utils/common/StringTokenizer.h>
00042 #include "OptionsLoader.h"
00043 #include "OptionsCont.h"
00044 #include <utils/common/UtilExceptions.h>
00045 #include <utils/common/FileHelpers.h>
00046 #include <utils/common/MsgHandler.h>
00047 #include <utils/common/ToString.h>
00048 
00049 
00050 // ===========================================================================
00051 // method definitions
00052 // ===========================================================================
00053 OptionsLoader::OptionsLoader()
00054     : myError(false), myOptions(OptionsCont::getOptions()), myItem() {}
00055 
00056 
00057 OptionsLoader::~OptionsLoader() {}
00058 
00059 
00060 void OptionsLoader::startElement(const XMLCh* const name,
00061                                  AttributeList& attributes) {
00062     myItem = TplConvert<XMLCh>::_2str(name);
00063     for (int i = 0; i < (int) attributes.getLength(); i++) {
00064         std::string key = TplConvert<XMLCh>::_2str(attributes.getName(i));
00065         std::string value = TplConvert<XMLCh>::_2str(attributes.getValue(i));
00066         if (key == "value" || key == "v") {
00067             setValue(myItem, value);
00068         }
00069         // could give a hint here about unsupported attributes in configuration files
00070     }
00071     myValue = "";
00072 }
00073 
00074 
00075 void OptionsLoader::setValue(const std::string& key,
00076                              std::string& value) {
00077     if (value.length() > 0) {
00078         try {
00079             if (!setSecure(key, value)) {
00080                 WRITE_ERROR("Could not set option '" + key + "' (probably defined twice).");
00081                 myError = true;
00082             }
00083         } catch (ProcessError& e) {
00084             WRITE_ERROR(e.what());
00085             myError = true;
00086         }
00087     }
00088 }
00089 
00090 
00091 void OptionsLoader::characters(const XMLCh* const chars,
00092                                const XERCES3_SIZE_t length) {
00093     myValue = myValue + TplConvert<XMLCh>::_2str(chars, (unsigned int) length);
00094 }
00095 
00096 
00097 bool
00098 OptionsLoader::setSecure(const std::string& name,
00099                          const std::string& value) const {
00100     if (myOptions.isWriteable(name)) {
00101         myOptions.set(name, value);
00102         return true;
00103     }
00104     return false;
00105 }
00106 
00107 
00108 void
00109 OptionsLoader::endElement(const XMLCh* const /*name*/) {
00110     if (myItem.length() == 0 || myValue.length() == 0) {
00111         return;
00112     }
00113     if (myValue.find_first_not_of("\n\t \a") == std::string::npos) {
00114         return;
00115     }
00116     setValue(myItem, myValue);
00117     myItem = "";
00118     myValue = "";
00119 }
00120 
00121 
00122 void
00123 OptionsLoader::warning(const SAXParseException& exception) {
00124     WRITE_WARNING(TplConvert<XMLCh>::_2str(exception.getMessage()));
00125     WRITE_WARNING(" (At line/column " \
00126                   + toString(exception.getLineNumber() + 1) + '/' \
00127                   + toString(exception.getColumnNumber()) + ").");
00128     myError = true;
00129 }
00130 
00131 
00132 void
00133 OptionsLoader::error(const SAXParseException& exception) {
00134     WRITE_ERROR(
00135         TplConvert<XMLCh>::_2str(exception.getMessage()));
00136     WRITE_ERROR(
00137         " (At line/column "
00138         + toString(exception.getLineNumber() + 1) + '/'
00139         + toString(exception.getColumnNumber()) + ").");
00140     myError = true;
00141 }
00142 
00143 
00144 void
00145 OptionsLoader::fatalError(const SAXParseException& exception) {
00146     WRITE_ERROR(
00147         TplConvert<XMLCh>::_2str(exception.getMessage()));
00148     WRITE_ERROR(
00149         " (At line/column "
00150         + toString(exception.getLineNumber() + 1) + '/'
00151         + toString(exception.getColumnNumber()) + ").");
00152     myError = true;
00153 }
00154 
00155 
00156 bool
00157 OptionsLoader::errorOccured() const {
00158     return myError;
00159 }
00160 
00161 
00162 
00163 /****************************************************************************/
00164 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Defines