Line data Source code
1 : /****************************************************************************/
2 : // Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
3 : // Copyright (C) 2012-2024 German Aerospace Center (DLR) and others.
4 : // This program and the accompanying materials are made available under the
5 : // terms of the Eclipse Public License 2.0 which is available at
6 : // https://www.eclipse.org/legal/epl-2.0/
7 : // This Source Code may also be made available under the following Secondary
8 : // Licenses when the conditions for such availability set forth in the Eclipse
9 : // Public License 2.0 are satisfied: GNU General Public License, version 2
10 : // or later which is available at
11 : // https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
12 : // SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
13 : /****************************************************************************/
14 : /// @file SUMOSAXReader.h
15 : /// @author Daniel Krajzewicz
16 : /// @author Jakob Erdmann
17 : /// @author Michael Behrisch
18 : /// @date Nov 2012
19 : ///
20 : // SAX-reader encapsulation containing binary reader
21 : /****************************************************************************/
22 : #pragma once
23 : #include <config.h>
24 :
25 : #include <string>
26 : #include <memory>
27 : #include <vector>
28 : #include <xercesc/sax2/SAX2XMLReader.hpp>
29 : #include <xercesc/sax/EntityResolver.hpp>
30 : #include <xercesc/sax/InputSource.hpp>
31 : #include "SUMOXMLDefinitions.h"
32 :
33 :
34 : // ===========================================================================
35 : // class declarations
36 : // ===========================================================================
37 :
38 : class GenericSAXHandler;
39 : class IStreamInputSource;
40 : class SUMOSAXAttributes;
41 :
42 : // ===========================================================================
43 : // class definitions
44 : // ===========================================================================
45 : /**
46 : * @class SUMOSAXReader
47 : * @brief SAX-reader encapsulation containing binary reader
48 : *
49 : * This class generates on demand either a SAX2XMLReader or parses the SUMO
50 : * binary xml. The interface is inspired by but not identical to
51 : * SAX2XMLReader.
52 : */
53 : class SUMOSAXReader {
54 :
55 : public:
56 : /**
57 : * @brief Constructor
58 : *
59 : * @param[in] file The name of the processed file
60 : */
61 : SUMOSAXReader(GenericSAXHandler& handler, const std::string& validationScheme, XERCES_CPP_NAMESPACE::XMLGrammarPool* grammarPool);
62 :
63 : /// Destructor
64 : ~SUMOSAXReader();
65 :
66 : /**
67 : * @brief Sets the given handler as content and error handler for the reader
68 : *
69 : * @param[in] handler The handler to assign to the reader
70 : */
71 : void setHandler(GenericSAXHandler& handler);
72 :
73 : /**
74 : * @brief Sets a new validation scheme and applies the validation settings to the XML reader
75 : *
76 : * If no new scheme is given, the settings of the current scheme are applied.
77 : *
78 : * @param[in] validationScheme The validation scheme (one of "never", "local", "auto", or "always")
79 : */
80 : void setValidation(std::string validationScheme = "");
81 :
82 : /**
83 : * @brief Parse the given file completely by calling parse of myXMLReader
84 : *
85 : * This throws a ProcessError if the file is not readable and can handle gzipped XML as well.
86 : *
87 : * @param[in] systemID file name
88 : */
89 : void parse(std::string systemID);
90 :
91 : /**
92 : * @brief Parse XML from the given string
93 : *
94 : * @param[in] content XML string
95 : */
96 : void parseString(std::string content);
97 :
98 : /**
99 : * @brief Start parsing the given file using parseFirst of myXMLReader
100 : *
101 : * @param[in] systemID file name
102 : * @return whether the prolog could be parsed successfully
103 : */
104 : bool parseFirst(std::string systemID);
105 :
106 : /**
107 : * @brief Continue a progressive parse started by parseFirst
108 : *
109 : * @return whether the next token could be parsed successfully
110 : */
111 : bool parseNext();
112 :
113 : /**
114 : * @brief Continue a progressive parse started by parseFirst until the given element is encountered
115 : *
116 : * The parse will continue until the section encapsulated by the element is completed
117 : *
118 : * @return whether the next section could be parsed successfully
119 : */
120 : bool parseSection(SumoXMLTag element);
121 :
122 : private:
123 : /// @brief Local Schema Resolver
124 97178 : class LocalSchemaResolver : public XERCES_CPP_NAMESPACE::EntityResolver {
125 :
126 : public:
127 : /// @brief constructor
128 : LocalSchemaResolver(const bool haveFallback, const bool noOp);
129 :
130 : /// @brief resolve entity
131 : XERCES_CPP_NAMESPACE::InputSource* resolveEntity(const XMLCh* const publicId, const XMLCh* const systemId);
132 :
133 : private:
134 : /// @brief flag for check if we have fallback
135 : const bool myHaveFallback;
136 :
137 : /// @brief flag for check if there is an operation
138 : const bool myNoOp;
139 : };
140 :
141 : /**
142 : * @brief Builds a reader, if needed
143 : *
144 : * Tries to build a SAX2XMLReader using XMLReaderFactory::createXMLReader,
145 : * if no reader has been created yet. If this
146 : * fails, a ProcessError is thrown. Otherwise the validation is set matching the value of
147 : * "myValidationScheme". If validation is not wanted, a WFXMLScanner is used
148 : * (see http://www.ibm.com/developerworks/library/x-xercesperf.html).
149 : */
150 : void ensureSAXReader();
151 :
152 : /// @brief generic SAX Handler
153 : GenericSAXHandler* myHandler;
154 :
155 : /// @brief Information whether built reader/parser shall validate XML-documents against schemata
156 : std::string myValidationScheme;
157 :
158 : /// @brief Schema cache to be used for grammars which are not declared
159 : XERCES_CPP_NAMESPACE::XMLGrammarPool* myGrammarPool;
160 :
161 : /// @brief token
162 : XERCES_CPP_NAMESPACE::XMLPScanToken myToken;
163 :
164 : /// @brief XML reader
165 : XERCES_CPP_NAMESPACE::SAX2XMLReader* myXMLReader;
166 :
167 : /// @brief istream
168 : std::unique_ptr<std::istream> myIStream;
169 :
170 : /// @brief input stream
171 : std::unique_ptr<IStreamInputSource> myInputStream;
172 :
173 : /// @brief The stack of begun xml elements
174 : std::vector<SumoXMLTag> myXMLStack;
175 :
176 : /// @brief schema resolver
177 : LocalSchemaResolver mySchemaResolver;
178 :
179 : /// @brief local resolver
180 : LocalSchemaResolver myLocalResolver;
181 :
182 : /// @brief no operation resolver
183 : LocalSchemaResolver myNoOpResolver;
184 :
185 : /// @brief next section
186 : std::pair<int, SUMOSAXAttributes*> myNextSection;
187 :
188 : /// @brief invalidated copy constructor
189 : SUMOSAXReader(const SUMOSAXReader& s) = delete;
190 :
191 : /// @brief invalidated assignment operator
192 : const SUMOSAXReader& operator=(const SUMOSAXReader& s) = delete;
193 : };
|