1. package org.apache.xalan.templates;
  2. //import org.w3c.dom.*;
  3. import org.apache.xml.dtm.DTM;
  4. import org.xml.sax.*;
  5. import org.apache.xpath.*;
  6. import org.apache.xpath.Expression;
  7. import org.apache.xpath.objects.XObjectFactory;
  8. import org.apache.xpath.objects.XObject;
  9. import org.apache.xpath.objects.XString;
  10. import org.apache.xpath.objects.XRTreeFrag;
  11. import org.apache.xpath.objects.XRTreeFragSelectWrapper;
  12. import org.apache.xml.utils.QName;
  13. import org.apache.xalan.trace.SelectionEvent;
  14. import org.apache.xalan.res.XSLTErrorResources;
  15. import org.apache.xalan.transformer.TransformerImpl;
  16. import javax.xml.transform.TransformerException;
  17. /**
  18. * Handles the EXSLT result element within an EXSLT function element.
  19. */
  20. public class ElemExsltFuncResult extends ElemVariable
  21. {
  22. /**
  23. * Generate the EXSLT function return value, and assign it to the variable
  24. * index slot assigned for it in ElemExsltFunction compose().
  25. *
  26. */
  27. public void execute(TransformerImpl transformer) throws TransformerException
  28. {
  29. XPathContext context = transformer.getXPathContext();
  30. VariableStack varStack = context.getVarStack();
  31. // ElemExsltFunc result should always be within an ElemExsltFunction.
  32. ElemExsltFunction owner = getOwnerFunction();
  33. if (owner != null)
  34. {
  35. int resultIndex = owner.getResultIndex();
  36. // Verify that result has not already been set by another result
  37. // element. Recursion is allowed: intermediate results are cleared
  38. // in the owner ElemExsltFunction execute().
  39. if (varStack.isLocalSet(resultIndex))
  40. throw new TransformerException
  41. ("An EXSLT function cannot set more than one result!");
  42. int sourceNode = context.getCurrentNode();
  43. // Set the return value;
  44. XObject var = getValue(transformer, sourceNode);
  45. varStack.setLocalVariable(resultIndex, var);
  46. }
  47. }
  48. /**
  49. * Get an integer representation of the element type.
  50. *
  51. * @return An integer representation of the element, defined in the
  52. * Constants class.
  53. * @see org.apache.xalan.templates.Constants
  54. */
  55. public int getXSLToken()
  56. {
  57. return Constants.EXSLT_ELEMNAME_FUNCRESULT;
  58. }
  59. /**
  60. * Return the node name, defined in the
  61. * Constants class.
  62. * @see org.apache.xalan.templates.Constants.
  63. * @return The node name
  64. *
  65. */
  66. public String getNodeName()
  67. {
  68. return Constants.EXSLT_ELEMNAME_FUNCRESULT_STRING;
  69. }
  70. /**
  71. * Get the ElemExsltFunction that contains the ElemResult so we can set an ElemExsltFunction variable
  72. * to the local variable stack index to the return value.
  73. */
  74. public ElemExsltFunction getOwnerFunction()
  75. {
  76. ElemTemplateElement elem = this;
  77. while((elem != null) && !(elem instanceof ElemExsltFunction))
  78. {
  79. elem = elem.getParentElem();
  80. }
  81. return (ElemExsltFunction)elem;
  82. }
  83. }