xlink.c 4.2 KB

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