1 /******************************************************************************
2 * Copyright (c) 2000-2016 Ericsson Telecom AB
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
15 ******************************************************************************/
19 #include "GeneralTypes.hh"
23 #include <libxml/parser.h>
24 #include <libxml/parserInternals.h>
25 #include <libxml/xmlschemas.h>
26 #include <libxml/xmlschemastypes.h>
33 * List of possible names of XSD tags
37 * List of possible names of XSD tag attributes
39 enum TagAttributeName {
40 a_abstract, // Not supported by now
41 a_attributeFormDefault,
43 a_block, // Not supported by now
44 a_blockDefault, // Not supported by now
47 a_final, // Not supported by now
48 a_finalDefault, // Not supported by now
53 a_lang, // Not supported by now
61 a_processContents, // Not supported by now
65 a_substitutionGroup, // Not supported by now
70 a_version, // Not supported by now
71 a_xpath, // Not supported by now
77 TagAttributes(const TagAttributes &); // not implemented
78 TagAttributes & operator=(const TagAttributes &); // not implemented
80 XMLParser * parser; // not responsibility for the member
83 * Members for storing actual values of attributes of an XML tag
86 FormValue attributeFormDefault;
89 BlockValue blockDefault;
91 FormValue elementFormDefault;
96 unsigned long long int maxOccurs;
98 unsigned long long int minOccurs;
104 Mstring schemaLocation;
106 Mstring substitionGroup;
107 Mstring targetNamespace;
112 TagAttributes(XMLParser * withThisParser);
113 // Default destructor is used
116 * Clear and fill up object with values of attributes of current XML tag
118 void fillUp(TagAttributeName * att_name_e, Mstring * att_value_s, const int att_count);
129 * Related TTCN-3 module
130 * Information from parsed XML schema is loaded into this module
132 * One-to-one relation between XMLParser object and TTCN3Module object
134 TTCN3Module * module; // no responsibility for this member
138 * Each schema file has a unique XMLParser object to parse it
143 * Pointers that are used by LibXML SAX parser
145 xmlSAXHandlerPtr parser;
146 xmlParserCtxtPtr context;
148 xmlSAXHandlerPtr parserCheckingXML;
149 xmlParserCtxtPtr contextCheckingXML;
150 xmlSchemaParserCtxtPtr contextCheckingXSD;
153 * Depth of the last read XML tag
158 * Name of the last read XML tag
160 TagName actualTagName;
163 * Attributes and their values of the last read XML tag
164 * Stored in a special object designed for this purpose
166 TagAttributes actualTagAttributes;
169 * Stack for storing the XML tag hierarchy but only for the last read XML tag
171 List<TagName> parentTagNames;
173 // Stack for keeping track if we are inside an annotation tag
174 List<TagName> inside_annotation;
176 static bool suspended;
179 * Used to register errors during the parsing phase
180 * After this it is possible to check it
181 * and if errors occur possible to stop converter
183 static unsigned int num_errors;
184 static unsigned int num_warnings;
187 * Callback functions for LibXML SAX parser
189 void startelementHandler(const xmlChar * localname, const int nb_namespaces, const xmlChar ** namespaces, int nb_attributes, const xmlChar ** attributes);
190 void endelementHandler(const xmlChar * localname);
191 void characterdataHandler(const xmlChar * text, const int length);
192 void commentHandler(const xmlChar * text);
194 /** Callbacks cannot be member functions, use these static members as wrappers */
195 static void wrapper_to_call_startelement_h(XMLParser *self, const xmlChar * localname, const xmlChar * prefix, const xmlChar * URI, int nb_namespaces, const xmlChar ** namespaces, int nb_attributes, int nb_defaulted, const xmlChar ** attributes);
196 static void wrapper_to_call_endelement_h(XMLParser *self, const xmlChar * localname, const xmlChar * prefix, const xmlChar * URI);
197 static void wrapper_to_call_characterdata_h(XMLParser *self, const xmlChar * ch, int len);
198 static void wrapper_to_call_comment_h(XMLParser *self, const xmlChar * value);
200 static void warningHandler(void * ctx, const char * msg, ...);
201 static void errorHandler(void * ctx, const char * msg, ...);
204 * Converts name of read tag to enumerated value
205 * and load it to actualTagName member
207 * mode argument indicates that it is called when
208 * startelement or endelement arrived
210 void fillUpActualTagName(const char * localname, const tagMode mode);
213 * Converts name and value of attributes of read tag
214 * and fill actualTagAttributes object with current values
216 void fillUpActualTagAttributes(const char ** attributes, const int att_count);
218 XMLParser(const XMLParser &); // not implemented
219 XMLParser & operator=(const XMLParser &); // not implemented
221 XMLParser(const char * a_filename);
225 * After an XMLParser object is born
226 * there is need to connect it with a TTCN3Module object
227 * for loading the read data into it
229 void connectWithModule(TTCN3Module * a_module);
232 * Start syntax checking, validation and parse
236 void startConversion(TTCN3Module * a_module);
238 static unsigned int getNumErrors() {
242 static unsigned int getNumWarnings() {
246 static void incrNumErrors() {
250 static void incrNumWarnings() {
254 const Mstring & getFilename() const {
258 int getActualLineNumber() const {
259 return xmlSAX2GetLineNumber(context);
262 int getActualDepth() const {
266 TagName getActualTagName() const {
267 return actualTagName;
270 TagName getParentTagName() const {
271 return parentTagNames.empty() ? n_NOTSET : parentTagNames.back();
274 const TagAttributes & getActualTagAttributes() const {
275 return actualTagAttributes;
279 #endif /* PARSER_HH_ */