Java Code Examples for freemarker.template.TemplateModel#getClass()

The following examples show how to use freemarker.template.TemplateModel#getClass() . 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: TransformUtil.java    From scipio-erp with Apache License 2.0 6 votes vote down vote up
/**
 * Gets boolean arg.
 * <p>
 * Will automatically interpret string true/false as boolean.
 */
public static Boolean getBooleanArg(TemplateModel obj, Boolean defaultValue) throws TemplateModelException {
    if (obj instanceof TemplateBooleanModel) {
        return ((TemplateBooleanModel) obj).getAsBoolean();
    }
    else if (obj instanceof TemplateScalarModel) {
        TemplateScalarModel s = (TemplateScalarModel) obj;
        String val = s.getAsString();
        // SCIPIO: empty check is desirable and makes it so caller can request default by specifying ""
        if (!val.isEmpty()) {
            return "true".equalsIgnoreCase(s.getAsString());
        }
    } else if (obj != null) {
        throw new TemplateModelException("Expected boolean model or string model representation of boolean, but got a " +
                obj.getClass() + " instead");
    }
    return defaultValue;
}
 
Example 2
Source File: TransformUtil.java    From scipio-erp with Apache License 2.0 6 votes vote down vote up
/**
 * Gets string arg.
 * <p>
 * If number or date passed, will be coerced to string. Other types such as maps or lists
 * will throw TemplateModelException.
 */
public static String getStringArg(TemplateModel obj, String defaultValue, boolean useDefaultWhenEmpty, boolean nonEscaping) throws TemplateModelException {
    String result = null;
    if (obj instanceof TemplateScalarModel) {
        TemplateScalarModel s = (TemplateScalarModel) obj;
        result = LangFtlUtil.getAsString(s, nonEscaping);
    } else if (obj == null) {
        return defaultValue;
    } else if (obj instanceof TemplateNumberModel || obj instanceof TemplateDateModel) {
        // TODO: optimize this call
        result = LangFtlUtil.execStringBuiltIn(obj, FreeMarkerWorker.getCurrentEnvironment()).getAsString();
    } else {
        throw new TemplateModelException("Expected string model or something coercible to string, but got a " +
                obj.getClass() + " instead");
    }
    if (useDefaultWhenEmpty && result.isEmpty()) {
        return defaultValue;
    }
    return result;
}
 
Example 3
Source File: TransformUtil.java    From scipio-erp with Apache License 2.0 6 votes vote down vote up
/**
 * Gets a deep-unwrapped map.
 * FIXME: nonEscaping bool is currently not handled... it may bypass escaping in some cases but not others...
 */
public static <K,V> Map<K, V> getMapArg(TemplateModel obj, Map<K, V> defaultValue, boolean useDefaultWhenEmpty, boolean nonEscaping) throws TemplateModelException {
    if (!nonEscaping) {
        throw new UnsupportedOperationException("getMapArg currently only supports escaping-bypassing (nonEscaping true)");
    }
    Map<K, V> result = null;
    if (obj instanceof TemplateHashModel) {
        result = UtilGenerics.checkMap(LangFtlUtil.unwrapAlways(obj));
    } else if (obj == null) {
        return defaultValue;
    } else {
        throw new TemplateModelException("Expected hash/map model or something coercible to map, but got a " +
                obj.getClass() + " instead");
    }
    if (useDefaultWhenEmpty && result.isEmpty()) {
        return defaultValue;
    }
    return result;
}
 
