void yum_xml_parse_filelists (const char *filename, CountFn count_callback, PackageFn package_callback, gpointer user_data, GError **err) { FilelistSAXContext ctx; SAXContext *sctx = &ctx.sctx; int rc; ctx.state = FILELIST_PARSER_TOPLEVEL; ctx.current_file = NULL; sax_context_init(sctx, "filelists.xml", count_callback, package_callback, user_data, err); xmlSubstituteEntitiesDefault (1); rc = xmlSAXUserParseFile (&filelist_sax_handler, &ctx, filename); if (sctx->current_package) { g_warning ("Incomplete package lost"); package_free (sctx->current_package); } if (ctx.current_file) g_free (ctx.current_file); g_string_free (sctx->text_buffer, TRUE); }
bool CXmlParser::parse(const Glib::ustring& p_file){ int l_ret; // Iniciamos el parser sax pasando los callbacks y la referencia al parser l_ret = xmlSAXUserParseFile( &m_sax_handler, this, p_file.c_str()); return l_ret; }
int main ( int argc, char **argv ) { // CHECK ARGUMENTS argCheck( argc, argv ); // SET SAX FUNCTIONS static xmlSAXHandler sax; sax.initialized = XML_SAX2_MAGIC; sax.startElement = startElement; sax.endElement = endElement; sax.characters = characters; char data; // [200] // PARSE FILE int exitCode = xmlSAXUserParseFile ( &sax, &data, fileName ); if ( exitCode != 0 ) { std::cerr << "\n\nError " << exitCode << "\n\n"; exit(1); } // END return 0; }
bool PreprocessOSM::Import(const TypeConfigRef& typeConfig, const ImportParameter& /*parameter*/, Progress& progress, const std::string& filename) { progress.SetAction(std::string("Parsing *.osm file '")+filename+"'"); Parser parser(*typeConfig, callback); xmlSAXHandler saxParser; memset(&saxParser,0,sizeof(xmlSAXHandler)); saxParser.startDocument=StartDocumentHandler; saxParser.endDocument=EndDocumentHandler; saxParser.initialized=XML_SAX2_MAGIC; saxParser.getEntity=GetEntity; saxParser.startElement=StartElement; saxParser.endElement=EndElement; saxParser.error=ErrorHandler; saxParser.fatalError=ErrorHandler; saxParser.serror=StructuredErrorHandler; xmlSAXUserParseFile(&saxParser, &parser, filename.c_str()); return true; }
int SaxHandler::parseFile( const char* filename ) { int result = xmlSAXUserParseFile( &sax, this, filename ); return result; }
void parseItem(const std::string &fileName, Background* background) { xmlSAXHandler sh = {NULL}; sh.startElement = start_background_element; sh.error = error; xmlSAXUserParseFile(&sh, background, fileName.c_str()); }
FcitxIsoCodes* FcitxXkbReadIsoCodes(const char* iso639, const char* iso3166) { xmlSAXHandler handle; memset(&handle, 0, sizeof(xmlSAXHandler)); xmlInitParser(); FcitxIsoCodes* isocodes = (FcitxIsoCodes*) fcitx_utils_malloc0(sizeof(FcitxIsoCodes)); handle.startElement = IsoCodes639HandlerStartElement; xmlSAXUserParseFile(&handle, isocodes, iso639); handle.startElement = IsoCodes3166HandlerStartElement; xmlSAXUserParseFile(&handle, isocodes, iso3166); xmlCleanupParser(); return isocodes; }
FcitxXkbRules* FcitxXkbReadRules(const char* file) { xmlSAXHandler handle; memset(&handle, 0, sizeof(xmlSAXHandler)); handle.startElement = RulesHandlerStartElement; handle.endElement = RulesHandlerEndElement; handle.characters = RulesHandlerCharacters; xmlInitParser(); FcitxXkbRules* rules = fcitx_utils_new(FcitxXkbRules); utarray_new(rules->layoutInfos, &layout_icd); utarray_new(rules->modelInfos, &model_icd); utarray_new(rules->optionGroupInfos, &option_group_icd); FcitxXkbRulesHandler ruleshandler; ruleshandler.rules = rules; ruleshandler.path = fcitx_utils_new_string_list(); ruleshandler.fromExtra = false; xmlSAXUserParseFile(&handle, &ruleshandler, file); utarray_free(ruleshandler.path); size_t extra_len = strlen(file) - strlen(".xml"); if (strcmp(file + extra_len, ".xml") == 0) { char extrafile[extra_len + strlen(".extras.xml") + 1]; memcpy(extrafile, file, extra_len); memcpy(extrafile + extra_len, ".extras.xml", sizeof(".extras.xml")); FcitxXkbRules *rulesextra = fcitx_utils_new(FcitxXkbRules); utarray_new(rulesextra->layoutInfos, &layout_icd); utarray_new(rulesextra->modelInfos, &model_icd); utarray_new(rulesextra->optionGroupInfos, &option_group_icd); ruleshandler.rules = rulesextra; ruleshandler.path = fcitx_utils_new_string_list(); xmlSAXUserParseFile(&handle, &ruleshandler, extrafile); utarray_free(ruleshandler.path); MergeRules(rules, rulesextra); } xmlCleanupParser(); return rules; }
int _login_connect(const char *username, const char *password) { Login_Parser state = { 0 }; char url[4096]; int ret; xmlInitParser(); snprintf(url, sizeof(url), "http://exchange.enlightenment.org/api/login?name=%s&password=%s", username, password); ret = xmlSAXUserParseFile(&LoginParser, &state, url); return ret; }
/*parse_cisml {{{*/ int parse_cisml(CISML_CALLBACKS_T *callbacks, void *state, const char *file_name) { CISML_PARSER_T cisml_parser; CHARBUF_T *buf; xmlSAXHandler handler; int result; DEBUG_FMT(HIGHER_VERBOSE, "CISML parser processing \"%s\"\n", file_name); //initilise parser state cisml_parser.callbacks = callbacks; cisml_parser.invoker_state = state; cisml_parser.state = PS_START; cisml_parser.multi = MP_UNDECIDED; cisml_parser.udepth = 0; //set up character buffer buf = &(cisml_parser.characters); buf->buffer = mm_malloc(sizeof(char)*10); buf->buffer[0] = '\0'; buf->size = 10; buf->pos = 0; //set up handler memset(&handler, 0, sizeof(xmlSAXHandler)); handler.startDocument = handle_cisml_start_doc; handler.endDocument = handle_cisml_end_doc; handler.characters = handle_cisml_characters; handler.startElement = handle_cisml_start_ele; handler.endElement = handle_cisml_end_ele; //parse result = xmlSAXUserParseFile(&handler, &cisml_parser, file_name); //clean up memory free(buf->buffer); buf->buffer = NULL; //check result if (result != 0) { DEBUG_FMT(HIGH_VERBOSE, "CISML parser halted due to SAX error; error code given: %d\n", result); } else { if (cisml_parser.state == PS_END) { DEBUG_MSG(HIGHER_VERBOSE, "CISML parser completed\n"); } else { DEBUG_FMT(HIGH_VERBOSE, "CISML parser did not reach end state; final state was %s\n", state_names[cisml_parser.state]); } } // return true on success return (result == 0 && cisml_parser.state == PS_END); }
void parseCheckpoint(string fileName, Checkpoint* checkpoint) { XMLCheckpointContext checkpointCtxt; /* Initialization of context */ checkpointCtxt.ctxt = checkpoint; checkpointCtxt.section = ""; xmlSAXHandler sh = {NULL}; sh.startElement = start_checkpoint_element; sh.error = error; xmlSAXUserParseFile(&sh, &checkpointCtxt, fileName.c_str()); }
void restore_file(fsp_link *link, char *filename) { xmlctxt *ctxt = calloc(sizeof(xmlctxt), 1); ctxt->link = link; ctxt->segments = fsp_link_segments(link); for (int s = 0; s < ctxt->segments; ++s) { res_count[s] = 0; } xmlSAXUserParseFile(&sax, (void *) ctxt, filename); flush_resources(ctxt); flush_quads(ctxt); free(ctxt); }
XmlItemInfoList *createXmlItemList(const char *filename, unsigned int count) { XmlItemInfoList *pItemInfoList = NULL; xmlSAXHandler saxHandler; XmlItemListParser xmlItemListParser; if (count < 1 ) { printf("Count was not at least 1.\n"); return NULL; } pItemInfoList = (XmlItemInfoList*)malloc(sizeof(XmlItemInfoList)); pItemInfoList->itemInfoList = (XmlItemInfo*)malloc((count) * sizeof(XmlItemInfo)); pItemInfoList->itemInfoCount = 0; pItemInfoList->postItemCount = 0; pItemInfoList->genMsgItemCount = 0; memset(&saxHandler, 0, sizeof(saxHandler)); memset(&xmlItemListParser, 0, sizeof(xmlItemListParser)); xmlItemListParser.pItemInfoList = pItemInfoList; xmlItemListParser.totalItemCount = count; xmlItemListParser.saxParsingState = XML_PARSE_ST_INIT; saxHandler.startElement = _saxStartElement; saxHandler.endElement = _saxEndElement; if (xmlSAXUserParseFile(&saxHandler, &xmlItemListParser, filename) < 0) { printf("xmlSAXUserParseFile() failed.\n", xmlItemListParser.saxParsingState); goto createXmlItemList_failure; } else if (xmlItemListParser.saxParsingState != XML_PARSE_ST_COMPLETE) { printf("xmlSAXUserParseFile() returned with unexpected parsing state: %d\n", xmlItemListParser.saxParsingState); goto createXmlItemList_failure; } return pItemInfoList; createXmlItemList_failure: free(pItemInfoList->itemInfoList); free(pItemInfoList); return NULL; }
int parse_xml_file(const char *filename) { ParserState my_state; /* The source of xmlSAXHandler and all the function prefixes I'm using are / in <libxml/parser.h> Use `xml2-config --cflags` to find the location of / that file. */ xmlSAXHandler xmlBinaryResolutionSAXParser = { 0, /* internalSubset */ 0, /* isStandalone */ 0, /* hasInternalSubset */ 0, /* hasExternalSubset */ 0, /* resolveEntity */ 0, /* getEntity */ 0, /* entityDecl */ 0, /* notationDecl */ 0, /* attributeDecl */ 0, /* elementDecl */ 0, /* unparsedEntityDecl */ 0, /* setDocumentLocator */ (startDocumentSAXFunc)OME_StartDocument, /* startDocument */ (endDocumentSAXFunc)OME_EndDocument, /* endDocument */ (startElementSAXFunc)OME_StartElement, /* startElement */ (endElementSAXFunc)OME_EndElement, /* endElement */ 0, /* reference */ (charactersSAXFunc)OME_Characters, /* characters */ 0, /* ignorableWhitespace */ 0, /* processingInstruction */ 0, /* comment */ (warningSAXFunc)BinDataWarning, /* warning */ (errorSAXFunc)BinDataError, /* error */ (fatalErrorSAXFunc)BinDataFatalError, /* fatalError */ 0, /* getParameterEntitySAXFunc */ 0, /* cdataBlockSAXFunc */ 0, /* externalSubsetSAXFunc */ 0, /* initialized */ }; if (xmlSAXUserParseFile(&xmlBinaryResolutionSAXParser, &my_state, filename) < 0) { return -1; } else return my_state.nOutputFiles; }
/** * Parse function */ int parse_resource(const char* path, parser_data_t* resource_data) { xmlSAXHandler saxHandler; memset(&saxHandler, 0, sizeof(saxHandler)); if(xmlSAXVersion(&saxHandler, 2)!=0){ error(2,errno,"error initializing SAX2 parser"); } saxHandler.startDocument = &start_document; saxHandler.endDocument = &end_document; saxHandler.startElementNs = &startElementNs; saxHandler.endElementNs = &endElementNs; saxHandler.characters = &chars; saxHandler.cdataBlock = &sax_nop; saxHandler.comment = &sax_nop; saxHandler.error = &sax_error; saxHandler.fatalError = &sax_fatal; saxHandler.warning = &sax_warn; // Creating context parse_context_t context; memset(&context, 0, sizeof(context)); context.devices = resource_data->devices; context.capabilities = resource_data->capabilities; context.current_devicedef = NULL; int sax_error = xmlSAXUserParseFile(&saxHandler, &context, path); if(sax_error) { error(2, 0, "SAX error parsing file: %s", path); } error(0,0, "parsed %d devices", hashmap_size(context.devices)); xmlCleanupParser(); return 0; }
// Parse the settings XML file. int parseXmlSettings(const char *filename) { int retVal = 0; xmlSAXHandler saxHandler; memset(&saxHandler, 0, sizeof(saxHandler)); saxHandler.initialized = XML_SAX2_MAGIC; saxHandler.startElement = settingsXmlStartElementCallback; saxHandler.characters = settingsXmlValueElementCallback; saxHandler.endElement = settingsXmlEndElementCallback; if(xmlSAXUserParseFile(&saxHandler, NULL, filename) < 0) retVal = 1; // Cleanup function for the XML library. xmlCleanupParser(); // This is to debug memory for regression tests xmlMemoryDump(); return retVal; }
bool read_pb_message_from_xml_file(google::protobuf::Message *document, const char *xmlfilepath) { LIBXML_TEST_VERSION xmlSAXHandler sax; init_xmlSAXHandler(&sax); xmlext::Context context; context.stack.push_back(document); context.paths.push_back(""); int result = xmlSAXUserParseFile(&sax, &context, xmlfilepath); context.paths.pop_back(); context.stack.pop_back(); if ( result != 0 ) { printf("Failed to parse document.\n" ); return false; } xmlCleanupParser(); xmlMemoryDump(); return true; }
/** * read Integrity Report (IR) by using SAX parser * */ int readIr(IR_CONTEXT *context, const char *filename) { xmlSAXHandler sax_handler; int rc = 0; memset(&sax_handler, 0, sizeof(xmlSAXHandler)); /* setup handlers */ sax_handler.startDocument = irStartDocument; sax_handler.endDocument = irEndDocument; sax_handler.startElement = irStartElement; sax_handler.endElement = irEndElement; sax_handler.characters = irCharacters; /* read IR, IR -> IML SAX */ if ((rc = xmlSAXUserParseFile(&sax_handler, (void *)context, filename)) != 0) { /* error */ // return rc; } else { /* ok */ // return rc; } /* free */ return rc; }
/* * Opens the configuration file. Any previously opened configuration file will * be closed. The ulog(3) facility will be used to log any problems. * * pathname The pathname of the file. (in) * * Returns: * pq.h:ENOERR if success. * errno.h:ENOMEM if out of memory. * errno.h:EINVAL if the pathname is NULL or if the configuration file was * invalid. * (else) <errno.h> error code. */ int cfOpen(const char* pathname) { int status; if (pathname == NULL) { log_error_q("NULL pathname"); status = EINVAL; } else { if (currState != CLOSED) cfClose(); md5 = new_MD5_CTX(); if (saxer.startDocument != myStartDocument) { saxer.startDocument = myStartDocument; saxer.startElement = myStartElement; saxer.getEntity = myGetEntity; saxer.characters = myCharacters; saxer.endElement = myEndElement; saxer.endDocument = myEndDocument; saxer.warning = xmlWarning; saxer.error = xmlError; saxer.fatalError = xmlError; } (void)xmlSAXUserParseFile(&saxer, NULL, pathname); if (currState != READY) { cfClose(); status = EINVAL; } else { prodData = realloc(prodData, prodSize); if (prodData == NULL) { log_syserr_q("Couldn't allocate %u bytes for product data", prodSize); status = ENOMEM; } else { (void)memset(prodData, 'x', prodSize); prodInfo.origin = (char*)ghostname(); prodInfo.feedtype = feedType; prodInfo.ident = prodId; prodInfo.sz = prodSize; prodXdrLen = xlen_prod_i(&prodInfo); prodXdrBuf = realloc(prodXdrBuf, prodXdrLen); if (prodXdrBuf == NULL) { log_syserr_q("Couldn't allocate %lu bytes for product XDR " "buffer", prodXdrLen); status = errno; } else { prod.data = prodData; status = ENOERR; } if (status != ENOERR) { free(prodData); prodData = NULL; } } } } return status; }
/* Parsing data sources */ static int rxml_sax_parser_parse_file(VALUE self, VALUE input) { VALUE file = rb_ivar_get(input, FILE_ATTR); return xmlSAXUserParseFile((xmlSAXHandlerPtr)&rxml_sax_hander_struct, (void *)self, StringValuePtr(file)); }
int main(int argc, char **argv) { #else int xpath_locator(int argc, char **argv) { #endif if (argc < 2) { fprintf(stderr, "Need to specify an input filename.\n" "Usage: xpath_locator file.xml xpath1 xpath2 ...\n"); exit(1); } char *xml_filename = argv[1]; num_xpaths = argc - 2; xpath_finders = (XPathFinder *) mmalloc(num_xpaths * sizeof(XPathFinder)); for (int xpath_num = 0; xpath_num < num_xpaths; ++xpath_num) { xmlChar *xpath_expr = (xmlChar *) argv[xpath_num + 2]; XPathFinder *xpath_finder = &xpath_finders[xpath_num]; xpath_finder->original = new_string(xpath_expr); xpath_finder->current_level = 0; xpath_finder->line_number = 0; xpath_finder->column_number = 0; escape_uri_slashes(xpath_expr, TRUE); int num_segs = xpath_finder->num_segs = count_chars(xpath_expr, '/'); XPathSegFinder *seg_finders = (XPathSegFinder *) mmalloc(num_segs * sizeof(XPathSegFinder)); xpath_finder->seg_finders = seg_finders; /* Extract each XPath segment */ xmlChar *seg = xstrtok(xpath_expr, "/"); int seg_num = 0; while (seg != NULL) { XPathSegFinder *seg_finder = &seg_finders[seg_num]; seg_finder->count = 0; escape_uri_slashes(seg, FALSE); seg_finder->original = new_string(seg); // Get the element local name xmlChar *lns = starts_with(seg, (const xmlChar *) "*:") ? seg + 2 : seg; const xmlChar *bracket = xstrchr(seg, '['); if (!bracket) xpath_error(xpath_num, xpath_finder, seg_num, "No bracket found"); int local_name_len = bracket - lns; xmlChar *local_name = seg_finder->local_name = new_string_n(lns, local_name_len); if (starts_with(bracket + 1, (const xmlChar *) "namespace-uri()=")) { const xmlChar *ns_start = bracket + 18; const xmlChar *ns_end = xstrchr(ns_start, '\''); if (!ns_end) xpath_error(xpath_num, xpath_finder, seg_num, "No end to the namespace URI"); seg_finder->namespace_uri = new_string_n(ns_start, ns_end - ns_start); bracket = xstrchr(ns_end, '['); if (!bracket) xpath_error(xpath_num, xpath_finder, seg_num, "No position found"); } else { seg_finder->namespace_uri = NULL; } const xmlChar *pos_start = bracket + 1; const xmlChar *pos_end = xstrchr(pos_start, ']'); if (!pos_end) xpath_error(xpath_num, xpath_finder, seg_num, "No closing bracket found"); size_t pos_str_len = pos_end - pos_start; char pos_str[10]; strncpy(pos_str, (const char *) pos_start, pos_str_len); pos_str[pos_str_len] = 0; seg_finder->position = strtol(pos_str, NULL, 10); if (seg_finder->position <= 0) xpath_error(xpath_num, xpath_finder, seg_num, "Bad position argument"); seg = xstrtok(NULL, "/"); seg_num++; } } // Initialize default handler structure for SAX 2 xmlSAXVersion(handlers, 2); handlers->startElementNs = my_startElementNs; handlers->endElementNs = my_endElementNs; parser_level = 0; // [c] parser_level is safe int res = xmlSAXUserParseFile(handlers, NULL, xml_filename); // Output the results for (int xpath_num = 0; xpath_num < num_xpaths; ++xpath_num) { XPathFinder *xpath_finder = &xpath_finders[xpath_num]; printf("%d:%d\n", xpath_finder->line_number, xpath_finder->column_number); } for (int xpath_num = 0; xpath_num < num_xpaths; ++xpath_num) { XPathFinder *xpath_finder = &xpath_finders[xpath_num]; int num_segs = xpath_finder->num_segs; XPathSegFinder *seg_finders = xpath_finder->seg_finders; for (int seg_num = 0; seg_num < num_segs; ++seg_num) { XPathSegFinder *seg_finder = &seg_finders[seg_num]; ffree(seg_finder->original); ffree(seg_finder->local_name); ffree(seg_finder->namespace_uri); } ffree(xpath_finder->seg_finders); ffree(xpath_finder->original); } ffree(xpath_finders); return 0; }