freemarker.template.utility.DeepUnwrap Java Examples

The following examples show how to use freemarker.template.utility.DeepUnwrap. 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: UpdateDocExtensionsListMojo.java    From camel-quarkus with Apache License 2.0 6 votes vote down vote up
@Override
public Object exec(List arguments) throws TemplateModelException {
    if (arguments.size() != 1) {
        throw new TemplateModelException("Expected one argument for getDocLink(); found " + arguments.size());
    }
    ArtifactModel<?> model = (ArtifactModel<?>) DeepUnwrap.unwrap((StringModel) arguments.get(0));
    final String artifactIdBase = CqUtils.getArtifactIdBase(model);
    final String extensionPageName = artifactIdBase + ".adoc";
    final Path extensionPagePath = extensionsDocPath.resolve(extensionPageName);
    if (!Files.exists(extensionPagePath)) {
        throw new IllegalStateException(
                "File " + extensionPagePath + " must exist to be able to refer to it from " + extensionListPath
                        + ".\nYou may need to add\n\n    org.apache.camel.quarkus:camel-quarkus-maven-plugin:update-extension-doc-page\n\nmojo in "
                        + artifactIdBase + " runtime module");
    }
    return "xref:extensions/" + extensionPageName;
}
 
Example #2
Source File: LangFtlUtil.java    From scipio-erp with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
    }
}
 
Example #3
Source File: FreeMarkerTag.java    From javalite with Apache License 2.0 5 votes vote down vote up
/**
 * Gets an object from context - by name.
 *
 * @param name name of object
 * @return object or null if not found.
 */
protected Object getUnwrapped(Object name) {
    try {
        return DeepUnwrap.unwrap(get(name));
    } catch (TemplateException e){
        throw new ViewException(e);
    }
}
 
Example #4
Source File: FreeMarkerArticleDirective.java    From cms with Apache License 2.0 5 votes vote down vote up
public void execute(Environment env,
        Map params, TemplateModel[] loopVars,
        TemplateDirectiveBody body)
        throws TemplateException, IOException 
{
    // Check if no parameters were given:
	if (body != null) throw new TemplateModelException("WBFreeMarkerArticleDirective does not suport directive body");
    
	String articleKeyStr = null;
	if (params.containsKey("externalKey"))
	{
		articleKeyStr = (String) DeepUnwrap.unwrap((TemplateModel) params.get("externalKey"));
	} else
	{
		throw new TemplateModelException("WBFreeMarkerArticleDirective does not have external key parameter set");
	}
	
    try
    {
    	WPBArticle article = cacheInstances.getArticleCache().getByExternalKey(articleKeyStr);
    	if (article == null)
    	{
    		throw new TemplateModelException("WBFreeMarkerArticleDirective externalKey does not match an existing Article : " + articleKeyStr);       
    	}
    	env.getOut().write(article.getHtmlSource());
    	
    } catch (WPBIOException e)
    {
    	log.log(Level.SEVERE, "ERROR: ", e);
    	throw new TemplateModelException("WBFreeMarkerArticleDirective IO exception when reading article: " + articleKeyStr);               	
    }
}
 
Example #5
Source File: RenderComponentDirective.java    From engine with GNU General Public License v3.0 5 votes vote down vote up
protected <T> T unwrap(String name, TemplateModel value, Class<T> expectedClass, Environment env) throws TemplateException {
    if (value != null) {
        Object unwrappedValue = DeepUnwrap.unwrap(value);
        try {
            return expectedClass.cast(unwrappedValue);
        } catch (ClassCastException e) {
            throw new TemplateException("Model value '" + name + "' of unexpected type: expected: " + expectedClass.getName() +
                                        ", actual: " + unwrappedValue.getClass().getName(), env);
        }
    } else {
        return null;
    }
}
 
Example #6
Source File: ExecuteControllerDirective.java    From engine with GNU General Public License v3.0 5 votes vote down vote up
public Object get(String varName) throws TemplateModelException {
    TemplateModel var = env.getVariable(varName);
    if (var != null) {
        return DeepUnwrap.unwrap(var);
    } else {
        return null;
    }
}
 
Example #7
Source File: ExecuteControllerDirective.java    From engine with GNU General Public License v3.0 5 votes vote down vote up
protected SiteItem getContentModel(Environment env) throws TemplateException {
    TemplateModel contentModel = env.getVariable(CrafterPageView.KEY_CONTENT_MODEL);
    if (contentModel != null) {
        Object unwrappedContentModel = DeepUnwrap.unwrap(contentModel);
        if (unwrappedContentModel instanceof SiteItem) {
            return (SiteItem)unwrappedContentModel;
        } else {
            throw new TemplateException("Variable '" + CrafterPageView.KEY_CONTENT_MODEL + " of unexpected type: expected: " +
                                        SiteItem.class.getName() + ", actual: " + unwrappedContentModel.getClass().getName(),
                                        env);
        }
    } else {
        return null;
    }
}
 
