KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > functions > Translate


1 package net.sf.saxon.functions;
2 import net.sf.saxon.expr.XPathContext;
3 import net.sf.saxon.om.Item;
4 import net.sf.saxon.om.FastStringBuffer;
5 import net.sf.saxon.trans.XPathException;
6 import net.sf.saxon.value.AtomicValue;
7 import net.sf.saxon.value.StringValue;
8
9
10
11 public class Translate extends SystemFunction {
12
13     /**
14     * Evaluate the function
15     */

16
17     public Item evaluateItem(XPathContext context) throws XPathException {
18
19         AtomicValue sv = (AtomicValue)argument[0].evaluateItem(context);
20         if (sv==null) {
21             return StringValue.EMPTY_STRING;
22         };
23         CharSequence JavaDoc s1 = sv.getStringValueCS();
24
25         sv = (AtomicValue)argument[1].evaluateItem(context);
26         CharSequence JavaDoc s2 = sv.getStringValueCS();
27
28         sv = (AtomicValue)argument[2].evaluateItem(context);
29         CharSequence JavaDoc s3 = sv.getStringValueCS();
30
31         return StringValue.makeStringValue(translate(s1, s2, s3));
32     }
33
34     /**
35     * Perform the translate function
36     */

37
38     private static CharSequence JavaDoc translate(CharSequence JavaDoc s0, CharSequence JavaDoc s1, CharSequence JavaDoc s2) {
39
40         // check for surrogate pairs
41
int len0 = StringValue.getStringLength(s0);
42         int len1 = StringValue.getStringLength(s1);
43         int len2 = StringValue.getStringLength(s2);
44         if (s0.length()!=len0 ||
45                 s1.length()!=len1 ||
46                 s2.length()!=len2 ) {
47             return slowTranslate(s0, s1, s2);
48         }
49         String JavaDoc st1 = s1.toString();
50         FastStringBuffer sb = new FastStringBuffer(s0.length());
51         int s2len = s2.length();
52         for (int i=0; i<s0.length(); i++) {
53             char c = s0.charAt(i);
54             int j = st1.indexOf(c);
55             if (j<s2len) {
56                 sb.append(( j<0 ? c : s2.charAt(j) ));
57             }
58         }
59         return sb;
60     }
61
62     /**
63     * Perform the translate function when surrogate pairs are in use
64     */

65
66     private static CharSequence JavaDoc slowTranslate(CharSequence JavaDoc s0, CharSequence JavaDoc s1, CharSequence JavaDoc s2) {
67         int[] a0 = StringValue.expand(s0);
68         int[] a1 = StringValue.expand(s1);
69         int[] a2 = StringValue.expand(s2);
70         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(s0.length());
71         for (int i=0; i<a0.length; i++) {
72             int c = a0[i];
73             int j = -1;
74             for (int test=0; test<a1.length; test++) {
75                 if (a1[test]==c) {
76                     j = test;
77                     break;
78                 }
79             }
80             int newchar = -1;
81             if (j<0) {
82                 newchar = a0[i];
83             } else if (j<a2.length) {
84                 newchar = a2[j];
85             } else {
86                 // no new character
87
}
88
89             if (newchar>=0) {
90                 if (newchar<65536) {
91                     sb.append((char)newchar);
92                 }
93                 else { // output a surrogate pair
94
//To compute the numeric value of the character corresponding to a surrogate
95
//pair, use this formula (all numbers are hex):
96
//(FirstChar - D800) * 400 + (SecondChar - DC00) + 10000
97
newchar -= 65536;
98                     sb.append((char)((newchar / 1024) + 55296));
99                     sb.append((char)((newchar % 1024) + 56320));
100                 }
101             }
102         }
103         return sb;
104     }
105
106
107 }
108
109
110
111 //
112
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
113
// you may not use this file except in compliance with the License. You may obtain a copy of the
114
// License at http://www.mozilla.org/MPL/
115
//
116
// Software distributed under the License is distributed on an "AS IS" basis,
117
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
118
// See the License for the specific language governing rights and limitations under the License.
119
//
120
// The Original Code is: all this file.
121
//
122
// The Initial Developer of the Original Code is Michael H. Kay.
123
//
124
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
125
//
126
// Contributor(s): none.
127
//
128
Popular Tags