Java Code Examples for freemarker.ext.beans.BeanModel#getWrappedObject()

The following examples show how to use freemarker.ext.beans.BeanModel#getWrappedObject() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FreeMarkerWorker.java    From scipio-erp with Apache License 2.0 6 votes vote down vote up
/**
* Gets BeanModel from FreeMarker context and returns the object that it wraps.
* @param varName the name of the variable in the FreeMarker context.
* @param env the FreeMarker Environment
*/
public static <T> T getWrappedObject(String varName, Environment env) {
    Object obj = null;
    try {
        obj = env.getVariable(varName);
        if (obj != null) {
            if (obj == TemplateModel.NOTHING) {
                obj = null;
            } else if (obj instanceof BeanModel) {
                BeanModel bean = (BeanModel) obj;
                obj = bean.getWrappedObject();
            } else if (obj instanceof SimpleScalar) {
                obj = obj.toString();
            }
        }
    } catch (TemplateModelException e) {
        Debug.logInfo(e.getMessage(), module);
    }
    return UtilGenerics.<T>cast(obj);
}
 
Example 2
Source File: HasAspectMethod.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * @see freemarker.template.TemplateMethodModel#exec(java.util.List)
 */
public Object exec(List args) throws TemplateModelException
{
    int result = 0;
    
    if (args.size() == 2)
    {
        // arg 0 must be a wrapped TemplateNode object
        BeanModel arg0 = (BeanModel)args.get(0);
        
        // arg 1 can be either wrapped QName object or a String 
        String arg1String = null;
        Object arg1 = args.get(1);
        if (arg1 instanceof BeanModel)
        {
            arg1String = ((BeanModel)arg1).getWrappedObject().toString();
        }
        else if (arg1 instanceof TemplateScalarModel)
        {
            arg1String = ((TemplateScalarModel)arg1).getAsString();
        }
        if (arg0.getWrappedObject() instanceof TemplateNode)
        {
            // test to see if this node has the aspect
            if ( ((TemplateNode)arg0.getWrappedObject()).hasAspect(arg1String) )
            {
                result = 1;
            }
        }
    }
    
    return Integer.valueOf(result);
}
 
Example 3
Source File: HasPermissionMethod.java    From alfresco-repository with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * @see freemarker.template.TemplateMethodModel#exec(java.util.List)
 */
public Object exec(List args) throws TemplateModelException
{
    int result = 0;
    
    if (args.size() == 2)
    {
        // arg 0 must be a wrapped TemplateNode object
        BeanModel arg0 = (BeanModel)args.get(0);
        
        // arg 1 must be a String permission name
        String permission;
        Object arg1 = args.get(1);
        if (arg1 instanceof TemplateScalarModel)
        {
            permission = ((TemplateScalarModel)arg1).getAsString();
            
            if (arg0.getWrappedObject() instanceof TemplateNode)
            {
                // test to see if this node has the permission
                if ( ((TemplateNode)arg0.getWrappedObject()).hasPermission(permission) )
                {
                    result = 1;
                }
            }
        }
    }
    
    return Integer.valueOf(result);
}
 
Example 4
Source File: SetRequestAttributeMethod.java    From scipio-erp with Apache License 2.0 5 votes vote down vote up
public Object exec(@SuppressWarnings("rawtypes") List args) throws TemplateModelException {
    if (args == null || args.size() != 2)
        throw new TemplateModelException("Invalid number of arguements");
    if (!(args.get(0) instanceof TemplateScalarModel))
        throw new TemplateModelException("First argument not an instance of TemplateScalarModel");
    // SCIPIO: This is too limiting...
    //if (!(args.get(1) instanceof BeanModel) && !(args.get(1) instanceof TemplateNumberModel) && !(args.get(1) instanceof TemplateScalarModel))
    //    throw new TemplateModelException("Second argument not an instance of BeanModel nor TemplateNumberModel nor TemplateScalarModel");

    Environment env = FreeMarkerWorker.getCurrentEnvironment();
    BeanModel req = (BeanModel)env.getVariable("request");
    HttpServletRequest request = (HttpServletRequest) req.getWrappedObject();

    // SCIPIO: name should not be escaped
    //String name = ((TemplateScalarModel) args.get(0)).getAsString();
    String name = LangFtlUtil.getAsStringNonEscaping(((TemplateScalarModel) args.get(0)));
    Object valueModel = args.get(1);
    Object value = null;
    // SCIPIO: Let DeepUnwrap handle this...
    //if (args.get(1) instanceof TemplateScalarModel)
    //    value = ((TemplateScalarModel) args.get(1)).getAsString();
    //if (args.get(1) instanceof TemplateNumberModel)
    //    value = ((TemplateNumberModel) args.get(1)).getAsNumber();
    //if (args.get(1) instanceof BeanModel)
    //    value = ((BeanModel) args.get(1)).getWrappedObject();
    // SCIPIO: NOTE: Unlike this above, this call will avoid the auto-escaping as implemented by Ofbiz (sensitive to DeepUnwrap implementation)
    value = LangFtlUtil.unwrapAlwaysUnlessNull(valueModel);

    request.setAttribute(name, value);
    return new SimpleScalar("");
}
 
