/
ConfigurationHandler.java
859 lines (742 loc) · 26.4 KB
/
ConfigurationHandler.java
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.felix.cm.file;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PushbackReader;
import java.io.Writer;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.BitSet;
import java.util.Collection;
import java.util.Collections;
import java.util.Dictionary;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
/**
* The <code>ConfigurationHandler</code> class implements configuration reading
* form a <code>java.io.InputStream</code> and writing to a
* <code>java.io.OutputStream</code> on behalf of the
* {@link FilePersistenceManager} class.
*
* <pre>
* cfg = prop "=" value .
* prop = symbolic-name . // 1.4.2 of OSGi Core Specification
* symbolic-name = token { "." token } .
* token = { [ 0..9 ] | [ a..z ] | [ A..Z ] | '_' | '-' } .
* value = [ type ] ( "[" values "]" | "(" values ")" | simple ) .
* values = simple { "," simple } .
* simple = """ stringsimple """ .
* type = // 1-char type code .
* stringsimple = // quoted string representation of the value .
* </pre>
*/
public class ConfigurationHandler
{
protected static final String ENCODING = "UTF-8";
protected static final int TOKEN_NAME = 'N';
protected static final int TOKEN_EQ = '=';
protected static final int TOKEN_ARR_OPEN = '[';
protected static final int TOKEN_ARR_CLOS = ']';
protected static final int TOKEN_VEC_OPEN = '(';
protected static final int TOKEN_VEC_CLOS = ')';
protected static final int TOKEN_COMMA = ',';
protected static final int TOKEN_VAL_OPEN = '"'; // '{';
protected static final int TOKEN_VAL_CLOS = '"'; // '}';
protected static final int TOKEN_COMMENT = '#';
// simple types (string & primitive wrappers)
protected static final int TOKEN_SIMPLE_STRING = 'T';
protected static final int TOKEN_SIMPLE_INTEGER = 'I';
protected static final int TOKEN_SIMPLE_LONG = 'L';
protected static final int TOKEN_SIMPLE_FLOAT = 'F';
protected static final int TOKEN_SIMPLE_DOUBLE = 'D';
protected static final int TOKEN_SIMPLE_BYTE = 'X';
protected static final int TOKEN_SIMPLE_SHORT = 'S';
protected static final int TOKEN_SIMPLE_CHARACTER = 'C';
protected static final int TOKEN_SIMPLE_BOOLEAN = 'B';
// primitives
protected static final int TOKEN_PRIMITIVE_INT = 'i';
protected static final int TOKEN_PRIMITIVE_LONG = 'l';
protected static final int TOKEN_PRIMITIVE_FLOAT = 'f';
protected static final int TOKEN_PRIMITIVE_DOUBLE = 'd';
protected static final int TOKEN_PRIMITIVE_BYTE = 'x';
protected static final int TOKEN_PRIMITIVE_SHORT = 's';
protected static final int TOKEN_PRIMITIVE_CHAR = 'c';
protected static final int TOKEN_PRIMITIVE_BOOLEAN = 'b';
protected static final String CRLF = "\r\n";
protected static final String INDENT = " ";
protected static final String COLLECTION_LINE_BREAK = " \\\r\n";
protected static final Map<Integer, Class<?>> code2Type;
protected static final Map<Class<?>, Integer> type2Code;
// set of valid characters for "symblic-name"
private static final BitSet NAME_CHARS;
private static final BitSet TOKEN_CHARS;
static
{
type2Code = new HashMap<Class<?>, Integer>();
// simple (exclusive String whose type code is not written)
type2Code.put( Integer.class, new Integer( TOKEN_SIMPLE_INTEGER ) );
type2Code.put( Long.class, new Integer( TOKEN_SIMPLE_LONG ) );
type2Code.put( Float.class, new Integer( TOKEN_SIMPLE_FLOAT ) );
type2Code.put( Double.class, new Integer( TOKEN_SIMPLE_DOUBLE ) );
type2Code.put( Byte.class, new Integer( TOKEN_SIMPLE_BYTE ) );
type2Code.put( Short.class, new Integer( TOKEN_SIMPLE_SHORT ) );
type2Code.put( Character.class, new Integer( TOKEN_SIMPLE_CHARACTER ) );
type2Code.put( Boolean.class, new Integer( TOKEN_SIMPLE_BOOLEAN ) );
// primitives
type2Code.put( Integer.TYPE, new Integer( TOKEN_PRIMITIVE_INT ) );
type2Code.put( Long.TYPE, new Integer( TOKEN_PRIMITIVE_LONG ) );
type2Code.put( Float.TYPE, new Integer( TOKEN_PRIMITIVE_FLOAT ) );
type2Code.put( Double.TYPE, new Integer( TOKEN_PRIMITIVE_DOUBLE ) );
type2Code.put( Byte.TYPE, new Integer( TOKEN_PRIMITIVE_BYTE ) );
type2Code.put( Short.TYPE, new Integer( TOKEN_PRIMITIVE_SHORT ) );
type2Code.put( Character.TYPE, new Integer( TOKEN_PRIMITIVE_CHAR ) );
type2Code.put( Boolean.TYPE, new Integer( TOKEN_PRIMITIVE_BOOLEAN ) );
// reverse map to map type codes to classes, string class mapping
// to be added manually, as the string type code is not written and
// hence not included in the type2Code map
code2Type = new HashMap<Integer, Class<?>>();
for(final Map.Entry<Class<?>, Integer> entry : type2Code.entrySet())
{
code2Type.put( entry.getValue(), entry.getKey() );
}
code2Type.put( new Integer( TOKEN_SIMPLE_STRING ), String.class );
NAME_CHARS = new BitSet();
for ( int i = '0'; i <= '9'; i++ )
NAME_CHARS.set( i );
for ( int i = 'a'; i <= 'z'; i++ )
NAME_CHARS.set( i );
for ( int i = 'A'; i <= 'Z'; i++ )
NAME_CHARS.set( i );
NAME_CHARS.set( '_' );
NAME_CHARS.set( '-' );
NAME_CHARS.set( '.' );
NAME_CHARS.set( '\\' );
TOKEN_CHARS = new BitSet();
TOKEN_CHARS.set( TOKEN_EQ );
TOKEN_CHARS.set( TOKEN_ARR_OPEN );
TOKEN_CHARS.set( TOKEN_ARR_CLOS );
TOKEN_CHARS.set( TOKEN_VEC_OPEN );
TOKEN_CHARS.set( TOKEN_VEC_CLOS );
TOKEN_CHARS.set( TOKEN_COMMA );
TOKEN_CHARS.set( TOKEN_VAL_OPEN );
TOKEN_CHARS.set( TOKEN_VAL_CLOS );
TOKEN_CHARS.set( TOKEN_SIMPLE_STRING );
TOKEN_CHARS.set( TOKEN_SIMPLE_INTEGER );
TOKEN_CHARS.set( TOKEN_SIMPLE_LONG );
TOKEN_CHARS.set( TOKEN_SIMPLE_FLOAT );
TOKEN_CHARS.set( TOKEN_SIMPLE_DOUBLE );
TOKEN_CHARS.set( TOKEN_SIMPLE_BYTE );
TOKEN_CHARS.set( TOKEN_SIMPLE_SHORT );
TOKEN_CHARS.set( TOKEN_SIMPLE_CHARACTER );
TOKEN_CHARS.set( TOKEN_SIMPLE_BOOLEAN );
// primitives
TOKEN_CHARS.set( TOKEN_PRIMITIVE_INT );
TOKEN_CHARS.set( TOKEN_PRIMITIVE_LONG );
TOKEN_CHARS.set( TOKEN_PRIMITIVE_FLOAT );
TOKEN_CHARS.set( TOKEN_PRIMITIVE_DOUBLE );
TOKEN_CHARS.set( TOKEN_PRIMITIVE_BYTE );
TOKEN_CHARS.set( TOKEN_PRIMITIVE_SHORT );
TOKEN_CHARS.set( TOKEN_PRIMITIVE_CHAR );
TOKEN_CHARS.set( TOKEN_PRIMITIVE_BOOLEAN );
}
/**
* Writes the configuration data from the <code>Dictionary</code> to the
* given <code>OutputStream</code>.
* <p>
* This method writes at the current location in the stream and does not
* close the output stream.
*
* @param out
* The <code>OutputStream</code> to write the configuration data
* to.
* @param properties
* The <code>Dictionary</code> to write.
* @throws IOException
* If an error occurs writing to the output stream.
*/
@SuppressWarnings("rawtypes")
public static void write( OutputStream out, Dictionary properties ) throws IOException
{
BufferedWriter bw = new BufferedWriter( new OutputStreamWriter( out, ENCODING ) );
for ( Enumeration ce = orderedKeys(properties); ce.hasMoreElements(); )
{
String key = ( String ) ce.nextElement();
// cfg = prop "=" value "." .
writeQuoted( bw, key );
bw.write( TOKEN_EQ );
writeValue( bw, properties.get( key ) );
bw.write( CRLF );
}
bw.flush();
}
/**
* Generates an <code>Enumeration</code> for the given
* <code>Dictionary</code> where the keys of the <code>Dictionary</code>
* are provided in sorted order.
*
* @param properties
* The <code>Dictionary</code> that keys are sorted.
* @return An <code>Enumeration</code> that provides the keys of
* properties in an ordered manner.
*/
@SuppressWarnings("rawtypes")
private static Enumeration orderedKeys(Dictionary properties) {
String[] keyArray = new String[properties.size()];
int i = 0;
for ( Enumeration ce = properties.keys(); ce.hasMoreElements(); )
{
keyArray[i] = ( String ) ce.nextElement();
i++;
}
Arrays.sort(keyArray);
return Collections.enumeration( Arrays.asList( keyArray ) );
}
/**
* Reads configuration data from the given <code>InputStream</code> and
* returns a new <code>Dictionary</code> object containing the data.
* <p>
* This method reads from the current location in the stream up to the end of
* the stream but does not close the stream at the end.
*
* @param ins
* The <code>InputStream</code> from which to read the
* configuration data.
* @return A <code>Dictionary</code> object containing the configuration
* data. This object may be empty if the stream contains no
* configuration data.
* @throws IOException
* If an error occurs reading from the stream. This exception
* is also thrown if a syntax error is encountered.
*/
@SuppressWarnings("rawtypes")
public static Dictionary read( InputStream ins ) throws IOException
{
return new ConfigurationHandler().readInternal( ins );
}
// private constructor, this class is not to be instantiated from the
// outside
private ConfigurationHandler()
{
}
// ---------- Configuration Input Implementation ---------------------------
private int token;
private String tokenValue;
private int line;
private int pos;
private Dictionary<String, ?> readInternal( InputStream ins ) throws IOException
{
BufferedReader br = new BufferedReader( new InputStreamReader( ins, ENCODING ) );
PushbackReader pr = new PushbackReader( br, 1 );
token = 0;
tokenValue = null;
line = 0;
pos = 0;
Dictionary<String, Object> configuration = new Hashtable<String, Object>();
token = 0;
while ( nextToken( pr, true ) == TOKEN_NAME )
{
String key = tokenValue;
// expect equal sign
if ( nextToken( pr, false ) != TOKEN_EQ )
{
throw readFailure( token, TOKEN_EQ );
}
// expect the token value
Object value = readValue( pr );
if ( value != null )
{
configuration.put( key, value );
}
}
return configuration;
}
/**
* value = type ( "[" values "]" | "(" values ")" | simple ) . values =
* value { "," value } . simple = "{" stringsimple "}" . type = // 1-char
* type code . stringsimple = // quoted string representation of the value .
*
* @param pr
* @return
* @throws IOException
*/
private Object readValue( PushbackReader pr ) throws IOException
{
// read (optional) type code
int type = read( pr );
// read value kind code if type code is not a value kinde code
int code;
if ( code2Type.containsKey( new Integer( type ) ) )
{
code = read( pr );
}
else
{
code = type;
type = TOKEN_SIMPLE_STRING;
}
switch ( code )
{
case TOKEN_ARR_OPEN:
return readArray( type, pr );
case TOKEN_VEC_OPEN:
return readCollection( type, pr );
case TOKEN_VAL_OPEN:
Object value = readSimple( type, pr );
ensureNext( pr, TOKEN_VAL_CLOS );
return value;
default:
return null;
}
}
private Object readArray( int typeCode, PushbackReader pr ) throws IOException
{
List<Object> list = new ArrayList<Object>();
for ( ;; )
{
int c = ignorablePageBreakAndWhiteSpace( pr );
if ( c == TOKEN_VAL_OPEN )
{
Object value = readSimple( typeCode, pr );
if ( value == null )
{
// abort due to error
return null;
}
ensureNext( pr, TOKEN_VAL_CLOS );
list.add( value );
c = ignorablePageBreakAndWhiteSpace( pr );
}
if ( c == TOKEN_ARR_CLOS )
{
Class<?> type = code2Type.get( new Integer( typeCode ) );
Object array = Array.newInstance( type, list.size() );
for ( int i = 0; i < list.size(); i++ )
{
Array.set( array, i, list.get( i ) );
}
return array;
}
else if ( c < 0 )
{
return null;
}
else if ( c != TOKEN_COMMA )
{
return null;
}
}
}
private Collection<Object> readCollection( int typeCode, PushbackReader pr ) throws IOException
{
Collection<Object> collection = new ArrayList<Object>();
for ( ;; )
{
int c = ignorablePageBreakAndWhiteSpace( pr );
if ( c == TOKEN_VAL_OPEN )
{
Object value = readSimple( typeCode, pr );
if ( value == null )
{
// abort due to error
return null;
}
ensureNext( pr, TOKEN_VAL_CLOS );
collection.add( value );
c = ignorablePageBreakAndWhiteSpace( pr );
}
if ( c == TOKEN_VEC_CLOS )
{
return collection;
}
else if ( c < 0 )
{
return null;
}
else if ( c != TOKEN_COMMA )
{
return null;
}
}
}
private Object readSimple( int code, PushbackReader pr ) throws IOException
{
switch ( code )
{
case -1:
return null;
case TOKEN_SIMPLE_STRING:
return readQuoted( pr );
// Simple/Primitive, only use wrapper classes
case TOKEN_SIMPLE_INTEGER:
case TOKEN_PRIMITIVE_INT:
return Integer.valueOf( readQuoted( pr ) );
case TOKEN_SIMPLE_LONG:
case TOKEN_PRIMITIVE_LONG:
return Long.valueOf( readQuoted( pr ) );
case TOKEN_SIMPLE_FLOAT:
case TOKEN_PRIMITIVE_FLOAT:
int fBits = Integer.parseInt( readQuoted( pr ) );
return new Float( Float.intBitsToFloat( fBits ) );
case TOKEN_SIMPLE_DOUBLE:
case TOKEN_PRIMITIVE_DOUBLE:
long dBits = Long.parseLong( readQuoted( pr ) );
return new Double( Double.longBitsToDouble( dBits ) );
case TOKEN_SIMPLE_BYTE:
case TOKEN_PRIMITIVE_BYTE:
return Byte.valueOf( readQuoted( pr ) );
case TOKEN_SIMPLE_SHORT:
case TOKEN_PRIMITIVE_SHORT:
return Short.valueOf( readQuoted( pr ) );
case TOKEN_SIMPLE_CHARACTER:
case TOKEN_PRIMITIVE_CHAR:
String cString = readQuoted( pr );
if ( cString != null && cString.length() > 0 )
{
return new Character( cString.charAt( 0 ) );
}
return null;
case TOKEN_SIMPLE_BOOLEAN:
case TOKEN_PRIMITIVE_BOOLEAN:
return Boolean.valueOf( readQuoted( pr ) );
// unknown type code
default:
return null;
}
}
private void ensureNext( PushbackReader pr, int expected ) throws IOException
{
int next = read( pr );
if ( next != expected )
{
readFailure( next, expected );
}
}
private String readQuoted( PushbackReader pr ) throws IOException
{
StringBuilder buf = new StringBuilder();
for ( ;; )
{
int c = read( pr );
switch ( c )
{
// escaped character
case '\\':
c = read( pr );
switch ( c )
{
// well known escapes
case 'b':
buf.append( '\b' );
break;
case 't':
buf.append( '\t' );
break;
case 'n':
buf.append( '\n' );
break;
case 'f':
buf.append( '\f' );
break;
case 'r':
buf.append( '\r' );
break;
case 'u':// need 4 characters !
char[] cbuf = new char[4];
if ( read( pr, cbuf ) == 4 )
{
c = Integer.parseInt( new String( cbuf ), 16 );
buf.append( ( char ) c );
}
break;
// just an escaped character, unescape
default:
buf.append( ( char ) c );
}
break;
// eof
case -1: // fall through
// separator token
case TOKEN_EQ:
case TOKEN_VAL_CLOS:
pr.unread( c );
return buf.toString();
// no escaping
default:
buf.append( ( char ) c );
}
}
}
private int nextToken( PushbackReader pr, final boolean newLine ) throws IOException
{
int c = ignorableWhiteSpace( pr );
// immediately return EOF
if ( c < 0 )
{
return ( token = c );
}
// check for comment
if ( newLine && c == TOKEN_COMMENT )
{
// skip everything until end of line
do
{
c = read( pr );
} while ( c != -1 && c != '\n' );
if ( c == -1 )
{
return ( token = c);
}
// and start over
return nextToken( pr, true );
}
// check whether there is a name
if ( NAME_CHARS.get( c ) || !TOKEN_CHARS.get( c ) )
{
// read the property name
pr.unread( c );
tokenValue = readQuoted( pr );
return ( token = TOKEN_NAME );
}
// check another token
if ( TOKEN_CHARS.get( c ) )
{
return ( token = c );
}
// unexpected character -> so what ??
return ( token = -1 );
}
private int ignorableWhiteSpace( PushbackReader pr ) throws IOException
{
int c = read( pr );
while ( c >= 0 && Character.isWhitespace( ( char ) c ) )
{
c = read( pr );
}
return c;
}
private int ignorablePageBreakAndWhiteSpace( PushbackReader pr ) throws IOException
{
int c = ignorableWhiteSpace( pr );
for ( ;; )
{
if ( c != '\\' )
{
break;
}
int c1 = pr.read();
if ( c1 == '\r' || c1 == '\n' )
{
c = ignorableWhiteSpace( pr );
} else {
pr.unread(c1);
break;
}
}
return c;
}
private int read( PushbackReader pr ) throws IOException
{
int c = pr.read();
if ( c == '\r' )
{
int c1 = pr.read();
if ( c1 != '\n' )
{
pr.unread( c1 );
}
c = '\n';
}
if ( c == '\n' )
{
line++;
pos = 0;
}
else
{
pos++;
}
return c;
}
private int read( PushbackReader pr, char[] buf ) throws IOException
{
for ( int i = 0; i < buf.length; i++ )
{
int c = read( pr );
if ( c >= 0 )
{
buf[i] = ( char ) c;
}
else
{
return i;
}
}
return buf.length;
}
private IOException readFailure( int current, int expected )
{
return new IOException( "Unexpected token " + current + "; expected: " + expected + " (line=" + line + ", pos="
+ pos + ")" );
}
// ---------- Configuration Output Implementation --------------------------
private static void writeValue( Writer out, Object value ) throws IOException
{
Class<?> clazz = value.getClass();
if ( clazz.isArray() )
{
writeArray( out, value );
}
else if ( value instanceof Collection )
{
writeCollection( out, ( Collection<?> ) value );
}
else
{
writeType( out, clazz );
writeSimple( out, value );
}
}
private static void writeArray( Writer out, Object arrayValue ) throws IOException
{
int size = Array.getLength( arrayValue );
writeType( out, arrayValue.getClass().getComponentType() );
out.write( TOKEN_ARR_OPEN );
out.write( COLLECTION_LINE_BREAK );
for ( int i = 0; i < size; i++ )
{
writeCollectionElement(out, Array.get( arrayValue, i ));
}
out.write( INDENT );
out.write( TOKEN_ARR_CLOS );
}
private static void writeCollection( Writer out, Collection<?> collection ) throws IOException
{
if ( collection.isEmpty() )
{
out.write( TOKEN_VEC_OPEN );
out.write( COLLECTION_LINE_BREAK );
out.write( TOKEN_VEC_CLOS );
}
else
{
Iterator<?> ci = collection.iterator();
Object firstElement = ci.next();
writeType( out, firstElement.getClass() );
out.write( TOKEN_VEC_OPEN );
out.write( COLLECTION_LINE_BREAK );
writeCollectionElement( out, firstElement );
while ( ci.hasNext() )
{
writeCollectionElement( out, ci.next() );
}
out.write( TOKEN_VEC_CLOS );
}
}
private static void writeCollectionElement(Writer out, Object element) throws IOException {
out.write( INDENT );
writeSimple( out, element );
out.write( TOKEN_COMMA );
out.write(COLLECTION_LINE_BREAK);
}
private static void writeType( Writer out, Class<?> valueType ) throws IOException
{
Integer code = type2Code.get( valueType );
if ( code != null )
{
out.write( ( char ) code.intValue() );
}
}
private static void writeSimple( Writer out, Object value ) throws IOException
{
if ( value instanceof Double )
{
double dVal = ( ( Double ) value ).doubleValue();
value = new Long( Double.doubleToRawLongBits( dVal ) );
}
else if ( value instanceof Float )
{
float fVal = ( ( Float ) value ).floatValue();
value = new Integer( Float.floatToRawIntBits( fVal ) );
}
out.write( TOKEN_VAL_OPEN );
writeQuoted( out, String.valueOf( value ) );
out.write( TOKEN_VAL_CLOS );
}
private static void writeQuoted( Writer out, String simple ) throws IOException
{
if ( simple == null || simple.length() == 0 )
{
return;
}
char c = 0;
int len = simple.length();
for ( int i = 0; i < len; i++ )
{
c = simple.charAt( i );
switch ( c )
{
case '\\':
case TOKEN_VAL_CLOS:
case ' ':
case TOKEN_EQ:
out.write( '\\' );
out.write( c );
break;
// well known escapes
case '\b':
out.write( "\\b" );
break;
case '\t':
out.write( "\\t" );
break;
case '\n':
out.write( "\\n" );
break;
case '\f':
out.write( "\\f" );
break;
case '\r':
out.write( "\\r" );
break;
// other escaping
default:
if ( c < ' ' )
{
String t = "000" + Integer.toHexString( c );
out.write( "\\u" + t.substring( t.length() - 4 ) );
}
else
{
out.write( c );
}
}
}
}
}