1 ///////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2000-2015 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>
25 * List of possible names of XSD tags
29 * List of possible names of XSD tag attributes
31 enum TagAttributeName {
32 a_abstract, // Not supported by now
33 a_attributeFormDefault,
35 a_block, // Not supported by now
36 a_blockDefault, // Not supported by now
39 a_final, // Not supported by now
40 a_finalDefault, // Not supported by now
45 a_lang, // Not supported by now
53 a_processContents, // Not supported by now
57 a_substitutionGroup, // Not supported by now
62 a_version, // Not supported by now
63 a_xpath, // Not supported by now
69 TagAttributes(const TagAttributes &); // not implemented
70 TagAttributes & operator=(const TagAttributes &); // not implemented
72 XMLParser * parser; // not responsibility for the member
75 * Members for storing actual values of attributes of an XML tag
78 FormValue attributeFormDefault;
81 BlockValue blockDefault;
83 FormValue elementFormDefault;
88 unsigned long long int maxOccurs;
90 unsigned long long int minOccurs;
96 Mstring schemaLocation;
98 Mstring substitionGroup;
99 Mstring targetNamespace;
104 TagAttributes(XMLParser * withThisParser);
105 // Default destructor is used
108 * Clear and fill up object with values of attributes of current XML tag
110 void fillUp(TagAttributeName * att_name_e, Mstring * att_value_s, const int att_count);
121 * Related TTCN-3 module
122 * Information from parsed XML schema is loaded into this module
124 * One-to-one relation between XMLParser object and TTCN3Module object
126 TTCN3Module * module; // no responsibility for this member
130 * Each schema file has a unique XMLParser object to parse it
135 * Pointers that are used by LibXML SAX parser
137 xmlSAXHandlerPtr parser;
138 xmlParserCtxtPtr context;
140 xmlSAXHandlerPtr parserCheckingXML;
141 xmlParserCtxtPtr contextCheckingXML;
142 xmlSchemaParserCtxtPtr contextCheckingXSD;
145 * Depth of the last read XML tag
150 * Name of the last read XML tag
152 TagName actualTagName;
155 * Attributes and their values of the last read XML tag
156 * Stored in a special object designed for this purpose
158 TagAttributes actualTagAttributes;
161 * Stack for storing the XML tag hierarchy but only for the last read XML tag
163 List<TagName> parentTagNames;
165 // Stack for keeping track if we are inside an annotation tag
166 List<TagName> inside_annotation;
168 static bool suspended;
171 * Used to register errors during the parsing phase
172 * After this it is possible to check it
173 * and if errors occur possible to stop converter
175 static unsigned int num_errors;
176 static unsigned int num_warnings;
179 * Callback functions for LibXML SAX parser
181 void startelementHandler(const xmlChar * localname, const int nb_namespaces, const xmlChar ** namespaces, int nb_attributes, const xmlChar ** attributes);
182 void endelementHandler(const xmlChar * localname);
183 void characterdataHandler(const xmlChar * text, const int length);
184 void commentHandler(const xmlChar * text);
186 /** Callbacks cannot be member functions, use these static members as wrappers */
187 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);
188 static void wrapper_to_call_endelement_h(XMLParser *self, const xmlChar * localname, const xmlChar * prefix, const xmlChar * URI);
189 static void wrapper_to_call_characterdata_h(XMLParser *self, const xmlChar * ch, int len);
190 static void wrapper_to_call_comment_h(XMLParser *self, const xmlChar * value);
192 static void warningHandler(void * ctx, const char * msg, ...);
193 static void errorHandler(void * ctx, const char * msg, ...);
196 * Converts name of read tag to enumerated value
197 * and load it to actualTagName member
199 * mode argument indicates that it is called when
200 * startelement or endelement arrived
202 void fillUpActualTagName(const char * localname, const tagMode mode);
205 * Converts name and value of attributes of read tag
206 * and fill actualTagAttributes object with current values
208 void fillUpActualTagAttributes(const char ** attributes, const int att_count);
210 XMLParser(const XMLParser &); // not implemented
211 XMLParser & operator=(const XMLParser &); // not implemented
213 XMLParser(const char * a_filename);
217 * After an XMLParser object is born
218 * there is need to connect it with a TTCN3Module object
219 * for loading the read data into it
221 void connectWithModule(TTCN3Module * a_module);
224 * Start syntax checking, validation and parse
228 void startConversion(TTCN3Module * a_module);
230 static unsigned int getNumErrors() {
234 static unsigned int getNumWarnings() {
238 static void incrNumErrors() {
242 static void incrNumWarnings() {
246 const Mstring & getFilename() const {
250 int getActualLineNumber() const {
251 return xmlSAX2GetLineNumber(context);
254 int getActualDepth() const {
258 TagName getActualTagName() const {
259 return actualTagName;
262 TagName getParentTagName() const {
263 return parentTagNames.empty() ? n_NOTSET : parentTagNames.back();
266 const TagAttributes & getActualTagAttributes() const {
267 return actualTagAttributes;
271 #endif /* PARSER_HH_ */