xlink.c 4.5 KB

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