01 /*
02 * Copyright 2010-2013 the original author or authors.
03 *
04 * Licensed under the Apache License, Version 2.0 (the "License");
05 * you may not use this file except in compliance with the License.
06 * You may obtain a copy of the License at
07 *
08 * http://www.apache.org/licenses/LICENSE-2.0
09 *
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
17 package org.codehaus.griffon.runtime.util;
18
19 import griffon.util.ConfigReader;
20 import groovy.lang.Script;
21 import groovy.util.ConfigObject;
22
23 import java.net.URL;
24 import java.util.*;
25
26 import static griffon.util.ConfigUtils.getConfigValue;
27 import static griffon.util.ConfigUtils.createConfigReader;
28
29 /**
30 * @author Andres Almiray
31 * @since 1.1.0
32 */
33 public class GroovyScriptResourceBundle extends ResourceBundle {
34 private final ConfigObject config;
35 private final List<String> keys = new ArrayList<String>();
36
37 public GroovyScriptResourceBundle(URL location) {
38 this(null, location);
39 }
40
41 public GroovyScriptResourceBundle(Script script) {
42 this(null, script);
43 }
44
45 public GroovyScriptResourceBundle(String script) {
46 this(null, script);
47 }
48
49 public GroovyScriptResourceBundle(Class scriptClass) {
50 this(null, scriptClass);
51 }
52
53 public GroovyScriptResourceBundle(ConfigReader reader, URL location) {
54 this(resolveConfigReader(reader).parse(location));
55 }
56
57
58 public GroovyScriptResourceBundle(ConfigReader reader, Script script) {
59 this(resolveConfigReader(reader).parse(script));
60 }
61
62 public GroovyScriptResourceBundle(ConfigReader reader, String script) {
63 this(resolveConfigReader(reader).parse(script));
64 }
65
66 public GroovyScriptResourceBundle(ConfigReader reader, Class scriptClass) {
67 this(resolveConfigReader(reader).parse(scriptClass));
68 }
69
70 private static ConfigReader resolveConfigReader(ConfigReader reader) {
71 return null != reader ? reader : createConfigReader();
72 }
73
74 private GroovyScriptResourceBundle(ConfigObject config) {
75 this.config = config;
76 keys.addAll(this.config.flatten(new LinkedHashMap()).keySet());
77 }
78
79 protected Object handleGetObject(String key) {
80 Object value = getConfigValue(config, key, null);
81 return null == value ? null : String.valueOf(value);
82 }
83
84 public Enumeration<String> getKeys() {
85 final Iterator<String> keysIterator = keys.iterator();
86 return new Enumeration<String>() {
87 public boolean hasMoreElements() {
88 return keysIterator.hasNext();
89 }
90
91 public String nextElement() {
92 return keysIterator.next();
93 }
94 };
95 }
96 }
|