KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icl > saxon > functions > Translate


1 package com.icl.saxon.functions;
2 import com.icl.saxon.*;
3 import com.icl.saxon.expr.*;
4
5 //import java.util.*;
6
//import java.lang.Math;
7
//import java.text.*;
8

9
10
11 public class Translate extends Function {
12
13     public String JavaDoc getName() {
14         return "translate";
15     };
16
17     /**
18     * Determine the data type of the expression
19     * @return Value.STRING
20     */

21
22     public int getDataType() {
23         return Value.STRING;
24     }
25
26     /**
27     * Simplfy and validate
28     */

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     /**
47     * Evaluate the function in a string context
48     */

49
50     public String JavaDoc evaluateAsString(Context context) throws XPathException {
51
52         String JavaDoc s1 = argument[0].evaluateAsString(context);
53         String JavaDoc s2 = argument[1].evaluateAsString(context);
54         String JavaDoc s3 = argument[2].evaluateAsString(context);
55         
56         return translate(s1, s2, s3);
57     }
58
59     /**
60     * Evaluate in a general context
61     */

62
63     public Value evaluate(Context c) throws XPathException {
64         return new StringValue(evaluateAsString(c));
65     }
66     
67     /**
68     * Get dependencies
69     */

70
71     public int getDependencies() {
72         return argument[0].getDependencies() |
73                      argument[1].getDependencies() |
74                      argument[2].getDependencies();
75     }
76
77     /**
78     * Remove dependencies
79     */

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     /**
91     * Perform the translate function
92     */

93
94     private static String JavaDoc translate(String JavaDoc s0, String JavaDoc s1, String JavaDoc s2) {
95
96         // check for surrogate pairs
97
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 JavaDoc sb = new StringBuffer JavaDoc();
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     /**
119     * Perform the translate function when surrogate pairs are in use
120     */

121
122     private static String JavaDoc slowTranslate(String JavaDoc s0, String JavaDoc s1, String JavaDoc s2) {
123         int[] a0 = StringValue.expand(s0);
124         int[] a1 = StringValue.expand(s1);
125         int[] a2 = StringValue.expand(s2);
126         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
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                 // no new character
143
}
144
145             if (newchar>=0) {
146                 if (newchar<65536) {
147                     sb.append((char)newchar);
148                 }
149                 else { // output a surrogate pair
150
//To compute the numeric value of the character corresponding to a surrogate
151
//pair, use this formula (all numbers are hex):
152
//(FirstChar - D800) * 400 + (SecondChar - DC00) + 10000
153
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 //
168
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
169
// you may not use this file except in compliance with the License. You may obtain a copy of the
170
// License at http://www.mozilla.org/MPL/
171
//
172
// Software distributed under the License is distributed on an "AS IS" basis,
173
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
174
// See the License for the specific language governing rights and limitations under the License.
175
//
176
// The Original Code is: all this file.
177
//
178
// The Initial Developer of the Original Code is
179
// Michael Kay of International Computers Limited (mhkay@iclway.co.uk).
180
//
181
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
182
//
183
// Contributor(s): none.
184
//
185
Popular Tags