Example #8
Source File: ExecuteControllerDirective.java    From engine with GNU General Public License v3.0 5 votes vote down vote up
protected String getPath(TemplateModel pathParam, Environment env) throws TemplateException {
    Object unwrappedPath = DeepUnwrap.unwrap(pathParam);
    if (unwrappedPath instanceof String) {
        return (String)unwrappedPath;
    } else {
        throw new TemplateException("Param '" + PATH_PARAM_NAME + " of unexpected type: expected: " + String.class.getName() +
                                    ", actual: " + unwrappedPath.getClass().getName(), env);
    }
}
 
Example #9
Source File: LangFtlUtil.java    From scipio-erp with Apache License 2.0 5 votes vote down vote up
/**
 * 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() + ")");
    }
}
 
Example #10
Source File: LangFtlUtil.java    From scipio-erp with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
    }
}
 
Example #11
Source File: LangFtlUtil.java    From scipio-erp with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
    }
}
 
Example #12
Source File: LangFtlUtil.java    From scipio-erp with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
    }
}
 
Example #13
Source File: LangFtlUtil.java    From scipio-erp with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
    }
}
 
Example #14
Source File: LangFtlUtil.java    From scipio-erp with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
    }
}
 
Example #15
Source File: FreeMarkerUriDirective.java    From cms with Apache License 2.0 4 votes vote down vote up
public void execute(Environment env,
        Map params, TemplateModel[] loopVars,
        TemplateDirectiveBody body)
        throws TemplateException, IOException 
{
    String bodyStr = null;
    if (body != null)
    {
        StringWriter strWriter = new StringWriter();
        body.render(strWriter);
        bodyStr = strWriter.toString().trim();
    }
    String uriPattern = null;
    if (params.containsKey("uriPattern"))
    {
        uriPattern = (String) DeepUnwrap.unwrap((TemplateModel) params.get("uriPattern"));
    } 
            
    String uriFile = null;
    if (params.containsKey("uriFile"))
    {
        uriFile = (String) DeepUnwrap.unwrap((TemplateModel) params.get("uriFile"));
        if (uriFile.startsWith("/"))
        {
            uriFile = uriFile.substring(1);
        }
    }
    
    if (uriFile == null && uriPattern == null)
    {
        throw new TemplateModelException("FreeMarkerUriDirective does not have the uriPattern or uriFile parameter set");
    }
    // decide a priority for considering the uri. Low priority is the uriFile, medium is the uriPattern
    // higher is the bodyStr
    String uri = uriFile;
    if (uriPattern != null)
    {
        uri = uriPattern; 
    }
    if (body != null)
    {         
        uri = bodyStr;
    }

    
    try
    {
        if (uriFile != null)
        {
            WPBFile file = cacheInstances.getFilesCache().geByPath(uriFile);
            if (file != null && (file.getDirectoryFlag()!=1))
            {
                if (uri.indexOf("&")>0)
                {
                    uri = uri.concat("&");
                } else
                {
                    uri = uri.concat("?");
                }
                uri = uri.concat(cache_query_param).concat("=").concat(file.getHash().toString());
            }
        } else
        {
            WPBUri wpbUri = cacheInstances.getUriCache().get(uriPattern, WPBUrisCache.HTTP_GET_INDEX);
            if (wpbUri == null)
            {
                log.log(Level.WARNING, "FreeMarkerUriDirective could not found WPBUri for " + uriPattern);      
            }
            if (wpbUri.getResourceType() == WPBUri.RESOURCE_TYPE_TEXT)
            {
                WPBPage page = cacheInstances.getPageCache().getByExternalKey(wpbUri.getResourceExternalKey());
                if (page != null && (page.getIsTemplateSource() == null || page.getIsTemplateSource() == 0))
                {
                    if (uri.indexOf("&")>0)
                    {
                        uri = uri.concat("&");
                    } else
                    {
                        uri = uri.concat("?");
                    }
                    uri = uri.concat(cache_query_param).concat("=").concat(page.getHash().toString());
                }
            }
        }

        env.getOut().write(uri);
        
    } catch (WPBIOException e)
    {
        log.log(Level.SEVERE, "ERROR: ", e);
        throw new TemplateModelException("FreeMarkerUriDirective IO exception when handling: " + uriPattern);                   
    }
}
 
Example #16
Source File: DebugTag.java    From javalite with Apache License 2.0 4 votes vote down vote up
@Override
protected void render(Map params, String body, Writer writer) throws Exception {
    validateParamsPresence(params, "print");
    writer.write(params.get("print") == null ? "DebugTag: value 'print' is null!" :
            DeepUnwrap.unwrap((TemplateModel) params.get("print")).toString());
}
 
Example #17
Source File: LangFtlUtil.java    From scipio-erp with Apache License 2.0 3 votes vote down vote up
/**
 * 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
    }
}
 
Example #18
Source File: LangFtlUtil.java    From scipio-erp with Apache License 2.0 3 votes vote down vote up
/**
 * 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() + ")");
    }
}
 
Example #19
Source File: LangFtlUtil.java    From scipio-erp with Apache License 2.0 2 votes vote down vote up
/**
 * 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
}