01: /* ***** BEGIN LICENSE BLOCK *****
02: * Version: MPL 1.1
03: * The contents of this file are subject to the Mozilla Public License Version
04: * 1.1 (the "License"); you may not use this file except in compliance with
05: * the License. You may obtain a copy of the License at
06: * http://www.mozilla.org/MPL/
07: *
08: * Software distributed under the License is distributed on an "AS IS" basis,
09: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10: * for the specific language governing rights and limitations under the
11: * License.
12: *
13: * The Original Code is Riot.
14: *
15: * The Initial Developer of the Original Code is
16: * Neteye GmbH.
17: * Portions created by the Initial Developer are Copyright (C) 2007
18: * the Initial Developer. All Rights Reserved.
19: *
20: * Contributor(s):
21: * alf
22: *
23: * ***** END LICENSE BLOCK ***** */
24: package org.riotfamily.common.web.view.freemarker;
25:
26: import org.riotfamily.common.beans.PropertyUtils;
27: import org.riotfamily.common.web.view.DynamicModel;
28:
29: import freemarker.template.TemplateHashModel;
30: import freemarker.template.TemplateModel;
31: import freemarker.template.TemplateModelException;
32:
33: /**
34: * @author alf Werder <alf dor werder at artundweise dot de>
35: * @since 6.5
36: */
37: public class DynamicModelWrapper implements ObjectWrapperPlugin {
38:
39: public boolean supports(Object obj) {
40: return obj instanceof DynamicModel;
41: }
42:
43: public TemplateModel wrapSupportedObject(Object obj,
44: PluginObjectWrapper wrapper) throws TemplateModelException {
45:
46: return new TemplateDynamicModel(wrapper, (DynamicModel) obj);
47: }
48:
49: private class TemplateDynamicModel implements TemplateHashModel {
50: private PluginObjectWrapper wrapper;
51: private DynamicModel model;
52:
53: public TemplateDynamicModel(PluginObjectWrapper wrapper,
54: DynamicModel model) {
55:
56: this .wrapper = wrapper;
57: this .model = model;
58: }
59:
60: public TemplateModel get(String key)
61: throws TemplateModelException {
62: if (PropertyUtils.findReadMethod(model.getClass(), key) != null) {
63:
64: return wrapper.wrap(PropertyUtils.getProperty(model,
65: key));
66: } else {
67: return wrapper.wrap(model.get(key));
68: }
69: }
70:
71: public boolean isEmpty() throws TemplateModelException {
72: return false;
73: }
74: }
75: }
|