001/*
002 * All rights reserved. This program and the accompanying materials
003 * are made available under the terms of the GNU Lesser General Public License
004 * (LGPL) version 2.1 which accompanies this distribution, and is available at
005 * http://www.gnu.org/licenses/lgpl.html
006 *
007 * This library is distributed in the hope that it will be useful,
008 * but WITHOUT ANY WARRANTY; without even the implied warranty of
009 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
010 * Lesser General Public License for more details.
011 *
012 * Contributors:
013 *     Original file from org.jboss.seam.pdf.ui.UIHtmlText.java in jboss-seam-pdf
014 *     Anahide Tchertchian
015 */
016package org.nuxeo.ecm.platform.ui.web.component.seam;
017
018import java.io.IOException;
019import java.io.StringReader;
020import java.io.StringWriter;
021import java.util.HashMap;
022
023import javax.faces.context.FacesContext;
024import javax.faces.context.ResponseWriter;
025import javax.servlet.http.HttpServletRequest;
026
027import org.apache.commons.logging.Log;
028import org.apache.commons.logging.LogFactory;
029import org.jboss.seam.ui.util.JSF;
030
031import com.lowagie.text.html.simpleparser.HTMLWorker;
032import com.lowagie.text.html.simpleparser.StyleSheet;
033
034/**
035 * Overrides basic p:html tag to use {@link NuxeoITextImageProvider} to resolve image resources.
036 *
037 * @since 5.4.2
038 */
039public class UIHtmlText extends org.jboss.seam.pdf.ui.UIHtmlText {
040
041    private static final Log log = LogFactory.getLog(UIHtmlText.class);
042
043    @Override
044    public void encodeChildren(FacesContext context) throws IOException {
045        ResponseWriter writer = context.getResponseWriter();
046
047        StringWriter stringWriter = new StringWriter();
048        ResponseWriter cachingResponseWriter = writer.cloneWithWriter(stringWriter);
049        context.setResponseWriter(cachingResponseWriter);
050        JSF.renderChildren(context, this);
051        context.setResponseWriter(writer);
052
053        String output = stringWriter.getBuffer().toString();
054        addFromHtml(output, context);
055    }
056
057    @Override
058    public void encodeEnd(FacesContext context) throws IOException {
059        Object value = getValue();
060        if (value != null) {
061            addFromHtml(convert(context, value), context);
062        }
063        super.encodeEnd(context);
064    }
065
066    private void addFromHtml(String html, FacesContext context) {
067        HashMap<String, Object> interfaceProps = new HashMap<String, Object>();
068        interfaceProps.put("img_provider", new NuxeoITextImageProvider(
069                (HttpServletRequest) context.getExternalContext().getRequest()));
070
071        try {
072            for (Object o : HTMLWorker.parseToList(new StringReader(html), getStyle(), interfaceProps)) {
073                addToITextParent(o);
074            }
075        } catch (IOException e) {
076            // XXX avoid crash when rendering an image with resource not found
077            log.error("Error converting HTML to PDF", e);
078        }
079    }
080
081    /**
082     * XXX - this needs some work
083     */
084    private StyleSheet getStyle() {
085        StyleSheet styles = new StyleSheet();
086        styles.loadTagStyle("body", "leading", "16,0");
087        return styles;
088    }
089
090}