1 package net.sf.saxon.functions; 2 import net.sf.saxon.expr.Expression; 3 import net.sf.saxon.expr.StaticContext; 4 import net.sf.saxon.expr.XPathContext; 5 import net.sf.saxon.sort.AtomicComparer; 6 import net.sf.saxon.sort.CodepointCollator; 7 import net.sf.saxon.trans.StaticError; 8 import net.sf.saxon.trans.XPathException; 9 import net.sf.saxon.value.AtomicValue; 10 import net.sf.saxon.value.StringValue; 11 import net.sf.saxon.value.Value; 12 13 import java.util.Comparator ; 14 15 18 19 21 public abstract class CollatingFunction extends SystemFunction { 22 23 Comparator collation = null; 25 26 29 30 public Expression preEvaluate(StaticContext env) throws XPathException { 31 if (getNumberOfArguments() == getDetails().maxArguments) { 32 collation = env.getCollation(((Value)argument[getNumberOfArguments()-1]).getStringValue()); 34 if (collation == null) { 35 StaticError err = new StaticError("Unknown collation {" + 36 ((Value)argument[getNumberOfArguments()-1]).getStringValue() + '}'); 37 err.setErrorCode("FOCH0002"); 38 err.setLocator(this); 39 throw err; 40 } 41 return super.preEvaluate(env); 42 } else { 43 String uri = env.getDefaultCollationName(); 45 collation = env.getCollation(uri); 46 return super.preEvaluate(env); 47 } 48 } 49 50 56 57 protected AtomicComparer getAtomicComparer(int arg, XPathContext context) throws XPathException { 58 return new AtomicComparer(getCollator(arg, context, true), context); 59 } 60 61 71 72 protected Comparator getCollator(int arg, XPathContext context, boolean useDefault) throws XPathException { 73 74 if (collation != null) { 75 return collation; 77 } else { 78 int numargs = argument.length; 79 if (numargs > arg) { 80 AtomicValue av = (AtomicValue)argument[arg].evaluateItem(context); 81 StringValue collationValue = (StringValue)av.getPrimitiveValue(); 82 String collationName = collationValue.getStringValue(); 83 return context.getCollation(collationName); 84 } else if (useDefault) { 85 Comparator collator = context.getDefaultCollation(); 86 return (collator==null ? CodepointCollator.getInstance() : collator); 87 } else { 88 return CodepointCollator.getInstance(); 89 } 90 } 91 } 92 } 93 94 95 | Popular Tags |