KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > xsl > Sort


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.xsl;
30
31 import com.caucho.xpath.Env;
32 import com.caucho.xpath.Expr;
33 import com.caucho.xpath.XPath;
34 import com.caucho.xpath.XPathException;
35 import com.caucho.xpath.XPathParseException;
36
37 import org.w3c.dom.Node JavaDoc;
38
39 import java.util.Comparator JavaDoc;
40
41 public class Sort {
42   public final static int NO_CASE_ORDER = 0;
43   public final static int UPPER_FIRST = 1;
44   public final static int LOWER_FIRST = 2;
45   
46   private Expr _expr;
47   private boolean _isText;
48   private Expr _isAscending;
49   private Expr _caseOrder;
50   
51   private Expr _lang;
52
53   Sort(Expr expr, Expr isAscending, boolean isText)
54   {
55     _expr = expr;
56     _isAscending = isAscending;
57     _isText = isText;
58   }
59
60   Sort(Expr expr, Expr isAscending, Expr lang)
61   {
62     _expr = expr;
63     _isAscending = isAscending;
64     _isText = true;
65     
66     _lang = lang;
67   }
68
69   public Sort(String JavaDoc expr, String JavaDoc isAscending, boolean isText)
70     throws XPathParseException
71   {
72     _expr = XPath.parseExpr(expr);
73     if (isAscending != null)
74       _isAscending = XPath.parseExpr(isAscending);
75     _isText = isText;
76   }
77
78   public Sort(String JavaDoc expr, String JavaDoc isAscending, String JavaDoc lang)
79     throws XPathParseException
80   {
81     _expr = XPath.parseExpr(expr);
82     
83     if (isAscending != null)
84       _isAscending = XPath.parseExpr(isAscending);
85
86     _lang = XPath.parseExpr(lang);
87     _isText = true;
88   }
89
90   public Sort(String JavaDoc expr, String JavaDoc isAscending, String JavaDoc lang, String JavaDoc caseOrder)
91     throws XPathParseException
92   {
93     _expr = XPath.parseExpr(expr);
94     
95     if (isAscending != null)
96       _isAscending = XPath.parseExpr(isAscending);
97
98     if (lang != null)
99       _lang = XPath.parseExpr(lang);
100     
101     _isText = true;
102
103     if (caseOrder != null)
104       _caseOrder = XPath.parseExpr(caseOrder);
105   }
106
107   public static Sort create(Expr expr, Expr isAscending, boolean isText)
108   {
109     return new Sort(expr, isAscending, isText);
110   }
111
112   public static Sort create(Expr expr, Expr isAscending, Expr lang)
113   {
114     return new Sort(expr, isAscending, lang);
115   }
116
117   public Expr getExpr()
118   {
119     return _expr;
120   }
121
122   public Expr getAscending()
123   {
124     return _isAscending;
125   }
126
127   /**
128    * Sets the case order.
129    */

130   public void setCaseOrder(Expr caseOrder)
131   {
132     _caseOrder = caseOrder;
133   }
134
135   /**
136    * Gets the case order.
137    */

138   public Expr getCaseOrder()
139   {
140     return _caseOrder;
141   }
142
143   public boolean isText()
144   {
145     return _isText;
146   }
147
148   public Expr getLang()
149   {
150     return _lang;
151   }
152
153   Object JavaDoc sortValue(Node JavaDoc child, Env env)
154     throws XPathException
155   {
156     if (_isText)
157       return _expr.evalString(child, env);
158     else
159       return new Double JavaDoc(_expr.evalNumber(child, env));
160   }
161
162   int cmp(Object JavaDoc a, Object JavaDoc b, Comparator JavaDoc comparator,
163       boolean isAscending, int caseOrder)
164   {
165     if (comparator != null) {
166       if (a == b)
167     return 0;
168       else if (a == null)
169     return isAscending ? -1 : 1;
170       else if (b == null)
171     return isAscending ? 1 : -1;
172       
173       int cmp = comparator.compare(a, b);
174       
175       return isAscending ? cmp : -cmp;
176     }
177     else if (! _isText) {
178       double da = ((Double JavaDoc) a).doubleValue();
179       double db = ((Double JavaDoc) b).doubleValue();
180
181       if (da == db)
182     return 0;
183       else if (da < db)
184     return isAscending ? -1 : 1;
185       else if (db < da)
186     return isAscending ? 1 : -1;
187       else if (! Double.isNaN(da))
188     return isAscending ? -1 : 1;
189       else if (! Double.isNaN(db))
190     return isAscending ? 1 : -1;
191       else
192     return 0;
193     }
194     else {
195       String JavaDoc va = (String JavaDoc) a;
196       String JavaDoc vb = (String JavaDoc) b;
197
198       if (va == vb)
199     return 0;
200       else if (va == null)
201     return isAscending ? -1 : 1;
202       else if (vb == null)
203     return isAscending ? 1 : -1;
204
205       int cmp = 0;
206
207       if (caseOrder == NO_CASE_ORDER)
208     cmp = va.compareTo(vb);
209       else if (caseOrder == UPPER_FIRST) {
210     int len = va.length();
211     if (vb.length() < len)
212       len = vb.length();
213
214     int i = 0;
215     for (; i < len; i++) {
216       char chA = va.charAt(i);
217       char chB = vb.charAt(i);
218
219       if (chA == chB)
220         continue;
221       else if (Character.isUpperCase(chA) &&
222            Character.isLowerCase(chB)) {
223         cmp = -1;
224         break;
225       }
226       else if (Character.isLowerCase(chA) &&
227            Character.isUpperCase(chB)) {
228         cmp = 1;
229         break;
230       }
231       else {
232         cmp = chA - chB;
233         break;
234       }
235     }
236
237     if (i < len) {
238     }
239     else if (va.length() == vb.length())
240       cmp = 0;
241     else if (va.length() < vb.length())
242       cmp = -1;
243     else
244       cmp = 1;
245       }
246       else if (caseOrder == LOWER_FIRST) {
247     int len = va.length();
248     if (vb.length() < len)
249       len = vb.length();
250
251     int i = 0;
252     for (; i < len; i++) {
253       char chA = va.charAt(i);
254       char chB = vb.charAt(i);
255
256       if (chA == chB)
257         continue;
258       else if (Character.isUpperCase(chA) &&
259            Character.isLowerCase(chB)) {
260         cmp = 1;
261         break;
262       }
263       else if (Character.isLowerCase(chA) &&
264            Character.isUpperCase(chB)) {
265         cmp = -1;
266         break;
267       }
268       else {
269         cmp = chA - chB;
270         break;
271       }
272     }
273
274     if (i < len) {
275     }
276     else if (va.length() == vb.length())
277       cmp = 0;
278     else if (va.length() < vb.length())
279       cmp = -1;
280     else
281       cmp = 1;
282       }
283
284       return isAscending ? cmp : -cmp;
285     }
286   }
287 }
288
Popular Tags