extra.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. * extra.c: Implementation of non-standard features
  3. *
  4. * Reference:
  5. * Michael Kay "XSLT Programmer's Reference" pp 637-643
  6. * The node-set() extension function
  7. *
  8. * See Copyright for the status of this software.
  9. *
  10. * daniel@veillard.com
  11. */
  12. #define IN_LIBXSLT
  13. #include "libxslt.h"
  14. #include <string.h>
  15. #ifdef HAVE_STDLIB_H
  16. #include <stdlib.h>
  17. #endif
  18. #include <libxml/xmlmemory.h>
  19. #include <libxml/tree.h>
  20. #include <libxml/hash.h>
  21. #include <libxml/xmlerror.h>
  22. #include <libxml/parserInternals.h>
  23. #include "xslt.h"
  24. #include "xsltInternals.h"
  25. #include "xsltutils.h"
  26. #include "extensions.h"
  27. #include "variables.h"
  28. #include "transform.h"
  29. #include "extra.h"
  30. #include "preproc.h"
  31. #ifdef WITH_XSLT_DEBUG
  32. #define WITH_XSLT_DEBUG_EXTRA
  33. #endif
  34. /************************************************************************
  35. * *
  36. * Handling of XSLT debugging *
  37. * *
  38. ************************************************************************/
  39. /**
  40. * xsltDebug:
  41. * @ctxt: an XSLT processing context
  42. * @node: The current node
  43. * @inst: the instruction in the stylesheet
  44. * @comp: precomputed information
  45. *
  46. * Process an debug node
  47. */
  48. void
  49. xsltDebug(xsltTransformContextPtr ctxt, xmlNodePtr node ATTRIBUTE_UNUSED,
  50. xmlNodePtr inst ATTRIBUTE_UNUSED,
  51. xsltElemPreCompPtr comp ATTRIBUTE_UNUSED)
  52. {
  53. int i, j;
  54. xsltGenericError(xsltGenericErrorContext, "Templates:\n");
  55. for (i = 0, j = ctxt->templNr - 1; ((i < 15) && (j >= 0)); i++, j--) {
  56. xsltGenericError(xsltGenericErrorContext, "#%d ", i);
  57. if (ctxt->templTab[j]->name != NULL)
  58. xsltGenericError(xsltGenericErrorContext, "name %s ",
  59. ctxt->templTab[j]->name);
  60. if (ctxt->templTab[j]->match != NULL)
  61. xsltGenericError(xsltGenericErrorContext, "name %s ",
  62. ctxt->templTab[j]->match);
  63. if (ctxt->templTab[j]->mode != NULL)
  64. xsltGenericError(xsltGenericErrorContext, "name %s ",
  65. ctxt->templTab[j]->mode);
  66. xsltGenericError(xsltGenericErrorContext, "\n");
  67. }
  68. xsltGenericError(xsltGenericErrorContext, "Variables:\n");
  69. for (i = 0, j = ctxt->varsNr - 1; ((i < 15) && (j >= 0)); i++, j--) {
  70. xsltStackElemPtr cur;
  71. if (ctxt->varsTab[j] == NULL)
  72. continue;
  73. xsltGenericError(xsltGenericErrorContext, "#%d\n", i);
  74. cur = ctxt->varsTab[j];
  75. while (cur != NULL) {
  76. if (cur->comp == NULL) {
  77. xsltGenericError(xsltGenericErrorContext,
  78. "corrupted !!!\n");
  79. } else if (cur->comp->type == XSLT_FUNC_PARAM) {
  80. xsltGenericError(xsltGenericErrorContext, "param ");
  81. } else if (cur->comp->type == XSLT_FUNC_VARIABLE) {
  82. xsltGenericError(xsltGenericErrorContext, "var ");
  83. }
  84. if (cur->name != NULL)
  85. xsltGenericError(xsltGenericErrorContext, "%s ",
  86. cur->name);
  87. else
  88. xsltGenericError(xsltGenericErrorContext, "noname !!!!");
  89. #ifdef LIBXML_DEBUG_ENABLED
  90. if (cur->value != NULL) {
  91. if ((xsltGenericDebugContext == stdout) ||
  92. (xsltGenericDebugContext == stderr))
  93. xmlXPathDebugDumpObject((FILE*)xsltGenericDebugContext,
  94. cur->value, 1);
  95. } else {
  96. xsltGenericError(xsltGenericErrorContext, "NULL !!!!");
  97. }
  98. #endif
  99. xsltGenericError(xsltGenericErrorContext, "\n");
  100. cur = cur->next;
  101. }
  102. }
  103. }
  104. /************************************************************************
  105. * *
  106. * Classic extensions as described by M. Kay *
  107. * *
  108. ************************************************************************/
  109. /**
  110. * xsltFunctionNodeSet:
  111. * @ctxt: the XPath Parser context
  112. * @nargs: the number of arguments
  113. *
  114. * Implement the node-set() XSLT function
  115. * node-set node-set(result-tree)
  116. *
  117. * This function is available in libxslt, saxon or xt namespace.
  118. */
  119. void
  120. xsltFunctionNodeSet(xmlXPathParserContextPtr ctxt, int nargs){
  121. if (nargs != 1) {
  122. xsltTransformError(xsltXPathGetTransformContext(ctxt), NULL, NULL,
  123. "node-set() : expects one result-tree arg\n");
  124. ctxt->error = XPATH_INVALID_ARITY;
  125. return;
  126. }
  127. if ((ctxt->value == NULL) ||
  128. ((ctxt->value->type != XPATH_XSLT_TREE) &&
  129. (ctxt->value->type != XPATH_NODESET))) {
  130. xsltTransformError(xsltXPathGetTransformContext(ctxt), NULL, NULL,
  131. "node-set() invalid arg expecting a result tree\n");
  132. ctxt->error = XPATH_INVALID_TYPE;
  133. return;
  134. }
  135. if (ctxt->value->type == XPATH_XSLT_TREE) {
  136. ctxt->value->type = XPATH_NODESET;
  137. }
  138. }
  139. /**
  140. * xsltRegisterExtras:
  141. * @ctxt: a XSLT process context
  142. *
  143. * Registers the built-in extensions. This function is deprecated, use
  144. * xsltRegisterAllExtras instead.
  145. */
  146. void
  147. xsltRegisterExtras(xsltTransformContextPtr ctxt ATTRIBUTE_UNUSED) {
  148. xsltRegisterAllExtras();
  149. }
  150. /**
  151. * xsltRegisterAllExtras:
  152. *
  153. * Registers the built-in extensions
  154. */
  155. void
  156. xsltRegisterAllExtras (void) {
  157. xsltRegisterExtModuleFunction((const xmlChar *) "node-set",
  158. XSLT_LIBXSLT_NAMESPACE,
  159. xsltFunctionNodeSet);
  160. xsltRegisterExtModuleFunction((const xmlChar *) "node-set",
  161. XSLT_SAXON_NAMESPACE,
  162. xsltFunctionNodeSet);
  163. xsltRegisterExtModuleFunction((const xmlChar *) "node-set",
  164. XSLT_XT_NAMESPACE,
  165. xsltFunctionNodeSet);
  166. xsltRegisterExtModuleElement((const xmlChar *) "debug",
  167. XSLT_LIBXSLT_NAMESPACE,
  168. NULL,
  169. xsltDebug);
  170. xsltRegisterExtModuleElement((const xmlChar *) "output",
  171. XSLT_SAXON_NAMESPACE,
  172. xsltDocumentComp,
  173. xsltDocumentElem);
  174. xsltRegisterExtModuleElement((const xmlChar *) "write",
  175. XSLT_XALAN_NAMESPACE,
  176. xsltDocumentComp,
  177. xsltDocumentElem);
  178. xsltRegisterExtModuleElement((const xmlChar *) "document",
  179. XSLT_XT_NAMESPACE,
  180. xsltDocumentComp,
  181. xsltDocumentElem);
  182. xsltRegisterExtModuleElement((const xmlChar *) "document",
  183. XSLT_NAMESPACE,
  184. xsltDocumentComp,
  185. xsltDocumentElem);
  186. }