SUMO - Simulation of Urban MObility

OptionsLoader.cpp

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