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