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