xref: /aosp_15_r20/external/libxml2/xlink.c (revision 7c5688314b92172186c154356a6374bf7684c3ca)
1 /*
2  * xlink.c : implementation of the hyperlinks detection module
3  *           This version supports both XML XLinks and HTML simple links
4  *
5  * See Copyright for the status of this software.
6  *
7  * [email protected]
8  */
9 
10 
11 #define IN_LIBXML
12 #include "libxml.h"
13 
14 #ifdef LIBXML_XPTR_ENABLED
15 #include <string.h> /* for memset() only */
16 #include <ctype.h>
17 #include <stdlib.h>
18 
19 #include <libxml/xmlmemory.h>
20 #include <libxml/tree.h>
21 #include <libxml/parser.h>
22 #include <libxml/xlink.h>
23 
24 #define XLINK_NAMESPACE (BAD_CAST "http://www.w3.org/1999/xlink/namespace/")
25 #define XHTML_NAMESPACE (BAD_CAST "http://www.w3.org/1999/xhtml/")
26 
27 /****************************************************************
28  *								*
29  *           Default setting and related functions		*
30  *								*
31  ****************************************************************/
32 
33 static xlinkHandlerPtr xlinkDefaultHandler = NULL;
34 static xlinkNodeDetectFunc	xlinkDefaultDetect = NULL;
35 
36 /**
37  * xlinkGetDefaultHandler:
38  *
39  * DEPRECATED: Don't use.
40  *
41  * Get the default xlink handler.
42  *
43  * Returns the current xlinkHandlerPtr value.
44  */
45 xlinkHandlerPtr
xlinkGetDefaultHandler(void)46 xlinkGetDefaultHandler(void) {
47     return(xlinkDefaultHandler);
48 }
49 
50 
51 /**
52  * xlinkSetDefaultHandler:
53  * @handler:  the new value for the xlink handler block
54  *
55  * DEPRECATED: Don't use.
56  *
57  * Set the default xlink handlers
58  */
59 void
xlinkSetDefaultHandler(xlinkHandlerPtr handler)60 xlinkSetDefaultHandler(xlinkHandlerPtr handler) {
61     xlinkDefaultHandler = handler;
62 }
63 
64 /**
65  * xlinkGetDefaultDetect:
66  *
67  * DEPRECATED: Don't use.
68  *
69  * Get the default xlink detection routine
70  *
71  * Returns the current function or NULL;
72  */
73 xlinkNodeDetectFunc
xlinkGetDefaultDetect(void)74 xlinkGetDefaultDetect	(void) {
75     return(xlinkDefaultDetect);
76 }
77 
78 /**
79  * xlinkSetDefaultDetect:
80  * @func: pointer to the new detection routine.
81  *
82  * DEPRECATED: Don't use.
83  *
84  * Set the default xlink detection routine
85  */
86 void
xlinkSetDefaultDetect(xlinkNodeDetectFunc func)87 xlinkSetDefaultDetect	(xlinkNodeDetectFunc func) {
88     xlinkDefaultDetect = func;
89 }
90 
91 /****************************************************************
92  *								*
93  *                  The detection routines			*
94  *								*
95  ****************************************************************/
96 
97 
98 /**
99  * xlinkIsLink:
100  * @doc:  the document containing the node
101  * @node:  the node pointer itself
102  *
103  * Check whether the given node carries the attributes needed
104  * to be a link element (or is one of the linking elements issued
105  * from the (X)HTML DtDs).
106  * This routine don't try to do full checking of the link validity
107  * but tries to detect and return the appropriate link type.
108  *
109  * Returns the xlinkType of the node (XLINK_TYPE_NONE if there is no
110  *         link detected.
111  */
112 xlinkType
xlinkIsLink(xmlDocPtr doc,xmlNodePtr node)113 xlinkIsLink	(xmlDocPtr doc, xmlNodePtr node) {
114     xmlChar *type = NULL, *role = NULL;
115     xlinkType ret = XLINK_TYPE_NONE;
116 
117     if (node == NULL) return(XLINK_TYPE_NONE);
118     if (doc == NULL) doc = node->doc;
119     if ((doc != NULL) && (doc->type == XML_HTML_DOCUMENT_NODE)) {
120         /*
121 	 * This is an HTML document.
122 	 */
123     } else if ((node->ns != NULL) &&
124                (xmlStrEqual(node->ns->href, XHTML_NAMESPACE))) {
125 	/*
126 	 * !!!! We really need an IS_XHTML_ELEMENT function from HTMLtree.h @@@
127 	 */
128         /*
129 	 * This is an XHTML element within an XML document
130 	 * Check whether it's one of the element able to carry links
131 	 * and in that case if it holds the attributes.
132 	 */
133     }
134 
135     /*
136      * We don't prevent a-priori having XML Linking constructs on
137      * XHTML elements
138      */
139     type = xmlGetNsProp(node, BAD_CAST"type", XLINK_NAMESPACE);
140     if (type != NULL) {
141 	if (xmlStrEqual(type, BAD_CAST "simple")) {
142             ret = XLINK_TYPE_SIMPLE;
143 	} else if (xmlStrEqual(type, BAD_CAST "extended")) {
144 	    role = xmlGetNsProp(node, BAD_CAST "role", XLINK_NAMESPACE);
145 	    if (role != NULL) {
146 		xmlNsPtr xlink;
147 		xlink = xmlSearchNs(doc, node, XLINK_NAMESPACE);
148 		if (xlink == NULL) {
149 		    /* Humm, fallback method */
150 		    if (xmlStrEqual(role, BAD_CAST"xlink:external-linkset"))
151 			ret = XLINK_TYPE_EXTENDED_SET;
152 		} else {
153 		    xmlChar buf[200];
154 		    snprintf((char *) buf, sizeof(buf), "%s:external-linkset",
155 			     (char *) xlink->prefix);
156                     buf[sizeof(buf) - 1] = 0;
157 		    if (xmlStrEqual(role, buf))
158 			ret = XLINK_TYPE_EXTENDED_SET;
159 
160 		}
161 
162 	    }
163 	    ret = XLINK_TYPE_EXTENDED;
164 	}
165     }
166 
167     if (type != NULL) xmlFree(type);
168     if (role != NULL) xmlFree(role);
169     return(ret);
170 }
171 #endif /* LIBXML_XPTR_ENABLED */
172