GenericSAXHandler.cpp

Go to the documentation of this file.
00001 /****************************************************************************/
00007 // A handler which converts occuring elements and attributes into enums
00008 /****************************************************************************/
00009 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
00010 // Copyright 2001-2010 DLR (http://www.dlr.de/) and contributors
00011 /****************************************************************************/
00012 //
00013 //   This program is free software; you can redistribute it and/or modify
00014 //   it under the terms of the GNU General Public License as published by
00015 //   the Free Software Foundation; either version 2 of the License, or
00016 //   (at your option) any later version.
00017 //
00018 /****************************************************************************/
00019 
00020 
00021 // ===========================================================================
00022 // included modules
00023 // ===========================================================================
00024 #ifdef _MSC_VER
00025 #include <windows_config.h>
00026 #else
00027 #include <config.h>
00028 #endif
00029 
00030 #include <cassert>
00031 #include "GenericSAXHandler.h"
00032 #include <utils/common/TplConvert.h>
00033 #include <utils/common/TplConvertSec.h>
00034 #include <utils/common/FileHelpers.h>
00035 #include "SUMOSAXAttributesImpl_Xerces.h"
00036 #include "XMLSubSys.h"
00037 
00038 #ifdef CHECK_MEMORY_LEAKS
00039 #include <foreign/nvwa/debug_new.h>
00040 #endif // CHECK_MEMORY_LEAKS
00041 
00042 
00043 // ===========================================================================
00044 // class definitions
00045 // ===========================================================================
00046 GenericSAXHandler::GenericSAXHandler(GenericSAXHandler::Tag *tags,
00047                                      GenericSAXHandler::Attr *attrs, const std::string &file) throw()
00048         : myParentHandler(0), myParentIndicator(SUMO_TAG_NOTHING), myFileName(file) {
00049     int i = 0;
00050     while (tags[i].key != SUMO_TAG_NOTHING) {
00051         myTagMap.insert(TagMap::value_type(tags[i].name, tags[i].key));
00052         i++;
00053     }
00054     i = 0;
00055     while (attrs[i].key != SUMO_ATTR_NOTHING) {
00056         assert(myPredefinedTags.find(attrs[i].key)==myPredefinedTags.end());
00057         myPredefinedTags[attrs[i].key] = convert(attrs[i].name);
00058         myPredefinedTagsMML[attrs[i].key] = attrs[i].name;
00059         i++;
00060     }
00061 }
00062 
00063 
00064 GenericSAXHandler::~GenericSAXHandler() throw() {
00065     for (AttrMap::iterator i1=myPredefinedTags.begin(); i1!=myPredefinedTags.end(); i1++) {
00066         delete[](*i1).second;
00067     }
00068 }
00069 
00070 
00071 void
00072 GenericSAXHandler::setFileName(const std::string &name) throw() {
00073     myFileName = name;
00074 }
00075 
00076 
00077 const std::string &
00078 GenericSAXHandler::getFileName() const throw() {
00079     return myFileName;
00080 }
00081 
00082 
00083 XMLCh*
00084 GenericSAXHandler::convert(const std::string &name) const throw() {
00085     size_t len = name.length();
00086     XMLCh *ret = new XMLCh[len+1];
00087     size_t i=0;
00088     for (; i<len; i++) {
00089         ret[i] = (XMLCh) name[i];
00090     }
00091     ret[i] = 0;
00092     return ret;
00093 }
00094 
00095 
00096 void
00097 GenericSAXHandler::startElement(const XMLCh* const /*uri*/,
00098                                 const XMLCh* const /*localname*/,
00099                                 const XMLCh* const qname,
00100                                 const Attributes& attrs) {
00101     std::string name = TplConvert<XMLCh>::_2str(qname);
00102     SumoXMLTag element = convertTag(name);
00103     myCharactersVector.clear();
00104     SUMOSAXAttributesImpl_Xerces na(attrs, myPredefinedTags, myPredefinedTagsMML);
00105     if (element == SUMO_TAG_INCLUDE) {
00106         std::string file = na.getString(SUMO_ATTR_HREF);
00107         if (!FileHelpers::isAbsolute(file)) {
00108             file = FileHelpers::getConfigurationRelative(getFileName(), file);
00109         }
00110         XMLSubSys::runParser(*this, file);
00111     } else {
00112         myStartElement(element, na);
00113     }
00114 }
00115 
00116 
00117 void
00118 GenericSAXHandler::endElement(const XMLCh* const /*uri*/,
00119                               const XMLCh* const /*localname*/,
00120                               const XMLCh* const qname) {
00121     std::string name = TplConvert<XMLCh>::_2str(qname);
00122     SumoXMLTag element = convertTag(name);
00123     // collect characters
00124     if (myCharactersVector.size()!=0) {
00125         size_t len = 0;
00126         unsigned i;
00127         for (i=0; i<myCharactersVector.size(); ++i) {
00128             len += myCharactersVector[i].length();
00129         }
00130         char *buf = new char[len+1];
00131         size_t pos = 0;
00132         for (i=0; i<myCharactersVector.size(); ++i) {
00133             memcpy((unsigned char*) buf+pos, (unsigned char*) myCharactersVector[i].c_str(),
00134                    sizeof(char)*myCharactersVector[i].length());
00135             pos += myCharactersVector[i].length();
00136         }
00137         buf[pos] = 0;
00138 
00139         // call user handler
00140         try {
00141             myCharacters(element, buf);
00142         } catch (std::runtime_error &) {
00143             delete[] buf;
00144             throw;
00145         }
00146         delete[] buf;
00147     }
00148     if (element != SUMO_TAG_INCLUDE) {
00149         myEndElement(element);
00150         if (myParentHandler && myParentIndicator == element) {
00151             XMLSubSys::setHandler(*myParentHandler);
00152             myParentIndicator = SUMO_TAG_NOTHING;
00153             myParentHandler = 0;
00154         }
00155     }
00156 }
00157 
00158 
00159 void
00160 GenericSAXHandler::registerParent(const SumoXMLTag tag, GenericSAXHandler* handler) {
00161     myParentHandler = handler;
00162     myParentIndicator = tag;
00163     XMLSubSys::setHandler(*this);
00164 }
00165 
00166 
00167 void
00168 GenericSAXHandler::characters(const XMLCh* const chars,
00169                               const XERCES3_SIZE_t length) {
00170     myCharactersVector.push_back(TplConvert<XMLCh>::_2str(chars, length));
00171 }
00172 
00173 
00174 SumoXMLTag
00175 GenericSAXHandler::convertTag(const std::string &tag) const throw() {
00176     TagMap::const_iterator i=myTagMap.find(tag);
00177     if (i==myTagMap.end()) {
00178         return SUMO_TAG_NOTHING;
00179     }
00180     return (*i).second;
00181 }
00182 
00183 
00184 void
00185 GenericSAXHandler::myStartElement(SumoXMLTag, const SUMOSAXAttributes &) throw(ProcessError) {}
00186 
00187 
00188 void
00189 GenericSAXHandler::myCharacters(SumoXMLTag, const std::string &) throw(ProcessError) {}
00190 
00191 
00192 void
00193 GenericSAXHandler::myEndElement(SumoXMLTag) throw(ProcessError) {}
00194 
00195 
00196 /****************************************************************************/
00197 

Generated on Wed May 5 00:06:29 2010 for Sumo - Simulation of Urban MObility by  doxygen 1.5.6