@Override protected ObjectWrapper initialValue() { return new DefaultObjectWrapper() { /* (non-Javadoc) * @see freemarker.template.DefaultObjectWrapper#wrap(java.lang.Object) */ @Override public TemplateModel wrap(Object obj) throws TemplateModelException { if (obj instanceof QNameMap) { return new QNameHash((QNameMap)obj, this); } else { return super.wrap(obj); } } }; }
/** * Gets the request stack as a list. The stack cannot be modified using this list. * It may be TemplateModel-wrapped or unwrapped as may be the individual values. * * @param name * @param request * @param context * @param env * @param copyTargetType target type for list copy. if null, does not copy (should be avoided in most cases!). * @return * @throws TemplateModelException */ static Object getRequestStackAsList(String name, HttpServletRequest request, Map<String, Object> context, Environment env, ObjectWrapper objectWrapper, TemplateValueTargetType copyTargetType) throws TemplateModelException { if (request != null) { return getStackAsList(getRequestVarMapFromReqAttribs(request), name, copyTargetType, objectWrapper, "request attributes"); } else { Map<String, Object> globalContext = getGlobalContext(context, env); if (globalContext != null) { return getStackAsList(getRequestVarMapFromGlobalContext(globalContext), name, copyTargetType, objectWrapper, "globalContext"); } else if (env != null) { return getStackAsList(getRequestVarMapFromFtlGlobals(env), name, copyTargetType, objectWrapper, "FTL globals"); } else { throw new IllegalArgumentException("No request, context or ftl environment to get request scope stack (name: " + name + ")"); } } }
private static Object getStackAsList(Map<String, Object> varMap, String name, TemplateValueTargetType copyTargetType, ObjectWrapper objectWrapper, String desc) throws TemplateModelException { List<Object> stack = null; Object stackObj = varMap.get(name); if (stackObj instanceof List) { stack = UtilGenerics.checkList(stackObj); } if (stack != null) { if (copyTargetType == null) { return Collections.unmodifiableList(stack); } else { return LangFtlUtil.copyList(stack, copyTargetType, objectWrapper); } } else { return null; } }
@SuppressWarnings("unchecked") @Override public Object exec(List args) throws TemplateModelException { if (args == null || args.size() != 1) { throw new TemplateModelException("Invalid number of arguments (expected: 1)"); } TemplateModel nameModel = (TemplateModel) args.get(0); if (!(nameModel instanceof TemplateScalarModel)) { throw new TemplateModelException("First argument not an instance of TemplateScalarModel (string)"); } Environment env = CommonFtlUtil.getCurrentEnvironment(); Object res = ContextFtlUtil.getRequestVar(LangFtlUtil.getAsStringNonEscaping(((TemplateScalarModel) nameModel)), env); ObjectWrapper objectWrapper = getResultObjectWrapper(env); return LangFtlUtil.wrap(res, objectWrapper); }
@SuppressWarnings("unchecked") protected Object execRead(List args, boolean pop) throws TemplateModelException { if (args == null || args.size() != 1) { throw new TemplateModelException("Invalid number of arguments (expected: 1)"); } TemplateModel nameModel = (TemplateModel) args.get(0); if (!(nameModel instanceof TemplateScalarModel)) { throw new TemplateModelException("First argument not an instance of TemplateScalarModel (string)"); } Environment env = CommonFtlUtil.getCurrentEnvironment(); Object res = ContextFtlUtil.readRequestStack(LangFtlUtil.getAsStringNonEscaping(((TemplateScalarModel) nameModel)), pop, env); ObjectWrapper objectWrapper = GetRequestVarMethod.getResultObjectWrapper(env); return LangFtlUtil.wrap(res, objectWrapper); }
/** * Wraps the TemplateInvoker in an appropriate TemplateModel, using explicit model type. * Can be called manually as this logic may not be present in <code>ObjectWrapper.wrap</code>. */ @SuppressWarnings("unchecked") public static <T extends TemplateModel> T wrap(TemplateInvoker invoker, WrapperModel targetModel, ObjectWrapper objectWrapper) throws TemplateModelException { if (targetModel == null || targetModel == WrapperModel.HYBRID) { return (T) new HybridInvokerWrapper(invoker); } else if (targetModel == null || targetModel == WrapperModel.SCALAR) { // 2017-03-31: just avoid the bean wrapper as much as possible, even though may not entirely be able to // if (invoker instanceof StringTemplateInvoker && objectWrapper != null) { // return (T) objectWrapper.wrap(invoker); // } else { return (T) new ScalarInvokerWrapper(invoker); // } } else if (targetModel == WrapperModel.DIRECTIVE) { return (T) new DirectiveInvokerWrapper(invoker); } throw new UnsupportedOperationException("Unsupported template invoker FTL wrapper model: " + targetModel); }
public static WrappingOptions makeOptions(String wrapper, String mode, Environment env) throws TemplateModelException { ObjectWrapper targetWrapper = ObjectWrapperUtil.getObjectWrapperByName(wrapper, env); if (targetWrapper == null) { throw new TemplateModelException("Unrecognized wrapper name: " + wrapper); } if (wrapper == null) { wrapper = ""; } RewrapMode rewrapMode = RewrapMode.ALWAYS_DEEP; if (mode != null && !mode.isEmpty()) { rewrapMode = RewrapMode.fromString(mode); if (rewrapMode == null) { throw new TemplateModelException("Unrecognized rewrapping mode: " + mode); } } return new WrappingOptions(wrapper, ObjectWrapperUtil.getObjectWrapperByName(wrapper, env), rewrapMode, env.getObjectWrapper()); }
@SuppressWarnings("unchecked") @Override public Object exec(List args) throws TemplateModelException { if (args == null || args.size() < 0 || args.size() > 1) { throw new TemplateModelException("Invalid number of arguments (expected: 0-1)"); } Environment env = CommonFtlUtil.getCurrentEnvironment(); if (args.size() > 0) { TemplateModel object = (TemplateModel) args.get(0); ObjectWrapper objectWrapper = LangFtlUtil.getCurrentObjectWrapper(env); return LangFtlUtil.toSet(object, objectWrapper); } else { return new HashSet<Object>(); } }
/** * Copies map. * <p> * WARN: For complex maps, auto-escaping is bypassed; caller must decide how to handle. * (e.g. the object wrapper used to rewrap the result). * <p> * FIXME: The rewrapping objectWrapper behavior is inconsistent! may lead to auto-escape issues */ public static Object copyMap(TemplateModel object, Set<String> inExKeys, Boolean include, TemplateValueTargetType targetType, ObjectWrapper objectWrapper) throws TemplateModelException { if (targetType == null) { targetType = TemplateValueTargetType.PRESERVE; } if (OfbizFtlObjectType.COMPLEXMAP.isObjectType(object)) { // WARN: bypasses auto-escaping Map<String, Object> wrappedObject = UtilGenerics.cast(((WrapperTemplateModel) object).getWrappedObject()); // TODO: this only handles most urgent targetType case if (targetType == TemplateValueTargetType.SIMPLEMODEL) { return LangFtlUtil.copyMapToSimple(wrappedObject, inExKeys, include, objectWrapper); } else { return LangFtlUtil.copyMapToRawMap(wrappedObject, inExKeys, include); } } else if (object instanceof TemplateHashModel && OfbizFtlObjectType.MAP.isObjectType(object)) { // TODO: this ignores targetType return LangFtlUtil.copyMapToSimple((TemplateHashModel) object, inExKeys, include, objectWrapper); } throw new TemplateModelException("Cannot copy map of type " + object.getClass().toString() + " to target type: " + targetType.toString()); }
/** * Converts map to a simple wrapper, if applicable. Currently only applies to complex maps. * <p> * 2017-01-26: This has been changed so that it will by default try to wrap everything * with DefaultMapAdapter (or SimpleMapModel for BeansWrapper compatibility), which will always work as long as * the ObjectWrapper implements ObjectWrapperWithAPISupport. * <p> * WARN: Bypasses auto-escaping for complex maps; caller must decide how to handle * (e.g. the object wrapper used to rewrap the result). * Other types of maps are not altered. */ public static TemplateHashModel toSimpleMap(TemplateModel object, Boolean copy, ObjectWrapper objectWrapper) throws TemplateModelException { if (OfbizFtlObjectType.COMPLEXMAP.isObjectType(object)) { // WARN: bypasses auto-escaping Map<?, ?> wrappedObject = UtilGenerics.cast(((WrapperTemplateModel) object).getWrappedObject()); if (Boolean.TRUE.equals(copy)) { return makeSimpleMapCopy(wrappedObject, objectWrapper); } else { return makeSimpleMapAdapter(wrappedObject, objectWrapper, true); } } else if (object instanceof TemplateHashModel) { return (TemplateHashModel) object; } else { throw new TemplateModelException("object is not a recognized map type"); } }
/** * Adapts a map to a TemplateHashModelEx using an appropriate simple adapter, normally * DefaultMapAdapter (or SimpleMapModel for BeansWrapper compatibility). * <p> * The ObjectWrapper is expected to implement at least ObjectWrapperWithAPISupport. * <p> * WARN: If impossible, it will duplicate the map using SimpleHash; but because this may result * in loss of ordering, a log warning will be printed. */ public static TemplateHashModelEx makeSimpleMapAdapter(Map<?, ?> map, ObjectWrapper objectWrapper, boolean permissive) throws TemplateModelException { // COMPATIBILITY MODE: check if exactly BeansWrapper, or a class that we know extends it WITHOUT extending DefaultObjectWrapper if (objectWrapper instanceof ScipioBeansWrapper || BeansWrapper.class.equals(objectWrapper.getClass())) { return new SimpleMapModel(map, (BeansWrapper) objectWrapper); } else if (objectWrapper instanceof ObjectWrapperWithAPISupport) { return DefaultMapAdapter.adapt(map, (ObjectWrapperWithAPISupport) objectWrapper); } else { if (permissive) { Debug.logWarning("Scipio: adaptSimpleMap: Unsupported Freemarker object wrapper (expected to implement ObjectWrapperWithAPISupport or BeansWrapper); forced to adapt map" + " using SimpleHash; this could cause loss of map insertion ordering; please switch renderer setup to a different ObjectWrapper", module); return new SimpleHash(map, objectWrapper); } else { throw new TemplateModelException("Tried to wrap a Map using an adapter class," + " but our ObjectWrapper does not implement ObjectWrapperWithAPISupport or BeansWrapper" + "; please switch renderer setup to a different ObjectWrapper"); } } }
/** * Converts map to a simple wrapper, if applicable, by rewrapping * known complex map wrappers that implement <code>WrapperTemplateModel</code>. * <p> * If the specified ObjectWrapper is a BeansWrapper, this forces rewrapping as a SimpleMapModel. * If it isn't we assume caller specified an objectWrapper that will rewrap the map with * a simple model (we have no way of knowing). * <p> * WARN: Bypasses auto-escaping for complex maps; caller must decide how to handle * (e.g. the object wrapper used to rewrap the result). * Other types of maps are not altered. * * @deprecated don't use */ @Deprecated private static TemplateHashModel toSimpleMapRewrapAdapters(TemplateModel object, ObjectWrapper objectWrapper) throws TemplateModelException { if (object instanceof SimpleMapModel || object instanceof BeanModel || object instanceof DefaultMapAdapter) { // Permissive Map<?, ?> wrappedObject = (Map<?, ?>) ((WrapperTemplateModel) object).getWrappedObject(); if (objectWrapper instanceof BeansWrapper) { // Bypass the beanswrapper wrap method and always make simple wrapper return new SimpleMapModel(wrappedObject, (BeansWrapper) objectWrapper); } else { // If anything other than BeansWrapper, assume caller is aware and his wrapper will create a simple map return (TemplateHashModel) objectWrapper.wrap(wrappedObject); } } else if (object instanceof TemplateHashModel) { return (TemplateHashModel) object; } else { throw new TemplateModelException("object is not a recognized map type"); } }
/** * Converts map to a simple wrapper, if applicable, by rewrapping * any map wrappers that implement <code>WrapperTemplateModel</code>. * <p> * This method is very permissive: anything that wraps a Map is accepted. * Other types of hashes are returned as-is. * <p> * If the specified ObjectWrapper is a BeansWrapper, this forces rewrapping as a SimpleMapModel. * If it isn't we assume caller specified an objectWrapper that will rewrap the map with * a simple model (we have no way of knowing). * <p> * WARN: Bypasses auto-escaping for complex maps; caller must decide how to handle * (e.g. the object wrapper used to rewrap the result). * Other types of maps are not altered. * * @deprecated don't use */ @Deprecated private static TemplateHashModel toSimpleMapRewrapAny(TemplateModel object, ObjectWrapper objectWrapper) throws TemplateModelException { if (object instanceof WrapperTemplateModel) { // Permissive Map<?, ?> wrappedObject = (Map<?, ?>) ((WrapperTemplateModel) object).getWrappedObject(); if (objectWrapper instanceof BeansWrapper) { // Bypass the beanswrapper wrap method and always make simple wrapper return new SimpleMapModel(wrappedObject, (BeansWrapper) objectWrapper); } else { // If anything other than BeansWrapper, assume caller is aware and his wrapper will create a simple map return (TemplateHashModel) objectWrapper.wrap(wrappedObject); } } else if (object instanceof TemplateHashModel) { return (TemplateHashModel) object; } else { throw new TemplateModelException("object is not a recognized map type"); } }
/** * Combines two maps with the given operator into a new hash. */ public static TemplateHashModelEx combineMaps(TemplateHashModelEx first, TemplateHashModelEx second, SetOperations ops, ObjectWrapper objectWrapper) throws TemplateModelException { SimpleHash res = new SimpleHash(objectWrapper); if (ops == null || ops == SetOperations.UNION) { // this is less efficient than freemarker + operator, but provides the "alternative" implementation, so have choice addToSimpleMap(res, first); addToSimpleMap(res, second); } else if (ops == SetOperations.INTERSECT) { Set<String> intersectKeys = toStringSet(second.keys()); intersectKeys.retainAll(toStringSet(first.keys())); addToSimpleMap(res, second, intersectKeys); } else if (ops == SetOperations.DIFFERENCE) { Set<String> diffKeys = toStringSet(first.keys()); diffKeys.removeAll(toStringSet(second.keys())); addToSimpleMap(res, first, diffKeys); } else { throw new TemplateModelException("Unsupported combineMaps operation"); } return res; }
@Override public ObjectWrapper getWrapper(Environment env) { ObjectWrapper curr = env.getObjectWrapper(); if (curr instanceof ScipioExtendedObjectWrapper) { if (curr instanceof ScipioBeansWrapper) { if (((BeansWrapper) curr).isSimpleMapWrapper()) { return FreeMarkerWorker.getDefaultOfbizSimpleMapWrapper(); } else { return FreeMarkerWorker.getDefaultOfbizWrapper(); } } else { return FreeMarkerWorker.getDefaultOfbizSimpleMapWrapper(); } } else { return curr; } }
/** * Initialize FreeMarker elements after servlet context and FreeMarker configuration have both * been populated. */ private static void finishConfig() { if (freeMarkerConfig != null && servletContext != null) { taglibFactory = new TaglibFactory(servletContext); objectWrapper = freeMarkerConfig.getObjectWrapper(); if (objectWrapper == null) { objectWrapper = ObjectWrapper.DEFAULT_WRAPPER; } GenericServlet servlet = new ServletAdapter(); try { servlet.init(new DelegatingServletConfig()); } catch (ServletException ex) { throw new BeanInitializationException("Initialization of GenericServlet adapter failed", ex); } servletContextHashModel = new ServletContextHashModel(servlet, ObjectWrapper.DEFAULT_WRAPPER); LOG.info("Freemarker configuration complete"); } }
/** * 初始化freemarker配置 * */ public void initialize() { try { if (templateconfig == null) { templateconfig = new Configuration(); templateconfig .setTemplateExceptionHandler(TemplateExceptionHandler.HTML_DEBUG_HANDLER); templateconfig.setObjectWrapper(ObjectWrapper.DEFAULT_WRAPPER); templateconfig.setTemplateLoader(new ClassTemplateLoader(this .getClass(), "/")); templateconfig.setTemplateUpdateDelay(1200); templateconfig.setDefaultEncoding("gb2312"); templateconfig.setLocale(new java.util.Locale("zh_CN")); templateconfig.setNumberFormat("0.##########"); } } catch (Exception e) { } }
/** * 初始化freemarker配置 * */ public void initialize() { try { if (templateconfig==null) { templateconfig = new Configuration(); templateconfig.setTemplateExceptionHandler(TemplateExceptionHandler.HTML_DEBUG_HANDLER); templateconfig.setObjectWrapper(ObjectWrapper.DEFAULT_WRAPPER); templateconfig.setTemplateLoader(new ClassTemplateLoader(this.getClass(),"/")); templateconfig.setTemplateUpdateDelay(1200); templateconfig.setDefaultEncoding("gb2312"); templateconfig.setLocale(new java.util.Locale("zh_CN")); templateconfig.setNumberFormat("0.##########"); } } catch (Exception e) {} }
@SuppressWarnings("unchecked") public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException { String navigationPositionString = DirectiveUtil.getStringParameter(POSITION_PARAMETER_NAME, params); Integer count = DirectiveUtil.getIntegerParameter(COUNT_PARAMETER_NAME, params); NavigationPosition navigationPosition = null; if (StringUtils.isNotEmpty(navigationPositionString)) { navigationPosition = NavigationPosition.valueOf(navigationPositionString); } List<Navigation> navigationList = navigationService.getNavigationList(navigationPosition, count); if (body != null && navigationList != null) { if (loopVars.length > 0) { loopVars[0] = ObjectWrapper.BEANS_WRAPPER.wrap(navigationList); } body.render(env.getOut()); } }
@SuppressWarnings("unchecked") public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException { String goodsId = DirectiveUtil.getStringParameter(GOODS_ID_PARAMETER_NAME, params); Integer count = DirectiveUtil.getIntegerParameter(COUNT_PARAMETER_NAME, params); Goods goods = null; if (StringUtils.isNotEmpty(goodsId)) { goods = goodsService.load(goodsId); } List<Comment> commentList = commentService.getCommentList(goods, count); if (body != null && commentList != null) { if (loopVars.length > 0) { loopVars[0] = ObjectWrapper.BEANS_WRAPPER.wrap(commentList); } body.render(env.getOut()); } }
@SuppressWarnings("unchecked") public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException { String instantMessagingTypeString = DirectiveUtil.getStringParameter(INSTANT_MESSAGING_TYPE_PARAMETER_NAME, params); Integer count = DirectiveUtil.getIntegerParameter(COUNT_PARAMETER_NAME, params); InstantMessagingType instantMessagingType = null; if (StringUtils.isNotEmpty(instantMessagingTypeString)) { instantMessagingType = InstantMessagingType.valueOf(instantMessagingTypeString); } List<InstantMessaging> instantMessagingList = instantMessagingService.getInstantMessagingList(instantMessagingType, count); if (body != null && instantMessagingList != null) { if (loopVars.length > 0) { loopVars[0] = ObjectWrapper.BEANS_WRAPPER.wrap(instantMessagingList); } body.render(env.getOut()); } }
public static void setVariables(Map variables, Environment env) throws TemplateModelException { Assert.notNull(variables); Assert.notNull(env); for(Iterator iterator = variables.entrySet().iterator(); iterator.hasNext();) { java.util.Map.Entry entry = (java.util.Map.Entry)iterator.next(); String s = (String)entry.getKey(); Object obj = entry.getValue(); if(obj instanceof TemplateModel) env.setVariable(s, (TemplateModel)obj); else env.setVariable(s, ObjectWrapper.BEANS_WRAPPER.wrap(obj)); } }
@Override public void execute(Environment e, Map map, TemplateModel[] tms, TemplateDirectiveBody tdb) throws TemplateException, IOException { tagParameter = map; request = (HttpServletRequest) e.getCustomAttribute("request"); urlSetter = (URLQueryHandler) e.getCustomAttribute("urlSetter"); out = e.getOut(); HashMap<String, Object> mapx = this.doService(); if (mapx != null) { for (Entry<String, Object> c : mapx.entrySet()) { e.setVariable(c.getKey(), ObjectWrapper.DEFAULT_WRAPPER.wrap(c.getValue())); } } if (tdb != null) { tdb.render(e.getOut()); } }
@Override public void execute(Environment e, Map map, TemplateModel[] tms, TemplateDirectiveBody tdb) throws TemplateException, IOException { this.tagParameter = map; this.request = (HttpServletRequest) e.getCustomAttribute("request"); this.out = e.getOut(); HashMap<String, Object> mapx = this.doService(); if (mapx != null) { for (Entry<String, Object> c : mapx.entrySet()) { e.setVariable(c.getKey(), ObjectWrapper.DEFAULT_WRAPPER.wrap(c.getValue())); } } if (tdb != null) { tdb.render(e.getOut()); } }
@Override public void execute(Environment e, Map map, TemplateModel[] tms, TemplateDirectiveBody tdb) throws TemplateException, IOException { this.tagParameter = map; this.out = e.getOut(); String[] tp = e.getCustomAttributeNames(); HashMap<String, Object> info = new HashMap<String, Object>(); for (String tpp : tp) { info.put(tpp, e.getCustomAttribute(tpp)); } ObjectSnooper.snoop(this).set(info); HashMap<String, Object> mapx = this.doService(); if (mapx != null) { for (Entry<String, Object> c : mapx.entrySet()) { e.setVariable(c.getKey(), ObjectWrapper.DEFAULT_WRAPPER.wrap(c.getValue())); } } if (tdb != null) { tdb.render(e.getOut()); } }
public void start() { try { cfg = new Configuration(); cfg.setDirectoryForTemplateLoading(new File("templates")); cfg.setTemplateUpdateDelay(0); cfg.setTemplateExceptionHandler(TemplateExceptionHandler.HTML_DEBUG_HANDLER); cfg.setObjectWrapper(ObjectWrapper.BEANS_WRAPPER); cfg.setDefaultEncoding("utf-8"); cfg.setLocale(Locale.CHINESE); cfg.setSharedVariable("sitename", config.getString("radius.version")); cfg.setSharedVariable("context", context); } catch (Exception e) { log.error("freemarker init error", e); } }
public static String getTempleteContent( Map map , String templateName) throws IOException, TemplateException { //根据数据将ftl转化为html,并以String类型返回 Configuration cfg = new Configuration(); // TODO: // as shown in TestFreeMarker, it's not perfect that the ?keys will return method names also, need refer to how spring freemarker plugin do the render // NOTE: however in runtime it works ok !! // refer to JAVADOC: Configuration.isClassicCompatible() cfg.setClassicCompatible(true); // refer to Freemarker manuual chapter: Bean wrapper cfg.setObjectWrapper(ObjectWrapper.BEANS_WRAPPER); //TODO cfg.setDirectoryForTemplateLoading(new File("D:/Jcommerce/JCommerceGae/war/web/front/library")); Template t = cfg.getTemplate(templateName,"UTF-8"); StringWriter stringWriter = new StringWriter(); t.process(map, stringWriter); return stringWriter.toString(); }
protected TemplateModel createModel(ObjectWrapper objectWrapper, ServletContext servletContext, final HttpServletRequest request, final HttpServletResponse response) throws TemplateModelException { AllHttpScopesHashModel params = (AllHttpScopesHashModel) super.createModel(objectWrapper, servletContext, request, response); for (String key : FREEMARKER_EXT_METHODS.keySet()) { params.putUnlistedModel(key, FREEMARKER_EXT_METHODS.get(key)); } params.putUnlistedModel("urlEncode", URLENCODEMETHOD); params.putUnlistedModel("sleep", SLEEPMETHOD); return params; }
public static Object popRequestStack(String name, Environment env, ObjectWrapper objectWrapper) throws TemplateModelException { HttpServletRequest request = getRequest(env); Map<String, Object> context = null; if (request == null) { context = getContext(env); } return readRequestStack(name, true, request, context, env); }
/** * Returns copy of request stack in request copyTargetType. * <strong>WARN</strong>: if copyTargetType is null, no copy is made and unmodifiable list is returned. * This list must be discarded by caller as soon as possible, before any more changes to the stack. */ public static Object getRequestStackAsList(String name, LangFtlUtil.TemplateValueTargetType copyTargetType, Environment env, ObjectWrapper objectWrapper) throws TemplateModelException { HttpServletRequest request = getRequest(env); Map<String, Object> context = null; if (request == null) { context = getContext(env); } return getRequestStackAsList(name, request, context, env, objectWrapper, copyTargetType); }
@SuppressWarnings("unchecked") protected Object execGetAsList(List args) throws TemplateModelException { if (args == null || args.size() < 1 || args.size() > 2) { throw new TemplateModelException("Invalid number of arguments (expected: 1-2)"); } TemplateModel nameModel = (TemplateModel) args.get(0); if (!(nameModel instanceof TemplateScalarModel)) { throw new TemplateModelException("First argument not an instance of TemplateScalarModel (string)"); } String stackName = LangFtlUtil.getAsStringNonEscaping(((TemplateScalarModel) nameModel)); boolean origList = false; if (args.size() >= 2) { TemplateModel listTypeModel = (TemplateModel) args.get(1); String listType = LangFtlUtil.getAsStringNonEscaping(((TemplateScalarModel) listTypeModel)); if ("orig".equals(listType)) { origList = true; } } Environment env = CommonFtlUtil.getCurrentEnvironment(); ObjectWrapper objectWrapper = GetRequestVarMethod.getResultObjectWrapper(env); Object res = ContextFtlUtil.getRequestStackAsList(stackName, (origList ? null : LangFtlUtil.TemplateValueTargetType.SIMPLEMODEL), env, objectWrapper); return LangFtlUtil.wrap(res, objectWrapper); }
@SuppressWarnings("unchecked") @Override public Object exec(List args) throws TemplateModelException { if (args == null || args.size() != 2) { throw new TemplateModelException("Invalid number of arguments (expected: 2)"); } String styleString = LangFtlUtil.getAsStringNonEscaping(((TemplateScalarModel) args.get(0))); styleString = TemplateFtlUtil.getPlainClassArgNames(styleString); String prefix = LangFtlUtil.getAsStringNonEscaping(((TemplateScalarModel) args.get(1))); String[] names = StringUtils.split(styleString, ' '); // NOTE: For emergency/safety reasons, use the current wrapper, which MAY be escaping. // style strings contain only simple characters anyway. ObjectWrapper objectWrapper = LangFtlUtil.getCurrentObjectWrapper(); SimpleSequence res = new SimpleSequence(names.length, objectWrapper); for(String name : names) { if (name.startsWith(prefix)) { res.add(name); } } // redundant //return LangFtlUtil.wrap(res, objectWrapper); return res; }
public WrappingOptions(String targetWrapperName, ObjectWrapper targetWrapper, RewrapMode rewrapMode, ObjectWrapper curObjectWrapper) { super(); this.targetWrapperName = targetWrapperName; this.targetWrapper = targetWrapper; this.rewrapMode = rewrapMode; this.curObjectWrapper = curObjectWrapper; }
@SuppressWarnings("unchecked") @Override public Object exec(List args) throws TemplateModelException { if (args == null || args.size() != 1) { throw new TemplateModelException("Invalid number of arguments (expected: 1)"); } Environment env = CommonFtlUtil.getCurrentEnvironment(); TemplateModel object = (TemplateModel) args.get(0); ObjectWrapper objectWrapper = LangFtlUtil.getCurrentObjectWrapper(env); return LangFtlUtil.toSimpleMap(object, null, objectWrapper); }
@SuppressWarnings("unchecked") @Override public Object exec(List args) throws TemplateModelException { if (args == null || args.size() < 1 || args.size() > 2) { throw new TemplateModelException("Invalid number of arguments (expected: 1-2)"); } Environment env = CommonFtlUtil.getCurrentEnvironment(); TemplateModel object = (TemplateModel) args.get(0); ObjectWrapper objectWrapper = LangFtlUtil.getCurrentObjectWrapper(env); return LangFtlUtil.copyObject(object, null, objectWrapper); }
/** * Checks if the wrapper is a special escaping wrapper, and if so, * returns a non-escaping one. */ public static ObjectWrapper getNonEscapingObjectWrapper(ObjectWrapper objectWrapper) { if (objectWrapper instanceof ScipioExtendedObjectWrapper) { return FreeMarkerWorker.getDefaultOfbizWrapper(); } else { return objectWrapper; } }
/** * Checks if the current env wrapper is a special escaping wrapper, and if so, * returns a non-escaping one. */ public static ObjectWrapper getNonEscapingObjectWrapper(Environment env) { ObjectWrapper objectWrapper = env.getObjectWrapper(); if (objectWrapper instanceof ScipioExtendedObjectWrapper) { return FreeMarkerWorker.getDefaultOfbizWrapper(); } else { return objectWrapper; } }
/** * Checks if the current env wrapper is a special escaping wrapper, and if so, * returns a non-escaping one. */ public static ObjectWrapper getNonEscapingObjectWrapper() { Environment env = FreeMarkerWorker.getCurrentEnvironment(); ObjectWrapper objectWrapper = env.getObjectWrapper(); if (objectWrapper instanceof ScipioExtendedObjectWrapper) { return FreeMarkerWorker.getDefaultOfbizWrapper(); } else { return objectWrapper; } }