2001-02-23 17:55:21 +00:00
/*
* xlink . c : implementation of the hyperlinks detection module
* This version supports both XML XLinks and HTML simple links
*
* See Copyright for the status of this software .
*
2001-06-24 12:13:24 +00:00
* daniel @ veillard . com
2001-02-23 17:55:21 +00:00
*/
2002-03-18 19:37:11 +00:00
# define IN_LIBXML
2001-04-21 16:57:29 +00:00
# include "libxml.h"
2001-02-23 17:55:21 +00:00
2003-09-28 18:58:27 +00:00
# ifdef LIBXML_XPTR_ENABLED
2001-02-23 17:55:21 +00:00
# include <string.h> /* for memset() only */
# include <ctype.h>
# include <stdlib.h>
2022-03-02 00:29:17 +01:00
2001-02-23 17:55:21 +00:00
# include <libxml/xmlmemory.h>
# include <libxml/tree.h>
# include <libxml/parser.h>
# include <libxml/xlink.h>
# define XLINK_NAMESPACE (BAD_CAST "http: //www.w3.org/1999/xlink/namespace/")
# define XHTML_NAMESPACE (BAD_CAST "http: //www.w3.org/1999/xhtml/")
/****************************************************************
* *
* Default setting and related functions *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2012-09-11 13:26:36 +08:00
2001-10-13 09:15:48 +00:00
static xlinkHandlerPtr xlinkDefaultHandler = NULL ;
static xlinkNodeDetectFunc xlinkDefaultDetect = NULL ;
2001-02-23 17:55:21 +00:00
/**
* xlinkGetDefaultHandler :
*
2024-06-12 19:43:22 +02:00
* DEPRECATED : Don ' t use .
*
2001-02-23 17:55:21 +00:00
* Get the default xlink handler .
*
* Returns the current xlinkHandlerPtr value .
*/
xlinkHandlerPtr
xlinkGetDefaultHandler ( void ) {
return ( xlinkDefaultHandler ) ;
}
/**
* xlinkSetDefaultHandler :
* @ handler : the new value for the xlink handler block
*
2024-06-12 19:43:22 +02:00
* DEPRECATED : Don ' t use .
*
2001-02-23 17:55:21 +00:00
* Set the default xlink handlers
*/
void
xlinkSetDefaultHandler ( xlinkHandlerPtr handler ) {
xlinkDefaultHandler = handler ;
}
/**
* xlinkGetDefaultDetect :
*
2024-06-12 19:55:47 +02:00
* DEPRECATED : Don ' t use .
*
2001-02-23 17:55:21 +00:00
* Get the default xlink detection routine
*
* Returns the current function or NULL ;
*/
xlinkNodeDetectFunc
xlinkGetDefaultDetect ( void ) {
return ( xlinkDefaultDetect ) ;
}
/**
* xlinkSetDefaultDetect :
2001-12-31 16:16:02 +00:00
* @ func : pointer to the new detection routine .
2001-02-23 17:55:21 +00:00
*
2024-06-12 19:55:47 +02:00
* DEPRECATED : Don ' t use .
*
2001-02-23 17:55:21 +00:00
* Set the default xlink detection routine
*/
2012-09-11 13:26:36 +08:00
void
2001-02-23 17:55:21 +00:00
xlinkSetDefaultDetect ( xlinkNodeDetectFunc func ) {
xlinkDefaultDetect = func ;
}
/****************************************************************
* *
* The detection routines *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2012-09-11 13:26:36 +08:00
2001-02-23 17:55:21 +00:00
/**
* xlinkIsLink :
* @ doc : the document containing the node
* @ node : the node pointer itself
*
* Check whether the given node carries the attributes needed
* to be a link element ( or is one of the linking elements issued
* from the ( X ) HTML DtDs ) .
* This routine don ' t try to do full checking of the link validity
* but tries to detect and return the appropriate link type .
*
* Returns the xlinkType of the node ( XLINK_TYPE_NONE if there is no
* link detected .
*/
2012-09-11 13:26:36 +08:00
xlinkType
2001-02-23 17:55:21 +00:00
xlinkIsLink ( xmlDocPtr doc , xmlNodePtr node ) {
xmlChar * type = NULL , * role = NULL ;
xlinkType ret = XLINK_TYPE_NONE ;
if ( node = = NULL ) return ( XLINK_TYPE_NONE ) ;
if ( doc = = NULL ) doc = node - > doc ;
if ( ( doc ! = NULL ) & & ( doc - > type = = XML_HTML_DOCUMENT_NODE ) ) {
/*
* This is an HTML document .
*/
} else if ( ( node - > ns ! = NULL ) & &
( xmlStrEqual ( node - > ns - > href , XHTML_NAMESPACE ) ) ) {
/*
* ! ! ! ! We really need an IS_XHTML_ELEMENT function from HTMLtree . h @ @ @
*/
/*
* This is an XHTML element within an XML document
* Check whether it ' s one of the element able to carry links
* and in that case if it holds the attributes .
*/
}
/*
* We don ' t prevent a - priori having XML Linking constructs on
* XHTML elements
*/
type = xmlGetNsProp ( node , BAD_CAST " type " , XLINK_NAMESPACE ) ;
if ( type ! = NULL ) {
2003-02-07 12:38:22 +00:00
if ( xmlStrEqual ( type , BAD_CAST " simple " ) ) {
2001-02-23 17:55:21 +00:00
ret = XLINK_TYPE_SIMPLE ;
2013-07-02 09:47:26 +08:00
} else if ( xmlStrEqual ( type , BAD_CAST " extended " ) ) {
2001-02-23 17:55:21 +00:00
role = xmlGetNsProp ( node , BAD_CAST " role " , XLINK_NAMESPACE ) ;
if ( role ! = NULL ) {
xmlNsPtr xlink ;
xlink = xmlSearchNs ( doc , node , XLINK_NAMESPACE ) ;
if ( xlink = = NULL ) {
/* Humm, fallback method */
2012-09-11 13:26:36 +08:00
if ( xmlStrEqual ( role , BAD_CAST " xlink:external-linkset " ) )
2001-02-23 17:55:21 +00:00
ret = XLINK_TYPE_EXTENDED_SET ;
} else {
xmlChar buf [ 200 ] ;
snprintf ( ( char * ) buf , sizeof ( buf ) , " %s:external-linkset " ,
( char * ) xlink - > prefix ) ;
buf [ sizeof ( buf ) - 1 ] = 0 ;
if ( xmlStrEqual ( role , buf ) )
ret = XLINK_TYPE_EXTENDED_SET ;
}
}
ret = XLINK_TYPE_EXTENDED ;
}
}
if ( type ! = NULL ) xmlFree ( type ) ;
if ( role ! = NULL ) xmlFree ( role ) ;
return ( ret ) ;
}
2003-09-28 18:58:27 +00:00
# endif /* LIBXML_XPTR_ENABLED */