SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SUMOSAXReader.cpp
Go to the documentation of this file.
1 /****************************************************************************/
7 // SAX-reader encapsulation containing binary reader
8 /****************************************************************************/
9 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
10 // Copyright (C) 2001-2012 DLR (http://www.dlr.de/) and contributors
11 /****************************************************************************/
12 //
13 // This file is part of SUMO.
14 // SUMO is free software: you can redistribute it and/or modify
15 // it under the terms of the GNU General Public License as published by
16 // the Free Software Foundation, either version 3 of the License, or
17 // (at your option) any later version.
18 //
19 /****************************************************************************/
20 
21 
22 // ===========================================================================
23 // included modules
24 // ===========================================================================
25 #ifdef _MSC_VER
26 #include <windows_config.h>
27 #else
28 #include <config.h>
29 #endif
30 
31 #include <string>
32 #include <iostream>
33 #include <xercesc/sax2/XMLReaderFactory.hpp>
34 #include <xercesc/framework/MemBufInputSource.hpp>
35 #include <utils/common/ToString.h>
39 #include "GenericSAXHandler.h"
40 #include "SUMOSAXReader.h"
41 
42 #ifdef CHECK_MEMORY_LEAKS
43 #include <foreign/nvwa/debug_new.h>
44 #endif // CHECK_MEMORY_LEAKS
45 
46 
47 // ===========================================================================
48 // method definitions
49 // ===========================================================================
50 SUMOSAXReader::SUMOSAXReader(GenericSAXHandler& handler, const bool enableValidation)
51  : myHandler(&handler), myEnableValidation(enableValidation),
52  myXMLReader(0), myBinaryInput(0) {}
53 
54 
56  delete myXMLReader;
57  delete myBinaryInput;
58 }
59 
60 
61 void
63  myHandler = &handler;
64  if (myXMLReader != 0) {
65  myXMLReader->setContentHandler(&handler);
66  myXMLReader->setErrorHandler(&handler);
67  }
68 }
69 
70 
71 void
72 SUMOSAXReader::parse(std::string systemID) {
73  if (systemID.substr(systemID.length() - 4) == ".sbx") {
74  if (parseFirst(systemID)) {
75  while (parseNext());
76  }
77  } else {
78  if (myXMLReader == 0) {
80  }
81  myXMLReader->parse(systemID.c_str());
82  }
83 }
84 
85 
86 void
87 SUMOSAXReader::parseString(std::string content) {
88  if (myXMLReader == 0) {
90  }
91  XERCES_CPP_NAMESPACE::MemBufInputSource memBufIS((const XMLByte*)content.c_str(), content.size(), "registrySettings");
92  myXMLReader->parse(memBufIS);
93 }
94 
95 
96 bool
97 SUMOSAXReader::parseFirst(std::string systemID) {
98  if (systemID.substr(systemID.length() - 4) == ".sbx") {
99  myBinaryInput = new BinaryInputDevice(systemID, true, myEnableValidation);
100  char sbxVer;
101  *myBinaryInput >> sbxVer;
102  if (sbxVer != 1) {
103  throw ProcessError("Unknown sbx version");
104  }
105  std::string sumoVer;
106  *myBinaryInput >> sumoVer;
107  std::vector<std::string> elems;
108  *myBinaryInput >> elems;
109  // !!! check elems here
110  elems.clear();
111  *myBinaryInput >> elems;
112  // !!! check attrs here
113  elems.clear();
114  *myBinaryInput >> elems;
115  // !!! check node types here
116  elems.clear();
117  *myBinaryInput >> elems;
118  // !!! check edge types here
119  elems.clear();
120  *myBinaryInput >> elems;
121  // !!! check edges here
122  std::vector< std::vector<unsigned int> > followers;
123  *myBinaryInput >> followers;
124  // !!! check followers here
125  return parseNext();
126  } else {
127  if (myXMLReader == 0) {
129  }
130  myToken = XERCES_CPP_NAMESPACE::XMLPScanToken();
131  return myXMLReader->parseFirst(systemID.c_str(), myToken);
132  }
133 }
134 
135 
136 bool
138  if (myBinaryInput != 0) {
139  int next = myBinaryInput->peek();
140  switch (next) {
141  case EOF:
142  delete myBinaryInput;
143  myBinaryInput = 0;
144  return false;
146  char t;
147  *myBinaryInput >> t;
149  myHandler->myStartElement(t, attrs);
150  break;
151  }
153  char t;
154  *myBinaryInput >> t;
156  break;
157  }
158  default:
159  throw ProcessError("Invalid binary file");
160  }
161  return true;
162  } else {
163  if (myXMLReader == 0) {
164  throw ProcessError("The XML-parser was not initialized.");
165  }
166  return myXMLReader->parseNext(myToken);
167  }
168 }
169 
170 
171 XERCES_CPP_NAMESPACE::SAX2XMLReader*
173  XERCES_CPP_NAMESPACE::SAX2XMLReader* reader = XERCES_CPP_NAMESPACE::XMLReaderFactory::createXMLReader();
174  if (reader == 0) {
175  throw ProcessError("The XML-parser could not be build.");
176  }
177  if (!myEnableValidation) {
178  reader->setProperty(XERCES_CPP_NAMESPACE::XMLUni::fgXercesScannerName, (void*)XERCES_CPP_NAMESPACE::XMLUni::fgWFXMLScanner);
179  }
180 // see here https://svn.apache.org/repos/asf/xerces/c/trunk/samples/src/SAX2Count/SAX2Count.cpp for the way to set features
181  reader->setFeature(XERCES_CPP_NAMESPACE::XMLUni::fgXercesSchema, myEnableValidation);
182  reader->setFeature(XERCES_CPP_NAMESPACE::XMLUni::fgSAX2CoreValidation, myEnableValidation);
183  reader->setFeature(XERCES_CPP_NAMESPACE::XMLUni::fgXercesDynamic, myEnableValidation);
184  reader->setContentHandler(myHandler);
185  reader->setErrorHandler(myHandler);
186  return reader;
187 }
188 
189 
190 /****************************************************************************/