1 package net.sf.saxon.sort; 2 import net.sf.saxon.expr.Expression; 3 import net.sf.saxon.expr.XPathContext; 4 import net.sf.saxon.expr.Container; 5 import net.sf.saxon.instruct.Executable; 6 import net.sf.saxon.trans.DynamicError; 7 import net.sf.saxon.trans.XPathException; 8 import net.sf.saxon.value.EmptySequence; 9 import net.sf.saxon.value.StringValue; 10 11 import java.io.Serializable ; 12 import java.util.Comparator ; 13 14 22 23 27 32 33 public class SortKeyDefinition implements Serializable { 34 35 private static StringValue defaultOrder = new StringValue("ascending"); 36 private static StringValue defaultCaseOrder = new StringValue("#default"); 37 private static StringValue defaultLanguage = StringValue.EMPTY_STRING; 38 39 protected Expression sortKey; 40 protected Expression order = defaultOrder; 41 protected Expression dataTypeExpression = EmptySequence.getInstance(); 42 protected Expression caseOrder = defaultCaseOrder; 44 protected Expression language = defaultLanguage; 45 protected Expression collationName = null; 46 protected Comparator collation; protected boolean emptyFirst = true; 48 protected Container parentExpression; 50 51 55 59 public void setParentExpression(Container container) { 60 parentExpression = container; 61 } 62 63 public Container getParentExpression() { 64 return parentExpression; 65 } 66 67 70 71 public void setSortKey(Expression exp) { 72 sortKey = exp; 73 } 74 75 78 79 public Expression getSortKey() { 80 return sortKey; 81 } 82 83 84 89 90 public void setOrder(Expression exp) { 91 order = exp; 92 } 93 94 public Expression getOrder() { 95 return order; 96 } 97 98 103 104 public void setDataTypeExpression(Expression exp) { 105 dataTypeExpression = exp; 106 } 107 108 public Expression getDataTypeExpression() { 109 return dataTypeExpression; 110 } 111 116 117 public void setCaseOrder(Expression exp) { 118 caseOrder = exp; 119 } 120 121 public Expression getCaseOrder() { 122 return caseOrder; 123 } 124 125 129 130 public void setLanguage(Expression exp) { 131 language = exp; 132 } 133 134 public Expression getLanguage() { 135 return language; 136 } 137 138 141 142 public void setCollationName(Expression collationName) { 143 this.collationName = collationName; 144 } 145 146 public Expression getCollationName() { 147 return collationName; 148 } 149 150 public void setCollation(Comparator collation) { 151 this.collation = collation; 152 } 153 154 public Comparator getCollation() { 155 return collation; 156 } 157 158 162 163 public void setEmptyFirst(boolean emptyFirst) { 164 this.emptyFirst = emptyFirst; 165 } 166 167 public boolean getEmptyFirst() { 168 return emptyFirst; 169 } 170 171 public SortKeyDefinition simplify(Executable exec) throws XPathException { 172 173 if (order instanceof StringValue && 174 (dataTypeExpression == null || dataTypeExpression instanceof StringValue) && 175 caseOrder instanceof StringValue && 176 language instanceof StringValue && 177 collation != null) { 178 179 FixedSortKeyDefinition fskd = new FixedSortKeyDefinition(); 180 fskd.setSortKey(sortKey); 181 fskd.setOrder(order); 182 fskd.setDataTypeExpression(dataTypeExpression); 183 fskd.setCaseOrder(caseOrder); 184 fskd.setLanguage(language); 185 fskd.setEmptyFirst(emptyFirst); 186 fskd.collation = collation; 187 fskd.bindComparer(exec.getConfiguration()); 188 return fskd; 189 } else { 190 return this; 191 } 192 } 193 194 200 201 public FixedSortKeyDefinition reduce(XPathContext context) throws XPathException { 202 203 FixedSortKeyDefinition sknew = new FixedSortKeyDefinition(); 204 205 Expression sortKey2 = sortKey; 206 207 sknew.setSortKey(sortKey2); 208 sknew.setOrder((StringValue)order.evaluateItem(context)); 209 sknew.setDataTypeExpression((StringValue)dataTypeExpression.evaluateItem(context)); 210 sknew.setCaseOrder((StringValue)caseOrder.evaluateItem(context)); 211 sknew.setLanguage((StringValue)language.evaluateItem(context)); 212 if (collation == null && collationName != null) { 213 String cname = collationName.evaluateItem(context).getStringValue(); 214 Comparator comp = context.getCollation(cname); 215 if (comp == null) { 216 throw new DynamicError("Collation " + cname + " is not recognized"); 217 } 218 sknew.setCollation(comp); 219 } 220 if (collation != null) { 221 sknew.setCollation(collation); 222 } 223 sknew.setEmptyFirst(emptyFirst); 224 sknew.bindComparer(context); 225 return sknew; 226 } 227 228 229 } 230 231 232 | Popular Tags |