Example 4
Source File: TemplateFtlUtil.java    From scipio-erp with Apache License 2.0 6 votes vote down vote up
public static String makeParamString(TemplateModel paramsModel, String delim, boolean nonEscaping) throws TemplateModelException, IllegalArgumentException {
    String paramStr;
    if (paramsModel == null) {
        paramStr = null;
    } else if (OfbizFtlObjectType.STRING.isObjectType(paramsModel)) {
        paramStr = TransformUtil.getStringArg(paramsModel, nonEscaping);
    } else if (OfbizFtlObjectType.MAP.isObjectType(paramsModel)) {
        // SPECIAL: we're forced to duplicate the params map for the case where
        // rawParams=false, to trigger the html auto-escaping
        TemplateHashModelEx paramsHashModel = (TemplateHashModelEx) paramsModel;
        paramStr = TemplateFtlUtil.makeParamString(paramsHashModel, delim, nonEscaping);
    } else {
        throw new IllegalArgumentException("url params argument: Expected string or map, but instead got: "
                + paramsModel.getClass());
    }
    return paramStr;
}
 
Example 5
Source File: TransformUtil.java    From scipio-erp with Apache License 2.0 5 votes vote down vote up
/**
 * Gets integer arg.
 * <p>
 * If string passed, will be parsed as integer. Other types such as maps or lists
 * will throw TemplateModelException.
 */
public static Integer getIntegerArg(TemplateModel obj, Integer defaultValue) throws TemplateModelException, NumberFormatException {
    if (obj instanceof TemplateNumberModel) {
        return ((TemplateNumberModel) obj).getAsNumber().intValue();
    } else if (obj instanceof TemplateScalarModel) {
        String strResult = LangFtlUtil.getAsString((TemplateScalarModel) obj, true);
        return strResult.isEmpty() ? defaultValue : Integer.parseInt(strResult);
    } else if (obj == null) {
        return defaultValue;
    }
    throw new TemplateModelException("Expected integer model or string representing of integer, but got a " +
            obj.getClass() + " instead");
}
 
Example 6
Source File: TransformUtil.java    From scipio-erp with Apache License 2.0 5 votes vote down vote up
/**
 * Gets integer arg.
 * <p>
 * If string passed, will be parsed as integer. Other types such as maps or lists
 * will throw TemplateModelException.
 */
public static Double getDoubleArg(TemplateModel obj, Double defaultValue) throws TemplateModelException, NumberFormatException {
    if (obj instanceof TemplateNumberModel) {
        return ((TemplateNumberModel) obj).getAsNumber().doubleValue();
    } else if (obj instanceof TemplateScalarModel) {
        String strResult = LangFtlUtil.getAsString((TemplateScalarModel) obj, true);
        return strResult.isEmpty() ? defaultValue :  Double.parseDouble(strResult);
    } else if (obj == null) {
        return defaultValue;
    }
    throw new TemplateModelException("Expected integer model or string representing of integer, but got a " +
            obj.getClass() + " instead");
}
 
Example 7
Source File: TransformUtil.java    From scipio-erp with Apache License 2.0 5 votes vote down vote up
/**
 * Gets BigDecimal arg.
 * <p>
 * If string or number passed, will be parsed as BigDecimal. Other types such as maps or lists
 * will throw TemplateModelException.
 */
public static BigDecimal getBigDecimalArg(TemplateModel obj, BigDecimal defaultValue) throws TemplateModelException, NumberFormatException {
    if (obj instanceof TemplateNumberModel) {
        Number number = ((TemplateNumberModel) obj).getAsNumber();
        return UtilNumber.toBigDecimal(number);
    } else if (obj instanceof TemplateScalarModel) {
        String strResult = LangFtlUtil.getAsString((TemplateScalarModel) obj, true);
        return strResult.isEmpty() ? defaultValue : UtilNumber.toBigDecimal(strResult);
    } else if (obj == null) {
        return defaultValue;
    }
    throw new TemplateModelException("Expected BigDecimal model or string representing of BigDecimal, but got a " +
            obj.getClass() + " instead");
}
 
Example 8
Source File: MultiVarMethod.java    From scipio-erp with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
static Collection<String> getAsRawSequence(TemplateModel elems) throws TemplateModelException {
    if (elems == null || elems instanceof TemplateBooleanModel) {
        return Collections.emptyList();
    } else if (elems instanceof TemplateCollectionModel || elems instanceof TemplateSequenceModel) {
        return (Collection<String>) LangFtlUtil.unwrapAlways(elems);
    } else if (elems instanceof TemplateHashModelEx) {
        return (Collection<String>) LangFtlUtil.getMapKeys(elems);
    } else {
        throw new TemplateModelException("invalid parameter type, can't interpret as sequence: " + elems.getClass());
    }
}
 
