Template.java 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /* Template.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.io.PrintStream;
  34. import javax.xml.namespace.QName;
  35. import javax.xml.transform.TransformerException;
  36. import org.w3c.dom.Node;
  37. import gnu.xml.xpath.Expr;
  38. import gnu.xml.xpath.NameTest;
  39. import gnu.xml.xpath.NodeTypeTest;
  40. import gnu.xml.xpath.Pattern;
  41. import gnu.xml.xpath.Selector;
  42. import gnu.xml.xpath.Test;
  43. /**
  44. * A template in an XSL stylesheet.
  45. *
  46. * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
  47. */
  48. class Template
  49. implements Comparable
  50. {
  51. static final double DEFAULT_PRIORITY = 0.5d;
  52. final Stylesheet stylesheet;
  53. final QName name;
  54. final Pattern match;
  55. final TemplateNode node;
  56. final double priority;
  57. final int precedence;
  58. final QName mode;
  59. final boolean isAnyNode; // is the match simply "node()"?
  60. Template(Stylesheet stylesheet,
  61. QName name, Pattern match, TemplateNode node,
  62. int precedence, String priorityAttr, QName mode)
  63. {
  64. this.stylesheet = stylesheet;
  65. this.name = name;
  66. this.match = match;
  67. this.node = node;
  68. this.precedence = precedence;
  69. this.mode = mode;
  70. double p = DEFAULT_PRIORITY;
  71. boolean a = false;
  72. if (priorityAttr != null)
  73. p = Double.parseDouble(priorityAttr);
  74. else
  75. {
  76. // adjust priority if necessary
  77. // see XSLT section 5.5
  78. if (match instanceof Selector)
  79. {
  80. Selector selector = (Selector) match;
  81. Test[] tests = selector.getTests();
  82. if (tests.length > 0)
  83. {
  84. Test test = tests[0];
  85. if (test instanceof NameTest)
  86. {
  87. NameTest nameTest = (NameTest) test;
  88. if (nameTest.matchesAny())
  89. p = -0.25d;
  90. else if (nameTest.matchesAnyLocalName())
  91. p = -0.20d;
  92. else
  93. p = 0.0d;
  94. }
  95. else
  96. {
  97. NodeTypeTest nodeTypeTest = (NodeTypeTest) test;
  98. if (nodeTypeTest.getNodeType() ==
  99. Node.PROCESSING_INSTRUCTION_NODE &&
  100. nodeTypeTest.getData() != null)
  101. p = 0.0d;
  102. else
  103. p = -0.5d;
  104. a = (nodeTypeTest.getNodeType() == 0);
  105. }
  106. // Add a small difference for predicates
  107. if (tests.length > 1)
  108. p += ((double) tests.length - 1) * 0.001;
  109. }
  110. }
  111. }
  112. this.priority = p;
  113. this.isAnyNode = a;
  114. }
  115. private Template(Stylesheet stylesheet,
  116. QName name, Pattern match, TemplateNode node,
  117. int precedence, double priority, QName mode, boolean isAnyNode)
  118. {
  119. this.stylesheet = stylesheet;
  120. this.name = name;
  121. this.match = match;
  122. this.node = node;
  123. this.precedence = precedence;
  124. this.priority = priority;
  125. this.mode = mode;
  126. this.isAnyNode = isAnyNode;
  127. }
  128. Template clone(Stylesheet stylesheet)
  129. {
  130. // FIXME by cloning we lose the imports() functionality, so
  131. // apply-imports will be broken.
  132. return new Template(stylesheet,
  133. name,
  134. (match == null) ? null :
  135. (Pattern) match.clone(stylesheet),
  136. (node == null) ? null : node.clone(stylesheet),
  137. precedence,
  138. priority,
  139. mode,
  140. isAnyNode);
  141. }
  142. public int compareTo(Object other)
  143. {
  144. if (other instanceof Template)
  145. {
  146. Template t = (Template) other;
  147. int d = t.precedence - precedence;
  148. if (d != 0)
  149. return d;
  150. double d2 = t.priority - priority;
  151. if (d2 != 0.0d)
  152. return (int) Math.round(d2 * 1000.0d);
  153. }
  154. return 0;
  155. }
  156. Test getNodeTest(Expr expr)
  157. {
  158. return null;
  159. }
  160. boolean matches(QName mode, Node node)
  161. {
  162. if ((mode == null && this.mode != null) ||
  163. (mode != null && !mode.equals(this.mode)))
  164. return false;
  165. if (match == null)
  166. return false;
  167. if (isAnyNode && node.getNodeType() == Node.DOCUMENT_NODE)
  168. return false; // don't match document node
  169. return match.matches(node);
  170. }
  171. boolean matches(QName name)
  172. {
  173. return name.equals(this.name);
  174. }
  175. boolean imports(Template other)
  176. {
  177. for (Stylesheet ctx = other.stylesheet.parent;
  178. ctx != null;
  179. ctx = ctx.parent)
  180. {
  181. if (ctx == stylesheet)
  182. return true;
  183. }
  184. return false;
  185. }
  186. /**
  187. * @param stylesheet the stylesheet
  188. * @param parent the parent of result nodes
  189. * @param context the context node in the source document
  190. * @param pos the context position
  191. * @param len the context size
  192. * @param nextSibling if non-null, add result nodes before this node
  193. */
  194. void apply(Stylesheet stylesheet, QName mode,
  195. Node context, int pos, int len,
  196. Node parent, Node nextSibling)
  197. throws TransformerException
  198. {
  199. if (stylesheet.debug)
  200. System.err.println("...applying " + toString() + " to " + context);
  201. if (node != null)
  202. node.apply(stylesheet, mode,
  203. context, pos, len,
  204. parent, nextSibling);
  205. }
  206. public String toString()
  207. {
  208. CPStringBuilder buf = new CPStringBuilder(getClass().getName());
  209. buf.append('[');
  210. if (name != null)
  211. {
  212. buf.append("name=");
  213. buf.append(name);
  214. }
  215. else if (match != null)
  216. {
  217. buf.append("match=");
  218. buf.append(match);
  219. }
  220. if (mode != null)
  221. {
  222. buf.append(",mode=");
  223. buf.append(mode);
  224. }
  225. buf.append(",node=");
  226. buf.append(node);
  227. buf.append(']');
  228. return buf.toString();
  229. //return (name != null) ? name.toString() : match.toString();
  230. }
  231. void list(PrintStream out)
  232. {
  233. out.println(toString());
  234. if (node != null)
  235. node.list(1, out, true);
  236. }
  237. }