TSPLIB
src/ValidateIO.cpp
Go to the documentation of this file.
00001 
00011 #include <iostream>
00012 #include <algorithm>
00013 #include <cmath>
00014 
00015 #include <xercesc/util/PlatformUtils.hpp>
00016 #include <xercesc/util/XMLString.hpp>
00017 #include <xercesc/sax2/SAX2XMLReader.hpp>
00018 #include <xercesc/sax2/XMLReaderFactory.hpp>
00019 #include <xercesc/util/OutOfMemoryException.hpp>
00020 
00021 #include "ValidateConstantsFunctionsAndClasses.hpp"
00022 #include "ValidateSAX2ErrorHandler.hpp"
00023 #include "ValidateSAX2ContentHandler.hpp"
00024 
00025 #include "ValidateIO.hpp"
00026 
00027 using namespace std;
00028 using namespace xercesc;
00029 
00030 XERCES_CPP_NAMESPACE_USE
00031 
00032 
00033 std::string parseCommandLineArguments(int argc, char* argv[]) {
00034         /*
00035          * Initialling.
00036          */
00037         string inputFileName = "";
00038         switch (argc) {
00039         case 1:
00040         {
00041                 try {
00042                         cout <<
00043                                         "The name of input file (the filename extension has to be \"" <<
00044                                         INPUT_FILE_FILENAME_EXTENSION.substr(1, INPUT_FILE_FILENAME_EXTENSION.size() - 1) <<
00045                                         "\"): ";
00046                         getline(cin, inputFileName);
00047                 }
00048                 catch (ios::failure &e) {
00049                     throw CommandLineArgumentsInvalid();
00050             }
00051         }
00052         break;
00053         case 2:
00054         {
00055                 inputFileName = argv[1];
00056         }
00057         break;
00058         default:
00059         {
00060             throw CommandLineArgumentsInvalid();
00061         }
00062         }
00063 
00064         //Checking of validity of the inputFileName
00065         if (inputFileName.size() < INPUT_FILE_FILENAME_EXTENSION.size() + 1) {
00066             throw CommandLineArgumentsInvalid();
00067         }
00068 
00069         string inputFileFilenameExtension =
00070                         inputFileName.substr(
00071                                         inputFileName.size() - INPUT_FILE_FILENAME_EXTENSION.size(),
00072                                         INPUT_FILE_FILENAME_EXTENSION.size());
00073         transform(
00074                         inputFileFilenameExtension.begin(),
00075                         inputFileFilenameExtension.end(),
00076                         inputFileFilenameExtension.begin(), ::tolower);
00077 
00078         if (inputFileFilenameExtension != INPUT_FILE_FILENAME_EXTENSION) {
00079             throw CommandLineArgumentsInvalid();
00080         }
00081 
00082         return (inputFileName);
00083 }
00084 
00088 class XMLStringTranscode {
00089 private :
00093     XMLCh *stringUnicodeForm;
00094 public:
00099     inline XMLStringTranscode(const string toTranscode) {
00100         // Call the private transcoding method
00101         stringUnicodeForm = XMLString::transcode(toTranscode.c_str());
00102     }
00103 
00107     inline ~XMLStringTranscode() {
00108         XMLString::release(&stringUnicodeForm);
00109     }
00110 
00115     inline const XMLCh *unicodeForm() const {
00116         return (stringUnicodeForm);
00117     }
00118 };
00119 #define unicodeForm(str) XMLStringTranscode(str).unicodeForm()
00120 
00121 Instance *instanceIn(const std::string &inputFileName) {
00122         Instance *instance = 0;
00123 
00124         try {
00125                 XMLPlatformUtils::Initialize();
00126         }
00127         catch (const XMLException &e) {
00128                 throw;
00129         }
00130 
00131         try     {
00132                 //Gets the SAX parser.
00133                 SAX2XMLReader* parser = XMLReaderFactory::createXMLReader();
00134 
00135                 parser->setFeature(XMLUni::fgSAX2CoreNameSpaces, true);
00136                 parser->setFeature(XMLUni::fgSAX2CoreNameSpacePrefixes, true);
00137                 parser->setFeature(XMLUni::fgSAX2CoreValidation, true);
00138 
00139                 //Validation.
00140                 parser->setFeature(XMLUni::fgXercesSchema, true);
00141                 parser->setFeature(XMLUni::fgXercesSchemaFullChecking, true);
00142                 parser->setFeature(XMLUni::fgXercesValidationErrorAsFatal, true);
00143                 parser->setFeature(XMLUni::fgXercesContinueAfterFatalError, false);
00144 
00145                 //Use the loaded grammar during parsing.
00146                 parser->setFeature(XMLUni::fgXercesUseCachedGrammarInParse, true);
00147 
00148                 //Don't load schemas from any other source (e.g., from XML document's
00149                 //xsi:schemaLocation attributes).
00150                 parser->setFeature(XMLUni::fgXercesLoadSchema, false);
00151 
00152         if (!parser->loadGrammar (VALIDATION_SCHEMA.c_str(), Grammar::SchemaGrammarType, true)) {
00153                 throw ValidationSchemaDoesNotExist();
00154         }
00155 
00156                 //ErrorHandler.
00157             SAX2ErrorHandler errorHandler;
00158                 parser->setErrorHandler(&errorHandler);
00159 
00160                 //DocumentHandler.
00161                 SAX2ContentHandler contentHandler;
00162             parser->setContentHandler(&contentHandler);
00163 
00164             parser->parse(inputFileName.c_str());
00165 
00166             if (errorHandler.getFailed()) {
00167                 throw ValidationFailed();
00168             }
00169 
00170             if (contentHandler.getFailed()) {
00171                 throw ValidationFailed();
00172             }
00173 
00174             instance = new Instance(
00175                         contentHandler.getName(),
00176                         contentHandler.getSource(),
00177                         contentHandler.getDescription(),
00178                         contentHandler.getDoublePrecision(),
00179                         contentHandler.getIgnoredDigits(),
00180                         Graph(contentHandler.getAdjacencyMatrix()));
00181         }
00182         catch (bad_alloc &e) {
00183                 if (instance != 0) {
00184                         delete instance;
00185                 }
00186                 XMLPlatformUtils::Terminate();
00187                 throw;
00188         }
00189     catch (OutOfMemoryException &e) {
00190                 XMLPlatformUtils::Terminate();
00191                 throw;
00192     }
00193     catch (XMLException &e) {
00194                 XMLPlatformUtils::Terminate();
00195                 throw;
00196     }
00197     catch (ValidationSchemaDoesNotExist &e) {
00198                 XMLPlatformUtils::Terminate();
00199                 throw;
00200     }
00201     catch (ValidationFailed &e) {
00202                 XMLPlatformUtils::Terminate();
00203                 throw;
00204     }
00205 /*      catch ( ... ) {
00206                 if (instance != 0) {
00207                         delete instance;
00208                 }
00209                 XMLPlatformUtils::Terminate();
00210                 throw;
00211         }*/
00212 
00213         XMLPlatformUtils::Terminate();
00214 
00215         return (instance);
00216 }
 All Classes Files Functions Variables Friends Defines