1 // Copyright 2010 Google Inc. All Rights Reserved.
3 package com
.google
.apphosting
.utils
.config
;
6 import java
.io
.FileReader
;
7 import java
.io
.FileNotFoundException
;
9 import java
.io
.StringReader
;
10 import java
.util
.List
;
11 import net
.sourceforge
.yamlbeans
.YamlException
;
12 import net
.sourceforge
.yamlbeans
.YamlReader
;
15 * Class to parse dos.yaml into a DosXml object.
18 public class DosYamlReader
{
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() {
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
);
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
;
55 public String
getFilename() {
56 return appDir
+ DosYamlReader
.FILENAME
;
59 public DosXml
parse() {
60 if (new File(getFilename()).exists()) {
62 return parse(new FileReader(getFilename()));
63 } catch (FileNotFoundException ex
) {
64 throw new AppEngineConfigException("Cannot find file " + getFilename(), ex
);
70 public static DosXml
parse(Reader yaml
) {
71 YamlReader reader
= new YamlReader(yaml
);
72 reader
.getConfig().setPropertyElementType(DosYaml
.class,
74 DosXml
.BlacklistEntry
.class);
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
));