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 <iostream>
34 #include <xercesc/sax2/XMLReaderFactory.hpp>
35 #include <xercesc/util/PlatformUtils.hpp>
38 #include "SUMOSAXHandler.h"
39 #include "XMLSubSys.h"
40 
41 #ifdef CHECK_MEMORY_LEAKS
42 #include <foreign/nvwa/debug_new.h>
43 #endif // CHECK_MEMORY_LEAKS
44 
45 
46 // ===========================================================================
47 // static member variables
48 // ===========================================================================
49 std::vector<SAX2XMLReader*> XMLSubSys::myReaders;
50 unsigned int XMLSubSys::myNextFreeReader;
52 
53 
54 // ===========================================================================
55 // method definitions
56 // ===========================================================================
57 void
58 XMLSubSys::init(bool enableValidation) {
59  myEnableValidation = enableValidation;
60  try {
61  XMLPlatformUtils::Initialize();
62  myReaders.push_back(getSAXReader());
63  myNextFreeReader = 0;
64  } catch (const XMLException& e) {
65  throw ProcessError("Error during XML-initialization:\n " + TplConvert<XMLCh>::_2str(e.getMessage()));
66  }
67 }
68 
69 
70 void
72  for (std::vector<SAX2XMLReader*>::iterator i = myReaders.begin(); i != myReaders.end(); ++i) {
73  delete *i;
74  }
75  myReaders.clear();
76  XMLPlatformUtils::Terminate();
77 }
78 
79 
80 SAX2XMLReader*
82  SAX2XMLReader* reader = getSAXReader();
83  if (reader == 0) {
84  return 0;
85  }
86  reader->setContentHandler(&handler);
87  reader->setErrorHandler(&handler);
88  return reader;
89 }
90 
91 
92 void
94  myReaders[myNextFreeReader - 1]->setContentHandler(&handler);
95  myReaders[myNextFreeReader - 1]->setErrorHandler(&handler);
96 }
97 
98 
99 bool
101  const std::string& file) {
102  try {
103  if (myNextFreeReader == myReaders.size()) {
104  myReaders.push_back(getSAXReader());
105  }
107  setHandler(handler);
108  std::string prevFile = handler.getFileName();
109  handler.setFileName(file);
110  myReaders[myNextFreeReader - 1]->parse(file.c_str());
111  handler.setFileName(prevFile);
113  } catch (ProcessError& e) {
114  if (std::string(e.what()) != std::string("Process Error") && std::string(e.what()) != std::string("")) {
115  WRITE_ERROR(e.what());
116  }
117  return false;
118  } catch (...) {
119  WRITE_ERROR("An error occured.");
120  return false;
121  }
123 }
124 
125 
126 SAX2XMLReader*
128  SAX2XMLReader* reader = XMLReaderFactory::createXMLReader();
129  if (reader == 0) {
130  WRITE_ERROR("The XML-parser could not be build");
131  return 0;
132  }
133  if (!myEnableValidation) {
134  reader->setProperty(XMLUni::fgXercesScannerName, (void*)XMLUni::fgWFXMLScanner);
135  }
136  setFeature(*reader, "http://xml.org/sax/features/namespaces", false);
137  setFeature(*reader, "http://apache.org/xml/features/validation/schema", myEnableValidation);
138  setFeature(*reader, "http://apache.org/xml/features/validation/schema-full-checking", myEnableValidation);
139  setFeature(*reader, "http://xml.org/sax/features/validation", myEnableValidation);
140  setFeature(*reader, "http://apache.org/xml/features/validation/dynamic", myEnableValidation);
141  return reader;
142 }
143 
144 
145 void
146 XMLSubSys::setFeature(XERCES_CPP_NAMESPACE_QUALIFIER SAX2XMLReader& reader,
147  const std::string& feature, bool value) {
148  XMLCh* xmlFeature = XMLString::transcode(feature.c_str());
149  reader.setFeature(xmlFeature, value);
150  XMLString::release(&xmlFeature);
151 }
152 
153 
154 /****************************************************************************/
155