TEST_P(CsvRecordParserParamTest, Generic){ const CsvRecordParserConfig& p = GetParam(); std::string filename = p.filename; base::ScopedFile file(filename); blockreader::IBlockReaderPtr b = blockreader::BlockReaderFactory::makeBlockReader(base::utils::getExtension(filename)); splitproducer::ISplitProducerPtr s = splitproducer::SplitProducerFactory::makeSplitProducer(base::utils::getExtension(filename)); IRecordParserPtr r = RecordParserFactory::makeRecordParser(base::utils::getExtension(filename)); base::ConfigurationMap blockReaderConf; blockReaderConf["blocksize"] = (uint64_t)(32 * 1024 * 1024); blockReaderConf["filename"] = filename; b->configure(blockReaderConf); base::ConfigurationMap splitProducerConf; splitProducerConf["delimiter"] = (uint8_t)'\n'; splitProducerConf["splitStart"] = (uint64_t)0; splitProducerConf["splitEnd"] = file.stat().length; splitProducerConf["fileEnd"] = file.stat().length; splitProducerConf["blockReader"] = b; splitProducerConf["skipHeader"] = false; s->configure(splitProducerConf); base::ConfigurationMap recordParserConf; recordParserConf["splitProducer"] = s; std::string schema = p.schema; recordParserConf["schema"] = parseSchema(schema); recordParserConf["delimiter"] = ','; recordParserConf["commentCharacter"] = '#'; // only useful for debugging failures recordParserConf["chunkStart"] = (uint64_t)0; recordParserConf["chunkEnd"] = (uint64_t)0; recordParserConf["url"] = std::string(); r->configure(recordParserConf); assembler::IAssemblerPtr fakeAssembler = assembler::AssemblerFactory::makeAssembler("fake"); r->registerListener(fakeAssembler.get()); std::vector<boost::any> records; while(r->hasNext()) { boost::any record = r->next(); records.push_back(record); } EXPECT_EQ(records.size(), p.numLines * p.numCols); recordparser::CsvRecord csvRecord = boost::any_cast<recordparser::CsvRecord>(records[0]); double value = boost::get<double>(csvRecord.value); EXPECT_TRUE(base::utils::areEqual(value, (double)0)); recordparser::CsvRecord csvRecord2 = boost::any_cast<recordparser::CsvRecord>(records[records.size() - 1]); double value2 = boost::get<double>(csvRecord2.value); EXPECT_TRUE(base::utils::areEqual(value2, (double)(records.size() - 1))); }
void SGMLParser :: loadSchema(const char * aSchemaFile) { string schemaFileName = aSchemaFile; if (mSchemas.size() == 0) { // We need to parse it first. printf("Parsing first schema\n"); parseSchema(aSchemaFile); mSchema->computeSchema(); mSchemas.insert(map<string, TSchemaPtr>::value_type(schemaFileName, mSchema)); } else if (mSchemas.count(schemaFileName)) { // We already parsed it once. Just reload it as current. map<string, TSchemaPtr>::iterator i = mSchemas.find(schemaFileName); printf("Found an existing schema\n"); mSchema = (*i).second; mCommentDeclParser->setSchema(mSchema); mDocTypeDeclParser->setSchema(mSchema); mDTDParser->setSchema(mSchema); } else { // There are already schemas parsed, but not the one requested. printf("Parsing additional schema\n"); // Create a new schema to parse into. mSchema = TSchemaPtr(new TSchema()); mSchema->setup(); printf("Setup done. Now assigning\n"); mCommentDeclParser->setSchema(mSchema); mDocTypeDeclParser->setSchema(mSchema); mDTDParser->setSchema(mSchema); // Parse the schema and store it. printf("Parsing...\n"); parseSchema(aSchemaFile); mSchema->computeSchema(); mSchemas.insert(map<string, TSchemaPtr>::value_type(schemaFileName, mSchema)); } }
void JsonValidator::readSchema(const char *schema_file) { std::ifstream t(schema_file); // reserver memory for entire schema instead of string class automatic // reallocation std::string str; t.seekg(0, std::ios::end); str.reserve(t.tellg()); t.seekg(0, std::ios::beg); str.assign((std::istreambuf_iterator<char>(t)), std::istreambuf_iterator<char>()); parseSchema(str); }
bool parse(const std::string& url) { // setup protocol = ""; host = ""; port = 0; username = ""; password = ""; path = ""; parameters = ""; anchor =""; pathless = true; if (url.size() > YAHTTP_MAX_URL_LENGTH) return false; size_t pos = 0; if (*(url.begin()) != '/') { // full url? if (parseSchema(url, pos) == false) return false; if (pathless) { parameters = url.substr(pos); return true; } if (parseUserPass(url, pos) == false) return false; if (parseHost(url, pos) == false) return false; } if (parsePath(url, pos) == false) return false; if (parseParameters(url, pos) == false) return false; return parseAnchor(url, pos); };
END_TEST START_TEST (test_strict_option) { EXIPSchema schema; Parser testParser; char buf[OUTPUT_BUFFER_SIZE]; errorCode tmp_err_code = UNEXPECTED_ERROR; BinaryBuffer buffer; char exipath[MAX_PATH_LEN + strlen(EMPTY_TYPE_DEFAULT)]; size_t pathlen; FILE *infile; struct appData parsingData; buffer.buf = buf; buffer.bufContent = 0; buffer.bufLen = OUTPUT_BUFFER_SIZE; buffer.ioStrm.readWriteToStream = NULL; buffer.ioStrm.stream = NULL; // Parsing steps: // I.A: First, read in the schema parseSchema(EMPTY_TYPE_SCHEMA, &schema); // I.B: Define an external stream for the input to the parser if any pathlen = strlen(dataDir); memcpy(exipath, dataDir, pathlen); exipath[pathlen] = '/'; memcpy(&exipath[pathlen+1], EMPTY_TYPE_STRICT, strlen(EMPTY_TYPE_STRICT)+1); infile = fopen(exipath, "rb" ); if(!infile) fail("Unable to open file %s", exipath); buffer.ioStrm.readWriteToStream = readFileInputStream; buffer.ioStrm.stream = infile; // II: Second, initialize the parser object tmp_err_code = initParser(&testParser, buffer, &schema, &parsingData); fail_unless (tmp_err_code == ERR_OK, "initParser returns an error code %d", tmp_err_code); // III: Initialize the parsing data and hook the callback handlers to the parser object parsingData.eventCount = 0; parsingData.expectAttributeData = 0; testParser.handler.fatalError = sample_fatalError; testParser.handler.error = sample_fatalError; testParser.handler.startElement = sample_startElement; testParser.handler.attribute = sample_attribute; testParser.handler.stringData = sample_stringData; testParser.handler.intData = sample_intData; // IV: Parse the header of the stream tmp_err_code = parseHeader(&testParser); fail_unless (tmp_err_code == ERR_OK, "parsing the header returns an error code %d", tmp_err_code); // V: Parse the body of the EXI stream while(tmp_err_code == ERR_OK) { tmp_err_code = parseNext(&testParser); } // VI: Free the memory allocated by the parser object destroyParser(&testParser); fail_unless (tmp_err_code == PARSING_COMPLETE, "Error during parsing of the EXI body %d", tmp_err_code); }
int main(int argc, char *argv[]) { errorCode tmp_err_code = UNEXPECTED_ERROR; FILE *outfile = stdout; // Default is the standard output char sourceFile[100]; EXIPSchema schema; EXIPSchema* schemaPtr = NULL; int argIndex = 1; strcpy(sourceFile, "stdout"); if(argc >= 2) { if(strcmp(argv[argIndex], "-help") == 0) { printfHelp(); return 0; } else if(strstr(argv[argIndex], "-schema") != NULL) { // Schema enabled encoding is requested. // All the xsd files should be passed as comma-separated list char* xsdList = argv[argIndex] + 7; parseSchema(xsdList, &schema); schemaPtr = &schema; argIndex++; } } if(argIndex < argc) { strcpy(sourceFile, argv[argIndex]); outfile = fopen(sourceFile, "wb" ); if(!outfile) { fprintf(stderr, "Unable to open file %s", sourceFile); return 1; } } tmp_err_code = encode(schemaPtr, outfile, writeFileOutputStream); if(schemaPtr != NULL) destroySchema(schemaPtr); if(tmp_err_code != ERR_OK) { printf("\nError (code: %d) during encoding of: %s\n", tmp_err_code, sourceFile); exit(1); } else { printf("\nSuccessful encoding in %s\n", sourceFile); exit(1); } fclose(outfile); }
JsonValidator::JsonValidator(std::string &schema) { m_primitive = NULL; parseSchema(schema); }
int main(int argc, char *argv[]) { FILE *infile = stdin; char sourceFileName[500]; EXIPSchema schema; EXIPSchema* schemaPtr = NULL; EXIOptions ops; EXIOptions* opsPtr = NULL; boolean outOfBandOpts = FALSE; unsigned char outFlag = OUT_EXI; // Default output option unsigned int argIndex = 1; errorCode tmp_err_code = EXIP_UNEXPECTED_ERROR; strcpy(sourceFileName, "stdin"); makeDefaultOpts(&ops); struct timespec start, stopGrammar, stopGrAndInstance; if(argc > 1) { if(strcmp(argv[argIndex], "-help") == 0) { printfHelp(); return 0; } else if(strcmp(argv[argIndex], "-exi") == 0) { outFlag = OUT_EXI; if(argc == 2) { printfHelp(); return 0; } argIndex += 1; } else if(strcmp(argv[argIndex], "-xml") == 0) { outFlag = OUT_XML; if(argc == 2) { printfHelp(); return 0; } argIndex += 1; } if(strstr(argv[argIndex], "-ops") != NULL) { char *mask = argv[argIndex] + 4; outOfBandOpts = TRUE; parseOpsMask(mask, &ops); opsPtr = &ops; argIndex += 1; } if(strstr(argv[argIndex], "-schema") != NULL) { char *xsdList = argv[argIndex] + 7; if( clock_gettime( CLOCK_MONOTONIC, &start) == -1 ) { perror( "clock gettime" ); exit( EXIT_FAILURE ); } parseSchema(xsdList, &schema); if( clock_gettime( CLOCK_MONOTONIC, &stopGrammar) == -1 ) { perror( "clock gettime" ); exit( EXIT_FAILURE ); } printf( "%ld ; ", ((long) (stopGrammar.tv_sec - start.tv_sec)*1000000000) + (long) (stopGrammar.tv_nsec - start.tv_nsec)); // printf( "SECONDS: %ld\n", (long) (stopGrammar.tv_sec - start.tv_sec)); // printf( "NANOSEC: %ld\n", (long) (stopGrammar.tv_nsec - start.tv_nsec)); schemaPtr = &schema; argIndex += 1; } } if(argIndex < argc) { strcpy(sourceFileName, argv[argIndex]); infile = fopen(sourceFileName, "rb" ); if(!infile) { fprintf(stderr, "Unable to open file %s\n", sourceFileName); exit(1); } } tmp_err_code = decode(schemaPtr, outFlag, infile, outOfBandOpts, opsPtr, readFileInputStream); if(schemaPtr != NULL) destroySchema(schemaPtr); fclose(infile); if( clock_gettime( CLOCK_MONOTONIC, &stopGrAndInstance) == -1 ) { perror( "clock gettime" ); exit( EXIT_FAILURE ); } printf( "%ld", ((long) (stopGrAndInstance.tv_sec - start.tv_sec)*1000000000) + (long) (stopGrAndInstance.tv_nsec - start.tv_nsec)); // printf( "SECONDS: %ld\n", (long) (stopGrAndInstance.tv_sec - start.tv_sec)); // printf( "NANOSEC: %ld\n", (long) (stopGrAndInstance.tv_nsec - start.tv_nsec)); if(tmp_err_code != EXIP_OK) { printf("\nError (code: %d) during parsing of the EXI stream: %s\n", tmp_err_code, sourceFileName); return 1; } else { // printf("\nSuccessful parsing of the EXI stream: %s\n", sourceFileName); return 0; } }
static error_code serializeDevDescMsg(char* buf, unsigned int buf_size, unsigned int* msg_size, DevDescribtion devDesc) { EXIStream strm; String uri; String ln; String ch; QName qname = {&uri, &ln, NULL}; errorCode tmp_err_code = EXIP_UNEXPECTED_ERROR; BinaryBuffer buffer; EXIPSchema lkab_schema; const char *schemafname = "SchemaStrict/lkab-devices-xsd.exi"; EXITypeClass valueType; buffer.buf = buf; buffer.bufLen = buf_size; buffer.bufContent = 0; // Serialization steps: // I.A: First, read in the schema parseSchema(schemafname, &lkab_schema); // I: First initialize the header of the stream serialize.initHeader(&strm); // II: Set any options in the header, if different from the defaults strm.header.has_options = TRUE; SET_STRICT(strm.header.opts.enumOpt); // III: Define an external stream for the output if any buffer.ioStrm.stream = NULL; buffer.ioStrm.readWriteToStream = NULL; // IV: Initialize the stream tmp_err_code = serialize.initStream(&strm, buffer, &lkab_schema); if(tmp_err_code != EXIP_OK) return tmp_err_code; // V: Start building the stream step by step: header, document, element etc... tmp_err_code += serialize.exiHeader(&strm); tmp_err_code += serialize.startDocument(&strm); qname.uri = &NS_STR; qname.localName = &ELEM_DEV_DESC_STR; tmp_err_code += serialize.startElement(&strm, qname, &valueType); // <DeviceDescription> qname.uri = &NS_STR; qname.localName = &ELEM_ID_STR; tmp_err_code += serialize.startElement(&strm, qname, &valueType); // <id> ch.str = devDesc.id; ch.length = strlen(devDesc.id); tmp_err_code += serialize.stringData(&strm, ch); // device id tmp_err_code += serialize.endElement(&strm); // </id> qname.uri = &NS_STR; qname.localName = &ELEM_NAME_STR; tmp_err_code += serialize.startElement(&strm, qname, &valueType); // <name> ch.str = devDesc.name; ch.length = strlen(devDesc.name); tmp_err_code += serialize.stringData(&strm, ch); // device name tmp_err_code += serialize.endElement(&strm); // </name> qname.uri = &NS_STR; qname.localName = &ELEM_TYPE_STR; tmp_err_code += serialize.startElement(&strm, qname, &valueType); // <type> ch.str = devDesc.type; ch.length = strlen(devDesc.type); tmp_err_code += serialize.stringData(&strm, ch); // device type tmp_err_code += serialize.endElement(&strm); // </type> qname.uri = &NS_STR; qname.localName = &ELEM_LOCATION_STR; tmp_err_code += serialize.startElement(&strm, qname, &valueType); // <location> ch.str = devDesc.location; ch.length = strlen(devDesc.location); tmp_err_code += serialize.stringData(&strm, ch); // device location tmp_err_code += serialize.endElement(&strm); // </location> qname.uri = &NS_STR; qname.localName = &ELEM_PROSS_VAL_STR; tmp_err_code += serialize.startElement(&strm, qname, &valueType); // <processValues> qname.uri = &NS_STR; qname.localName = &ELEM_NAME_STR; tmp_err_code += serialize.startElement(&strm, qname, &valueType); // <name> ch.str = devDesc.processValue.name; ch.length = strlen(devDesc.processValue.name); tmp_err_code += serialize.stringData(&strm, ch); // processValues name tmp_err_code += serialize.endElement(&strm); // </name> qname.uri = &NS_STR; qname.localName = &ELEM_TYPE_STR; tmp_err_code += serialize.startElement(&strm, qname, &valueType); // <type> tmp_err_code += serialize.stringData(&strm, ENUM_DATA_VAL_TYPE[devDesc.processValue.type]); // processValues type tmp_err_code += serialize.endElement(&strm); // </type> qname.uri = &NS_STR; qname.localName = &ELEM_IS_READONLY_STR; tmp_err_code += serialize.startElement(&strm, qname, &valueType); // <isReadOnly> tmp_err_code += serialize.booleanData(&strm, 0); // processValues isReadOnly tmp_err_code += serialize.endElement(&strm); // </isReadOnly> qname.uri = &NS_STR; qname.localName = &ELEM_DESC_STR; tmp_err_code += serialize.startElement(&strm, qname, &valueType); // <description> ch.str = devDesc.processValue.description; ch.length = strlen(devDesc.processValue.description); tmp_err_code += serialize.stringData(&strm, ch); // processValues description tmp_err_code += serialize.endElement(&strm); // </description> tmp_err_code += serialize.endElement(&strm); // </processValues> tmp_err_code += serialize.endElement(&strm); // </DeviceDescription> tmp_err_code += serialize.endDocument(&strm); if(tmp_err_code != EXIP_OK) return tmp_err_code; *msg_size = strm.context.bufferIndx + 1; // VI: Free the memory allocated by the EXI stream object tmp_err_code = serialize.closeEXIStream(&strm); return EXIP_OK; }
static error_code serializeIOMsg(char* buf, unsigned int buf_size, unsigned int* msg_size, BoolValue val) { EXIStream strm; String uri; String ln; QName qname = {&uri, &ln, NULL}; errorCode tmp_err_code = EXIP_UNEXPECTED_ERROR; BinaryBuffer buffer; EXIPDateTime dt; EXIPSchema lkab_schema; const char *schemafname = "SchemaStrict/lkab-devices-xsd.exi"; EXITypeClass valueType; buffer.buf = buf; buffer.bufLen = buf_size; buffer.bufContent = 0; // Serialization steps: // I.A: First, read in the schema parseSchema(schemafname, &lkab_schema); // I: First initialize the header of the stream serialize.initHeader(&strm); // II: Set any options in the header, if different from the defaults strm.header.has_options = TRUE; SET_STRICT(strm.header.opts.enumOpt); // III: Define an external stream for the output if any buffer.ioStrm.stream = NULL; buffer.ioStrm.readWriteToStream = NULL; // IV: Initialize the stream tmp_err_code = serialize.initStream(&strm, buffer, &lkab_schema); if(tmp_err_code != EXIP_OK) return tmp_err_code; // V: Start building the stream step by step: header, document, element etc... tmp_err_code += serialize.exiHeader(&strm); tmp_err_code += serialize.startDocument(&strm); qname.uri = &NS_STR; qname.localName = &ELEM_BOOL_VAL_STR; tmp_err_code += serialize.startElement(&strm, qname, &valueType); // <BoolValue> qname.uri = &NS_STR; qname.localName = &ELEM_TIMESTAMP_STR; tmp_err_code += serialize.startElement(&strm, qname, &valueType); // <timeStamp> dt.presenceMask = FRACT_PRESENCE; dt.dateTime.tm_year = val.ts.year; dt.dateTime.tm_mon = val.ts.month; dt.dateTime.tm_mday = val.ts.mday; dt.dateTime.tm_hour = val.ts.hour; dt.dateTime.tm_min = val.ts.min; dt.dateTime.tm_sec = val.ts.sec; dt.fSecs.value = val.ts.msec; dt.fSecs.offset = 2; tmp_err_code += serialize.dateTimeData(&strm, dt); tmp_err_code += serialize.endElement(&strm); // </timeStamp> qname.uri = &NS_STR; qname.localName = &ELEM_QUAL_STR; tmp_err_code += serialize.startElement(&strm, qname, &valueType); // <quality> tmp_err_code += serialize.stringData(&strm, ENUM_DATA_QUALITY[val.quality]); // quality tmp_err_code += serialize.endElement(&strm); // </quality> qname.uri = &NS_STR; qname.localName = &ELEM_VALUE_STR; tmp_err_code += serialize.startElement(&strm, qname, &valueType); // <value> tmp_err_code += serialize.booleanData(&strm, val.val); tmp_err_code += serialize.endElement(&strm); // </value> tmp_err_code += serialize.endElement(&strm); // </BoolValue> tmp_err_code += serialize.endDocument(&strm); if(tmp_err_code != EXIP_OK) return tmp_err_code; *msg_size = strm.context.bufferIndx + 1; // VI: Free the memory allocated by the EXI stream object tmp_err_code = serialize.closeEXIStream(&strm); return EXIP_OK; }
END_TEST /* * Verifies error when more elements than schema maxOccurs permits. Attempts * to encode: * * <A xmlns='urn:foo'> * <AB/><AC/><AC/><AC/> * </A> */ START_TEST (test_acceptance_for_A_01b) { EXIPSchema schema; EXIStream testStrm; String uri; String ln; QName qname = {&uri, &ln, NULL}; char buf[OUTPUT_BUFFER_SIZE]; const char *schemafname = "testStates/acceptance-xsd.exi"; errorCode tmp_err_code = EXIP_UNEXPECTED_ERROR; EXITypeClass valueType; const String NS_STR = {"urn:foo", 7}; const String ELEM_A = {"A", 1}; const String ELEM_AB = {"AB", 2}; const String ELEM_AC = {"AC", 2}; const String CH = {"", 0}; BinaryBuffer buffer; buffer.buf = buf; buffer.bufLen = OUTPUT_BUFFER_SIZE; buffer.bufContent = 0; // Serialization steps: // I.A: First, read in the schema parseSchema(schemafname, &schema); // 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 = NULL; buffer.ioStrm.stream = NULL; // IV: Initialize the stream tmp_err_code = serialize.initStream(&testStrm, buffer, &schema); fail_unless (tmp_err_code == EXIP_OK, "initStream returns an error code %d", tmp_err_code); // 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_A; tmp_err_code += serialize.startElement(&testStrm, qname, &valueType); fail_unless (tmp_err_code == EXIP_OK, "serialization returns an error code %d", tmp_err_code); qname.localName = &ELEM_AB; tmp_err_code += serialize.startElement(&testStrm, qname, &valueType); tmp_err_code += serialize.stringData(&testStrm, CH); tmp_err_code += serialize.endElement(&testStrm); fail_unless (tmp_err_code == EXIP_OK, "serialization returns an error code %d", tmp_err_code); qname.localName = &ELEM_AC; tmp_err_code += serialize.startElement(&testStrm, qname, &valueType); tmp_err_code += serialize.stringData(&testStrm, CH); tmp_err_code += serialize.endElement(&testStrm); fail_unless (tmp_err_code == EXIP_OK, "serialization returns an error code %d", tmp_err_code); tmp_err_code += serialize.startElement(&testStrm, qname, &valueType); tmp_err_code += serialize.stringData(&testStrm, CH); tmp_err_code += serialize.endElement(&testStrm); fail_unless (tmp_err_code == EXIP_OK, "serialization returns an error code %d", tmp_err_code); /* Expect failure when start third AC element */ tmp_err_code += serialize.startElement(&testStrm, qname, &valueType); fail_unless (tmp_err_code == EXIP_INCONSISTENT_PROC_STATE, "Expected EXIP_INCONSISTENT_PROC_STATE, but returns an error code %d", tmp_err_code); // VI: Free the memory allocated by the EXI stream object tmp_err_code = serialize.closeEXIStream(&testStrm); }
END_TEST /* Verifies a single global element also used as a reference. */ START_TEST (test_acceptance_for_A_01_exip1) { EXIPSchema schema; FILE *infile; Parser testParser; char buf[INPUT_BUFFER_SIZE]; const char *schemafname = "testStates/acceptance-xsd.exi"; const char *exifname = "testStates/acceptance_a_01a.exi"; char exipath[MAX_PATH_LEN + strlen(exifname)]; struct appData parsingData; errorCode tmp_err_code = EXIP_UNEXPECTED_ERROR; BinaryBuffer buffer; buffer.buf = buf; buffer.bufContent = 0; buffer.bufLen = INPUT_BUFFER_SIZE; // Parsing steps: // I.A: First, read in the schema parseSchema(schemafname, &schema); // I.B: Define an external stream for the input to the parser if any size_t pathlen = strlen(dataDir); memcpy(exipath, dataDir, pathlen); exipath[pathlen] = '/'; memcpy(&exipath[pathlen+1], exifname, strlen(exifname)+1); infile = fopen(exipath, "rb" ); if(!infile) fail("Unable to open file %s", exipath); buffer.ioStrm.readWriteToStream = readFileInputStream; buffer.ioStrm.stream = infile; // II: Second, initialize the parser object tmp_err_code = initParser(&testParser, buffer, &parsingData); fail_unless (tmp_err_code == EXIP_OK, "initParser returns an error code %d", tmp_err_code); // III: Initialize the parsing data and hook the callback handlers to the parser object parsingData.eventCount = 0; parsingData.expectAttributeData = 0; if (EXIP_OK != initAllocList(&parsingData.allocList)) fail("Memory allocation error!"); testParser.handler.fatalError = sample_fatalError; testParser.handler.error = sample_fatalError; testParser.handler.startDocument = sample_startDocument; testParser.handler.endDocument = sample_endDocument; testParser.handler.startElement = sample_startElement; testParser.handler.attribute = sample_attribute; testParser.handler.stringData = sample_stringData; testParser.handler.endElement = sample_endElement; testParser.handler.decimalData = sample_decimalData; testParser.handler.intData = sample_intData; testParser.handler.floatData = sample_floatData; // IV: Parse the header of the stream tmp_err_code = parseHeader(&testParser, FALSE); fail_unless (tmp_err_code == EXIP_OK, "parsing the header returns an error code %d", tmp_err_code); tmp_err_code = setSchema(&testParser, &schema); fail_unless (tmp_err_code == EXIP_OK, "setSchema() returns an error code %d", tmp_err_code); // V: Parse the body of the EXI stream while(tmp_err_code == EXIP_OK) { switch (parsingData.eventCount) { case 0: fail_unless(stringEqualToAscii(parsingData.eventCode, "SD")); break; case 1: fail_unless(stringEqualToAscii(parsingData.eventCode, "SE")); fail_unless(stringEqualToAscii(parsingData.uri, "urn:foo")); fail_unless(stringEqualToAscii(parsingData.localName, "AB")); break; case 2: fail_unless(stringEqualToAscii(parsingData.eventCode, "CH")); break; case 3: fail_unless(stringEqualToAscii(parsingData.eventCode, "EE")); break; case 4: fail_unless(stringEqualToAscii(parsingData.eventCode, "ED")); break; default: // Unexpected event count caught below. break; } tmp_err_code = parseNext(&testParser); parsingData.eventCount++; } fail_unless(stringEqualToAscii(parsingData.eventCode, "ED")); fail_unless(parsingData.eventCount == 4, "Unexpected event count: %u", parsingData.eventCount); // VI: Free the memory allocated by the parser object freeAllocList(&parsingData.allocList); destroyParser(&testParser); fclose(infile); fail_unless (tmp_err_code == EXIP_PARSING_COMPLETE, "Error during parsing of the EXI body %d", tmp_err_code); }
static error_code parseDevDescMsg(char* buf, unsigned int buf_size, DevDescribtion* devDesc) { Parser lkabParser; BinaryBuffer buffer; errorCode tmp_err_code = EXIP_UNEXPECTED_ERROR; struct appDataLKAB parsingData; EXIPSchema lkab_schema; const char *schemafname = "SchemaStrict/lkab-devices-xsd.exi"; buffer.buf = buf; buffer.bufLen = buf_size; buffer.bufContent = buf_size; // Parsing steps: // I.A: First, read in the schema parseSchema(schemafname, &lkab_schema); // I: First, define an external stream for the input to the parser if any buffer.ioStrm.stream = NULL; // II: Second, initialize the parser object tmp_err_code = initParser(&lkabParser, buffer, &parsingData); if(tmp_err_code != EXIP_OK) return tmp_err_code; // III: Initialize the parsing data and hook the callback handlers to the parser object parsingData.currElementNumber = 0; parsingData.devDesc.id[0] = '\0'; parsingData.devDesc.location[0] = '\0'; parsingData.devDesc.name[0] = '\0'; parsingData.devDesc.type[0] = '\0'; parsingData.devDesc.processValue.description[0] = '\0'; parsingData.devDesc.processValue.name[0] = '\0'; parsingData.devDesc.processValue.isReadOnly = 0; parsingData.devDesc.processValue.type = Bool; lkabParser.handler.fatalError = lkab_fatalError; lkabParser.handler.error = lkab_fatalError; lkabParser.handler.startElement = lkab_startElement_desc; lkabParser.handler.stringData = lkab_stringData_desc; lkabParser.handler.endElement = lkab_endElement; lkabParser.handler.booleanData = lkab_booleanData_desc; // IV: Parse the header of the stream tmp_err_code = parseHeader(&lkabParser, FALSE); tmp_err_code = setSchema(&lkabParser, &lkab_schema); // V: Parse the body of the EXI stream while(tmp_err_code == EXIP_OK) { tmp_err_code = parseNext(&lkabParser); } // VI: Free the memory allocated by the parser object destroyParser(&lkabParser); strcpy(devDesc->id, parsingData.devDesc.id); strcpy(devDesc->location, parsingData.devDesc.location); strcpy(devDesc->name, parsingData.devDesc.name); strcpy(devDesc->type, parsingData.devDesc.type); strcpy(devDesc->processValue.description, parsingData.devDesc.processValue.description); strcpy(devDesc->processValue.name, parsingData.devDesc.processValue.name); devDesc->processValue.isReadOnly = parsingData.devDesc.processValue.isReadOnly; devDesc->processValue.type = parsingData.devDesc.processValue.type; if(tmp_err_code == EXIP_PARSING_COMPLETE) return EXIP_OK; else return tmp_err_code; }
static error_code parseIOMsg(char* buf, unsigned int buf_size, BoolValue *val) { Parser lkabParser; BinaryBuffer buffer; errorCode tmp_err_code = EXIP_UNEXPECTED_ERROR; struct appDataLKAB parsingData; EXIPSchema lkab_schema; const char *schemafname = "SchemaStrict/lkab-devices-xsd.exi"; buffer.buf = buf; buffer.bufLen = buf_size; buffer.bufContent = buf_size; // Parsing steps: // I.A: First, read in the schema parseSchema(schemafname, &lkab_schema); // I: First, define an external stream for the input to the parser if any buffer.ioStrm.stream = NULL; // II: Second, initialize the parser object tmp_err_code = initParser(&lkabParser, buffer, &parsingData); if(tmp_err_code != EXIP_OK) return tmp_err_code; // III: Initialize the parsing data and hook the callback handlers to the parser object parsingData.currElementNumber = 0; parsingData.val.quality = Good; parsingData.val.val = 0; parsingData.val.ts.year = 0; parsingData.val.ts.month = 0; parsingData.val.ts.mday = 0; parsingData.val.ts.hour = 0; parsingData.val.ts.min = 0; parsingData.val.ts.sec = 0; parsingData.val.ts.msec = 0; lkabParser.handler.fatalError = lkab_fatalError; lkabParser.handler.error = lkab_fatalError; lkabParser.handler.startElement = lkab_startElement_io; lkabParser.handler.stringData = lkab_stringData_io; lkabParser.handler.endElement = lkab_endElement; lkabParser.handler.booleanData = lkab_booleanData_io; lkabParser.handler.dateTimeData = lkab_dateTimeData; // IV: Parse the header of the stream tmp_err_code = parseHeader(&lkabParser, FALSE); tmp_err_code = setSchema(&lkabParser, &lkab_schema); // V: Parse the body of the EXI stream while(tmp_err_code == EXIP_OK) { tmp_err_code = parseNext(&lkabParser); } // VI: Free the memory allocated by the parser object destroyParser(&lkabParser); val->ts = parsingData.val.ts; val->val = parsingData.val.val; val->quality = parsingData.val.quality; if(tmp_err_code == EXIP_PARSING_COMPLETE) return EXIP_OK; else return tmp_err_code; }