KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xalan > templates > ElemSort


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 /*
17  * $Id: ElemSort.java,v 1.11 2004/02/16 20:32:32 minchau Exp $
18  */

19 package org.apache.xalan.templates;
20
21 import org.apache.xalan.res.XSLTErrorResources;
22 import org.apache.xpath.XPath;
23
24 import org.w3c.dom.DOMException JavaDoc;
25 import org.w3c.dom.Node JavaDoc;
26
27 /**
28  * Implement xsl:sort.
29  * <pre>
30  * <!ELEMENT xsl:sort EMPTY>
31  * <!ATTLIST xsl:sort
32  * select %expr; "."
33  * lang %avt; #IMPLIED
34  * data-type %avt; "text"
35  * order %avt; "ascending"
36  * case-order %avt; #IMPLIED
37  * >
38  * <!-- xsl:sort cannot occur after any other elements or
39  * any non-whitespace character -->
40  * </pre>
41  * @see <a HREF="http://www.w3.org/TR/xslt#sorting">sorting in XSLT Specification</a>
42  * @xsl.usage advanced
43  */

44 public class ElemSort extends ElemTemplateElement
45 {
46
47   /**
48    * xsl:sort has a select attribute whose value is an expression.
49    * @serial
50    */

51   private XPath m_selectExpression = null;
52
53   /**
54    * Set the "select" attribute.
55    * xsl:sort has a select attribute whose value is an expression.
56    * For each node to be processed, the expression is evaluated
57    * with that node as the current node and with the complete
58    * list of nodes being processed in unsorted order as the current
59    * node list. The resulting object is converted to a string as if
60    * by a call to the string function; this string is used as the
61    * sort key for that node. The default value of the select attribute
62    * is ., which will cause the string-value of the current node to
63    * be used as the sort key.
64    *
65    * @param v Value to set for the "select" attribute
66    */

67   public void setSelect(XPath v)
68   {
69
70     if (v.getPatternString().indexOf("{") < 0)
71       m_selectExpression = v;
72     else
73       error(XSLTErrorResources.ER_NO_CURLYBRACE, null);
74   }
75
76   /**
77    * Get the "select" attribute.
78    * xsl:sort has a select attribute whose value is an expression.
79    * For each node to be processed, the expression is evaluated
80    * with that node as the current node and with the complete
81    * list of nodes being processed in unsorted order as the current
82    * node list. The resulting object is converted to a string as if
83    * by a call to the string function; this string is used as the
84    * sort key for that node. The default value of the select attribute
85    * is ., which will cause the string-value of the current node to
86    * be used as the sort key.
87    *
88    * @return The value of the "select" attribute
89    */

90   public XPath getSelect()
91   {
92     return m_selectExpression;
93   }
94
95   /**
96    * lang specifies the language of the sort keys.
97    * @serial
98    */

99   private AVT m_lang_avt = null;
100
101   /**
102    * Set the "lang" attribute.
103    * lang specifies the language of the sort keys; it has the same
104    * range of values as xml:lang [XML]; if no lang value is
105    * specified, the language should be determined from the system environment.
106    *
107    * @param v The value to set for the "lang" attribute
108    */

109   public void setLang(AVT v)
110   {
111     m_lang_avt = v;
112   }
113
114   /**
115    * Get the "lang" attribute.
116    * lang specifies the language of the sort keys; it has the same
117    * range of values as xml:lang [XML]; if no lang value is
118    * specified, the language should be determined from the system environment.
119    *
120    * @return The value of the "lang" attribute
121    */

122   public AVT getLang()
123   {
124     return m_lang_avt;
125   }
126
127   /**
128    * data-type specifies the data type of the
129    * strings to be sorted.
130    * @serial
131    */

132   private AVT m_dataType_avt = null;
133
134   /**
135    * Set the "data-type" attribute.
136    * <code>data-type</code> specifies the data type of the
137    * strings; the following values are allowed:
138    * <ul>
139    * <li>
140    * <code>text</code> specifies that the sort keys should be
141    * sorted lexicographically in the culturally correct manner for the
142    * language specified by <code>lang</code>.
143    * </li>
144    * <li>
145    * <code>number</code> specifies that the sort keys should be
146    * converted to numbers and then sorted according to the numeric value;
147    * the sort key is converted to a number as if by a call to the
148    * <b><a HREF="http://www.w3.org/TR/xpath#function-number">number</a></b> function; the <code>lang</code>
149    * attribute is ignored.
150    * </li>
151    * <li>
152    * A <a HREF="http://www.w3.org/TR/REC-xml-names#NT-QName">QName</a> with a prefix
153    * is expanded into an <a HREF="http://www.w3.org/TR/xpath#dt-expanded-name">expanded-name</a> as described
154    * in <a HREF="#qname">[<b>2.4 Qualified Names</b>]</a>; the expanded-name identifies the data-type;
155    * the behavior in this case is not specified by this document.
156    * </li>
157    * </ul>
158    * <p>The default value is <code>text</code>.</p>
159    * <blockquote>
160    * <b>NOTE: </b>The XSL Working Group plans that future versions of XSLT will
161    * leverage XML Schemas to define further values for this
162    * attribute.</blockquote>
163    *
164    * @param v Value to set for the "data-type" attribute
165    */

166   public void setDataType(AVT v)
167   {
168     m_dataType_avt = v;
169   }
170
171   /**
172    * Get the "data-type" attribute.
173    * <code>data-type</code> specifies the data type of the
174    * strings; the following values are allowed:
175    * <ul>
176    * <li>
177    * <code>text</code> specifies that the sort keys should be
178    * sorted lexicographically in the culturally correct manner for the
179    * language specified by <code>lang</code>.
180    * </li>
181    * <li>
182    * <code>number</code> specifies that the sort keys should be
183    * converted to numbers and then sorted according to the numeric value;
184    * the sort key is converted to a number as if by a call to the
185    * <b><a HREF="http://www.w3.org/TR/xpath#function-number">number</a></b> function; the <code>lang</code>
186    * attribute is ignored.
187    * </li>
188    * <li>
189    * A <a HREF="http://www.w3.org/TR/REC-xml-names#NT-QName">QName</a> with a prefix
190    * is expanded into an <a HREF="http://www.w3.org/TR/xpath#dt-expanded-name">expanded-name</a> as described
191    * in <a HREF="#qname">[<b>2.4 Qualified Names</b>]</a>; the expanded-name identifies the data-type;
192    * the behavior in this case is not specified by this document.
193    * </li>
194    * </ul>
195    * <p>The default value is <code>text</code>.</p>
196    * <blockquote>
197    * <b>NOTE: </b>The XSL Working Group plans that future versions of XSLT will
198    * leverage XML Schemas to define further values for this
199    * attribute.</blockquote>
200    *
201    * @return The value of the "data-type" attribute
202    */

203   public AVT getDataType()
204   {
205     return m_dataType_avt;
206   }
207
208   /**
209    * order specifies whether the strings should be sorted in ascending
210    * or descending order.
211    * @serial
212    */

213   private AVT m_order_avt = null;
214
215   /**
216    * Set the "order" attribute.
217    * order specifies whether the strings should be sorted in ascending
218    * or descending order; ascending specifies ascending order; descending
219    * specifies descending order; the default is ascending.
220    *
221    * @param v The value to set for the "order" attribute
222    */

223   public void setOrder(AVT v)
224   {
225     m_order_avt = v;
226   }
227
228   /**
229    * Get the "order" attribute.
230    * order specifies whether the strings should be sorted in ascending
231    * or descending order; ascending specifies ascending order; descending
232    * specifies descending order; the default is ascending.
233    *
234    * @return The value of the "order" attribute
235    */

236   public AVT getOrder()
237   {
238     return m_order_avt;
239   }
240
241   /**
242    * case-order has the value upper-first or lower-first.
243    * The default value is language dependent.
244    * @serial
245    */

246   private AVT m_caseorder_avt = null;
247
248   /**
249    * Set the "case-order" attribute.
250    * case-order has the value upper-first or lower-first; this applies
251    * when data-type="text", and specifies that upper-case letters should
252    * sort before lower-case letters or vice-versa respectively.
253    * For example, if lang="en", then A a B b are sorted with
254    * case-order="upper-first" and a A b B are sorted with case-order="lower-first".
255    * The default value is language dependent.
256    *
257    * @param v The value to set for the "case-order" attribute
258    *
259    * @serial
260    */

261   public void setCaseOrder(AVT v)
262   {
263     m_caseorder_avt = v;
264   }
265
266   /**
267    * Get the "case-order" attribute.
268    * case-order has the value upper-first or lower-first; this applies
269    * when data-type="text", and specifies that upper-case letters should
270    * sort before lower-case letters or vice-versa respectively.
271    * For example, if lang="en", then A a B b are sorted with
272    * case-order="upper-first" and a A b B are sorted with case-order="lower-first".
273    * The default value is language dependent.
274    *
275    * @return The value of the "case-order" attribute
276    */

277   public AVT getCaseOrder()
278   {
279     return m_caseorder_avt;
280   }
281
282   /**
283    * Get an int constant identifying the type of element.
284    * @see org.apache.xalan.templates.Constants
285    *
286    * @return The token ID of the element
287    */

288   public int getXSLToken()
289   {
290     return Constants.ELEMNAME_SORT;
291   }
292
293   /**
294    * Return the node name.
295    *
296    * @return The element's name
297    */

298   public String JavaDoc getNodeName()
299   {
300     return Constants.ELEMNAME_SORT_STRING;
301   }
302
303   /**
304    * Add a child to the child list.
305    *
306    * @param newChild Child to add to the child list
307    *
308    * @return Child just added to the child list
309    *
310    * @throws DOMException
311    */

312   public Node JavaDoc appendChild(Node JavaDoc newChild) throws DOMException JavaDoc
313   {
314
315     error(XSLTErrorResources.ER_CANNOT_ADD,
316           new Object JavaDoc[]{ newChild.getNodeName(),
317                         this.getNodeName() }); //"Can not add " +((ElemTemplateElement)newChild).m_elemName +
318

319     //" to " + this.m_elemName);
320
return null;
321   }
322   
323   /**
324    * This function is called after everything else has been
325    * recomposed, and allows the template to set remaining
326    * values that may be based on some other property that
327    * depends on recomposition.
328    */

329   public void compose(StylesheetRoot sroot)
330     throws javax.xml.transform.TransformerException JavaDoc
331   {
332     super.compose(sroot);
333     StylesheetRoot.ComposeState cstate = sroot.getComposeState();
334     java.util.Vector JavaDoc vnames = cstate.getVariableNames();
335     if(null != m_caseorder_avt)
336       m_caseorder_avt.fixupVariables(vnames, cstate.getGlobalsSize());
337     if(null != m_dataType_avt)
338       m_dataType_avt.fixupVariables(vnames, cstate.getGlobalsSize());
339     if(null != m_lang_avt)
340       m_lang_avt.fixupVariables(vnames, cstate.getGlobalsSize());
341     if(null != m_order_avt)
342       m_order_avt.fixupVariables(vnames, cstate.getGlobalsSize());
343     if(null != m_selectExpression)
344       m_selectExpression.fixupVariables(vnames, cstate.getGlobalsSize());
345   }
346 }
347
Popular Tags