static void parseSchema(const char* fileName, EXIPSchema* schema) { FILE *schemaFile; BinaryBuffer buffer; errorCode tmp_err_code = UNEXPECTED_ERROR; size_t pathlen = strlen(dataDir); char exipath[MAX_PATH_LEN + strlen(fileName)]; memcpy(exipath, dataDir, pathlen); exipath[pathlen] = '/'; memcpy(&exipath[pathlen+1], fileName, strlen(fileName)+1); schemaFile = fopen(exipath, "rb" ); if(!schemaFile) { fail("Unable to open file %s", exipath); } else { //Get file length fseek(schemaFile, 0, SEEK_END); buffer.bufLen = ftell(schemaFile) + 1; fseek(schemaFile, 0, SEEK_SET); //Allocate memory buffer.buf = (char *)malloc(buffer.bufLen); if (!buffer.buf) { fclose(schemaFile); fail("Memory allocation error!"); } //Read file contents into buffer fread(buffer.buf, buffer.bufLen, 1, schemaFile); fclose(schemaFile); buffer.bufContent = buffer.bufLen; buffer.ioStrm.readWriteToStream = NULL; buffer.ioStrm.stream = NULL; tmp_err_code = generateSchemaInformedGrammars(&buffer, 1, SCHEMA_FORMAT_XSD_EXI, schema); if(tmp_err_code != ERR_OK) { fail("\n Error reading schema: %d", tmp_err_code); } free(buffer.buf); } }
int main(int argc, char *argv[]) { errorCode tmp_err_code = UNEXPECTED_ERROR; FILE *outfile; char sourceFile[50]; EXIPSchema schema; EXIPSchema* schemaPtr = NULL; if(argc > 1) { if(strcmp(argv[1], "-help") == 0) { printfHelp(); return 0; } else if(strcmp(argv[1], "-schema") == 0) { FILE *schemaFile; BinaryBuffer buffer[MAX_XSD_FILES_COUNT]; // up to 10 XSD files char schemaFileName[50]; unsigned int schemaFilesCount = 0; unsigned int i; if(argc <= 3) { printfHelp(); return 0; } else schemaFilesCount = argc - 3; if(schemaFilesCount > MAX_XSD_FILES_COUNT) { fprintf(stderr, "Too many xsd files given as an input: %d", schemaFilesCount); exit(1); } for(i = 0; i < schemaFilesCount; i++) { strcpy(schemaFileName, argv[2 + i]); schemaFile = fopen(schemaFileName, "rb" ); if(!schemaFile) { fprintf(stderr, "Unable to open file %s", schemaFileName); return 1; } else { //Get file length fseek(schemaFile, 0, SEEK_END); buffer[i].bufLen = ftell(schemaFile) + 1; fseek(schemaFile, 0, SEEK_SET); //Allocate memory buffer[i].buf = (char *) malloc(buffer[i].bufLen); if (!buffer[i].buf) { fprintf(stderr, "Memory allocation error!"); fclose(schemaFile); return 1; } //Read file contents into buffer fread(buffer[i].buf, buffer[i].bufLen, 1, schemaFile); fclose(schemaFile); buffer[i].bufContent = buffer[i].bufLen; buffer[i].ioStrm.readWriteToStream = NULL; buffer[i].ioStrm.stream = NULL; } } tmp_err_code = generateSchemaInformedGrammars(buffer, schemaFilesCount, SCHEMA_FORMAT_XSD_EXI, &schema); if(tmp_err_code != ERR_OK) { printf("\n Error occured: %d", tmp_err_code); return 1; } schemaPtr = &schema; for(i = 0; i < schemaFilesCount; i++) { free(buffer[i].buf); } } strcpy(sourceFile, argv[argc - 1]); outfile = fopen(sourceFile, "wb" ); if(!outfile) { fprintf(stderr, "Unable to open file %s", sourceFile); return 1; } else { EXIStream testStrm; String uri; String ln; QName qname = {&uri, &ln, NULL}; String chVal; char buf[OUTPUT_BUFFER_SIZE]; BinaryBuffer buffer; buffer.buf = buf; buffer.bufLen = OUTPUT_BUFFER_SIZE; buffer.bufContent = 0; // Serialization steps: // I: First initialize the header of the stream serialize.initHeader(&testStrm); // II: Set any options in the header, if different from the defaults testStrm.header.has_cookie = TRUE; testStrm.header.has_options = TRUE; testStrm.header.opts.valueMaxLength = 300; testStrm.header.opts.valuePartitionCapacity = 50; SET_STRICT(testStrm.header.opts.enumOpt); // III: Define an external stream for the output if any buffer.ioStrm.readWriteToStream = writeFileOutputStream; buffer.ioStrm.stream = outfile; // IV: Initialize the stream tmp_err_code = serialize.initStream(&testStrm, buffer, schemaPtr, SCHEMA_ID_ABSENT, NULL); if(tmp_err_code != ERR_OK) printError(tmp_err_code, &testStrm, outfile); // V: Start building the stream step by step: header, document, element etc... tmp_err_code += serialize.exiHeader(&testStrm); tmp_err_code += serialize.startDocument(&testStrm); qname.uri = &NS_STR; qname.localName = &ELEM_MULT_TEST_STR; tmp_err_code += serialize.startElement(&testStrm, qname); // <MultipleXSDsTest> qname.uri = &NS_STR; qname.localName = &ELEM_ENCODE_STR; tmp_err_code += serialize.startElement(&testStrm, qname); // <EXIPEncoder> // NOTE: attributes should come lexicographically sorted during serialization qname.uri = &NS_EMPTY_STR; qname.localName = &ATTR_BYTE_STR; if(schemaPtr != NULL) { // schema mode tmp_err_code += serialize.attribute(&testStrm, qname, VALUE_TYPE_SMALL_INTEGER); // testByte=" tmp_err_code += serialize.intData(&testStrm, 55); } else { // schema-less mode tmp_err_code += serialize.attribute(&testStrm, qname, VALUE_TYPE_STRING); // testByte=" tmp_err_code += asciiToString("55", &chVal, &testStrm.memList, FALSE); tmp_err_code += serialize.stringData(&testStrm, chVal); } qname.localName = &ATTR_VERSION_STR; tmp_err_code += serialize.attribute(&testStrm, qname, VALUE_TYPE_STRING); // version=" tmp_err_code += asciiToString("0.2", &chVal, &testStrm.memList, FALSE); tmp_err_code += serialize.stringData(&testStrm, chVal); tmp_err_code += asciiToString("This is an example of serializing EXI streams using EXIP low level API", &chVal, &testStrm.memList, FALSE); tmp_err_code += serialize.stringData(&testStrm, chVal); tmp_err_code += serialize.endElement(&testStrm); // </EXIPEncoder> qname.uri = &NS_STR; qname.localName = &ELEM_DESCR_STR; tmp_err_code += serialize.startElement(&testStrm, qname); // <description> tmp_err_code += asciiToString("This is a test of processing XML schemes with multiple XSD files", &chVal, &testStrm.memList, FALSE); tmp_err_code += serialize.stringData(&testStrm, chVal); tmp_err_code += serialize.endElement(&testStrm); // </description> qname.uri = &NS_NESTED_STR; qname.localName = &ELEM_TEST_SETUP_STR; tmp_err_code += serialize.startElement(&testStrm, qname); // <testSetup> qname.uri = &NS_EMPTY_STR; qname.localName = &ATTR_GOAL_STR; tmp_err_code += serialize.attribute(&testStrm, qname, VALUE_TYPE_STRING); // goal=" tmp_err_code += asciiToString("Verify that the implementation works!", &chVal, &testStrm.memList, FALSE); tmp_err_code += serialize.stringData(&testStrm, chVal); tmp_err_code += asciiToString("Simple test element with single attribute", &chVal, &testStrm.memList, FALSE); tmp_err_code += serialize.stringData(&testStrm, chVal); tmp_err_code += serialize.endElement(&testStrm); // </testSetup> qname.uri = &NS_STR; qname.localName = &ELEM_TYPE_TEST_STR; tmp_err_code += serialize.startElement(&testStrm, qname); // <type-test> if(schemaPtr != NULL) { // schema mode qname.uri = &NS_EMPTY_STR; qname.localName = &ATTR_ID_STR; tmp_err_code += serialize.attribute(&testStrm, qname, VALUE_TYPE_INTEGER); // id=" tmp_err_code += serialize.intData(&testStrm, 1001); } else { // schema-less mode qname.uri = &NS_EMPTY_STR; qname.localName = &ATTR_ID_STR; tmp_err_code += serialize.attribute(&testStrm, qname, VALUE_TYPE_STRING); // id=" tmp_err_code += asciiToString("1001", &chVal, &testStrm.memList, FALSE); tmp_err_code += serialize.stringData(&testStrm, chVal); } qname.uri = &NS_NESTED_STR; qname.localName = &ELEM_BOOL_STR; tmp_err_code += serialize.startElement(&testStrm, qname); // <bool> if(schemaPtr != NULL) { // schema mode tmp_err_code += serialize.booleanData(&testStrm, TRUE); } else { // schema-less mode tmp_err_code += asciiToString("true", &chVal, &testStrm.memList, FALSE); tmp_err_code += serialize.stringData(&testStrm, chVal); } tmp_err_code += serialize.endElement(&testStrm); // </bool> tmp_err_code += serialize.endElement(&testStrm); // </type-test> qname.uri = &NS_STR; qname.localName = &ELEM_EXT_TYPES_STR; tmp_err_code += serialize.startElement(&testStrm, qname); // <extendedTypeTest> qname.uri = &NS_EMPTY_STR; qname.localName = &ELEM_BYTE_TYPES_STR; tmp_err_code += serialize.startElement(&testStrm, qname); // <byteTest> if(schemaPtr != NULL) { // schema mode tmp_err_code += serialize.intData(&testStrm, 11); } else { // schema-less mode tmp_err_code += asciiToString("11", &chVal, &testStrm.memList, FALSE); tmp_err_code += serialize.stringData(&testStrm, chVal); } tmp_err_code += serialize.endElement(&testStrm); // </byteTest> qname.uri = &NS_EMPTY_STR; qname.localName = &ELEM_DATE_TYPES_STR; tmp_err_code += serialize.startElement(&testStrm, qname); // <dateTimeTest> if(schemaPtr != NULL) { // schema mode EXIPDateTime dt; dt.presenceMask = 0; dt.presenceMask = dt.presenceMask | YEAR_PRESENCE; dt.presenceMask = dt.presenceMask | MON_PRESENCE; dt.presenceMask = dt.presenceMask | MDAY_PRESENCE; dt.presenceMask = dt.presenceMask | HOUR_PRESENCE; dt.presenceMask = dt.presenceMask | MIN_PRESENCE; dt.presenceMask = dt.presenceMask | SEC_PRESENCE; dt.presenceMask = dt.presenceMask | FRACT_PRESENCE; dt.dateTime.tm_year = 112; // 2012 dt.dateTime.tm_mon = 6; // July dt.dateTime.tm_mday = 31; dt.dateTime.tm_hour = 13; dt.dateTime.tm_min = 33; dt.dateTime.tm_sec = 55; dt.fSecs.value = 839; dt.fSecs.offset = 5; tmp_err_code += serialize.dateTimeData(&testStrm, dt); } else { // schema-less mode tmp_err_code += asciiToString("2012 Jul 31 13:33", &chVal, &testStrm.memList, FALSE); tmp_err_code += serialize.stringData(&testStrm, chVal); } tmp_err_code += serialize.endElement(&testStrm); // </dateTimeTest> qname.uri = &NS_EMPTY_STR; qname.localName = &ELEM_BIN_TYPES_STR; tmp_err_code += serialize.startElement(&testStrm, qname); // <binaryTest> if(schemaPtr != NULL) { // schema mode tmp_err_code += serialize.binaryData(&testStrm, SOME_BINARY_DATA, 10); } else { // schema-less mode tmp_err_code += serialize.stringData(&testStrm, SOME_BINARY_DATA_BASE64); } tmp_err_code += serialize.endElement(&testStrm); // </binaryTest> qname.uri = &NS_EMPTY_STR; qname.localName = &ELEM_ENUM_TYPES_STR; tmp_err_code += serialize.startElement(&testStrm, qname); // <enumTest> tmp_err_code += serialize.stringData(&testStrm, ENUM_DATA_4); tmp_err_code += serialize.endElement(&testStrm); // </enumTest> tmp_err_code += serialize.endElement(&testStrm); // </extendedTypeTest> tmp_err_code += serialize.endElement(&testStrm); // </MultipleXSDsTest> tmp_err_code += serialize.endDocument(&testStrm); if(tmp_err_code != ERR_OK) printError(tmp_err_code, &testStrm, outfile); // VI: Free the memory allocated by the EXI stream object tmp_err_code = serialize.closeEXIStream(&testStrm); fclose(outfile); if(schemaPtr != NULL) destroySchema(schemaPtr); } } else { printfHelp(); return 1; } return 0; }
static void parseSchema(char* xsdList, EXIPSchema* schema) { errorCode tmp_err_code = UNEXPECTED_ERROR; FILE *schemaFile; BinaryBuffer buffer[MAX_XSD_FILES_COUNT]; // up to 10 XSD files char schemaFileName[50]; unsigned int schemaFilesCount = 0; unsigned int i; char *token; for (token = strtok(xsdList, "=,"), i = 0; token != NULL; token = strtok(NULL, "=,"), i++) { schemaFilesCount++; if(schemaFilesCount > MAX_XSD_FILES_COUNT) { fprintf(stderr, "Too many xsd files given as an input: %d", schemaFilesCount); exit(1); } strcpy(schemaFileName, token); schemaFile = fopen(schemaFileName, "rb" ); if(!schemaFile) { fprintf(stderr, "Unable to open file %s", schemaFileName); exit(1); } else { //Get file length fseek(schemaFile, 0, SEEK_END); buffer[i].bufLen = ftell(schemaFile) + 1; fseek(schemaFile, 0, SEEK_SET); //Allocate memory buffer[i].buf = (char *) malloc(buffer[i].bufLen); if (!buffer[i].buf) { fprintf(stderr, "Memory allocation error!"); fclose(schemaFile); exit(1); } //Read file contents into buffer fread(buffer[i].buf, buffer[i].bufLen, 1, schemaFile); fclose(schemaFile); buffer[i].bufContent = buffer[i].bufLen; buffer[i].ioStrm.readWriteToStream = NULL; buffer[i].ioStrm.stream = NULL; } } // Generate the EXI grammars based on the schema information tmp_err_code = generateSchemaInformedGrammars(buffer, schemaFilesCount, SCHEMA_FORMAT_XSD_EXI, NULL, schema); for(i = 0; i < schemaFilesCount; i++) { free(buffer[i].buf); } if(tmp_err_code != ERR_OK) { printf("\nGrammar generation error occurred: %d", tmp_err_code); exit(1); } }
/** * @brief Load a list of schema files. The resulting schema can be accessable with the * g_schemaPtr global variable. * @param fileNames List of filenames * @param schemaFilesCount The number of filenames in the list * @return void */ /*DEF*/ void XoLoadEXISchemaFiles(char** fileNames, unsigned int schemaFilesCount) { FILE *schemaFile; BinaryBuffer buffer[MAX_XSD_FILES_COUNT]; errorCode tmp_err_code; unsigned int i, cnt=0; if (schemaFilesCount > MAX_XSD_FILES_COUNT) schemaFilesCount = MAX_XSD_FILES_COUNT; for (i = 0; i < schemaFilesCount; i++) { schemaFile = fopen(fileNames[i], "rb" ); if(!schemaFile) { char tmp[1024]; // chercher d'abord chez jb sprintf (tmp,"%s/etc/xo/%s", getenv("ROOTACT"),fileNames[i]); schemaFile = fopen(tmp, "rb" ); if(!schemaFile) { sprintf (tmp,"%s/m2mxoref/exixsd/%s", getenv("ROOTACT"),fileNames[i]); schemaFile = fopen(tmp, "rb" ); } if(!schemaFile) continue; } //printf ("XoLoadEXISchemaFiles %s\n", fileNames[i]); fseek(schemaFile, 0, SEEK_END); buffer[cnt].bufLen = ftell(schemaFile) /* + 1 */; fseek(schemaFile, 0, SEEK_SET); buffer[cnt].buf = (char *) malloc(buffer[cnt].bufLen); if (!buffer[cnt].buf) { fprintf(stderr,"[%s:%d] Memory allocation error!", __FILE__, __LINE__); fclose(schemaFile); continue; } if(fread(buffer[cnt].buf, buffer[cnt].bufLen, 1, schemaFile)!=1) { fprintf(stderr,"[%s:%d] fread error!", __FILE__, __LINE__); fclose(schemaFile); continue; } buffer[cnt].bufContent = buffer[cnt].bufLen; buffer[cnt].ioStrm.readWriteToStream = NULL; buffer[cnt].ioStrm.stream = NULL; cnt ++; } tmp_err_code = generateSchemaInformedGrammars(buffer, cnt, SCHEMA_FORMAT_XSD_EXI, NULL, &g_schema); if (tmp_err_code) { fprintf (stderr,"[%s:%d] generateSchemaInformedGrammars with %d schemas => error %d\n", __FILE__, __LINE__, cnt, tmp_err_code); } if(tmp_err_code != ERR_OK) { g_schemaPtr = NULL; } else { g_schemaPtr = &g_schema; } for(i = 0; i < cnt; i++) free(buffer[i].buf); }