001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.xsl;
030:
031: import com.caucho.xpath.Env;
032: import com.caucho.xpath.Expr;
033: import com.caucho.xpath.XPath;
034: import com.caucho.xpath.XPathException;
035: import com.caucho.xpath.XPathParseException;
036:
037: import org.w3c.dom.Node;
038:
039: import java.util.Comparator;
040:
041: public class Sort {
042: public final static int NO_CASE_ORDER = 0;
043: public final static int UPPER_FIRST = 1;
044: public final static int LOWER_FIRST = 2;
045:
046: private Expr _expr;
047: private boolean _isText;
048: private Expr _isAscending;
049: private Expr _caseOrder;
050:
051: private Expr _lang;
052:
053: Sort(Expr expr, Expr isAscending, boolean isText) {
054: _expr = expr;
055: _isAscending = isAscending;
056: _isText = isText;
057: }
058:
059: Sort(Expr expr, Expr isAscending, Expr lang) {
060: _expr = expr;
061: _isAscending = isAscending;
062: _isText = true;
063:
064: _lang = lang;
065: }
066:
067: public Sort(String expr, String isAscending, boolean isText)
068: throws XPathParseException {
069: _expr = XPath.parseExpr(expr);
070: if (isAscending != null)
071: _isAscending = XPath.parseExpr(isAscending);
072: _isText = isText;
073: }
074:
075: public Sort(String expr, String isAscending, String lang)
076: throws XPathParseException {
077: _expr = XPath.parseExpr(expr);
078:
079: if (isAscending != null)
080: _isAscending = XPath.parseExpr(isAscending);
081:
082: _lang = XPath.parseExpr(lang);
083: _isText = true;
084: }
085:
086: public Sort(String expr, String isAscending, String lang,
087: String caseOrder) throws XPathParseException {
088: _expr = XPath.parseExpr(expr);
089:
090: if (isAscending != null)
091: _isAscending = XPath.parseExpr(isAscending);
092:
093: if (lang != null)
094: _lang = XPath.parseExpr(lang);
095:
096: _isText = true;
097:
098: if (caseOrder != null)
099: _caseOrder = XPath.parseExpr(caseOrder);
100: }
101:
102: public static Sort create(Expr expr, Expr isAscending,
103: boolean isText) {
104: return new Sort(expr, isAscending, isText);
105: }
106:
107: public static Sort create(Expr expr, Expr isAscending, Expr lang) {
108: return new Sort(expr, isAscending, lang);
109: }
110:
111: public Expr getExpr() {
112: return _expr;
113: }
114:
115: public Expr getAscending() {
116: return _isAscending;
117: }
118:
119: /**
120: * Sets the case order.
121: */
122: public void setCaseOrder(Expr caseOrder) {
123: _caseOrder = caseOrder;
124: }
125:
126: /**
127: * Gets the case order.
128: */
129: public Expr getCaseOrder() {
130: return _caseOrder;
131: }
132:
133: public boolean isText() {
134: return _isText;
135: }
136:
137: public Expr getLang() {
138: return _lang;
139: }
140:
141: Object sortValue(Node child, Env env) throws XPathException {
142: if (_isText)
143: return _expr.evalString(child, env);
144: else
145: return new Double(_expr.evalNumber(child, env));
146: }
147:
148: int cmp(Object a, Object b, Comparator comparator,
149: boolean isAscending, int caseOrder) {
150: if (comparator != null) {
151: if (a == b)
152: return 0;
153: else if (a == null)
154: return isAscending ? -1 : 1;
155: else if (b == null)
156: return isAscending ? 1 : -1;
157:
158: int cmp = comparator.compare(a, b);
159:
160: return isAscending ? cmp : -cmp;
161: } else if (!_isText) {
162: double da = ((Double) a).doubleValue();
163: double db = ((Double) b).doubleValue();
164:
165: if (da == db)
166: return 0;
167: else if (da < db)
168: return isAscending ? -1 : 1;
169: else if (db < da)
170: return isAscending ? 1 : -1;
171: else if (!Double.isNaN(da))
172: return isAscending ? -1 : 1;
173: else if (!Double.isNaN(db))
174: return isAscending ? 1 : -1;
175: else
176: return 0;
177: } else {
178: String va = (String) a;
179: String vb = (String) b;
180:
181: if (va == vb)
182: return 0;
183: else if (va == null)
184: return isAscending ? -1 : 1;
185: else if (vb == null)
186: return isAscending ? 1 : -1;
187:
188: int cmp = 0;
189:
190: if (caseOrder == NO_CASE_ORDER)
191: cmp = va.compareTo(vb);
192: else if (caseOrder == UPPER_FIRST) {
193: int len = va.length();
194: if (vb.length() < len)
195: len = vb.length();
196:
197: int i = 0;
198: for (; i < len; i++) {
199: char chA = va.charAt(i);
200: char chB = vb.charAt(i);
201:
202: if (chA == chB)
203: continue;
204: else if (Character.isUpperCase(chA)
205: && Character.isLowerCase(chB)) {
206: cmp = -1;
207: break;
208: } else if (Character.isLowerCase(chA)
209: && Character.isUpperCase(chB)) {
210: cmp = 1;
211: break;
212: } else {
213: cmp = chA - chB;
214: break;
215: }
216: }
217:
218: if (i < len) {
219: } else if (va.length() == vb.length())
220: cmp = 0;
221: else if (va.length() < vb.length())
222: cmp = -1;
223: else
224: cmp = 1;
225: } else if (caseOrder == LOWER_FIRST) {
226: int len = va.length();
227: if (vb.length() < len)
228: len = vb.length();
229:
230: int i = 0;
231: for (; i < len; i++) {
232: char chA = va.charAt(i);
233: char chB = vb.charAt(i);
234:
235: if (chA == chB)
236: continue;
237: else if (Character.isUpperCase(chA)
238: && Character.isLowerCase(chB)) {
239: cmp = 1;
240: break;
241: } else if (Character.isLowerCase(chA)
242: && Character.isUpperCase(chB)) {
243: cmp = -1;
244: break;
245: } else {
246: cmp = chA - chB;
247: break;
248: }
249: }
250:
251: if (i < len) {
252: } else if (va.length() == vb.length())
253: cmp = 0;
254: else if (va.length() < vb.length())
255: cmp = -1;
256: else
257: cmp = 1;
258: }
259:
260: return isAscending ? cmp : -cmp;
261: }
262: }
263: }
|