Example usage for org.apache.commons.jxpath DynamicPropertyHandler setProperty

List of usage examples for org.apache.commons.jxpath DynamicPropertyHandler setProperty

Introduction

In this page you can find the example usage for org.apache.commons.jxpath DynamicPropertyHandler setProperty.

Prototype

void setProperty(Object object, String propertyName, Object value);

Source Link

Document

Modifies the value of the specified dynamic property.

Usage

From source file:org.apache.cocoon.generation.VelocityGenerator.java

/**
 * @see org.apache.cocoon.sitemap.SitemapModelComponent#setup(SourceResolver, Map, String, Parameters)
 */// w w  w.  ja v  a2 s .c o  m
public void setup(SourceResolver resolver, Map objectModel, String src, Parameters params)
        throws ProcessingException, SAXException, IOException {
    if (activeFlag) {
        throw new IllegalStateException(
                "setup called on recyclable sitemap component before properly recycling previous state");
    }

    super.setup(resolver, objectModel, src, params);

    // Pass along the SourceResolver to the Velocity resource loader
    this.resolverContext.put(CONTEXT_RESOLVER_KEY, resolver);
    this.resolverContext.put(CONTEXT_SOURCE_CACHE_KEY, new HashMap());

    // FIXME: Initialize the Velocity context. Use objectModel to pass these
    final Object bean = FlowHelper.getContextObject(objectModel);
    if (bean != null) {

        final WebContinuation kont = FlowHelper.getWebContinuation(objectModel);

        // Hack? I use JXPath to determine the properties of the bean object
        final JXPathBeanInfo bi = JXPathIntrospector.getBeanInfo(bean.getClass());
        DynamicPropertyHandler h = null;
        final PropertyDescriptor[] props;
        if (bi.isDynamic()) {
            Class cl = bi.getDynamicPropertyHandlerClass();
            try {
                h = (DynamicPropertyHandler) cl.newInstance();
            } catch (Exception exc) {
                exc.printStackTrace();
                h = null;
            }
            props = null;
        } else {
            h = null;
            props = bi.getPropertyDescriptors();
        }
        final DynamicPropertyHandler handler = h;

        this.velocityContext = new Context() {
            public Object put(String key, Object value) {
                if (key.equals("flowContext") || key.equals("continuation")) {
                    return value;
                }
                if (handler != null) {
                    handler.setProperty(bean, key, value);
                    return value;
                } else {
                    for (int i = 0; i < props.length; i++) {
                        if (props[i].getName().equals(key)) {
                            try {
                                return props[i].getWriteMethod().invoke(bean, new Object[] { value });
                            } catch (Exception ignored) {
                                break;
                            }
                        }
                    }
                    return value;
                }
            }

            public boolean containsKey(Object key) {
                if (key.equals("flowContext") || key.equals("continuation")) {
                    return true;
                }
                if (handler != null) {
                    String[] result = handler.getPropertyNames(bean);
                    for (int i = 0; i < result.length; i++) {
                        if (key.equals(result[i])) {
                            return true;
                        }
                    }
                } else {
                    for (int i = 0; i < props.length; i++) {
                        if (key.equals(props[i].getName())) {
                            return true;
                        }
                    }
                }
                return false;
            }

            public Object[] getKeys() {
                Object[] result = null;
                if (handler != null) {
                    result = handler.getPropertyNames(bean);
                } else {
                    result = new Object[props.length];
                    for (int i = 0; i < props.length; i++) {
                        result[i] = props[i].getName();
                    }
                }
                Set set = new HashSet();
                for (int i = 0; i < result.length; i++) {
                    set.add(result[i]);
                }
                set.add("flowContext");
                set.add("continuation");
                result = new Object[set.size()];
                set.toArray(result);
                return result;
            }

            public Object get(String key) {
                if (key.equals("flowContext")) {
                    return bean;
                }
                if (key.equals("continuation")) {
                    return kont;
                }
                if (handler != null) {
                    return handler.getProperty(bean, key);
                } else {
                    for (int i = 0; i < props.length; i++) {
                        if (props[i].getName().equals(key)) {
                            try {
                                return props[i].getReadMethod().invoke(bean, null);
                            } catch (Exception ignored) {
                                break;
                            }
                        }
                    }
                    return null;
                }
            }

            public Object remove(Object key) {
                // not implemented
                return key;
            }
        };
    }
    this.velocityContext = new ChainedContext(this.velocityContext, ObjectModelHelper.getRequest(objectModel),
            ObjectModelHelper.getResponse(objectModel), ObjectModelHelper.getContext(objectModel), params);
    this.velocityContext.put("template", src);
    this.activeFlag = true;
}