SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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.sourceforge.net/
13 // Copyright (C) 2001-2012 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  size_t len = name.length();
94  XMLCh* ret = new XMLCh[len + 1];
95  size_t 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  size_t len = 0;
134  unsigned i;
135  for (i = 0; i < myCharactersVector.size(); ++i) {
136  len += myCharactersVector[i].length();
137  }
138  char* buf = new char[len + 1];
139  size_t pos = 0;
140  for (i = 0; i < myCharactersVector.size(); ++i) {
141  memcpy((unsigned char*) buf + pos, (unsigned char*) myCharactersVector[i].c_str(),
142  sizeof(char)*myCharactersVector[i].length());
143  pos += myCharactersVector[i].length();
144  }
145  buf[pos] = 0;
146 
147  // call user handler
148  try {
149  myCharacters(element, buf);
150  } catch (std::runtime_error&) {
151  delete[] buf;
152  throw;
153  }
154  delete[] buf;
155  }
156  if (element != SUMO_TAG_INCLUDE) {
157  myEndElement(element);
158  if (myParentHandler && myParentIndicator == element) {
161  myParentHandler = 0;
162  }
163  }
164 }
165 
166 
167 void
169  myParentHandler = handler;
170  myParentIndicator = tag;
171  XMLSubSys::setHandler(*this);
172 }
173 
174 
175 void
176 GenericSAXHandler::characters(const XMLCh* const chars,
177  const XERCES3_SIZE_t length) {
178  myCharactersVector.push_back(TplConvert::_2str(chars, static_cast<unsigned int>(length)));
179 }
180 
181 
182 int
183 GenericSAXHandler::convertTag(const std::string& tag) const {
184  TagMap::const_iterator i = myTagMap.find(tag);
185  if (i == myTagMap.end()) {
186  return SUMO_TAG_NOTHING;
187  }
188  return (*i).second;
189 }
190 
191 
192 std::string
193 GenericSAXHandler::buildErrorMessage(const XERCES_CPP_NAMESPACE::SAXParseException& exception) {
194  std::ostringstream buf;
195  char* pMsg = XERCES_CPP_NAMESPACE::XMLString::transcode(exception.getMessage());
196  buf << pMsg << std::endl;
197  buf << " In file '" << getFileName() << "'" << std::endl;
198  buf << " At line/column " << exception.getLineNumber() + 1
199  << '/' << exception.getColumnNumber() << "." << std::endl;
200  XERCES_CPP_NAMESPACE::XMLString::release(&pMsg);
201  return buf.str();
202 }
203 
204 
205 void
206 GenericSAXHandler::warning(const XERCES_CPP_NAMESPACE::SAXParseException& exception) {
207  WRITE_WARNING(buildErrorMessage(exception));
208 }
209 
210 
211 void
212 GenericSAXHandler::error(const XERCES_CPP_NAMESPACE::SAXParseException& exception) {
213  throw ProcessError(buildErrorMessage(exception));
214 }
215 
216 
217 void
218 GenericSAXHandler::fatalError(const XERCES_CPP_NAMESPACE::SAXParseException& exception) {
219  throw ProcessError(buildErrorMessage(exception));
220 }
221 
222 
223 void
225 
226 
227 void
228 GenericSAXHandler::myCharacters(int, const std::string&) {}
229 
230 
231 void
233 
234 
235 /****************************************************************************/
236