1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.yaml.snakeyaml;
17
18 import java.io.IOException;
19 import java.io.InputStream;
20 import java.io.Reader;
21 import java.io.StringReader;
22 import java.io.StringWriter;
23 import java.io.Writer;
24 import java.util.ArrayList;
25 import java.util.Iterator;
26 import java.util.List;
27 import java.util.regex.Pattern;
28
29 import org.yaml.snakeyaml.DumperOptions.FlowStyle;
30 import org.yaml.snakeyaml.composer.Composer;
31 import org.yaml.snakeyaml.constructor.BaseConstructor;
32 import org.yaml.snakeyaml.constructor.Constructor;
33 import org.yaml.snakeyaml.emitter.Emitable;
34 import org.yaml.snakeyaml.emitter.Emitter;
35 import org.yaml.snakeyaml.error.YAMLException;
36 import org.yaml.snakeyaml.events.Event;
37 import org.yaml.snakeyaml.introspector.BeanAccess;
38 import org.yaml.snakeyaml.nodes.Node;
39 import org.yaml.snakeyaml.nodes.Tag;
40 import org.yaml.snakeyaml.parser.Parser;
41 import org.yaml.snakeyaml.parser.ParserImpl;
42 import org.yaml.snakeyaml.reader.StreamReader;
43 import org.yaml.snakeyaml.reader.UnicodeReader;
44 import org.yaml.snakeyaml.representer.Representer;
45 import org.yaml.snakeyaml.resolver.Resolver;
46 import org.yaml.snakeyaml.serializer.Serializer;
47
48
49
50
51 public class Yaml {
52 protected final Resolver resolver;
53 private String name;
54 protected BaseConstructor constructor;
55 protected Representer representer;
56 protected DumperOptions dumperOptions;
57
58
59
60
61
62 public Yaml() {
63 this(new Constructor(), new Representer(), new DumperOptions(), new Resolver());
64 }
65
66
67
68
69 public Yaml(LoaderOptions loaderOptions) {
70 this(new Constructor(), new Representer(), new DumperOptions(), new Resolver());
71 }
72
73
74
75
76
77
78
79 public Yaml(DumperOptions dumperOptions) {
80 this(new Constructor(), new Representer(), dumperOptions);
81 }
82
83
84
85
86
87
88
89
90 public Yaml(Representer representer) {
91 this(new Constructor(), representer);
92 }
93
94
95
96
97
98
99
100
101 public Yaml(BaseConstructor constructor) {
102 this(constructor, new Representer());
103 }
104
105
106
107
108
109
110
111
112
113
114 public Yaml(BaseConstructor constructor, Representer representer) {
115 this(constructor, representer, new DumperOptions());
116 }
117
118
119
120
121
122
123
124
125
126
127 public Yaml(Representer representer, DumperOptions dumperOptions) {
128 this(new Constructor(), representer, dumperOptions, new Resolver());
129 }
130
131
132
133
134
135
136
137
138
139
140
141
142 public Yaml(BaseConstructor constructor, Representer representer, DumperOptions dumperOptions) {
143 this(constructor, representer, dumperOptions, new Resolver());
144 }
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159 public Yaml(BaseConstructor constructor, Representer representer, DumperOptions dumperOptions,
160 Resolver resolver) {
161 if (!constructor.isExplicitPropertyUtils()) {
162 constructor.setPropertyUtils(representer.getPropertyUtils());
163 } else if (!representer.isExplicitPropertyUtils()) {
164 representer.setPropertyUtils(constructor.getPropertyUtils());
165 }
166 this.constructor = constructor;
167 representer.setDefaultFlowStyle(dumperOptions.getDefaultFlowStyle());
168 representer.setDefaultScalarStyle(dumperOptions.getDefaultScalarStyle());
169 representer.getPropertyUtils().setAllowReadOnlyProperties(
170 dumperOptions.isAllowReadOnlyProperties());
171 representer.setTimeZone(dumperOptions.getTimeZone());
172 this.representer = representer;
173 this.dumperOptions = dumperOptions;
174 this.resolver = resolver;
175 this.name = "Yaml:" + System.identityHashCode(this);
176 }
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194 public Yaml(BaseConstructor constructor, LoaderOptions loaderOptions, Representer representer,
195 DumperOptions dumperOptions, Resolver resolver) {
196 this(constructor, representer, dumperOptions, resolver);
197 }
198
199
200
201
202
203
204
205
206 public String dump(Object data) {
207 List<Object> list = new ArrayList<Object>(1);
208 list.add(data);
209 return dumpAll(list.iterator());
210 }
211
212
213
214
215
216
217
218
219
220
221 public Node represent(Object data) {
222 return representer.represent(data);
223 }
224
225
226
227
228
229
230
231
232 public String dumpAll(Iterator<? extends Object> data) {
233 StringWriter buffer = new StringWriter();
234 dumpAll(data, buffer);
235 return buffer.toString();
236 }
237
238
239
240
241
242
243
244
245
246 public void dump(Object data, Writer output) {
247 List<Object> list = new ArrayList<Object>(1);
248 list.add(data);
249 dumpAll(list.iterator(), output);
250 }
251
252
253
254
255
256
257
258
259
260 @SuppressWarnings("deprecation")
261 public void dumpAll(Iterator<? extends Object> data, Writer output) {
262 dumpAll(data, output, dumperOptions.getExplicitRoot());
263 }
264
265 private void dumpAll(Iterator<? extends Object> data, Writer output, Tag rootTag) {
266 Serializer serializer = new Serializer(new Emitter(output, dumperOptions), resolver,
267 dumperOptions, rootTag);
268 try {
269 serializer.open();
270 while (data.hasNext()) {
271 Node node = representer.represent(data.next());
272 serializer.serialize(node);
273 }
274 serializer.close();
275 } catch (java.io.IOException e) {
276 throw new YAMLException(e);
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 public String dumpAs(Object data, Tag rootTag, FlowStyle flowStyle) {
321 FlowStyle oldStyle = representer.getDefaultFlowStyle();
322 if (flowStyle != null) {
323 representer.setDefaultFlowStyle(flowStyle);
324 }
325 List<Object> list = new ArrayList<Object>(1);
326 list.add(data);
327 StringWriter buffer = new StringWriter();
328 dumpAll(list.iterator(), buffer, rootTag);
329 representer.setDefaultFlowStyle(oldStyle);
330 return buffer.toString();
331 }
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352 public String dumpAsMap(Object data) {
353 return dumpAs(data, Tag.MAP, FlowStyle.BLOCK);
354 }
355
356
357
358
359
360
361
362
363
364 public List<Event> serialize(Node data) {
365 SilentEmitter emitter = new SilentEmitter();
366 @SuppressWarnings("deprecation")
367 Serializer serializer = new Serializer(emitter, resolver, dumperOptions,
368 dumperOptions.getExplicitRoot());
369 try {
370 serializer.open();
371 serializer.serialize(data);
372 serializer.close();
373 } catch (java.io.IOException e) {
374 throw new YAMLException(e);
375 }
376 return emitter.getEvents();
377 }
378
379 private static class SilentEmitter implements Emitable {
380 private List<Event> events = new ArrayList<Event>(100);
381
382 public List<Event> getEvents() {
383 return events;
384 }
385
386 public void emit(Event event) throws IOException {
387 events.add(event);
388 }
389 }
390
391
392
393
394
395
396
397
398
399 public Object load(String yaml) {
400 return loadFromReader(new StreamReader(yaml), Object.class);
401 }
402
403
404
405
406
407
408
409
410
411 public Object load(InputStream io) {
412 return loadFromReader(new StreamReader(new UnicodeReader(io)), Object.class);
413 }
414
415
416
417
418
419
420
421
422
423 public Object load(Reader io) {
424 return loadFromReader(new StreamReader(io), Object.class);
425 }
426
427
428
429
430
431
432
433
434
435
436
437
438
439 @SuppressWarnings("unchecked")
440 public <T> T loadAs(Reader io, Class<T> type) {
441 return (T) loadFromReader(new StreamReader(io), type);
442 }
443
444
445
446
447
448
449
450
451
452
453
454
455
456 @SuppressWarnings("unchecked")
457 public <T> T loadAs(String yaml, Class<T> type) {
458 return (T) loadFromReader(new StreamReader(yaml), type);
459 }
460
461
462
463
464
465
466
467
468
469
470
471
472
473 @SuppressWarnings("unchecked")
474 public <T> T loadAs(InputStream input, Class<T> type) {
475 return (T) loadFromReader(new StreamReader(new UnicodeReader(input)), type);
476 }
477
478 private Object loadFromReader(StreamReader sreader, Class<?> type) {
479 Composer composer = new Composer(new ParserImpl(sreader), resolver);
480 constructor.setComposer(composer);
481 return constructor.getSingleData(type);
482 }
483
484
485
486
487
488
489
490
491
492
493 public Iterable<Object> loadAll(Reader yaml) {
494 Composer composer = new Composer(new ParserImpl(new StreamReader(yaml)), resolver);
495 constructor.setComposer(composer);
496 Iterator<Object> result = new Iterator<Object>() {
497 public boolean hasNext() {
498 return constructor.checkData();
499 }
500
501 public Object next() {
502 return constructor.getData();
503 }
504
505 public void remove() {
506 throw new UnsupportedOperationException();
507 }
508 };
509 return new YamlIterable(result);
510 }
511
512 private static class YamlIterable implements Iterable<Object> {
513 private Iterator<Object> iterator;
514
515 public YamlIterable(Iterator<Object> iterator) {
516 this.iterator = iterator;
517 }
518
519 public Iterator<Object> iterator() {
520 return iterator;
521 }
522 }
523
524
525
526
527
528
529
530
531
532
533
534 public Iterable<Object> loadAll(String yaml) {
535 return loadAll(new StringReader(yaml));
536 }
537
538
539
540
541
542
543
544
545
546
547 public Iterable<Object> loadAll(InputStream yaml) {
548 return loadAll(new UnicodeReader(yaml));
549 }
550
551
552
553
554
555
556
557
558
559
560
561 public Node compose(Reader yaml) {
562 Composer composer = new Composer(new ParserImpl(new StreamReader(yaml)), resolver);
563 constructor.setComposer(composer);
564 return composer.getSingleNode();
565 }
566
567
568
569
570
571
572
573
574
575
576 public Iterable<Node> composeAll(Reader yaml) {
577 final Composer composer = new Composer(new ParserImpl(new StreamReader(yaml)), resolver);
578 constructor.setComposer(composer);
579 Iterator<Node> result = new Iterator<Node>() {
580 public boolean hasNext() {
581 return composer.checkNode();
582 }
583
584 public Node next() {
585 return composer.getNode();
586 }
587
588 public void remove() {
589 throw new UnsupportedOperationException();
590 }
591 };
592 return new NodeIterable(result);
593 }
594
595 private static class NodeIterable implements Iterable<Node> {
596 private Iterator<Node> iterator;
597
598 public NodeIterable(Iterator<Node> iterator) {
599 this.iterator = iterator;
600 }
601
602 public Iterator<Node> iterator() {
603 return iterator;
604 }
605 }
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621 public void addImplicitResolver(String tag, Pattern regexp, String first) {
622 addImplicitResolver(new Tag(tag), regexp, first);
623 }
624
625
626
627
628
629
630
631
632
633
634
635
636
637 public void addImplicitResolver(Tag tag, Pattern regexp, String first) {
638 resolver.addImplicitResolver(tag, regexp, first);
639 }
640
641 @Override
642 public String toString() {
643 return name;
644 }
645
646
647
648
649
650
651
652
653 public String getName() {
654 return name;
655 }
656
657
658
659
660
661
662
663 public void setName(String name) {
664 this.name = name;
665 }
666
667
668
669
670
671
672
673
674
675 public Iterable<Event> parse(Reader yaml) {
676 final Parser parser = new ParserImpl(new StreamReader(yaml));
677 Iterator<Event> result = new Iterator<Event>() {
678 public boolean hasNext() {
679 return parser.peekEvent() != null;
680 }
681
682 public Event next() {
683 return parser.getEvent();
684 }
685
686 public void remove() {
687 throw new UnsupportedOperationException();
688 }
689 };
690 return new EventIterable(result);
691 }
692
693 private static class EventIterable implements Iterable<Event> {
694 private Iterator<Event> iterator;
695
696 public EventIterable(Iterator<Event> iterator) {
697 this.iterator = iterator;
698 }
699
700 public Iterator<Event> iterator() {
701 return iterator;
702 }
703 }
704
705 public void setBeanAccess(BeanAccess beanAccess) {
706 constructor.getPropertyUtils().setBeanAccess(beanAccess);
707 representer.getPropertyUtils().setBeanAccess(beanAccess);
708 }
709
710
711
712
713
714 public Yaml(Loader loader) {
715 this(loader, new Dumper(new DumperOptions()));
716 }
717
718
719
720
721 public Yaml(Loader loader, Dumper dumper) {
722 this(loader, dumper, new Resolver());
723 }
724
725
726
727
728 public Yaml(Loader loader, Dumper dumper, Resolver resolver) {
729 this(loader.constructor, dumper.representer, dumper.options, resolver);
730 }
731
732
733
734
735
736
737
738
739 @SuppressWarnings("deprecation")
740 public Yaml(Dumper dumper) {
741 this(new Constructor(), dumper.representer, dumper.options);
742 }
743 }