Home | History | Annotate | Download | only in snakeyaml
      1 /**
      2  * Copyright (c) 2008, 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 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         // try to read generated presentation to prove that the presentation
     50         // is identical to the source
     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 }
     80