ISO9075.java
3.64 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
/*
* Decompiled with CFR 0_118.
*/
package com.day.text;
import com.day.text.Text;
import com.day.text.XMLChar;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ISO9075 {
private static final Pattern ENCODE_PATTERN = Pattern.compile("_x\\p{XDigit}{4}_");
private static final char[] PADDING = new char[]{'0', '0', '0'};
private static final String HEX_DIGITS = "0123456789abcdefABCDEF";
private ISO9075() {
}
public static String encode(String name) {
if (name.length() == 0) {
return name;
}
if (XMLChar.isValidName(name) && name.indexOf("_x") < 0) {
return name;
}
StringBuffer encoded = new StringBuffer();
for (int i = 0; i < name.length(); ++i) {
if (i == 0) {
if (XMLChar.isNameStart(name.charAt(i))) {
if (ISO9075.needsEscaping(name, i)) {
ISO9075.encode('_', encoded);
continue;
}
encoded.append(name.charAt(i));
continue;
}
ISO9075.encode(name.charAt(i), encoded);
continue;
}
if (!XMLChar.isName(name.charAt(i))) {
ISO9075.encode(name.charAt(i), encoded);
continue;
}
if (ISO9075.needsEscaping(name, i)) {
ISO9075.encode('_', encoded);
continue;
}
encoded.append(name.charAt(i));
}
return encoded.toString();
}
public static String encodePath(String path) {
String[] names = Text.explode(path, 47, true);
StringBuffer encoded = new StringBuffer(path.length());
for (int i = 0; i < names.length; ++i) {
String index = null;
int idx = names[i].indexOf(91);
if (idx != -1) {
index = names[i].substring(idx);
names[i] = names[i].substring(0, idx);
}
encoded.append(ISO9075.encode(names[i]));
if (index != null) {
encoded.append(index);
}
if (i >= names.length - 1) continue;
encoded.append('/');
}
return encoded.toString();
}
public static String decode(String name) {
if (name.indexOf("_x") < 0) {
return name;
}
StringBuffer decoded = new StringBuffer();
Matcher m = ENCODE_PATTERN.matcher(name);
while (m.find()) {
char ch = (char)Integer.parseInt(m.group().substring(2, 6), 16);
if (ch == '$' || ch == '\\') {
m.appendReplacement(decoded, "\\" + ch);
continue;
}
m.appendReplacement(decoded, Character.toString(ch));
}
m.appendTail(decoded);
return decoded.toString();
}
private static void encode(char c, StringBuffer b) {
b.append("_x");
String hex = Integer.toHexString(c);
b.append(PADDING, 0, 4 - hex.length());
b.append(hex);
b.append("_");
}
private static boolean needsEscaping(String name, int location) throws ArrayIndexOutOfBoundsException {
if (name.charAt(location) == '_' && name.length() >= location + 6) {
return name.charAt(location + 1) == 'x' && "0123456789abcdefABCDEF".indexOf(name.charAt(location + 2)) != -1 && "0123456789abcdefABCDEF".indexOf(name.charAt(location + 3)) != -1 && "0123456789abcdefABCDEF".indexOf(name.charAt(location + 4)) != -1 && "0123456789abcdefABCDEF".indexOf(name.charAt(location + 5)) != -1;
}
return false;
}
}