SortKey.java 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /* SortKey.java --
  2. Copyright (C) 2004 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 javax.xml.namespace.QName;
  33. import javax.xml.transform.TransformerException;
  34. import org.w3c.dom.Document;
  35. import org.w3c.dom.DocumentFragment;
  36. import org.w3c.dom.Node;
  37. import gnu.xml.xpath.Expr;
  38. /**
  39. * <p>
  40. * An XSL sort key, as specified by section 10 of the XSL
  41. * Transformations specification. This takes the form:
  42. * </p>
  43. * <pre>
  44. * &lt;xsl:sort
  45. * select = string-expression
  46. * lang = { nmtoken }
  47. * data-type = { "text" | "number" | qname-but-not-ncname }
  48. * order = { "ascending" | "descending" }
  49. * case-order = { "upper-first" | "lower-first" } /&rt;
  50. * </pre>
  51. * <p>
  52. * Note that all but the selection expression are optional,
  53. * and so may be {@code null}.
  54. * </p>
  55. *
  56. * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
  57. */
  58. final class SortKey
  59. {
  60. static final int DEFAULT = 0;
  61. static final int UPPER_FIRST = 1;
  62. static final int LOWER_FIRST = 2;
  63. final Expr select;
  64. final TemplateNode langTemplate;
  65. final TemplateNode dataTypeTemplate;
  66. final TemplateNode orderTemplate;
  67. final TemplateNode caseOrderTemplate;
  68. transient String lang;
  69. transient String dataType;
  70. transient boolean descending;
  71. transient int caseOrder;
  72. /**
  73. * Constructs a new {@link SortKey} to represent an &lt;xsl:sort&rt;
  74. * tag.
  75. *
  76. * @param select the XPath expression which selects the nodes to be sorted.
  77. * @param lang the language of the sort keys or {@code null} if unspecified.
  78. * @param dataType the data type of the strings. May be "string", "number",
  79. * a QName or {@code null} if unspecified.
  80. * @param order the ordering of the nodes, which may be "ascending", "descending"
  81. * or {@code null} if unspecified.
  82. * @param caseOrder the treatment of case when the data type is a string. This
  83. * may be "upper-first", "lower-first" or {@code null} if
  84. * unspecified.
  85. */
  86. SortKey(Expr select, TemplateNode lang, TemplateNode dataType,
  87. TemplateNode order, TemplateNode caseOrder)
  88. {
  89. this.select = select;
  90. this.langTemplate = lang;
  91. this.dataTypeTemplate = dataType;
  92. this.orderTemplate = order;
  93. this.caseOrderTemplate = caseOrder;
  94. }
  95. String key(Node node)
  96. {
  97. Object ret = select.evaluate(node, 1, 1);
  98. if (ret instanceof String)
  99. {
  100. return (String) ret;
  101. }
  102. else
  103. {
  104. return Expr._string(node, ret);
  105. }
  106. }
  107. /**
  108. * Prepare for a sort.
  109. * This sets all transient variables from their AVTs.
  110. */
  111. void init(Stylesheet stylesheet, QName mode,
  112. Node context, int pos, int len,
  113. Node parent, Node nextSibling)
  114. throws TransformerException
  115. {
  116. Document doc = (context instanceof Document) ? (Document) context :
  117. context.getOwnerDocument();
  118. if (langTemplate == null)
  119. {
  120. lang = null;
  121. }
  122. else
  123. {
  124. DocumentFragment fragment = doc.createDocumentFragment();
  125. langTemplate.apply(stylesheet, mode, context, pos, len,
  126. fragment, null);
  127. lang = Expr.stringValue(fragment);
  128. }
  129. if (dataTypeTemplate == null)
  130. {
  131. dataType = "text";
  132. }
  133. else
  134. {
  135. DocumentFragment fragment = doc.createDocumentFragment();
  136. dataTypeTemplate.apply(stylesheet, mode, context, pos, len,
  137. fragment, null);
  138. dataType = Expr.stringValue(fragment);
  139. }
  140. if (orderTemplate == null)
  141. {
  142. descending = false;
  143. }
  144. else
  145. {
  146. DocumentFragment fragment = doc.createDocumentFragment();
  147. orderTemplate.apply(stylesheet, mode, context, pos, len,
  148. fragment, null);
  149. String order = Expr.stringValue(fragment);
  150. descending = "descending".equals(order);
  151. }
  152. if (caseOrderTemplate == null)
  153. {
  154. caseOrder = DEFAULT;
  155. }
  156. else
  157. {
  158. DocumentFragment fragment = doc.createDocumentFragment();
  159. caseOrderTemplate.apply(stylesheet, mode, context, pos, len,
  160. fragment, null);
  161. String co = Expr.stringValue(fragment);
  162. caseOrder = "upper-first".equals(co) ? UPPER_FIRST :
  163. "lower-first".equals(co) ? LOWER_FIRST :
  164. DEFAULT;
  165. }
  166. }
  167. boolean references(QName var)
  168. {
  169. if (select != null && select.references(var))
  170. {
  171. return true;
  172. }
  173. if (langTemplate != null && langTemplate.references(var))
  174. {
  175. return true;
  176. }
  177. if (dataTypeTemplate != null && dataTypeTemplate.references(var))
  178. {
  179. return true;
  180. }
  181. if (orderTemplate != null && orderTemplate.references(var))
  182. {
  183. return true;
  184. }
  185. if (caseOrderTemplate != null && caseOrderTemplate.references(var))
  186. {
  187. return true;
  188. }
  189. return false;
  190. }
  191. /**
  192. * Provides a clone of this {@link SortKey}, using the given
  193. * stylesheet as a context.
  194. *
  195. * @param stylesheet the stylesheet which provides context for the cloning.
  196. * @return a clone of this instance.
  197. */
  198. SortKey clone(Stylesheet stylesheet)
  199. {
  200. return new SortKey(select.clone(stylesheet),
  201. langTemplate == null ? null : cloneAttributeValueTemplate(langTemplate, stylesheet),
  202. dataTypeTemplate == null ? null : cloneAttributeValueTemplate(dataTypeTemplate, stylesheet),
  203. orderTemplate == null ? null : cloneAttributeValueTemplate(orderTemplate, stylesheet),
  204. caseOrderTemplate == null ? null : cloneAttributeValueTemplate(caseOrderTemplate, stylesheet));
  205. }
  206. /**
  207. * Clones an attribute value template as created by
  208. * {@link Stylesheet#parseAttributeValueTemplate(String, Node)}.
  209. * The node may either by a literal node or an xsl:value-of expression.
  210. *
  211. * @param node the node to clone.
  212. * @param stylesheet the stylesheet which provides context for the cloning.
  213. * @return the cloned node.
  214. */
  215. private TemplateNode cloneAttributeValueTemplate(TemplateNode node, Stylesheet stylesheet)
  216. {
  217. if (node instanceof ValueOfNode)
  218. return ((ValueOfNode) node).clone(stylesheet);
  219. return ((LiteralNode) node).clone(stylesheet);
  220. }
  221. }