summaryrefslogtreecommitdiffstats
path: root/dcaedt_validator/kwalify/src/main/java/kwalify/PlainYamlParser.java
blob: 5f23a19f67ddf20b421999b7f919d9459b67be9e (plain)
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
/*
 * copyright(c) 2005 kuwata-lab all rights reserved.
 */

package kwalify;

import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;
import java.util.IdentityHashMap;
import java.util.regex.Matcher;
import java.util.Calendar;
import java.util.TimeZone;

/**
 * plain yaml parser class which is a parent of YamlParser class.
 */
public class PlainYamlParser implements Parser {

    private static final String ANCHOR = "anchor '";
    private static final String ENDFLAG_EOF       = "<EOF>";
    private static final String ENDFLAG_DOC_BEGIN = "---";
    private static final String ENDFLAG_DOC_END   = "...";
    private static final String REGEXP1 = "^( *)(.*)";
    private static final String REGEXP2 = "^((?::?[-.\\w]+|'.*?'|\".*?\"|=|<<) *):(( +)(.*))?$";

    public static class Alias {
        private String label;
        private int lineNum;

        Alias(String label, int lineNum) {
            this.label = label;
            this.lineNum = lineNum;
        }

        String getLabel() { return label; }

        int getLineNumber() { return lineNum; }
    }


    private String[] lines;
    private String line = null;
    private int linenum = 0;
    private Map<String,Object> anchors = new HashMap<>();
    private Map<String,Integer> aliases = new HashMap<>();
    private String endFlag = null;
    private String sbuf = null;
    private int index = 0;

    PlainYamlParser(String yamlStr) {
        List list = Util.toListOfLines(yamlStr);
        int len = list.size();
        lines = new String[len + 1];
        for (int i = 0; i < len; i++) {
            lines[i + 1] = (String)list.get(i);
        }
    }

    public Object parse() throws SyntaxException {
        Object data = parseChild(0);
        if (data == null && endFlag.equals(ENDFLAG_DOC_BEGIN)) {
            data = parseChild(0);
        }
        if (aliases.size() > 0) {
            resolveAliases(data);
        }
        return data;
    }

    public boolean hasNext() {
        return !endFlag.equals(ENDFLAG_EOF);
    }

    private List createSequence() {
        return new ArrayList();
    }

    private void addSequenceValue(List seq, Object value) {
        seq.add(value);
    }

    private void setSequenceValueAt(List seq, int index, Object value) {
        seq.set(index, value);
    }

    Map createMapping() {
        return new DefaultableHashMap();
    }

    private void setMappingValueWith(Map map, Object key, Object value) {
        map.put(key, value);
    }

    void setMappingDefault(Map map, Object value) {
        if (map instanceof Defaultable) {
            ((Defaultable)map).setDefault(value);
        }
    }

    private void mergeMapping(Map map, Map map2) {
        for (Object key : map2.keySet()) {
            if (!map.containsKey(key)) {
                Object value = map2.get(key);
                map.put(key, value);
            }
        }
    }

    private void mergeList(Map map, List maplist) throws SyntaxException {
        for (Object elem : maplist) {
            mergeCollection(map, elem);
        }
    }

    private void mergeCollection(Map map, Object collection) throws SyntaxException {
        if (collection instanceof Map) {
            mergeMapping(map, (Map)collection);
        } else if (collection instanceof List) {
            mergeList(map, (List)collection);
        } else {
            throw syntaxError("'<<' requires collection (mapping, or sequence of mapping).");
        }
    }

    private Object createScalar(Object value) {
        return value;
    }

    private String currentLine() {
        return line;
    }

    int currentLineNumber() {
        return linenum;
    }

    protected String getLine() {
        String currentLine;
        do {
            currentLine = getCurrentLine();
        } while (currentLine != null && Util.matches(currentLine, "^\\s*($|#)"));
        return currentLine;
    }

    private String getCurrentLine() {
        if (++linenum < lines.length) {
            line = lines[linenum];
            if (Util.matches(line, "^\\.\\.\\.$")) {
                line = null;
                endFlag = ENDFLAG_DOC_END;
            } else if (Util.matches(line, "^---( [!%].*)?$")) {
                line = null;
                endFlag = ENDFLAG_DOC_BEGIN;
            }
        } else {
            line = null;
            endFlag = ENDFLAG_EOF;
        }
        return line;
    }

    private void resetBuffer(String str) {
        sbuf = str.charAt(str.length() - 1) == '\n' ? str : str + "\n";
        index = -1;
    }

