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