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.ByteArrayOutputStream;
20 import java.io.InputStream;
21 import java.io.OutputStreamWriter;
22 import java.io.UnsupportedEncodingException;
23 import java.nio.charset.Charset;
24
25 import org.yaml.snakeyaml.constructor.Constructor;
26
27 public class YamlDocument {
28 public static final String ROOT = "specification/";
29 private String source;
30 private String presentation;
31 private Object nativeData;
32
33 public YamlDocument(String sourceName, boolean check, Constructor constructor) {
34 InputStream input = YamlDocument.class.getClassLoader().getResourceAsStream(
35 ROOT + sourceName);
36 if (constructor == null) {
37 constructor = new Constructor();
38 }
39 Yaml yaml = new Yaml(constructor);
40 nativeData = yaml.load(input);
41 ByteArrayOutputStream output = new ByteArrayOutputStream();
42 Charset charset = Charset.forName("UTF-8");
43 yaml.dump(nativeData, new OutputStreamWriter(output, charset));
44 try {
45 presentation = output.toString(charset.name());
46 source = Util.getLocalResource(ROOT + sourceName);
47 } catch (UnsupportedEncodingException e) {
48 throw new RuntimeException(e);
49 }
50
51
52 Object result = yaml.load(presentation);
53 if (check && !nativeData.equals(result)) {
54 throw new RuntimeException("Generated presentation is not valid: " + presentation);
55 }
56 }
57
58 public YamlDocument(String sourceName, boolean check) {
59 this(sourceName, check, null);
60 }
61
62 public YamlDocument(String sourceName) {
63 this(sourceName, true);
64 }
65
66 public String getSource() {
67 return source;
68 }
69
70 public String getPresentation() {
71 return presentation;
72 }
73
74 public Object getNativeData() {
75 if (nativeData == null) {
76 throw new NullPointerException("No object is parsed.");
77 }
78 return nativeData;
79 }
80 }