    private int getCurrentCharacter() {
        if (index + 1 < sbuf.length()) {
            index++;
        } else {
            String currentLine = getLine();
            if (currentLine == null) {
                return -1;
            }
            resetBuffer(currentLine);
            index++;
        }
        return sbuf.charAt(index);
    }

    private int getChar() {
        int ch;
        do {
            ch = getCurrentCharacter();
        } while (ch >= 0 && isWhite(ch));
        return ch;
    }

    private int getCharOrNewline() {
        int ch;
        do {
            ch = getCurrentCharacter();
        } while (ch >= 0 && isWhite(ch) && ch != '\n');
        return ch;
    }

    private int currentChar() {
        return sbuf.charAt(index);
    }

    private SyntaxException syntaxError(String message, int linenum) {
        return new YamlSyntaxException(message, linenum);
    }

    private SyntaxException syntaxError(String message) {
        return new SyntaxException(message, linenum);
    }

    private Object parseChild(int column) throws SyntaxException {
        String currentLine = getLine();
        if (currentLine == null) {
            return createScalar(null);
        }
        Matcher m = Util.matcher(currentLine, REGEXP1);
        if (! m.find()) {
            assert false;
            return null;
        }
        int indent = m.group(1).length();
        if (indent < column) {
            return createScalar(null);
        }
        String value = m.group(2);
        return parseValue(column, value, indent);
    }

    private Object parseValue(int column, String value, int valueStartColumn) throws SyntaxException {
        Object data;
        if        (Util.matches(value, "^-( |$)")) {
            data = parseSequence(valueStartColumn, value);
        } else if (Util.matches(value, REGEXP2)) {
            data = parseMapping(valueStartColumn, value);
        } else if (Util.matches(value, "^[\\[\\{]")) {
            data = parseFlowStyle(value);
        } else if (Util.matches(value, "^\\&[-\\w]+( |$)")) {
            data = parseAnchor(column, value);
        } else if (Util.matches(value, "^\\*[-\\w]+( |$)")) {
            data = parseAlias(value);
        } else if (Util.matches(value, "^[|>]")) {
            data = parseBlockText(column, value);
        } else if (Util.matches(value, "^!")) {
            data = parseTag(column, value);
        } else if (Util.matches(value, "^\\#")) {
            data = parseChild(column);
        } else {
            data = parseScalar(value);
        }
        return data;
    }

    private static boolean isWhite(int ch) {
        return ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r';
    }


    private Object parseFlowStyle(String value) throws SyntaxException {
        resetBuffer(value);
        getChar();
        Object data = parseFlow(0);
        int ch = currentChar();
        assert ch == ']' || ch == '}';
        ch = getCharOrNewline();
        if (ch != '\n' && ch != '#' && ch >= 0) {
            throw syntaxError("flow style sequence is closed buf got '" + ((char)ch) + "'.");
        }
        if (ch >= 0) {
            getLine();
        }
        return data;
    }

    private Object parseFlow(int depth) throws SyntaxException {
        int ch = currentChar();
        if (ch < 0) {
            throw syntaxError("found EOF when parsing flow style.");
        }
        Object data;
        if (ch == '[') {
            data = parseFlowSequence(depth);
        } else if (ch == '{') {
            data = parseFlowMapping(depth);
        } else {
            data = parseFlowScalar();
        }
        return data;
    }

    private List parseFlowSequence(int depth) throws SyntaxException {
        assert currentChar() == '[';
        List seq = createSequence();
        int ch = getChar();
        if (ch != '}') {
            addSequenceValue(seq, parseFlowSequenceItem(depth + 1));
            while ((ch = currentChar()) == ',') {
                ch = getChar();
                if (ch == '}') {
                    throw syntaxError("sequence item required (or last comma is extra).");
                }
                addSequenceValue(seq, parseFlowSequenceItem(depth + 1));
            }
        }
        if (currentChar() != ']') {
            throw syntaxError("flow style sequence requires ']'.");
        }
        if (depth > 0) {
            getChar();
        }
        return seq;
    }

    private Object parseFlowSequenceItem(int depth) throws SyntaxException {
        return parseFlow(depth);
    }

    private Map parseFlowMapping(int depth) throws SyntaxException {
        assert currentChar() == '{';
        Map map = createMapping();
        int ch = getChar();
        if (ch != '}') {
            Object[] pair = parseFlowMappingItem(depth + 1);
            Object key   = pair[0];
            Object value = pair[1];
            setMappingValueWith(map, key, value);
            while ((ch = currentChar()) == ',') {
                ch = getChar();
                if (ch == '}') {
                    throw syntaxError("mapping item required (or last comman is extra.");
                }
                pair = parseFlowMappingItem(depth + 1);
                key   = pair[0];
                value = pair[1];
                setMappingValueWith(map, key, value);
            }
        }
        if (currentChar() != '}') {
            throw syntaxError("flow style mapping requires '}'.");
        }
        if (depth > 0) {
            getChar();
        }
        return map;
    }

