Java 类freemarker.template.utility.DeepUnwrap 实例源码

项目:Equella    文件:ChooseRenderer.java   
@Override
public Object exec(@SuppressWarnings("rawtypes") List list) throws TemplateModelException
{
    RenderContext info = getSectionWriter();

    Object wrapped = DeepUnwrap.unwrap((TemplateModel) list.get(0));
    String type = list.get(1).toString();
    SectionRenderable renderable = getSectionRenderable(info, wrapped, type, factory);
    if( renderable == null )
    {
        return TemplateModel.NOTHING;
    }
    return renderable;
}
项目:Equella    文件:MutableMapModel.java   
@Override
public Object exec(List list) throws TemplateModelException
{
    if( list.size() != 2 )
    {
        throw new RuntimeException("Needs a key and a value"); //$NON-NLS-1$
    }

    String key = (String) DeepUnwrap.unwrap((TemplateModel) list.get(0));
    map.put(key, (TemplateModel) list.get(1));
    return TemplateModel.NOTHING;
}
项目:scipio-erp    文件:LangFtlUtil.java   
/**
 * Unwraps template model; if cannot, returns null.
 * <p>
 * NOTE: This automatically bypasses the auto-escaping done by wrappers implementing the <code>EscapingModel</code> interface
 * (such as Ofbiz special widget wrappers).
 */
public static Object unwrapOrNull(TemplateModel templateModel) throws TemplateModelException {
    if (templateModel != null) {
        Object res = DeepUnwrap.permissiveUnwrap(templateModel);
        if (res != templateModel) {
            return res;
        }
        else {
            return null;
        }
    }
    else {
        return null;
    }
}
项目:scipio-erp    文件:LangFtlUtil.java   
/**
 * If TemplateModel, unwraps value, or if cannot, returns null;
 * if not TemplateModel, returns as-is.
 * <p>
 * Ensures no TemplateModels remain.
 * <p>
 * NOTE: This automatically bypasses the auto-escaping done by wrappers implementing the <code>EscapingModel</code> interface
 * (such as Ofbiz special widget wrappers).
 */
public static Object unwrapOrNull(Object value) throws TemplateModelException {
    if (value instanceof TemplateModel) {
        Object res = DeepUnwrap.permissiveUnwrap((TemplateModel) value);
        if (res != value) {
            return res;
        }
        else {
            return null;
        }
    }
    else {
        return value;
    }
}
项目:scipio-erp    文件:LangFtlUtil.java   
/**
 * Unwraps template model; if cannot, returns as-is.
 * <p>
 * NOTE: This automatically bypasses the auto-escaping done by wrappers implementing the <code>EscapingModel</code> interface
 * (such as Ofbiz special widget wrappers).
 */
public static Object unwrapPermissive(TemplateModel templateModel) throws TemplateModelException {
    if (templateModel != null) {
        return DeepUnwrap.permissiveUnwrap(templateModel);
    }
    else {
        return null;
    }
}
项目:scipio-erp    文件:LangFtlUtil.java   
/**
 * Unwraps value; if cannot, returns value, even if still TemplateModel.
 * <p>
 * NOTE: This automatically bypasses the auto-escaping done by wrappers implementing the <code>EscapingModel</code> interface
 * (such as Ofbiz special widget wrappers).
 */
public static Object unwrapPermissive(Object value) throws TemplateModelException {
    if (value instanceof TemplateModel) {
        return DeepUnwrap.permissiveUnwrap((TemplateModel) value);
    }
    else {
        return value;
    }
}
项目:scipio-erp    文件:LangFtlUtil.java   
/**
 * Unwraps template model; if cannot, throws exception. If null, returns null.
 * <p>
 * NOTE: This automatically bypasses the auto-escaping done by wrappers implementing the <code>EscapingModel</code> interface
 * (such as Ofbiz special widget wrappers).
 */
public static Object unwrap(TemplateModel templateModel) throws TemplateModelException {
    if (templateModel != null) {
        return DeepUnwrap.unwrap(templateModel); // will throw exception if improper type
    }
    else {
        return null;
    }
}
项目:scipio-erp    文件:LangFtlUtil.java   
/**
 * If template model, unwraps, or if cannot, throws exception;
 * if not template model or null, returns value.
 * <p>
 * NOTE: This automatically bypasses the auto-escaping done by wrappers implementing the <code>EscapingModel</code> interface
 * (such as Ofbiz special widget wrappers).
 */
public static Object unwrap(Object value) throws TemplateModelException {
    if (value instanceof TemplateModel) {
        return DeepUnwrap.unwrap((TemplateModel) value);
    }
    else {
        return value;
    }
}
项目:scipio-erp    文件:LangFtlUtil.java   
/**
 * Unwraps value if template model and unwrappable; else exception.
 * <p>
 * Interpretation of null depends on the ObjectWrapper.
 * <p>
 * NOTE: This automatically bypasses the auto-escaping done by wrappers implementing the <code>EscapingModel</code> interface
 * (such as Ofbiz special widget wrappers).
 */
