void xmlg_parser_error_abort( xmlGParser *xml_parser, HqBool fire_error_handler, uint8 *detail, int32 detail_len) { xmlGParserCommon *c ; xmlGFilterChain *filter_chain ; uint32 line, column ; XMLGASSERT(xml_parser != NULL, "xml_parser is NULL") ; c = xmlg_get_parser_common(xml_parser); XMLGASSERT(c != NULL, "common is NULL"); filter_chain = c->filter_chain ; /* setting the callback functions to NULL ensures we stop */ XML_SetElementHandler(xml_parser->ctxt, NULL, NULL) ; XML_SetStartNamespaceDeclHandler(xml_parser->ctxt, NULL) ; XML_SetCharacterDataHandler(xml_parser->ctxt, NULL) ; XML_SetXmlDeclHandler(xml_parser->ctxt, NULL) ; XML_SetStartDoctypeDeclHandler(xml_parser->ctxt, NULL) ; if (fire_error_handler) { xmlg_parser_line_and_column(xml_parser, &line, &column) ; /* only ever invoke the error handler once */ if (c->parse_error_cb != NULL && fire_error_handler && ! c->success_abort) c->parse_error_cb(xml_parser, xmlg_fc_get_uri(filter_chain), line, column, detail, detail_len) ; } if (! c->success_abort) c->error_abort = TRUE ; }
result_t XmlParser::parse(XmlDocument* doc, exlib::string source) { XmlParser parser(doc, true); parser.m_now = doc; parser.m_list.push_back(doc); XML_Parser xml_parser = XML_ParserCreate(NULL); XML_SetParamEntityParsing(xml_parser, XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE); XML_SetUserData(xml_parser, &parser); XML_SetXmlDeclHandler(xml_parser, XmlDeclHandler); XML_SetElementHandler(xml_parser, StartElementHandler, EndElementHandler); XML_SetCharacterDataHandler(xml_parser, CharacterDataHandler); XML_SetProcessingInstructionHandler(xml_parser, ProcessingInstructionHandler); XML_SetCommentHandler(xml_parser, CommentHandler); XML_SetCdataSectionHandler(xml_parser, StartCdataSectionHandler, EndCdataSectionHandler); XML_SetStartDoctypeDeclHandler(xml_parser, StartDoctypeDeclHandler); if (XML_Parse(xml_parser, source.c_str(), (int32_t)source.length(), true) != XML_STATUS_OK) { char msg[128]; sprintf(msg, "XmlParser: error on line %lu at column %lu: %s", XML_GetCurrentLineNumber(xml_parser), XML_GetCurrentColumnNumber(xml_parser) + 1, XML_ErrorString(XML_GetErrorCode(xml_parser))); XML_ParserFree(xml_parser); return CHECK_ERROR(Runtime::setError(msg)); } XML_ParserFree(xml_parser); return 0; }
MeaXMLParser::MeaXMLParser(MeaXMLParserHandler *handler, bool buildDOM) : IValidationHandler(), m_isSubParser(false), m_handler(handler), m_haveDTD(false), m_context(NULL), m_buildDOM(buildDOM), m_dom(NULL) { // Create the XML parser and set its handlers. // m_pathnameStack = new PathnameStack; m_elementStack = new ElementStack; m_nodeStack = new NodeStack; m_parser = XML_ParserCreate(NULL); MeaAssert(m_parser != NULL); XML_SetUserData(m_parser, this); XML_SetExternalEntityRefHandlerArg(m_parser, this); XML_SetParamEntityParsing(m_parser, XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE); XML_SetElementHandler(m_parser, StartElementHandler, EndElementHandler); XML_SetCharacterDataHandler(m_parser, CharacterDataHandler); XML_SetExternalEntityRefHandler(m_parser, ExternalEntityRefHandler); XML_SetStartDoctypeDeclHandler(m_parser, DoctypeDeclHandler); XML_SetElementHandler(m_parser, StartElementHandler, EndElementHandler); XML_SetElementDeclHandler(m_parser, ElementDeclHandler); XML_SetAttlistDeclHandler(m_parser, AttributeDeclHandler); // Create the validator. // m_validator = new ev::Validator(this); }
ExpatAdapter::ExpatAdapter() : parser(0) { #if XMP_DebugBuild this->elemNesting = 0; #if DumpXMLParseEvents if ( this->parseLog == 0 ) this->parseLog = stdout; #endif #endif this->parser = XML_ParserCreateNS ( 0, FullNameSeparator ); if ( this->parser == 0 ) XMP_Throw ( "Failure creating Expat parser", kXMPErr_ExternalFailure ); XML_SetUserData ( this->parser, this ); XML_SetNamespaceDeclHandler ( this->parser, StartNamespaceDeclHandler, EndNamespaceDeclHandler ); XML_SetElementHandler ( this->parser, StartElementHandler, EndElementHandler ); XML_SetCharacterDataHandler ( this->parser, CharacterDataHandler ); XML_SetCdataSectionHandler ( this->parser, StartCdataSectionHandler, EndCdataSectionHandler ); XML_SetProcessingInstructionHandler ( this->parser, ProcessingInstructionHandler ); XML_SetCommentHandler ( this->parser, CommentHandler ); #if BanAllEntityUsage XML_SetStartDoctypeDeclHandler ( this->parser, StartDoctypeDeclHandler ); isAborted = false; #endif this->parseStack.push_back ( &this->tree ); // Push the XML root node. } // ExpatAdapter::ExpatAdapter
/* ============================================================================ * Abort and information functions. */ void xmlg_p_abort_parse( xmlGParser *xml_parser) { xmlGParserCommon *c; XMLGASSERT(xml_parser != NULL, "xml_parser is NULL"); c = xmlg_get_parser_common(xml_parser); XMLGASSERT(c != NULL, "common is NULL"); c->success_abort = TRUE; /* setting the callback functions to NULL ensures we stop */ XML_SetElementHandler(xml_parser->ctxt, NULL, NULL); XML_SetStartNamespaceDeclHandler(xml_parser->ctxt, NULL); XML_SetCharacterDataHandler(xml_parser->ctxt, NULL); XML_SetXmlDeclHandler(xml_parser->ctxt, NULL) ; XML_SetStartDoctypeDeclHandler(xml_parser->ctxt, NULL) ; }
bool_t reset_handlers_parser(parser_t *parser) { if( parser ) { XML_SetStartElementHandler(parser->p, parser->callbacks.start_tag ? xml_startelementhandler : NULL); XML_SetEndElementHandler(parser->p, parser->callbacks.end_tag ? xml_endelementhandler : NULL); XML_SetCommentHandler(parser->p, parser->callbacks.comment ? xml_commenthandler : NULL); XML_SetCharacterDataHandler(parser->p, parser->callbacks.chardata ? xml_characterdatahandler : NULL); XML_SetProcessingInstructionHandler(parser->p, parser->callbacks.pidata ? xml_processinginstructionhandler : NULL); XML_SetStartCdataSectionHandler(parser->p, parser->callbacks.start_cdata ? xml_startcdatahandler : NULL); /* if start_cdata, always set endcdatahandler */ XML_SetEndCdataSectionHandler(parser->p, parser->callbacks.start_cdata ? xml_endcdatahandler : NULL); XML_SetDefaultHandler(parser->p, parser->callbacks.dfault ? xml_defaulthandler : NULL); XML_SetStartDoctypeDeclHandler(parser->p, parser->callbacks.start_doctypedecl ? xml_startdoctypedeclhandler : NULL); /* if start_doctypedecl, always set enddoctypedeclhandler */ XML_SetEndDoctypeDeclHandler(parser->p, parser->callbacks.start_doctypedecl ? xml_enddoctypedeclhandler : NULL); XML_SetEntityDeclHandler(parser->p, parser->callbacks.entitydecl ? xml_entitydeclhandler : NULL); /* what to do about entities: there can be internal and external * entities, which expat treats differently. We want all our * entities to stay unprocessed, otherwise we'll get well formedness * errors. When a handler is called, the raw string is passed to * the default handler (provided it exists). */ XML_UseForeignDTD(parser->p, XML_TRUE); XML_SetParamEntityParsing(parser->p, XML_PARAM_ENTITY_PARSING_NEVER); XML_SetExternalEntityRefHandler(parser->p, xml_externalentityrefhandler); XML_SetExternalEntityRefHandlerArg(parser->p, parser); XML_SetSkippedEntityHandler(parser->p, xml_skippedentityhandler); return TRUE; } return FALSE; }
static int lxp_make_parser (lua_State *L) { XML_Parser p; int bufferCharData = (lua_type(L, 3) != LUA_TBOOLEAN) || (lua_toboolean(L, 3) != 0); char sep = *luaL_optstring(L, 2, ""); lxp_userdata *xpu = createlxp(L); xpu->bufferCharData = bufferCharData; p = xpu->parser = (sep == '\0') ? XML_ParserCreate(NULL) : XML_ParserCreateNS(NULL, sep); if (!p) luaL_error(L, "XML_ParserCreate failed"); luaL_checktype(L, 1, LUA_TTABLE); checkcallbacks(L); lua_pushvalue(L, 1); xpu->tableref = luaL_ref(L, LUA_REGISTRYINDEX); XML_SetUserData(p, xpu); if (hasfield(L, StartCdataKey) || hasfield(L, EndCdataKey)) XML_SetCdataSectionHandler(p, f_StartCdata, f_EndCdataKey); if (hasfield(L, CharDataKey)) XML_SetCharacterDataHandler(p, f_CharData); if (hasfield(L, CommentKey)) XML_SetCommentHandler(p, f_Comment); if (hasfield(L, DefaultKey)) XML_SetDefaultHandler(p, f_Default); if (hasfield(L, DefaultExpandKey)) XML_SetDefaultHandlerExpand(p, f_DefaultExpand); if (hasfield(L, StartElementKey) || hasfield(L, EndElementKey)) XML_SetElementHandler(p, f_StartElement, f_EndElement); if (hasfield(L, ExternalEntityKey)) XML_SetExternalEntityRefHandler(p, f_ExternaEntity); if (hasfield(L, StartNamespaceDeclKey) || hasfield(L, EndNamespaceDeclKey)) XML_SetNamespaceDeclHandler(p, f_StartNamespaceDecl, f_EndNamespaceDecl); if (hasfield(L, NotationDeclKey)) XML_SetNotationDeclHandler(p, f_NotationDecl); if (hasfield(L, NotStandaloneKey)) XML_SetNotStandaloneHandler(p, f_NotStandalone); if (hasfield(L, ProcessingInstructionKey)) XML_SetProcessingInstructionHandler(p, f_ProcessingInstruction); if (hasfield(L, UnparsedEntityDeclKey)) XML_SetUnparsedEntityDeclHandler(p, f_UnparsedEntityDecl); if (hasfield(L, StartDoctypeDeclKey)) XML_SetStartDoctypeDeclHandler(p, f_StartDoctypeDecl); if (hasfield(L, XmlDeclKey)) XML_SetXmlDeclHandler(p, f_XmlDecl); return 1; }
WBXML_DECLARE(WBXMLError) wbxml_tree_from_xml(WB_UTINY *xml, WB_ULONG xml_len, WBXMLTree **tree) { #if defined( HAVE_EXPAT ) const XML_Feature *feature_list = NULL; XML_Parser xml_parser = NULL; WBXMLError ret = WBXML_OK; WB_BOOL expat_utf16 = FALSE; WBXMLTreeClbCtx wbxml_tree_clb_ctx; /* First Check if Expat is outputing UTF-16 strings */ feature_list = (const XML_Feature *)XML_GetFeatureList(); if ((feature_list != NULL) && (feature_list[0].value != sizeof(WB_TINY))) { #if !defined( HAVE_ICONV ) /* Ouch, can't convert from UTF-16 to UTF-8 */ return WBXML_ERROR_XMLPARSER_OUTPUT_UTF16; #else /* Expat returns UTF-16 encoded strings in its callbacks */ expat_utf16 = TRUE; #endif /* !HAVE_ICONV */ } if (tree != NULL) *tree = NULL; /* Create Expat XML Parser */ if ((xml_parser = XML_ParserCreateNS(NULL, WBXML_NAMESPACE_SEPARATOR)) == NULL) return WBXML_ERROR_NOT_ENOUGH_MEMORY; /* Init context */ wbxml_tree_clb_ctx.current = NULL; wbxml_tree_clb_ctx.error = WBXML_OK; wbxml_tree_clb_ctx.skip_lvl = 0; wbxml_tree_clb_ctx.skip_start = 0; wbxml_tree_clb_ctx.xml_parser = xml_parser; wbxml_tree_clb_ctx.input_buff = xml; wbxml_tree_clb_ctx.expat_utf16 = expat_utf16; /* Create WBXML Tree */ if ((wbxml_tree_clb_ctx.tree = wbxml_tree_create(WBXML_LANG_UNKNOWN, WBXML_CHARSET_UNKNOWN)) == NULL) { XML_ParserFree(xml_parser); WBXML_ERROR((WBXML_PARSER, "Can't create WBXML Tree")); return WBXML_ERROR_NOT_ENOUGH_MEMORY; } /* Set Handlers Callbacks */ XML_SetXmlDeclHandler(xml_parser, wbxml_tree_clb_xml_decl); XML_SetStartDoctypeDeclHandler(xml_parser, wbxml_tree_clb_xml_doctype_decl); XML_SetElementHandler(xml_parser, wbxml_tree_clb_xml_start_element, wbxml_tree_clb_xml_end_element); XML_SetCdataSectionHandler(xml_parser, wbxml_tree_clb_xml_start_cdata, wbxml_tree_clb_xml_end_cdata); XML_SetProcessingInstructionHandler(xml_parser , wbxml_tree_clb_xml_pi); XML_SetCharacterDataHandler(xml_parser, wbxml_tree_clb_xml_characters); XML_SetUserData(xml_parser, (void*)&wbxml_tree_clb_ctx); /* Parse the XML Document to WBXML Tree */ if (XML_Parse(xml_parser, (WB_TINY*) xml, xml_len, TRUE) == 0) { WBXML_ERROR((WBXML_CONV, "xml2wbxml conversion failed - expat error %i\n" "\tdescription: %s\n" "\tline: %i\n" "\tcolumn: %i\n" "\tbyte index: %i\n" "\ttotal bytes: %i\n%s", XML_GetErrorCode(xml_parser), XML_ErrorString(XML_GetErrorCode(xml_parser)), XML_GetCurrentLineNumber(xml_parser), XML_GetCurrentColumnNumber(xml_parser), XML_GetCurrentByteIndex(xml_parser), XML_GetCurrentByteCount(xml_parser), xml)); wbxml_tree_destroy(wbxml_tree_clb_ctx.tree); ret = WBXML_ERROR_XML_PARSING_FAILED; } else { if ((ret = wbxml_tree_clb_ctx.error) != WBXML_OK) { WBXML_ERROR((WBXML_CONV, "xml2wbxml conversion failed - context error %i", ret)); wbxml_tree_destroy(wbxml_tree_clb_ctx.tree); } else *tree = wbxml_tree_clb_ctx.tree; } /* Clean-up */ XML_ParserFree(xml_parser); return ret; #else /* HAVE_EXPAT */ #if defined( HAVE_LIBXML ) /** @todo Use LibXML2 SAX interface ! */ return WBXML_ERROR_NO_XMLPARSER; #else /* HAVE_LIBXML */ /** @note You can add here another XML Parser support */ return WBXML_ERROR_NO_XMLPARSER; #endif /* HAVE_LIBXML */ #endif /* HAVE_EXPAT */ }
/* The handler. Create a new parser and/or filter context where appropriate * and parse the chunks of data received from the brigade */ static int idlChunkHandler( ap_filter_t *f, apr_bucket_brigade *brigade ) { idlChunkContext* ctx = f->ctx; apr_bucket* currentBucket = NULL; apr_pool_t* pool = f->r->pool; const char* data; apr_size_t len; osrfStringArray* params = NULL; mparams = NULL; /* load the per-dir/location config */ idlChunkConfig* config = ap_get_module_config( f->r->per_dir_config, &idlchunk_module ); ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, f->r, "IDLCHUNK Config:\nContent Type = %s, " "Strip PI = %s, Strip Comments = %s, Doctype = %s", config->contentType, (config->stripPI) ? "yes" : "no", (config->stripComments) ? "yes" : "no", config->doctype); /* set the content type based on the config */ ap_set_content_type(f->r, config->contentType); //ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, f->r, "Set content type"); params = apacheParseParms(f->r); /* free me */ mparams = apacheGetParamValues( params, "class" ); /* free me */ all = 1; if (mparams && mparams->size > 0) all = 0; //ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, f->r, "Parsed the params, if any"); /* create the XML parser */ int firstrun = 0; if( parser == NULL ) { firstrun = 1; parser = XML_ParserCreate("UTF-8"); XML_SetUserData(parser, f); XML_SetElementHandler(parser, startElement, endElement); XML_SetCharacterDataHandler(parser, charHandler); if(!config->stripDoctype) XML_SetStartDoctypeDeclHandler( parser, doctypeHandler ); if(!config->stripPI) XML_SetProcessingInstructionHandler(parser, handlePI); if(!config->stripComments) XML_SetCommentHandler(parser, handleComment); } /* create the filter context */ if( ctx == NULL ) { f->ctx = ctx = apr_pcalloc( pool, sizeof(*ctx)); ctx->brigade = apr_brigade_create( pool, f->c->bucket_alloc ); ctx->parser = parser; } if(firstrun) { /* we haven't started writing the data to the stream yet */ /* go ahead and write the doctype out if we have one defined */ if(config->doctype) { ap_log_rerror( APLOG_MARK, APLOG_DEBUG, 0, f->r, "IDLCHUNK DOCTYPE => %s", config->doctype); _fwrite(f, "%s\n", config->doctype); } } /* cycle through the buckets in the brigade */ while (!APR_BRIGADE_EMPTY(brigade)) { /* grab the next bucket */ currentBucket = APR_BRIGADE_FIRST(brigade); /* clean up when we're done */ if (APR_BUCKET_IS_EOS(currentBucket) || APR_BUCKET_IS_FLUSH(currentBucket)) { APR_BUCKET_REMOVE(currentBucket); APR_BRIGADE_INSERT_TAIL(ctx->brigade, currentBucket); ap_pass_brigade(f->next, ctx->brigade); XML_ParserFree(parser); if (params) osrfStringArrayFree(params); if (mparams) osrfStringArrayFree(mparams); parser = NULL; return APR_SUCCESS; } /* read the incoming data */ int s = apr_bucket_read(currentBucket, &data, &len, APR_NONBLOCK_READ); if( s != APR_SUCCESS ) { ap_log_rerror( APLOG_MARK, APLOG_ERR, 0, f->r, "IDLCHUNK error reading data from filter with status %d", s); if (params) osrfStringArrayFree(params); if (mparams) osrfStringArrayFree(mparams); return s; } if (len > 0) { ap_log_rerror( APLOG_MARK, APLOG_DEBUG, 0, f->r, "IDLCHUNK read %d bytes", (int)len); /* push data into the XML push parser */ if ( XML_Parse(ctx->parser, data, len, 0) == XML_STATUS_ERROR ) { char tmp[len+1]; memcpy(tmp, data, len); tmp[len] = '\0'; /* log and die on XML errors */ ap_log_rerror( APLOG_MARK, APLOG_ERR, 0, f->r, "IDLCHUNK XML Parse Error: %s at line %d: parsing %s: data %s", XML_ErrorString(XML_GetErrorCode(ctx->parser)), (int) XML_GetCurrentLineNumber(ctx->parser), f->r->filename, tmp); XML_ParserFree(parser); if (params) osrfStringArrayFree(params); if (mparams) osrfStringArrayFree(mparams); parser = NULL; return HTTP_INTERNAL_SERVER_ERROR; } } /* so a subrequest doesn't re-read this bucket */ apr_bucket_delete(currentBucket); } apr_brigade_destroy(brigade); if (params) osrfStringArrayFree(params); if (mparams) osrfStringArrayFree(mparams); return APR_SUCCESS; }
/* ============================================================================ * Create/Destroy XML parse handler. */ HqBool xmlg_p_new( xmlGContext *xml_ctxt, xmlGParser **xml_parser, xmlGMemoryHandler *memory_handler, xmlGFilterChain *filter_chain) { xmlGParser *new_xml_parser; XMLGASSERT(xml_ctxt != NULL, "xml_ctxt is NULL"); XMLGASSERT(xml_parser != NULL, "xml_parser is NULL"); XMLGASSERT(filter_chain != NULL, "filter_chain is NULL"); *xml_parser = NULL; /* Although this code is going to be common to all back end XML parsers, we need to do it here because only this file knows about the size of xmlGParser - which is back end specific. */ if (memory_handler != NULL) { if (memory_handler->f_malloc == NULL || memory_handler->f_realloc == NULL || memory_handler->f_free == NULL) { XMLGASSERT(FALSE, "incomplete memory handler") ; return FALSE ; } /* Use the pluged memory handler to allocate this structure */ if ((new_xml_parser = memory_handler->f_malloc(sizeof(xmlGParser))) == NULL) return FALSE; } else { /* use the XML sub-system memory management */ if ((new_xml_parser = xmlg_subsystem_malloc(xml_ctxt, sizeof(xmlGParser))) == NULL) return FALSE; } if (! xmlg_parser_common_init(new_xml_parser, xml_ctxt, memory_handler, filter_chain)) { /* We need to do this as we don't know if the memory handlers have been plugged successfuly. Better safe than sorry. */ if (memory_handler != NULL) { memory_handler->f_free(new_xml_parser); } else { xmlg_subsystem_free(xml_ctxt, new_xml_parser); } return FALSE; } /* xmlg_parser_malloc and friends are now available for this handler. */ /* Do backend XML parser specific stuff. */ /* setup expat memory handler structure */ if (memory_handler != NULL) { new_xml_parser->expat_mem.malloc_fcn = memory_handler->f_malloc ; new_xml_parser->expat_mem.realloc_fcn = memory_handler->f_realloc ; new_xml_parser->expat_mem.free_fcn = memory_handler->f_free ; } else { new_xml_parser->expat_mem.malloc_fcn = xml_ctxt->memory_handler.f_malloc ; new_xml_parser->expat_mem.realloc_fcn = xml_ctxt->memory_handler.f_realloc ; new_xml_parser->expat_mem.free_fcn = xml_ctxt->memory_handler.f_free ; } new_xml_parser->ctxt = XML_ParserCreate_MM(NULL, &new_xml_parser->expat_mem, &sep); if (new_xml_parser->ctxt == NULL) { xmlg_parser_free(new_xml_parser, new_xml_parser); return FALSE; } /* Turn prefix mapping on. */ XML_SetReturnNSTriplet(new_xml_parser->ctxt, 1); XML_SetUserData(new_xml_parser->ctxt, (void *)(new_xml_parser)) ; XML_SetElementHandler(new_xml_parser->ctxt, expat_start_element_ns_cb, expat_end_element_ns_cb) ; XML_SetStartNamespaceDeclHandler(new_xml_parser->ctxt, expat_start_namespace_cb) ; XML_SetCharacterDataHandler(new_xml_parser->ctxt, expat_characters_data_cb) ; XML_SetXmlDeclHandler(new_xml_parser->ctxt, expat_xml_decl_cb) ; XML_SetStartDoctypeDeclHandler(new_xml_parser->ctxt, expat_xml_dtd_cb) ; if (filter_chain != NULL) { xmlg_p_set_parse_error_cb(new_xml_parser, filter_chain->parse_error_cb) ; } *xml_parser = new_xml_parser; return TRUE; }
void _Expat_XML_SetStartDoctypeDeclHandler(struct ExpatIFace * Self, XML_Parser parser, XML_StartDoctypeDeclHandler start) { XML_SetStartDoctypeDeclHandler(parser, start); }
void XMLParser::EnableStartDoctypeDeclHandler(bool enable) { assert(m_parser != NULL); XML_SetStartDoctypeDeclHandler(m_parser, enable ? StartDoctypeDeclHandler : NULL); }
void bmx_expat_XML_SetStartDoctypeDeclHandler(XML_Parser parser) { XML_SetStartDoctypeDeclHandler(parser, bmx_expat_StartDoctypeDeclHandler); }