blob: d0d8ea0a53b1a9b208682ef61cb880bf471c2f9b [file] [log] [blame]
Owen Taylor3473f882001-02-23 17:55:21 +00001/*
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 *
Daniel Veillardc5d64342001-06-24 12:13:24 +00007 * daniel@veillard.com
Owen Taylor3473f882001-02-23 17:55:21 +00008 */
9
10
Bjorn Reese70a9da52001-04-21 16:57:29 +000011#include "libxml.h"
Owen Taylor3473f882001-02-23 17:55:21 +000012
Owen Taylor3473f882001-02-23 17:55:21 +000013#include <string.h> /* for memset() only */
14#ifdef HAVE_CTYPE_H
15#include <ctype.h>
16#endif
17#ifdef HAVE_STDLIB_H
18#include <stdlib.h>
19#endif
20#ifdef HAVE_SYS_STAT_H
21#include <sys/stat.h>
22#endif
23#ifdef HAVE_FCNTL_H
24#include <fcntl.h>
25#endif
26#ifdef HAVE_UNISTD_H
27#include <unistd.h>
28#endif
29#ifdef HAVE_ZLIB_H
30#include <zlib.h>
31#endif
32
33#include <libxml/xmlmemory.h>
34#include <libxml/tree.h>
35#include <libxml/parser.h>
36#include <libxml/valid.h>
37#include <libxml/xlink.h>
Daniel Veillard3c01b1d2001-10-17 15:58:35 +000038#include <libxml/globals.h>
Owen Taylor3473f882001-02-23 17:55:21 +000039
40#define XLINK_NAMESPACE (BAD_CAST "http://www.w3.org/1999/xlink/namespace/")
41#define XHTML_NAMESPACE (BAD_CAST "http://www.w3.org/1999/xhtml/")
42
43/****************************************************************
44 * *
45 * Default setting and related functions *
46 * *
47 ****************************************************************/
48
Daniel Veillardd0463562001-10-13 09:15:48 +000049static xlinkHandlerPtr xlinkDefaultHandler = NULL;
50static xlinkNodeDetectFunc xlinkDefaultDetect = NULL;
Owen Taylor3473f882001-02-23 17:55:21 +000051
52/**
53 * xlinkGetDefaultHandler:
54 *
55 * Get the default xlink handler.
56 *
57 * Returns the current xlinkHandlerPtr value.
58 */
59xlinkHandlerPtr
60xlinkGetDefaultHandler(void) {
61 return(xlinkDefaultHandler);
62}
63
64
65/**
66 * xlinkSetDefaultHandler:
67 * @handler: the new value for the xlink handler block
68 *
69 * Set the default xlink handlers
70 */
71void
72xlinkSetDefaultHandler(xlinkHandlerPtr handler) {
73 xlinkDefaultHandler = handler;
74}
75
76/**
77 * xlinkGetDefaultDetect:
78 *
79 * Get the default xlink detection routine
80 *
81 * Returns the current function or NULL;
82 */
83xlinkNodeDetectFunc
84xlinkGetDefaultDetect (void) {
85 return(xlinkDefaultDetect);
86}
87
88/**
89 * xlinkSetDefaultDetect:
Daniel Veillardcbaf3992001-12-31 16:16:02 +000090 * @func: pointer to the new detection routine.
Owen Taylor3473f882001-02-23 17:55:21 +000091 *
92 * Set the default xlink detection routine
93 */
94void
95xlinkSetDefaultDetect (xlinkNodeDetectFunc func) {
96 xlinkDefaultDetect = func;
97}
98
99/****************************************************************
100 * *
101 * The detection routines *
102 * *
103 ****************************************************************/
104
105
106/**
107 * xlinkIsLink:
108 * @doc: the document containing the node
109 * @node: the node pointer itself
110 *
111 * Check whether the given node carries the attributes needed
112 * to be a link element (or is one of the linking elements issued
113 * from the (X)HTML DtDs).
114 * This routine don't try to do full checking of the link validity
115 * but tries to detect and return the appropriate link type.
116 *
117 * Returns the xlinkType of the node (XLINK_TYPE_NONE if there is no
118 * link detected.
119 */
120xlinkType
121xlinkIsLink (xmlDocPtr doc, xmlNodePtr node) {
122 xmlChar *type = NULL, *role = NULL;
123 xlinkType ret = XLINK_TYPE_NONE;
124
125 if (node == NULL) return(XLINK_TYPE_NONE);
126 if (doc == NULL) doc = node->doc;
127 if ((doc != NULL) && (doc->type == XML_HTML_DOCUMENT_NODE)) {
128 /*
129 * This is an HTML document.
130 */
131 } else if ((node->ns != NULL) &&
132 (xmlStrEqual(node->ns->href, XHTML_NAMESPACE))) {
133 /*
134 * !!!! We really need an IS_XHTML_ELEMENT function from HTMLtree.h @@@
135 */
136 /*
137 * This is an XHTML element within an XML document
138 * Check whether it's one of the element able to carry links
139 * and in that case if it holds the attributes.
140 */
141 }
142
143 /*
144 * We don't prevent a-priori having XML Linking constructs on
145 * XHTML elements
146 */
147 type = xmlGetNsProp(node, BAD_CAST"type", XLINK_NAMESPACE);
148 if (type != NULL) {
149 if (!xmlStrEqual(type, BAD_CAST "simple")) {
150 ret = XLINK_TYPE_SIMPLE;
151 } if (!xmlStrEqual(type, BAD_CAST "extended")) {
152 role = xmlGetNsProp(node, BAD_CAST "role", XLINK_NAMESPACE);
153 if (role != NULL) {
154 xmlNsPtr xlink;
155 xlink = xmlSearchNs(doc, node, XLINK_NAMESPACE);
156 if (xlink == NULL) {
157 /* Humm, fallback method */
158 if (xmlStrEqual(role, BAD_CAST"xlink:external-linkset"))
159 ret = XLINK_TYPE_EXTENDED_SET;
160 } else {
161 xmlChar buf[200];
Owen Taylor3473f882001-02-23 17:55:21 +0000162 snprintf((char *) buf, sizeof(buf), "%s:external-linkset",
163 (char *) xlink->prefix);
Owen Taylor3473f882001-02-23 17:55:21 +0000164 buf[sizeof(buf) - 1] = 0;
165 if (xmlStrEqual(role, buf))
166 ret = XLINK_TYPE_EXTENDED_SET;
167
168 }
169
170 }
171 ret = XLINK_TYPE_EXTENDED;
172 }
173 }
174
175 if (type != NULL) xmlFree(type);
176 if (role != NULL) xmlFree(role);
177 return(ret);
178}