1 ///////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2000-2014 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
7 ///////////////////////////////////////////////////////////////////////////////
11 #include "GeneralTypes.hh"
15 #include <libxml/parser.h>
16 #include <libxml/parserInternals.h>
17 #include <libxml/xmlschemas.h>
18 #include <libxml/xmlschemastypes.h>
26 * List of possible names of XSD tags
44 n_field, // Not supported by now
48 n_key, // Not supported by now
49 n_keyref, // Not supported by now
51 n_notation, // Not supported by now
55 n_selector, // Not supported by now
60 n_unique, // Not supported by now
62 // XSD Restrictions / Facets for Datatypes:
64 n_fractionDigits, // Not supported by now
76 // Others - non-standard, but used:
84 * List of possible names of XSD tag attributes
88 a_abstract, // Not supported by now
89 a_attributeFormDefault,
91 a_block, // Not supported by now
92 a_blockDefault, // Not supported by now
95 a_final, // Not supported by now
96 a_finalDefault, // Not supported by now
101 a_lang, // Not supported by now
109 a_processContents, // Not supported by now
113 a_substitutionGroup, // Not supported by now
118 a_version, // Not supported by now
119 a_xpath, // Not supported by now
126 TagAttributes (const TagAttributes &); // not implemented
127 TagAttributes & operator = (const TagAttributes &); // not implemented
129 XMLParser * parser; // not responsibility for the member
132 * Members for storing actual values of attributes of an XML tag
134 FormValue attributeFormDefault;
137 FormValue elementFormDefault;
142 unsigned long long int maxOccurs;
144 unsigned long long int minOccurs;
150 Mstring schemaLocation;
152 Mstring targetNamespace;
157 TagAttributes (XMLParser * withThisParser);
158 // Default destructor is used
161 * Clear and fill up object with values of attributes of current XML tag
163 void fillUp (TagAttributeName * att_name_e, Mstring * att_value_s, int att_count);
175 * Related TTCN-3 module
176 * Information from parsed XML schema is loaded into this module
178 * One-to-one relation between XMLParser object and TTCN3Module object
180 TTCN3Module * module; // no responsibility for this member
184 * Each schema file has a unique XMLParser object to parse it
189 * Pointers that are used by LibXML SAX parser
191 xmlSAXHandlerPtr parser;
192 xmlParserCtxtPtr context;
194 xmlSAXHandlerPtr parserCheckingXML;
195 xmlParserCtxtPtr contextCheckingXML;
196 xmlSchemaParserCtxtPtr contextCheckingXSD;
199 * Depth of the last read XML tag
204 * Name of the last read XML tag
206 TagName actualTagName;
209 * Attributes and their values of the last read XML tag
210 * Stored in a special object designed for this purpose
212 TagAttributes actualTagAttributes;
215 * Stack for storing the XML tag hierarchy but only for the last read XML tag
217 List<TagName> parentTagNames;
219 static bool suspended;
222 * Used to register errors during the parsing phase
223 * After this it is possible to check it
224 * and if errors occur possible to stop converter
226 static unsigned int num_errors;
227 static unsigned int num_warnings;
230 * Callback functions for LibXML SAX parser
232 void startelementHandler (const xmlChar * localname, int nb_namespaces, const xmlChar ** namespaces, int nb_attributes, const xmlChar ** attributes);
233 void endelementHandler (const xmlChar * localname);
234 void characterdataHandler (const xmlChar * text, int length);
235 void commentHandler (const xmlChar * text);
237 /** Callbacks cannot be member functions, use these static members as wrappers */
238 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);
239 static void wrapper_to_call_endelement_h (XMLParser *self, const xmlChar * localname, const xmlChar * prefix, const xmlChar * URI);
240 static void wrapper_to_call_characterdata_h (XMLParser *self, const xmlChar * ch, int len);
241 static void wrapper_to_call_comment_h (XMLParser *self, const xmlChar * value);
243 static void warningHandler (void * ctx, const char * msg, ...);
244 static void errorHandler (void * ctx, const char * msg, ...);
247 * Converts name of read tag to enumerated value
248 * and load it to actualTagName member
250 * mode argument indicates that it is called when
251 * startelement or endelement arrived
253 void fillUpActualTagName (const char * localname, tagMode mode);
256 * Converts name and value of attributes of read tag
257 * and fill actualTagAttributes object with current values
259 void fillUpActualTagAttributes (const char ** attributes, int att_count);
261 XMLParser (const XMLParser &); // not implemented
262 XMLParser & operator = (const XMLParser &); // not implemented
264 XMLParser (const char * a_filename);
268 * After an XMLParser object is born
269 * there is need to connect it with a TTCN3Module object
270 * for loading the read data into it
272 void connectWithModule (TTCN3Module * a_module);
275 * Start syntax checking, validation and parse
279 void startConversion (TTCN3Module * a_module);
281 static unsigned int getNumErrors () {return num_errors;}
282 static unsigned int getNumWarnings () {return num_warnings;}
283 static void incrNumErrors () {++num_errors;}
284 static void incrNumWarnings () {++num_warnings;}
286 const Mstring & getFilename () const {return filename;}
287 int getActualLineNumber () const {return xmlSAX2GetLineNumber(context);}
288 int getActualDepth () const {return actualDepth;}
289 TagName getActualTagName () const {return actualTagName;}
290 TagName getParentTagName () {return parentTagNames.empty() ? n_NOTSET : parentTagNames.back();}
291 const TagAttributes & getActualTagAttributes () const {return actualTagAttributes;}
294 #endif /* PARSER_HH_ */