SUMO - Simulation of Urban MObility
GenericSAXHandler.cpp
Go to the documentation of this file.
1 /****************************************************************************/
2 // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.org/sumo
3 // Copyright (C) 2002-2017 German Aerospace Center (DLR) and others.
4 /****************************************************************************/
5 //
6 // This program and the accompanying materials
7 // are made available under the terms of the Eclipse Public License v2.0
8 // which accompanies this distribution, and is available at
9 // http://www.eclipse.org/legal/epl-v20.html
10 //
11 /****************************************************************************/
20 // A handler which converts occuring elements and attributes into enums
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 <cassert>
34 #include "GenericSAXHandler.h"
39 #include <utils/common/ToString.h>
41 #include "XMLSubSys.h"
42 
43 
44 // ===========================================================================
45 // class definitions
46 // ===========================================================================
48  StringBijection<int>::Entry* tags, int terminatorTag,
49  StringBijection<int>::Entry* attrs, int terminatorAttr,
50  const std::string& file)
51  : myParentHandler(0), myParentIndicator(SUMO_TAG_NOTHING), myFileName(file) {
52  int i = 0;
53  while (tags[i].key != terminatorTag) {
54  myTagMap.insert(TagMap::value_type(tags[i].str, tags[i].key));
55  i++;
56  }
57  i = 0;
58  while (attrs[i].key != terminatorAttr) {
59  assert(myPredefinedTags.find(attrs[i].key) == myPredefinedTags.end());
60  myPredefinedTags[attrs[i].key] = convert(attrs[i].str);
61  myPredefinedTagsMML[attrs[i].key] = attrs[i].str;
62  i++;
63  }
64 }
65 
66 
68  for (AttrMap::iterator i1 = myPredefinedTags.begin(); i1 != myPredefinedTags.end(); i1++) {
69  delete[](*i1).second;
70  }
71 }
72 
73 
74 void
75 GenericSAXHandler::setFileName(const std::string& name) {
76  myFileName = name;
77 }
78 
79 
80 const std::string&
82  return myFileName;
83 }
84 
85 
86 XMLCh*
87 GenericSAXHandler::convert(const std::string& name) const {
88  int len = (int)name.length();
89  XMLCh* ret = new XMLCh[len + 1];
90  int i = 0;
91  for (; i < len; i++) {
92  ret[i] = (XMLCh) name[i];
93  }
94  ret[i] = 0;
95  return ret;
96 }
97 
98 
99 void
100 GenericSAXHandler::startElement(const XMLCh* const /*uri*/,
101  const XMLCh* const /*localname*/,
102  const XMLCh* const qname,
103  const XERCES_CPP_NAMESPACE::Attributes& attrs) {
104  std::string name = TplConvert::_2str(qname);
105  int element = convertTag(name);
106  myCharactersVector.clear();
108  if (element == SUMO_TAG_INCLUDE) {
109  std::string file = na.getString(SUMO_ATTR_HREF);
110  if (!FileHelpers::isAbsolute(file)) {
112  }
113  XMLSubSys::runParser(*this, file);
114  } else {
115  myStartElement(element, na);
116  }
117 }
118 
119 
120 void
121 GenericSAXHandler::endElement(const XMLCh* const /*uri*/,
122  const XMLCh* const /*localname*/,
123  const XMLCh* const qname) {
124  std::string name = TplConvert::_2str(qname);
125  int element = convertTag(name);
126  // collect characters
127  if (myCharactersVector.size() != 0) {
128  int len = 0;
129  for (int i = 0; i < (int)myCharactersVector.size(); ++i) {
130  len += (int)myCharactersVector[i].length();
131  }
132  char* buf = new char[len + 1];
133  int pos = 0;
134  for (int i = 0; i < (int)myCharactersVector.size(); ++i) {
135  memcpy((unsigned char*) buf + pos, (unsigned char*) myCharactersVector[i].c_str(),
136  sizeof(char)*myCharactersVector[i].length());
137  pos += (int)myCharactersVector[i].length();
138  }
139  buf[pos] = 0;
140 
141  // call user handler
142  try {
143  myCharacters(element, buf);
144  } catch (std::runtime_error&) {
145  delete[] buf;
146  throw;
147  }
148  delete[] buf;
149  }
150  if (element != SUMO_TAG_INCLUDE) {
151  myEndElement(element);
152  if (myParentHandler && myParentIndicator == element) {
155  myParentHandler = 0;
156  }
157  }
158 }
159 
160 
161 void
163  myParentHandler = handler;
164  myParentIndicator = tag;
165  XMLSubSys::setHandler(*this);
166 }
167 
168 
169 void
170 GenericSAXHandler::characters(const XMLCh* const chars,
171  const XERCES3_SIZE_t length) {
172  myCharactersVector.push_back(TplConvert::_2str(chars, (int)length));
173 }
174 
175 
176 int
177 GenericSAXHandler::convertTag(const std::string& tag) const {
178  TagMap::const_iterator i = myTagMap.find(tag);
179  if (i == myTagMap.end()) {
180  return SUMO_TAG_NOTHING;
181  }
182  return (*i).second;
183 }
184 
185 
186 std::string
187 GenericSAXHandler::buildErrorMessage(const XERCES_CPP_NAMESPACE::SAXParseException& exception) {
188  std::ostringstream buf;
189  char* pMsg = XERCES_CPP_NAMESPACE::XMLString::transcode(exception.getMessage());
190  buf << pMsg << std::endl;
191  buf << " In file '" << getFileName() << "'" << std::endl;
192  buf << " At line/column " << exception.getLineNumber() + 1
193  << '/' << exception.getColumnNumber() << "." << std::endl;
194  XERCES_CPP_NAMESPACE::XMLString::release(&pMsg);
195  return buf.str();
196 }
197 
198 
199 void
200 GenericSAXHandler::warning(const XERCES_CPP_NAMESPACE::SAXParseException& exception) {
201  WRITE_WARNING(buildErrorMessage(exception));
202 }
203 
204 
205 void
206 GenericSAXHandler::error(const XERCES_CPP_NAMESPACE::SAXParseException& exception) {
207  throw ProcessError(buildErrorMessage(exception));
208 }
209 
210 
211 void
212 GenericSAXHandler::fatalError(const XERCES_CPP_NAMESPACE::SAXParseException& exception) {
213  throw ProcessError(buildErrorMessage(exception));
214 }
215 
216 
217 void
219 
220 
221 void
222 GenericSAXHandler::myCharacters(int, const std::string&) {}
223 
224 
225 void
227 
228 
229 /****************************************************************************/
230 
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:81
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:109
void error(const XERCES_CPP_NAMESPACE::SAXParseException &exception)
Handler for XML-errors.
#define WRITE_WARNING(msg)
Definition: MsgHandler.h:199
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:96
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:103
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:56
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.