KickJava   Java API By Example, From Geeks To Geeks.

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


1 package net.sf.saxon.functions;
2 import net.sf.saxon.expr.XPathContext;
3 import net.sf.saxon.om.Item;
4 import net.sf.saxon.sort.AtomicComparer;
5 import net.sf.saxon.trans.XPathException;
6 import net.sf.saxon.value.AtomicValue;
7 import net.sf.saxon.value.IntegerValue;
8
9 //import java.util.Comparator;
10

11 /**
12 * XSLT 2.0 compare() function
13 */

14
15 // Supports string comparison using a collation
16

17 public class Compare extends CollatingFunction {
18
19     /**
20     * Evaluate the expression
21     */

22
23     public Item evaluateItem(XPathContext context) throws XPathException {
24
25         AtomicValue arg0 = (AtomicValue)argument[0].evaluateItem(context);
26         if (arg0==null) {
27             return null;
28         }
29         arg0 = arg0.getPrimitiveValue();
30
31         AtomicValue arg1 = (AtomicValue)argument[1].evaluateItem(context);
32         if (arg1==null) {
33             return null;
34         }
35         arg1 = arg1.getPrimitiveValue();
36
37         AtomicComparer collator = getAtomicComparer(2, context);
38
39         int result = collator.compare(arg0, arg1);
40         if (result < 0) {
41             return IntegerValue.MINUS_ONE;
42         } else if (result > 0) {
43             return IntegerValue.PLUS_ONE;
44         } else {
45             return IntegerValue.ZERO;
46         }
47     }
48
49 }
50
51
52 //
53
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
54
// you may not use this file except in compliance with the License. You may obtain a copy of the
55
// License at http://www.mozilla.org/MPL/
56
//
57
// Software distributed under the License is distributed on an "AS IS" basis,
58
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
59
// See the License for the specific language governing rights and limitations under the License.
60
//
61
// The Original Code is: all this file.
62
//
63
// The Initial Developer of the Original Code is Michael H. Kay.
64
//
65
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
66
//
67
// Contributor(s): none.
68
//
69
Popular Tags