Java Code Examples for freemarker.template.TemplateSequenceModel#size()

The following examples show how to use freemarker.template.TemplateSequenceModel#size() . 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: 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.
 */
public static <T> Set<T> toSet(TemplateSequenceModel object) throws TemplateModelException {
    TemplateSequenceModel seqModel = (TemplateSequenceModel) object;
    Set<Object> res = new HashSet<>();
    for(int i=0; i < seqModel.size(); i++) {
        res.add(LangFtlUtil.unwrapAlways(seqModel.get(i)));
    }
    return UtilGenerics.cast(res);
}
 
Example 2
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, as TemplateModels.
 * <p>
 * NOTE: This method does NOT handle string escaping explicitly; you may want {@link #toStringSet} instead.
 */
public static <T extends TemplateModel> Set<T> toSetNoUnwrap(TemplateSequenceModel object) throws TemplateModelException {
    TemplateSequenceModel seqModel = (TemplateSequenceModel) object;
    Set<Object> res = new HashSet<>();
    for(int i=0; i < seqModel.size(); i++) {
        res.add(seqModel.get(i));
    }
    return UtilGenerics.cast(res);
}
 
Example 3
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 list.
 * <p>
 * NOTE: This method does NOT handle string escaping explicitly; you may want {@link #toStringSet} instead.
 */
public static <T> List<T> toList(TemplateSequenceModel seqModel) throws TemplateModelException {
    List<Object> res = new ArrayList<>();
    for(int i=0; i < seqModel.size(); i++) {
        res.add(LangFtlUtil.unwrapAlways(seqModel.get(i)));
    }
    return UtilGenerics.cast(res);
}
 
Example 4
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 list, as TemplateModels.
 * <p>
 * NOTE: This method does NOT handle string escaping explicitly; you may want {@link #toStringSet} instead.
 */
public static <T extends TemplateModel> List<T> toListNoUnwrap(TemplateSequenceModel seqModel) throws TemplateModelException {
    List<Object> res = new ArrayList<>();
    for(int i=0; i < seqModel.size(); i++) {
        res.add(seqModel.get(i));
    }
    return UtilGenerics.cast(res);
}
 
Example 5
Source File: LangFtlUtil.java    From scipio-erp with Apache License 2.0 5 votes vote down vote up
/**
 * To string set.
 * <p>
 * WARN: bypasses auto-escaping, caller handles.
 * (e.g. the object wrapper used to rewrap the result).
 */
public static Set<String> toStringSet(TemplateSequenceModel seqModel) throws TemplateModelException {
    Set<String> set = new HashSet<String>();
    for(int i=0; i < seqModel.size(); i++) {
        set.add(getAsStringNonEscaping((TemplateScalarModel) seqModel.get(i)));
    }
    return set;
}
 
Example 6
Source File: LangFtlUtil.java    From scipio-erp with Apache License 2.0 5 votes vote down vote up
/**
 * Gets collection as a keys.
 * <p>
 * WARN: This bypasses auto-escaping in all cases. Caller must decide how to handle.
 * (e.g. the object wrapper used to rewrap the result).
 */
public static Set<String> getAsStringSet(TemplateModel model) throws TemplateModelException {
    Set<String> exKeys = null;
    if (model != null) {
        if (model instanceof BeanModel && ((BeanModel) model).getWrappedObject() instanceof Set) {
            // WARN: bypasses auto-escaping
            exKeys = UtilGenerics.cast(((BeanModel) model).getWrappedObject());
        }
        else if (model instanceof TemplateCollectionModel) {
            exKeys = new HashSet<String>();
            TemplateModelIterator keysIt = ((TemplateCollectionModel) model).iterator();
            while(keysIt.hasNext()) {
                exKeys.add(getAsStringNonEscaping((TemplateScalarModel) keysIt.next()));
            }
        }
        else if (model instanceof TemplateSequenceModel) {
            TemplateSequenceModel seqModel = (TemplateSequenceModel) model;
            exKeys = new HashSet<String>(seqModel.size());
            for(int i=0; i < seqModel.size(); i++) {
                exKeys.add(getAsStringNonEscaping((TemplateScalarModel) seqModel.get(i)));
            }
        }
        else {
            throw new TemplateModelException("Include/exclude keys argument not a collection or set of strings");
        }
    }
    return exKeys;
}
 
Example 7
Source File: PermistionDirective.java    From Lottery with GNU General Public License v2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public void execute(Environment env, Map params, TemplateModel[] loopVars,
		TemplateDirectiveBody body) throws TemplateException, IOException {
	// 此处的权限判断有可能和拦截器的不一致,有没有关系?大部分应该没有关系,因为不需要判断权限的可以不加这个标签。
	// 光一个perms可能还不够,至少还有一个是否只浏览的问题。这个是否可以不管?可以!
	// 是否控制权限这个总是要的吧?perms为null代表无需控制权限。
	String url = DirectiveUtils.getString(PARAM_URL, params);
	boolean pass = false;
	if (StringUtils.isBlank(url)) {
		// url为空,则认为有权限。
		pass = true;
	} else {
		TemplateSequenceModel perms = getPerms(env);
		if (perms == null) {
			// perms为null,则代表不需要判断权限。
			pass = true;
		} else {
			String perm;
			for (int i = 0, len = perms.size(); i < len; i++) {
				perm = ((TemplateScalarModel) perms.get(i)).getAsString();
				if (url.startsWith(perm)) {
					pass = true;
					break;
				}
			}
		}
	}
	if (pass) {
		body.render(env.getOut());
	}
}
 
Example 8
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;
}
 
Example 9
Source File: LangFtlUtil.java    From scipio-erp with Apache License 2.0 4 votes vote down vote up
public static void addToSimpleList(SimpleSequence dest, TemplateSequenceModel source) throws TemplateModelException {
    for(int i=0; i < source.size(); i++) {
        dest.add(source.get(0));
    }
}
 
Example 10
Source File: LangFtlUtil.java    From scipio-erp with Apache License 2.0 2 votes vote down vote up
/**
 * Add to string set.
 * <p>
 * WARN: bypasses auto-escaping, caller handles.
 * (e.g. the object wrapper used to rewrap the result).
 */
public static void addToStringSet(Set<String> dest, TemplateSequenceModel seqModel) throws TemplateModelException {
    for(int i=0; i < seqModel.size(); i++) {
        dest.add(getAsStringNonEscaping(((TemplateScalarModel) seqModel.get(i))));
    }
}