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-2017 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 
45 // ===========================================================================
46 // class definitions
47 // ===========================================================================
49  StringBijection<int>::Entry* tags, int terminatorTag,
50  StringBijection<int>::Entry* attrs, int terminatorAttr,
51  const std::string& file)
52  : myParentHandler(0), myParentIndicator(SUMO_TAG_NOTHING), myFileName(file) {
53  int i = 0;
54  while (tags[i].key != terminatorTag) {
55  myTagMap.insert(TagMap::value_type(tags[i].str, tags[i].key));
56  i++;
57  }
58  i = 0;
59  while (attrs[i].key != terminatorAttr) {
60  assert(myPredefinedTags.find(attrs[i].key) == myPredefinedTags.end());
61  myPredefinedTags[attrs[i].key] = convert(attrs[i].str);
62  myPredefinedTagsMML[attrs[i].key] = attrs[i].str;
63  i++;
64  }
65 }
66 
67 
69  for (AttrMap::iterator i1 = myPredefinedTags.begin(); i1 != myPredefinedTags.end(); i1++) {
70  delete[](*i1).second;
71  }
72 }
73 
74 
75 void
76 GenericSAXHandler::setFileName(const std::string& name) {
77  myFileName = name;
78 }
79 
80 
81 const std::string&
83  return myFileName;
84 }
85 
86 
87 XMLCh*
88 GenericSAXHandler::convert(const std::string& name) const {
89  int len = (int)name.length();
90  XMLCh* ret = new XMLCh[len + 1];
91  int i = 0;
92  for (; i < len; i++) {
93  ret[i] = (XMLCh) name[i];
94  }
95  ret[i] = 0;
96  return ret;
97 }
98 
99 
100 void
101 GenericSAXHandler::startElement(const XMLCh* const /*uri*/,
102  const XMLCh* const /*localname*/,
103  const XMLCh* const qname,
104  const XERCES_CPP_NAMESPACE::Attributes& attrs) {
105  std::string name = TplConvert::_2str(qname);
106  int element = convertTag(name);
107  myCharactersVector.clear();
109  if (element == SUMO_TAG_INCLUDE) {
110  std::string file = na.getString(SUMO_ATTR_HREF);
111  if (!FileHelpers::isAbsolute(file)) {
113  }
114  XMLSubSys::runParser(*this, file);
115  } else {
116  myStartElement(element, na);
117  }
118 }
119 
120 
121 void
122 GenericSAXHandler::endElement(const XMLCh* const /*uri*/,
123  const XMLCh* const /*localname*/,
124  const XMLCh* const qname) {
125  std::string name = TplConvert::_2str(qname);
126  int element = convertTag(name);
127  // collect characters
128  if (myCharactersVector.size() != 0) {
129  int len = 0;
130  for (int i = 0; i < (int)myCharactersVector.size(); ++i) {
131  len += (int)myCharactersVector[i].length();
132  }
133  char* buf = new char[len + 1];
134  int pos = 0;
135  for (int i = 0; i < (int)myCharactersVector.size(); ++i) {
136  memcpy((unsigned char*) buf + pos, (unsigned char*) myCharactersVector[i].c_str(),
137  sizeof(char)*myCharactersVector[i].length());
138  pos += (int)myCharactersVector[i].length();
139  }
140  buf[pos] = 0;
141 
142  // call user handler
143  try {
144  myCharacters(element, buf);
145  } catch (std::runtime_error&) {
146  delete[] buf;
147  throw;
148  }
149  delete[] buf;
150  }
151  if (element != SUMO_TAG_INCLUDE) {
152  myEndElement(element);
153  if (myParentHandler && myParentIndicator == element) {
156  myParentHandler = 0;
157  }
158  }
159 }
160 
161 
162 void
164  myParentHandler = handler;
165  myParentIndicator = tag;
166  XMLSubSys::setHandler(*this);
167 }
168 
169 
170 void
171 GenericSAXHandler::characters(const XMLCh* const chars,
172  const XERCES3_SIZE_t length) {
173  myCharactersVector.push_back(TplConvert::_2str(chars, (int)length));
174 }
175 
176 
177 int
178 GenericSAXHandler::convertTag(const std::string& tag) const {
179  TagMap::const_iterator i = myTagMap.find(tag);
180  if (i == myTagMap.end()) {
181  return SUMO_TAG_NOTHING;
182  }
183  return (*i).second;
184 }
185 
186 
187 std::string
188 GenericSAXHandler::buildErrorMessage(const XERCES_CPP_NAMESPACE::SAXParseException& exception) {
189  std::ostringstream buf;
190  char* pMsg = XERCES_CPP_NAMESPACE::XMLString::transcode(exception.getMessage());
191  buf << pMsg << std::endl;
192  buf << " In file '" << getFileName() << "'" << std::endl;
193  buf << " At line/column " << exception.getLineNumber() + 1
194  << '/' << exception.getColumnNumber() << "." << std::endl;
195  XERCES_CPP_NAMESPACE::XMLString::release(&pMsg);
196  return buf.str();
197 }
198 
199 
200 void
201 GenericSAXHandler::warning(const XERCES_CPP_NAMESPACE::SAXParseException& exception) {
202  WRITE_WARNING(buildErrorMessage(exception));
203 }
204 
205 
206 void
207 GenericSAXHandler::error(const XERCES_CPP_NAMESPACE::SAXParseException& exception) {
208  throw ProcessError(buildErrorMessage(exception));
209 }
210 
211 
212 void
213 GenericSAXHandler::fatalError(const XERCES_CPP_NAMESPACE::SAXParseException& exception) {
214  throw ProcessError(buildErrorMessage(exception));
215 }
216 
217 
218 void
220 
221 
222 void
223 GenericSAXHandler::myCharacters(int, const std::string&) {}
224 
225 
226 void
228 
229 
230 /****************************************************************************/
231 
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:82
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:110
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.
Definition: FileHelpers.cpp:97
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:104
Encapsulated Xerces-SAX-attributes.
#define XERCES3_SIZE_t
Definition: config.h:216
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.