cziegeler 2004/04/02 00:06:46
Modified: src/blocks/scratchpad/java/org/apache/cocoon/environment
TemplateObjectModelHelper.java
Log:
Make attributes available as properties
Revision Changes Path
1.2 +60 -3
cocoon-2.1/src/blocks/scratchpad/java/org/apache/cocoon/environment/TemplateObjectModelHelper.java
Index: TemplateObjectModelHelper.java
===================================================================
RCS file:
/home/cvs/cocoon-2.1/src/blocks/scratchpad/java/org/apache/cocoon/environment/TemplateObjectModelHelper.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- TemplateObjectModelHelper.java 1 Apr 2004 11:54:52 -0000 1.1
+++ TemplateObjectModelHelper.java 2 Apr 2004 08:06:46 -0000 1.2
@@ -22,6 +22,8 @@
import org.apache.avalon.framework.parameters.Parameters;
import org.apache.cocoon.components.flow.FlowHelper;
+import org.apache.commons.beanutils.MethodUtils;
+import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.jxpath.DynamicPropertyHandler;
import org.apache.commons.jxpath.JXPathBeanInfo;
import org.apache.commons.jxpath.JXPathIntrospector;
@@ -96,12 +98,12 @@
// cocoon.request
final Request request = ObjectModelHelper.getRequest( objectModel );
- cocoon.put("request", request);
+ cocoon.put("request", new DynamicMap(request));
// cocoon.session
final Session session = request.getSession(false);
if (session != null) {
- cocoon.put("session", session);
+ cocoon.put("session", new DynamicMap(session));
}
// cocoon.context
@@ -132,4 +134,59 @@
return map;
}
+ /**
+ * This is a dynamic map that should provide the same functionality as
the
+ * FOM wrappers for objects.
+ */
+ public static class DynamicMap extends HashMap {
+
+ protected final Object information;
+
+ public DynamicMap(Object info) {
+ this.information = info;
+ }
+
+ /* (non-Javadoc)
+ * @see java.util.Map#get(java.lang.Object)
+ */
+ public Object get(Object key) {
+ Object result = super.get(key);
+ if ( result == null ) {
+ result = this.getDynamicInfo(key);
+ }
+ return result;
+ }
+
+
+ /* (non-Javadoc)
+ * @see java.util.Map#containsKey(java.lang.Object)
+ */
+ public boolean containsKey(Object key) {
+ boolean result = super.containsKey(key);
+ if ( result == false ) {
+ result = (this.getDynamicInfo(key) != null);
+ }
+ return result;
+ }
+
+ protected Object getDynamicInfo(Object key) {
+ Object result = null;
+ try {
+ result = PropertyUtils.getProperty(this.information,
key.toString());
+ } catch (Exception ignore) {
+ }
+ if ( result == null ) {
+ try {
+ result = MethodUtils.invokeMethod(this.information,
+ "getAttribute",
+ key.toString());
+ } catch (Exception ignore) {
+ }
+ }
+ if ( result != null ) {
+ this.put(key, result);
+ }
+ return result;
+ }
+ }
}