1 package com.icl.saxon.functions; 2 import com.icl.saxon.*; 3 import com.icl.saxon.expr.*; 4 5 9 10 11 public class Translate extends Function { 12 13 public String getName() { 14 return "translate"; 15 }; 16 17 21 22 public int getDataType() { 23 return Value.STRING; 24 } 25 26 29 30 public Expression simplify() throws XPathException { 31 int numArgs = checkArgumentCount(3,3); 32 argument[0] = argument[0].simplify(); 33 argument[1] = argument[1].simplify(); 34 argument[2] = argument[2].simplify(); 35 36 boolean fixed = (argument[0] instanceof Value) && 37 (argument[1] instanceof Value) && 38 (argument[2] instanceof Value); 39 40 if (fixed) { 41 return evaluate(null); 42 } 43 return this; 44 } 45 46 49 50 public String evaluateAsString(Context context) throws XPathException { 51 52 String s1 = argument[0].evaluateAsString(context); 53 String s2 = argument[1].evaluateAsString(context); 54 String s3 = argument[2].evaluateAsString(context); 55 56 return translate(s1, s2, s3); 57 } 58 59 62 63 public Value evaluate(Context c) throws XPathException { 64 return new StringValue(evaluateAsString(c)); 65 } 66 67 70 71 public int getDependencies() { 72 return argument[0].getDependencies() | 73 argument[1].getDependencies() | 74 argument[2].getDependencies(); 75 } 76 77 80 81 public Expression reduce(int dep, Context c) throws XPathException { 82 Translate f = new Translate(); 83 f.addArgument(argument[0].reduce(dep, c)); 84 f.addArgument(argument[1].reduce(dep, c)); 85 f.addArgument(argument[2].reduce(dep, c)); 86 f.setStaticContext(getStaticContext()); 87 return f.simplify(); 88 } 89 90 93 94 private static String translate(String s0, String s1, String s2) { 95 96 int len0 = StringValue.getLength(s0); 98 int len1 = StringValue.getLength(s1); 99 int len2 = StringValue.getLength(s2); 100 if (s0.length()!=len0 || 101 s1.length()!=len1 || 102 s2.length()!=len2 ) { 103 return slowTranslate(s0, s1, s2); 104 } 105 106 StringBuffer sb = new StringBuffer (); 107 int s2len = s2.length(); 108 for (int i=0; i<s0.length(); i++) { 109 char c = s0.charAt(i); 110 int j = s1.indexOf(c); 111 if (j<s2len) { 112 sb.append(( j<0 ? c : s2.charAt(j) )); 113 } 114 } 115 return sb.toString(); 116 } 117 118 121 122 private static String slowTranslate(String s0, String s1, String s2) { 123 int[] a0 = StringValue.expand(s0); 124 int[] a1 = StringValue.expand(s1); 125 int[] a2 = StringValue.expand(s2); 126 StringBuffer sb = new StringBuffer (); 127 for (int i=0; i<a0.length; i++) { 128 int c = a0[i]; 129 int j = -1; 130 for (int test=0; test<a1.length; test++) { 131 if (a1[test]==c) { 132 j = test; 133 break; 134 } 135 } 136 int newchar = -1; 137 if (j<0) { 138 newchar = a0[i]; 139 } else if (j<a2.length) { 140 newchar = a2[j]; 141 } else { 142 } 144 145 if (newchar>=0) { 146 if (newchar<65536) { 147 sb.append((char)newchar); 148 } 149 else { newchar -= 65536; 154 sb.append((char)((newchar / 1024) + 55296)); 155 sb.append((char)((newchar % 1024) + 56320)); 156 } 157 } 158 } 159 return sb.toString(); 160 } 161 162 163 } 164 165 166 167 | Popular Tags |