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
|
package net.wotonomy.foundation;
/**
* Class for serializing/unserializing property lists in the .plist format
*/
public class NSPropertyListSerialization {
public static final int PLIST_ARRAY = 0;
public static final int PLIST_DICTIONARY = 1;
public static final int PLIST_DATA = 2;
public static final int PLIST_STRING = 3;
public static final char[] TOKEN_BEGIN = new char[] { '(', '{', '<', '"' };
public static final char[] TOKEN_END = new char[] { ')', '}', '>', '"' };
public static final char[] QUOTING_CHARS = new char[] { ':', '/', '-', '.', '\\' };
private NSPropertyListSerialization() {
super();
}
/**
* Creates a NSArray object from a string representation.
*
* @s The string representation of a NSArray object.
*/
public static NSArray<Object> arrayForString(String s) {
s = s.trim();
if (!(s.charAt(0) == TOKEN_BEGIN[PLIST_ARRAY] && s.charAt(s.length() - 1) == TOKEN_END[PLIST_ARRAY]))
return null;
NSMutableArray<Object> arr = new NSMutableArray<>();
int pos = 1;
int valbegin = -1;
while (pos < s.length()) {
char c = s.charAt(pos);
int tokenCount = 0;
int what = 0;
for (int i = 0; i < TOKEN_BEGIN.length; i++) {
if (c == TOKEN_BEGIN[i]) {
tokenCount = 1;
what = i;
break;
}
}
if (tokenCount > 0) {
// mark it
int quote = pos;
// find the closing token
do {
pos++;
try {
c = s.charAt(pos);
} catch (StringIndexOutOfBoundsException ex) {
throw new IllegalArgumentException(
"Could not parse property list; unclosed token '" + TOKEN_BEGIN[what] + "'");
}
if (c == '"' && what == PLIST_STRING) {
if (pos > 0 && s.charAt(pos - 1) != '\\') {
tokenCount--;
}
} else if (c == TOKEN_BEGIN[what])
tokenCount++;
else if (c == TOKEN_END[what])
tokenCount--;
} while (tokenCount > 0);
arr.addObject(propertyListFromString(s.substring(quote, pos + 1)));
valbegin = -1;
// advance to the next position
do {
pos++;
c = s.charAt(pos);
} while (Character.isWhitespace(c));
}
if (c == ',' || c == ')') {
if (valbegin > 0) {
arr.addObject(s.substring(valbegin, pos).trim());
valbegin = -1;
}
} else if (!Character.isWhitespace(c)) {
if (valbegin < 0) {
valbegin = pos;
}
}
pos++;
}
return arr;
}
/**
* Creates a NSDictionary instance from a string representation.
*
* @s The string representation of a NSDictionary.
*/
public static NSDictionary<Object, Object> dictionaryForString(String s) {
s = s.trim();
if (!(s.charAt(0) == TOKEN_BEGIN[PLIST_DICTIONARY] && s.charAt(s.length() - 1) == TOKEN_END[PLIST_DICTIONARY]))
return null;
NSMutableDictionary<Object, Object> d = new NSMutableDictionary<>();
int pos = 1;
Object key = null;
int valbegin = -1;
while (pos < s.length()) {
// look for an opening token
char c = s.charAt(pos);
int tokenCount = 0;
int what = 0;
for (int i = 0; i < TOKEN_BEGIN.length; i++) {
if (c == TOKEN_BEGIN[i]) {
tokenCount = 1;
what = i;
break;
}
}
if (tokenCount > 0) {
// mark it
int quote = pos;
// find the closing token
do {
pos++;
try {
c = s.charAt(pos);
} catch (StringIndexOutOfBoundsException ex) {
throw new IllegalArgumentException(
"Could not parse property list; unclosed token '" + TOKEN_BEGIN[what] + "'");
}
if (c == '"' && what == PLIST_STRING) {
if (pos > 0 && s.charAt(pos - 1) != '\\') {
tokenCount--;
}
} else if (c == TOKEN_BEGIN[what])
tokenCount++;
else if (c == TOKEN_END[what])
tokenCount--;
} while (tokenCount > 0);
if (key == null) {
key = propertyListFromString(s.substring(quote, pos + 1));
} else {
d.setObjectForKey(propertyListFromString(s.substring(quote, pos + 1)), key);
key = null;
}
valbegin = -1;
// advance to the next position
do {
pos++;
c = s.charAt(pos);
} while (Character.isWhitespace(c));
}
if (c == ';' || c == '=' || c == '}') {
if (valbegin > 0) {
if (key == null) {
key = s.substring(valbegin, pos).trim();
} else {
d.setObjectForKey(s.substring(valbegin, pos).trim(), key);
key = null;
}
valbegin = -1;
}
} else if (!Character.isWhitespace(c)) {
if (valbegin < 0) {
valbegin = pos;
}
}
pos++;
}
return d;
}
public static boolean booleanForString(String s) {
return s.trim().toLowerCase().equals("true");
}
/**
* Creates a NSData instance from a string representation.
*
* @s The string representation of a NSData object.
*/
public static NSData dataFromPropertyList(String s) {
String hex = "0123456789ABCDEF";
s = s.trim();
if (!(s.charAt(0) == TOKEN_BEGIN[PLIST_DATA] && s.charAt(s.length() - 1) == TOKEN_END[PLIST_DATA]))
return null;
int pos = 1;
java.io.ByteArrayOutputStream bout = new java.io.ByteArrayOutputStream();
while (pos < s.length() - 1) {
char c1 = s.charAt(pos);
while (c1 == ' ') {
pos++;
if (pos == s.length() - 1)
return new NSData(bout.toByteArray());
c1 = s.charAt(pos);
}
if (hex.indexOf(c1) < 0)
throw new IllegalArgumentException(
"The string does not represent a NSData object (" + s + ", pos " + pos + ")");
pos++;
char c2 = s.charAt(pos);
if (hex.indexOf(c2) < 0)
throw new IllegalArgumentException("The string does not represent a NSData object (" + s + ")");
int x = (hex.indexOf(c1) << 4) | hex.indexOf(c2);
bout.write(x);
pos++;
}
return new NSData(bout.toByteArray());
}
public static int intForString(String s) {
return Integer.parseInt(s);
}
/**
* Returns the string representation of a property list.
*
* @plist The property list. It can be a String, NSData, NSArray, NSDictionary.
*/
public static String stringForPropertyList(Object plist) {
if (plist == null)
return "";
if (plist instanceof NSArray || plist instanceof NSDictionary || plist instanceof NSData)
return plist.toString();
String x = plist.toString();
boolean quote = false;
for (int i = 0; i < x.length(); i++) {
char c = x.charAt(i);
for (int z = 0; z < TOKEN_BEGIN.length; z++) {
if (c == TOKEN_BEGIN[z] || c == TOKEN_END[z])
quote = true;
}
if (!quote) {
for (int z = 0; z < QUOTING_CHARS.length; z++) {
if (c == QUOTING_CHARS[z])
quote = true;
}
}
if (!quote && Character.isWhitespace(c)) {
quote = true;
i = x.length();
}
}
if (quote)
return "\"" + x + "\"";
return x;
}
/**
* Returns an property list created from a string representation.
*
* @s The string with a representation of a property list.
* @returns A property list object; either a NSData, NSArray, NSDictionary, or a
* String.
*/
public static Object propertyListFromString(String s) {
s = s.trim();
int type = -1;
for (int i = 0; i < TOKEN_BEGIN.length; i++) {
if (TOKEN_BEGIN[i] == s.charAt(0)) {
if (TOKEN_END[i] == s.charAt(s.length() - 1))
type = i;
}
}
switch (type) {
case PLIST_DATA:
return dataFromPropertyList(s);
case PLIST_ARRAY:
return arrayForString(s);
case PLIST_DICTIONARY:
return dictionaryForString(s);
case PLIST_STRING:
if (s.equals("\"\""))
return "";
return s.substring(1, s.length() - 1);
}
return s;
}
/*
* $Log$ Revision 1.2 2006/02/16 13:15:00 cgruber Check in all sources in
* eclipse-friendly maven-enabled packages.
*
* Revision 1.5 2003/08/11 18:18:08 chochos improved encoding of strings,
* removed warnings
*
* Revision 1.4 2003/08/11 17:33:45 chochos Implemented detection of escaped
* quotes (\"). improved quoting strings when converting property lists to
* strings.
*
* Revision 1.3 2003/08/04 23:50:55 chochos propertyListForString() now works.
* dictionaryForString and arrayForString can parse complex structures with
* nested arrays and dictionaries.
*
*/
}
|