/****************************************************************************/1// Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo2// Copyright (C) 2012-2025 German Aerospace Center (DLR) and others.3// This program and the accompanying materials are made available under the4// terms of the Eclipse Public License 2.0 which is available at5// https://www.eclipse.org/legal/epl-2.0/6// This Source Code may also be made available under the following Secondary7// Licenses when the conditions for such availability set forth in the Eclipse8// Public License 2.0 are satisfied: GNU General Public License, version 29// or later which is available at10// https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html11// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later12/****************************************************************************/13/// @file SUMOSAXReader.h14/// @author Daniel Krajzewicz15/// @author Jakob Erdmann16/// @author Michael Behrisch17/// @date Nov 201218///19// SAX-reader encapsulation containing binary reader20/****************************************************************************/21#pragma once22#include <config.h>2324#include <string>25#include <memory>26#include <vector>27#include <xercesc/sax2/SAX2XMLReader.hpp>28#include <xercesc/sax/EntityResolver.hpp>29#include <xercesc/sax/InputSource.hpp>30#include "SUMOXMLDefinitions.h"313233// ===========================================================================34// class declarations35// ===========================================================================3637class GenericSAXHandler;38class IStreamInputSource;39class SUMOSAXAttributes;4041// ===========================================================================42// class definitions43// ===========================================================================44/**45* @class SUMOSAXReader46* @brief SAX-reader encapsulation containing binary reader47*48* This class generates on demand either a SAX2XMLReader or parses the SUMO49* binary xml. The interface is inspired by but not identical to50* SAX2XMLReader.51*/52class SUMOSAXReader {5354public:55/**56* @brief Constructor57*58* @param[in] file The name of the processed file59*/60SUMOSAXReader(GenericSAXHandler& handler, const std::string& validationScheme, XERCES_CPP_NAMESPACE::XMLGrammarPool* grammarPool);6162/// Destructor63~SUMOSAXReader();6465/**66* @brief Sets the given handler as content and error handler for the reader67*68* @param[in] handler The handler to assign to the reader69*/70void setHandler(GenericSAXHandler& handler);7172/**73* @brief Sets a new validation scheme and applies the validation settings to the XML reader74*75* If no new scheme is given, the settings of the current scheme are applied.76*77* @param[in] validationScheme The validation scheme (one of "never", "local", "auto", or "always")78*/79void setValidation(std::string validationScheme = "");8081/**82* @brief Parse the given file completely by calling parse of myXMLReader83*84* This throws a ProcessError if the file is not readable and can handle gzipped XML as well.85*86* @param[in] systemID file name87*/88void parse(std::string systemID);8990/**91* @brief Parse XML from the given string92*93* @param[in] content XML string94*/95void parseString(std::string content);9697/**98* @brief Start parsing the given file using parseFirst of myXMLReader99*100* @param[in] systemID file name101* @return whether the prolog could be parsed successfully102*/103bool parseFirst(std::string systemID);104105/**106* @brief Continue a progressive parse started by parseFirst107*108* @return whether the next token could be parsed successfully109*/110bool parseNext();111112/**113* @brief Continue a progressive parse started by parseFirst until the given element is encountered114*115* The parse will continue until the section encapsulated by the element is completed116*117* @return whether the next section could be parsed successfully118*/119bool parseSection(SumoXMLTag element);120121private:122/// @brief Local Schema Resolver123class LocalSchemaResolver : public XERCES_CPP_NAMESPACE::EntityResolver {124125public:126/// @brief constructor127LocalSchemaResolver(const bool haveFallback, const bool noOp);128129/// @brief resolve entity130XERCES_CPP_NAMESPACE::InputSource* resolveEntity(const XMLCh* const publicId, const XMLCh* const systemId);131132private:133/// @brief flag for check if we have fallback134const bool myHaveFallback;135136/// @brief flag for check if there is an operation137const bool myNoOp;138};139140/**141* @brief Builds a reader, if needed142*143* Tries to build a SAX2XMLReader using XMLReaderFactory::createXMLReader,144* if no reader has been created yet. If this145* fails, a ProcessError is thrown. Otherwise the validation is set matching the value of146* "myValidationScheme". If validation is not wanted, a WFXMLScanner is used147* (see http://www.ibm.com/developerworks/library/x-xercesperf.html).148*/149void ensureSAXReader();150151/// @brief generic SAX Handler152GenericSAXHandler* myHandler;153154/// @brief Information whether built reader/parser shall validate XML-documents against schemata155std::string myValidationScheme;156157/// @brief Schema cache to be used for grammars which are not declared158XERCES_CPP_NAMESPACE::XMLGrammarPool* myGrammarPool;159160/// @brief token161XERCES_CPP_NAMESPACE::XMLPScanToken myToken;162163/// @brief XML reader164XERCES_CPP_NAMESPACE::SAX2XMLReader* myXMLReader;165166/// @brief istream167std::unique_ptr<std::istream> myIStream;168169/// @brief input stream170std::unique_ptr<IStreamInputSource> myInputStream;171172/// @brief The stack of begun xml elements173std::vector<SumoXMLTag> myXMLStack;174175/// @brief schema resolver176LocalSchemaResolver mySchemaResolver;177178/// @brief local resolver179LocalSchemaResolver myLocalResolver;180181/// @brief no operation resolver182LocalSchemaResolver myNoOpResolver;183184/// @brief next section185std::pair<int, SUMOSAXAttributes*> myNextSection;186187/// @brief invalidated copy constructor188SUMOSAXReader(const SUMOSAXReader& s) = delete;189190/// @brief invalidated assignment operator191const SUMOSAXReader& operator=(const SUMOSAXReader& s) = delete;192};193194195