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