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