Java Code Examples for freemarker.template.TemplateScalarModel#getAsString()

The following examples show how to use freemarker.template.TemplateScalarModel#getAsString() . 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: GetEffortDescriptionForPoints.java    From windup with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public Object exec(List arguments) throws TemplateModelException
{
    ExecutionStatistics.get().begin(NAME);
    if (arguments.size() < 1)
    {
        throw new TemplateModelException("Error, method expects one or two arguments (Integer, [verbosity:String])");
    }
    SimpleNumber simpleNumber = (SimpleNumber) arguments.get(0);
    int effort = simpleNumber.getAsNumber().intValue();

    Verbosity verbosity = Verbosity.SHORT;
    if (arguments.size() > 1)
    {
        final TemplateScalarModel verbosityModel = (TemplateScalarModel) arguments.get(1);
        String verbosityString = verbosityModel.getAsString();
        verbosity = Verbosity.valueOf(verbosityString.toUpperCase());
    }

    String result = EffortReportService.getEffortLevelDescription(verbosity, effort);

    ExecutionStatistics.get().end(NAME);
    return result;
}
 
Example 3
Source File: RewrapObjectMethod.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() < 1 || args.size() > 3 ) {
        throw new TemplateModelException("Invalid number of arguments (expected: 1-3)");
    }
    Environment env = CommonFtlUtil.getCurrentEnvironment();
    TemplateModel object = (TemplateModel) args.get(0);

    String wrapperStr = null;
    String modeStr = null;
    if (args.size() >= 2) {
        TemplateScalarModel wrapperModel = (TemplateScalarModel) args.get(1);
        if (wrapperModel != null) {
            wrapperStr = wrapperModel.getAsString();
        }

        if (args.size() >= 3) {
            TemplateScalarModel modeModel = (TemplateScalarModel) args.get(2);
            if (modeModel != null) {
                modeStr = modeModel.getAsString();
            }
        }
    }

    Object res = LangFtlUtil.rewrapObject(object, WrappingOptions.makeOptions(wrapperStr, modeStr, env), env);
    return res;
}
 
Example 4
Source File: LangFtlUtil.java    From scipio-erp with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the given model as string, optionally bypassing auto-escaping done by EscapingModels.
 *
 * @see EscapingModel
 */
public static String getAsString(TemplateScalarModel model, boolean nonEscaping) throws TemplateModelException {
    if (nonEscaping && (model instanceof EscapingModel)) {
        return (String) ((EscapingModel) model).getWrappedObject();
    } else {
        return model.getAsString();
    }
}
 
Example 5
Source File: LangFtlUtil.java    From scipio-erp with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the given model as string, optionally bypassing auto-escaping done by EscapingModels;
 * if not a string, calles getAsString on it.
 * <p>
 * NOTE: this behaves similar to {@link #toRawString}, but returns a String.
 * They are almost the same.
 *
 * @see EscapingModel
 */
public static String getAsOrToString(TemplateScalarModel model, boolean nonEscaping) throws TemplateModelException {
    if (nonEscaping && (model instanceof EscapingModel)) {
        Object value = ((EscapingModel) model).getWrappedObject();
        if (value instanceof String || value == null) {
            return (String) value;
        } else {
            return model.getAsString();
        }
    } else {
        return model.getAsString();
    }
}
 
Example 6
Source File: LangFtlUtil.java    From scipio-erp with Apache License 2.0 3 votes vote down vote up
/**
 * Returns the given model as string, bypassing auto-escaping done by EscapingModels.
 * <p>
 * WARN (TODO?: REVIEW?): this can crash when model is CollectionModel or MapModel, childs of TemplateScalarModel.
 * we let it crash because non-strict typing may be dangerous and hide errors...
 *
 * @see EscapingModel
 */
public static String getAsStringNonEscaping(TemplateScalarModel model) throws TemplateModelException {
    if (model instanceof EscapingModel) {
        return (String) ((EscapingModel) model).getWrappedObject();
    } else {
        return model.getAsString();
    }
}