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

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

Introduction

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

Prototype

String[] getPropertyNames(Object object);

Source Link

Document

Returns a list of dynamic property names for the supplied object.

Usage

From source file:org.apache.cocoon.el.impl.objectmodel.ObjectModelImpl.java

public void fillContext() {
    // Hack: I use jxpath to populate the context object's properties
    // in the jexl context
    Object contextObject = get(CONTEXTBEAN);
    if (contextObject == null) {
        //nothing to do
        return;//from  w ww.  ja v  a  2s .c o m
    }

    // FIXME Exception Handling
    final JXPathBeanInfo bi = JXPathIntrospector.getBeanInfo(contextObject.getClass());
    if (bi.isDynamic()) {
        Class cl = bi.getDynamicPropertyHandlerClass();
        try {
            DynamicPropertyHandler h = (DynamicPropertyHandler) cl.newInstance();
            String[] result = h.getPropertyNames(contextObject);
            int len = result.length;
            for (int i = 0; i < len; i++) {
                try {
                    put(result[i], h.getProperty(contextObject, result[i]));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    } else {
        PropertyDescriptor[] props = bi.getPropertyDescriptors();
        int len = props.length;
        for (int i = 0; i < len; i++) {
            try {
                Method read = props[i].getReadMethod();
                if (read != null) {
                    put(props[i].getName(), read.invoke(contextObject, null));
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

From source file:org.apache.cocoon.environment.TemplateObjectModelHelper.java

public static void fillContext(Object contextObject, Map map) {
    // Hack: I use jxpath to populate the context object's properties
    // in the jexl context
    final JXPathBeanInfo bi = JXPathIntrospector.getBeanInfo(contextObject.getClass());
    if (bi.isDynamic()) {
        Class cl = bi.getDynamicPropertyHandlerClass();
        try {//w  ww .ja  v  a 2s  .  c om
            DynamicPropertyHandler h = (DynamicPropertyHandler) cl.newInstance();
            String[] result = h.getPropertyNames(contextObject);
            int len = result.length;
            for (int i = 0; i < len; i++) {
                try {
                    map.put(result[i], h.getProperty(contextObject, result[i]));
                } catch (Exception exc) {
                    exc.printStackTrace();
                }
            }
        } catch (Exception ignored) {
            ignored.printStackTrace();
        }
    } else {
        PropertyDescriptor[] props = bi.getPropertyDescriptors();
        int len = props.length;
        for (int i = 0; i < len; i++) {
            try {
                Method read = props[i].getReadMethod();
                if (read != null) {
                    map.put(props[i].getName(), read.invoke(contextObject, null));
                }
            } catch (Exception ignored) {
                ignored.printStackTrace();
            }
        }
    }
}

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

private void fillContext(Object contextObject, Map map) {
    if (contextObject != null) {
        // Hack: I use jxpath to populate the context object's properties
        // in the jexl context
        final JXPathBeanInfo bi = JXPathIntrospector.getBeanInfo(contextObject.getClass());
        if (bi.isDynamic()) {
            Class cl = bi.getDynamicPropertyHandlerClass();
            try {
                DynamicPropertyHandler h = (DynamicPropertyHandler) cl.newInstance();
                String[] result = h.getPropertyNames(contextObject);
                int len = result.length;
                for (int i = 0; i < len; i++) {
                    try {
                        map.put(result[i], h.getProperty(contextObject, result[i]));
                    } catch (Exception exc) {
                        exc.printStackTrace();
                    }/*from   w  w  w .  j  a  va 2s  .  c o  m*/
                }
            } catch (Exception ignored) {
                ignored.printStackTrace();
            }
        } else {
            PropertyDescriptor[] props = bi.getPropertyDescriptors();
            int len = props.length;
            for (int i = 0; i < len; i++) {
                try {
                    Method read = props[i].getReadMethod();
                    if (read != null) {
                        map.put(props[i].getName(), read.invoke(contextObject, null));
                    }
                } catch (Exception ignored) {
                    ignored.printStackTrace();
                }
            }
        }
    }
}

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

/**
 * @see org.apache.cocoon.sitemap.SitemapModelComponent#setup(SourceResolver, Map, String, Parameters)
 *//*  ww  w  . j a  v  a2 s. com*/
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;
}