KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > sort > FixedSortKeyDefinition


1 package net.sf.saxon.sort;
2 import net.sf.saxon.Configuration;
3 import net.sf.saxon.ConversionContext;
4 import net.sf.saxon.expr.CardinalityChecker;
5 import net.sf.saxon.expr.RoleLocator;
6 import net.sf.saxon.expr.StaticProperty;
7 import net.sf.saxon.expr.XPathContext;
8 import net.sf.saxon.trans.DynamicError;
9 import net.sf.saxon.trans.XPathException;
10 import net.sf.saxon.value.EmptySequence;
11 import net.sf.saxon.value.StringValue;
12
13 import java.text.Collator JavaDoc;
14 import java.util.Comparator JavaDoc;
15 import java.util.Locale JavaDoc;
16
17 /**
18 * A FixedSortKeyDefinition is a SortKeyDefinition in which all aspects of the
19 * sort key definition (sort order, data type, etc,) are known.
20 * A SortKeyDefinition defines one component of a sort key. <BR>
21 *
22 */

23
24
25 public class FixedSortKeyDefinition extends SortKeyDefinition {
26
27     public FixedSortKeyDefinition() {}
28
29     private transient Comparator JavaDoc comparer = null;
30     // Note, the "collation" defines the collating sequence for the sort key. The
31
// "comparer" is what is actually used to do comparisons, after taking into account
32
// ascending/descending, caseOrder, etc.
33

34     public SortKeyDefinition simplify() throws XPathException {
35         return this;
36     }
37
38     /**
39     * Eliminate dependencies of the sort key definition on the context. For the sort key select
40     * expression, this means things that don't depend on the individual node: specifically, variables
41     * and current-group(). For the AVTs used to select data type, case order, language, it means
42     * all dependencies: after reduction, these values will be constants.
43     */

44
45     public FixedSortKeyDefinition reduce(XPathContext context) throws XPathException {
46             return this;
47     }
48
49     /**
50     * Allocate a reusable Comparer to implement this sort key comparison
51     */

52
53     public void bindComparer(ConversionContext conversion) throws XPathException {
54
55         String JavaDoc orderX = ((StringValue)order).getStringValue();
56         String JavaDoc caseOrderX = ((StringValue)caseOrder).getStringValue();
57         String JavaDoc languageX = ((StringValue)language).getStringValue();
58
59         Comparator JavaDoc comp;
60         if (collation != null) {
61            comp = collation;
62         } else {
63             Collator JavaDoc base;
64             if (languageX.equals("")) {
65                 // get Java collator for the default locale
66
base = Collator.getInstance();
67             } else {
68                 Locale JavaDoc locale = Configuration.getLocale(languageX);
69                 base = Collator.getInstance(locale);
70             }
71             comp = getCaseOrderComparer(base, caseOrderX);
72         }
73
74         if (dataTypeExpression==null || dataTypeExpression instanceof EmptySequence) {
75             RoleLocator role =
76                 new RoleLocator(RoleLocator.INSTRUCTION, "xsl:sort/sort-key", 0, null);
77             sortKey = CardinalityChecker.makeCardinalityChecker(sortKey, StaticProperty.ALLOWS_ZERO_OR_ONE, role);
78             comp = new AtomicSortComparer(comp, conversion);
79         } else {
80             String JavaDoc dataType = ((StringValue)dataTypeExpression).getStringValue();
81             if (dataType.equals("text")) {
82                 comp = new TextComparer(comp);
83             } else if (dataType.equals("number")) {
84                 comp = new NumericComparer();
85             } else {
86                 DynamicError err = new DynamicError("data-type on xsl:sort must be 'text' or 'number'");
87                 err.setErrorCode("XTDE0030");
88                 throw err;
89             }
90         }
91
92         comparer = getOrderedComparer(comp, orderX);
93     }
94
95     private Comparator JavaDoc getOrderedComparer(Comparator JavaDoc base, String JavaDoc order)
96     throws XPathException {
97         if (order.equals("ascending")) {
98             return base;
99         } else if (order.equals("descending")) {
100             return new DescendingComparer(base);
101         } else {
102             DynamicError err = new DynamicError("order must be 'ascending' or 'descending'");
103             err.setErrorCode("XTDE0030");
104             throw err;
105         }
106     }
107
108     private Comparator JavaDoc getCaseOrderComparer(Collator JavaDoc base, String JavaDoc caseOrder)
109     throws XPathException {
110         if (caseOrder.equals("#default")) {
111             return base;
112         } else if (caseOrder.equals("lower-first")) {
113             return new LowercaseFirstComparer(base);
114         } else if (caseOrder.equals("upper-first")) {
115             return new UppercaseFirstComparer(base);
116         } else {
117             DynamicError err = new DynamicError("case-order must be 'lower-first' or 'upper-first'");
118             err.setErrorCode("XTDE0030");
119             throw err;
120         }
121     }
122
123     /**
124     * Get the comparer which is used to compare two values according to this sort key.
125     */

126
127     public Comparator JavaDoc getComparer(XPathContext context) throws XPathException {
128         return comparer;
129     }
130
131 }
132
133
134 //
135
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
136
// you may not use this file except in compliance with the License. You may obtain a copy of the
137
// License at http://www.mozilla.org/MPL/
138
//
139
// Software distributed under the License is distributed on an "AS IS" basis,
140
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
141
// See the License for the specific language governing rights and limitations under the License.
142
//
143
// The Original Code is: all this file.
144
//
145
// The Initial Developer of the Original Code is Michael H. Kay.
146
//
147
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
148
//
149
// Contributor(s): none.
150
//
151
Popular Tags