App Engine 1.8.4.
[gae.git] / java / src / main / com / google / apphosting / utils / config / DosYamlReader.java
blob20effdb56838ca4b4f0d6aa4e04f91beea68b3a0
1 // Copyright 2010 Google Inc. All Rights Reserved.
3 package com.google.apphosting.utils.config;
5 import java.io.File;
6 import java.io.FileReader;
7 import java.io.FileNotFoundException;
8 import java.io.Reader;
9 import java.io.StringReader;
10 import java.util.List;
11 import net.sourceforge.yamlbeans.YamlException;
12 import net.sourceforge.yamlbeans.YamlReader;
14 /**
15 * Class to parse dos.yaml into a DosXml object.
18 public class DosYamlReader {
20 /**
21 * Wrapper around DosXml to make the JavaBeans properties match the YAML file syntax.
23 public static class DosYaml {
24 private List<DosXml.BlacklistEntry> entries;
26 public List<DosXml.BlacklistEntry> getBlacklist() {
27 return entries;
30 public void setBlacklist(List<DosXml.BlacklistEntry> entries) {
31 this.entries = entries;
34 public DosXml toXml() {
35 DosXml xml = new DosXml();
36 if (entries != null) {
37 for (DosXml.BlacklistEntry entry : entries) {
38 xml.addBlacklistEntry(entry);
41 return xml;
45 private static final String FILENAME = "dos.yaml";
46 private String appDir;
48 public DosYamlReader(String appDir) {
49 if (appDir.length() > 0 && appDir.charAt(appDir.length() - 1) != File.separatorChar) {
50 appDir += File.separatorChar;
52 this.appDir = appDir;
55 public String getFilename() {
56 return appDir + DosYamlReader.FILENAME;
59 public DosXml parse() {
60 if (new File(getFilename()).exists()) {
61 try {
62 return parse(new FileReader(getFilename()));
63 } catch (FileNotFoundException ex) {
64 throw new AppEngineConfigException("Cannot find file " + getFilename(), ex);
67 return null;
70 public static DosXml parse(Reader yaml) {
71 YamlReader reader = new YamlReader(yaml);
72 reader.getConfig().setPropertyElementType(DosYaml.class,
73 "blacklist",
74 DosXml.BlacklistEntry.class);
75 try {
76 DosYaml dosYaml = reader.read(DosYaml.class);
77 if (dosYaml == null) {
78 throw new AppEngineConfigException("Empty dos configuration.");
80 return dosYaml.toXml();
81 } catch (YamlException ex) {
82 throw new AppEngineConfigException(ex.getMessage(), ex);
86 public static DosXml parse(String yaml) {
87 return parse(new StringReader(yaml));