View Javadoc

1   /**
2    * Copyright (c) 2008-2012, 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.BufferedInputStream;
19  import java.io.IOException;
20  import java.io.InputStream;
21  
22  public class Util {
23  
24      public static String getLocalResource(String theName) {
25          try {
26              InputStream input;
27              input = YamlDocument.class.getClassLoader().getResourceAsStream(theName);
28              if (input == null) {
29                  throw new RuntimeException("Can not find " + theName);
30              }
31              BufferedInputStream is = new BufferedInputStream(input);
32              StringBuilder buf = new StringBuilder(3000);
33              int i;
34              try {
35                  while ((i = is.read()) != -1) {
36                      buf.append((char) i);
37                  }
38              } finally {
39                  is.close();
40              }
41              String resource = buf.toString();
42              // convert EOLs
43              String[] lines = resource.split("\\r?\\n");
44              StringBuilder buffer = new StringBuilder();
45              for (int j = 0; j < lines.length; j++) {
46                  buffer.append(lines[j]);
47                  buffer.append("\n");
48              }
49              return buffer.toString();
50          } catch (IOException e) {
51              throw new RuntimeException(e);
52          }
53      }
54  }