exip  Alpha 0.5.4
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
xsitypeProduct.c
Go to the documentation of this file.
1 /*==================================================================*\
2 | EXIP - Embeddable EXI Processor in C |
3 |--------------------------------------------------------------------|
4 | This work is licensed under BSD 3-Clause License |
5 | The full license terms and conditions are located in LICENSE.txt |
6 \===================================================================*/
7 
8 /*
9  Based on tests/check_xsi_type.c, this program encodes an EXI containing xsi:type
10  and then decodes it
11 
12 */
13 
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include "procTypes.h"
17 #include "EXISerializer.h"
18 #include "EXIParser.h"
19 #include "stringManipulate.h"
20 #include "grammarGenerator.h"
21 
22 #define OUTPUT_BUFFER_SIZE 2000
23 #define check(str) if (tmp_err_code != EXIP_OK) { printf (" =====> Err line %d (%s) code:%d\n", __LINE__, str, tmp_err_code); exit(0); }
24 
25 static int parseSchema(char* xsdList, EXIPSchema* schema);
26 
27 char *XSI = "http://www.w3.org/2001/XMLSchema-instance";
28 char *XS = "http://www.w3.org/2001/XMLSchema";
29 char *EXEMPLE = "http://www.exemple.com/XMLNameSpace";
30 
31 
32 static errorCode sample_fatalError(const errorCode code, const char* msg, void* app_data);
33 static errorCode sample_startDocument(void* app_data);
34 static errorCode sample_endDocument(void* app_data);
35 static errorCode sample_startElement(QName qname, void* app_data);
36 static errorCode sample_endElement(void* app_data);
37 static errorCode sample_attribute(QName qname, void* app_data);
38 static errorCode sample_stringData(const String value, void* app_data);
39 static errorCode sample_decimalData(Decimal value, void* app_data);
40 static errorCode sample_intData(Integer int_val, void* app_data);
41 static errorCode sample_floatData(Float fl_val, void* app_data);
42 static errorCode sample_booleanData(boolean bool_val, void* app_data);
43 static errorCode sample_dateTimeData(EXIPDateTime dt_val, void* app_data);
44 static errorCode sample_binaryData(const char* binary_val, Index nbytes, void* app_data);
45 static errorCode sample_qnameData(const QName qname, void* app_data);
46 
47 int main(int ac, char **av) {
48  EXIStream testStrm;
49  EXIPSchema schema, *schemaPtr = NULL;
50  String uri;
51  String ln;
52  QName qname= {&uri, &ln};
53  String chVal;
54  char buf[OUTPUT_BUFFER_SIZE];
55  errorCode tmp_err_code = 0;
56  BinaryBuffer buffer;
57  EXITypeClass valueType;
58  int opt;
59  char files[1000] = "";
60 
61  while ((opt = getopt(ac, av, "s")) != -1) {
62  switch (opt) {
63  case 's' :
64  strcpy (files, "Product.exs");
65  break;
66  }
67  }
68 
69  printf ("### %s\n", av[0]);
70 
71  buffer.buf = buf;
72  buffer.bufContent = 0;
73  buffer.bufLen = OUTPUT_BUFFER_SIZE;
74  buffer.ioStrm.readWriteToStream = NULL;
75  buffer.ioStrm.stream = NULL;
76 
77  if (*files && parseSchema(files, &schema) == 0) {
78  schemaPtr = &schema;
79  }
80 
81  serialize.initHeader(&testStrm);
82 
83  testStrm.header.has_options = TRUE;
84 
86 
87  if (schemaPtr) {
88  tmp_err_code = asciiToString("product", &testStrm.header.opts.schemaID, &testStrm.memList, FALSE); check("")
90  printf ("### schemaId %s\n", "product");
91  }
92 
93  tmp_err_code = serialize.initStream(&testStrm, buffer, schemaPtr); check("")
94  tmp_err_code = serialize.exiHeader(&testStrm); check("")
95  tmp_err_code = serialize.startDocument(&testStrm); check("SD")
96 
97  printf ("### startElement %s:%s\n", EXEMPLE, "product");
98  tmp_err_code += asciiToString(EXEMPLE, &uri, &testStrm.memList, FALSE); check("")
99  tmp_err_code += asciiToString("product", &ln, &testStrm.memList, FALSE); check("")
100  tmp_err_code += serialize.startElement(&testStrm, qname, &valueType); check("SE exe:product")
101 
102  printf ("### startElement %s:%s\n", EXEMPLE, "subproduct");
103  tmp_err_code += asciiToString("", &uri, &testStrm.memList, FALSE); check("")
104  tmp_err_code += asciiToString("subproduct", &ln, &testStrm.memList, FALSE); check("")
105  tmp_err_code += serialize.startElement(&testStrm, qname, &valueType); check("SE exe:subproduct")
106 
107  printf ("### attribute %s:%s\n", XSI, "type");
108  tmp_err_code += asciiToString(XSI, &uri, &testStrm.memList, FALSE); check("")
109  tmp_err_code += asciiToString("type", &ln, &testStrm.memList, FALSE); check("")
110  tmp_err_code += serialize.attribute(&testStrm, qname, TRUE, &valueType); check("xsi:type")
111 
112  printf ("### qnameData %s:%s\n", EXEMPLE, "ShirtType");
113  tmp_err_code += asciiToString(EXEMPLE, &uri, &testStrm.memList, FALSE); check("")
114  tmp_err_code += asciiToString("ShirtType", &ln, &testStrm.memList, FALSE); check("")
115  tmp_err_code += serialize.qnameData(&testStrm, qname); check("qnameData exe:ShirtType")
116 
117  printf ("### startElement %s:%s valueType=%d\n", "", "number", valueType);
118  tmp_err_code += asciiToString("", &uri, &testStrm.memList, FALSE); check("")
119  tmp_err_code += asciiToString("number", &ln, &testStrm.memList, FALSE); check("")
120  tmp_err_code += serialize.startElement(&testStrm, qname, &valueType); check("SE number")
121 
122  if (schemaPtr) {
123  printf ("### intData %s\n", "12345");
124  tmp_err_code += serialize.intData(&testStrm, 12345);
125  }
126  else {
127  printf ("### stringData %s\n", "12345");
128  tmp_err_code += asciiToString("12345", &chVal, &testStrm.memList, FALSE);
129  tmp_err_code += serialize.stringData(&testStrm, chVal); check("CH")
130  }
131 
132  printf ("### EE\n");
133  tmp_err_code += serialize.endElement(&testStrm); check("EE number")
134 
135  printf ("### startElement %s:%s valueType=%d\n", "", "size", valueType);
136  tmp_err_code += asciiToString("", &uri, &testStrm.memList, FALSE); check("")
137  tmp_err_code += asciiToString("size", &ln, &testStrm.memList, FALSE); check("")
138  tmp_err_code += serialize.startElement(&testStrm, qname, &valueType); check("SE size")
139 
140  if (schemaPtr) {
141  printf ("### intData %s\n", "33");
142  tmp_err_code += serialize.intData(&testStrm, 33);
143  }
144  else {
145  printf ("### stringData %s\n", "33");
146  tmp_err_code += asciiToString("33", &chVal, &testStrm.memList, FALSE);
147  tmp_err_code += serialize.stringData(&testStrm, chVal); check("CH")
148  }
149 
150  printf ("### EE\n");
151  tmp_err_code += serialize.endElement(&testStrm); check("EE size")
152 
153  printf ("### EE\n");
154  tmp_err_code += serialize.endElement(&testStrm); check("EE product")
155 
156  printf ("### EE\n");
157  tmp_err_code += serialize.endElement(&testStrm); check("EE subproduct")
158 
159  printf ("### ED\n");
160  tmp_err_code += serialize.endDocument(&testStrm); check("ED")
161 
162  // V: Free the memory allocated by the EXI stream object
163  tmp_err_code = serialize.closeEXIStream(&testStrm); check("")
164 
165  if (tmp_err_code == EXIP_OK)
166  printf ("### ENCODING SUCCESS sz=%d\n", testStrm.context.bufferIndx + 1);
167 
168  printf ("### START PARSING\n");
169 
170  // DECODING
171 
172  Parser testParser;
173 
174  buffer.bufContent = testStrm.context.bufferIndx + 1;
175  tmp_err_code = initParser(&testParser, buffer, NULL); check("")
176 
177  testParser.handler.fatalError = sample_fatalError;
178  testParser.handler.error = sample_fatalError;
179  testParser.handler.startDocument = sample_startDocument;
180  testParser.handler.endDocument = sample_endDocument;
181  testParser.handler.startElement = sample_startElement;
182  testParser.handler.attribute = sample_attribute;
183  testParser.handler.stringData = sample_stringData;
184  testParser.handler.endElement = sample_endElement;
185  testParser.handler.decimalData = sample_decimalData;
186  testParser.handler.intData = sample_intData;
187  testParser.handler.floatData = sample_floatData;
188  testParser.handler.booleanData = sample_booleanData;
189  testParser.handler.dateTimeData = sample_dateTimeData;
190  testParser.handler.binaryData = sample_binaryData;
191  testParser.handler.qnameData = sample_qnameData;
192 
193  tmp_err_code = parseHeader(&testParser, FALSE); check("")
194 
195  // IV.1: Set the schema to be used for parsing.
196  // The schemaID mode and schemaID field can be read at
197  // parser.strm.header.opts.schemaIDMode and
198  // parser.strm.header.opts.schemaID respectively
199  // If schemaless mode, use setSchema(&parser, NULL);
200 
201  TRY(setSchema(&testParser, schemaPtr));
202 
203  while(tmp_err_code == EXIP_OK)
204  {
205  tmp_err_code = parseNext(&testParser);
206  }
207 
208  destroyParser(&testParser);
209 
210  if (tmp_err_code == EXIP_PARSING_COMPLETE)
211  printf ("### PARSING SUCCESS\n");
212  else
213  printf ("### tmp_err_code = %d\n", tmp_err_code);
214 
215  return 0;
216 }
217 
218 static void printURI(const String *str) {
219  if (stringEqualToAscii(*str, XSI))
220  printf ("xsi");
221  else if (stringEqualToAscii(*str, XS))
222  printf ("xs");
223  else if (stringEqualToAscii(*str, EXEMPLE))
224  printf ("ABC");
225  else
226  fwrite (str->str, sizeof(CharType), str->length, stdout);
227 }
228 
229 void printQName(const QName qname) {
230  printURI (qname.uri);
231  printf(":");
232  printString(qname.localName);
233 }
234 
235 static errorCode sample_fatalError(const errorCode code, const char* msg, void* app_data)
236 {
237  printf("\n### %d : FATAL ERROR: %s\n", code, msg);
238  return EXIP_HANDLER_STOP;
239 }
240 
241 static errorCode sample_startDocument(void* app_data)
242 {
243  printf("### SD\n");
244  return EXIP_OK;
245 }
246 
247 static errorCode sample_endDocument(void* app_data)
248 {
249  printf("### ED\n");
250  return EXIP_OK;
251 }
252 
253 static errorCode sample_startElement(QName qname, void* app_data)
254 {
255  printf("### SE ");
256  printQName (qname);
257  printf("\n");
258  return EXIP_OK;
259 }
260 
261 static errorCode sample_endElement(void* app_data)
262 {
263  printf("### EE\n");
264  return EXIP_OK;
265 }
266 
268 
269 static errorCode sample_attribute(QName qname, void* app_data)
270 {
271  printf("### AT ");
272  printQName (qname);
273  printf("=\"");
274  expectAttributeData = 1;
275  return EXIP_OK;
276 }
277 
278 static errorCode sample_stringData(const String value, void* app_data)
279 {
280  if(expectAttributeData)
281  {
282  printString(&value);
283  printf("\"\n");
284  expectAttributeData = 0;
285  }
286  else
287  {
288  printf("### CH ");
289  printString(&value);
290  printf("\n");
291  }
292  return EXIP_OK;
293 }
294 
295 static errorCode sample_decimalData(Decimal value, void* app_data)
296 {
297  return EXIP_OK;
298 }
299 
300 static errorCode sample_intData(Integer int_val, void* app_data)
301 {
302  char tmp_buf[30];
303  if(expectAttributeData)
304  {
305  sprintf(tmp_buf, "%lld", (long long int) int_val);
306  printf("### intData %s", tmp_buf);
307  printf("\"\n");
308  expectAttributeData = 0;
309  }
310  else
311  {
312  printf("### intData ");
313  sprintf(tmp_buf, "%lld", (long long int) int_val);
314  printf("%s", tmp_buf);
315  printf("\n");
316  }
317  return EXIP_OK;
318 }
319 
320 static errorCode sample_booleanData(boolean bool_val, void* app_data)
321 {
322  return EXIP_OK;
323 }
324 
325 static errorCode sample_floatData(Float fl_val, void* app_data)
326 {
327  return EXIP_OK;
328 }
329 
330 static errorCode sample_dateTimeData(EXIPDateTime dt_val, void* app_data)
331 {
332  return EXIP_OK;
333 }
334 
335 static errorCode sample_binaryData(const char* binary_val, Index nbytes, void* app_data)
336 {
337  return EXIP_OK;
338 }
339 
340 static errorCode sample_qnameData(const QName qname, void* app_data)
341 {
342  //if(expectAttributeData)
343  {
344  printf ("### qnameData : ");
345  printQName (qname);
346  printf("\"\n");
347  expectAttributeData = 0;
348  }
349  return EXIP_OK;
350 }
351 
352 
353 #define MAX_XSD_FILES_COUNT 10 // up to 10 XSD files
354 
355 static int parseSchema(char* xsdList, EXIPSchema* schema)
356 {
357  errorCode tmp_err_code = EXIP_UNEXPECTED_ERROR;
358  FILE *schemaFile;
359  BinaryBuffer buffer[MAX_XSD_FILES_COUNT]; // up to 10 XSD files
360  char schemaFileName[50];
361  unsigned int schemaFilesCount = 0;
362  unsigned int i;
363  char *token;
364 
365  for (token = strtok(xsdList, "=,"), i = 0; token != NULL; token = strtok(NULL, "=,"), i++)
366  {
367 printf ("### %d %s\n", i, token);
368  schemaFilesCount++;
369  if(schemaFilesCount > MAX_XSD_FILES_COUNT)
370  {
371  fprintf(stderr, "Too many xsd files given as an input: %d", schemaFilesCount);
372  return 0;
373  }
374 
375  strcpy(schemaFileName, token);
376  schemaFile = fopen(schemaFileName, "rb" );
377  if(!schemaFile)
378  {
379  fprintf(stderr, "Unable to open file %s", schemaFileName);
380  continue;
381  }
382  else
383  {
384  //Get file length
385  fseek(schemaFile, 0, SEEK_END);
386  buffer[i].bufLen = ftell(schemaFile) + 1;
387  fseek(schemaFile, 0, SEEK_SET);
388 
389  //Allocate memory
390  buffer[i].buf = (char *) malloc(buffer[i].bufLen);
391  if (!buffer[i].buf)
392  {
393  fprintf(stderr, "Memory allocation error!");
394  fclose(schemaFile);
395  return -1;
396  }
397 
398  //Read file contents into buffer
399  fread(buffer[i].buf, buffer[i].bufLen, 1, schemaFile);
400  fclose(schemaFile);
401 
402  buffer[i].bufContent = buffer[i].bufLen;
403  buffer[i].ioStrm.readWriteToStream = NULL;
404  buffer[i].ioStrm.stream = NULL;
405  }
406  }
407 
408  // Generate the EXI grammars based on the schema information
409  tmp_err_code = generateSchemaInformedGrammars(buffer, schemaFilesCount, SCHEMA_FORMAT_XSD_EXI, NULL, schema);
410 
411  for(i = 0; i < schemaFilesCount; i++)
412  {
413  free(buffer[i].buf);
414  }
415 
416  if(tmp_err_code != EXIP_OK)
417  printf("\n### Grammar generation error occurred: %d\n", tmp_err_code);
418  else
419  printf("### Grammar generation SUCCESS\n");
420  return 0;
421 }