TSPLIB
|
00001 00011 #include <fstream> 00012 #include <sstream> 00013 #include <cmath> 00014 00015 #include <xercesc/util/XMLString.hpp> 00016 #include <xercesc/sax2/Attributes.hpp> 00017 00018 #include "ValidateConstantsFunctionsAndClasses.hpp" 00019 00020 #include "ValidateSAX2ContentHandler.hpp" 00021 00022 using namespace std; 00023 00024 00028 class XMLStringTranscode { 00029 private: 00033 XMLCh *stringUnicodeForm; 00034 public: 00039 inline XMLStringTranscode(const string toTranscode) { 00040 // Call the private transcoding method 00041 stringUnicodeForm = XMLString::transcode(toTranscode.c_str()); 00042 } 00043 00047 inline ~XMLStringTranscode() { 00048 XMLString::release(&stringUnicodeForm); 00049 } 00050 00055 inline const XMLCh *unicodeForm() const { 00056 return (stringUnicodeForm); 00057 } 00058 }; 00059 #define unicodeForm(str) XMLStringTranscode(str).unicodeForm() 00060 00064 class StringTranscode { 00065 private: 00069 string stringStringForm; 00070 public: 00075 inline StringTranscode(const XMLCh *toTranscode) { 00076 // Call the private transcoding method 00077 char* cStringForm = XMLString::transcode(toTranscode); 00078 stringStringForm = string(cStringForm); 00079 } 00080 00084 inline ~StringTranscode() { 00085 } 00086 00091 inline const string stringForm() const { 00092 return (stringStringForm); 00093 } 00094 }; 00095 #define stringForm(str) StringTranscode(str).stringForm() 00096 00097 void SAX2ContentHandler::init() { 00098 failed = false; 00099 name = ""; 00100 source = ""; 00101 description = ""; 00102 doublePrecision = 0; 00103 doubleZero = 0.0; 00104 firstVertex = true; 00105 numberOfParsedVertices = 0; 00106 n = adjacencyMatrix.size(); 00107 for (std::vector<std::vector<double> >::size_type i = 0; i < n; i++) { 00108 adjacencyMatrix.at(i).clear(); 00109 } 00110 adjacencyMatrix.clear(); 00111 n = 0; 00112 costsOfEdgesDefinedInTheFirstVertex.clear(); 00113 buffer = ""; 00114 cost = 0.0; 00115 } 00116 00117 SAX2ContentHandler::SAX2ContentHandler(): ContentHandler() { 00118 init(); 00119 } 00120 00121 SAX2ContentHandler::~SAX2ContentHandler() { 00122 } 00123 00124 void SAX2ContentHandler::setDocumentLocator(const Locator *const) { 00125 } 00126 00127 void SAX2ContentHandler::startDocument() { 00128 init(); 00129 } 00130 00131 void SAX2ContentHandler::endDocument() { 00132 //The decision if the graph is undirected. If yes, 00133 //the matrix will be resized. 00134 bool isUndirected = true; 00135 for (vector<vector<double> >::size_type i = 0; i < n; i++) { 00136 for ( 00137 vector<double>::size_type j = 0; 00138 j < static_cast<vector<double>::size_type>(i); 00139 j++) { 00140 if (!parsedEntries.at( 00141 static_cast<vector<vector<bool> >::size_type>(i)).at( 00142 static_cast<vector<bool>::size_type>(j))) { 00143 failed = true; 00144 return; 00145 } 00146 if (!parsedEntries.at( 00147 static_cast<vector<vector<bool> >::size_type>(j)).at( 00148 static_cast<vector<bool>::size_type>(i))) { 00149 failed = true; 00150 return; 00151 } 00152 if ( 00153 abs(adjacencyMatrix.at(i).at(j) - 00154 adjacencyMatrix.at( 00155 static_cast<vector<vector<double> >::size_type>(j)).at( 00156 static_cast<vector<double>::size_type>(i))) > 00157 doubleZero) { 00158 isUndirected = false; 00159 } 00160 } 00161 } 00162 00163 //Setting 0 on the main diagonal if necessary 00164 if (isUndirected) { 00165 for (vector<vector<double> >::size_type i = 0; i < n; i++) { 00166 adjacencyMatrix.at(i).resize(static_cast<vector<double>::size_type>(i + 1)); 00167 if (!parsedEntries.at(i).at( 00168 static_cast<vector<bool>::size_type>(i))) { 00169 adjacencyMatrix.at(i).at(static_cast<vector<double>::size_type>(i)) = 0; 00170 } 00171 } 00172 } 00173 else { 00174 for (vector<vector<bool> >::size_type i = 0; i < n; i++) { 00175 if (!parsedEntries.at(i).at( 00176 static_cast<vector<bool>::size_type>(i))) { 00177 adjacencyMatrix.at(i).at(static_cast<vector<double>::size_type>(i)) = 0; 00178 } 00179 } 00180 } 00181 } 00182 00183 void SAX2ContentHandler::startPrefixMapping(const XMLCh *const, const XMLCh *const) { 00184 } 00185 00186 void SAX2ContentHandler::endPrefixMapping(const XMLCh *const) { 00187 } 00188 00189 void SAX2ContentHandler::skippedEntity(const XMLCh *const) { 00190 } 00191 00192 void SAX2ContentHandler::startElement( 00193 const XMLCh *const, 00194 const XMLCh *const localname, 00195 const XMLCh *const, 00196 const Attributes &attributes) { 00197 buffer.clear(); 00198 00199 //vertex 00200 if (XMLString::compareIString(localname, unicodeForm(XML_VERTEX)) == 0) { 00201 if (firstVertex) { 00202 n = 1; 00203 } 00204 } 00205 00206 //edge 00207 else if (XMLString::compareIString(localname, unicodeForm(XML_EDGE)) == 0) { 00208 if (firstVertex) { 00209 costsOfEdgesDefinedInTheFirstVertex.resize( 00210 costsOfEdgesDefinedInTheFirstVertex.size() + 1); 00211 } 00212 const XMLCh* costXMLCh = attributes.getValue(static_cast<XMLSize_t>(0)); 00213 string costString = stringForm(costXMLCh); 00214 trim(costString); 00215 istringstream costIStringstream(costString); 00216 costIStringstream.exceptions(ifstream::failbit | ifstream::badbit); 00217 try { 00218 costIStringstream >> cost; 00219 } 00220 catch (ifstream::failure &e) { 00221 failed = true; 00222 } 00223 } 00224 } 00225 00226 void SAX2ContentHandler::endElement( 00227 const XMLCh *const, 00228 const XMLCh *const localname, 00229 const XMLCh *const) { 00230 //name 00231 if (XMLString::compareIString(localname, unicodeForm(XML_NAME)) == 0) { 00232 name = buffer; 00233 trim(name); 00234 } 00235 00236 //source 00237 else if (XMLString::compareIString(localname, unicodeForm(XML_SOURCE)) == 0) { 00238 source = buffer; 00239 trim(source); 00240 } 00241 00242 //descritpion 00243 else if (XMLString::compareIString(localname, unicodeForm(XML_DESCRIPTION)) == 0) { 00244 description = buffer; 00245 trim(description); 00246 } 00247 00248 //doublePrecision 00249 else if (XMLString::compareIString(localname, unicodeForm(XML_DOUBLE_PRECISION)) == 0) { 00250 string doublePrecisionString = buffer; 00251 trim(doublePrecisionString); 00252 istringstream doublePrecisionIStringstream(doublePrecisionString); 00253 doublePrecisionIStringstream.exceptions(ifstream::failbit | ifstream::badbit); 00254 try { 00255 doublePrecisionIStringstream >> doublePrecision; 00256 } 00257 catch (ifstream::failure &e) { 00258 failed = true; 00259 } 00260 } 00261 00262 //ignoredDigits 00263 else if (XMLString::compareIString(localname, unicodeForm(XML_IGNORED_DIGITS)) == 0) { 00264 string ignoredDigitsString = buffer; 00265 trim(ignoredDigitsString); 00266 istringstream ignoredDigitsIStringstream(ignoredDigitsString); 00267 ignoredDigitsIStringstream.exceptions(ifstream::failbit | ifstream::badbit); 00268 try { 00269 ignoredDigitsIStringstream >> ignoredDigits; 00270 } 00271 catch (ifstream::failure &e) { 00272 failed = true; 00273 } 00274 doubleZero = pow(10, 00275 -1.0 * (static_cast<double>(doublePrecision - ignoredDigits))); 00276 } 00277 00278 //graph 00279 else if (XMLString::compareIString(localname, unicodeForm(XML_GRAPH)) == 0) { 00280 //We have to check if all vertices are defined. 00281 if (numberOfParsedVertices != n) { 00282 failed = true; 00283 } 00284 } 00285 00286 //vertex 00287 else if (XMLString::compareIString(localname, unicodeForm(XML_VERTEX)) == 0) { 00288 //If the first vertex is read. We can assume that the graph is complete 00289 //(with, or without self-loops). 00290 if (firstVertex) { 00291 firstVertex = false; 00292 parsedEntries.resize(static_cast<vector<vector<bool> >::size_type>(n)); 00293 adjacencyMatrix.resize(n); 00294 00295 //Resizing. 00296 for (vector<vector<double> >::size_type i = 0; i < n; i++) { 00297 parsedEntries.at(static_cast<vector<vector<bool> >::size_type>(i)).resize( 00298 static_cast<vector<bool>::size_type>(n)); 00299 adjacencyMatrix.at(i).resize( 00300 static_cast<vector<double>::size_type>(n)); 00301 } 00302 00303 //Initializing 00304 for (vector<vector<bool> >::size_type i = 0; i < n; i++) { 00305 for (vector<bool>::size_type j = 0; j < n; j++) { 00306 parsedEntries.at(i).at(j) = false; 00307 } 00308 } 00309 00310 //Copying of the first row. 00311 for ( 00312 vector<double>::size_type j = 0; 00313 j < costsOfEdgesDefinedInTheFirstVertex.size(); 00314 j++) { 00315 if (edgesDefinedInTheFirstVertex.count(j) > 0) { 00316 adjacencyMatrix.at(0).at(j) = costsOfEdgesDefinedInTheFirstVertex.at(j); 00317 parsedEntries.at( 00318 static_cast<vector<vector<bool> >::size_type>(0)).at( 00319 static_cast<vector<bool>::size_type>(j)) = true; 00320 } 00321 } 00322 } 00323 00324 //Vertex counter. 00325 numberOfParsedVertices++; 00326 } 00327 00328 //edge 00329 else if (XMLString::compareIString(localname, unicodeForm(XML_EDGE)) == 0) { 00330 vector<double>::size_type edge; 00331 string edgeString = buffer; 00332 trim(edgeString); 00333 istringstream edgeIStringstream(edgeString); 00334 edgeIStringstream.exceptions(ifstream::failbit | ifstream::badbit); 00335 try { 00336 edgeIStringstream >> edge; 00337 } 00338 catch (ifstream::failure &e) { 00339 failed = true; 00340 } 00341 00342 //Now, the inequality numberOfParsedEdges >= 0 allways holds. 00343 //We need special handling of the first row (since we do not know the number 00344 //of vertices) 00345 if (firstVertex) { 00346 //In a complete graph there must be also defined an edge to the last vertex. 00347 if (edge + 1 > n) { 00348 n = edge + 1; 00349 } 00350 00351 //If necessary, we make the first row longer. 00352 if (costsOfEdgesDefinedInTheFirstVertex.size() < edge + 1) { 00353 costsOfEdgesDefinedInTheFirstVertex.resize(edge + 1); 00354 } 00355 00356 //The edges are not allowed to be redefined. 00357 if (edgesDefinedInTheFirstVertex.count(edge) > 0) { 00358 failed = true; 00359 return; 00360 } 00361 00362 //We mark the edges which have been defined. 00363 edgesDefinedInTheFirstVertex.insert(edge); 00364 costsOfEdgesDefinedInTheFirstVertex.at(edge) = cost; 00365 } 00366 else { 00367 //We try to set the matrix entries 00368 try { 00369 //The edges are not allowed to be redefined. 00370 if (parsedEntries.at( 00371 static_cast<vector<vector<bool> >::size_type>(numberOfParsedVertices)).at( 00372 static_cast<vector<bool>::size_type>(edge))) { 00373 failed = true; 00374 return; 00375 } 00376 00377 //We set the cost. 00378 parsedEntries.at( 00379 static_cast<vector<vector<bool> >::size_type>(numberOfParsedVertices)).at( 00380 static_cast<vector<bool>::size_type>(edge)) = true; 00381 adjacencyMatrix.at(numberOfParsedVertices).at(edge) = cost; 00382 } 00383 catch ( ... ) { 00384 failed = true; 00385 } 00386 } 00387 } 00388 } 00389 00390 void SAX2ContentHandler::characters(const XMLCh *const chars, const XMLSize_t) { 00391 string toAppend = stringForm(chars); 00392 trim(toAppend); 00393 string::size_type i = 0; 00394 while (i < toAppend.size()) { 00395 switch (toAppend.at(i)) { 00396 case '\t': 00397 case '\r': 00398 case '\n': 00399 { 00400 toAppend.erase(i, static_cast<string::size_type>(1)); 00401 } 00402 break; 00403 default: 00404 { 00405 i++; 00406 } 00407 break; 00408 } 00409 } 00410 00411 buffer.append(toAppend); 00412 } 00413 00414 void SAX2ContentHandler::ignorableWhitespace(const XMLCh *const, const XMLSize_t) { 00415 } 00416 00417 void SAX2ContentHandler::processingInstruction(const XMLCh *const, const XMLCh *const) { 00418 } 00419 00420 void SAX2ContentHandler::resetDocument() { 00421 init(); 00422 }