1
0
mirror of https://github.com/GNOME/libxml2.git synced 2025-10-14 02:58:39 +08:00

parser: Add more parser context accessors

The only thing remaining is access to parser input, see #762.
This commit is contained in:
Nick Wellnhofer
2025-07-22 20:50:13 +02:00
parent a2562b9d8a
commit a7fc9e1add
3 changed files with 125 additions and 17 deletions

View File

@@ -243,9 +243,10 @@ struct _xmlParserCtxt {
*/
struct _xmlSAXHandler *sax;
/**
* @deprecated Use #xmlCtxtGetUserData
*
* user data for SAX interface, defaults to the context itself
*/
/* TODO: Add accessor */
void *userData;
/**
* @deprecated Use xmlCtxtGetDocument()
@@ -308,16 +309,10 @@ struct _xmlParserCtxt {
/* Node analysis stack only used for DOM building */
/**
* @deprecated Use #xmlCtxtGetNode
*
* The current element.
*
* This is only valid and useful if the default SAX callbacks
* which build a document tree are intercepted. This mode of
* operation is fragile and discouraged.
*
* Contains the current element whose content is being parsed,
* or NULL if the parser is in top-level or DTD content.
*/
/* TODO: Add accessor */
xmlNode *node;
/* Depth of the parsing stack */
int nodeNr XML_DEPRECATED_MEMBER;
@@ -370,10 +365,11 @@ struct _xmlParserCtxt {
int token XML_DEPRECATED_MEMBER;
/**
* @deprecated Don't use
*
* The main document URI, if available, with its last
* component stripped.
*/
/* TODO: Add accessor */
char *directory;
/* Node name stack */
@@ -404,38 +400,36 @@ struct _xmlParserCtxt {
*/
int disableSAX XML_DEPRECATED_MEMBER;
/**
* @deprecated Use xmlCtxtIsInSubset
*
* Set if DTD content is parsed.
*
* - 0: not in DTD
* - 1: in internal DTD subset
* - 2: in external DTD subset
*/
/* TODO: Add accessor */
int inSubset;
/**
* @deprecated Use the `name` argument of the
* `internalSubset` SAX callback.
* `internalSubset` SAX callback or #xmlCtxtGetDocTypeDecl
*
* Name of the internal subset (root element type).
*/
/* TODO: Add accessor */
const xmlChar *intSubName;
/**
* @deprecated Use the `systemId` argument of the
* `internalSubset` SAX callback.
* `internalSubset` SAX callback or #xmlCtxtGetDocTypeDecl
*
* System identifier (URI) of external the subset.
*/
/* TODO: Add accessor */
xmlChar *extSubURI;
/**
* @deprecated Use the `publicId` argument of the
* `internalSubset` SAX callback.
* `internalSubset` SAX callback or #xmlCtxtGetDocTypeDecl
*
* This member is MISNAMED. It contains the *public* identifier
* of the external subset.
*/
/* TODO: Add accessor */
xmlChar *extSubSystem;
/* xml:space values */
@@ -1909,6 +1903,8 @@ XMLPUBFUN int
xmlCtxtIsHtml (xmlParserCtxt *ctxt);
XMLPUBFUN int
xmlCtxtIsStopped (xmlParserCtxt *ctxt);
XMLPUBFUN int
xmlCtxtIsInSubset (xmlParserCtxt *ctxt);
#ifdef LIBXML_VALID_ENABLED
XMLPUBFUN xmlValidCtxt *
xmlCtxtGetValidCtxt (xmlParserCtxt *ctxt);
@@ -1921,6 +1917,15 @@ XMLPUBFUN int
xmlCtxtGetStandalone (xmlParserCtxt *ctxt);
XMLPUBFUN xmlParserStatus
xmlCtxtGetStatus (xmlParserCtxt *ctxt);
XMLPUBFUN void *
xmlCtxtGetUserData (xmlParserCtxt *ctxt);
XMLPUBFUN xmlNode *
xmlCtxtGetNode (xmlParserCtxt *ctxt);
XMLPUBFUN int
xmlCtxtGetDocTypeDecl (xmlParserCtxt *ctxt,
const xmlChar **name,
const xmlChar **systemId,
const xmlChar **publicId);
XMLPUBFUN void
xmlCtxtSetErrorHandler (xmlParserCtxt *ctxt,
xmlStructuredErrorFunc handler,

View File

@@ -3211,6 +3211,30 @@ xmlCtxtIsStopped(xmlParserCtxt *ctxt) {
return(ctxt->disableSAX != 0);
}
/**
* Check whether a DTD subset is being parsed.
*
* Should only be used by SAX callbacks.
*
* Return values are
*
* - 0: not in DTD
* - 1: in internal DTD subset
* - 2: in external DTD subset
*
* @since 2.15.0
*
* @param ctxt parser context
* @returns the subset status
*/
int
xmlCtxtIsInSubset(xmlParserCtxt *ctxt) {
if (ctxt == NULL)
return(0);
return(ctxt->inSubset);
}
#ifdef LIBXML_VALID_ENABLED
/**
* @since 2.14.0
@@ -3227,6 +3251,81 @@ xmlCtxtGetValidCtxt(xmlParserCtxt *ctxt) {
}
#endif
/**
* Return user data.
*
* Return user data of a custom SAX parser or the parser context
* itself if unset.
*
* @since 2.15.0
*
* @param ctxt parser context
* @returns the user data.
*/
void *
xmlCtxtGetUserData(xmlParserCtxt *ctxt) {
if (ctxt == NULL)
return NULL;
return ctxt->userData;
}
/**
* Return the current node being parsed.
*
* This is only useful if the default SAX callbacks which build
* a document tree are intercepted. This mode of operation is
* fragile and discouraged.
*
* Returns the current element node, or the document node if no
* element was parsed yet.
*
* @since 2.15.0
*
* @param ctxt parser context
* @returns the current node.
*/
xmlNode *
xmlCtxtGetNode(xmlParserCtxt *ctxt) {
if (ctxt == NULL)
return NULL;
if (ctxt->node != NULL)
return ctxt->node;
return (xmlNode *) ctxt->myDoc;
}
/**
* Return data from the doctype declaration.
*
* Should only be used by SAX callbacks.
*
* @since 2.15.0
*
* @param ctxt parser context
* @param name name of the root element (output)
* @param systemId system ID (URI) of the external subset (output)
* @param publicId public ID of the external subset (output)
* @returns 0 on success, -1 if argument is invalid
*/
int
xmlCtxtGetDocTypeDecl(xmlParserCtxt *ctxt,
const xmlChar **name,
const xmlChar **systemId,
const xmlChar **publicId) {
if (ctxt == NULL)
return -1;
if (name != NULL)
*name = ctxt->intSubName;
if (systemId != NULL)
*systemId = ctxt->extSubURI;
if (publicId != NULL)
*publicId = ctxt->extSubSystem; /* The member is misnamed */
return 0;
}
/************************************************************************
* *
* Handling of node information *

View File

@@ -144,15 +144,19 @@ main(int argc ATTRIBUTE_UNUSED, char **argv ATTRIBUTE_UNUSED) {
xmlCtxtGetCatalogs(NULL);
xmlCtxtGetDeclaredEncoding(NULL);
xmlDictFree(xmlCtxtGetDict(NULL));
xmlCtxtGetDocTypeDecl(NULL, NULL, NULL, NULL);
xmlFreeDoc(xmlCtxtGetDocument(NULL));
xmlCtxtGetLastError(NULL);
xmlFreeNode(xmlCtxtGetNode(NULL));
xmlCtxtGetOptions(NULL);
xmlCtxtGetPrivate(NULL);
xmlCtxtGetSaxHandler(NULL);
xmlCtxtGetStandalone(NULL);
xmlCtxtGetStatus(NULL);
xmlCtxtGetUserData(NULL);
xmlCtxtGetVersion(NULL);
xmlCtxtIsHtml(NULL);
xmlCtxtIsInSubset(NULL);
xmlCtxtIsStopped(NULL);
xmlFreeNode(xmlCtxtParseContent(NULL, NULL, NULL, 0));
xmlFreeDoc(xmlCtxtParseDocument(NULL, NULL));