View Javadoc

1   /**
2    * Copyright (c) 2008-2011, http://www.snakeyaml.org
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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          // try to read generated presentation to prove that the presentation
51          // is identical to the source
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  }