    private Object[] parseFlowMappingItem(int depth) throws SyntaxException {
        Object key = parseFlow(depth);
        int ch = currentChar();
        if (ch != ':') {
            String s = ch >= 0 ? "'" + ((char)ch) + "'" : "EOF";
            throw syntaxError("':' expected but got " + s);
        }
        getChar();
        Object value = parseFlow(depth);
        return new Object[] { key, value };
    }

    private Object parseFlowScalar() {
        int ch = currentChar();
        Object scalar;
        StringBuilder sb = new StringBuilder();
        if (ch == '"' || ch == '\'') {
            int endch = ch;
            while ((ch = getCurrentCharacter()) >= 0 && ch != endch) {
                sb.append((char)ch);
            }
            getChar();
            scalar = sb.toString();
        } else {
            sb.append((char)ch);
            while ((ch = getCurrentCharacter()) >= 0 && ch != ':' && ch != ',' && ch != ']' && ch != '}') {
                sb.append((char)ch);
            }
            scalar = toScalar(sb.toString().trim());
        }
        return createScalar(scalar);
    }

    private Object parseTag(int column, String value) throws SyntaxException {
        assert Util.matches(value, "^!\\S+");
        Matcher m = Util.matcher(value, "^!(\\S+)((\\s+)(.*))?$");
        if (! m.find()) {
            assert false;
            return null;
        }
        String tag = m.group(1);
        String space = m.group(3);
        String value2 = m.group(4);
        Object data;
        if (value2 != null && value2.length() > 0) {
            int valueStartColumn = column + 1 + tag.length() + space.length();
            data = parseValue(column, value2, valueStartColumn);
        } else {
            data = parseChild(column);
        }
        return data;
    }

    private Object parseAnchor(int column, String value) throws SyntaxException {
        assert Util.matches(value, "^\\&([-\\w]+)(( *)(.*))?$");
        Matcher m = Util.matcher(value, "^\\&([-\\w]+)(( *)(.*))?$");
        if (! m.find()) {
            assert false;
            return null;
        }
        String label  = m.group(1);
        String space  = m.group(3);
        String value2 = m.group(4);
        Object data;
        if (value2 != null && value2.length() > 0) {
            int valueStartColumn = column + 1 + label.length() + space.length();
            data = parseValue(column, value2, valueStartColumn);
        } else {
            data = parseChild(column);
        }
        registerAnchor(label, data);
        return data;
    }

    private void registerAnchor(String label, Object data) throws SyntaxException {
        if (anchors.containsKey(label)) {
            throw syntaxError(ANCHOR + label + "' is already used.");
        }
        anchors.put(label, data);
    }

    private Object parseAlias(String value) throws SyntaxException {
        assert value.matches("^\\*([-\\w]+)(( *)(.*))?$");
        Matcher m = Util.matcher(value, "^\\*([-\\w]+)(( *)(.*))?$");
        if (! m.find()) {
            assert false;
            return null;
        }
        String label  = m.group(1);
        String value2 = m.group(4);
        if (value2 != null && value2.length() > 0 && value2.charAt(0) != '#') {
            throw syntaxError("alias cannot take any data.");
        }
        Object data = anchors.get(label);
        if (data == null) {
            data = registerAlias(label);
        }
        getLine();
        return data;
    }

    private Alias registerAlias(String label) {
        aliases.merge(label, 1, (a, b) -> a + b);
        return new Alias(label, linenum);
    }


    private void resolveAliases(Object data) throws SyntaxException {
        Map resolved = new IdentityHashMap();
        resolveAliases(data, resolved);
    }


    private void resolveAliases(Object data, Map resolved) throws SyntaxException {
        if (resolved.containsKey(data)) {
            return;
        }
        resolved.put(data, data);
        if (data instanceof List) {
            resolveAliases((List)data, resolved);
        } else if (data instanceof Map) {
            resolveAliases((Map)data, resolved);
        } else {
            assert !(data instanceof Alias);
        }
        if (data instanceof Defaultable) {
            Object defaultValue = ((Defaultable)data).getDefault();
            if (defaultValue != null) {
                resolveAliases(defaultValue, resolved);
            }
        }
    }

