KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > functions > CollatingFunction


1 package net.sf.saxon.functions;
2 import net.sf.saxon.expr.Expression;
3 import net.sf.saxon.expr.StaticContext;
4 import net.sf.saxon.expr.XPathContext;
5 import net.sf.saxon.sort.AtomicComparer;
6 import net.sf.saxon.sort.CodepointCollator;
7 import net.sf.saxon.trans.StaticError;
8 import net.sf.saxon.trans.XPathException;
9 import net.sf.saxon.value.AtomicValue;
10 import net.sf.saxon.value.StringValue;
11 import net.sf.saxon.value.Value;
12
13 import java.util.Comparator JavaDoc;
14
15 /**
16 * Abstract superclass for all functions that take an optional collation argument
17 */

18
19 // Supports string comparison using a collation
20

21 public abstract class CollatingFunction extends SystemFunction {
22
23     // The collation, if known statically
24
Comparator JavaDoc collation = null;
25
26     /**
27     * preEvaluate: if all arguments are known statically, evaluate early
28     */

29
30     public Expression preEvaluate(StaticContext env) throws XPathException {
31         if (getNumberOfArguments() == getDetails().maxArguments) {
32             // A collection was supplied explicitly (as a compile-time name, or we wouldn't be here)
33
collation = env.getCollation(((Value)argument[getNumberOfArguments()-1]).getStringValue());
34             if (collation == null) {
35                 StaticError err = new StaticError("Unknown collation {" +
36                         ((Value)argument[getNumberOfArguments()-1]).getStringValue() + '}');
37                 err.setErrorCode("FOCH0002");
38                 err.setLocator(this);
39                 throw err;
40             }
41             return super.preEvaluate(env);
42         } else {
43             // Use the default collation
44
String JavaDoc uri = env.getDefaultCollationName();
45             collation = env.getCollation(uri);
46             return super.preEvaluate(env);
47         }
48     }
49
50     /**
51     * Get a AtomicComparer that can be used to compare values
52     * @param arg the position of the argument (starting at 0) containing the collation name.
53     * If this argument was not supplied, the default collation is used
54     * @param context The dynamic evaluation context.
55     */

56
57     protected AtomicComparer getAtomicComparer(int arg, XPathContext context) throws XPathException {
58         return new AtomicComparer(getCollator(arg, context, true), context);
59     }
60
61     /**
62     * Get a collator suitable for comparing strings. Returns the collator specified in the
63     * given function argument if present, otherwise returns the default collator.
64      * @param arg The argument position (counting from zero) that holds the collation
65      * URI if present
66      * @param context The dynamic context
67      * @param useDefault true if, in the absence of a collation argument, the default
68      * collation should be used; false if the codepoint collation should be used.
69     * @return a Comparator, which will either be a java.text.Collator, or a CodepointCollator
70     */

71
72     protected Comparator JavaDoc getCollator(int arg, XPathContext context, boolean useDefault) throws XPathException {
73
74         if (collation != null) {
75             // the collation was determined statically
76
return collation;
77         } else {
78             int numargs = argument.length;
79             if (numargs > arg) {
80                 AtomicValue av = (AtomicValue)argument[arg].evaluateItem(context);
81                 StringValue collationValue = (StringValue)av.getPrimitiveValue();
82                 String JavaDoc collationName = collationValue.getStringValue();
83                 return context.getCollation(collationName);
84             } else if (useDefault) {
85                 Comparator JavaDoc collator = context.getDefaultCollation();
86                 return (collator==null ? CodepointCollator.getInstance() : collator);
87             } else {
88                 return CodepointCollator.getInstance();
89             }
90         }
91     }
92 }
93
94
95 //
96
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
97
// you may not use this file except in compliance with the License. You may obtain a copy of the
98
// License at http://www.mozilla.org/MPL/
99
//
100
// Software distributed under the License is distributed on an "AS IS" basis,
101
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
102
// See the License for the specific language governing rights and limitations under the License.
103
//
104
// The Original Code is: all this file.
105
//
106
// The Initial Developer of the Original Code is Michael H. Kay.
107
//
108
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
109
//
110
// Contributor(s): none.
111
//
112
Popular Tags