int XML_ProcessFile(XML_Parser parser, const XML_Char *filename, unsigned flags) { int result; if (!XML_SetBase(parser, filename)) { ftprintf(stderr, T("%s: out of memory"), filename); exit(1); } if (flags & XML_EXTERNAL_ENTITIES) XML_SetExternalEntityRefHandler(parser, (flags & XML_MAP_FILE) ? externalEntityRefFilemap : externalEntityRefStream); if (flags & XML_MAP_FILE) { PROCESS_ARGS args; args.retPtr = &result; args.parser = parser; if (!filemap(filename, processFile, &args)) result = 0; } else result = processStream(filename, parser); return result; }
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); }
/** * Parses the given input stream and returns a DOM Document. * A NULL pointer will be returned if errors occurred */ nsresult txDriver::parse(istream& aInputStream, const nsAString& aUri) { mErrorString.Truncate(); if (!aInputStream) { mErrorString.AppendLiteral("unable to parse xml: invalid or unopen stream encountered."); return NS_ERROR_FAILURE; } static const XML_Memory_Handling_Suite memsuite = { (void *(*)(size_t))PR_Malloc, (void *(*)(void *, size_t))PR_Realloc, PR_Free }; static const PRUnichar expatSeparator = kExpatSeparatorChar; mExpatParser = XML_ParserCreate_MM(nsnull, &memsuite, &expatSeparator); if (!mExpatParser) { return NS_ERROR_OUT_OF_MEMORY; } XML_SetReturnNSTriplet(mExpatParser, XML_TRUE); XML_SetUserData(mExpatParser, this); XML_SetElementHandler(mExpatParser, startElement, endElement); XML_SetCharacterDataHandler(mExpatParser, charData); #ifdef XML_DTD XML_SetParamEntityParsing(mExpatParser, XML_PARAM_ENTITY_PARSING_ALWAYS); #endif XML_SetExternalEntityRefHandler(mExpatParser, externalEntityRefHandler); XML_SetExternalEntityRefHandlerArg(mExpatParser, this); XML_SetBase(mExpatParser, (const XML_Char*)(PromiseFlatString(aUri).get())); const int bufferSize = 1024; char buf[bufferSize]; PRBool done; int success; mRV = NS_OK; do { aInputStream.read(buf, bufferSize); done = aInputStream.eof(); success = XML_Parse(mExpatParser, buf, aInputStream.gcount(), done); // mRV is set in onDoneCompiling in case of an error if (!success || NS_FAILED(mRV)) { createErrorString(); done = MB_TRUE; } } while (!done); aInputStream.clear(); // clean up XML_ParserFree(mExpatParser); mCompiler->doneLoading(); if (!success) { return NS_ERROR_FAILURE; } return mRV; }
PRIVATE void HTXML_setHandlers (XML_Parser me) { XML_SetElementHandler(me, XML_startElement, XML_endElement); XML_SetCharacterDataHandler(me, XML_characterData); XML_SetProcessingInstructionHandler(me, XML_processingInstruction); XML_SetDefaultHandler(me, XML_default); XML_SetUnparsedEntityDeclHandler(me, XML_unparsedEntityDecl); XML_SetNotationDeclHandler(me, XML_notationDecl); XML_SetExternalEntityRefHandler(me, XML_externalEntityRef); XML_SetUnknownEncodingHandler(me, XML_unknownEncoding, NULL); }
void ParserEngine::init() { if (_parser) XML_ParserFree(_parser); if (!_pBuffer) _pBuffer = new char[PARSE_BUFFER_SIZE]; if (dynamic_cast<NoNamespacePrefixesStrategy*>(_pNamespaceStrategy)) { _parser = XML_ParserCreateNS(_encodingSpecified ? _encoding.c_str() : 0, '\t'); if (_parser) { XML_SetNamespaceDeclHandler(_parser, handleStartNamespaceDecl, handleEndNamespaceDecl); } } else if (dynamic_cast<NamespacePrefixesStrategy*>(_pNamespaceStrategy)) { _parser = XML_ParserCreateNS(_encodingSpecified ? _encoding.c_str() : 0, '\t'); if (_parser) { XML_SetReturnNSTriplet(_parser, 1); XML_SetNamespaceDeclHandler(_parser, handleStartNamespaceDecl, handleEndNamespaceDecl); } } else { _parser = XML_ParserCreate(_encodingSpecified ? _encoding.c_str() : 0); } if (!_parser) throw XMLException("Cannot create Expat parser"); XML_SetUserData(_parser, this); XML_SetElementHandler(_parser, handleStartElement, handleEndElement); XML_SetCharacterDataHandler(_parser, handleCharacterData); XML_SetProcessingInstructionHandler(_parser, handleProcessingInstruction); if (_expandInternalEntities) XML_SetDefaultHandlerExpand(_parser, handleDefault); else XML_SetDefaultHandler(_parser, handleDefault); XML_SetUnparsedEntityDeclHandler(_parser, handleUnparsedEntityDecl); XML_SetNotationDeclHandler(_parser, handleNotationDecl); XML_SetExternalEntityRefHandler(_parser, handleExternalEntityRef); XML_SetCommentHandler(_parser, handleComment); XML_SetCdataSectionHandler(_parser, handleStartCdataSection, handleEndCdataSection); XML_SetDoctypeDeclHandler(_parser, handleStartDoctypeDecl, handleEndDoctypeDecl); XML_SetEntityDeclHandler(_parser, handleEntityDecl); XML_SetSkippedEntityHandler(_parser, handleSkippedEntity); XML_SetParamEntityParsing(_parser, _externalParameterEntities ? XML_PARAM_ENTITY_PARSING_ALWAYS : XML_PARAM_ENTITY_PARSING_NEVER); XML_SetUnknownEncodingHandler(_parser, handleUnknownEncoding, this); }
void RS_XML(initParser)(XML_Parser parser, RS_XMLParserData *parserData) { XML_SetUserData(parser, parserData); XML_SetElementHandler(parser, RS_XML(startElement), RS_XML(endElement)); XML_SetCommentHandler(parser, RS_XML(commentHandler)); XML_SetExternalEntityRefHandler(parser, RS_XML(externalEntityHandler)); XML_SetUnparsedEntityDeclHandler(parser, RS_XML(entityDeclarationHandler)); XML_SetCharacterDataHandler(parser, RS_XML(textHandler)); XML_SetProcessingInstructionHandler(parser, RS_XML(processingInstructionHandler)); XML_SetCdataSectionHandler(parser, RS_XML(startCdataSectionHandler), RS_XML(endCdataSectionHandler)); XML_SetBase(parser, parserData->fileName); XML_SetNotStandaloneHandler(parser, RS_XML(notStandAloneHandler)); }
/** * Creates a new Expat parser. Called from the Java ExpatParser constructor. * * @param object the Java ExpatParser instance * @param javaEncoding the character encoding name * @param processNamespaces true if the parser should handle namespaces * @returns the pointer to the C Expat parser */ static jlong ExpatParser_initialize(JNIEnv* env, jobject object, jstring javaEncoding, jboolean processNamespaces) { // Allocate parsing context. UniquePtr<ParsingContext> context(new ParsingContext(object)); if (context.get() == NULL) { jniThrowOutOfMemoryError(env, NULL); return 0; } context->processNamespaces = processNamespaces; // Create a parser. XML_Parser parser; ScopedUtfChars encoding(env, javaEncoding); if (encoding.c_str() == NULL) { return 0; } if (processNamespaces) { // Use '|' to separate URIs from local names. parser = XML_ParserCreateNS(encoding.c_str(), '|'); } else { parser = XML_ParserCreate(encoding.c_str()); } if (parser != NULL) { if (processNamespaces) { XML_SetNamespaceDeclHandler(parser, startNamespace, endNamespace); XML_SetReturnNSTriplet(parser, 1); } XML_SetCdataSectionHandler(parser, startCdata, endCdata); XML_SetCharacterDataHandler(parser, text); XML_SetCommentHandler(parser, comment); XML_SetDoctypeDeclHandler(parser, startDtd, endDtd); XML_SetElementHandler(parser, startElement, endElement); XML_SetExternalEntityRefHandler(parser, handleExternalEntity); XML_SetNotationDeclHandler(parser, notationDecl); XML_SetProcessingInstructionHandler(parser, processingInstruction); XML_SetUnparsedEntityDeclHandler(parser, unparsedEntityDecl); XML_SetUserData(parser, context.release()); } else { jniThrowOutOfMemoryError(env, NULL); return 0; } return fromXMLParser(parser); }
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; }
static void pdb_xml_init_parser (PdbXmlParser *parser, XML_Parser xml_parser) { XML_SetParamEntityParsing (xml_parser, XML_PARAM_ENTITY_PARSING_ALWAYS); XML_SetUserData (xml_parser, parser); XML_SetElementHandler (xml_parser, pdb_xml_start_element_cb, pdb_xml_end_element_cb); XML_SetCharacterDataHandler (xml_parser, pdb_xml_character_data_cb); XML_SetExternalEntityRefHandler (xml_parser, pdb_xml_external_entity_ref_cb); }
END_TEST /* Test that no error is reported for unknown entities if we have read an external subset, and standalone is false. */ START_TEST(test_wfc_undeclared_entity_with_external_subset) { char *text = "<?xml version='1.0' encoding='us-ascii'?>\n" "<!DOCTYPE doc SYSTEM 'foo'>\n" "<doc>&entity;</doc>"; char *foo_text = "<!ELEMENT doc (#PCDATA)*>"; XML_SetParamEntityParsing(parser, XML_PARAM_ENTITY_PARSING_ALWAYS); XML_SetUserData(parser, foo_text); XML_SetExternalEntityRefHandler(parser, external_entity_loader); if (XML_Parse(parser, text, strlen(text), XML_TRUE) == XML_STATUS_ERROR) xml_failure(parser); }
/** * Parses the given input stream and returns a DOM Document. * A NULL pointer will be returned if errors occurred */ nsresult txXMLParser::parse(istream& aInputStream, const nsAString& aUri, txXPathNode** aResultDoc) { mErrorString.Truncate(); *aResultDoc = nsnull; if (!aInputStream) { mErrorString.AppendLiteral("unable to parse xml: invalid or unopen stream encountered."); return NS_ERROR_FAILURE; } static const XML_Memory_Handling_Suite memsuite = { (void *(*)(size_t))PR_Malloc, (void *(*)(void *, size_t))PR_Realloc, PR_Free }; static const PRUnichar expatSeparator = kExpatSeparatorChar; mExpatParser = XML_ParserCreate_MM(nsnull, &memsuite, &expatSeparator); if (!mExpatParser) { return NS_ERROR_OUT_OF_MEMORY; } mDocument = new Document(); if (!mDocument) { XML_ParserFree(mExpatParser); return NS_ERROR_OUT_OF_MEMORY; } mDocument->documentBaseURI = aUri; mCurrentNode = mDocument; XML_SetReturnNSTriplet(mExpatParser, XML_TRUE); XML_SetUserData(mExpatParser, this); XML_SetElementHandler(mExpatParser, startElement, endElement); XML_SetCharacterDataHandler(mExpatParser, charData); XML_SetProcessingInstructionHandler(mExpatParser, piHandler); XML_SetCommentHandler(mExpatParser, commentHandler); #ifdef XML_DTD XML_SetParamEntityParsing(mExpatParser, XML_PARAM_ENTITY_PARSING_ALWAYS); #endif XML_SetExternalEntityRefHandler(mExpatParser, externalEntityRefHandler); XML_SetExternalEntityRefHandlerArg(mExpatParser, this); XML_SetBase(mExpatParser, (const XML_Char*)(PromiseFlatString(aUri).get())); const int bufferSize = 1024; char buf[bufferSize]; PRBool done; do { aInputStream.read(buf, bufferSize); done = aInputStream.eof(); if (!XML_Parse(mExpatParser, buf, aInputStream.gcount(), done)) { createErrorString(); done = MB_TRUE; delete mDocument; mDocument = nsnull; } } while (!done); aInputStream.clear(); // clean up XML_ParserFree(mExpatParser); // ownership to the caller *aResultDoc = txXPathNativeNode::createXPathNode(mDocument); mDocument = nsnull; return *aResultDoc ? NS_OK : NS_ERROR_OUT_OF_MEMORY; }
bool f_xml_set_external_entity_ref_handler(const Resource& parser, const Variant& handler) { XmlParser * p = parser.getTyped<XmlParser>(); xml_set_handler(&p->externalEntityRefHandler, handler); XML_SetExternalEntityRefHandler(p->parser, _xml_externalEntityRefHandler); return true; }
static void parse_init(XML_Parser parser) { XML_SetExternalEntityRefHandler(parser, external_entity_handler); }
NS_IMETHODIMP nsExpatDriver::WillBuildModel(const CParserContext& aParserContext, nsITokenizer* aTokenizer, nsIContentSink* aSink) { mSink = do_QueryInterface(aSink); if (!mSink) { NS_ERROR("nsExpatDriver didn't get an nsIExpatSink"); // Make sure future calls to us bail out as needed mInternalState = NS_ERROR_UNEXPECTED; return mInternalState; } mOriginalSink = aSink; static const XML_Memory_Handling_Suite memsuite = { (void *(*)(size_t))PR_Malloc, (void *(*)(void *, size_t))PR_Realloc, PR_Free }; static const PRUnichar kExpatSeparator[] = { kExpatSeparatorChar, '\0' }; mExpatParser = XML_ParserCreate_MM(kUTF16, &memsuite, kExpatSeparator); NS_ENSURE_TRUE(mExpatParser, NS_ERROR_FAILURE); XML_SetReturnNSTriplet(mExpatParser, XML_TRUE); #ifdef XML_DTD XML_SetParamEntityParsing(mExpatParser, XML_PARAM_ENTITY_PARSING_ALWAYS); #endif mURISpec = aParserContext.mScanner->GetFilename(); XML_SetBase(mExpatParser, mURISpec.get()); nsCOMPtr<nsIDocument> doc = do_QueryInterface(mOriginalSink->GetTarget()); if (doc) { nsCOMPtr<nsPIDOMWindow> win = doc->GetWindow(); if (!win) { PRBool aHasHadScriptHandlingObject; nsIScriptGlobalObject *global = doc->GetScriptHandlingObject(aHasHadScriptHandlingObject); if (global) { win = do_QueryInterface(global); } } if (win && !win->IsInnerWindow()) { win = win->GetCurrentInnerWindow(); } if (win) { mInnerWindowID = win->WindowID(); } } // Set up the callbacks XML_SetXmlDeclHandler(mExpatParser, Driver_HandleXMLDeclaration); XML_SetElementHandler(mExpatParser, Driver_HandleStartElement, Driver_HandleEndElement); XML_SetCharacterDataHandler(mExpatParser, Driver_HandleCharacterData); XML_SetProcessingInstructionHandler(mExpatParser, Driver_HandleProcessingInstruction); XML_SetDefaultHandlerExpand(mExpatParser, Driver_HandleDefault); XML_SetExternalEntityRefHandler(mExpatParser, (XML_ExternalEntityRefHandler) Driver_HandleExternalEntityRef); XML_SetExternalEntityRefHandlerArg(mExpatParser, this); XML_SetCommentHandler(mExpatParser, Driver_HandleComment); XML_SetCdataSectionHandler(mExpatParser, Driver_HandleStartCdataSection, Driver_HandleEndCdataSection); XML_SetParamEntityParsing(mExpatParser, XML_PARAM_ENTITY_PARSING_UNLESS_STANDALONE); XML_SetDoctypeDeclHandler(mExpatParser, Driver_HandleStartDoctypeDecl, Driver_HandleEndDoctypeDecl); // If the sink is an nsIExtendedExpatSink, // register some addtional handlers. mExtendedSink = do_QueryInterface(mSink); if (mExtendedSink) { XML_SetNamespaceDeclHandler(mExpatParser, Driver_HandleStartNamespaceDecl, Driver_HandleEndNamespaceDecl); XML_SetUnparsedEntityDeclHandler(mExpatParser, Driver_HandleUnparsedEntityDecl); XML_SetNotationDeclHandler(mExpatParser, Driver_HandleNotationDecl); } // Set up the user data. XML_SetUserData(mExpatParser, this); // XML must detect invalid character convertion aParserContext.mScanner->OverrideReplacementCharacter(0xffff); return mInternalState; }
bool f_xml_set_external_entity_ref_handler(CObjRef parser, CVarRef handler) { XmlParser * p = parser.getTyped<XmlParser>(); xml_set_handler(&p->externalEntityRefHandler, handler); XML_SetExternalEntityRefHandler(p->parser, _xml_externalEntityRefHandler); return true; }
void _Expat_XML_SetExternalEntityRefHandler(struct ExpatIFace * Self, XML_Parser parser, XML_ExternalEntityRefHandler handler) { XML_SetExternalEntityRefHandler(parser, handler); }