001 /*
002 * Copyright 2010-2013 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017 package org.codehaus.griffon.runtime.core.resources;
018
019 import griffon.core.resources.NoSuchResourceException;
020 import griffon.core.resources.ResourceResolver;
021 import griffon.util.CallableWithArgs;
022 import groovy.lang.Closure;
023
024 import java.text.MessageFormat;
025 import java.util.List;
026 import java.util.Locale;
027 import java.util.Map;
028 import java.util.MissingResourceException;
029
030 /**
031 * @author Andres Almiray
032 * @since 1.1.0
033 */
034 public abstract class AbstractResourceResolver implements ResourceResolver {
035 protected static final Object[] EMPTY_OBJECT_ARGS = new Object[0];
036
037 public Object resolveResource(String key) throws NoSuchResourceException {
038 return resolveResource(key, EMPTY_OBJECT_ARGS, Locale.getDefault());
039 }
040
041 public Object resolveResource(String key, Locale locale) throws NoSuchResourceException {
042 return resolveResource(key, EMPTY_OBJECT_ARGS, locale);
043 }
044
045 public Object resolveResource(String key, Object[] args) throws NoSuchResourceException {
046 return resolveResource(key, args, Locale.getDefault());
047 }
048
049 public Object resolveResource(String key, List args) throws NoSuchResourceException {
050 return resolveResource(key, toObjectArray(args), Locale.getDefault());
051 }
052
053 public Object resolveResource(String key, List args, Locale locale) throws NoSuchResourceException {
054 return resolveResource(key, toObjectArray(args), locale);
055 }
056
057 public Object resolveResource(String key, Object defaultValue) {
058 return resolveResource(key, EMPTY_OBJECT_ARGS, defaultValue, Locale.getDefault());
059 }
060
061 public Object resolveResource(String key, Object defaultValue, Locale locale) {
062 return resolveResource(key, EMPTY_OBJECT_ARGS, defaultValue, locale);
063 }
064
065 public Object resolveResource(String key, Object[] args, Object defaultValue) {
066 return resolveResource(key, args, defaultValue, Locale.getDefault());
067 }
068
069 public Object resolveResource(String key, Object[] args, Object defaultValue, Locale locale) {
070 try {
071 return resolveResource(key, args, locale);
072 } catch (NoSuchResourceException nsme) {
073 return defaultValue;
074 }
075 }
076
077 public Object resolveResource(String key, Map<String, Object> args) throws NoSuchResourceException {
078 return resolveResource(key, args, Locale.getDefault());
079 }
080
081 public Object resolveResource(String key, Map<String, Object> args, Object defaultValue) {
082 return resolveResource(key, args, defaultValue, Locale.getDefault());
083 }
084
085 public Object resolveResource(String key, Map<String, Object> args, Object defaultValue, Locale locale) {
086 try {
087 return resolveResource(key, args, locale);
088 } catch (NoSuchResourceException nsme) {
089 return defaultValue;
090 }
091 }
092
093 public Object resolveResource(String key, Map<String, Object> args, Locale locale) throws NoSuchResourceException {
094 Object resource = resolveResourceInternal(key, locale);
095 return evalResourceWithArguments(resource, args);
096 }
097
098 public Object resolveResource(String key, List args, Object defaultValue) {
099 return resolveResource(key, toObjectArray(args), defaultValue, Locale.getDefault());
100 }
101
102 public Object resolveResource(String key, List args, Object defaultValue, Locale locale) {
103 return resolveResource(key, toObjectArray(args), defaultValue, locale);
104 }
105
106 public Object resolveResource(String key, Object[] args, Locale locale) throws NoSuchResourceException {
107 if (null == args) args = EMPTY_OBJECT_ARGS;
108 if (null == locale) locale = Locale.getDefault();
109 try {
110 Object resource = resolveResourceInternal(key, locale);
111 return evalResourceWithArguments(resource, args);
112 } catch (MissingResourceException e) {
113 throw new NoSuchResourceException(key, locale);
114 }
115 }
116
117 protected Object evalResourceWithArguments(Object resource, Object[] args) {
118 if (resource instanceof Closure) {
119 Closure closure = (Closure) resource;
120 return closure.call(args);
121 } else if (resource instanceof CallableWithArgs) {
122 CallableWithArgs callable = (CallableWithArgs) resource;
123 return callable.call(args);
124 } else if (resource instanceof CharSequence) {
125 return formatResource(String.valueOf(resource), args);
126 }
127 return resource;
128 }
129
130 protected Object evalResourceWithArguments(Object resource, Map<String, Object> args) {
131 if (resource instanceof Closure) {
132 Closure closure = (Closure) resource;
133 return closure.call(args);
134 } else if (resource instanceof CallableWithArgs) {
135 CallableWithArgs callable = (CallableWithArgs) resource;
136 return callable.call(new Object[]{args});
137 } else if (resource instanceof CharSequence) {
138 return formatResource(String.valueOf(resource), args);
139 }
140 return resource;
141 }
142
143 protected String formatResource(String resource, Object[] args) {
144 return MessageFormat.format(resource, args);
145 }
146
147 protected String formatResource(String resource, Map<String, Object> args) {
148 for (Map.Entry<String, Object> variable : args.entrySet()) {
149 String var = variable.getKey();
150 String value = variable.getValue() != null ? variable.getValue().toString() : null;
151 if (value != null) resource = resource.replace("{:" + var + "}", value);
152 }
153 return resource;
154 }
155
156 protected abstract Object resolveResourceInternal(String key, Locale locale) throws NoSuchResourceException;
157
158 protected Object[] toObjectArray(List args) {
159 if (null == args || args.isEmpty()) {
160 return EMPTY_OBJECT_ARGS;
161 }
162 return args.toArray(new Object[args.size()]);
163 }
164 }
|