CallTemplateNode.java 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /* CallTemplateNode.java --
  2. Copyright (C) 2004,2006 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package gnu.xml.transform;
  32. import gnu.java.lang.CPStringBuilder;
  33. import java.util.ArrayList;
  34. import java.util.Iterator;
  35. import java.util.LinkedList;
  36. import java.util.List;
  37. import javax.xml.namespace.QName;
  38. import javax.xml.transform.TransformerException;
  39. import org.w3c.dom.Node;
  40. /**
  41. * A template node representing the XSL <code>call-template</code>
  42. * instruction.
  43. *
  44. * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
  45. */
  46. final class CallTemplateNode
  47. extends TemplateNode
  48. {
  49. final QName name;
  50. final List withParams;
  51. CallTemplateNode(QName name, List withParams)
  52. {
  53. this.name = name;
  54. this.withParams = withParams;
  55. }
  56. TemplateNode clone(Stylesheet stylesheet)
  57. {
  58. int len = withParams.size();
  59. List withParams2 = new ArrayList(len);
  60. for (int i = 0; i < len; i++)
  61. withParams2.add(((WithParam) withParams.get(i)).clone(stylesheet));
  62. TemplateNode ret = new CallTemplateNode(name, withParams2);
  63. if (children != null)
  64. ret.children = children.clone(stylesheet);
  65. if (next != null)
  66. ret.next = next.clone(stylesheet);
  67. return ret;
  68. }
  69. void doApply(Stylesheet stylesheet, QName mode,
  70. Node context, int pos, int len,
  71. Node parent, Node nextSibling)
  72. throws TransformerException
  73. {
  74. TemplateNode t = stylesheet.getTemplate(mode, name);
  75. if (t != null)
  76. {
  77. if (!withParams.isEmpty())
  78. {
  79. // compute the parameter values
  80. LinkedList values = new LinkedList();
  81. for (Iterator i = withParams.iterator(); i.hasNext(); )
  82. {
  83. WithParam p = (WithParam) i.next();
  84. if (t.hasParam(p.name)) // ignore parameters not specified
  85. {
  86. Object value = p.getValue(stylesheet, mode, context,
  87. pos, len);
  88. Object[] pair = new Object[2];
  89. pair[0] = p.name;
  90. pair[1] = value;
  91. values.add(pair);
  92. }
  93. }
  94. // push the parameter context
  95. stylesheet.bindings.push(Bindings.WITH_PARAM);
  96. // set the parameters
  97. for (Iterator i = values.iterator(); i.hasNext(); )
  98. {
  99. Object[] pair = (Object[]) i.next();
  100. QName name = (QName) pair[0];
  101. Object value = pair[1];
  102. stylesheet.bindings.set(name, value, Bindings.WITH_PARAM);
  103. if (stylesheet.debug)
  104. System.err.println("with-param: " + name + " = " + value);
  105. }
  106. }
  107. t.apply(stylesheet, mode, context, pos, len,
  108. parent, nextSibling);
  109. if (!withParams.isEmpty())
  110. {
  111. // pop the variable context
  112. stylesheet.bindings.pop(Bindings.WITH_PARAM);
  113. }
  114. }
  115. // call-template doesn't have processable children
  116. if (next != null)
  117. next.apply(stylesheet, mode,
  118. context, pos, len,
  119. parent, nextSibling);
  120. }
  121. public boolean references(QName var)
  122. {
  123. for (Iterator i = withParams.iterator(); i.hasNext(); )
  124. {
  125. if (((WithParam) i.next()).references(var))
  126. return true;
  127. }
  128. return super.references(var);
  129. }
  130. public String toString()
  131. {
  132. CPStringBuilder buf = new CPStringBuilder("call-template");
  133. buf.append('[');
  134. buf.append("name=");
  135. buf.append(name);
  136. buf.append(']');
  137. return buf.toString();
  138. }
  139. }