/* call-seq: * XML::Parser::Context.io(io) -> XML::Parser::Context * * Creates a new parser context based on the specified io object. * * Parameters: * * io - A ruby IO object. */ static VALUE rxml_parser_context_io(VALUE klass, VALUE io) { xmlParserCtxtPtr ctxt; xmlParserInputBufferPtr input; xmlParserInputPtr stream; input = xmlParserInputBufferCreateIO((xmlInputReadCallback) rxml_read_callback, NULL, (void*)io, XML_CHAR_ENCODING_NONE); ctxt = xmlNewParserCtxt(); if (!ctxt) { xmlFreeParserInputBuffer(input); rxml_raise(&xmlLastError); } stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE); if (!stream) { xmlFreeParserInputBuffer(input); xmlFreeParserCtxt(ctxt); rxml_raise(&xmlLastError); } inputPush(ctxt, stream); return rxml_parser_context_wrap(ctxt); }
/** * Validate epg document in bstring format against dtd */ int epg_validate(const char *xml) { if (!xml) return -1; int rc; xmlValidCtxtPtr ctx = xmlNewValidCtxt(); error_if(ctx == NULL, error, "Error creating validation context"); xmlDocPtr doc = xmlParseMemory((char *)xml, strlen(xml)); error_if(doc == NULL, error, "Error creating Parser"); /* Validate against in memory dtd */ xmlParserInputBufferPtr buf = xmlParserInputBufferCreateMem( xmltv_dtd, strlen(xmltv_dtd), XML_CHAR_ENCODING_NONE); xmlDtdPtr dtd = xmlIOParseDTD(NULL, buf, XML_CHAR_ENCODING_NONE); xmlFreeParserInputBuffer(buf); /* xmlDtdPtr dtd = xmlParseDTD(NULL, BAD_CAST epg_dtd_file); */ rc = xmlValidateDtd(ctx, doc, dtd); trace("Resultado de validaciĆ³n: %d", rc); xmlCleanupParser(); return rc; error: xmlCleanupParser(); return -1; }
static int raptor_rss_parse_start(raptor_parser *rdf_parser) { raptor_locator *locator=&rdf_parser->locator; raptor_rss_parser_context* rss_parser=(raptor_rss_parser_context*)rdf_parser->context; locator->line=1; locator->column=0; locator->byte=0; rss_parser->items_count=0; rss_parser->last=rss_parser->items=NULL; rss_parser->current_type=RAPTOR_RSS_NONE; rss_parser->prev_type=RAPTOR_RSS_NONE; rss_parser->current_field=RAPTOR_RSS_FIELD_NONE; if(rss_parser->reader) { xmlFreeTextReader(rss_parser->reader); rss_parser->reader=NULL; } if(rss_parser->input) { xmlFreeParserInputBuffer(rss_parser->input); rss_parser->input=NULL; } return 0; }
static void on_downloaded (SummerWebBackend *web, gchar *save_path, gchar *save_data, GError *error, gpointer user_data) { g_return_if_fail (SUMMER_IS_FEED (user_data)); SummerFeed *self = SUMMER_FEED (user_data); SummerFeedPrivate *priv = self->priv; if (error == NULL) { SummerFeedParser *parsers[] = { SUMMER_FEED_PARSER (summer_atom_parser_new ()), SUMMER_FEED_PARSER (summer_rss2_parser_new ())}; xmlParserInputBufferPtr buffer = xmlParserInputBufferCreateMem (save_data, strlen (save_data), 0); xmlTextReaderPtr reader = xmlNewTextReader (buffer, priv->url); priv->feed_data = summer_feed_parser_parse (parsers, sizeof (parsers) / sizeof (*parsers), reader); priv->feed_data->url = priv->url; unsigned int i; for (i = 0; i < sizeof (parsers) / sizeof (*parsers); i++) { g_object_unref (parsers[i]); } xmlTextReaderClose (reader); xmlFreeTextReader (reader); xmlFreeParserInputBuffer (buffer); g_signal_emit_by_name (self, "new-entries"); } g_object_unref (web); if (priv->frequency <= 0) g_object_unref (self); }
static void testCharRanges(void) { char data[5]; xmlParserCtxtPtr ctxt; xmlParserInputBufferPtr buf; xmlParserInputPtr input; memset(data, 0, 5); /* * Set up a parsing context using the above data buffer as * the current input source. */ ctxt = xmlNewParserCtxt(); if (ctxt == NULL) { fprintf(stderr, "Failed to allocate parser context\n"); return; } buf = xmlParserInputBufferCreateStatic(data, sizeof(data), XML_CHAR_ENCODING_NONE); if (buf == NULL) { fprintf(stderr, "Failed to allocate input buffer\n"); goto error; } input = xmlNewInputStream(ctxt); if (input == NULL) { xmlFreeParserInputBuffer(buf); goto error; } input->filename = NULL; input->buf = buf; input->cur = input->base = xmlBufContent(input->buf->buffer); input->end = input->base + 4; inputPush(ctxt, input); printf("testing char range: 1"); fflush(stdout); testCharRangeByte1(ctxt, data); printf(" 2"); fflush(stdout); testCharRangeByte2(ctxt, data); printf(" 3"); fflush(stdout); testCharRangeByte3(ctxt, data); printf(" 4"); fflush(stdout); testCharRangeByte4(ctxt, data); printf(" done\n"); fflush(stdout); error: xmlFreeParserCtxt(ctxt); }
bool HHVM_METHOD(XMLReader, XML, const String& source, const Variant& encoding /*= null_variant*/, int64_t options /*= 0*/) { auto* data = Native::data<XMLReader>(this_); const String& str_encoding = encoding.isNull() ? null_string : encoding.toString(); xmlParserInputBufferPtr inputbfr = xmlParserInputBufferCreateMem( source.c_str(), source.size(), XML_CHAR_ENCODING_NONE); if (inputbfr != nullptr) { char *uri = nullptr; String directory = g_context->getCwd(); if (!directory.empty()) { if (directory[directory.size() - 1] != '/') { directory += "/"; } uri = (char *) xmlCanonicPath((const xmlChar *) directory.c_str()); } xmlTextReaderPtr reader = xmlNewTextReader(inputbfr, uri); if (reader != nullptr) { int ret = 0; #if LIBXML_VERSION >= 20628 ret = xmlTextReaderSetup(reader, nullptr, uri, str_encoding.data(), options); #endif if (ret == 0) { data->m_ptr = reader; data->m_input = inputbfr; if (uri) { xmlFree(uri); } return true; } } if (uri) { xmlFree(uri); } } if (inputbfr) { xmlFreeParserInputBuffer(inputbfr); } raise_warning("Unable to load source data"); return false; }
void c_XMLReader::close_impl() { if (m_ptr) { xmlFreeTextReader(m_ptr); m_ptr = NULL; } if (m_input) { xmlFreeParserInputBuffer(m_input); m_input = NULL; } if (m_schema) { xmlRelaxNGFree((xmlRelaxNGPtr) m_schema); m_schema = NULL; } }
static int raptor_rss_parse_chunk(raptor_parser* rdf_parser, const unsigned char *s, size_t len, int is_end) { raptor_rss_parser_context* rss_parser=(raptor_rss_parser_context*)rdf_parser->context; int ret; if(!rss_parser->reader) { unsigned char *uri=raptor_uri_as_string(rdf_parser->base_uri); rss_parser->input=xmlParserInputBufferCreateMem((const char*)s, len, XML_CHAR_ENCODING_NONE); rss_parser->reader=xmlNewTextReader(rss_parser->input, (const char*)uri); xmlTextReaderSetErrorHandler(rss_parser->reader, raptor_rss_error_handler, rdf_parser); } else if(s && len) xmlParserInputBufferPush(rss_parser->input, len, (const char*)s); if(!is_end) return 0; ret = xmlTextReaderRead(rss_parser->reader); while (ret == 1) { if(rdf_parser->failed) break; raptor_rss_parser_processNode(rdf_parser); ret = xmlTextReaderRead(rss_parser->reader); } xmlFreeTextReader(rss_parser->reader); rss_parser->reader=NULL; xmlFreeParserInputBuffer(rss_parser->input); rss_parser->input=NULL; if(rdf_parser->failed) return 1; /* turn strings into URIs, move things around if needed */ raptor_rss_insert_identifiers(rdf_parser); /* generate the triples */ raptor_rss_emit(rdf_parser); return (ret != 0); }
/* * Read xmlDocument from server */ static xmlDoc * js_document_read (xmlParserCtxtPtr ctxt, js_session_t *jsp, const char *url, const char *encoding, int options) { xmlParserInputBufferPtr input; xmlParserInputPtr stream; xmlDoc *docp; if (jsp == NULL || ctxt == NULL || jsp->js_state == JSS_DEAD) return NULL; xmlCtxtReset(ctxt); input = js_buffer_create(jsp, XML_CHAR_ENCODING_NONE); if (input == NULL) return NULL; input->closecallback = NULL; stream = xmlNewIOInputStream(ctxt, input, XML_CHAR_ENCODING_NONE); if (stream == NULL) { xmlFreeParserInputBuffer(input); return NULL; } inputPush(ctxt, stream); xmlCtxtUseOptions(ctxt, options); xmlCharEncodingHandlerPtr hdlr; if (encoding && ((hdlr = xmlFindCharEncodingHandler(encoding)) != NULL)) xmlSwitchToEncoding(ctxt, hdlr); if (url != NULL && ctxt->input != NULL && ctxt->input->filename == NULL) ctxt->input->filename = (char *) xmlStrdup((const xmlChar *) url); /* * All right. The stage is set, time to open the curtain and let * the show begin. */ xmlParseDocument(ctxt); docp = ctxt->myDoc; ctxt->myDoc = NULL; if (docp && !ctxt->wellFormed) { xmlFreeDoc(docp); docp = NULL; } return docp; }
xmlreader _xmlreader_from_string(const char *buffer, int size, const char *URL, const char *encoding, int options) { xmlTextReaderPtr reader; xmlParserInputBufferPtr buf; buf = xmlParserInputBufferCreateMem(buffer, size, XML_CHAR_ENCODING_NONE); if (buf == NULL) return (NULL); reader = xmlNewTextReader(buf, URL); if (reader == NULL) { xmlFreeParserInputBuffer(buf); return (NULL); } xmlTextReaderSetup(reader, buf, URL, encoding, options); return (reader); }
void XMLReader::close() { SYNC_VM_REGS_SCOPED(); if (m_ptr) { xmlFreeTextReader(m_ptr); m_ptr = nullptr; } if (m_input) { xmlFreeParserInputBuffer(m_input); m_input = nullptr; } if (m_schema) { xmlRelaxNGFree((xmlRelaxNGPtr) m_schema); m_schema = nullptr; } }
bool c_XMLReader::t_xml(const String& source, const String& encoding /*= null_string*/, int64_t options /*= 0*/) { xmlParserInputBufferPtr inputbfr = xmlParserInputBufferCreateMem(source.c_str(), source.size(), XML_CHAR_ENCODING_NONE); if (inputbfr != NULL) { char *uri = NULL; String directory = g_context->getCwd(); if (!directory.empty()) { if (directory[directory.size() - 1] != '/') { directory += "/"; } uri = (char *) xmlCanonicPath((const xmlChar *) directory.c_str()); } xmlTextReaderPtr reader = xmlNewTextReader(inputbfr, uri); if (reader != NULL) { int ret = 0; #if LIBXML_VERSION >= 20628 ret = xmlTextReaderSetup(reader, NULL, uri, encoding.data(), options); #endif if (ret == 0) { m_ptr = reader; m_input = inputbfr; if (uri) { xmlFree(uri); } return true; } } if (uri) { xmlFree(uri); } } if (inputbfr) { xmlFreeParserInputBuffer(inputbfr); } raise_warning("Unable to load source data"); return false; }
/* {{{ xmlreader_free_resources */ static void xmlreader_free_resources(xmlreader_object *intern) { if (intern) { if (intern->input) { xmlFreeParserInputBuffer(intern->input); intern->input = NULL; } if (intern->ptr) { xmlFreeTextReader(intern->ptr); intern->ptr = NULL; } #ifdef LIBXML_SCHEMAS_ENABLED if (intern->schema) { xmlRelaxNGFree((xmlRelaxNGPtr) intern->schema); intern->schema = NULL; } #endif } }
bool c_XMLReader::t_xml(CStrRef source, CStrRef encoding /*= null_string*/, int64 options /*= 0*/) { INSTANCE_METHOD_INJECTION_BUILTIN(XMLReader, XMLReader::xml); xmlParserInputBufferPtr inputbfr = xmlParserInputBufferCreateMem(source.c_str(), source.size(), XML_CHAR_ENCODING_NONE); if (inputbfr != NULL) { char *uri = NULL; String directory = g_context->getCwd(); if (!directory.empty()) { if (directory[directory.size() - 1] != '/') { directory += '/'; } uri = (char *) xmlCanonicPath((const xmlChar *) directory.c_str()); } xmlTextReaderPtr reader = xmlNewTextReader(inputbfr, uri); if (reader != NULL) { int ret = xmlTextReaderSetup(reader, NULL, uri, encoding.data(), options); if (ret == 0) { m_ptr = reader; m_input = inputbfr; if (uri) { xmlFree(uri); } return true; } } if (uri) { xmlFree(uri); } } if (inputbfr) { xmlFreeParserInputBuffer(inputbfr); } raise_warning("Unable to load source data"); return false; }
void CWatchBSP::Reset(){ if ( m_pInSocket ) { Net_Disconnect( m_pInSocket ); m_pInSocket = NULL; } if ( m_pListenSocket ) { Net_Disconnect( m_pListenSocket ); m_pListenSocket = NULL; } if ( m_xmlInputBuffer ) { xmlFreeParserInputBuffer( m_xmlInputBuffer ); m_xmlInputBuffer = NULL; } if ( m_xmlParserCtxt ) { xmlFreeParserCtxt( m_xmlParserCtxt ); m_xmlParserCtxt = NULL; } m_eState = EIdle; }
static void raptor_rss_parse_terminate(raptor_parser *rdf_parser) { raptor_rss_parser_context *rss_parser=(raptor_rss_parser_context*)rdf_parser->context; int i; if(rss_parser->reader) xmlFreeTextReader(rss_parser->reader); if(rss_parser->input) xmlFreeParserInputBuffer(rss_parser->input); raptor_rss_items_free(rss_parser); for(i=0; i< RAPTOR_RSS_COMMON_SIZE; i++) raptor_item_free(&rss_parser->common[i]); for(i=0; i<RSS_NAMESPACES_SIZE;i++) { if(rss_parser->namespace_uris[i]) raptor_free_uri(rss_parser->namespace_uris[i]); } for(i=0; i< RAPTOR_RSS_N_CONCEPTS; i++) { raptor_uri* concept_uri=rss_parser->concepts[i]; if(concept_uri) { raptor_free_uri(concept_uri); rss_parser->concepts[i]=NULL; } } for(i=0; i< RAPTOR_RSS_COMMON_SIZE; i++) { if(raptor_rss_types_info[i].uri) raptor_free_uri(raptor_rss_types_info[i].uri); } for(i=0; i< RAPTOR_RSS_FIELDS_SIZE; i++) { if(raptor_rss_fields_info[i].uri) raptor_free_uri(raptor_rss_fields_info[i].uri); } }
/** * xmlXIncludeLoadTxt: * @ctxt: the XInclude context * @url: the associated URL * @nr: the xinclude node number * * Load the content, and store the result in the XInclude context */ static void xmlXIncludeLoadTxt(xmlXIncludeCtxtPtr ctxt, const xmlChar *url, int nr) { xmlParserInputBufferPtr buf; xmlNodePtr node; xmlURIPtr uri; xmlChar *URL; int i; /* * Check the URL and remove any fragment identifier */ uri = xmlParseURI((const char *)url); if (uri == NULL) { xmlGenericError(xmlGenericErrorContext, "XInclude: invalid value URI %s\n", url); return; } if (uri->fragment != NULL) { xmlGenericError(xmlGenericErrorContext, "XInclude: fragment identifier forbidden for text: %s\n", uri->fragment); xmlFreeURI(uri); return; } URL = xmlSaveUri(uri); xmlFreeURI(uri); if (URL == NULL) { xmlGenericError(xmlGenericErrorContext, "XInclude: invalid value URI %s\n", url); return; } /* * Handling of references to the local document are done * directly through ctxt->doc. */ if (URL[0] == 0) { xmlGenericError(xmlGenericErrorContext, "XInclude: text serialization of document not available\n"); xmlFree(URL); return; } /* * Prevent reloading twice the document. */ for (i = 0; i < ctxt->txtNr; i++) { if (xmlStrEqual(URL, ctxt->txturlTab[i])) { node = xmlCopyNode(ctxt->txtTab[i], 1); goto loaded; } } /* * Load it. * Issue 62: how to detect the encoding */ buf = xmlParserInputBufferCreateFilename((const char *)URL, 0); if (buf == NULL) { xmlGenericError(xmlGenericErrorContext, "XInclude: could not load %s\n", URL); xmlFree(URL); return; } node = xmlNewText(NULL); /* * Scan all chars from the resource and add the to the node */ while (xmlParserInputBufferRead(buf, 128) > 0) { int len; const xmlChar *content; content = xmlBufferContent(buf->buffer); len = xmlBufferLength(buf->buffer); for (i = 0; i < len; i++) { /* * TODO: if the encoding issue is solved, scan UTF8 chars instead */ if (!IS_CHAR(content[i])) { xmlGenericError(xmlGenericErrorContext, "XInclude: %s contains invalid char %d\n", URL, content[i]); } else { xmlNodeAddContentLen(node, &content[i], 1); } } xmlBufferShrink(buf->buffer, len); } xmlFreeParserInputBuffer(buf); xmlXIncludeAddTxt(ctxt, node, URL); loaded: /* * Add the element as the replacement copy. */ ctxt->repTab[nr] = node; xmlFree(URL); }
void OtNtParallels::read(const gchar * filename) { // Get contents of the data file. Bail out if not there. ustring xmlfilename = gw_build_filename(Directories->get_package_data(), filename); if (!g_file_test(xmlfilename.c_str(), G_FILE_TEST_IS_REGULAR)) return; gchar *contents; g_file_get_contents(xmlfilename.c_str(), &contents, NULL, NULL); /* Read the xml data. Example: <section title="The Descendants of Noah's Sons"> <set> <reference book="Genesis" chapter="10" verse="2-4"/> <reference book="1 Chronicles" chapter="1" verse="5-7"/> </set> <set> <reference book="Genesis" chapter="10" verse="6-8"/> <reference book="1 Chronicles" chapter="1" verse="8-10"/> </set> <set> <reference book="Genesis" chapter="10" verse="13-18"/> <reference book="1 Chronicles" chapter="1" verse="11-16"/> </set> </section> */ xmlParserInputBufferPtr inputbuffer; inputbuffer = xmlParserInputBufferCreateMem(contents, strlen(contents), XML_CHAR_ENCODING_NONE); xmlTextReaderPtr reader = xmlNewTextReader(inputbuffer, NULL); if (reader) { char *opening_element = NULL; OtNtParallelSection parallelsection(0); OtNtParallelSet parallelset(0); while ((xmlTextReaderRead(reader) == 1)) { switch (xmlTextReaderNodeType(reader)) { case XML_READER_TYPE_ELEMENT: { opening_element = (char *)xmlTextReaderName(reader); if (!strcmp(opening_element, "section")) { parallelsection.sets.clear(); parallelsection.title.clear(); char *attribute; attribute = (char *)xmlTextReaderGetAttribute(reader, BAD_CAST "title"); if (attribute) { parallelsection.title = attribute; free(attribute); } } if (!strcmp(opening_element, "set")) { parallelset.references.clear(); } if (!strcmp(opening_element, "reference")) { Reference ref(0); char *attribute; attribute = (char *)xmlTextReaderGetAttribute(reader, BAD_CAST "book"); if (attribute) { ref.book = books_english_to_id(attribute); free(attribute); } attribute = (char *)xmlTextReaderGetAttribute(reader, BAD_CAST "chapter"); if (attribute) { ref.chapter = convert_to_int(attribute); free(attribute); } attribute = (char *)xmlTextReaderGetAttribute(reader, BAD_CAST "verse"); if (attribute) { ref.verse = attribute; free(attribute); } parallelset.references.push_back(ref); } break; } case XML_READER_TYPE_TEXT: { char *text = (char *)xmlTextReaderValue(reader); if (text) { free(text); } break; } case XML_READER_TYPE_END_ELEMENT: { char *closing_element = (char *)xmlTextReaderName(reader); if (!strcmp(closing_element, "set")) { parallelsection.sets.push_back(parallelset); } if (!strcmp(closing_element, "section")) { sections.push_back(parallelsection); } break; } } } } // Free memory. if (reader) xmlFreeTextReader(reader); if (inputbuffer) xmlFreeParserInputBuffer(inputbuffer); if (contents) g_free(contents); }
/** * Parse an Index from tape and populate the vol->index->root virtual dentry tree * with the nodes found during the scanning. * If a file mark is encountered at the end of the Index, the tape is positioned before * the file mark. * @param eod_pos EOD block position for the current partition, or 0 to assume EOD will not be * encountered during parsing. * @param vol LTFS volume. * @return 0 on success, 1 if parsing succeeded but no file mark was encountered, * or a negative value on error. */ int xml_schema_from_tape(uint64_t eod_pos, struct ltfs_volume *vol) { int ret; struct tc_position current_pos; struct xml_input_tape *ctx; xmlParserInputBufferPtr read_buf; xmlTextReaderPtr reader; xmlDocPtr doc; CHECK_ARG_NULL(vol, -LTFS_NULL_ARG); ret = tape_get_position(vol->device, ¤t_pos); if (ret < 0) { ltfsmsg(LTFS_ERR, "17013E", ret); return ret; } /* Create output callback context data structure. */ ctx = malloc(sizeof(struct xml_input_tape)); if (! ctx) { ltfsmsg(LTFS_ERR, "10001E", "xml_schema_from_tape: ctx"); return -LTFS_NO_MEMORY; } ctx->buf = malloc(vol->label->blocksize + LTFS_CRC_SIZE); if (! ctx->buf) { ltfsmsg(LTFS_ERR, "10001E", "xml_schema_from_tape: ctx->buf"); free(ctx); return -LTFS_NO_MEMORY; } ctx->vol = vol; ctx->current_pos = current_pos.block; ctx->eod_pos = eod_pos; ctx->saw_small_block = false; ctx->saw_file_mark = false; ctx->buf_size = vol->label->blocksize; ctx->buf_start = 0; ctx->buf_used = 0; /* Create input buffer pointer. */ read_buf = xmlParserInputBufferCreateIO(xml_input_tape_read_callback, xml_input_tape_close_callback, ctx, XML_CHAR_ENCODING_NONE); if (! read_buf) { ltfsmsg(LTFS_ERR, "17014E"); free(ctx->buf); free(ctx); return -LTFS_LIBXML2_FAILURE; } /* Create XML reader. */ reader = xmlNewTextReader(read_buf, NULL); if (! reader) { ltfsmsg(LTFS_ERR, "17015E"); xmlFreeParserInputBuffer(read_buf); return -LTFS_LIBXML2_FAILURE; } /* Workaround for old libxml2 version on OS X 10.5. See comment in xml_schema_from_file() * for details. */ doc = xmlTextReaderCurrentDoc(reader); /* Generate the Index. */ ret = _xml_parse_schema(reader, vol->index, vol); if (ret < 0) { ltfsmsg(LTFS_ERR, "17016E"); if ((ret != -LTFS_UNSUPPORTED_INDEX_VERSION)&&( ret != -LTFS_SYMLINK_CONFLICT)) { if (ret == -LTFS_NO_MEMORY) ret = -LTFS_NO_MEMORY; else ret = -LTFS_INDEX_INVALID; } } else if (ret == 0) { if( ! ctx->saw_file_mark) ret = 1; } if (doc) xmlFreeDoc(doc); xmlFreeTextReader(reader); xmlFreeParserInputBuffer(read_buf); #ifdef DEBUG /* dump the tree if it isn't too large */ if (ret >= 0 && vol->index->file_count < 1000) fs_dump_tree(vol->index->root); #endif return ret; }
void kjv_import_sword (const ustring& textfile, const ustring& database) { // Show the progress. KJV has 31102 verses. ProgressWindow progresswindow (_("Importing King James Bible"), false); progresswindow.set_iterate (0, 1, 31102); gchar * contents; g_file_get_contents(textfile.c_str(), &contents, NULL, NULL); if (!contents) return; // Create the database, put it in fast mode. unix_unlink (database.c_str()); sqlite3 *db; sqlite3_open(database.c_str(), &db); sqlite3_exec(db, "create table kjv (book integer, chapter integer, verse integer, item integer, fragment text, lemma text);", NULL, NULL, NULL); sqlite3_exec(db, "PRAGMA synchronous=OFF;", NULL, NULL, NULL); // Parse input. xmlParserInputBufferPtr inputbuffer; inputbuffer = xmlParserInputBufferCreateMem(contents, strlen (contents), XML_CHAR_ENCODING_NONE); xmlTextReaderPtr reader = xmlNewTextReader(inputbuffer, NULL); if (reader) { bool within_relevant_element = false; Reference reference (0, 0, "0"); unsigned int item_number = 0; ustring textfragment; ustring lemmata; while ((xmlTextReaderRead(reader) == 1)) { switch (xmlTextReaderNodeType(reader)) { case XML_READER_TYPE_ELEMENT: { xmlChar *element_name = xmlTextReaderName(reader); // Deal with a verse element. if (!xmlStrcmp(element_name, BAD_CAST "verse")) { progresswindow.iterate(); char *attribute; attribute = (char *)xmlTextReaderGetAttribute(reader, BAD_CAST "osisID"); if (attribute) { Parse parse (attribute, false, "."); if (parse.words.size() == 3) { reference.assign(books_osis_to_id (parse.words[0]), // book convert_to_int (parse.words[1]), // chapter parse.words[2]); // verse } else { gw_critical (attribute); } free(attribute); } item_number = 0; } // Deal with a w element. if (!xmlStrcmp(element_name, BAD_CAST "w")) { within_relevant_element = true; item_number++; textfragment.clear(); lemmata.clear(); char *attribute; attribute = (char *)xmlTextReaderGetAttribute(reader, BAD_CAST "lemma"); if (attribute) { lemmata = attribute; free(attribute); } } break; } case XML_READER_TYPE_TEXT: { if (within_relevant_element) { xmlChar *text = xmlTextReaderValue(reader); if (text) { textfragment = (const char *)text; xmlFree(text); textfragment = textfragment.casefold(); } } break; } case XML_READER_TYPE_END_ELEMENT: { xmlChar *element_name = xmlTextReaderName(reader); if (!xmlStrcmp(element_name, BAD_CAST "w")) { replace_text (lemmata, "strong:", ""); char *sql; sql = g_strdup_printf("insert into kjv values (%d, %d, %d, %d, '%s', '%s');", reference.book_get(), reference.chapter_get(), convert_to_int (reference.verse_get()), item_number, double_apostrophy (textfragment).c_str(), lemmata.c_str()); sqlite3_exec(db, sql, NULL, NULL, NULL); g_free(sql); within_relevant_element = false; } break; } } } } if (reader) xmlFreeTextReader(reader); if (inputbuffer) xmlFreeParserInputBuffer(inputbuffer); // Close database. sqlite3_close(db); // Free xml data. g_free(contents); }
/* Taken from libxml2, xmlSAXParseMemoryWithData */ xmlDocPtr mxslt_doc_xml_parse(mxslt_doc_t * document, xmlParserInputBufferPtr buf, char * localfile) { xmlParserCtxtPtr ctx; xmlParserInputPtr input; xmlDocPtr retval; mxslt_doc_debug_print(document, MXSLT_DBG_LIBXML | MXSLT_DBG_DEBUG | MXSLT_DBG_VERBOSE0, "xml_parse/xmlCreateMemoryParserCtxt -- replacing entities: %08x\n", xmlSubstituteEntitiesDefaultValue); if(buf == NULL) return NULL; /* SNIPPET: This is a good mix&shake of * xmlCreateMemoryParserCtxt, xmlCreateFileParserCtxt */ ctx=xmlNewParserCtxt(); if(ctx == NULL) { xmlFreeParserInputBuffer(buf); return NULL; } #if LIBXML_VERSION >= 20600 xmlCtxtUseOptions(ctx, MXSLT_XSLT_OPTIONS); #endif /* Default are no longer changed... since 2.6.0 they * are completely ignored, leading to broken modxslt :|| */ if(ctx->sax) { ctx->sax->resolveEntity=mxslt_sax_resolve_entity; ctx->sax->processingInstruction=mxslt_sax_processing_instruction; } /* Remember which document we are parsing * in this context */ /* ctx->_private=document; */ input=xmlNewInputStream(ctx); if(input == NULL) { xmlFreeParserInputBuffer(buf); xmlFreeParserCtxt(ctx); return NULL; } input->filename=(char *)xmlCanonicPath((xmlChar *)localfile); if(input->filename == NULL) { xmlFreeParserCtxt(ctx); xmlFreeParserInputBuffer(buf); xmlFreeInputStream(input); return NULL; } input->buf=buf; #if LIBXML_VERSION < 20900 input->base=input->buf->buffer->content; input->cur=input->buf->buffer->content; input->end=&input->buf->buffer->content[input->buf->buffer->use]; #else /* With libxml2 2.9.0, the buffer struct can only be accessed through * methods. */ input->base=xmlBufContent(input->buf->buffer); input->cur=xmlBufContent(input->buf->buffer); input->end=xmlBufEnd(input->buf->buffer); #endif inputPush(ctx, input); if(ctx->directory == NULL) ctx->directory=xmlParserGetDirectory(localfile); /* END SNIPPET */ if(mxslt_doc_debug_level(document, MXSLT_DBG_LIBXML | MXSLT_DBG_DEBUG | MXSLT_DBG_VERBOSE2)) mxslt_doc_dump_ctx(ctx, document); /* Parse document */ xmlParseDocument(ctx); if(ctx->wellFormed) retval=ctx->myDoc; else { retval=NULL; xmlFreeDoc(ctx->myDoc); ctx->myDoc=NULL; } xmlFreeParserCtxt(ctx); /* xmlFreeParserInputBuffer(buf); */ return retval; }
Stylesheet::Stylesheet(const ustring & name_in) { gw_message("Creating Stylesheet called '" + name_in + "'"); // Save the sheet's name. name = name_in; // If the name is empty, read from the template. ustring filename = stylesheet_xml_filename(name); if (name.empty()) filename = stylesheet_xml_template_filename(); // Read the xml data, bail out on failure. gchar *contents; g_file_get_contents(filename.c_str(), &contents, NULL, NULL); if (contents == NULL) { gw_critical(_("Failure reading stylesheet ") + filename); return; } // Parse the stylesheet. xmlParserInputBufferPtr inputbuffer; inputbuffer = xmlParserInputBufferCreateMem(contents, strlen(contents), XML_CHAR_ENCODING_NONE); ustring value; xmlTextReaderPtr reader = xmlNewTextReader(inputbuffer, NULL); if (reader) { StyleV2 * style = NULL; while ((xmlTextReaderRead(reader) == 1)) { switch (xmlTextReaderNodeType(reader)) { case XML_READER_TYPE_ELEMENT: { xmlChar *element_name = xmlTextReaderName(reader); if (!xmlStrcmp(element_name, BAD_CAST "style")) { char *attribute = (char *)xmlTextReaderGetAttribute(reader, BAD_CAST "marker"); if (attribute) { style = new StyleV2 (0); style->marker = attribute; free(attribute); } } free(element_name); element_name = NULL; break; } case XML_READER_TYPE_TEXT: { xmlChar *text = xmlTextReaderValue(reader); if (text) { value = (gchar *) text; xmlFree(text); } break; } case XML_READER_TYPE_END_ELEMENT: { xmlChar *element_name = xmlTextReaderName(reader); if (style) { if (!xmlStrcmp(element_name, BAD_CAST "marker")) style->marker = value; if (!xmlStrcmp(element_name, BAD_CAST "name")) style->name = value; if (!xmlStrcmp(element_name, BAD_CAST "info")) style->info = value; if (!xmlStrcmp(element_name, BAD_CAST "type")) style->type = (StyleType) convert_to_int(value); if (!xmlStrcmp(element_name, BAD_CAST "subtype")) style->subtype = convert_to_int(value); if (!xmlStrcmp(element_name, BAD_CAST "fontsize")) style->fontsize = convert_to_double(value); if (!xmlStrcmp(element_name, BAD_CAST "italic")) style->italic = value; if (!xmlStrcmp(element_name, BAD_CAST "bold")) style->bold = value; if (!xmlStrcmp(element_name, BAD_CAST "underline")) style->underline = value; if (!xmlStrcmp(element_name, BAD_CAST "smallcaps")) style->smallcaps = value; if (!xmlStrcmp(element_name, BAD_CAST "superscript")) style->superscript = convert_to_bool(value); if (!xmlStrcmp(element_name, BAD_CAST "justification")) style->justification = value; if (!xmlStrcmp(element_name, BAD_CAST "spacebefore")) style->spacebefore = convert_to_double(value); if (!xmlStrcmp(element_name, BAD_CAST "spaceafter")) style->spaceafter = convert_to_double(value); if (!xmlStrcmp(element_name, BAD_CAST "leftmargin")) style->leftmargin = convert_to_double(value); if (!xmlStrcmp(element_name, BAD_CAST "rightmargin")) style->rightmargin = convert_to_double(value); if (!xmlStrcmp(element_name, BAD_CAST "firstlineindent")) style->firstlineindent = convert_to_double(value); if (!xmlStrcmp(element_name, BAD_CAST "spancolumns")) style->spancolumns = convert_to_bool(value); if (!xmlStrcmp(element_name, BAD_CAST "color")) style->color = convert_to_int(value); if (!xmlStrcmp(element_name, BAD_CAST "print")) style->print = convert_to_bool(value); if (!xmlStrcmp(element_name, BAD_CAST "userbool1")) style->userbool1 = convert_to_bool(value); if (!xmlStrcmp(element_name, BAD_CAST "userbool2")) style->userbool2 = convert_to_bool(value); if (!xmlStrcmp(element_name, BAD_CAST "userbool3")) style->userbool3 = convert_to_bool(value); if (!xmlStrcmp(element_name, BAD_CAST "userint1")) style->userint1 = convert_to_int(value); if (!xmlStrcmp(element_name, BAD_CAST "userint2")) style->userint2 = convert_to_int(value); if (!xmlStrcmp(element_name, BAD_CAST "userint3")) style->userint3 = convert_to_int(value); if (!xmlStrcmp(element_name, BAD_CAST "userstring1")) style->userstring1 = value; if (!xmlStrcmp(element_name, BAD_CAST "userstring2")) style->userstring2 = value; if (!xmlStrcmp(element_name, BAD_CAST "userstring3")) style->userstring3 = value; } value.clear(); if (!xmlStrcmp(element_name, BAD_CAST "style")) { if (style) { insert (style); style = NULL; } } free(element_name); element_name = NULL; break; } } } } // Free memory. if (reader) xmlFreeTextReader(reader); if (inputbuffer) xmlFreeParserInputBuffer(inputbuffer); if (contents) g_free(contents); }
void OTQuotations::read() { // Get contents of the data file. Bail out if not there. ustring xmlfilename = gw_build_filename(Directories->get_package_data(), "ot-quotations-in-nt.xml"); if (!g_file_test(xmlfilename.c_str(), G_FILE_TEST_IS_REGULAR)) return; gchar *contents; g_file_get_contents(xmlfilename.c_str(), &contents, NULL, NULL); /* Read the xml data. Example: <set> <nt book="Matthew" chapter="1" verse="23"/> <ot book="Isaiah" chapter="8" verse="8"/> <ot book="Isaiah" chapter="8" verse="10"/> <lxx>1</lxx> </set> */ xmlParserInputBufferPtr inputbuffer; inputbuffer = xmlParserInputBufferCreateMem(contents, strlen(contents), XML_CHAR_ENCODING_NONE); xmlTextReaderPtr reader = xmlNewTextReader(inputbuffer, NULL); if (reader) { char *opening_element = NULL; OTQuotation quotation(0); while ((xmlTextReaderRead(reader) == 1)) { switch (xmlTextReaderNodeType(reader)) { case XML_READER_TYPE_ELEMENT: { opening_element = (char *)xmlTextReaderName(reader); if (!strcmp(opening_element, "set")) { quotation.reference.clear(); quotation.referents.clear(); quotation.lxx = false; free(opening_element); opening_element = NULL; // not used next loop iteration } else if (!strcmp(opening_element, "nt") || !strcmp(opening_element, "ot")) { Reference ref; char *attribute; attribute = (char *)xmlTextReaderGetAttribute(reader, BAD_CAST "book"); if (attribute) { ref.book_set(books_english_to_id(attribute)); free(attribute); } attribute = (char *)xmlTextReaderGetAttribute(reader, BAD_CAST "chapter"); if (attribute) { ref.chapter_set(convert_to_int(attribute)); free(attribute); } attribute = (char *)xmlTextReaderGetAttribute(reader, BAD_CAST "verse"); if (attribute) { ref.verse_set(attribute); free(attribute); } if (!strcmp(opening_element, "nt")) { quotation.reference.assign(ref); } if (!strcmp(opening_element, "ot")) { quotation.referents.push_back(ref); } // cannot free(opening_element) because it will be used next loop iter in following switch case } break; } case XML_READER_TYPE_TEXT: { char *text = (char *)xmlTextReaderValue(reader); if (opening_element && text) { if (!strcmp(opening_element, "lxx")) { quotation.lxx = convert_to_bool(text); } free(text); free(opening_element); opening_element = NULL; } break; } case XML_READER_TYPE_END_ELEMENT: { char *closing_element = (char *)xmlTextReaderName(reader); if (!strcmp(closing_element, "set")) { quotations_nt_order.push_back(quotation); } free(closing_element); break; } } } } // Free memory. if (reader) xmlFreeTextReader(reader); if (inputbuffer) xmlFreeParserInputBuffer(inputbuffer); if (contents) g_free(contents); }
InputBuffer::~InputBuffer() { xmlFreeParserInputBuffer(_buf); _buf = nullptr; }
/* {{{ proto boolean XMLReader::XML(string source [, string encoding [, int options]]) Sets the string that the XMLReader will parse. */ PHP_METHOD(xmlreader, XML) { zval *id; size_t source_len = 0, encoding_len = 0; zend_long options = 0; xmlreader_object *intern = NULL; char *source, *uri = NULL, *encoding = NULL; int resolved_path_len, ret = 0; char *directory=NULL, resolved_path[MAXPATHLEN]; xmlParserInputBufferPtr inputbfr; xmlTextReaderPtr reader; if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|s!l", &source, &source_len, &encoding, &encoding_len, &options) == FAILURE) { return; } id = getThis(); if (id != NULL && ! instanceof_function(Z_OBJCE_P(id), xmlreader_class_entry)) { id = NULL; } if (id != NULL) { intern = Z_XMLREADER_P(id); xmlreader_free_resources(intern); } if (!source_len) { php_error_docref(NULL, E_WARNING, "Empty string supplied as input"); RETURN_FALSE; } inputbfr = xmlParserInputBufferCreateMem(source, source_len, XML_CHAR_ENCODING_NONE); if (inputbfr != NULL) { /* Get the URI of the current script so that we can set the base directory in libxml */ #if HAVE_GETCWD directory = VCWD_GETCWD(resolved_path, MAXPATHLEN); #elif HAVE_GETWD directory = VCWD_GETWD(resolved_path); #endif if (directory) { resolved_path_len = strlen(resolved_path); if (resolved_path[resolved_path_len - 1] != DEFAULT_SLASH) { resolved_path[resolved_path_len] = DEFAULT_SLASH; resolved_path[++resolved_path_len] = '\0'; } uri = (char *) xmlCanonicPath((const xmlChar *) resolved_path); } reader = xmlNewTextReader(inputbfr, uri); if (reader != NULL) { #if LIBXML_VERSION >= 20628 ret = xmlTextReaderSetup(reader, NULL, uri, encoding, options); #endif if (ret == 0) { if (id == NULL) { object_init_ex(return_value, xmlreader_class_entry); intern = Z_XMLREADER_P(return_value); } else { RETVAL_TRUE; } intern->input = inputbfr; intern->ptr = reader; if (uri) { xmlFree(uri); } return; } } } if (uri) { xmlFree(uri); } if (inputbfr) { xmlFreeParserInputBuffer(inputbfr); } php_error_docref(NULL, E_WARNING, "Unable to load source data"); RETURN_FALSE; }