Titan Core Initial Contribution
[deliverable/titan.core.git] / xsdconvert / XMLParser.hh
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 ///////////////////////////////////////////////////////////////////////////////
8 #ifndef PARSER_HH_
9 #define PARSER_HH_
10
11 #include "GeneralTypes.hh"
12 #include "Mstring.hh"
13 #include "List.hh"
14
15 #include <libxml/parser.h>
16 #include <libxml/parserInternals.h>
17 #include <libxml/xmlschemas.h>
18 #include <libxml/xmlschemastypes.h>
19
20 class TTCN3Module;
21
22 class XMLParser
23 {
24 public:
25 /**
26 * List of possible names of XSD tags
27 */
28 enum TagName
29 {
30 // XSD Elements:
31 n_all,
32 n_annotation,
33 n_any,
34 n_anyAttribute,
35 n_appinfo,
36 n_attribute,
37 n_attributeGroup,
38 n_choice,
39 n_complexContent,
40 n_complexType,
41 n_documentation,
42 n_element,
43 n_extension,
44 n_field, // Not supported by now
45 n_group,
46 n_import,
47 n_include,
48 n_key, // Not supported by now
49 n_keyref, // Not supported by now
50 n_list,
51 n_notation, // Not supported by now
52 n_redefine,
53 n_restriction,
54 n_schema,
55 n_selector, // Not supported by now
56 n_sequence,
57 n_simpleContent,
58 n_simpleType,
59 n_union,
60 n_unique, // Not supported by now
61
62 // XSD Restrictions / Facets for Datatypes:
63 n_enumeration,
64 n_fractionDigits, // Not supported by now
65 n_length,
66 n_maxExclusive,
67 n_maxInclusive,
68 n_maxLength,
69 n_minExclusive,
70 n_minInclusive,
71 n_minLength,
72 n_pattern,
73 n_totalDigits,
74 n_whiteSpace,
75
76 // Others - non-standard, but used:
77 n_label, // ???
78 n_definition, // ???
79
80 n_NOTSET
81 };
82
83 /**
84 * List of possible names of XSD tag attributes
85 */
86 enum TagAttributeName
87 {
88 a_abstract, // Not supported by now
89 a_attributeFormDefault,
90 a_base,
91 a_block, // Not supported by now
92 a_blockDefault, // Not supported by now
93 a_default,
94 a_elementFormDefault,
95 a_final, // Not supported by now
96 a_finalDefault, // Not supported by now
97 a_fixed,
98 a_form,
99 a_id,
100 a_itemType,
101 a_lang, // Not supported by now
102 a_maxOccurs,
103 a_memberTypes,
104 a_minOccurs,
105 a_mixed,
106 a_name,
107 a_namespace,
108 a_nillable,
109 a_processContents, // Not supported by now
110 a_ref,
111 a_schemaLocation,
112 a_source, // ???
113 a_substitutionGroup, // Not supported by now
114 a_targetNamespace,
115 a_type,
116 a_use,
117 a_value,
118 a_version, // Not supported by now
119 a_xpath, // Not supported by now
120
121 a_NOTSET
122 };
123
124 class TagAttributes
125 {
126 TagAttributes (const TagAttributes &); // not implemented
127 TagAttributes & operator = (const TagAttributes &); // not implemented
128 public:
129 XMLParser * parser; // not responsibility for the member
130
131 /**
132 * Members for storing actual values of attributes of an XML tag
133 */
134 FormValue attributeFormDefault;
135 Mstring base;
136 Mstring default_;
137 FormValue elementFormDefault;
138 Mstring fixed;
139 FormValue form;
140 Mstring id;
141 Mstring itemType;
142 unsigned long long int maxOccurs;
143 Mstring memberTypes;
144 unsigned long long int minOccurs;
145 bool mixed;
146 Mstring name;
147 Mstring namespace_;
148 bool nillable;
149 Mstring ref;
150 Mstring schemaLocation;
151 Mstring source;
152 Mstring targetNamespace;
153 Mstring type;
154 UseValue use;
155 Mstring value;
156
157 TagAttributes (XMLParser * withThisParser);
158 // Default destructor is used
159
160 /**
161 * Clear and fill up object with values of attributes of current XML tag
162 */
163 void fillUp (TagAttributeName * att_name_e, Mstring * att_value_s, int att_count);
164 };
165
166 private:
167
168 enum tagMode
169 {
170 startElement,
171 endElement
172 };
173
174 /**
175 * Related TTCN-3 module
176 * Information from parsed XML schema is loaded into this module
177 *
178 * One-to-one relation between XMLParser object and TTCN3Module object
179 */
180 TTCN3Module * module; // no responsibility for this member
181
182 /**
183 * Name of XML schema
184 * Each schema file has a unique XMLParser object to parse it
185 */
186 Mstring filename;
187
188 /**
189 * Pointers that are used by LibXML SAX parser
190 */
191 xmlSAXHandlerPtr parser;
192 xmlParserCtxtPtr context;
193
194 xmlSAXHandlerPtr parserCheckingXML;
195 xmlParserCtxtPtr contextCheckingXML;
196 xmlSchemaParserCtxtPtr contextCheckingXSD;
197
198 /**
199 * Depth of the last read XML tag
200 */
201 int actualDepth;
202
203 /**
204 * Name of the last read XML tag
205 */
206 TagName actualTagName;
207
208 /**
209 * Attributes and their values of the last read XML tag
210 * Stored in a special object designed for this purpose
211 */
212 TagAttributes actualTagAttributes;
213
214 /**
215 * Stack for storing the XML tag hierarchy but only for the last read XML tag
216 */
217 List<TagName> parentTagNames;
218
219 static bool suspended;
220
221 /**
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
225 */
226 static unsigned int num_errors;
227 static unsigned int num_warnings;
228
229 /**
230 * Callback functions for LibXML SAX parser
231 */
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);
236
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);
242
243 static void warningHandler (void * ctx, const char * msg, ...);
244 static void errorHandler (void * ctx, const char * msg, ...);
245
246 /**
247 * Converts name of read tag to enumerated value
248 * and load it to actualTagName member
249 *
250 * mode argument indicates that it is called when
251 * startelement or endelement arrived
252 */
253 void fillUpActualTagName (const char * localname, tagMode mode);
254
255 /**
256 * Converts name and value of attributes of read tag
257 * and fill actualTagAttributes object with current values
258 */
259 void fillUpActualTagAttributes (const char ** attributes, int att_count);
260
261 XMLParser (const XMLParser &); // not implemented
262 XMLParser & operator = (const XMLParser &); // not implemented
263 public:
264 XMLParser (const char * a_filename);
265 ~XMLParser ();
266
267 /**
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
271 */
272 void connectWithModule (TTCN3Module * a_module);
273
274 /**
275 * Start syntax checking, validation and parse
276 */
277 void checkSyntax ();
278 void validate ();
279 void startConversion (TTCN3Module * a_module);
280
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;}
285
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;}
292 };
293
294 #endif /* PARSER_HH_ */
This page took 0.044092 seconds and 5 git commands to generate.