App Engine 1.8.4.
[gae.git] / java / src / main / com / google / apphosting / utils / config / YamlUtils.java
blob35c69905f370b111b50412027d97356c30d261ae
1 // Copyright 2010 Google Inc. All Rights Reserved.
3 package com.google.apphosting.utils.config;
5 import net.sourceforge.yamlbeans.YamlException;
6 import net.sourceforge.yamlbeans.YamlReader;
8 import java.util.HashMap;
9 import java.util.Map;
10 import java.util.Map.Entry;
11 import java.util.regex.Pattern;
13 /**
14 * Helper methods for parsing YAML files.
17 public class YamlUtils {
19 static final Pattern TRUE_PATTERN =
20 Pattern.compile("y|Y|yes|Yes|YES|true|True|TRUE|on|On|ON");
21 static final Pattern FALSE_PATTERN =
22 Pattern.compile("n|N|no|No|NO|false|False|FALSE|off|Off|OFF");
24 private static final String RESERVED_URL =
25 "The URL '%s' is reserved and cannot be used.";
27 private YamlUtils() { }
29 /**
30 * Parse a YAML !!bool type. YamlBeans only supports "true" or "false".
32 * @throws AppEngineConfigException
34 static boolean parseBoolean(String value) {
35 if (TRUE_PATTERN.matcher(value).matches()) {
36 return true;
37 } else if (FALSE_PATTERN.matcher(value).matches()) {
38 return false;
40 throw new AppEngineConfigException("Invalid boolean value '" + value + "'.");
43 /**
44 * Check that a URL is not one of the reserved URLs according to
45 * http://code.google.com/appengine/docs/java/configyaml/appconfig_yaml.html#Reserved_URLs
47 * @throws AppEngineConfigException
49 static void validateUrl(String url) {
50 if (url.equals("/form")) {
51 throw new AppEngineConfigException(String.format(RESERVED_URL, url));
55 /**
56 * Parse the data into YAML.
58 * @param data The text to parse as YAML.
59 * @return The YAML data as a Map.
60 * @throws AppEngineConfigException Throws when the underlying Yaml library
61 * detects bad data of when the data does not parse to a Map.
63 @SuppressWarnings("unchecked")
64 public static Map<String, Object> genericParse(String data)
65 throws AppEngineConfigException {
66 try {
67 YamlReader reader = new YamlReader(data);
68 return reader.read(Map.class);
69 } catch (YamlException exc) {
70 throw new AppEngineConfigException("Invalid YAML data: " + data, exc);
74 /**
75 * A utility class that encapsulates conversion between different
76 * types of objects.
78 public static interface ObjectConverter<T> {
79 T convert(Object obj) throws Exception;
82 /**
83 * Parses the data into YAML and converts the map values.
85 * @param data The text to parse as YAML.
86 * @param converter A converter for updating the values of the Map from
87 * the type returned from the underlying Yaml library to the expected type.
88 * @return The YAML data as a Map.
89 * @throws AppEngineConfigException Throws when the underlying Yaml library
90 * detects bad data or when the data does not parse to a Map. Also all
91 * Exceptions from ObjectConverted are encapsulated in an
92 * AppEngineConfigException.
94 public static <T> Map<String, T> genericParse(String data,
95 ObjectConverter<T> converter) throws AppEngineConfigException {
96 try {
97 Map<String, T> yaml = new HashMap<String, T>();
98 for (Entry<String, Object> entry : genericParse(data).entrySet()) {
99 yaml.put(entry.getKey(), converter.convert(entry.getValue()));
101 return yaml;
102 } catch (Exception exc) {
103 throw new AppEngineConfigException(exc);