1 package net.sf.saxon.functions; 2 import net.sf.saxon.codenorm.Normalizer; 3 import net.sf.saxon.expr.XPathContext; 4 import net.sf.saxon.om.Item; 5 import net.sf.saxon.trans.DynamicError; 6 import net.sf.saxon.trans.XPathException; 7 import net.sf.saxon.value.StringValue; 8 9 12 13 public class NormalizeUnicode extends SystemFunction { 14 15 18 19 public Item evaluateItem(XPathContext c) throws XPathException { 20 StringValue sv = (StringValue)argument[0].evaluateItem(c); 21 if (sv==null) { 22 return StringValue.EMPTY_STRING; 23 } 24 byte fb = Normalizer.C; 25 if (argument.length == 2) { 26 String form = argument[1].evaluateAsString(c); 27 if (form.equals("NFC")) { 28 fb = Normalizer.C; 29 } else if (form.equals("NFD")) { 30 fb = Normalizer.D; 31 } else if (form.equals("NFKC")) { 32 fb = Normalizer.KC; 33 } else if (form.equals("NFKD")) { 34 fb = Normalizer.KD; 35 } else { 36 String msg = "Normalization form " + form + " is not supported"; 37 DynamicError err = new DynamicError(msg); 38 err.setErrorCode("FOCH0003"); 39 err.setXPathContext(c); 40 throw err; 41 } 42 } 43 Normalizer norm = new Normalizer(fb); 44 String result = norm.normalize(sv.getStringValue()); 45 return StringValue.makeStringValue(result); 46 } 47 48 } 49 50 51 52 53 | Popular Tags |