SUMO - Simulation of Urban MObility
GenericSAXHandler.cpp
Go to the documentation of this file.
1 /****************************************************************************/
10 // A handler which converts occuring elements and attributes into enums
11 /****************************************************************************/
12 // SUMO, Simulation of Urban MObility; see http://sumo.dlr.de/
13 // Copyright (C) 2002-2016 DLR (http://www.dlr.de/) and contributors
14 /****************************************************************************/
15 //
16 // This file is part of SUMO.
17 // SUMO is free software: you can redistribute it and/or modify
18 // it under the terms of the GNU General Public License as published by
19 // the Free Software Foundation, either version 3 of the License, or
20 // (at your option) any later version.
21 //
22 /****************************************************************************/
23 
24 
25 // ===========================================================================
26 // included modules
27 // ===========================================================================
28 #ifdef _MSC_VER
29 #include <windows_config.h>
30 #else
31 #include <config.h>
32 #endif
33 
34 #include <cassert>
35 #include "GenericSAXHandler.h"
40 #include <utils/common/ToString.h>
42 #include "XMLSubSys.h"
43 
44 #ifdef CHECK_MEMORY_LEAKS
45 #include <foreign/nvwa/debug_new.h>
46 #endif // CHECK_MEMORY_LEAKS
47 
48 
49 // ===========================================================================
50 // class definitions
51 // ===========================================================================
53  StringBijection<int>::Entry* tags, int terminatorTag,
54  StringBijection<int>::Entry* attrs, int terminatorAttr,
55  const std::string& file)
56  : myParentHandler(0), myParentIndicator(SUMO_TAG_NOTHING), myFileName(file) {
57  int i = 0;
58  while (tags[i].key != terminatorTag) {
59  myTagMap.insert(TagMap::value_type(tags[i].str, tags[i].key));
60  i++;
61  }
62  i = 0;
63  while (attrs[i].key != terminatorAttr) {
64  assert(myPredefinedTags.find(attrs[i].key) == myPredefinedTags.end());
65  myPredefinedTags[attrs[i].key] = convert(attrs[i].str);
66  myPredefinedTagsMML[attrs[i].key] = attrs[i].str;
67  i++;
68  }
69 }
70 
71 
73  for (AttrMap::iterator i1 = myPredefinedTags.begin(); i1 != myPredefinedTags.end(); i1++) {
74  delete[](*i1).second;
75  }
76 }
77 
78 
79 void
80 GenericSAXHandler::setFileName(const std::string& name) {
81  myFileName = name;
82 }
83 
84 
85 const std::string&
87  return myFileName;
88 }
89 
90 
91 XMLCh*
92 GenericSAXHandler::convert(const std::string& name) const {
93  int len = (int)name.length();
94  XMLCh* ret = new XMLCh[len + 1];
95  int i = 0;
96  for (; i < len; i++) {
97  ret[i] = (XMLCh) name[i];
98  }
99  ret[i] = 0;
100  return ret;
101 }
102 
103 
104 void
105 GenericSAXHandler::startElement(const XMLCh* const /*uri*/,
106  const XMLCh* const /*localname*/,
107  const XMLCh* const qname,
108  const XERCES_CPP_NAMESPACE::Attributes& attrs) {
109  std::string name = TplConvert::_2str(qname);
110  int element = convertTag(name);
111  myCharactersVector.clear();
113  if (element == SUMO_TAG_INCLUDE) {
114  std::string file = na.getString(SUMO_ATTR_HREF);
115  if (!FileHelpers::isAbsolute(file)) {
117  }
118  XMLSubSys::runParser(*this, file);
119  } else {
120  myStartElement(element, na);
121  }
122 }
123 
124 
125 void
126 GenericSAXHandler::endElement(const XMLCh* const /*uri*/,
127  const XMLCh* const /*localname*/,
128  const XMLCh* const qname) {
129  std::string name = TplConvert::_2str(qname);
130  int element = convertTag(name);
131  // collect characters
132  if (myCharactersVector.size() != 0) {
133  int len = 0;
134  for (int i = 0; i < (int)myCharactersVector.size(); ++i) {
135  len += (int)myCharactersVector[i].length();
136  }
137  char* buf = new char[len + 1];
138  int pos = 0;
139  for (int i = 0; i < (int)myCharactersVector.size(); ++i) {
140  memcpy((unsigned char*) buf + pos, (unsigned char*) myCharactersVector[i].c_str(),
141  sizeof(char)*myCharactersVector[i].length());
142  pos += (int)myCharactersVector[i].length();
143  }
144  buf[pos] = 0;
145 
146  // call user handler
147  try {
148  myCharacters(element, buf);
149  } catch (std::runtime_error&) {
150  delete[] buf;
151  throw;
152  }
153  delete[] buf;
154  }
155  if (element != SUMO_TAG_INCLUDE) {
156  myEndElement(element);
157  if (myParentHandler && myParentIndicator == element) {
160  myParentHandler = 0;
161  }
162  }
163 }
164 
165 
166 void
168  myParentHandler = handler;
169  myParentIndicator = tag;
170  XMLSubSys::setHandler(*this);
171 }
172 
173 
174 void
175 GenericSAXHandler::characters(const XMLCh* const chars,
176  const XERCES3_SIZE_t length) {
177  myCharactersVector.push_back(TplConvert::_2str(chars, (int)length));
178 }
179 
180 
181 int
182 GenericSAXHandler::convertTag(const std::string& tag) const {
183  TagMap::const_iterator i = myTagMap.find(tag);
184  if (i == myTagMap.end()) {
185  return SUMO_TAG_NOTHING;
186  }
187  return (*i).second;
188 }
189 
190 
191 std::string
192 GenericSAXHandler::buildErrorMessage(const XERCES_CPP_NAMESPACE::SAXParseException& exception) {
193  std::ostringstream buf;
194  char* pMsg = XERCES_CPP_NAMESPACE::XMLString::transcode(exception.getMessage());
195  buf << pMsg << std::endl;
196  buf << " In file '" << getFileName() << "'" << std::endl;
197  buf << " At line/column " << exception.getLineNumber() + 1
198  << '/' << exception.getColumnNumber() << "." << std::endl;
199  XERCES_CPP_NAMESPACE::XMLString::release(&pMsg);
200  return buf.str();
201 }
202 
203 
204 void
205 GenericSAXHandler::warning(const XERCES_CPP_NAMESPACE::SAXParseException& exception) {
206  WRITE_WARNING(buildErrorMessage(exception));
207 }
208 
209 
210 void
211 GenericSAXHandler::error(const XERCES_CPP_NAMESPACE::SAXParseException& exception) {
212  throw ProcessError(buildErrorMessage(exception));
213 }
214 
215 
216 void
217 GenericSAXHandler::fatalError(const XERCES_CPP_NAMESPACE::SAXParseException& exception) {
218  throw ProcessError(buildErrorMessage(exception));
219 }
220 
221 
222 void
224 
225 
226 void
227 GenericSAXHandler::myCharacters(int, const std::string&) {}
228 
229 
230 void
232 
233 
234 /****************************************************************************/
235 
static std::string getConfigurationRelative(const std::string &configPath, const std::string &path)
Returns the second path as a relative path to the first file.
Definition: FileHelpers.cpp:86
int convertTag(const std::string &tag) const
Converts a tag from its string into its numerical representation.
GenericSAXHandler(StringBijection< int >::Entry *tags, int terminatorTag, StringBijection< int >::Entry *attrs, int terminatorAttr, const std::string &file)
Constructor.
const std::string & getFileName() const
returns the current file name
std::string myFileName
The name of the currently parsed file.
std::string buildErrorMessage(const XERCES_CPP_NAMESPACE::SAXParseException &exception)
Builds an error message.
std::vector< std::string > myCharactersVector
A list of character strings obtained so far to build the complete characters string at the end...
static bool runParser(GenericSAXHandler &handler, const std::string &file, const bool isNet=false)
Runs the given handler on the given file; returns if everything&#39;s ok.
Definition: XMLSubSys.cpp:114
void error(const XERCES_CPP_NAMESPACE::SAXParseException &exception)
Handler for XML-errors.
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:200
void characters(const XMLCh *const chars, const XERCES3_SIZE_t length)
The inherited method called when characters occured.
virtual ~GenericSAXHandler()
Destructor.
A handler which converts occuring elements and attributes into enums.
void setFileName(const std::string &name)
Sets the current file name.
Encapsulated SAX-Attributes.
static bool isAbsolute(const std::string &path)
Returns the information whether the given path is absolute.
XMLCh * convert(const std::string &name) const
converts from c++-string into unicode
void warning(const XERCES_CPP_NAMESPACE::SAXParseException &exception)
Handler for XML-warnings.
virtual void myCharacters(int element, const std::string &chars)
Callback method for characters to implement by derived classes.
GenericSAXHandler * myParentHandler
The handler to give control back to.
void registerParent(const int tag, GenericSAXHandler *handler)
Assigning a parent handler which is enabled when the specified tag is closed.
static void setHandler(GenericSAXHandler &handler)
Sets the given handler for the default reader.
Definition: XMLSubSys.cpp:108
Encapsulated Xerces-SAX-attributes.
#define XERCES3_SIZE_t
Definition: config.h:231
std::string getString(int id) const
Returns the string-value of the named (by its enum-value) attribute.
void fatalError(const XERCES_CPP_NAMESPACE::SAXParseException &exception)
Handler for XML-errors.
virtual void myStartElement(int element, const SUMOSAXAttributes &attrs)
Callback method for an opening tag to implement by derived classes.
std::map< int, std::string > myPredefinedTagsMML
the map from ids to their string representation
static std::string _2str(const int var)
convert int to string
Definition: TplConvert.h:57
int myParentIndicator
The tag indicating that control should be given back.
void startElement(const XMLCh *const uri, const XMLCh *const localname, const XMLCh *const qname, const XERCES_CPP_NAMESPACE::Attributes &attrs)
The inherited method called when a new tag opens.
void endElement(const XMLCh *const uri, const XMLCh *const localname, const XMLCh *const qname)
The inherited method called when a tag is being closed.
virtual void myEndElement(int element)
Callback method for a closing tag to implement by derived classes.