1 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 ; 38 39 import java.util.Comparator ; 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 expr, String 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 expr, String isAscending, String 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 expr, String isAscending, String lang, String 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 130 public void setCaseOrder(Expr caseOrder) 131 { 132 _caseOrder = caseOrder; 133 } 134 135 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 sortValue(Node child, Env env) 154 throws XPathException 155 { 156 if (_isText) 157 return _expr.evalString(child, env); 158 else 159 return new Double (_expr.evalNumber(child, env)); 160 } 161 162 int cmp(Object a, Object b, Comparator 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 ) a).doubleValue(); 179 double db = ((Double ) 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 va = (String ) a; 196 String vb = (String ) 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 |