1 package net.sf.saxon.sort; 2 import net.sf.saxon.Configuration; 3 import net.sf.saxon.ConversionContext; 4 import net.sf.saxon.expr.CardinalityChecker; 5 import net.sf.saxon.expr.RoleLocator; 6 import net.sf.saxon.expr.StaticProperty; 7 import net.sf.saxon.expr.XPathContext; 8 import net.sf.saxon.trans.DynamicError; 9 import net.sf.saxon.trans.XPathException; 10 import net.sf.saxon.value.EmptySequence; 11 import net.sf.saxon.value.StringValue; 12 13 import java.text.Collator ; 14 import java.util.Comparator ; 15 import java.util.Locale ; 16 17 23 24 25 public class FixedSortKeyDefinition extends SortKeyDefinition { 26 27 public FixedSortKeyDefinition() {} 28 29 private transient Comparator comparer = null; 30 34 public SortKeyDefinition simplify() throws XPathException { 35 return this; 36 } 37 38 44 45 public FixedSortKeyDefinition reduce(XPathContext context) throws XPathException { 46 return this; 47 } 48 49 52 53 public void bindComparer(ConversionContext conversion) throws XPathException { 54 55 String orderX = ((StringValue)order).getStringValue(); 56 String caseOrderX = ((StringValue)caseOrder).getStringValue(); 57 String languageX = ((StringValue)language).getStringValue(); 58 59 Comparator comp; 60 if (collation != null) { 61 comp = collation; 62 } else { 63 Collator base; 64 if (languageX.equals("")) { 65 base = Collator.getInstance(); 67 } else { 68 Locale locale = Configuration.getLocale(languageX); 69 base = Collator.getInstance(locale); 70 } 71 comp = getCaseOrderComparer(base, caseOrderX); 72 } 73 74 if (dataTypeExpression==null || dataTypeExpression instanceof EmptySequence) { 75 RoleLocator role = 76 new RoleLocator(RoleLocator.INSTRUCTION, "xsl:sort/sort-key", 0, null); 77 sortKey = CardinalityChecker.makeCardinalityChecker(sortKey, StaticProperty.ALLOWS_ZERO_OR_ONE, role); 78 comp = new AtomicSortComparer(comp, conversion); 79 } else { 80 String dataType = ((StringValue)dataTypeExpression).getStringValue(); 81 if (dataType.equals("text")) { 82 comp = new TextComparer(comp); 83 } else if (dataType.equals("number")) { 84 comp = new NumericComparer(); 85 } else { 86 DynamicError err = new DynamicError("data-type on xsl:sort must be 'text' or 'number'"); 87 err.setErrorCode("XTDE0030"); 88 throw err; 89 } 90 } 91 92 comparer = getOrderedComparer(comp, orderX); 93 } 94 95 private Comparator getOrderedComparer(Comparator base, String order) 96 throws XPathException { 97 if (order.equals("ascending")) { 98 return base; 99 } else if (order.equals("descending")) { 100 return new DescendingComparer(base); 101 } else { 102 DynamicError err = new DynamicError("order must be 'ascending' or 'descending'"); 103 err.setErrorCode("XTDE0030"); 104 throw err; 105 } 106 } 107 108 private Comparator getCaseOrderComparer(Collator base, String caseOrder) 109 throws XPathException { 110 if (caseOrder.equals("#default")) { 111 return base; 112 } else if (caseOrder.equals("lower-first")) { 113 return new LowercaseFirstComparer(base); 114 } else if (caseOrder.equals("upper-first")) { 115 return new UppercaseFirstComparer(base); 116 } else { 117 DynamicError err = new DynamicError("case-order must be 'lower-first' or 'upper-first'"); 118 err.setErrorCode("XTDE0030"); 119 throw err; 120 } 121 } 122 123 126 127 public Comparator getComparer(XPathContext context) throws XPathException { 128 return comparer; 129 } 130 131 } 132 133 134 | Popular Tags |