| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.functions; |
| 9 |
|
| 10 |
import net.sf.saxon.expr.Callable; |
| 11 |
import net.sf.saxon.expr.Expression; |
| 12 |
import net.sf.saxon.expr.StringLiteral; |
| 13 |
import net.sf.saxon.expr.XPathContext; |
| 14 |
import net.sf.saxon.om.Sequence; |
| 15 |
import net.sf.saxon.regex.UnicodeString; |
| 16 |
import net.sf.saxon.trans.XPathException; |
| 17 |
import net.sf.saxon.tree.util.FastStringBuffer; |
| 18 |
import net.sf.saxon.value.StringValue; |
| 19 |
import net.sf.saxon.z.IntToIntHashMap; |
| 20 |
import net.sf.saxon.z.IntToIntMap; |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
public class Translate extends SystemFunction implements Callable, StatefulSystemFunction { |
| 27 |
|
| 28 |
private IntToIntMap staticMap = null; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
@Override |
| 38 |
public Expression fixArguments(Expression... arguments) { |
| 39 |
if (arguments[1] instanceof StringLiteral && arguments[2] instanceof StringLiteral) { |
| 40 |
staticMap = buildMap(((StringLiteral) arguments[1]).getValue(), ((StringLiteral) arguments[2]).getValue()); |
| 41 |
} |
| 42 |
return null; |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
public IntToIntMap getStaticMap() { |
| 52 |
return staticMap; |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
public static CharSequence translate(StringValue sv0, StringValue sv1, StringValue sv2) { |
| 65 |
|
| 66 |
|
| 67 |
if (sv0.containsSurrogatePairs() || sv1.containsSurrogatePairs() || sv2.containsSurrogatePairs()) { |
| 68 |
return translateUsingMap(sv0, buildMap(sv1, sv2)); |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
if (sv0.getStringLength() * sv1.getStringLength() > 1000) { |
| 73 |
|
| 74 |
return translateUsingMap(sv0, buildMap(sv1, sv2)); |
| 75 |
} |
| 76 |
|
| 77 |
CharSequence cs0 = sv0.getStringValueCS(); |
| 78 |
CharSequence cs1 = sv1.getStringValueCS(); |
| 79 |
CharSequence cs2 = sv2.getStringValueCS(); |
| 80 |
|
| 81 |
String st1 = cs1.toString(); |
| 82 |
FastStringBuffer sb = new FastStringBuffer(cs0.length()); |
| 83 |
int s2len = cs2.length(); |
| 84 |
int s0len = cs0.length(); |
| 85 |
for (int i = 0; i < s0len; i++) { |
| 86 |
char c = cs0.charAt(i); |
| 87 |
int j = st1.indexOf(c); |
| 88 |
if (j < s2len) { |
| 89 |
sb.append(j < 0 ? c : cs2.charAt(j)); |
| 90 |
} |
| 91 |
} |
| 92 |
return sb; |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
private static IntToIntMap buildMap(StringValue arg1, StringValue arg2) { |
| 104 |
UnicodeString a1 = arg1.getUnicodeString(); |
| 105 |
UnicodeString a2 = arg2.getUnicodeString(); |
| 106 |
IntToIntMap map = new IntToIntHashMap(a1.uLength(), 0.5); |
| 107 |
|
| 108 |
for (int i = 0; i < a1.uLength(); i++) { |
| 109 |
if (!map.find(a1.uCharAt(i))) { |
| 110 |
map.put(a1.uCharAt(i), i > a2.uLength() - 1 ? -1 : a2.uCharAt(i)); |
| 111 |
} |
| 112 |
|
| 113 |
} |
| 114 |
return map; |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
public static CharSequence translateUsingMap(StringValue in, IntToIntMap map) { |
| 128 |
UnicodeString us = in.getUnicodeString(); |
| 129 |
int len = us.uLength(); |
| 130 |
FastStringBuffer sb = new FastStringBuffer(len); |
| 131 |
for (int i = 0; i < len; i++) { |
| 132 |
int c = us.uCharAt(i); |
| 133 |
int newchar = map.get(c); |
| 134 |
if (newchar == Integer.MAX_VALUE) { |
| 135 |
|
| 136 |
newchar = c; |
| 137 |
} |
| 138 |
if (newchar != -1) { |
| 139 |
sb.appendWideChar(newchar); |
| 140 |
} |
| 141 |
|
| 142 |
} |
| 143 |
return sb; |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
public StringValue call(XPathContext context, Sequence[] arguments) throws XPathException { |
| 155 |
StringValue sv0 = (StringValue) arguments[0].head(); |
| 156 |
if (sv0 == null) { |
| 157 |
return StringValue.EMPTY_STRING; |
| 158 |
} |
| 159 |
if (staticMap != null) { |
| 160 |
return new StringValue(translateUsingMap(sv0, staticMap)); |
| 161 |
} else { |
| 162 |
StringValue sv1 = (StringValue) arguments[1].head(); |
| 163 |
StringValue sv2 = (StringValue) arguments[2].head(); |
| 164 |
return new StringValue(translate(sv0, sv1, sv2)); |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
public String getCompilerName() { |
| 169 |
return "TranslateCompiler"; |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
@Override |
| 180 |
public Translate copy() { |
| 181 |
Translate copy = (Translate) SystemFunction.makeFunction(getFunctionName().getLocalPart(), getRetainedStaticContext(), getArity()); |
| 182 |
copy.staticMap = staticMap; |
| 183 |
return copy; |
| 184 |
} |
| 185 |
|
| 186 |
} |
| 187 |
|