-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathEncode.java
More file actions
292 lines (247 loc) · 11 KB
/
Encode.java
File metadata and controls
292 lines (247 loc) · 11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
package org.perlonjava.runtime.perlmodule;
import org.perlonjava.runtime.runtimetypes.RuntimeArray;
import org.perlonjava.runtime.runtimetypes.RuntimeList;
import org.perlonjava.runtime.runtimetypes.RuntimeScalar;
import org.perlonjava.runtime.runtimetypes.RuntimeScalarCache;
import java.nio.charset.Charset;
import java.nio.charset.IllegalCharsetNameException;
import java.nio.charset.StandardCharsets;
import java.nio.charset.UnsupportedCharsetException;
import java.util.HashMap;
import java.util.Map;
import static org.perlonjava.runtime.runtimetypes.RuntimeScalarCache.scalarUndef;
import static org.perlonjava.runtime.runtimetypes.RuntimeScalarType.BYTE_STRING;
/**
* The Encode module for PerlOnJava.
* Provides character encoding/decoding functionality similar to Perl's Encode module.
*/
public class Encode extends PerlModuleBase {
private static final Map<String, Charset> CHARSET_ALIASES = new HashMap<>();
static {
// Initialize common charset aliases
CHARSET_ALIASES.put("utf8", StandardCharsets.UTF_8);
CHARSET_ALIASES.put("UTF8", StandardCharsets.UTF_8);
CHARSET_ALIASES.put("utf-8", StandardCharsets.UTF_8);
CHARSET_ALIASES.put("UTF-8", StandardCharsets.UTF_8);
CHARSET_ALIASES.put("latin1", StandardCharsets.ISO_8859_1);
CHARSET_ALIASES.put("Latin1", StandardCharsets.ISO_8859_1);
CHARSET_ALIASES.put("iso-8859-1", StandardCharsets.ISO_8859_1);
CHARSET_ALIASES.put("ISO-8859-1", StandardCharsets.ISO_8859_1);
CHARSET_ALIASES.put("ascii", StandardCharsets.US_ASCII);
CHARSET_ALIASES.put("ASCII", StandardCharsets.US_ASCII);
CHARSET_ALIASES.put("us-ascii", StandardCharsets.US_ASCII);
CHARSET_ALIASES.put("US-ASCII", StandardCharsets.US_ASCII);
CHARSET_ALIASES.put("utf16", StandardCharsets.UTF_16);
CHARSET_ALIASES.put("UTF16", StandardCharsets.UTF_16);
CHARSET_ALIASES.put("utf-16", StandardCharsets.UTF_16);
CHARSET_ALIASES.put("UTF-16", StandardCharsets.UTF_16);
CHARSET_ALIASES.put("utf16be", StandardCharsets.UTF_16BE);
CHARSET_ALIASES.put("UTF16BE", StandardCharsets.UTF_16BE);
CHARSET_ALIASES.put("utf-16be", StandardCharsets.UTF_16BE);
CHARSET_ALIASES.put("UTF-16BE", StandardCharsets.UTF_16BE);
CHARSET_ALIASES.put("utf16le", StandardCharsets.UTF_16LE);
CHARSET_ALIASES.put("UTF16LE", StandardCharsets.UTF_16LE);
CHARSET_ALIASES.put("utf-16le", StandardCharsets.UTF_16LE);
CHARSET_ALIASES.put("UTF-16LE", StandardCharsets.UTF_16LE);
}
public Encode() {
super("Encode", true);
}
public static void initialize() {
Encode encode = new Encode();
encode.initializeExporter();
encode.defineExport("EXPORT", "encode", "decode", "encode_utf8", "decode_utf8",
"is_utf8", "find_encoding", "from_to");
try {
encode.registerMethod("encode", null);
encode.registerMethod("decode", null);
encode.registerMethod("encode_utf8", null);
encode.registerMethod("decode_utf8", null);
encode.registerMethod("is_utf8", null);
encode.registerMethod("find_encoding", null);
encode.registerMethod("from_to", null);
encode.registerMethod("_utf8_on", null);
encode.registerMethod("_utf8_off", null);
} catch (NoSuchMethodException e) {
System.err.println("Warning: Missing Encode method: " + e.getMessage());
}
}
/**
* encode($encoding, $string [, $check])
* Encodes a string from Perl's internal format to the specified encoding.
*/
public static RuntimeList encode(RuntimeArray args, int ctx) {
if (args.size() < 2) {
throw new IllegalStateException("Bad number of arguments for encode");
}
String encodingName = args.get(0).toString();
String string = args.get(1).toString();
// TODO: Handle $check parameter (args.get(2)) for error handling modes
try {
Charset charset = getCharset(encodingName);
byte[] bytes = string.getBytes(charset);
// Return the encoded bytes as a byte string, inside a list
return new RuntimeScalar(bytes).getList();
} catch (Exception e) {
throw new RuntimeException("Cannot encode string to " + encodingName + ": " + e.getMessage());
}
}
/**
* decode($encoding, $octets [, $check])
* Decodes a string from the specified encoding to Perl's internal format.
*/
public static RuntimeList decode(RuntimeArray args, int ctx) {
if (args.size() < 2) {
throw new IllegalStateException("Bad number of arguments for decode");
}
String encodingName = args.get(0).toString();
String octets = args.get(1).toString();
// TODO: Handle $check parameter (args.get(2)) for error handling modes
try {
Charset charset = getCharset(encodingName);
// Convert the string to bytes assuming it contains raw octets
byte[] bytes = octets.getBytes(StandardCharsets.ISO_8859_1);
String decoded = new String(bytes, charset);
return new RuntimeScalar(decoded).getList();
} catch (Exception e) {
throw new RuntimeException("Cannot decode string from " + encodingName + ": " + e.getMessage());
}
}
/**
* encode_utf8($string)
* Equivalent to encode("utf8", $string)
*/
public static RuntimeList encode_utf8(RuntimeArray args, int ctx) {
if (args.size() < 1) {
throw new IllegalStateException("Bad number of arguments for encode_utf8");
}
String string = args.get(0).toString();
byte[] bytes = string.getBytes(StandardCharsets.UTF_8);
// Return the encoded bytes as a string, inside a list
return new RuntimeScalar(bytes).getList();
}
/**
* decode_utf8($octets [, $check])
* Equivalent to decode("utf8", $octets [, $check])
*/
public static RuntimeList decode_utf8(RuntimeArray args, int ctx) {
if (args.size() < 1) {
throw new IllegalStateException("Bad number of arguments for decode_utf8");
}
String octets = args.get(0).toString();
// TODO: Handle $check parameter (args.get(1)) for error handling modes
try {
// Convert the string to bytes assuming it contains raw octets
byte[] bytes = octets.getBytes(StandardCharsets.ISO_8859_1);
String decoded = new String(bytes, StandardCharsets.UTF_8);
return new RuntimeScalar(decoded).getList();
} catch (Exception e) {
throw new RuntimeException("Cannot decode UTF-8 string: " + e.getMessage());
}
}
/**
* is_utf8($string [, $check])
* Tests whether the UTF8 flag is turned on in the string.
*/
public static RuntimeList is_utf8(RuntimeArray args, int ctx) {
if (args.isEmpty()) {
throw new IllegalStateException("Bad number of arguments for is_utf8");
}
RuntimeScalar arg = args.get(0);
if (arg.type == BYTE_STRING) {
return RuntimeScalarCache.scalarFalse.getList();
}
String s = arg.toString();
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) > 255) {
return RuntimeScalarCache.scalarTrue.getList();
}
}
return RuntimeScalarCache.scalarFalse.getList();
}
/**
* find_encoding($encoding)
* Returns an encoding object for the given encoding name.
*/
public static RuntimeList find_encoding(RuntimeArray args, int ctx) {
if (args.isEmpty()) {
throw new IllegalStateException("Bad number of arguments for find_encoding");
}
String encodingName = args.get(0).toString();
try {
Charset charset = getCharset(encodingName);
// For now, return the charset name as a string
// TODO: Create proper encoding object
return new RuntimeScalar(charset.name()).getList();
} catch (Exception e) {
// Return undef if encoding not found
return scalarUndef.getList();
}
}
/**
* from_to($octets, $from_enc, $to_enc [, $check])
* Converts in-place the octet sequence from one encoding to another.
*/
public static RuntimeList from_to(RuntimeArray args, int ctx) {
if (args.size() < 3) {
throw new IllegalStateException("Bad number of arguments for from_to");
}
RuntimeScalar octetsRef = args.get(0);
String fromEnc = args.get(1).toString();
String toEnc = args.get(2).toString();
// TODO: Handle $check parameter (args.get(3)) for error handling modes
try {
Charset fromCharset = getCharset(fromEnc);
Charset toCharset = getCharset(toEnc);
// Get the octets
String octets = octetsRef.toString();
byte[] bytes = octets.getBytes(StandardCharsets.ISO_8859_1);
// Decode from source encoding
String decoded = new String(bytes, fromCharset);
// Encode to target encoding
byte[] encoded = decoded.getBytes(toCharset);
// Update the original scalar in-place
octetsRef.set(new String(encoded, StandardCharsets.ISO_8859_1));
// Return the number of characters converted
return new RuntimeScalar(decoded.length()).getList();
} catch (Exception e) {
throw new RuntimeException("Cannot convert from " + fromEnc + " to " + toEnc + ": " + e.getMessage());
}
}
public static RuntimeList _utf8_on(RuntimeArray args, int ctx) {
if (args.isEmpty()) {
throw new IllegalStateException("Bad number of arguments for _utf8_on");
}
return scalarUndef.getList();
}
public static RuntimeList _utf8_off(RuntimeArray args, int ctx) {
if (args.isEmpty()) {
throw new IllegalStateException("Bad number of arguments for _utf8_off");
}
RuntimeScalar arg = args.get(0);
if (arg.type != BYTE_STRING) {
String s = arg.toString();
byte[] bytes = s.getBytes(StandardCharsets.UTF_8);
arg.set(new String(bytes, StandardCharsets.ISO_8859_1));
arg.type = BYTE_STRING;
}
return scalarUndef.getList();
}
/**
* Helper method to get a Charset from an encoding name.
* Handles common aliases and Perl-style encoding names.
*/
private static Charset getCharset(String encodingName) {
// Check aliases first
Charset charset = CHARSET_ALIASES.get(encodingName);
if (charset != null) {
return charset;
}
// Try to get charset by name
try {
return Charset.forName(encodingName);
} catch (IllegalCharsetNameException | UnsupportedCharsetException e) {
throw new RuntimeException("Unknown encoding: " + encodingName);
}
}
}