OptionsLoader.cpp
Go to the documentation of this file.00001
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
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
00049
00050 OptionsLoader::OptionsLoader() throw()
00051 : myError(false), myOptions(OptionsCont::getOptions()), myItem() {}
00052
00053
00054 OptionsLoader::~OptionsLoader() throw() {}
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 name = TplConvert<XMLCh>::_2str(attributes.getName(i));
00062 std::string value = TplConvert<XMLCh>::_2str(attributes.getValue(i));
00063 if (myOptions.exists(myItem) && (name == "value" || name == "v")) {
00064 setValue(myItem, value);
00065 } else {
00066 setValue(name, value);
00067 }
00068 }
00069 myValue = "";
00070 }
00071
00072
00073 void OptionsLoader::setValue(const std::string &key,
00074 std::string &value) {
00075 if (value.length()>0) {
00076 try {
00077 bool isWriteable;
00078 if (myOptions.isBool(key)) {
00079 std::transform(value.begin(), value.end(), value.begin(), tolower);
00080 if (value=="1"||value=="yes"||value=="true"||value=="on"||value=="x") {
00081 isWriteable = setSecure(key, true);
00082 } else if (value=="0"||value=="no"||value=="false"||value=="off") {
00083 isWriteable = setSecure(key, false);
00084 } else {
00085 throw InvalidArgument("Invalid boolean value for option '" + key + "'.");
00086 }
00087 } else {
00088 isWriteable = setSecure(key, value);
00089 }
00090 if (!isWriteable) {
00091 MsgHandler::getErrorInstance()->inform("Could not set option '" + key + "' (probably defined twice).");
00092 myError = true;
00093 }
00094 } catch (InvalidArgument e) {
00095 MsgHandler::getErrorInstance()->inform(e.what());
00096 myError = true;
00097 }
00098 }
00099 }
00100
00101
00102 void OptionsLoader::characters(const XMLCh* const chars,
00103 const XERCES3_SIZE_t length) {
00104 myValue = myValue + TplConvert<XMLCh>::_2str(chars, (unsigned int) length);
00105 }
00106
00107
00108 bool
00109 OptionsLoader::setSecure(const std::string &name,
00110 bool value) const throw() {
00111 if (myOptions.isWriteable(name)) {
00112 myOptions.set(name, value);
00113 return true;
00114 }
00115 return false;
00116 }
00117
00118
00119 bool
00120 OptionsLoader::setSecure(const std::string &name,
00121 const std::string &value) const throw() {
00122 if (myOptions.isWriteable(name)) {
00123 myOptions.set(name, value);
00124 return true;
00125 }
00126 return false;
00127 }
00128
00129
00130 void
00131 OptionsLoader::endElement(const XMLCh* const ) {
00132 if (myItem.length()==0 || myValue.length()==0) {
00133 return;
00134 }
00135 if (myValue.find_first_not_of("\n\t \a")==std::string::npos) {
00136 return;
00137 }
00138 setValue(myItem, myValue);
00139 myItem = "";
00140 myValue = "";
00141 }
00142
00143
00144 void
00145 OptionsLoader::warning(const SAXParseException& exception) {
00146 WRITE_WARNING(TplConvert<XMLCh>::_2str(exception.getMessage()));
00147 WRITE_WARNING(" (At line/column " \
00148 + toString(exception.getLineNumber()+1) + '/' \
00149 + toString(exception.getColumnNumber()) + ").");
00150 myError = true;
00151 }
00152
00153
00154 void
00155 OptionsLoader::error(const SAXParseException& exception) {
00156 MsgHandler::getErrorInstance()->inform(
00157 TplConvert<XMLCh>::_2str(exception.getMessage()));
00158 MsgHandler::getErrorInstance()->inform(
00159 " (At line/column "
00160 + toString(exception.getLineNumber()+1) + '/'
00161 + toString(exception.getColumnNumber()) + ").");
00162 myError = true;
00163 }
00164
00165
00166 void
00167 OptionsLoader::fatalError(const SAXParseException& exception) {
00168 MsgHandler::getErrorInstance()->inform(
00169 TplConvert<XMLCh>::_2str(exception.getMessage()));
00170 MsgHandler::getErrorInstance()->inform(
00171 " (At line/column "
00172 + toString(exception.getLineNumber()+1) + '/'
00173 + toString(exception.getColumnNumber()) + ").");
00174 myError = true;
00175 }
00176
00177
00178 bool
00179 OptionsLoader::errorOccured() const throw() {
00180 return myError;
00181 }
00182
00183
00184
00185
00186