aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/yaml/snakeyaml/Yaml.java
blob: 16114cd9ac6d1f07ee6db9ffba9d0ee6cac8cfd0 (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
/**
 * Copyright (c) 2008, SnakeYAML
 *
 * Licensed 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.yaml.snakeyaml;

import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.regex.Pattern;
import org.yaml.snakeyaml.DumperOptions.FlowStyle;
import org.yaml.snakeyaml.composer.Composer;
import org.yaml.snakeyaml.constructor.BaseConstructor;
import org.yaml.snakeyaml.constructor.Constructor;
import org.yaml.snakeyaml.emitter.Emitable;
import org.yaml.snakeyaml.emitter.Emitter;
import org.yaml.snakeyaml.error.YAMLException;
import org.yaml.snakeyaml.events.Event;
import org.yaml.snakeyaml.introspector.BeanAccess;
import org.yaml.snakeyaml.nodes.Node;
import org.yaml.snakeyaml.nodes.Tag;
import org.yaml.snakeyaml.parser.Parser;
import org.yaml.snakeyaml.parser.ParserImpl;
import org.yaml.snakeyaml.reader.StreamReader;
import org.yaml.snakeyaml.reader.UnicodeReader;
import org.yaml.snakeyaml.representer.Representer;
import org.yaml.snakeyaml.resolver.Resolver;
import org.yaml.snakeyaml.serializer.Serializer;

/**
 * Public YAML interface. This class is not thread-safe. Which means that all the methods of the
 * same instance can be called only by one thread. It is better to create an instance for every YAML
 * stream.
 */
public class Yaml {

  protected final Resolver resolver;
  private String name;
  protected BaseConstructor constructor;
  protected Representer representer;
  protected DumperOptions dumperOptions;
  protected LoaderOptions loadingConfig;

  /**
   * Create Yaml instance.
   */
  public Yaml() {
    this(new Constructor(), new Representer(), new DumperOptions(), new LoaderOptions(),
        new Resolver());
  }

  /**
   * Create Yaml instance.
   *
   * @param dumperOptions DumperOptions to configure outgoing objects
   */
  public Yaml(DumperOptions dumperOptions) {
    this(new Constructor(), new Representer(dumperOptions), dumperOptions);
  }

  /**
   * Create Yaml instance.
   *
   * @param loadingConfig LoadingConfig to control load behavior
   */
  public Yaml(LoaderOptions loadingConfig) {
    this(new Constructor(loadingConfig), new Representer(), new DumperOptions(), loadingConfig);
  }

  /**
   * Create Yaml instance.
   *
   * @param representer Representer to emit outgoing objects
   */
  public Yaml(Representer representer) {
    this(new Constructor(), representer);
  }

  /**
   * Create Yaml instance.
   *
   * @param constructor BaseConstructor to construct incoming documents
   */
  public Yaml(BaseConstructor constructor) {
    this(constructor, new Representer());
  }

  /**
   * Create Yaml instance.
   *
   * @param constructor BaseConstructor to construct incoming documents
   * @param representer Representer to emit outgoing objects
   */
  public Yaml(BaseConstructor constructor, Representer representer) {
    this(constructor, representer, initDumperOptions(representer));
  }

  private static DumperOptions initDumperOptions(Representer representer) {
    DumperOptions dumperOptions = new DumperOptions();
    dumperOptions.setDefaultFlowStyle(representer.getDefaultFlowStyle());
    dumperOptions.setDefaultScalarStyle(representer.getDefaultScalarStyle());
    dumperOptions
        .setAllowReadOnlyProperties(representer.getPropertyUtils().isAllowReadOnlyProperties());
    dumperOptions.setTimeZone(representer.getTimeZone());
    return dumperOptions;
  }

  /**
   * Create Yaml instance. It is safe to create a few instances and use them in different Threads.
   *
   * @param representer Representer to emit outgoing objects
   * @param dumperOptions DumperOptions to configure outgoing objects
   */
  public Yaml(Representer representer, DumperOptions dumperOptions) {
    this(new Constructor(), representer, dumperOptions, new LoaderOptions(), new Resolver());
  }

  /**
   * Create Yaml instance. It is safe to create a few instances and use them in different Threads.
   *
   * @param constructor BaseConstructor to construct incoming documents
   * @param representer Representer to emit outgoing objects
   * @param dumperOptions DumperOptions to configure outgoing objects
   */
  public Yaml(BaseConstructor constructor, Representer representer, DumperOptions dumperOptions) {
    this(constructor, representer, dumperOptions, new LoaderOptions(), new Resolver());
  }

  /**
   * Create Yaml instance. It is safe to create a few instances and use them in different Threads.
   *
   * @param constructor BaseConstructor to construct incoming documents
   * @param representer Representer to emit outgoing objects
   * @param dumperOptions DumperOptions to configure outgoing objects
   * @param loadingConfig LoadingConfig to control load behavior
   */
  public Yaml(BaseConstructor constructor, Representer representer, DumperOptions dumperOptions,
      LoaderOptions loadingConfig) {
    this(constructor, representer, dumperOptions, loadingConfig, new Resolver());
  }

  /**
   * Create Yaml instance. It is safe to create a few instances and use them in different Threads.
   *
   * @param constructor BaseConstructor to construct incoming documents
   * @param representer Representer to emit outgoing objects
   * @param dumperOptions DumperOptions to configure outgoing objects
   * @param resolver Resolver to detect implicit type
   */
  public Yaml(BaseConstructor constructor, Representer representer, DumperOptions dumperOptions,
      Resolver resolver) {
    this(constructor, representer, dumperOptions, new LoaderOptions(), resolver);
  }

  /**
   * Create Yaml instance. It is safe to create a few instances and use them in different Threads.
   *
   * @param constructor BaseConstructor to construct incoming documents
   * @param representer Representer to emit outgoing objects
   * @param dumperOptions DumperOptions to configure outgoing objects
   * @param loadingConfig LoadingConfig to control load behavior
   * @param resolver Resolver to detect implicit type
   */
  public Yaml(BaseConstructor constructor, Representer representer, DumperOptions dumperOptions,
      LoaderOptions loadingConfig, Resolver resolver) {
    if (!constructor.isExplicitPropertyUtils()) {
      constructor.setPropertyUtils(representer.getPropertyUtils());
    } else if (!representer.isExplicitPropertyUtils()) {
      representer.setPropertyUtils(constructor.getPropertyUtils());
    }
    this.constructor = constructor;
    this.constructor.setAllowDuplicateKeys(loadingConfig.isAllowDuplicateKeys());
    this.constructor.setWrappedToRootException(loadingConfig.isWrappedToRootException());
    if (!dumperOptions.getIndentWithIndicator()
        && dumperOptions.getIndent() <= dumperOptions.getIndicatorIndent()) {
      throw new YAMLException("Indicator indent must be smaller then indent.");
    }
    representer.setDefaultFlowStyle(dumperOptions.getDefaultFlowStyle());
    representer.setDefaultScalarStyle(dumperOptions.getDefaultScalarStyle());
    representer.getPropertyUtils()
        .setAllowReadOnlyProperties(dumperOptions.isAllowReadOnlyProperties());
    representer.setTimeZone(dumperOptions.getTimeZone());
    this.representer = representer;
    this.dumperOptions = dumperOptions;
    this.loadingConfig = loadingConfig;
    this.resolver = resolver;
    this.name = "Yaml:" + System.identityHashCode(this);
  }

  /**
   * Serialize a Java object into a YAML String.
   *
   * @param data Java object to be Serialized to YAML
   * @return YAML String
   */
  public String dump(Object data) {
    List<Object> list = new ArrayList<Object>(1);
    list.add(data);
    return dumpAll(list.iterator());
  }

  /**
   * Produce the corresponding representation tree for a given Object.
   *
   * @param data instance to build the representation tree for
   * @return representation tree
   * @see <a href="http://yaml.org/spec/1.1/#id859333">Figure 3.1. Processing Overview</a>
   */
  public Node represent(Object data) {
    return representer.represent(data);
  }

  /**
   * Serialize a sequence of Java objects into a YAML String.
   *
   * @param data Iterator with Objects
   * @return YAML String with all the objects in proper sequence
   */
  public String dumpAll(Iterator<? extends Object> data) {
    StringWriter buffer = new StringWriter();
    dumpAll(data, buffer, null);
    return buffer.toString();
  }

  /**
   * Serialize a Java object into a YAML stream.
   *
   * @param data Java object to be serialized to YAML
   * @param output stream to write to
   */
  public void dump(Object data, Writer output) {
    List<Object> list = new ArrayList<Object>(1);
    list.add(data);
    dumpAll(list.iterator(), output, null);
  }

  /**
   * Serialize a sequence of Java objects into a YAML stream.
   *
   * @param data Iterator with Objects
   * @param output stream to write to
   */
  public void dumpAll(Iterator<? extends Object> data, Writer output) {
    dumpAll(data, output, null);
  }

  private void dumpAll(Iterator<? extends Object> data, Writer output, Tag rootTag) {
    Serializer serializer =
        new Serializer(new Emitter(output, dumperOptions), resolver, dumperOptions, rootTag);
    try {
      serializer.open();
      while (data.hasNext()) {
        Node node = representer.represent(data.next());
        serializer.serialize(node);
      }
      serializer.close();
    } catch (IOException e) {
      throw new YAMLException(e);
    }
  }

  /**
   * <p>
   * Serialize a Java object into a YAML string. Override the default root tag with
   * <code>rootTag</code>.
   * </p>
   *
   * <p>
   * This method is similar to <code>Yaml.dump(data)</code> except that the root tag for the whole
   * document is replaced with the given tag. This has two main uses.
   * </p>
   *
   * <p>
   * First, if the root tag is replaced with a standard YAML tag, such as <code>Tag.MAP</code>, then
   * the object will be dumped as a map. The root tag will appear as <code>!!map</code>, or blank
   * (implicit !!map).
   * </p>
   *
   * <p>
   * Second, if the root tag is replaced by a different custom tag, then the document appears to be
   * a different type when loaded. For example, if an instance of MyClass is dumped with the tag
   * !!YourClass, then it will be handled as an instance of YourClass when loaded.
   * </p>
   *
   * @param data Java object to be serialized to YAML
   * @param rootTag the tag for the whole YAML document. The tag should be Tag.MAP for a JavaBean to
   *        make the tag disappear (to use implicit tag !!map). If <code>null</code> is provided
   *        then the standard tag with the full class name is used.
   * @param flowStyle flow style for the whole document. See Chapter 10. Collection Styles
   *        http://yaml.org/spec/1.1/#id930798. If <code>null</code> is provided then the flow style
   *        from DumperOptions is used.
   * @return YAML String
   */
  public String dumpAs(Object data, Tag rootTag, FlowStyle flowStyle) {
    FlowStyle oldStyle = representer.getDefaultFlowStyle();
    if (flowStyle != null) {
      representer.setDefaultFlowStyle(flowStyle);
    }
    List<Object> list = new ArrayList<Object>(1);
    list.add(data);
    StringWriter buffer = new StringWriter();
    dumpAll(list.iterator(), buffer, rootTag);
    representer.setDefaultFlowStyle(oldStyle);
    return buffer.toString();
  }

  /**
   * <p>
   * Serialize a Java object into a YAML string. Override the default root tag with
   * <code>Tag.MAP</code>.
   * </p>
   * <p>
   * This method is similar to <code>Yaml.dump(data)</code> except that the root tag for the whole
   * document is replaced with <code>Tag.MAP</code> tag (implicit !!map).
   * </p>
   * <p>
   * Block Mapping is used as the collection style. See 10.2.2. Block Mappings
   * (http://yaml.org/spec/1.1/#id934537)
   * </p>
   *
   * @param data Java object to be serialized to YAML
   * @return YAML String
   */
  public String dumpAsMap(Object data) {
    return dumpAs(data, Tag.MAP, FlowStyle.BLOCK);
  }

  /**
   * Serialize (dump) a YAML node into a YAML stream.
   *
   * @param node YAML node to be serialized to YAML
   * @param output stream to write to
   */
  public void serialize(Node node, Writer output) {
    Serializer serializer =
        new Serializer(new Emitter(output, dumperOptions), resolver, dumperOptions, null);
    try {
      serializer.open();
      serializer.serialize(node);
      serializer.close();
    } catch (IOException e) {
      throw new YAMLException(e);
    }
  }

  /**
   * Serialize the representation tree into Events.
   *
   * @param data representation tree
   * @return Event list
   * @see <a href="http://yaml.org/spec/1.1/#id859333">Processing Overview</a>
   */
  public List<Event> serialize(Node data) {
    SilentEmitter emitter = new SilentEmitter();
    Serializer serializer = new Serializer(emitter, resolver, dumperOptions, null);
    try {
      serializer.open();
      serializer.serialize(data);
      serializer.close();
    } catch (IOException e) {
      throw new YAMLException(e);
    }
    return emitter.getEvents();
  }

  private static class SilentEmitter implements Emitable {

    private final List<Event> events = new ArrayList<Event>(100);

    public List<Event> getEvents() {
      return events;
    }

    @Override
    public void emit(Event event) throws IOException {
      events.add(event);
    }
  }

  /**
   * Parse the only YAML document in a String and produce the corresponding Java object. (Because
   * the encoding in known BOM is not respected.)
   *
   * @param yaml YAML data to load from (BOM must not be present)
   * @param <T> the class of the instance to be created
   * @return parsed object
   */
  @SuppressWarnings("unchecked")
  public <T> T load(String yaml) {
    return (T) loadFromReader(new StreamReader(yaml), Object.class);
  }

  /**
   * Parse the only YAML document in a stream and produce the corresponding Java object.
   *
   * @param io data to load from (BOM is respected to detect encoding and removed from the data)
   * @param <T> the class of the instance to be created
   * @return parsed object
   */
  @SuppressWarnings("unchecked")
  public <T> T load(InputStream io) {
    return (T) loadFromReader(new StreamReader(new UnicodeReader(io)), Object.class);
  }

  /**
   * Parse the only YAML document in a stream and produce the corresponding Java object.
   *
   * @param io data to load from (BOM must not be present)
   * @param <T> the class of the instance to be created
   * @return parsed object
   */
  @SuppressWarnings("unchecked")
  public <T> T load(Reader io) {
    return (T) loadFromReader(new StreamReader(io), Object.class);
  }

  /**
   * Parse the only YAML document in a stream and produce the corresponding Java object.
   *
   * @param <T> Class is defined by the second argument
   * @param io data to load from (BOM must not be present)
   * @param type Class of the object to be created
   * @return parsed object
   */
  @SuppressWarnings("unchecked")
  public <T> T loadAs(Reader io, Class<T> type) {
    return (T) loadFromReader(new StreamReader(io), type);
  }

  /**
   * Parse the only YAML document in a String and produce the corresponding Java object. (Because
   * the encoding in known BOM is not respected.)
   *
   * @param <T> Class is defined by the second argument
   * @param yaml YAML data to load from (BOM must not be present)
   * @param type Class of the object to be created
   * @return parsed object
   */
  @SuppressWarnings("unchecked")
  public <T> T loadAs(String yaml, Class<T> type) {
    return (T) loadFromReader(new StreamReader(yaml), type);
  }

  /**
   * Parse the only YAML document in a stream and produce the corresponding Java object.
   *
   * @param <T> Class is defined by the second argument
   * @param input data to load from (BOM is respected to detect encoding and removed from the data)
   * @param type Class of the object to be created
   * @return parsed object
   */
  @SuppressWarnings("unchecked")
  public <T> T loadAs(InputStream input, Class<T> type) {
    return (T) loadFromReader(new StreamReader(new UnicodeReader(input)), type);
  }

  private Object loadFromReader(StreamReader sreader, Class<?> type) {
    Composer composer =
        new Composer(new ParserImpl(sreader, loadingConfig), resolver, loadingConfig);
    constructor.setComposer(composer);
    return constructor.getSingleData(type);
  }

  /**
   * Parse all YAML documents in the Reader and produce corresponding Java objects. The documents
   * are parsed only when the iterator is invoked.
   *
   * @param yaml YAML data to load from (BOM must not be present)
   * @return an Iterable over the parsed Java objects in this String in proper sequence
   */
  public Iterable<Object> loadAll(Reader yaml) {
    Composer composer =
        new Composer(new ParserImpl(new StreamReader(yaml), loadingConfig.isProcessComments()),
            resolver, loadingConfig);
    constructor.setComposer(composer);
    Iterator<Object> result = new Iterator<Object>() {
      @Override
      public boolean hasNext() {
        return constructor.checkData();
      }

      @Override
      public Object next() {
        return constructor.getData();
      }

      @Override
      public void remove() {
        throw new UnsupportedOperationException();
      }
    };
    return new YamlIterable(result);
  }

  private static class YamlIterable implements Iterable<Object> {

    private final Iterator<Object> iterator;

    public YamlIterable(Iterator<Object> iterator) {
      this.iterator = iterator;
    }

    @Override
    public Iterator<Object> iterator() {
      return iterator;
    }
  }

  /**
   * Parse all YAML documents in a String and produce corresponding Java objects. (Because the
   * encoding in known BOM is not respected.) The documents are parsed only when the iterator is
   * invoked.
   *
   * @param yaml YAML data to load from (BOM must not be present)
   * @return an Iterable over the parsed Java objects in this String in proper sequence
   */
  public Iterable<Object> loadAll(String yaml) {
    return loadAll(new StringReader(yaml));
  }

  /**
   * Parse all YAML documents in a stream and produce corresponding Java objects. The documents are
   * parsed only when the iterator is invoked.
   *
   * @param yaml YAML data to load from (BOM is respected to detect encoding and removed from the
   *        data)
   * @return an Iterable over the parsed Java objects in this stream in proper sequence
   */
  public Iterable<Object> loadAll(InputStream yaml) {
    return loadAll(new UnicodeReader(yaml));
  }

  /**
   * Parse the first YAML document in a stream and produce the corresponding representation tree.
   * (This is the opposite of the represent() method)
   *
   * @param yaml YAML document
   * @return parsed root Node for the specified YAML document
   * @see <a href="http://yaml.org/spec/1.1/#id859333">Figure 3.1. Processing Overview</a>
   */
  public Node compose(Reader yaml) {
    Composer composer =
        new Composer(new ParserImpl(new StreamReader(yaml), loadingConfig.isProcessComments()),
            resolver, loadingConfig);
    return composer.getSingleNode();
  }

  /**
   * Parse all YAML documents in a stream and produce corresponding representation trees.
   *
   * @param yaml stream of YAML documents
   * @return parsed root Nodes for all the specified YAML documents
   * @see <a href="http://yaml.org/spec/1.1/#id859333">Processing Overview</a>
   */
  public Iterable<Node> composeAll(Reader yaml) {
    final Composer composer =
        new Composer(new ParserImpl(new StreamReader(yaml), loadingConfig.isProcessComments()),
            resolver, loadingConfig);
    Iterator<Node> result = new Iterator<Node>() {
      @Override
      public boolean hasNext() {
        return composer.checkNode();
      }

      @Override
      public Node next() {
        Node node = composer.getNode();
        if (node != null) {
          return node;
        } else {
          throw new NoSuchElementException("No Node is available.");
        }
      }

      @Override
      public void remove() {
        throw new UnsupportedOperationException();
      }
    };
    return new NodeIterable(result);
  }

  private static class NodeIterable implements Iterable<Node> {

    private final Iterator<Node> iterator;

    public NodeIterable(Iterator<Node> iterator) {
      this.iterator = iterator;
    }

    @Override
    public Iterator<Node> iterator() {
      return iterator;
    }
  }

  /**
   * Add an implicit scalar detector. If an implicit scalar value matches the given regexp, the
   * corresponding tag is assigned to the scalar.
   *
   * @param tag tag to assign to the node
   * @param regexp regular expression to match against
   * @param first a sequence of possible initial characters or null (which means any).
   */
  public void addImplicitResolver(Tag tag, Pattern regexp, String first) {
    resolver.addImplicitResolver(tag, regexp, first);
  }

  /**
   * Add an implicit scalar detector. If an implicit scalar value matches the given regexp, the
   * corresponding tag is assigned to the scalar.
   *
   * @param tag tag to assign to the node
   * @param regexp regular expression to match against
   * @param first a sequence of possible initial characters or null (which means any).
   * @param limit the max length of the value which may match the regular expression
   */
  public void addImplicitResolver(Tag tag, Pattern regexp, String first, int limit) {
    resolver.addImplicitResolver(tag, regexp, first, limit);
  }

  @Override
  public String toString() {
    return name;
  }

  /**
   * Get a meaningful name. It simplifies debugging in a multi-threaded environment. If nothing is
   * set explicitly the address of the instance is returned.
   *
   * @return human readable name
   */
  public String getName() {
    return name;
  }

  /**
   * Set a meaningful name to be shown in toString()
   *
   * @param name human readable name
   */
  public void setName(String name) {
    this.name = name;
  }

  /**
   * Parse a YAML stream and produce parsing events.
   *
   * @param yaml YAML document(s)
   * @return parsed events
   * @see <a href="http://yaml.org/spec/1.1/#id859333">Processing Overview</a>
   */
  public Iterable<Event> parse(Reader yaml) {
    final Parser parser = new ParserImpl(new StreamReader(yaml), loadingConfig.isProcessComments());
    Iterator<Event> result = new Iterator<Event>() {
      @Override
      public boolean hasNext() {
        return parser.peekEvent() != null;
      }

      @Override
      public Event next() {
        Event event = parser.getEvent();
        if (event != null) {
          return event;
        } else {
          throw new NoSuchElementException("No Event is available.");
        }
      }

      @Override
      public void remove() {
        throw new UnsupportedOperationException();
      }
    };
    return new EventIterable(result);
  }

  private static class EventIterable implements Iterable<Event> {

    private final Iterator<Event> iterator;

    public EventIterable(Iterator<Event> iterator) {
      this.iterator = iterator;
    }

    @Override
    public Iterator<Event> iterator() {
      return iterator;
    }
  }

  public void setBeanAccess(BeanAccess beanAccess) {
    constructor.getPropertyUtils().setBeanAccess(beanAccess);
    representer.getPropertyUtils().setBeanAccess(beanAccess);
  }

  public void addTypeDescription(TypeDescription td) {
    constructor.addTypeDescription(td);
    representer.addTypeDescription(td);
  }
}