SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
XMLSubSys.cpp
Go to the documentation of this file.
1 /****************************************************************************/
9 // Utility methods for initialising, closing and using the XML-subsystem
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 <xercesc/util/PlatformUtils.hpp>
36 #include "SUMOSAXHandler.h"
37 #include "SUMOSAXReader.h"
38 #include "XMLSubSys.h"
39 
40 #ifdef CHECK_MEMORY_LEAKS
41 #include <foreign/nvwa/debug_new.h>
42 #endif // CHECK_MEMORY_LEAKS
43 
44 
45 // ===========================================================================
46 // static member variables
47 // ===========================================================================
48 std::vector<SUMOSAXReader*> XMLSubSys::myReaders;
49 unsigned int XMLSubSys::myNextFreeReader;
51 
52 
53 // ===========================================================================
54 // method definitions
55 // ===========================================================================
56 void
58  try {
59  XERCES_CPP_NAMESPACE::XMLPlatformUtils::Initialize();
60  myNextFreeReader = 0;
61  } catch (const XERCES_CPP_NAMESPACE::XMLException& e) {
62  throw ProcessError("Error during XML-initialization:\n " + TplConvert::_2str(e.getMessage()));
63  }
64 }
65 
66 
67 void
68 XMLSubSys::setValidation(bool enableValidation) {
69  myEnableValidation = enableValidation;
70 }
71 
72 
73 void
75  for (std::vector<SUMOSAXReader*>::iterator i = myReaders.begin(); i != myReaders.end(); ++i) {
76  delete *i;
77  }
78  myReaders.clear();
79  XERCES_CPP_NAMESPACE::XMLPlatformUtils::Terminate();
80 }
81 
82 
85  return new SUMOSAXReader(handler, myEnableValidation);
86 }
87 
88 
89 void
91  myReaders[myNextFreeReader - 1]->setHandler(handler);
92 }
93 
94 
95 bool
97  const std::string& file) {
98  try {
99  if (myNextFreeReader == myReaders.size()) {
100  myReaders.push_back(new SUMOSAXReader(handler, myEnableValidation));
101  } else {
102  myReaders[myNextFreeReader]->setHandler(handler);
103  }
105  std::string prevFile = handler.getFileName();
106  handler.setFileName(file);
107  myReaders[myNextFreeReader - 1]->parse(file);
108  handler.setFileName(prevFile);
110  } catch (ProcessError& e) {
111  if (std::string(e.what()) != std::string("Process Error") && std::string(e.what()) != std::string("")) {
112  WRITE_ERROR(e.what());
113  }
114  return false;
115  } catch (...) {
116  WRITE_ERROR("An error occured.");
117  return false;
118  }
120 }
121 
122 
123 /****************************************************************************/
124