All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.butterfaces.context.StringHtmlEncoder Maven / Gradle / Ivy

There is a newer version: 3.5.0
Show newest version
/*
 * Copyright Lars Michaelis and Stephan Zerhusen 2016.
 * Distributed under the MIT License.
 * (See accompanying file README.md file or copy at http://opensource.org/licenses/MIT)
 */
package org.butterfaces.context;

import org.butterfaces.util.StringUtils;
import org.butterfaces.util.StringUtils;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import java.io.IOException;

/**
 * Util class to use {@link FacesContext}s {@link javax.faces.context.ResponseWriter} to create a html string.
 *
 * @author Lars Michaelis
 */
public class StringHtmlEncoder {

    /**
     * Encodes complete component by calling {@link UIComponent#encodeAll(FacesContext)}.
     *
     * @param component component
     * @param context   {@link FacesContext}
     * @return the rendered string.
     * @throws IOException thrown by writer
     */
    public static String encodeComponent(final FacesContext context,
                                         final UIComponent component) throws IOException {
        final FacesContextStringResolverWrapper resolver = new FacesContextStringResolverWrapper(context);
        component.encodeAll(resolver);
        return resolver.getStringWriter().toString().replace("\n", "").trim();
    }

    /**
     * Encodes complete component by calling {@link UIComponent#encodeAll(FacesContext)}.
     *
     * @param component component
     * @param context   {@link FacesContext}
     * @return the rendered string.
     * @throws IOException thrown by writer
     */
    public static String encodeComponentWithSurroundingDiv(final FacesContext context,
                                                           final UIComponent component) throws IOException {
        return encodeComponentWithSurroundingDiv(context, component, null);
    }

    /**
     * Encodes complete component by calling {@link UIComponent#encodeAll(FacesContext)}.
     *
     * @param component  component
     * @param context    {@link FacesContext}
     * @param styleClass a div wrapper style class
     * @return the rendered string.
     * @throws IOException thrown by writer
     */
    public static String encodeComponentWithSurroundingDiv(final FacesContext context,
                                                           final UIComponent component,
                                                           final String styleClass) throws IOException {
        if (StringUtils.isNotEmpty(styleClass)) {
            return "
" + encodeComponent(context, component) + "
"; } return "
" + encodeComponent(context, component) + "
"; } /** * Encodes complete component by calling {@link UIComponent#encodeAll(FacesContext)}. Surrounds template with given * styleclass if template does not contains it. * * @param component component * @param context {@link FacesContext} * @param styleClass a div wrapper style class * @return the rendered string. * @throws IOException thrown by writer */ public static String encodeComponentWithSurroundingDivIfNecessary(final FacesContext context, final UIComponent component, final String styleClass) throws IOException { final String encodedFacet = encodeComponent(context, component); if (StringUtils.isNotEmpty(styleClass) && !encodedFacet.contains(styleClass)) { return "
" + encodedFacet + "
"; } return "
" + encodedFacet + "
"; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy