ExternalEntityRefHandlerImpl(XMLParser *parser, char *context, char *baseUri, char *systemId, char *publicId) { int result = (int)XMLStatus.OK; GCHandle parserHandle = (GCHandle)LibExpat.XMLGetUserData(parser); X expatParser = (X)parserHandle.Target; E newEntityContext = expatParser.freeEntityContext; // should always be != null string encoding; try { if (!newEntityContext.Open(context, baseUri, systemId, publicId, out encoding)) { return(result); } } catch (Exception e) { expatParser.error = e; result = (int)XMLStatus.ERROR; return(result); } expatParser.PushChildEntityParseContext(parser, context, encoding); ParseStatus status = ExpatUtils.Parse( newEntityContext.XmlParser, newEntityContext.Read, out newEntityContext.error); switch (status) { case ParseStatus.Finished: expatParser.PopChildEntityParseContext(); break; case ParseStatus.Suspended: // must suspend parent parser as well - don't check return value LibExpat.XMLStopParser(parser, XMLBool.TRUE); break; case ParseStatus.FatalError: result = (int)XMLStatus.ERROR; break; case ParseStatus.Aborted: // must abort parent parser as well - don't check return value LibExpat.XMLStopParser(parser, XMLBool.FALSE); break; } return(result); }
private XMLParser *ResetParser() { Reset(); XMLParser *parser = entityContext.XmlParser; if (parser != null && LibExpat.XMLParserReset(parser, encoding) == XMLBool.FALSE) { throw new ExpatException(InternalStateError); } // all handlers have been cleared (except for unknownEncodingHandler) LibExpat.XMLSetUserData(parser, (IntPtr)parserHandle); ReInitializeHandlers(parser); LibExpat.XMLSetParamEntityParsing(parser, paramEntityParsing); return(parser); }
/// <summary>Cleans up unmanaged resources.</summary> /// <remarks>Called at end of child entity, after <see cref="Finish"/> and /// before <see cref="Reset"/>. Gets called for root entity only when the /// <see cref="ExpatParser<X, E, U>">ExpatParser</see> instance that owns /// it gets disposed.</remarks> protected internal virtual void Cleanup() { lock (this) { RuntimeHelpers.PrepareConstrainedRegions(); try { } finally { XMLParser *parser = xmlParser; if (parser != null) { LibExpat.XMLParserFree(parser); xmlParser = null; } } } GC.SuppressFinalize(this); }
Parse(XMLParser *parser, ReadBuffer read, int bufferSize, out XMLError error) { Debug.Assert(read != null && bufferSize > 0); bool isFinal = false; GCHandle bufferHandle; while (!isFinal) { XMLStatus status; int count = read(bufferSize, out bufferHandle); try { isFinal = count == 0; IntPtr bufPtr = bufferHandle.AddrOfPinnedObject(); status = LibExpat.XMLParse(parser, (byte *)bufPtr, count, isFinal ? 1 : 0); } finally { if (!isFinal) // if count == 0 then GCHandle was not pinned { bufferHandle.Free(); } } switch (status) { case XMLStatus.OK: continue; case XMLStatus.ERROR: error = LibExpat.XMLGetErrorCode(parser); #if EXPAT_1_95_8_UP if (error == XMLError.ABORTED) { return(ParseStatus.Aborted); } #endif return(ParseStatus.FatalError); #if EXPAT_1_95_8_UP case XMLStatus.SUSPENDED: error = XMLError.NONE; return(ParseStatus.Suspended); #endif } } error = XMLError.NONE; return(ParseStatus.Finished); }
/* Resumable features */ #if EXPAT_1_95_8_UP public bool Suspend() { CheckNotDisposed(); bool result; XMLParser *parser = entityContext.XmlParser; result = LibExpat.XMLStopParser(parser, XMLBool.TRUE) == XMLStatus.OK; if (result) { entityContext.error = XMLError.NONE; } else { entityContext.error = LibExpat.XMLGetErrorCode(parser); } return(result); }
public bool Abort() { CheckNotDisposed(); bool result; XMLParser *parser = entityContext.XmlParser; // no cleanup here, as we are in the middle of a call-back result = LibExpat.XMLStopParser(parser, XMLBool.FALSE) == XMLStatus.OK; if (result) { entityContext.error = XMLError.NONE; } else { entityContext.error = LibExpat.XMLGetErrorCode(parser); } return(result); }
ResumeParsingBuffer(XMLParser *parser, ref ParseStatus parseStatus, out XMLError error) { bool result = true; XMLStatus status = LibExpat.XMLResumeParser(parser); switch (status) { case XMLStatus.OK: error = XMLError.NONE; XMLParsingStatus pStatus; LibExpat.XMLGetParsingStatus(parser, out pStatus); if (pStatus.FinalBuffer == XMLBool.FALSE) { result = false; } else { parseStatus = ParseStatus.Finished; } break; case XMLStatus.ERROR: error = LibExpat.XMLGetErrorCode(parser); if (error == XMLError.ABORTED) { parseStatus = ParseStatus.Aborted; } else { parseStatus = ParseStatus.FatalError; } break; case XMLStatus.SUSPENDED: error = XMLError.NONE; parseStatus = ParseStatus.Suspended; break; default: error = XMLError.NONE; break; } return(result); }
internal void InitAsChild(XMLParser *parentParser, char *context, string encoding) { Debug.Assert(parentParser != null); XMLParser *parser; lock (this) { if (xmlParser != null) { throw new ExpatException(ExpatParser <X, E, U> .InternalStateError); } RuntimeHelpers.PrepareConstrainedRegions(); try { } finally { parser = LibExpat.XMLExternalEntityParserCreate(parentParser, context, encoding); xmlParser = parser; GC.ReRegisterForFinalize(this); } } if (parser == null) { throw new OutOfMemoryException(); } }
// this sets all non-null handlers except for the UnknownEncodingHandler protected void ReInitializeHandlers(XMLParser *parser) { if (elementDeclHandler != null) { LibExpat.XMLSetElementDeclHandler(parser, elementDeclHandler); } if (attlistDeclHandler != null) { LibExpat.XMLSetAttlistDeclHandler(parser, attlistDeclHandler); } if (xmlDeclHandler != null) { LibExpat.XMLSetXmlDeclHandler(parser, xmlDeclHandler); } if (startElementHandler != null || endElementHandler != null) { LibExpat.XMLSetElementHandler(parser, startElementHandler, endElementHandler); } if (characterDataHandler != null) { LibExpat.XMLSetCharacterDataHandler(parser, characterDataHandler); } if (processingInstructionHandler != null) { LibExpat.XMLSetProcessingInstructionHandler(parser, processingInstructionHandler); } if (commentHandler != null) { LibExpat.XMLSetCommentHandler(parser, commentHandler); } if (startCdataSectionHandler != null || endCdataSectionHandler != null) { LibExpat.XMLSetCdataSectionHandler(parser, startCdataSectionHandler, endCdataSectionHandler); } if (defaultHandler != null) { if (skipInternal) { LibExpat.XMLSetDefaultHandler(parser, defaultHandler); } else { LibExpat.XMLSetDefaultHandlerExpand(parser, defaultHandler); } } if (startDoctypeDeclHandler != null || endDoctypeDeclHandler != null) { LibExpat.XMLSetDoctypeDeclHandler(parser, startDoctypeDeclHandler, endDoctypeDeclHandler); } if (entityDeclHandler != null) { LibExpat.XMLSetEntityDeclHandler(parser, entityDeclHandler); } if (notationDeclHandler != null) { LibExpat.XMLSetNotationDeclHandler(parser, notationDeclHandler); } if (startNamespaceDeclHandler != null || endNamespaceDeclHandler != null) { LibExpat.XMLSetNamespaceDeclHandler(parser, startNamespaceDeclHandler, endNamespaceDeclHandler); } if (notStandaloneHandler != null) { LibExpat.XMLSetNotStandaloneHandler(parser, notStandaloneHandler); } if (skippedEntityHandler != null) { LibExpat.XMLSetSkippedEntityHandler(parser, skippedEntityHandler); } // this is a private handler that always needs to be set LibExpat.XMLSetExternalEntityRefHandler(parser, externalEntityRefHandler); }
Parse(XMLParser *parser, ReadBuffer read, out XMLError error) { return(Parse(parser, read, DefaultReadBufSize, out error)); }