public static Object unwrapAlways(Object value) throws TemplateModelException {
    if (value instanceof TemplateModel || value == null) {
        return DeepUnwrap.unwrap((TemplateModel) value);
    }
    else {
        throw new TemplateModelException("Cannot unwrap non-TemplateModel value (type " + value.getClass().getName() + ")");
    }
}
项目:scipio-erp    文件:LangFtlUtil.java   
/**
 * Unwraps template model; if cannot, throws exception. Special case where null accepted.
 * <p>
 * Interpretation of null depends on the ObjectWrapper.
 * <p>
 * NOTE: This automatically bypasses the auto-escaping done by wrappers implementing the <code>EscapingModel</code> interface
 * (such as Ofbiz special widget wrappers).
 */
public static Object unwrapAlwaysUnlessNull(TemplateModel templateModel) throws TemplateModelException {
    if (templateModel == null) {
        return null;
    }
    else {
        return DeepUnwrap.unwrap(templateModel); // will throw exception if improper type
    }
}
项目:scipio-erp    文件:LangFtlUtil.java   
/**
 * Unwraps value if template model and unwrappable; else exception. Special case where null accepted.
 * <p>
 * Interpretation of null depends on the ObjectWrapper.
 * <p>
 * NOTE: This automatically bypasses the auto-escaping done by wrappers implementing the <code>EscapingModel</code> interface
 * (such as Ofbiz special widget wrappers).
 */
public static Object unwrapAlwaysUnlessNull(Object value) throws TemplateModelException {
    if (value instanceof TemplateModel) {
        return DeepUnwrap.unwrap((TemplateModel) value);
    }
    else if (value == null) {
        return null;
    }
    else {
        throw new TemplateModelException("Cannot unwrap non-TemplateModel value (type " + value.getClass().getName() + ")");
    }
}
项目:my-paper    文件:FreemarkerUtils.java   
/**
 * 获取参数
 * 
 * @param name
 *            名称
 * @param type
 *            类型
 * @param params
 *            参数
 * @return 参数,若不存在则返回null
 */
