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