2 * xlink.c : implementation of the hyperlinks detection module
3 * This version supports both XML XLinks and HTML simple links
5 * See Copyright for the status of this software.
14 #ifdef LIBXML_XPTR_ENABLED
15 #include <string.h> /* for memset() only */
22 #ifdef HAVE_SYS_STAT_H
35 #include <libxml/xmlmemory.h>
36 #include <libxml/tree.h>
37 #include <libxml/parser.h>
38 #include <libxml/valid.h>
39 #include <libxml/xlink.h>
40 #include <libxml/globals.h>
42 #define XLINK_NAMESPACE (BAD_CAST "http://www.w3.org/1999/xlink/namespace/")
43 #define XHTML_NAMESPACE (BAD_CAST "http://www.w3.org/1999/xhtml/")
45 /****************************************************************
47 * Default setting and related functions *
49 ****************************************************************/
51 static xlinkHandlerPtr xlinkDefaultHandler
= NULL
;
52 static xlinkNodeDetectFunc xlinkDefaultDetect
= NULL
;
55 * xlinkGetDefaultHandler:
57 * Get the default xlink handler.
59 * Returns the current xlinkHandlerPtr value.
62 xlinkGetDefaultHandler(void) {
63 return(xlinkDefaultHandler
);
68 * xlinkSetDefaultHandler:
69 * @handler: the new value for the xlink handler block
71 * Set the default xlink handlers
74 xlinkSetDefaultHandler(xlinkHandlerPtr handler
) {
75 xlinkDefaultHandler
= handler
;
79 * xlinkGetDefaultDetect:
81 * Get the default xlink detection routine
83 * Returns the current function or NULL;
86 xlinkGetDefaultDetect (void) {
87 return(xlinkDefaultDetect
);
91 * xlinkSetDefaultDetect:
92 * @func: pointer to the new detection routine.
94 * Set the default xlink detection routine
97 xlinkSetDefaultDetect (xlinkNodeDetectFunc func
) {
98 xlinkDefaultDetect
= func
;
101 /****************************************************************
103 * The detection routines *
105 ****************************************************************/
110 * @doc: the document containing the node
111 * @node: the node pointer itself
113 * Check whether the given node carries the attributes needed
114 * to be a link element (or is one of the linking elements issued
115 * from the (X)HTML DtDs).
116 * This routine don't try to do full checking of the link validity
117 * but tries to detect and return the appropriate link type.
119 * Returns the xlinkType of the node (XLINK_TYPE_NONE if there is no
123 xlinkIsLink (xmlDocPtr doc
, xmlNodePtr node
) {
124 xmlChar
*type
= NULL
, *role
= NULL
;
125 xlinkType ret
= XLINK_TYPE_NONE
;
127 if (node
== NULL
) return(XLINK_TYPE_NONE
);
128 if (doc
== NULL
) doc
= node
->doc
;
129 if ((doc
!= NULL
) && (doc
->type
== XML_HTML_DOCUMENT_NODE
)) {
131 * This is an HTML document.
133 } else if ((node
->ns
!= NULL
) &&
134 (xmlStrEqual(node
->ns
->href
, XHTML_NAMESPACE
))) {
136 * !!!! We really need an IS_XHTML_ELEMENT function from HTMLtree.h @@@
139 * This is an XHTML element within an XML document
140 * Check whether it's one of the element able to carry links
141 * and in that case if it holds the attributes.
146 * We don't prevent a-priori having XML Linking constructs on
149 type
= xmlGetNsProp(node
, BAD_CAST
"type", XLINK_NAMESPACE
);
151 if (xmlStrEqual(type
, BAD_CAST
"simple")) {
152 ret
= XLINK_TYPE_SIMPLE
;
153 } else if (xmlStrEqual(type
, BAD_CAST
"extended")) {
154 role
= xmlGetNsProp(node
, BAD_CAST
"role", XLINK_NAMESPACE
);
157 xlink
= xmlSearchNs(doc
, node
, XLINK_NAMESPACE
);
159 /* Humm, fallback method */
160 if (xmlStrEqual(role
, BAD_CAST
"xlink:external-linkset"))
161 ret
= XLINK_TYPE_EXTENDED_SET
;
164 snprintf((char *) buf
, sizeof(buf
), "%s:external-linkset",
165 (char *) xlink
->prefix
);
166 buf
[sizeof(buf
) - 1] = 0;
167 if (xmlStrEqual(role
, buf
))
168 ret
= XLINK_TYPE_EXTENDED_SET
;
173 ret
= XLINK_TYPE_EXTENDED
;
177 if (type
!= NULL
) xmlFree(type
);
178 if (role
!= NULL
) xmlFree(role
);
181 #endif /* LIBXML_XPTR_ENABLED */
183 #include "elfgcchack.h"