    private void resolveAliases(List seq, Map resolved) throws SyntaxException {
        int len = seq.size();
        for (int i = 0; i < len; i++) {
            Object val = seq.get(i);
            if (val instanceof Alias) {
                Alias alias = (Alias)val;
                String label = alias.getLabel();
                if (anchors.containsKey(label)) {
                    setSequenceValueAt(seq, i, anchors.get(label));
                } else {
                    throw syntaxError(ANCHOR + alias.getLabel() + "' not found.");
                }
            } else if (val instanceof List || val instanceof Map) {
                resolveAliases(val, resolved);
            }
        }
    }

    private void resolveAliases(Map map, Map resolved) throws SyntaxException {
        for (Object key : map.keySet()) {
            Object val = map.get(key);
            if (val instanceof Alias) {
                Alias alias = (Alias) val;
                String label = alias.getLabel();
                if (anchors.containsKey(label)) {
                    setMappingValueWith(map, key, anchors.get(label));
                } else {
                    throw syntaxError(ANCHOR + alias.getLabel() + "' not found.", alias.getLineNumber());
                }
            } else if (val instanceof List || val instanceof Map) {
                resolveAliases(val, resolved);
            }
        }
    }

    private Object parseBlockText(int column, String value) throws SyntaxException {
        assert Util.matches(value, "^[>|]");
        Matcher m = Util.matcher(value, "^([>|])([-+]?)(\\d*)\\s*(.*)$");
        if (! m.find()) {
            assert false;
            return null;
        }
        char blockChar = m.group(1).length() > 0 ? m.group(1).charAt(0) : '\0';
        char indicator = m.group(2).length() > 0 ? m.group(2).charAt(0) : '\0';
        int indent     = m.group(3).length() > 0 ? Integer.parseInt(m.group(3)) : -1;
        String text    = m.group(4);
        char sep = blockChar == '|' ? '\n' : ' ';
        String currentLine;
        StringBuilder sb = new StringBuilder();
        int n = 0;
        while ((currentLine = getCurrentLine()) != null) {
            m = Util.matcher(currentLine, "^( *)(.*)$");
            m.find();
            String space = m.group(1);
            String str   = m.group(2);
            if (indent < 0) {
                indent = space.length();
            }
            if (str.length() == 0) {
                n++;
            } else {
                int slen = space.length();
                if (slen < column) {
                    break;
                } else if (slen < indent) {
                    throw syntaxError("invalid indent in block text.");
                } else {
                    if (n > 0) {
                        if (blockChar == '>' && sb.length() > 0) {
                            sb.deleteCharAt(sb.length() - 1);
                        }
                        for (int i = 0; i < n; i++) {
                            sb.append('\n');
                        }
                        n = 0;
                    }
                    str = currentLine.substring(indent);
                }
            }
            sb.append(str);
            if ((blockChar == '>') && (sb.charAt(sb.length() - 1) == '\n')) {
                    sb.setCharAt(sb.length() - 1, ' ');
            }
        }
        if (currentLine != null && Util.matches(currentLine, "^ *#")) {
            getLine();
        }
        switch (indicator) {
        case '+':
            handlePlus(blockChar, sb, n);
            break;
        case '-':
            handleMinus(sep, sb);
            break;
        default:
            if (blockChar == '>') {
                sb.setCharAt(sb.length() - 1, '\n');
            }
        }
        return createScalar(text + sb.toString());
    }

    private void handleMinus(char sep, StringBuilder sb) {
        if (sb.charAt(sb.length() - 1) == sep) {
            sb.deleteCharAt(sb.length() - 1);
        }
    }

    private void handlePlus(char blockChar, StringBuilder sb, int n) {
        if (n > 0) {
            if (blockChar == '>') {
                sb.setCharAt(sb.length() - 1, '\n');
            }
            for (int i = 0; i < n; i++) {
                sb.append('\n');
            }
        }
    }


    private List parseSequence(int column, String value) throws SyntaxException {
        assert Util.matches(value, "^-(( +)(.*))?$");
        List seq = createSequence();
        while (true) {
            Matcher m = Util.matcher(value, "^-(( +)(.*))?$");
            if (! m.find()) {
                throw syntaxError("sequence item is expected.");
            }
            String space  = m.group(2);
            String value2 = m.group(3);
            int column2   = column + 1;

            Object elem;
            if (value2 == null || value2.length() == 0) {
                elem = parseChild(column2);
            } else {
                int valueStartColumn = column2 + space.length();
                elem = parseValue(column2, value2, valueStartColumn);
            }
            addSequenceValue(seq, elem);

            String currentLine = currentLine();
            if (currentLine == null) {
                break;
            }
            Matcher m2 = Util.matcher(currentLine, REGEXP1);
            m2.find();
            int indent = m2.group(1).length();
            if (indent < column) {
                break;
            } else if (indent > column) {
                throw syntaxError("invalid indent of sequence.");
            }
            value = m2.group(2);
        }
        return seq;
    }