Example 5
Source File: SetContextFieldTransform.java    From scipio-erp with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public Object exec(@SuppressWarnings("rawtypes") List args) throws TemplateModelException {
    if (args == null || args.size() != 2)
        throw new TemplateModelException("Invalid number of arguements");
    if (!(args.get(0) instanceof TemplateScalarModel))
        throw new TemplateModelException("First argument not an instance of TemplateScalarModel");
    // SCIPIO: This is too limiting...
    //if (!(args.get(1) instanceof BeanModel) && !(args.get(1) instanceof TemplateNumberModel) && !(args.get(1) instanceof TemplateScalarModel))
    //    throw new TemplateModelException("Second argument not an instance of BeanModel nor TemplateNumberModel nor TemplateScalarModel");

    Environment env = FreeMarkerWorker.getCurrentEnvironment();
    BeanModel req = (BeanModel)env.getVariable("context");
    Map<String, Object> context = (Map<String, Object>) req.getWrappedObject();

    // SCIPIO: name should not be escaped
    //String name = ((TemplateScalarModel) args.get(0)).getAsString();
    String name = LangFtlUtil.getAsStringNonEscaping(((TemplateScalarModel) args.get(0)));
    Object valueModel = args.get(1);
    Object value = null;
    // SCIPIO: Let DeepUnwrap handle this...
    //if (args.get(1) instanceof TemplateNumberModel)
    //    value = ((TemplateNumberModel) args.get(1)).getAsNumber();
    //if (args.get(1) instanceof BeanModel)
    //    value = ((BeanModel) args.get(1)).getWrappedObject();
    // SCIPIO: NOTE: Unlike this above, this call will avoid the auto-escaping as implemented by Ofbiz (sensitive to DeepUnwrap implementation)
    value = LangFtlUtil.unwrapAlwaysUnlessNull(valueModel);

    context.put(name, value);
    return new SimpleScalar("");
}
 
Example 6
Source File: SetGlobalContextFieldMethod.java    From scipio-erp with Apache License 2.0 5 votes vote down vote up
@Override
public Object exec(@SuppressWarnings("rawtypes") List args) throws TemplateModelException {
    if (args == null || args.size() != 2)
        throw new TemplateModelException("Invalid number of arguments");
    if (!(args.get(0) instanceof TemplateScalarModel))
        throw new TemplateModelException("First argument not an instance of TemplateScalarModel");
    // SCIPIO: This is too limiting
    //if (!(args.get(1) instanceof BeanModel) && !(args.get(1) instanceof TemplateNumberModel) && !(args.get(1) instanceof TemplateScalarModel))
    //    throw new TemplateModelException("Second argument not an instance of BeanModel nor TemplateNumberModel nor TemplateScalarModel");

    Environment env = FreeMarkerWorker.getCurrentEnvironment();
    BeanModel globalContextModel = (BeanModel) env.getVariable("globalContext");
    @SuppressWarnings("unchecked")
    Map<String, Object> globalContext = (Map<String, Object>) globalContextModel.getWrappedObject();

    String name = LangFtlUtil.getAsStringNonEscaping(((TemplateScalarModel) args.get(0)));
    Object valueModel = args.get(1);
    Object value = null;
    // SCIPIO: Let DeepUnwrap handle this...
    //if (args.get(1) instanceof TemplateScalarModel)
    //    value = ((TemplateScalarModel) args.get(1)).getAsString();
    //if (args.get(1) instanceof TemplateNumberModel)
    //    value = ((TemplateNumberModel) args.get(1)).getAsNumber();
    //if (args.get(1) instanceof BeanModel)
    //    value = ((BeanModel) args.get(1)).getWrappedObject();
    // SCIPIO: NOTE: Unlike this above, this call will avoid the auto-escaping as implemented by Ofbiz (sensitive to DeepUnwrap implementation)
    value = LangFtlUtil.unwrapAlwaysUnlessNull(valueModel);

    globalContext.put(name, value);
    return new SimpleScalar("");
}
 
Example 7
Source File: SortFilesByPathMethod.java    From windup with Eclipse Public License 1.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private Iterable<Object> getIterable(Object arg) throws TemplateModelException
{
    if (arg instanceof BeanModel)
    {
        BeanModel beanModel = (BeanModel) arg;
        return (Iterable<Object>) beanModel.getWrappedObject();
    }
    else if (arg instanceof SimpleSequence)
    {
        SimpleSequence simpleSequence = (SimpleSequence) arg;
        return (Iterable<Object>) simpleSequence.toList();
    }
    else if (arg instanceof DefaultIterableAdapter)
    {
        DefaultIterableAdapter adapter = (DefaultIterableAdapter) arg;
        return (Iterable<Object>) adapter.getAdaptedObject(Iterable.class);
    }
    else if (arg instanceof DefaultListAdapter)
    {
        DefaultListAdapter defaultListAdapter = (DefaultListAdapter) arg;
        return (Iterable<Object>) defaultListAdapter.getWrappedObject();
    }
    else
    {
        throw new WindupException("Unrecognized type passed to: " + getMethodName() + ": "
                    + arg.getClass().getCanonicalName());
    }
}