From: James Clark Date: Tue, 24 Nov 1998 05:42:17 +0000 (+0000) Subject: Add CDATA section handlers X-Git-Tag: V19981231~36 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=7b4a0046eac55c318ec0252b958a5b7c2b2df072;p=libexpat Add CDATA section handlers --- diff --git a/expat/xmlparse/xmlparse.c b/expat/xmlparse/xmlparse.c index 37f0aa79..e5e77309 100755 --- a/expat/xmlparse/xmlparse.c +++ b/expat/xmlparse/xmlparse.c @@ -268,6 +268,8 @@ typedef struct { XML_CharacterDataHandler characterDataHandler; XML_ProcessingInstructionHandler processingInstructionHandler; XML_CommentHandler commentHandler; + XML_StartCdataSectionHandler startCdataSectionHandler; + XML_EndCdataSectionHandler endCdataSectionHandler; XML_DefaultHandler defaultHandler; XML_UnparsedEntityDeclHandler unparsedEntityDeclHandler; XML_NotationDeclHandler notationDeclHandler; @@ -319,6 +321,8 @@ typedef struct { #define characterDataHandler (((Parser *)parser)->characterDataHandler) #define processingInstructionHandler (((Parser *)parser)->processingInstructionHandler) #define commentHandler (((Parser *)parser)->commentHandler) +#define startCdataSectionHandler (((Parser *)parser)->startCdataSectionHandler) +#define endCdataSectionHandler (((Parser *)parser)->endCdataSectionHandler) #define defaultHandler (((Parser *)parser)->defaultHandler) #define unparsedEntityDeclHandler (((Parser *)parser)->unparsedEntityDeclHandler) #define notationDeclHandler (((Parser *)parser)->notationDeclHandler) @@ -385,6 +389,8 @@ XML_Parser XML_ParserCreate(const XML_Char *encodingName) characterDataHandler = 0; processingInstructionHandler = 0; commentHandler = 0; + startCdataSectionHandler = 0; + endCdataSectionHandler = 0; defaultHandler = 0; unparsedEntityDeclHandler = 0; notationDeclHandler = 0; @@ -477,6 +483,8 @@ XML_Parser XML_ExternalEntityParserCreate(XML_Parser oldParser, XML_CharacterDataHandler oldCharacterDataHandler = characterDataHandler; XML_ProcessingInstructionHandler oldProcessingInstructionHandler = processingInstructionHandler; XML_CommentHandler oldCommentHandler = commentHandler; + XML_StartCdataSectionHandler oldStartCdataSectionHandler = startCdataSectionHandler; + XML_EndCdataSectionHandler oldEndCdataSectionHandler = endCdataSectionHandler; XML_DefaultHandler oldDefaultHandler = defaultHandler; XML_ExternalEntityRefHandler oldExternalEntityRefHandler = externalEntityRefHandler; XML_UnknownEncodingHandler oldUnknownEncodingHandler = unknownEncodingHandler; @@ -494,6 +502,8 @@ XML_Parser XML_ExternalEntityParserCreate(XML_Parser oldParser, characterDataHandler = oldCharacterDataHandler; processingInstructionHandler = oldProcessingInstructionHandler; commentHandler = oldCommentHandler; + startCdataSectionHandler = oldStartCdataSectionHandler; + endCdataSectionHandler = oldEndCdataSectionHandler; defaultHandler = oldDefaultHandler; externalEntityRefHandler = oldExternalEntityRefHandler; unknownEncodingHandler = oldUnknownEncodingHandler; @@ -612,6 +622,14 @@ void XML_SetCommentHandler(XML_Parser parser, commentHandler = handler; } +void XML_SetCdataSectionHandler(XML_Parser parser, + XML_StartCdataSectionHandler start, + XML_EndCdataSectionHandler end) +{ + startCdataSectionHandler = start; + endCdataSectionHandler = end; +} + void XML_SetDefaultHandler(XML_Parser parser, XML_DefaultHandler handler) { @@ -1255,8 +1273,23 @@ doContent(XML_Parser parser, case XML_TOK_CDATA_SECT_OPEN: { enum XML_Error result; - if (characterDataHandler) + if (startCdataSectionHandler) + startCdataSectionHandler(handlerArg); +#if 0 + /* Suppose you doing a transformation on a document that involves + changing only the character data. You set up a defaultHandler + and a characterDataHandler. The defaultHandler simply copies + characters through. The characterDataHandler does the transformation + and writes the characters out escaping them as necessary. This case + will fail to work if we leave out the following two lines (because & + and < inside CDATA sections will be incorrectly escaped). + + However, now we have a start/endCdataSectionHandler, so it seems + easier to let the user deal with this. */ + + else if (characterDataHandler) characterDataHandler(handlerArg, dataBuf, 0); +#endif else if (defaultHandler) reportDefault(parser, enc, s, next); result = doCdataSection(parser, enc, &next, end, nextPtr); @@ -1614,8 +1647,13 @@ enum XML_Error doCdataSection(XML_Parser parser, *eventEndPP = next; switch (tok) { case XML_TOK_CDATA_SECT_CLOSE: - if (characterDataHandler) + if (endCdataSectionHandler) + endCdataSectionHandler(handlerArg); +#if 0 + /* see comment under XML_TOK_CDATA_SECT_OPEN */ + else if (characterDataHandler) characterDataHandler(handlerArg, dataBuf, 0); +#endif else if (defaultHandler) reportDefault(parser, enc, s, next); *startPtr = next; diff --git a/expat/xmlparse/xmlparse.h b/expat/xmlparse/xmlparse.h index 57549e52..473f2afe 100755 --- a/expat/xmlparse/xmlparse.h +++ b/expat/xmlparse/xmlparse.h @@ -107,6 +107,9 @@ typedef void (*XML_ProcessingInstructionHandler)(void *userData, /* data is 0 terminated */ typedef void (*XML_CommentHandler)(void *userData, const XML_Char *data); +typedef void (*XML_StartCdataSectionHandler)(void *userData); +typedef void (*XML_EndCdataSectionHandler)(void *userData); + /* This is called for any characters in the XML document for which there is no applicable handler. This includes both characters that are part of markup which is of a kind that is @@ -255,6 +258,11 @@ void XMLPARSEAPI XML_SetCommentHandler(XML_Parser parser, XML_CommentHandler handler); +void XMLPARSEAPI +XML_SetCdataSectionHandler(XML_Parser parser, + XML_StartCdataSectionHandler start, + XML_EndCdataSectionHandler end); + /* This sets the default handler and also inhibits expansion of internal entities. The entity reference will be passed to the default handler. */ diff --git a/expat/xmlwf/xmlwf.c b/expat/xmlwf/xmlwf.c index d3c89de1..5525d7ba 100755 --- a/expat/xmlwf/xmlwf.c +++ b/expat/xmlwf/xmlwf.c @@ -334,6 +334,24 @@ void metaComment(XML_Parser parser, const XML_Char *data) fputts(T("/>\n"), fp); } +static +void metaStartCdataSection(XML_Parser parser) +{ + FILE *fp = XML_GetUserData(parser); + fputts(T("\n"), fp); +} + +static +void metaEndCdataSection(XML_Parser parser) +{ + FILE *fp = XML_GetUserData(parser); + fputts(T("\n"), fp); +} + static void metaCharacterData(XML_Parser parser, const XML_Char *s, int len) { @@ -695,6 +713,7 @@ int tmain(int argc, XML_Char **argv) XML_SetElementHandler(parser, metaStartElement, metaEndElement); XML_SetProcessingInstructionHandler(parser, metaProcessingInstruction); XML_SetCommentHandler(parser, metaComment); + XML_SetCdataSectionHandler(parser, metaStartCdataSection, metaEndCdataSection); XML_SetCharacterDataHandler(parser, metaCharacterData); XML_SetUnparsedEntityDeclHandler(parser, metaUnparsedEntityDecl); XML_SetNotationDeclHandler(parser, metaNotationDecl);