SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
OptionsLoader.cpp
Go to the documentation of this file.
1 /****************************************************************************/
9 // A SAX-Handler for loading options
10 /****************************************************************************/
11 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
12 // Copyright (C) 2001-2012 DLR (http://www.dlr.de/) and contributors
13 /****************************************************************************/
14 //
15 // This file is part of SUMO.
16 // SUMO is free software: you can redistribute it and/or modify
17 // it under the terms of the GNU General Public License as published by
18 // the Free Software Foundation, either version 3 of the License, or
19 // (at your option) any later version.
20 //
21 /****************************************************************************/
22 
23 
24 // ===========================================================================
25 // included modules
26 // ===========================================================================
27 #ifdef _MSC_VER
28 #include <windows_config.h>
29 #else
30 #include <config.h>
31 #endif
32 
33 #include <algorithm>
34 #include <string>
35 #include <vector>
36 #include <xercesc/sax/HandlerBase.hpp>
37 #include <xercesc/sax/AttributeList.hpp>
38 #include <xercesc/sax/SAXParseException.hpp>
39 #include <xercesc/sax/SAXException.hpp>
42 #include "OptionsLoader.h"
43 #include "OptionsCont.h"
47 #include <utils/common/ToString.h>
48 
49 
50 // ===========================================================================
51 // method definitions
52 // ===========================================================================
54  : myError(false), myOptions(OptionsCont::getOptions()), myItem() {}
55 
56 
58 
59 
60 void OptionsLoader::startElement(const XMLCh* const name,
61  XERCES_CPP_NAMESPACE::AttributeList& attributes) {
62  myItem = TplConvert::_2str(name);
63  for (int i = 0; i < (int) attributes.getLength(); i++) {
64  std::string key = TplConvert::_2str(attributes.getName(i));
65  std::string value = TplConvert::_2str(attributes.getValue(i));
66  if (key == "value" || key == "v") {
67  setValue(myItem, value);
68  }
69  // could give a hint here about unsupported attributes in configuration files
70  }
71  myValue = "";
72 }
73 
74 
75 void OptionsLoader::setValue(const std::string& key,
76  std::string& value) {
77  if (value.length() > 0) {
78  try {
79  if (!setSecure(key, value)) {
80  WRITE_ERROR("Could not set option '" + key + "' (probably defined twice).");
81  myError = true;
82  }
83  } catch (ProcessError& e) {
84  WRITE_ERROR(e.what());
85  myError = true;
86  }
87  }
88 }
89 
90 
91 void OptionsLoader::characters(const XMLCh* const chars,
92  const XERCES3_SIZE_t length) {
93  myValue = myValue + TplConvert::_2str(chars, (unsigned int) length);
94 }
95 
96 
97 bool
98 OptionsLoader::setSecure(const std::string& name,
99  const std::string& value) const {
100  if (myOptions.isWriteable(name)) {
101  myOptions.set(name, value);
102  return true;
103  }
104  return false;
105 }
106 
107 
108 void
109 OptionsLoader::endElement(const XMLCh* const /*name*/) {
110  if (myItem.length() == 0 || myValue.length() == 0) {
111  return;
112  }
113  if (myValue.find_first_not_of("\n\t \a") == std::string::npos) {
114  return;
115  }
117  myItem = "";
118  myValue = "";
119 }
120 
121 
122 void
123 OptionsLoader::warning(const XERCES_CPP_NAMESPACE::SAXParseException& exception) {
124  WRITE_WARNING(TplConvert::_2str(exception.getMessage()));
125  WRITE_WARNING(" (At line/column " \
126  + toString(exception.getLineNumber() + 1) + '/' \
127  + toString(exception.getColumnNumber()) + ").");
128  myError = true;
129 }
130 
131 
132 void
133 OptionsLoader::error(const XERCES_CPP_NAMESPACE::SAXParseException& exception) {
134  WRITE_ERROR(
135  TplConvert::_2str(exception.getMessage()));
136  WRITE_ERROR(
137  " (At line/column "
138  + toString(exception.getLineNumber() + 1) + '/'
139  + toString(exception.getColumnNumber()) + ").");
140  myError = true;
141 }
142 
143 
144 void
145 OptionsLoader::fatalError(const XERCES_CPP_NAMESPACE::SAXParseException& exception) {
146  WRITE_ERROR(
147  TplConvert::_2str(exception.getMessage()));
148  WRITE_ERROR(
149  " (At line/column "
150  + toString(exception.getLineNumber() + 1) + '/'
151  + toString(exception.getColumnNumber()) + ").");
152  myError = true;
153 }
154 
155 
156 bool
158  return myError;
159 }
160 
161 
162 
163 /****************************************************************************/
164