1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. *
  5. * Copyright (c) 1999 The Apache Software Foundation. All rights
  6. * reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. The end-user documentation included with the redistribution,
  21. * if any, must include the following acknowledgment:
  22. * "This product includes software developed by the
  23. * Apache Software Foundation (http://www.apache.org/)."
  24. * Alternately, this acknowledgment may appear in the software itself,
  25. * if and wherever such third-party acknowledgments normally appear.
  26. *
  27. * 4. The names "Xalan" and "Apache Software Foundation" must
  28. * not be used to endorse or promote products derived from this
  29. * software without prior written permission. For written
  30. * permission, please contact apache@apache.org.
  31. *
  32. * 5. Products derived from this software may not be called "Apache",
  33. * nor may "Apache" appear in their name, without prior written
  34. * permission of the Apache Software Foundation.
  35. *
  36. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  37. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  38. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  39. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  40. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  41. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  42. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  43. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  44. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  45. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  46. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  47. * SUCH DAMAGE.
  48. * ====================================================================
  49. *
  50. * This software consists of voluntary contributions made by many
  51. * individuals on behalf of the Apache Software Foundation and was
  52. * originally based on software copyright (c) 1999, Lotus
  53. * Development Corporation., http://www.lotus.com. For more
  54. * information on the Apache Software Foundation, please see
  55. * <http://www.apache.org/>.
  56. */
  57. package org.apache.xalan.templates;
  58. import org.w3c.dom.*;
  59. import org.xml.sax.*;
  60. import org.apache.xpath.*;
  61. import org.apache.xalan.res.XSLTErrorResources;
  62. import org.apache.xalan.transformer.TransformerImpl;
  63. /**
  64. * <meta name="usage" content="advanced"/>
  65. * Implement xsl:sort.
  66. * <pre>
  67. * <!ELEMENT xsl:sort EMPTY>
  68. * <!ATTLIST xsl:sort
  69. * select %expr; "."
  70. * lang %avt; #IMPLIED
  71. * data-type %avt; "text"
  72. * order %avt; "ascending"
  73. * case-order %avt; #IMPLIED
  74. * >
  75. * <!-- xsl:sort cannot occur after any other elements or
  76. * any non-whitespace character -->
  77. * </pre>
  78. * @see <a href="http://www.w3.org/TR/xslt#sorting">sorting in XSLT Specification</a>
  79. */
  80. public class ElemSort extends ElemTemplateElement
  81. {
  82. /**
  83. * xsl:sort has a select attribute whose value is an expression.
  84. * @serial
  85. */
  86. private XPath m_selectExpression = null;
  87. /**
  88. * Set the "select" attribute.
  89. * xsl:sort has a select attribute whose value is an expression.
  90. * For each node to be processed, the expression is evaluated
  91. * with that node as the current node and with the complete
  92. * list of nodes being processed in unsorted order as the current
  93. * node list. The resulting object is converted to a string as if
  94. * by a call to the string function; this string is used as the
  95. * sort key for that node. The default value of the select attribute
  96. * is ., which will cause the string-value of the current node to
  97. * be used as the sort key.
  98. *
  99. * @param v Value to set for the "select" attribute
  100. */
  101. public void setSelect(XPath v)
  102. {
  103. if (v.getPatternString().indexOf("{") < 0)
  104. m_selectExpression = v;
  105. else
  106. error(XSLTErrorResources.ER_NO_CURLYBRACE, null);
  107. }
  108. /**
  109. * Get the "select" attribute.
  110. * xsl:sort has a select attribute whose value is an expression.
  111. * For each node to be processed, the expression is evaluated
  112. * with that node as the current node and with the complete
  113. * list of nodes being processed in unsorted order as the current
  114. * node list. The resulting object is converted to a string as if
  115. * by a call to the string function; this string is used as the
  116. * sort key for that node. The default value of the select attribute
  117. * is ., which will cause the string-value of the current node to
  118. * be used as the sort key.
  119. *
  120. * @return The value of the "select" attribute
  121. */
  122. public XPath getSelect()
  123. {
  124. return m_selectExpression;
  125. }
  126. /**
  127. * lang specifies the language of the sort keys.
  128. * @serial
  129. */
  130. private AVT m_lang_avt = null;
  131. /**
  132. * Set the "lang" attribute.
  133. * lang specifies the language of the sort keys; it has the same
  134. * range of values as xml:lang [XML]; if no lang value is
  135. * specified, the language should be determined from the system environment.
  136. *
  137. * @param v The value to set for the "lang" attribute
  138. */
  139. public void setLang(AVT v)
  140. {
  141. m_lang_avt = v;
  142. }
  143. /**
  144. * Get the "lang" attribute.
  145. * lang specifies the language of the sort keys; it has the same
  146. * range of values as xml:lang [XML]; if no lang value is
  147. * specified, the language should be determined from the system environment.
  148. *
  149. * @return The value of the "lang" attribute
  150. */
  151. public AVT getLang()
  152. {
  153. return m_lang_avt;
  154. }
  155. /**
  156. * data-type specifies the data type of the
  157. * strings to be sorted.
  158. * @serial
  159. */
  160. private AVT m_dataType_avt = null;
  161. /**
  162. * Set the "data-type" attribute.
  163. * <code>data-type</code> specifies the data type of the
  164. * strings; the following values are allowed:
  165. * <ul>
  166. * <li>
  167. * <code>text</code> specifies that the sort keys should be
  168. * sorted lexicographically in the culturally correct manner for the
  169. * language specified by <code>lang</code>.
  170. * </li>
  171. * <li>
  172. * <code>number</code> specifies that the sort keys should be
  173. * converted to numbers and then sorted according to the numeric value;
  174. * the sort key is converted to a number as if by a call to the
  175. * <b><a href="http://www.w3.org/TR/xpath#function-number">number</a></b> function; the <code>lang</code>
  176. * attribute is ignored.
  177. * </li>
  178. * <li>
  179. * A <a href="http://www.w3.org/TR/REC-xml-names#NT-QName">QName</a> with a prefix
  180. * is expanded into an <a href="http://www.w3.org/TR/xpath#dt-expanded-name">expanded-name</a> as described
  181. * in <a href="#qname">[<b>2.4 Qualified Names</b>]</a> the expanded-name identifies the data-type;
  182. * the behavior in this case is not specified by this document.
  183. * </li>
  184. * </ul>
  185. * <p>The default value is <code>text</code>.</p>
  186. * <blockquote>
  187. * <b>NOTE: </b>The XSL Working Group plans that future versions of XSLT will
  188. * leverage XML Schemas to define further values for this
  189. * attribute.</blockquote>
  190. *
  191. * @param v Value to set for the "data-type" attribute
  192. */
  193. public void setDataType(AVT v)
  194. {
  195. m_dataType_avt = v;
  196. }
  197. /**
  198. * Get the "data-type" attribute.
  199. * <code>data-type</code> specifies the data type of the
  200. * strings; the following values are allowed:
  201. * <ul>
  202. * <li>
  203. * <code>text</code> specifies that the sort keys should be
  204. * sorted lexicographically in the culturally correct manner for the
  205. * language specified by <code>lang</code>.
  206. * </li>
  207. * <li>
  208. * <code>number</code> specifies that the sort keys should be
  209. * converted to numbers and then sorted according to the numeric value;
  210. * the sort key is converted to a number as if by a call to the
  211. * <b><a href="http://www.w3.org/TR/xpath#function-number">number</a></b> function; the <code>lang</code>
  212. * attribute is ignored.
  213. * </li>
  214. * <li>
  215. * A <a href="http://www.w3.org/TR/REC-xml-names#NT-QName">QName</a> with a prefix
  216. * is expanded into an <a href="http://www.w3.org/TR/xpath#dt-expanded-name">expanded-name</a> as described
  217. * in <a href="#qname">[<b>2.4 Qualified Names</b>]</a> the expanded-name identifies the data-type;
  218. * the behavior in this case is not specified by this document.
  219. * </li>
  220. * </ul>
  221. * <p>The default value is <code>text</code>.</p>
  222. * <blockquote>
  223. * <b>NOTE: </b>The XSL Working Group plans that future versions of XSLT will
  224. * leverage XML Schemas to define further values for this
  225. * attribute.</blockquote>
  226. *
  227. * @return The value of the "data-type" attribute
  228. */
  229. public AVT getDataType()
  230. {
  231. return m_dataType_avt;
  232. }
  233. /**
  234. * order specifies whether the strings should be sorted in ascending
  235. * or descending order.
  236. * @serial
  237. */
  238. private AVT m_order_avt = null;
  239. /**
  240. * Set the "order" attribute.
  241. * order specifies whether the strings should be sorted in ascending
  242. * or descending order; ascending specifies ascending order; descending
  243. * specifies descending order; the default is ascending.
  244. *
  245. * @param v The value to set for the "order" attribute
  246. */
  247. public void setOrder(AVT v)
  248. {
  249. m_order_avt = v;
  250. }
  251. /**
  252. * Get the "order" attribute.
  253. * order specifies whether the strings should be sorted in ascending
  254. * or descending order; ascending specifies ascending order; descending
  255. * specifies descending order; the default is ascending.
  256. *
  257. * @return The value of the "order" attribute
  258. */
  259. public AVT getOrder()
  260. {
  261. return m_order_avt;
  262. }
  263. /**
  264. * case-order has the value upper-first or lower-first.
  265. * The default value is language dependent.
  266. * @serial
  267. */
  268. private AVT m_caseorder_avt = null;
  269. /**
  270. * Set the "case-order" attribute.
  271. * case-order has the value upper-first or lower-first; this applies
  272. * when data-type="text", and specifies that upper-case letters should
  273. * sort before lower-case letters or vice-versa respectively.
  274. * For example, if lang="en", then A a B b are sorted with
  275. * case-order="upper-first" and a A b B are sorted with case-order="lower-first".
  276. * The default value is language dependent.
  277. *
  278. * @param v The value to set for the "case-order" attribute
  279. *
  280. * @serial
  281. */
  282. public void setCaseOrder(AVT v)
  283. {
  284. m_caseorder_avt = v;
  285. }
  286. /**
  287. * Get the "case-order" attribute.
  288. * case-order has the value upper-first or lower-first; this applies
  289. * when data-type="text", and specifies that upper-case letters should
  290. * sort before lower-case letters or vice-versa respectively.
  291. * For example, if lang="en", then A a B b are sorted with
  292. * case-order="upper-first" and a A b B are sorted with case-order="lower-first".
  293. * The default value is language dependent.
  294. *
  295. * @return The value of the "case-order" attribute
  296. */
  297. public AVT getCaseOrder()
  298. {
  299. return m_caseorder_avt;
  300. }
  301. /**
  302. * Get an int constant identifying the type of element.
  303. * @see org.apache.xalan.templates.Constants
  304. *
  305. * @return The token ID of the element
  306. */
  307. public int getXSLToken()
  308. {
  309. return Constants.ELEMNAME_SORT;
  310. }
  311. /**
  312. * Return the node name.
  313. *
  314. * @return The element's name
  315. */
  316. public String getNodeName()
  317. {
  318. return Constants.ELEMNAME_SORT_STRING;
  319. }
  320. /**
  321. * Add a child to the child list.
  322. *
  323. * @param newChild Child to add to the child list
  324. *
  325. * @return Child just added to the child list
  326. *
  327. * @throws DOMException
  328. */
  329. public Node appendChild(Node newChild) throws DOMException
  330. {
  331. error(XSLTErrorResources.ER_CANNOT_ADD,
  332. new Object[]{ newChild.getNodeName(),
  333. this.getNodeName() }); //"Can not add " +((ElemTemplateElement)newChild).m_elemName +
  334. //" to " + this.m_elemName);
  335. return null;
  336. }
  337. /**
  338. * This function is called after everything else has been
  339. * recomposed, and allows the template to set remaining
  340. * values that may be based on some other property that
  341. * depends on recomposition.
  342. */
  343. public void compose(StylesheetRoot sroot)
  344. throws javax.xml.transform.TransformerException
  345. {
  346. super.compose(sroot);
  347. StylesheetRoot.ComposeState cstate = sroot.getComposeState();
  348. java.util.Vector vnames = cstate.getVariableNames();
  349. if(null != m_caseorder_avt)
  350. m_caseorder_avt.fixupVariables(vnames, cstate.getGlobalsSize());
  351. if(null != m_dataType_avt)
  352. m_dataType_avt.fixupVariables(vnames, cstate.getGlobalsSize());
  353. if(null != m_lang_avt)
  354. m_lang_avt.fixupVariables(vnames, cstate.getGlobalsSize());
  355. if(null != m_order_avt)
  356. m_order_avt.fixupVariables(vnames, cstate.getGlobalsSize());
  357. if(null != m_selectExpression)
  358. m_selectExpression.fixupVariables(vnames, cstate.getGlobalsSize());
  359. }
  360. }