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