Java Code Examples for freemarker.template.TemplateHashModelEx#get()

The following examples show how to use freemarker.template.TemplateHashModelEx#get() . 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: EntityTemplateModel.java    From keycloak with Apache License 2.0 5 votes vote down vote up
public static String modelExToString(TemplateHashModelEx thme) {
    StringBuilder sb = new StringBuilder();
    TemplateModelIterator i;
    try {
        i = thme.keys().iterator();
        while (i.hasNext()) {
            TemplateModel k = i.next();
            TemplateModel v = thme.get(k.toString());
            sb.append(" - ").append(k).append("=").append(v).append('\n');
        }
    } catch (TemplateModelException ex) {
        throw new RuntimeException(ex);
    }
    return sb.toString();
}
 
Example 2
Source File: MakeAttribMapFromArgMapMethod.java    From scipio-erp with Apache License 2.0 4 votes vote down vote up
@Override
public Object exec(@SuppressWarnings("rawtypes") List args) throws TemplateModelException {
    if (args == null || args.size() < 1 || args.size() > 2 ) {
        throw new TemplateModelException("Invalid number of arguments (expected: 1-2)");
    }

    ObjectWrapper objectWrapper = CommonFtlUtil.getCurrentEnvironment().getObjectWrapper();

    // support empty list (ignore, treat as empty hash)
    TemplateModel argsObj = (TemplateModel) args.get(0);
    if (argsObj instanceof TemplateSequenceModel) {
        TemplateSequenceModel argsSeq = (TemplateSequenceModel) argsObj;
        if (argsSeq.size() == 0) {
            argsObj = TemplateHashModelEx.NOTHING;
        } else {
            throw new TemplateModelException("Invalid argument type (sequence) - expected hash");
        }
    }

    TemplateHashModelEx argsMap = (TemplateHashModelEx) argsObj;

    // caller-supplied excludes
    TemplateModel excludesModel = (args.size() >=2) ? (TemplateModel) args.get(1) : null;
    Set<String> excludes;
    if (excludesModel != null) {
        excludes = LangFtlUtil.getAsStringSet(excludesModel);
    } else {
        excludes = new HashSet<>();
    }

    SimpleHash res = null;

    final Boolean useExclude = Boolean.FALSE;

    // put attribs from explicit attribs map first, if any
    TemplateModel attribsModel = argsMap.get("attribs");
    if (attribsModel != null && OfbizFtlObjectType.isObjectType(OfbizFtlObjectType.MAP, attribsModel)) {
        if (OfbizFtlObjectType.isObjectType(OfbizFtlObjectType.COMPLEXMAP, attribsModel)) {
            attribsModel = LangFtlUtil.toSimpleMap(attribsModel, false, objectWrapper);
        }
        res = LangFtlUtil.copyMapToSimple((TemplateHashModel) attribsModel, excludes, useExclude, objectWrapper);
    }

    // to get inline attribs, add list of all arg names to excludes as well as the lists themselves
    TemplateModel allArgNamesModel = argsMap.get("allArgNames");
    if (allArgNamesModel != null) {
        excludes.addAll(LangFtlUtil.getAsStringSet(allArgNamesModel));
    }
    excludes.add("allArgNames");
    excludes.add("localArgNames");
    excludes.add("attribs"); // 2020-02-12: in most cases this was automatically added by makeArgMaps from default args list, but custom usages need this now

    // add the inline attribs over the attribs map (if any)
    if (res == null) {
        res = LangFtlUtil.copyMapToSimple(argsMap, excludes, useExclude, objectWrapper);
    } else {
        LangFtlUtil.putAll(res, argsMap, excludes, useExclude, objectWrapper);
    }

    return res;
}