Example 9
Source File: LangFtlUtil.java    From scipio-erp with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings({ "unchecked", "unused" })
@Deprecated
private static TemplateSequenceModel toSimpleSequence(TemplateModel object, ObjectWrapper objectWrapper) throws TemplateModelException {
    if (object instanceof TemplateSequenceModel) {
        return (TemplateSequenceModel) object;
    }
    else if (object instanceof WrapperTemplateModel) {
        WrapperTemplateModel wrapperModel = (WrapperTemplateModel) object;
        // WARN: bypasses auto-escaping
        Object wrappedObject = wrapperModel.getWrappedObject();
        if (wrappedObject instanceof List) {
            return DefaultListAdapter.adapt((List<Object>) wrappedObject, (RichObjectWrapper) objectWrapper);
        }
        else if (wrappedObject instanceof Object[]) {
            return DefaultArrayAdapter.adapt((Object[]) wrappedObject, (ObjectWrapperAndUnwrapper) objectWrapper);
        }
        else if (wrappedObject instanceof Set) {
            throw new UnsupportedOperationException("Not yet implemented");
        }
        else if (wrappedObject instanceof Collection) {
            throw new UnsupportedOperationException("Not yet implemented");
        }
        else if (wrappedObject instanceof Iterable) {
            throw new UnsupportedOperationException("Not yet implemented");
        }
        else {
            throw new TemplateModelException("Cannot convert bean-wrapped object of type " + (object != null ? object.getClass() : "null") + " to simple sequence");
        }
    } else if (object instanceof TemplateCollectionModel) {
        TemplateCollectionModel collModel = (TemplateCollectionModel) object;
        SimpleSequence res = new SimpleSequence(objectWrapper);
        TemplateModelIterator it = collModel.iterator();
        while(it.hasNext()) {
            res.add(it.next());
        }
        return res;
    } else {
        throw new TemplateModelException("Cannot convert object of type " + (object != null ? object.getClass() : "null") + " to simple sequence");
    }
}
 
Example 10
Source File: LangFtlUtil.java    From scipio-erp with Apache License 2.0 5 votes vote down vote up
/**
 * Adds all the elements in the given collection to a new set.
 * <p>
 * NOTE: This method does NOT handle string escaping explicitly; you may want {@link #toStringSet} instead.
 */
@SuppressWarnings("unchecked")
public static <T> Set<T> toSet(TemplateModel object) throws TemplateModelException {
    if (object instanceof WrapperTemplateModel && ((WrapperTemplateModel) object).getWrappedObject() instanceof Set) {
        return (Set<T>) ((WrapperTemplateModel) object).getWrappedObject();
    } else if (object instanceof TemplateCollectionModel) {
        return toSet((TemplateCollectionModel) object);
    } else if (object instanceof TemplateSequenceModel) {
        return toSet((TemplateSequenceModel) object);
    } else {
        throw new TemplateModelException("Cannot convert object of type " + (object != null ? object.getClass() : "null") + " to set");
    }
}
 
Example 11
Source File: LangFtlUtil.java    From scipio-erp with Apache License 2.0 5 votes vote down vote up
public static void addToSimpleList(SimpleSequence dest, TemplateModel source) throws TemplateModelException {
    if (source instanceof TemplateCollectionModel) {
        addToSimpleList(dest, (TemplateCollectionModel) source);
    }
    else if (source instanceof TemplateSequenceModel) {
        addToSimpleList(dest, (TemplateSequenceModel) source);
    }
    else {
        throw new TemplateModelException("Can't add to simple list from source type (non-list type): " + source.getClass());
    }
}