public static <T> T getParameter(String name, Class<T> type, Map<String, TemplateModel> params) throws TemplateModelException {
    Assert.hasText(name);
    Assert.notNull(type);
    Assert.notNull(params);
    TemplateModel templateModel = params.get(name);
    if (templateModel == null) {
        return null;
    }
    Object value = DeepUnwrap.unwrap(templateModel);
    return (T) convertUtils.convert(value, type);
}
项目:geeCommerce-Java-Shop-Software-and-PIM    文件:SetDirective.java   
@SuppressWarnings("rawtypes")
@Override
public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body)
    throws TemplateException, IOException {
    TemplateModel pValue = (TemplateModel) params.get("value");
    SimpleScalar pVar = (SimpleScalar) params.get("var");

    if (pVar == null)
        throw new IllegalArgumentException("The var parameter cannot be null in set-directive");

    String var = pVar.getAsString();
    Object value = null;

    if (pValue != null) {
        if (pValue instanceof StringModel) {
            value = ((StringModel) pValue).getWrappedObject();
        } else if (pValue instanceof SimpleHash) {
            value = ((SimpleHash) pValue).toMap();
        } else {
            value = DeepUnwrap.unwrap(pValue);
        }
    } else if (body != null) {
        StringWriter sw = new StringWriter();

        try {
            body.render(sw);
            value = sw.toString().trim();
        } finally {
            IOUtils.closeQuietly(sw);
        }
    }

    env.setVariable(var, DefaultObjectWrapper.getDefaultInstance().wrap(value));
}
项目:wsdl2html    文件:FreemarkerWebServiceDisplayer.java   
@SuppressWarnings("rawtypes")
@Override
public Object exec(List arguments) throws TemplateModelException {
    Stub stub = (Stub) DeepUnwrap.unwrap((TemplateModel) arguments.get(0));
    String parentPath = (String) DeepUnwrap.unwrap((TemplateModel) arguments.get(1));
    if (parentPath == null || StringUtils.isEmpty(parentPath)) {
        return getStubNameConsideringMultiple(stub);
    }
    return parentPath + "." + getStubNameConsideringMultiple(stub);
}
项目:wsdl2html    文件:FreemarkerWebServiceDisplayer.java   
@SuppressWarnings("rawtypes")
@Override
public Object exec(List arguments) throws TemplateModelException {
    Stub stub = (Stub) DeepUnwrap.unwrap((TemplateModel) arguments.get(0));
    String typeText = stub.getType().getSimpleName();
    if (stub.isMultiOccurs()) {
        typeText += "[]";
    }
    return typeText;
}
项目:elpi    文件:CatalogUrlDirective.java   
@Override
public void execute(Environment env, Map args, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException {
    Map<String, TemplateModel> params = UtilGenerics.checkMap(args);
    String productId = (String) DeepUnwrap.unwrap(params.get("productId"));
    String currentCategoryId = (String) DeepUnwrap.unwrap(params.get("currentCategoryId"));
    String previousCategoryId = (String) DeepUnwrap.unwrap(params.get("previousCategoryId"));

    BeanModel req = (BeanModel) env.getVariable("request");

    if (req != null) {
        HttpServletRequest request = (HttpServletRequest) req.getWrappedObject();
        env.getOut().write(CatalogUrlServlet.makeCatalogUrl(request, productId, currentCategoryId, previousCategoryId));
    }
}
项目:o3erp    文件:CatalogUrlDirective.java   
@Override
public void execute(Environment env, Map args, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException {
    Map<String, TemplateModel> params = UtilGenerics.checkMap(args);
    String productId = (String) DeepUnwrap.unwrap(params.get("productId"));
    String currentCategoryId = (String) DeepUnwrap.unwrap(params.get("currentCategoryId"));
    String previousCategoryId = (String) DeepUnwrap.unwrap(params.get("previousCategoryId"));

    BeanModel req = (BeanModel) env.getVariable("request");

    if (req != null) {
        HttpServletRequest request = (HttpServletRequest) req.getWrappedObject();
        env.getOut().write(CatalogUrlServlet.makeCatalogUrl(request, productId, currentCategoryId, previousCategoryId));
    }
}
项目:SelfSoftShop    文件:DirectiveUtil.java   
/**
 * 获取Object类型的参数值
 * 
 * @return 参数值
 */
public static Object getObjectParameter(String name, Map<String, TemplateModel> params) throws TemplateModelException {
    TemplateModel templateModel = params.get(name);
    if (templateModel == null) {
        return null;
    }
    try {
         return DeepUnwrap.unwrap(templateModel);
    } catch (TemplateModelException e) {
        throw new TemplateModelException("The \"" + name + "\" parameter " + "must be a object.");
    }
}
项目:pbox    文件:LocalizedNameDirective.java   
@Override
public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body)
        throws TemplateException, IOException {
    if (!params.containsKey("of")) {
        throw new TemplateModelException("LocalizedNameDirective directive expects 'of'.");
    }

    if (params.size() != 1) {
        throw new TemplateModelException("TeamDirective directive expects the only parameter named 'of'.");
    }

    Object obj = DeepUnwrap.unwrap((TemplateModel) params.get("of"));

    if (obj instanceof Localized) {
        Localized localized = (Localized) obj;
        String name;

        if (LocaleUtil.isRussian(ApplicationContext.getInstance().getLocale())) {
            name = StringUtil.isEmpty(localized.getRussianName())
                    ? localized.getEnglishName()
                    : localized.getRussianName();
        } else {
            name = StringUtil.isEmpty(localized.getEnglishName())
                    ? localized.getRussianName()
                    : localized.getEnglishName();
        }

        name = StringEscapeUtils.escapeHtml(name);

        name = Patterns.LINE_BREAK_PATTERN.matcher(name).replaceAll("<br/>");

        env.getOut().write(name);
    }
}
项目:mtools    文件:FreemarkerUtils.java   
public static <T> T getParameter(String name, Class<T> type, Map<String, TemplateModel> params) throws TemplateModelException
{
  Assert.hasText(name);
  Assert.notNull(type);
  Assert.notNull(params);
  TemplateModel model = (TemplateModel)params.get(name);
  if (model == null)
    return null;
  Object obj = DeepUnwrap.unwrap(model);
  return (T) convertUtilsBean.convert(obj, type);
}
项目:appleframework    文件:DirectiveUtil.java   
/**
 * 获取Object类型的参数值
 * 
 * @return 参数值
 */
public static Object getObjectParameter(String name, Map<String, TemplateModel> params) throws TemplateModelException {
    TemplateModel templateModel = params.get(name);
    if (templateModel == null) {
        return null;
    }
    try {
         return DeepUnwrap.unwrap(templateModel);
    } catch (TemplateModelException e) {
        throw new TemplateModelException("The \"" + name + "\" parameter " + "must be a object.");
    }
}
项目:geeCommerce-Java-Shop-Software-and-PIM    文件:IsActiveDirective.java   
@Override
public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body)
    throws TemplateException, IOException {
    TemplateModel pItem = (TemplateModel) params.get("item");
    SimpleScalar pVar = (SimpleScalar) params.get("var");

    App app = App.get();
    List<NavigationItem> navItems = app.registryGet(NavigationConstant.NAV_ITEMS);
    List<Id> navItemIds = app.registryGet(NavigationConstant.NAV_ITEM_IDS);

    boolean isActive = false;
    String var = null;

    if (pVar != null)
        var = pVar.getAsString();

    if (pItem != null && navItems != null) {
        Object navItem = null;

        if (pItem instanceof SimpleScalar) {
            navItem = ((SimpleScalar) pItem).getAsString();
        } else if (pItem instanceof SimpleNumber) {
            navItem = Id.valueOf(((SimpleNumber) pItem).getAsNumber());
        } else if (pItem instanceof StringModel) {
            navItem = ((StringModel) pItem).getWrappedObject();
        } else {
            navItem = DeepUnwrap.unwrap(pItem);
        }

        if (navItem instanceof String) {
            for (NavigationItem navigationItem : navItems) {
                if ((navigationItem.getLabel() != null
                    && Str.trimEqualsIgnoreCase((String) navItem, navigationItem.getLabel().str()))
                    || Str.trimEqualsIgnoreCase((String) navItem, navigationItem.getDisplayURI())) {
                    isActive = true;
                    break;
                }
            }
        } else if (navItem instanceof Id) {
            isActive = navItemIds.contains(navItem);
        } else if (navItem instanceof NavigationItem) {
            isActive = navItemIds.contains(((NavigationItem) navItem).getId());
        }
    }

    if (isActive && Str.isEmpty(var)) {
        body.render(env.getOut());
    } else {
        env.setVariable(var, DefaultObjectWrapper.getDefaultInstance().wrap(isActive));
    }
}
项目:geeCommerce-Java-Shop-Software-and-PIM    文件:URLDirective.java   
@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body)
    throws TemplateException, IOException {
    SimpleScalar pPath = (SimpleScalar) params.get("path");
    TemplateModel pTarget = (TemplateModel) params.get("target");
    SimpleScalar pVar = (SimpleScalar) params.get("var");

    String path = null;
    String varName = null;

    App app = App.get();

    // String path has precedence.
    if (pPath != null) {
        path = pPath.getAsString();

        if (!Str.isEmpty(path) && !Str.SLASH.equals(path.trim())) {
            UrlRewrite urlRewrite = urlRewrites.forTargetURI(path);

            if (urlRewrite != null) {
                String requestPath = ContextObjects.findCurrentLanguage(urlRewrite.getRequestURI());

                if (!Str.isEmpty(requestPath))
                    path = requestPath;
            }
        }
    }

    // If no path was set, attempt other objects.
    if (path == null && pTarget != null) {
        Object target = DeepUnwrap.unwrap(pTarget);

        if (target instanceof TargetSupport) {
            path = app.helper(TargetSupportHelper.class).findURI((TargetSupport) target);
        } else if (target instanceof ContextObject) {
            path = ContextObjects.findCurrentLanguageOrGlobal((ContextObject<String>) target);
        } else {
            path = target.toString();
        }
    }

    // Optionally put the result into a parameters map instead of outputting
    // it.
    if (pVar != null)
        varName = pVar.getAsString();

    if (path != null) {
        HttpServletResponse response = app.servletResponse();

        if (varName != null) {
            // Sets the result into the current template as if using
            // <#assign name=model>.
            env.setVariable(varName, DefaultObjectWrapper.getDefaultInstance().wrap(response.encodeURL(path)));
        } else {
            // Simply writes the result to the template.
            env.getOut().write(response.encodeURL(path));
        }
    }
}
项目:wsdl2html    文件:FreemarkerWebServiceDisplayer.java   
@SuppressWarnings("rawtypes")
@Override
public Object exec(List arguments) throws TemplateModelException {
    Stub stub = (Stub) DeepUnwrap.unwrap((TemplateModel) arguments.get(0));
    return getStubNameConsideringMultiple(stub);
}
项目:wsdl2html    文件:FreemarkerWebServiceDisplayer.java   
@SuppressWarnings("rawtypes")
@Override
public Object exec(List arguments) throws TemplateModelException {
    Class<?> clazz = (Class<?>) DeepUnwrap.unwrap((TemplateModel) arguments.get(0));
    return clazz.getSimpleName();
}
项目:scipio-erp    文件:LangFtlUtil.java   
/**
 * Unwraps template model; if cannot, throws exception.
 * <p>
 * Interpretation of null depends on the ObjectWrapper.
 * <p>
 * NOTE: This automatically bypasses the auto-escaping done by wrappers implementing the <code>EscapingModel</code> interface
 * (such as Ofbiz special widget wrappers).
 */
public static Object unwrapAlways(TemplateModel templateModel) throws TemplateModelException {
    return DeepUnwrap.unwrap(templateModel); // will throw exception if improper type
}