Merge pull request #77 from balaskoa/master
[deliverable/titan.core.git] / xsdconvert / XMLParser.hh
CommitLineData
d44e3c4f 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
7 *
8 * Contributors:
9 * Balasko, Jeno
10 * Godar, Marton
11 * Raduly, Csaba
12 * Szabados, Kristof
13 * Szabo, Bence Janos
14 *
15 ******************************************************************************/
970ed795
EL
16#ifndef PARSER_HH_
17#define PARSER_HH_
18
19#include "GeneralTypes.hh"
20#include "Mstring.hh"
21#include "List.hh"
22
23#include <libxml/parser.h>
24#include <libxml/parserInternals.h>
25#include <libxml/xmlschemas.h>
26#include <libxml/xmlschemastypes.h>
27
28class TTCN3Module;
29
3abe9331 30class XMLParser {
970ed795
EL
31public:
32 /**
33 * List of possible names of XSD tags
34 */
970ed795
EL
35
36 /**
37 * List of possible names of XSD tag attributes
38 */
3abe9331 39 enum TagAttributeName {
970ed795
EL
40 a_abstract, // Not supported by now
41 a_attributeFormDefault,
42 a_base,
43 a_block, // Not supported by now
44 a_blockDefault, // Not supported by now
45 a_default,
46 a_elementFormDefault,
47 a_final, // Not supported by now
48 a_finalDefault, // Not supported by now
49 a_fixed,
50 a_form,
51 a_id,
52 a_itemType,
53 a_lang, // Not supported by now
54 a_maxOccurs,
55 a_memberTypes,
56 a_minOccurs,
57 a_mixed,
58 a_name,
59 a_namespace,
60 a_nillable,
61 a_processContents, // Not supported by now
62 a_ref,
63 a_schemaLocation,
64 a_source, // ???
65 a_substitutionGroup, // Not supported by now
66 a_targetNamespace,
67 a_type,
68 a_use,
69 a_value,
70 a_version, // Not supported by now
71 a_xpath, // Not supported by now
72
73 a_NOTSET
74 };
75
3abe9331 76 class TagAttributes {
77 TagAttributes(const TagAttributes &); // not implemented
78 TagAttributes & operator=(const TagAttributes &); // not implemented
970ed795
EL
79 public:
80 XMLParser * parser; // not responsibility for the member
81
82 /**
83 * Members for storing actual values of attributes of an XML tag
84 */
3abe9331 85 bool abstract;
970ed795
EL
86 FormValue attributeFormDefault;
87 Mstring base;
3abe9331 88 BlockValue block;
89 BlockValue blockDefault;
970ed795
EL
90 Mstring default_;
91 FormValue elementFormDefault;
92 Mstring fixed;
93 FormValue form;
94 Mstring id;
95 Mstring itemType;
96 unsigned long long int maxOccurs;
97 Mstring memberTypes;
98 unsigned long long int minOccurs;
99 bool mixed;
100 Mstring name;
101 Mstring namespace_;
102 bool nillable;
103 Mstring ref;
104 Mstring schemaLocation;
105 Mstring source;
3abe9331 106 Mstring substitionGroup;
970ed795
EL
107 Mstring targetNamespace;
108 Mstring type;
109 UseValue use;
110 Mstring value;
111
3abe9331 112 TagAttributes(XMLParser * withThisParser);
970ed795
EL
113 // Default destructor is used
114
115 /**
116 * Clear and fill up object with values of attributes of current XML tag
117 */
3abe9331 118 void fillUp(TagAttributeName * att_name_e, Mstring * att_value_s, const int att_count);
970ed795
EL
119 };
120
121private:
122
3abe9331 123 enum tagMode {
970ed795
EL
124 startElement,
125 endElement
126 };
127
128 /**
129 * Related TTCN-3 module
130 * Information from parsed XML schema is loaded into this module
131 *
132 * One-to-one relation between XMLParser object and TTCN3Module object
133 */
134 TTCN3Module * module; // no responsibility for this member
135
136 /**
137 * Name of XML schema
138 * Each schema file has a unique XMLParser object to parse it
139 */
140 Mstring filename;
141
142 /**
143 * Pointers that are used by LibXML SAX parser
144 */
145 xmlSAXHandlerPtr parser;
146 xmlParserCtxtPtr context;
147
148 xmlSAXHandlerPtr parserCheckingXML;
149 xmlParserCtxtPtr contextCheckingXML;
150 xmlSchemaParserCtxtPtr contextCheckingXSD;
151
152 /**
153 * Depth of the last read XML tag
154 */
155 int actualDepth;
156
157 /**
158 * Name of the last read XML tag
159 */
160 TagName actualTagName;
161
162 /**
163 * Attributes and their values of the last read XML tag
164 * Stored in a special object designed for this purpose
165 */
166 TagAttributes actualTagAttributes;
167
168 /**
169 * Stack for storing the XML tag hierarchy but only for the last read XML tag
170 */
171 List<TagName> parentTagNames;
172
3abe9331 173 // Stack for keeping track if we are inside an annotation tag
174 List<TagName> inside_annotation;
175
970ed795
EL
176 static bool suspended;
177
178 /**
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
182 */
183 static unsigned int num_errors;
184 static unsigned int num_warnings;
185
186 /**
187 * Callback functions for LibXML SAX parser
188 */
3abe9331 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);
970ed795
EL
193
194 /** Callbacks cannot be member functions, use these static members as wrappers */
3abe9331 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);
970ed795 199
3abe9331 200 static void warningHandler(void * ctx, const char * msg, ...);
201 static void errorHandler(void * ctx, const char * msg, ...);
970ed795
EL
202
203 /**
204 * Converts name of read tag to enumerated value
205 * and load it to actualTagName member
206 *
207 * mode argument indicates that it is called when
208 * startelement or endelement arrived
209 */
3abe9331 210 void fillUpActualTagName(const char * localname, const tagMode mode);
970ed795
EL
211
212 /**
213 * Converts name and value of attributes of read tag
214 * and fill actualTagAttributes object with current values
215 */
3abe9331 216 void fillUpActualTagAttributes(const char ** attributes, const int att_count);
970ed795 217
3abe9331 218 XMLParser(const XMLParser &); // not implemented
219 XMLParser & operator=(const XMLParser &); // not implemented
970ed795 220public:
3abe9331 221 XMLParser(const char * a_filename);
222 ~XMLParser();
970ed795
EL
223
224 /**
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
228 */
3abe9331 229 void connectWithModule(TTCN3Module * a_module);
970ed795
EL
230
231 /**
232 * Start syntax checking, validation and parse
233 */
3abe9331 234 void checkSyntax();
235 void validate();
236 void startConversion(TTCN3Module * a_module);
237
238 static unsigned int getNumErrors() {
239 return num_errors;
240 }
241
242 static unsigned int getNumWarnings() {
243 return num_warnings;
244 }
245
246 static void incrNumErrors() {
247 ++num_errors;
248 }
249
250 static void incrNumWarnings() {
251 ++num_warnings;
252 }
253
254 const Mstring & getFilename() const {
255 return filename;
256 }
257
258 int getActualLineNumber() const {
259 return xmlSAX2GetLineNumber(context);
260 }
261
262 int getActualDepth() const {
263 return actualDepth;
264 }
265
266 TagName getActualTagName() const {
267 return actualTagName;
268 }
269
270 TagName getParentTagName() const {
271 return parentTagNames.empty() ? n_NOTSET : parentTagNames.back();
272 }
273
274 const TagAttributes & getActualTagAttributes() const {
275 return actualTagAttributes;
276 }
970ed795
EL
277};
278
279#endif /* PARSER_HH_ */
This page took 0.034492 seconds and 5 git commands to generate.