Java Code Examples for freemarker.core.Environment#getObjectWrapper()

The following examples show how to use freemarker.core.Environment#getObjectWrapper() . 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: WrappingOptions.java    From scipio-erp with Apache License 2.0 6 votes vote down vote up
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());
}
 
Example 2
Source File: ObjectWrapperUtil.java    From scipio-erp with Apache License 2.0 6 votes vote down vote up
@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;
    }
}
 
Example 3
Source File: MultiVarMethod.java    From scipio-erp with Apache License 2.0 5 votes vote down vote up
public TemplateHashModelEx toMapModel(Environment env) {
    SimpleHash map = new SimpleHash(env.getObjectWrapper());
    map.put("ctxVars", ctxVars);
    map.put("globalCtxVars", globalCtxVars);
    map.put("reqAttribs", reqAttribs);
    return map;
}
 
Example 4
Source File: LangFtlUtil.java    From scipio-erp with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
    }
}
 
Example 5
Source File: LangFtlUtil.java    From scipio-erp with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
    }
}
 
Example 6
Source File: ObjectWrapperUtil.java    From scipio-erp with Apache License 2.0 5 votes vote down vote up
@Override
public ObjectWrapper getWrapper(Environment env) {
    ObjectWrapper wrapper = env.getObjectWrapper();
    // FIXME: Support for extended ScipioDefaultObjectWrapper: it is currently ditched by this code
    if (wrapper instanceof ScipioExtendedBeansWrapper && ((BeansWrapper) wrapper).isSimpleMapWrapper() == simpleMap) {
        return wrapper;
    } else {
        // semi-reliable way to get the language
        Object simpleEncoder = null;
        try {
            simpleEncoder = LangFtlUtil.unwrapOrNull(env.getGlobalVariable("simpleEncoder"));
        } catch (TemplateModelException e) {
            ;
        }
        if (simpleEncoder != null) {
            if (simpleEncoder instanceof UtilCodec.HtmlEncoder) { // heuristic
                return simpleMap ? defaultHtmlExtendedWrapper : defaultHtmlExtendedSimpleMapWrapper;
            } else {
                // TODO: REVIEW: what is this doing for non-html?? HtmlWidget was missing other lang support,
                // that's why not doing it here...
                return simpleMap ? FreeMarkerWorker.getDefaultOfbizWrapper() : FreeMarkerWorker.getDefaultOfbizSimpleMapWrapper();
            }
        } else {
            // FIXME: if we don't know the lang, we have to play it safe and use html
            return simpleMap ? defaultHtmlExtendedWrapper : defaultHtmlExtendedSimpleMapWrapper;
        }
    }
}
 
Example 7
Source File: ObjectWrapperUtil.java    From scipio-erp with Apache License 2.0 5 votes vote down vote up
public static ObjectWrapper getObjectWrapperByName(String wrapper, Environment env) throws TemplateModelException {
    if (wrapper == null || wrapper.isEmpty()) {
        return env.getObjectWrapper();
    } else {
        ObjectWrapperFetcher fetcher = objectWrapperFetcherMap.get(wrapper);
        if (fetcher != null) {
            return fetcher.getWrapper(env);
        } else {
            return null;
        }
    }
}
 
Example 8
Source File: FreeMarkerInlineRenderUtils.java    From rice with Educational Community License v2.0 5 votes vote down vote up
/**
 * Get the object wrapper from the FreeMarker environment, as a {@link BeansWrapper}.
 *
 * @param env The FreeMarker environment.
 * @return The object wrapper from the FreeMarker environment, type-cast as {@link BeansWrapper}
 * .
 */
public static BeansWrapper getBeansWrapper(Environment env) {
    ObjectWrapper wrapper = env.getObjectWrapper();

    if (!(wrapper instanceof BeansWrapper)) {
        throw new UnsupportedOperationException("FreeMarker environment uses unsupported ObjectWrapper " + wrapper);
    }

    return (BeansWrapper) wrapper;
}
 
Example 9
Source File: LangFtlUtil.java    From scipio-erp with Apache License 2.0 4 votes vote down vote up
/**
 * Gets current object wrapper, whatever it may be.
 */
public static ObjectWrapper getCurrentObjectWrapper(Environment env) {
    return env.getObjectWrapper();
}
 
Example 10
Source File: ObjectWrapperUtil.java    From scipio-erp with Apache License 2.0 4 votes vote down vote up
@Override
public ObjectWrapper getWrapper(Environment env) {
    return env.getObjectWrapper();
}