    private Map parseMapping(int column, String value) throws SyntaxException {
        assert Util.matches(value, REGEXP2);
        Map map = createMapping();
        while (true) {
            Matcher m = Util.matcher(value, REGEXP2);
            if (! m.find()) {
                throw syntaxError("mapping item is expected.");
            }
            String v = m.group(1).trim();
            Object key = toScalar(v);
            String value2 = m.group(4);
            int column2 = column + 1;

            Object elem;
            if (value2 == null || value2.length() == 0) {
                elem = parseChild(column2);
            } else {
                int valueStartColumn = column2 + m.group(1).length() + m.group(3).length();
                elem = parseValue(column2, value2, valueStartColumn);
            }
            if ("=".equals(v)) {
                setMappingDefault(map, elem);
            } else if ("<<".equals(v)) {
                mergeCollection(map, elem);
            } else {
                setMappingValueWith(map, key, elem);
            }

            String currentLine = currentLine();
            if (currentLine == null) {
                break;
            }
            Matcher m2 = Util.matcher(currentLine, REGEXP1);
            m2.find();
            int indent = m2.group(1).length();
            if (indent < column) {
                break;
            } else if (indent > column) {
                throw syntaxError("invalid indent of mapping.");
            }
            value = m2.group(2);
        }
        return map;
    }


    private Object parseScalar(String value) {
        Object data = createScalar(toScalar(value));
        getLine();
        return data;
    }


    private Object toScalar(String value) {
        Matcher m;
        if ((m = Util.matcher(value, "^\"(.*)\"([ \t]*#.*$)?")).find()) {
            return m.group(1);
        } else if ((m = Util.matcher(value, "^'(.*)'([ \t]*#.*$)?")).find()) {
            return m.group(1);
        } else if ((m = Util.matcher(value, "^(.*\\S)[ \t]*#")).find()) {
            value = m.group(1);
        }

        if (Util.matches(value, "^-?0x\\d+$")) {
            return Integer.parseInt(value, 16);
        } else if (Util.matches(value, "^-?0\\d+$")) {
            return Integer.parseInt(value, 8);
        } else if (Util.matches(value, "^-?\\d+$")) {
            return Integer.parseInt(value, 10);
        } else if (Util.matches(value, "^-?\\d+\\.\\d+$")) {
            return Double.parseDouble(value);
        } else if (Util.matches(value, "^(true|yes|on)$")) {
            return Boolean.TRUE;
        } else if (Util.matches(value, "^(false|no|off)$")) {
            return Boolean.FALSE;
        } else if (Util.matches(value, "^(null|~)$")){
            return null;
        } else if (Util.matches(value, "^:(\\w+)$"))       {
            return value;
        } else if ((m = Util.matcher(value, "^(\\d\\d\\d\\d)-(\\d\\d)-(\\d\\d)$")).find()) {
            int year  = Integer.parseInt(m.group(1));
            int month = Integer.parseInt(m.group(2));
            int day   = Integer.parseInt(m.group(3));
            Calendar cal = Calendar.getInstance();
            cal.set(year, month, day, 0, 0, 0);
            return cal.getTime();
        } else if ((m = Util.matcher(value, "^(\\d\\d\\d\\d)-(\\d\\d)-(\\d\\d)(?:[Tt]|[ \t]+)(\\d\\d?):(\\d\\d):(\\d\\d)(\\.\\d*)?(?:Z|[ \t]*([-+]\\d\\d?)(?::(\\d\\d))?)?$")).find()) {
            int year    = Integer.parseInt(m.group(1));
            int month   = Integer.parseInt(m.group(2));
            int day     = Integer.parseInt(m.group(3));
            int hour    = Integer.parseInt(m.group(4));
            int min     = Integer.parseInt(m.group(5));
            int sec     = Integer.parseInt(m.group(6));

            String timezone = "GMT" + m.group(8) + ":" + m.group(9);
            Calendar cal = Calendar.getInstance();
            cal.set(year, month, day, hour, min, sec);
            cal.setTimeZone(TimeZone.getTimeZone(timezone));
            return cal.getTime();
        } else {
            return value;
        }
    }

}