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 * daniel@veillard.com
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 * Get the default xlink handler.
40 *
41 * Returns the current xlinkHandlerPtr value.
42 */
43 xlinkHandlerPtr
44 xlinkGetDefaultHandler(void) {
45 return(xlinkDefaultHandler);
46 }
47
48
49 /**
50 * xlinkSetDefaultHandler:
51 * @handler: the new value for the xlink handler block
52 *
53 * Set the default xlink handlers
54 */
55 void
56 xlinkSetDefaultHandler(xlinkHandlerPtr handler) {
57 xlinkDefaultHandler = handler;
58 }
59
60 /**
61 * xlinkGetDefaultDetect:
62 *
63 * Get the default xlink detection routine
64 *
65 * Returns the current function or NULL;
66 */
67 xlinkNodeDetectFunc
68 xlinkGetDefaultDetect (void) {
69 return(xlinkDefaultDetect);
70 }
71
72 /**
73 * xlinkSetDefaultDetect:
74 * @func: pointer to the new detection routine.
75 *
76 * Set the default xlink detection routine
77 */
78 void
79 xlinkSetDefaultDetect (xlinkNodeDetectFunc func) {
80 xlinkDefaultDetect = func;
81 }
82
83 /****************************************************************
84 * *
85 * The detection routines *
86 * *
87 ****************************************************************/
88
89
90 /**
91 * xlinkIsLink:
92 * @doc: the document containing the node
93 * @node: the node pointer itself
94 *
95 * Check whether the given node carries the attributes needed
96 * to be a link element (or is one of the linking elements issued
97 * from the (X)HTML DtDs).
98 * This routine don't try to do full checking of the link validity
99 * but tries to detect and return the appropriate link type.
100 *
101 * Returns the xlinkType of the node (XLINK_TYPE_NONE if there is no
102 * link detected.
103 */
104 xlinkType
105 xlinkIsLink (xmlDocPtr doc, xmlNodePtr node) {
106 xmlChar *type = NULL, *role = NULL;
107 xlinkType ret = XLINK_TYPE_NONE;
108
109 if (node == NULL) return(XLINK_TYPE_NONE);
110 if (doc == NULL) doc = node->doc;
111 if ((doc != NULL) && (doc->type == XML_HTML_DOCUMENT_NODE)) {
112 /*
113 * This is an HTML document.
114 */
115 } else if ((node->ns != NULL) &&
116 (xmlStrEqual(node->ns->href, XHTML_NAMESPACE))) {
117 /*
118 * !!!! We really need an IS_XHTML_ELEMENT function from HTMLtree.h @@@
119 */
120 /*
121 * This is an XHTML element within an XML document
122 * Check whether it's one of the element able to carry links
123 * and in that case if it holds the attributes.
124 */
125 }
126
127 /*
128 * We don't prevent a-priori having XML Linking constructs on
129 * XHTML elements
130 */
131 type = xmlGetNsProp(node, BAD_CAST"type", XLINK_NAMESPACE);
132 if (type != NULL) {
133 if (xmlStrEqual(type, BAD_CAST "simple")) {
134 ret = XLINK_TYPE_SIMPLE;
135 } else if (xmlStrEqual(type, BAD_CAST "extended")) {
136 role = xmlGetNsProp(node, BAD_CAST "role", XLINK_NAMESPACE);
137 if (role != NULL) {
138 xmlNsPtr xlink;
139 xlink = xmlSearchNs(doc, node, XLINK_NAMESPACE);
140 if (xlink == NULL) {
141 /* Humm, fallback method */
142 if (xmlStrEqual(role, BAD_CAST"xlink:external-linkset"))
143 ret = XLINK_TYPE_EXTENDED_SET;
144 } else {
145 xmlChar buf[200];
146 snprintf((char *) buf, sizeof(buf), "%s:external-linkset",
147 (char *) xlink->prefix);
148 buf[sizeof(buf) - 1] = 0;
149 if (xmlStrEqual(role, buf))
150 ret = XLINK_TYPE_EXTENDED_SET;
151
152 }
153
154 }
155 ret = XLINK_TYPE_EXTENDED;
156 }
157 }
158
159 if (type != NULL) xmlFree(type);
160 if (role != NULL) xmlFree(role);
161 return(ret);
162 }
163 #endif /* LIBXML_XPTR_ENABLED */