1 16 19 20 package org.apache.xalan.xsltc.dom; 21 22 import java.text.CollationKey ; 23 import java.text.Collator ; 24 import java.util.Locale ; 25 26 import org.apache.xalan.xsltc.CollatorFactory; 27 import org.apache.xalan.xsltc.DOM; 28 import org.apache.xalan.xsltc.TransletException; 29 import org.apache.xalan.xsltc.runtime.AbstractTranslet; 30 import org.apache.xml.utils.StringComparable; 31 32 35 public abstract class NodeSortRecord { 36 public static final int COMPARE_STRING = 0; 37 public static final int COMPARE_NUMERIC = 1; 38 39 public static final int COMPARE_ASCENDING = 0; 40 public static final int COMPARE_DESCENDING = 1; 41 42 48 private static final Collator DEFAULT_COLLATOR = Collator.getInstance(); 49 50 55 protected Collator _collator = DEFAULT_COLLATOR; 56 protected Collator [] _collators; 57 58 63 protected Locale _locale; 64 65 protected CollatorFactory _collatorFactory; 66 67 protected SortSettings _settings; 68 69 private DOM _dom = null; 70 private int _node; private int _last = 0; private int _scanned = 0; 74 private Object [] _values; 76 82 public NodeSortRecord(int node) { 83 _node = node; 84 } 85 86 public NodeSortRecord() { 87 this(0); 88 } 89 90 94 public final void initialize(int node, int last, DOM dom, 95 SortSettings settings) 96 throws TransletException 97 { 98 _dom = dom; 99 _node = node; 100 _last = last; 101 _settings = settings; 102 103 int levels = settings.getSortOrders().length; 104 _values = new Object [levels]; 105 106 String colFactClassname = 108 System.getProperty("org.apache.xalan.xsltc.COLLATOR_FACTORY"); 109 110 if (colFactClassname != null) { 111 try { 112 Object candObj = ObjectFactory.findProviderClass( 113 colFactClassname, ObjectFactory.findClassLoader(), true); 114 _collatorFactory = (CollatorFactory)candObj; 115 } catch (ClassNotFoundException e) { 116 throw new TransletException(e); 117 } 118 Locale [] locales = settings.getLocales(); 119 _collators = new Collator [levels]; 120 for (int i = 0; i < levels; i++){ 121 _collators[i] = _collatorFactory.getCollator(locales[i]); 122 } 123 _collator = _collators[0]; 124 } else { 125 _collators = settings.getCollators(); 126 _collator = _collators[0]; 127 } 128 } 129 130 133 public final int getNode() { 134 return _node; 135 } 136 137 140 public final int compareDocOrder(NodeSortRecord other) { 141 return _node - other._node; 142 } 143 144 149 private final Comparable stringValue(int level) { 150 if (_scanned <= level) { 152 AbstractTranslet translet = _settings.getTranslet(); 153 Locale [] locales = _settings.getLocales(); 154 String [] caseOrder = _settings.getCaseOrders(); 155 156 final String str = extractValueFromDOM(_dom, _node, level, 158 translet, _last); 159 final Comparable key = 160 StringComparable.getComparator(str, locales[level], 161 _collators[level], 162 caseOrder[level]); 163 _values[_scanned++] = key; 164 return(key); 165 } 166 return((Comparable )_values[level]); 167 } 168 169 private final Double numericValue(int level) { 170 if (_scanned <= level) { 172 AbstractTranslet translet = _settings.getTranslet(); 173 174 final String str = extractValueFromDOM(_dom, _node, level, 176 translet, _last); 177 Double num; 178 try { 179 num = new Double (str); 180 } 181 catch (NumberFormatException e) { 183 num = new Double (Double.NEGATIVE_INFINITY); 184 } 185 _values[_scanned++] = num; 186 return(num); 187 } 188 return((Double )_values[level]); 189 } 190 191 198 public int compareTo(NodeSortRecord other) { 199 int cmp, level; 200 int[] sortOrder = _settings.getSortOrders(); 201 int levels = _settings.getSortOrders().length; 202 int[] compareTypes = _settings.getTypes(); 203 204 for (level = 0; level < levels; level++) { 205 if (compareTypes[level] == COMPARE_NUMERIC) { 207 final Double our = numericValue(level); 208 final Double their = other.numericValue(level); 209 cmp = our.compareTo(their); 210 } 211 else { 212 final Comparable our = stringValue(level); 213 final Comparable their = other.stringValue(level); 214 cmp = our.compareTo(their); 215 } 216 217 if (cmp != 0) { 219 return sortOrder[level] == COMPARE_DESCENDING ? 0 - cmp : cmp; 220 } 221 } 222 return(_node - other._node); 224 } 225 226 230 public Collator [] getCollator() { 231 return _collators; 232 } 233 234 237 public abstract String extractValueFromDOM(DOM dom, int current, int level, 238 AbstractTranslet translet, 239 int last); 240 241 } 242 | Popular Tags |