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
|
package sun.applet;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import net.sourceforge.jnlp.PluginParameters;
public class PluginParameterParser {
static private final char DELIMITER_ESCAPE = ':';
static private final String KEY_VALUE_DELIMITER = ";";
/**
* Unescape characters passed from C++.
* Specifically, "\n" -> new line, "\\" -> "\", "\:" -> ";"
*
* @param str The string to unescape
* @return The unescaped string
*/
static String unescapeString(String str) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < str.length(); i++) {
char chr = str.charAt(i);
if (chr != '\\') {
sb.append(chr);
} else {
i++; // Skip ahead one
chr = str.charAt(i);
if (chr == 'n') {
sb.append('\n');
} else if (chr == '\\') {
sb.append('\\');
} else if (chr == DELIMITER_ESCAPE) {
sb.append(KEY_VALUE_DELIMITER);
}
}
}
return sb.toString();
}
/**
* Parse semi-colon delimited key-value pairs.
* @param keyvalString the escaped, semicolon-delimited, string
* @return a map of the keys to the values
*/
static Map<String, String> parseEscapedKeyValuePairs(String keyvalString) {
// Split on ';', ensuring empty strings at end are kept
String[] strs = keyvalString.split(KEY_VALUE_DELIMITER, -1 /* Keep empty strings */);
Map<String, String> attributes = new HashMap<String, String>();
/* Note that we will typically have one empty string at end */
for (int i = 0; i < strs.length - 1; i += 2) {
String key = unescapeString(strs[i]).toLowerCase();
String value = unescapeString(strs[i + 1]);
attributes.put(key, value);
}
return attributes;
}
static boolean isInt(String s) {
try {
Integer.parseInt(s);
return true;
} catch(NumberFormatException e) {
return false;
}
}
/**
* Parsers parameters given a string containing
* parameters in quotes.
*
* @param width default applet width
* @param height default applet height
* @param parameterString the parameters
* @return the attributes in a hash table
*/
public PluginParameters parse(String width,
String height, String parameterString) {
Map<String, String> params = parseEscapedKeyValuePairs(parameterString);
if (params.get("width") == null || !isInt(params.get("width"))) {
params.put("width", width);
}
if (params.get("height") == null || !isInt(params.get("height"))) {
params.put("height", height);
}
return new PluginParameters(params);
}
}
|