001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.environment;
018:
019: import java.beans.PropertyDescriptor;
020: import java.lang.reflect.Method;
021: import java.util.Collection;
022: import java.util.HashMap;
023: import java.util.Iterator;
024: import java.util.Map;
025: import java.util.Set;
026:
027: import org.apache.avalon.framework.parameters.ParameterException;
028: import org.apache.avalon.framework.parameters.Parameters;
029: import org.apache.cocoon.components.flow.FlowHelper;
030: import org.apache.commons.jxpath.DynamicPropertyHandler;
031: import org.apache.commons.jxpath.JXPathBeanInfo;
032: import org.apache.commons.jxpath.JXPathIntrospector;
033:
034: /**
035: * This is an utility class to create an object model which is similar to the one
036: * used in flow, that can be used from every component.
037: *
038: * Work-in-progress, derived from JXTemplateGenerator
039: *
040: * @version $Id: TemplateObjectModelHelper.java 359757 2005-12-29 08:53:30Z cziegeler $
041: */
042: public class TemplateObjectModelHelper {
043:
044: /** Avoid instantiation */
045: private TemplateObjectModelHelper() {
046: }
047:
048: public static void fillContext(Object contextObject, Map map) {
049: // Hack: I use jxpath to populate the context object's properties
050: // in the jexl context
051: final JXPathBeanInfo bi = JXPathIntrospector
052: .getBeanInfo(contextObject.getClass());
053: if (bi.isDynamic()) {
054: Class cl = bi.getDynamicPropertyHandlerClass();
055: try {
056: DynamicPropertyHandler h = (DynamicPropertyHandler) cl
057: .newInstance();
058: String[] result = h.getPropertyNames(contextObject);
059: int len = result.length;
060: for (int i = 0; i < len; i++) {
061: try {
062: map.put(result[i], h.getProperty(contextObject,
063: result[i]));
064: } catch (Exception exc) {
065: exc.printStackTrace();
066: }
067: }
068: } catch (Exception ignored) {
069: ignored.printStackTrace();
070: }
071: } else {
072: PropertyDescriptor[] props = bi.getPropertyDescriptors();
073: int len = props.length;
074: for (int i = 0; i < len; i++) {
075: try {
076: Method read = props[i].getReadMethod();
077: if (read != null) {
078: map.put(props[i].getName(), read.invoke(
079: contextObject, null));
080: }
081: } catch (Exception ignored) {
082: ignored.printStackTrace();
083: }
084: }
085: }
086: }
087:
088: /**
089: * Create the object model.
090: * Currently the object model is a map with one single entry:
091: * cocoon + request The Request Object
092: * + session The Session (if available)
093: * + context The Context
094: * + continuation The Continuation (if available)
095: * + parameters The parameters (if provided)
096: */
097: public static Map getTemplateObjectModel(final Map objectModel,
098: final Parameters parameters) {
099:
100: // first create the "cocoon object":
101: final Map cocoon = new HashMap();
102:
103: // cocoon.request
104: final Request request = ObjectModelHelper
105: .getRequest(objectModel);
106: cocoon.put("request", request);
107:
108: // cocoon.session
109: final Session session = request.getSession(false);
110: if (session != null) {
111: cocoon.put("session", session);
112: }
113:
114: // cocoon.context
115: final org.apache.cocoon.environment.Context context = ObjectModelHelper
116: .getContext(objectModel);
117: cocoon.put("context", context);
118:
119: // cocoon.continuation
120: final Object cont = FlowHelper.getWebContinuation(objectModel);
121: if (cont != null) {
122: cocoon.put("continuation", cont);
123: }
124:
125: // cocoon.parameters
126: if (parameters != null) {
127: cocoon.put("parameters", new ParametersMap(parameters));
128: }
129:
130: final Map map = new HashMap();
131: map.put("cocoon", cocoon);
132:
133: // Now add objects from flow context (if any)
134: final Object contextObject = FlowHelper
135: .getContextObject(objectModel);
136: if (contextObject instanceof Map) {
137: map.putAll((Map) contextObject);
138: } else if (contextObject != null) {
139: fillContext(contextObject, map);
140: }
141:
142: return map;
143: }
144:
145: protected static final class ParametersMap extends Parameters
146: implements Map {
147:
148: protected final Parameters wrappedParameters;
149: protected Map map;
150:
151: public ParametersMap(Parameters wrapped) {
152: wrappedParameters = wrapped;
153: }
154:
155: public boolean equals(Object arg0) {
156: return wrappedParameters.equals(arg0);
157: }
158:
159: public String[] getNames() {
160: return wrappedParameters.getNames();
161: }
162:
163: public String getParameter(String arg0, String arg1) {
164: return wrappedParameters.getParameter(arg0, arg1);
165: }
166:
167: public String getParameter(String arg0)
168: throws ParameterException {
169: return wrappedParameters.getParameter(arg0);
170: }
171:
172: public boolean getParameterAsBoolean(String arg0, boolean arg1) {
173: return wrappedParameters.getParameterAsBoolean(arg0, arg1);
174: }
175:
176: public boolean getParameterAsBoolean(String arg0)
177: throws ParameterException {
178: return wrappedParameters.getParameterAsBoolean(arg0);
179: }
180:
181: public float getParameterAsFloat(String arg0, float arg1) {
182: return wrappedParameters.getParameterAsFloat(arg0, arg1);
183: }
184:
185: public float getParameterAsFloat(String arg0)
186: throws ParameterException {
187: return wrappedParameters.getParameterAsFloat(arg0);
188: }
189:
190: public int getParameterAsInteger(String arg0, int arg1) {
191: return wrappedParameters.getParameterAsInteger(arg0, arg1);
192: }
193:
194: public int getParameterAsInteger(String arg0)
195: throws ParameterException {
196: return wrappedParameters.getParameterAsInteger(arg0);
197: }
198:
199: public long getParameterAsLong(String arg0, long arg1) {
200: return wrappedParameters.getParameterAsLong(arg0, arg1);
201: }
202:
203: public long getParameterAsLong(String arg0)
204: throws ParameterException {
205: return wrappedParameters.getParameterAsLong(arg0);
206: }
207:
208: public Iterator getParameterNames() {
209: return wrappedParameters.getParameterNames();
210: }
211:
212: public int hashCode() {
213: return wrappedParameters.hashCode();
214: }
215:
216: public boolean isParameter(String arg0) {
217: return wrappedParameters.isParameter(arg0);
218: }
219:
220: public void makeReadOnly() {
221: wrappedParameters.makeReadOnly();
222: }
223:
224: public Parameters merge(Parameters arg0) {
225: return wrappedParameters.merge(arg0);
226: }
227:
228: public void removeParameter(String arg0) {
229: wrappedParameters.removeParameter(arg0);
230: }
231:
232: public String setParameter(String arg0, String arg1)
233: throws IllegalStateException {
234: return wrappedParameters.setParameter(arg0, arg1);
235: }
236:
237: public void clear() {
238: this .checkWriteable();
239: }
240:
241: protected Map getMap() {
242: if (this .map == null) {
243: this .map = new HashMap();
244: String[] names = this .getNames();
245: for (int i = 0; i < names.length; i++) {
246: map
247: .put(names[i], this .getParameter(names[i],
248: null));
249: }
250: }
251: return this .map;
252: }
253:
254: public boolean containsKey(Object arg0) {
255: if (arg0 == null) {
256: return false;
257: }
258: return this .getParameter(arg0.toString(), null) != null;
259: }
260:
261: public boolean containsValue(Object arg0) {
262: return this .getMap().containsValue(arg0);
263: }
264:
265: public Set entrySet() {
266: return this .getMap().entrySet();
267: }
268:
269: public Object get(Object arg0) {
270: if (arg0 == null) {
271: return null;
272: }
273: return this .getParameter(arg0.toString(), null);
274: }
275:
276: public boolean isEmpty() {
277: return this .getNames().length == 0;
278: }
279:
280: public Set keySet() {
281: return this .getMap().keySet();
282: }
283:
284: public Object put(Object arg0, Object arg1) {
285: this .checkWriteable();
286: return null;
287: }
288:
289: public void putAll(Map arg0) {
290: this .checkWriteable();
291: }
292:
293: public Object remove(Object arg0) {
294: this .checkWriteable();
295: return null;
296: }
297:
298: public int size() {
299: return this .getNames().length;
300: }
301:
302: public Collection values() {
303: return this .getMap().values();
304: }
305:
306: /**
307: * @see org.apache.avalon.framework.parameters.Parameters#toString()
308: */
309: public String toString() {
310: return this.wrappedParameters.toString();
311: }
312: }
313: }
|