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

net.sf.jasperreports.engine.export.JRHtmlExporter Maven / Gradle / Ivy

There is a newer version: 6.21.3
Show newest version
/*
 * JasperReports - Free Java Reporting Library.
 * Copyright (C) 2001 - 2011 Jaspersoft Corporation. All rights reserved.
 * http://www.jaspersoft.com
 *
 * Unless you have purchased a commercial license agreement from Jaspersoft,
 * the following license terms apply:
 *
 * This program is part of JasperReports.
 *
 * JasperReports is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * JasperReports is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with JasperReports. If not, see .
 */

/*
 * Contributors:
 * Alex Parfenov - [email protected]
 * Adrian Jackson - [email protected]
 * David Taylor - [email protected]
 * Lars Kristensen - [email protected]
 */

package net.sf.jasperreports.engine.export;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.font.TextAttribute;
import java.awt.geom.Dimension2D;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.text.AttributedCharacterIterator;
import java.text.AttributedCharacterIterator.Attribute;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
import java.util.Locale;
import java.util.Map;

import net.sf.jasperreports.crosstabs.JRCellContents;
import net.sf.jasperreports.engine.DefaultJasperReportsContext;
import net.sf.jasperreports.engine.ImageMapRenderable;
import net.sf.jasperreports.engine.JRAbstractExporter;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JRGenericPrintElement;
import net.sf.jasperreports.engine.JRImageRenderer;
import net.sf.jasperreports.engine.JRLineBox;
import net.sf.jasperreports.engine.JRPen;
import net.sf.jasperreports.engine.JRPrintElement;
import net.sf.jasperreports.engine.JRPrintElementIndex;
import net.sf.jasperreports.engine.JRPrintEllipse;
import net.sf.jasperreports.engine.JRPrintFrame;
import net.sf.jasperreports.engine.JRPrintGraphicElement;
import net.sf.jasperreports.engine.JRPrintHyperlink;
import net.sf.jasperreports.engine.JRPrintHyperlinkParameter;
import net.sf.jasperreports.engine.JRPrintImage;
import net.sf.jasperreports.engine.JRPrintImageArea;
import net.sf.jasperreports.engine.JRPrintImageAreaHyperlink;
import net.sf.jasperreports.engine.JRPrintLine;
import net.sf.jasperreports.engine.JRPrintPage;
import net.sf.jasperreports.engine.JRPrintRectangle;
import net.sf.jasperreports.engine.JRPrintText;
import net.sf.jasperreports.engine.JRPropertiesUtil;
import net.sf.jasperreports.engine.JRRuntimeException;
import net.sf.jasperreports.engine.JRWrappingSvgRenderer;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReportsContext;
import net.sf.jasperreports.engine.Renderable;
import net.sf.jasperreports.engine.RenderableUtil;
import net.sf.jasperreports.engine.base.JRBasePrintFrame;
import net.sf.jasperreports.engine.fonts.FontFamily;
import net.sf.jasperreports.engine.fonts.FontInfo;
import net.sf.jasperreports.engine.fonts.FontUtil;
import net.sf.jasperreports.engine.type.HyperlinkTypeEnum;
import net.sf.jasperreports.engine.type.LineDirectionEnum;
import net.sf.jasperreports.engine.type.LineSpacingEnum;
import net.sf.jasperreports.engine.type.LineStyleEnum;
import net.sf.jasperreports.engine.type.ModeEnum;
import net.sf.jasperreports.engine.type.RenderableTypeEnum;
import net.sf.jasperreports.engine.type.RunDirectionEnum;
import net.sf.jasperreports.engine.type.ScaleImageEnum;
import net.sf.jasperreports.engine.util.JRColorUtil;
import net.sf.jasperreports.engine.util.JRStringUtil;
import net.sf.jasperreports.engine.util.JRStyledText;
import net.sf.jasperreports.engine.util.JRTextAttribute;
import net.sf.jasperreports.engine.util.Pair;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


/**
 * Exports a JasperReports document to HTML format. It has character output type and exports the document to a
 * grid-based layout.
 * 
 * @author Teodor Danciu ([email protected])
 * @version $Id: JRHtmlExporter.java 5749 2012-10-30 13:33:17Z narcism $
 */
public class JRHtmlExporter extends JRAbstractExporter
{
	
	private static final Log log = LogFactory.getLog(JRHtmlExporter.class);

	private static final String HTML_EXPORTER_PROPERTIES_PREFIX = JRPropertiesUtil.PROPERTY_PREFIX + "export.html.";

	/**
	 * The exporter key, as used in
	 * {@link GenericElementHandlerEnviroment#getHandler(net.sf.jasperreports.engine.JRGenericElementType, String)}.
	 */
	public static final String HTML_EXPORTER_KEY = JRPropertiesUtil.PROPERTY_PREFIX + "html";
	
	/**
	 *
	 */
	public static final String PROPERTY_HTML_CLASS = HTML_EXPORTER_PROPERTIES_PREFIX + "class";

	/**
	 *
	 */
	public static final String PROPERTY_HTML_POPUP_ID = HTML_EXPORTER_PROPERTIES_PREFIX + "popupid";

	/**
	 *
	 */
	public static final String PROPERTY_HTML_POPUP_COLUMN = HTML_EXPORTER_PROPERTIES_PREFIX + "popupcolumn";

	/**
	 *
	 */
	public static final String PROPERTY_HTML_UUID = HTML_EXPORTER_PROPERTIES_PREFIX + "uuid";

	/**
	 *
	 */
	public static final String PROPERTY_HTML_ID = HTML_EXPORTER_PROPERTIES_PREFIX + "id";

	/**
	 * Configuration property that determines the exporter to produce accessible HTML.
	 */
	public static final String PROPERTY_ACCESSIBLE = HTML_EXPORTER_PROPERTIES_PREFIX + "accessible";
	
	/**
	 *
	 */
	protected static final String JR_PAGE_ANCHOR_PREFIX = "JR_PAGE_ANCHOR_";

	protected static final float DEFAULT_ZOOM = 1f;

	/**
	 *
	 */
	protected static final String CSS_TEXT_ALIGN_LEFT = "left";
	protected static final String CSS_TEXT_ALIGN_RIGHT = "right";
	protected static final String CSS_TEXT_ALIGN_CENTER = "center";
	protected static final String CSS_TEXT_ALIGN_JUSTIFY = "justify";

	/**
	 *
	 */
	protected static final String HTML_VERTICAL_ALIGN_TOP = "top";
	protected static final String HTML_VERTICAL_ALIGN_MIDDLE = "middle";
	protected static final String HTML_VERTICAL_ALIGN_BOTTOM = "bottom";
	
	public static final String IMAGE_NAME_PREFIX = "img_";
	protected static final int IMAGE_NAME_PREFIX_LEGTH = IMAGE_NAME_PREFIX.length();

	protected class ExporterContext extends BaseExporterContext implements JRHtmlExporterContext
	{
		public String getExportPropertiesPrefix()
		{
			return HTML_EXPORTER_PROPERTIES_PREFIX;
		}

		public String getHyperlinkURL(JRPrintHyperlink link)
		{
			return JRHtmlExporter.this.getHyperlinkURL(link);
		}
	}

	/**
	 *
	 */
	protected Writer writer;
	protected JRExportProgressMonitor progressMonitor;
	protected Map rendererToImagePathMap;
	protected Map,String> imageMaps;
	protected Map imageNameToImageDataMap;
	protected List imagesToProcess;
	protected boolean isPxImageLoaded;

	protected int reportIndex;
	protected int pageIndex;

	/**
	 *
	 */
	protected File imagesDir;
	protected String imagesURI;
	protected boolean isOutputImagesToDir;
	protected boolean isRemoveEmptySpace;
	protected boolean isWhitePageBackground;
	protected String encoding;
	protected String sizeUnit;
	protected float zoom = DEFAULT_ZOOM;
	protected boolean isUsingImagesToAlign;
	protected boolean isWrapBreakWord;
	protected boolean isIgnorePageMargins;
	protected boolean accessibleHtml;

	protected boolean flushOutput;

	/**
	 *
	 */
	protected String htmlHeader;
	protected String betweenPagesHtml;
	protected String htmlFooter;

	protected StringProvider emptyCellStringProvider;

	private LinkedList backcolorStack = new LinkedList();
	private Color backcolor;

	protected JRHyperlinkTargetProducerFactory targetProducerFactory;		

	protected boolean hyperlinkStarted;
	protected int thDepth;
	
	protected ExporterNature nature;

	protected JRHtmlExporterContext exporterContext = new ExporterContext();

	/**
	 * @see #JRHtmlExporter(JasperReportsContext)
	 */
	public JRHtmlExporter()
	{
		this(DefaultJasperReportsContext.getInstance());
	}


	/**
	 *
	 */
	public JRHtmlExporter(JasperReportsContext jasperReportsContext)
	{
		super(jasperReportsContext);

		targetProducerFactory = new DefaultHyperlinkTargetProducerFactory(jasperReportsContext);		
	}


	/**
	 *
	 */
	public void exportReport() throws JRException
	{
		progressMonitor = (JRExportProgressMonitor)parameters.get(JRExporterParameter.PROGRESS_MONITOR);

		/*   */
		setOffset();

		try
		{
			/*   */
			setExportContext();
	
			/*   */
			setInput();
			
			if (!parameters.containsKey(JRExporterParameter.FILTER))
			{
				filter = createFilter(HTML_EXPORTER_PROPERTIES_PREFIX);
			}

			/*   */
			if (!isModeBatch)
			{
				setPageRange();
			}
	
			htmlHeader = (String)parameters.get(JRHtmlExporterParameter.HTML_HEADER);
			betweenPagesHtml = (String)parameters.get(JRHtmlExporterParameter.BETWEEN_PAGES_HTML);
			htmlFooter = (String)parameters.get(JRHtmlExporterParameter.HTML_FOOTER);
	
			imagesDir = (File)parameters.get(JRHtmlExporterParameter.IMAGES_DIR);
			if (imagesDir == null)
			{
				String dir = (String)parameters.get(JRHtmlExporterParameter.IMAGES_DIR_NAME);
				if (dir != null)
				{
					imagesDir = new File(dir);
				}
			}
	
			isRemoveEmptySpace = 
				getBooleanParameter(
					JRHtmlExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS,
					JRHtmlExporterParameter.PROPERTY_REMOVE_EMPTY_SPACE_BETWEEN_ROWS,
					false
					);
	
			isWhitePageBackground = 
				getBooleanParameter(
					JRHtmlExporterParameter.IS_WHITE_PAGE_BACKGROUND,
					JRHtmlExporterParameter.PROPERTY_WHITE_PAGE_BACKGROUND,
					true
					);
	
			Boolean isOutputImagesToDirParameter = (Boolean)parameters.get(JRHtmlExporterParameter.IS_OUTPUT_IMAGES_TO_DIR);
			if (isOutputImagesToDirParameter != null)
			{
				isOutputImagesToDir = isOutputImagesToDirParameter.booleanValue();
			}
	
			String uri = (String)parameters.get(JRHtmlExporterParameter.IMAGES_URI);
			if (uri != null)
			{
				imagesURI = uri;
			}
	
			encoding = 
				getStringParameterOrDefault(
					JRExporterParameter.CHARACTER_ENCODING, 
					JRExporterParameter.PROPERTY_CHARACTER_ENCODING
					);
	
			rendererToImagePathMap = new HashMap();
			imageMaps = new HashMap,String>();
			imagesToProcess = new ArrayList();
			isPxImageLoaded = false;
	
			//backward compatibility with the IMAGE_MAP parameter
			imageNameToImageDataMap = (Map)parameters.get(JRHtmlExporterParameter.IMAGES_MAP);
	//		if (imageNameToImageDataMap == null)
	//		{
	//			imageNameToImageDataMap = new HashMap();
	//		}
			//END - backward compatibility with the IMAGE_MAP parameter
	
			isWrapBreakWord = 
				getBooleanParameter(
					JRHtmlExporterParameter.IS_WRAP_BREAK_WORD,
					JRHtmlExporterParameter.PROPERTY_WRAP_BREAK_WORD,
					false
					);
	
			sizeUnit = 
				getStringParameterOrDefault(
					JRHtmlExporterParameter.SIZE_UNIT,
					JRHtmlExporterParameter.PROPERTY_SIZE_UNIT
					);
	
			Float zoomRatio = (Float)parameters.get(JRHtmlExporterParameter.ZOOM_RATIO);
			if (zoomRatio != null)
			{
				zoom = zoomRatio.floatValue();
				if (zoom <= 0)
				{
					throw new JRException("Invalid zoom ratio : " + zoom);
				}
			}
			else
			{
				zoom = DEFAULT_ZOOM;
			}
	
			isUsingImagesToAlign = 
				getBooleanParameter(
					JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN,
					JRHtmlExporterParameter.PROPERTY_USING_IMAGES_TO_ALIGN,
					true
					);
		
			if (isUsingImagesToAlign)
			{
				emptyCellStringProvider =
					new StringProvider()
					{
						public String getStringForCollapsedTD(Object value, int width, int height)
						{
							return ">\"\"";
						}
						public String getStringForEmptyTD(Object value)
						{
							return "\"\"";
						}
						
						public String getReportTableStyle()
						{
							return null;
						}
					};

				loadPxImage();
			}
			else
			{
				emptyCellStringProvider =
					new StringProvider()
					{
						public String getStringForCollapsedTD(Object value, int width, int height)
						{
							return " style=\"width: " + toSizeUnit(width) + "; height: " + toSizeUnit(height) + ";\">";
						}
						public String getStringForEmptyTD(Object value)
						{
							return "";
						}
						
						public String getReportTableStyle()
						{
							// required for lines and rectangles, but doesn't work in IE
							// border-collapse: collapse seems to take care of this though
							return "empty-cells: show";
						}
					};
			}
			
			isIgnorePageMargins = 
				getBooleanParameter(
					JRExporterParameter.IGNORE_PAGE_MARGINS,
					JRExporterParameter.PROPERTY_IGNORE_PAGE_MARGINS,
					false
					);
			
			accessibleHtml = 
				getPropertiesUtil().getBooleanProperty(
					jasperPrint,
					PROPERTY_ACCESSIBLE,
					false
					);
			
			setFontMap();
						
			setHyperlinkProducerFactory();

			//FIXMENOW check all exporter properties that are supposed to work at report level
			boolean deepGrid = 
				!getBooleanParameter(
					JRHtmlExporterParameter.FRAMES_AS_NESTED_TABLES,
					JRHtmlExporterParameter.PROPERTY_FRAMES_AS_NESTED_TABLES,
					true
					);
			
			nature = new JRHtmlExporterNature(jasperReportsContext, filter, deepGrid, isIgnorePageMargins);
	
			flushOutput = getBooleanParameter(JRHtmlExporterParameter.FLUSH_OUTPUT, 
					JRHtmlExporterParameter.PROPERTY_FLUSH_OUTPUT, 
					true);
			
			StringBuffer sb = (StringBuffer)parameters.get(JRExporterParameter.OUTPUT_STRING_BUFFER);
			if (sb != null)
			{
				try
				{
					writer = new StringWriter();
					exportReportToWriter();
					sb.append(writer.toString());
				}
				catch (IOException e)
				{
					throw new JRException("Error writing to StringBuffer writer : " + jasperPrint.getName(), e);
				}
				finally
				{
					if (writer != null)
					{
						try
						{
							writer.close();
						}
						catch(IOException e)
						{
						}
					}
				}
			}
			else
			{
				writer = (Writer)parameters.get(JRExporterParameter.OUTPUT_WRITER);
				if (writer != null)
				{
					try
					{
						exportReportToWriter();
					}
					catch (IOException e)
					{
						throw new JRException("Error writing to writer : " + jasperPrint.getName(), e);
					}
				}
				else
				{
					OutputStream os = (OutputStream)parameters.get(JRExporterParameter.OUTPUT_STREAM);
					if (os != null)
					{
						try
						{
							writer = new OutputStreamWriter(os, encoding);
							exportReportToWriter();
						}
						catch (IOException e)
						{
							throw new JRException("Error writing to OutputStream writer : " + jasperPrint.getName(), e);
						}
					}
					else
					{
						File destFile = (File)parameters.get(JRExporterParameter.OUTPUT_FILE);
						if (destFile == null)
						{
							String fileName = (String)parameters.get(JRExporterParameter.OUTPUT_FILE_NAME);
							if (fileName != null)
							{
								destFile = new File(fileName);
							}
							else
							{
								throw new JRException("No output specified for the exporter.");
							}
						}
	
						try
						{
							os = new FileOutputStream(destFile);
							writer = new OutputStreamWriter(os, encoding);
						}
						catch (IOException e)
						{
							throw new JRException("Error creating to file writer : " + jasperPrint.getName(), e);
						}
	
						if (imagesDir == null)
						{
							imagesDir = new File(destFile.getParent(), destFile.getName() + "_files");
						}
	
						if (isOutputImagesToDirParameter == null)
						{
							isOutputImagesToDir = true;
						}
	
						if (imagesURI == null)
						{
							imagesURI = imagesDir.getName() + "/";
						}
	
						try
						{
							exportReportToWriter();
						}
						catch (IOException e)
						{
							throw new JRException("Error writing to file writer : " + jasperPrint.getName(), e);
						}
						finally
						{
							if (writer != null)
							{
								try
								{
									writer.close();
								}
								catch(IOException e)
								{
								}
							}
						}
					}
				}
			}
	
			if (isOutputImagesToDir)
			{
				if (imagesDir == null)
				{
					throw new JRException("The images directory was not specified for the exporter.");
				}
	
				if (isPxImageLoaded || (imagesToProcess != null && imagesToProcess.size() > 0))
				{
					if (!imagesDir.exists())
					{
						imagesDir.mkdir();
					}
	
					if (isPxImageLoaded)
					{
						Renderable pxRenderer =
							RenderableUtil.getInstance(jasperReportsContext).getRenderable("net/sf/jasperreports/engine/images/pixel.GIF");
						byte[] imageData = pxRenderer.getImageData(jasperReportsContext);
	
						File imageFile = new File(imagesDir, "px");
						FileOutputStream fos = null;
	
						try
						{
							fos = new FileOutputStream(imageFile);
							fos.write(imageData, 0, imageData.length);
						}
						catch (IOException e)
						{
							throw new JRException("Error writing to image file : " + imageFile, e);
						}
						finally
						{
							if (fos != null)
							{
								try
								{
									fos.close();
								}
								catch(IOException e)
								{
								}
							}
						}
					}
	
					for(Iterator it = imagesToProcess.iterator(); it.hasNext();)
					{
						JRPrintElementIndex imageIndex = it.next();
	
						JRPrintImage image = getImage(jasperPrintList, imageIndex);
						Renderable renderer = image.getRenderable();
						if (renderer.getTypeValue() == RenderableTypeEnum.SVG)
						{
							renderer =
								new JRWrappingSvgRenderer(
									renderer,
									new Dimension(image.getWidth(), image.getHeight()),
									ModeEnum.OPAQUE == image.getModeValue() ? image.getBackcolor() : null
									);
						}
	
						byte[] imageData = renderer.getImageData(jasperReportsContext);
	
						File imageFile = new File(imagesDir, getImageName(imageIndex));
						FileOutputStream fos = null;
	
						try
						{
							fos = new FileOutputStream(imageFile);
							fos.write(imageData, 0, imageData.length);
						}
						catch (IOException e)
						{
							throw new JRException("Error writing to image file : " + imageFile, e);
						}
						finally
						{
							if (fos != null)
							{
								try
								{
									fos.close();
								}
								catch(IOException e)
								{
								}
							}
						}
					}
				}
			}
		}
		finally
		{
			resetExportContext();
		}
	}


	public static JRPrintImage getImage(List jasperPrintList, String imageName)
	{
		return getImage(jasperPrintList, getPrintElementIndex(imageName));
	}


	public static JRPrintImage getImage(List jasperPrintList, JRPrintElementIndex imageIndex)
	{
		JasperPrint report = jasperPrintList.get(imageIndex.getReportIndex());
		JRPrintPage page = report.getPages().get(imageIndex.getPageIndex());

		Integer[] elementIndexes = imageIndex.getAddressArray();
		Object element = page.getElements().get(elementIndexes[0].intValue());

		for (int i = 1; i < elementIndexes.length; ++i)
		{
			JRPrintFrame frame = (JRPrintFrame) element;
			element = frame.getElements().get(elementIndexes[i].intValue());
		}

		return (JRPrintImage) element;
	}


	/**
	 *
	 */
	protected void exportReportToWriter() throws JRException, IOException
	{
		if (htmlHeader == null)
		{
			// no doctype because of bug 1430880
//			writer.write("\n");
//			writer.write("\n");
			writer.write("\n");
			writer.write("\n");
			writer.write("  \n");
			writer.write("  \n");
			writer.write("  \n");
			writer.write("\n");
			writer.write("\n");
			writer.write("\n");
			writer.write("\n");
			writer.write("
 \n"); writer.write("\n"); } else { writer.write(htmlHeader); } for(reportIndex = 0; reportIndex < jasperPrintList.size(); reportIndex++) { setJasperPrint(jasperPrintList.get(reportIndex)); List pages = jasperPrint.getPages(); if (pages != null && pages.size() > 0) { if (isModeBatch) { startPageIndex = 0; endPageIndex = pages.size() - 1; } JRPrintPage page = null; for(pageIndex = startPageIndex; pageIndex <= endPageIndex; pageIndex++) { if (Thread.interrupted()) { throw new JRException("Current thread interrupted."); } page = pages.get(pageIndex); writer.write("\n"); /* */ exportPage(page); if (reportIndex < jasperPrintList.size() - 1 || pageIndex < endPageIndex) { if (betweenPagesHtml == null) { writer.write("
\n
\n"); } else { writer.write(betweenPagesHtml); } } writer.write("\n"); } } } if (htmlFooter == null) { writer.write("
 
\n"); writer.write("\n"); writer.write("\n"); } else { writer.write(htmlFooter); } if (flushOutput) { writer.flush(); } } /** * */ protected void exportPage(JRPrintPage page) throws JRException, IOException { List elements = null; if (accessibleHtml) { JRBasePrintFrame frame = new JRBasePrintFrame(jasperPrint.getDefaultStyleProvider()); new JRHtmlExporterHelper(jasperPrint).createNestedFrames(page.getElements().listIterator(), frame); elements = frame.getElements(); } else { elements = page.getElements(); } JRGridLayout layout = new JRGridLayout( nature, elements, jasperPrint.getPageWidth(), jasperPrint.getPageHeight(), globalOffsetX, globalOffsetY, null //address ); exportGrid(layout, isWhitePageBackground); if (progressMonitor != null) { progressMonitor.afterPageExport(); } } /** * */ protected void exportGrid(JRGridLayout gridLayout, boolean whitePageBackground) throws IOException, JRException { CutsInfo xCuts = gridLayout.getXCuts(); JRExporterGridCell[][] grid = gridLayout.getGrid(); String tableStyle = "width: " + toSizeUnit(gridLayout.getWidth()) + "; border-collapse: collapse"; String additionalTableStyle = emptyCellStringProvider.getReportTableStyle(); if (additionalTableStyle != null) { tableStyle += "; " + additionalTableStyle; } writer.write("\n"); if (whitePageBackground) { setBackcolor(Color.white); } writer.write("\n"); int width = 0; for(int i = 1; i < xCuts.size(); i++) { width = xCuts.getCutOffset(i) - xCuts.getCutOffset(i - 1); writer.write(" \n"); } writer.write("\n"); thDepth = 0; for(int y = 0; y < grid.length; y++) { if (gridLayout.getYCuts().isCutSpanned(y) || !isRemoveEmptySpace) { JRExporterGridCell[] gridRow = grid[y]; int rowHeight = JRGridLayout.getRowHeight(gridRow); boolean hasEmptyCell = hasEmptyCell(gridRow); writer.write("\n"); for(int x = 0; x < gridRow.length; x++) { JRExporterGridCell gridCell = gridRow[x]; if(gridCell.getWrapper() == null) { writeEmptyCell(gridCell, rowHeight); } else { JRPrintElement element = gridCell.getWrapper().getElement(); String thTag = null; if ( element != null && element.hasProperties() ) { thTag = element.getPropertiesMap().getProperty(JRPdfExporterTagHelper.PROPERTY_TAG_TH); } if (thTag != null && (JRPdfExporterTagHelper.TAG_START.equals(thTag) || JRPdfExporterTagHelper.TAG_FULL.equals(thTag))) { thDepth++; } if (element instanceof JRPrintLine) { exportLine((JRPrintLine)element, gridCell); } else if (element instanceof JRPrintRectangle) { exportRectangle((JRPrintRectangle)element, gridCell); } else if (element instanceof JRPrintEllipse) { exportRectangle((JRPrintEllipse)element, gridCell); } else if (element instanceof JRPrintImage) { exportImage((JRPrintImage)element, gridCell); } else if (element instanceof JRPrintText) { exportText((JRPrintText)element, gridCell); } else if (element instanceof JRPrintFrame) { exportFrame((JRPrintFrame) element, gridCell); } else if (element instanceof JRGenericPrintElement) { exportGenericElement((JRGenericPrintElement) element, gridCell, rowHeight); } if (thTag != null && (JRPdfExporterTagHelper.TAG_END.equals(thTag) || JRPdfExporterTagHelper.TAG_FULL.equals(thTag))) { thDepth--; } } x += gridCell.getColSpan() - 1; } writer.write("\n"); } } if (whitePageBackground) { restoreBackcolor(); } writer.write("
\n"); } private boolean hasEmptyCell(JRExporterGridCell[] gridRow) { if (gridRow[0].getWrapper() == null) // quick exit { return true; } boolean hasEmptyCell = false; for(int x = 1; x < gridRow.length; x++) { if (gridRow[x].getWrapper() == null) { hasEmptyCell = true; break; } } return hasEmptyCell; } protected void writeEmptyCell(JRExporterGridCell cell, int rowHeight) throws IOException { String cellTag = getCellTag(cell); writer.write(" <" + cellTag); if (cell.getColSpan() > 1) { writer.write(" colspan=\"" + cell.getColSpan() + "\""); } StringBuffer styleBuffer = new StringBuffer(); appendBackcolorStyle(cell, styleBuffer); appendBorderStyle(cell.getBox(), styleBuffer); if (styleBuffer.length() > 0) { writer.write(" style=\""); writer.write(styleBuffer.toString()); writer.write("\""); } writer.write(emptyCellStringProvider.getStringForCollapsedTD(imagesURI, cell.getWidth(), rowHeight)); writer.write("\n"); } /** * */ protected void exportLine(JRPrintLine line, JRExporterGridCell gridCell) throws IOException { writeCellStart(gridCell); StringBuffer styleBuffer = new StringBuffer(); appendBackcolorStyle(gridCell, styleBuffer); String side = null; float ratio = line.getWidth() / line.getHeight(); if (ratio > 1) { if (line.getDirectionValue() == LineDirectionEnum.TOP_DOWN) { side = "top"; } else { side = "bottom"; } } else { if (line.getDirectionValue() == LineDirectionEnum.TOP_DOWN) { side = "left"; } else { side = "right"; } } appendPen( styleBuffer, line.getLinePen(), side ); if (styleBuffer.length() > 0) { writer.write(" style=\""); writer.write(styleBuffer.toString()); writer.write("\""); } writer.write(">"); writer.write(emptyCellStringProvider.getStringForEmptyTD(imagesURI)); writeCellEnd(gridCell); } /** * */ protected void writeCellStart(JRExporterGridCell gridCell) throws IOException { writer.write(" <" + getCellTag(gridCell)); if (gridCell.getColSpan() > 1) { writer.write(" colspan=\"" + gridCell.getColSpan() +"\""); } if (gridCell.getRowSpan() > 1) { writer.write(" rowspan=\"" + gridCell.getRowSpan() + "\""); } if (gridCell.getWrapper() != null) { JRPrintElement element = gridCell.getWrapper().getElement(); if (element != null) { String id = getPropertiesUtil().getProperty(element, PROPERTY_HTML_ID); if (id != null) { writer.write(" id=\"" + id +"\""); } String clazz = getPropertiesUtil().getProperty(element, PROPERTY_HTML_CLASS); if (clazz != null) { writer.write(" class=\"" + clazz +"\""); } } } } /** * */ protected void writeCellEnd(JRExporterGridCell gridCell) throws IOException { writer.write("\n"); } /** * */ protected String getCellTag(JRExporterGridCell gridCell) { if (accessibleHtml) { if (thDepth > 0) { return "th"; //FIXMEHTML th tags have center alignment by default } else { ElementWrapper wrapper = gridCell.getWrapper(); OccupiedGridCell occupiedCell = gridCell instanceof OccupiedGridCell ? (OccupiedGridCell)gridCell : null; if (occupiedCell != null) { wrapper = occupiedCell.getOccupier().getWrapper(); } if (wrapper != null) { String cellContentsType = wrapper.getProperty(JRCellContents.PROPERTY_TYPE); if ( JRCellContents.TYPE_CROSSTAB_HEADER.equals(cellContentsType) || JRCellContents.TYPE_COLUMN_HEADER.equals(cellContentsType) || JRCellContents.TYPE_ROW_HEADER.equals(cellContentsType) ) { return "th"; } } } } return "td"; } /** * */ protected void exportRectangle(JRPrintGraphicElement element, JRExporterGridCell gridCell) throws IOException { writeCellStart(gridCell); StringBuffer styleBuffer = new StringBuffer(); appendBackcolorStyle(gridCell, styleBuffer); appendPen( styleBuffer, element.getLinePen(), null ); if (styleBuffer.length() > 0) { writer.write(" style=\""); writer.write(styleBuffer.toString()); writer.write("\""); } writer.write(">"); writer.write(emptyCellStringProvider.getStringForEmptyTD(imagesURI)); writeCellEnd(gridCell); } /** * */ protected void exportStyledText(JRStyledText styledText, Locale locale) throws IOException { exportStyledText(styledText, null, locale, null, null); } /** * */ protected void exportStyledText(JRStyledText styledText, String tooltip, Locale locale, LineSpacingEnum lineSpacing, Float lineSpacingSize) throws IOException { String text = styledText.getText(); int runLimit = 0; AttributedCharacterIterator iterator = styledText.getAttributedString().getIterator(); boolean first = true; boolean startedSpan = false; while(runLimit < styledText.length() && (runLimit = iterator.getRunLimit()) <= styledText.length()) { //if there are several text runs, write the tooltip into a parent if (first && runLimit < styledText.length() && tooltip != null) { startedSpan = true; writer.write(""); //reset the tooltip so that inner s to not use it tooltip = null; } first = false; exportStyledTextRun( iterator.getAttributes(), text.substring(iterator.getIndex(), runLimit), tooltip, locale, lineSpacing, lineSpacingSize ); iterator.setIndex(runLimit); } if (startedSpan) { writer.write(""); } } /** * */ protected void exportStyledTextRun(Map attributes, String text, Locale locale) throws IOException { exportStyledTextRun(attributes, text, null, locale, null, null); } /** * */ protected void exportStyledTextRun( Map attributes, String text, String tooltip, Locale locale, LineSpacingEnum lineSpacing, Float lineSpacingSize ) throws IOException { String fontFamilyAttr = (String)attributes.get(TextAttribute.FAMILY); String fontFamily = fontFamilyAttr; if (fontMap != null && fontMap.containsKey(fontFamilyAttr)) { fontFamily = fontMap.get(fontFamilyAttr); } else { FontInfo fontInfo = FontUtil.getInstance(jasperReportsContext).getFontInfo(fontFamilyAttr, locale); if (fontInfo != null) { //fontName found in font extensions FontFamily family = fontInfo.getFontFamily(); String exportFont = family.getExportFont(getExporterKey()); if (exportFont != null) { fontFamily = exportFont; } } } boolean localHyperlink = false; JRPrintHyperlink hyperlink = (JRPrintHyperlink)attributes.get(JRTextAttribute.HYPERLINK); if (!hyperlinkStarted && hyperlink != null) { startHyperlink(hyperlink); localHyperlink = true; } writer.write(""); writer.write( JRStringUtil.htmlEncode(text) ); writer.write(""); if (localHyperlink) { endHyperlink(); } } /** * */ protected void exportText(JRPrintText text, JRExporterGridCell gridCell) throws IOException { JRStyledText styledText = getStyledText(text); int textLength = 0; if (styledText != null) { textLength = styledText.length(); } writeCellStart(gridCell);//FIXME why dealing with cell style if no text to print (textLength == 0)? if (text.getRunDirectionValue() == RunDirectionEnum.RTL) { writer.write(" dir=\"rtl\""); } StringBuffer styleBuffer = new StringBuffer(); String verticalAlignment = HTML_VERTICAL_ALIGN_TOP; switch (text.getVerticalAlignmentValue()) { case BOTTOM : { verticalAlignment = HTML_VERTICAL_ALIGN_BOTTOM; break; } case MIDDLE : { verticalAlignment = HTML_VERTICAL_ALIGN_MIDDLE; break; } case TOP : default : { verticalAlignment = HTML_VERTICAL_ALIGN_TOP; } } if (!verticalAlignment.equals(HTML_VERTICAL_ALIGN_TOP)) { styleBuffer.append(" vertical-align: "); styleBuffer.append(verticalAlignment); styleBuffer.append(";"); } appendBackcolorStyle(gridCell, styleBuffer); appendBorderStyle(gridCell.getBox(), styleBuffer); appendPaddingStyle(text.getLineBox(), styleBuffer); String horizontalAlignment = CSS_TEXT_ALIGN_LEFT; if (textLength > 0) { switch (text.getHorizontalAlignmentValue()) { case RIGHT : { horizontalAlignment = CSS_TEXT_ALIGN_RIGHT; break; } case CENTER : { horizontalAlignment = CSS_TEXT_ALIGN_CENTER; break; } case JUSTIFIED : { horizontalAlignment = CSS_TEXT_ALIGN_JUSTIFY; break; } case LEFT : default : { horizontalAlignment = CSS_TEXT_ALIGN_LEFT; } } if ( (text.getRunDirectionValue() == RunDirectionEnum.LTR && !horizontalAlignment.equals(CSS_TEXT_ALIGN_LEFT)) || (text.getRunDirectionValue() == RunDirectionEnum.RTL && !horizontalAlignment.equals(CSS_TEXT_ALIGN_RIGHT)) ) { styleBuffer.append("text-align: "); styleBuffer.append(horizontalAlignment); styleBuffer.append(";"); } } if (isWrapBreakWord) { styleBuffer.append("width: " + toSizeUnit(gridCell.getWidth()) + "; "); styleBuffer.append("word-wrap: break-word; "); } if (text.getLineBreakOffsets() != null) { //if we have line breaks saved in the text, set nowrap so that //the text only wraps at the explicit positions styleBuffer.append("white-space: nowrap; "); } if (styleBuffer.length() > 0) { writer.write(" style=\""); writer.write(styleBuffer.toString()); writer.write("\""); } writer.write(">"); writer.write("

"); if (text.getAnchorName() != null) { writer.write(""); } startHyperlink(text); if (textLength > 0) { //only use text tooltip when no hyperlink present String textTooltip = hyperlinkStarted ? null : text.getHyperlinkTooltip(); exportStyledText(styledText, textTooltip, getTextLocale(text), text.getParagraph().getLineSpacing(), text.getParagraph().getLineSpacingSize()); } else { writer.write(emptyCellStringProvider.getStringForEmptyTD(imagesURI)); } endHyperlink(); writer.write("

"); writeCellEnd(gridCell); } protected boolean startHyperlink(JRPrintHyperlink link) throws IOException { String href = getHyperlinkURL(link); if (href != null) { writer.write(""); } hyperlinkStarted = href != null; return hyperlinkStarted; } protected String getHyperlinkTarget(JRPrintHyperlink link) { String target = null; JRHyperlinkTargetProducer producer = targetProducerFactory.getHyperlinkTargetProducer(link.getLinkTarget()); if (producer == null) { switch(link.getHyperlinkTargetValue()) { case BLANK : { target = "_blank";//FIXME make reverse for html markup hyperlinks break; } case PARENT : { target = "_parent"; break; } case TOP : { target = "_top"; break; } case CUSTOM : { boolean paramFound = false; List parameters = link.getHyperlinkParameters() == null ? null : link.getHyperlinkParameters().getParameters(); if (parameters != null) { for(Iterator it = parameters.iterator(); it.hasNext();) { JRPrintHyperlinkParameter parameter = it.next(); if (link.getLinkTarget().equals(parameter.getName())) { target = parameter.getValue() == null ? null : parameter.getValue().toString(); paramFound = true; break; } } } if (!paramFound) { target = link.getLinkTarget(); } break; } case SELF : default : { } } } else { target = producer.getHyperlinkTarget(link); } return target; } protected String getHyperlinkURL(JRPrintHyperlink link) { String href = null; JRHyperlinkProducer customHandler = getHyperlinkProducer(link); if (customHandler == null) { switch(link.getHyperlinkTypeValue()) { case REFERENCE : { if (link.getHyperlinkReference() != null) { href = link.getHyperlinkReference(); } break; } case LOCAL_ANCHOR : { if (link.getHyperlinkAnchor() != null) { href = "#" + link.getHyperlinkAnchor(); } break; } case LOCAL_PAGE : { if (link.getHyperlinkPage() != null) { href = "#" + JR_PAGE_ANCHOR_PREFIX + reportIndex + "_" + link.getHyperlinkPage().toString(); } break; } case REMOTE_ANCHOR : { if ( link.getHyperlinkReference() != null && link.getHyperlinkAnchor() != null ) { href = link.getHyperlinkReference() + "#" + link.getHyperlinkAnchor(); } break; } case REMOTE_PAGE : { if ( link.getHyperlinkReference() != null && link.getHyperlinkPage() != null ) { href = link.getHyperlinkReference() + "#" + JR_PAGE_ANCHOR_PREFIX + "0_" + link.getHyperlinkPage().toString(); } break; } case NONE : default : { break; } } } else { href = customHandler.getHyperlink(link); } return href; } protected void endHyperlink() throws IOException { if (hyperlinkStarted) { writer.write(""); } hyperlinkStarted = false; } protected boolean appendBorderStyle(JRLineBox box, StringBuffer styleBuffer) { boolean addedToStyle = false; if (box != null) { LineStyleEnum tps = box.getTopPen().getLineStyleValue(); LineStyleEnum lps = box.getLeftPen().getLineStyleValue(); LineStyleEnum bps = box.getBottomPen().getLineStyleValue(); LineStyleEnum rps = box.getRightPen().getLineStyleValue(); float tpw = box.getTopPen().getLineWidth().floatValue(); float lpw = box.getLeftPen().getLineWidth().floatValue(); float bpw = box.getBottomPen().getLineWidth().floatValue(); float rpw = box.getRightPen().getLineWidth().floatValue(); if (0f < tpw && tpw < 1f) { tpw = 1f; } if (0f < lpw && lpw < 1f) { lpw = 1f; } if (0f < bpw && bpw < 1f) { bpw = 1f; } if (0f < rpw && rpw < 1f) { rpw = 1f; } Color tpc = box.getTopPen().getLineColor(); // try to compact all borders into one css property if (tps == lps && // same line style tps == bps && tps == rps && tpw == lpw && // same line width tpw == bpw && tpw == rpw && tpc.equals(box.getLeftPen().getLineColor()) && // same line color tpc.equals(box.getBottomPen().getLineColor()) && tpc.equals(box.getRightPen().getLineColor())) { addedToStyle |= appendPen( styleBuffer, box.getTopPen(), null ); } else { addedToStyle |= appendPen( styleBuffer, box.getTopPen(), "top" ); addedToStyle |= appendPen( styleBuffer, box.getLeftPen(), "left" ); addedToStyle |= appendPen( styleBuffer, box.getBottomPen(), "bottom" ); addedToStyle |= appendPen( styleBuffer, box.getRightPen(), "right" ); } } return addedToStyle; } protected boolean appendPaddingStyle(JRLineBox box, StringBuffer styleBuffer) { boolean addedToStyle = false; if (box != null) { Integer tp = box.getTopPadding(); Integer lp = box.getLeftPadding(); Integer bp = box.getBottomPadding(); Integer rp = box.getRightPadding(); // try to compact all paddings into one css property if (tp == lp && tp == bp && tp == rp) { addedToStyle |= appendPadding( styleBuffer, tp, null ); } else { addedToStyle |= appendPadding( styleBuffer, box.getTopPadding(), "top" ); addedToStyle |= appendPadding( styleBuffer, box.getLeftPadding(), "left" ); addedToStyle |= appendPadding( styleBuffer, box.getBottomPadding(), "bottom" ); addedToStyle |= appendPadding( styleBuffer, box.getRightPadding(), "right" ); } } return addedToStyle; } protected Color appendBackcolorStyle(JRExporterGridCell gridCell, StringBuffer styleBuffer) { Color cellBackcolor = gridCell.getCellBackcolor(); if (cellBackcolor != null && (backcolor == null || cellBackcolor.getRGB() != backcolor.getRGB())) { styleBuffer.append("background-color: #"); styleBuffer.append(JRColorUtil.getColorHexa(cellBackcolor)); styleBuffer.append("; "); return cellBackcolor; } return null; } /** * */ protected void exportImage(JRPrintImage image, JRExporterGridCell gridCell) throws JRException, IOException { writeCellStart(gridCell); StringBuffer styleBuffer = new StringBuffer(); String horizontalAlignment = CSS_TEXT_ALIGN_LEFT; switch (image.getHorizontalAlignmentValue()) { case RIGHT : { horizontalAlignment = CSS_TEXT_ALIGN_RIGHT; break; } case CENTER : { horizontalAlignment = CSS_TEXT_ALIGN_CENTER; break; } case LEFT : default : { horizontalAlignment = CSS_TEXT_ALIGN_LEFT; } } if (!horizontalAlignment.equals(CSS_TEXT_ALIGN_LEFT)) { styleBuffer.append("text-align: "); styleBuffer.append(horizontalAlignment); styleBuffer.append(";"); } String verticalAlignment = HTML_VERTICAL_ALIGN_TOP; switch (image.getVerticalAlignmentValue()) { case BOTTOM : { verticalAlignment = HTML_VERTICAL_ALIGN_BOTTOM; break; } case MIDDLE : { verticalAlignment = HTML_VERTICAL_ALIGN_MIDDLE; break; } case TOP : default : { verticalAlignment = HTML_VERTICAL_ALIGN_TOP; } } if (!verticalAlignment.equals(HTML_VERTICAL_ALIGN_TOP)) { styleBuffer.append(" vertical-align: "); styleBuffer.append(verticalAlignment); styleBuffer.append(";"); } appendBackcolorStyle(gridCell, styleBuffer); boolean addedToStyle = appendBorderStyle(gridCell.getBox(), styleBuffer); if (!addedToStyle) { appendPen( styleBuffer, image.getLinePen(), null ); } appendPaddingStyle(image.getLineBox(), styleBuffer); if (styleBuffer.length() > 0) { writer.write(" style=\""); writer.write(styleBuffer.toString()); writer.write("\""); } writer.write(">"); if (image.getAnchorName() != null) { writer.write(""); } Renderable renderer = image.getRenderable(); Renderable originalRenderer = renderer; boolean imageMapRenderer = renderer != null && renderer instanceof ImageMapRenderable && ((ImageMapRenderable) renderer).hasImageAreaHyperlinks(); boolean hasHyperlinks = false; if(renderer != null || isUsingImagesToAlign) { if (imageMapRenderer) { hasHyperlinks = true; hyperlinkStarted = false; } else { hasHyperlinks = startHyperlink(image); } writer.write(" imageMapAreas = null; ScaleImageEnum scaleImage = image.getScaleImageValue(); if (renderer != null) { if (renderer.getTypeValue() == RenderableTypeEnum.IMAGE && rendererToImagePathMap.containsKey(renderer.getId())) { imagePath = rendererToImagePathMap.get(renderer.getId()); } else { if (image.isLazy()) { imagePath = ((JRImageRenderer)renderer).getImageLocation(); } else { JRPrintElementIndex imageIndex = getElementIndex(gridCell); imagesToProcess.add(imageIndex); String imageName = getImageName(imageIndex); imagePath = imagesURI + imageName; //backward compatibility with the IMAGE_MAP parameter if (imageNameToImageDataMap != null) { if (renderer.getTypeValue() == RenderableTypeEnum.SVG) { renderer = new JRWrappingSvgRenderer( renderer, new Dimension(image.getWidth(), image.getHeight()), ModeEnum.OPAQUE == image.getModeValue() ? image.getBackcolor() : null ); } imageNameToImageDataMap.put(imageName, renderer.getImageData(jasperReportsContext)); } //END - backward compatibility with the IMAGE_MAP parameter } rendererToImagePathMap.put(renderer.getId(), imagePath); } if (imageMapRenderer) { Rectangle renderingArea = new Rectangle(image.getWidth(), image.getHeight()); if (renderer.getTypeValue() == RenderableTypeEnum.IMAGE) { imageMapName = imageMaps.get(new Pair(renderer.getId(), renderingArea)); } if (imageMapName == null) { imageMapName = "map_" + getElementIndex(gridCell).toString(); imageMapAreas = ((ImageMapRenderable) originalRenderer).getImageAreaHyperlinks(renderingArea);//FIXMECHART if (renderer.getTypeValue() == RenderableTypeEnum.IMAGE) { imageMaps.put(new Pair(renderer.getId(), renderingArea), imageMapName); } } } } else // ie: if(isUsingImagesToAlign) { loadPxImage(); imagePath = imagesURI + "px"; scaleImage = ScaleImageEnum.FILL_FRAME; } writer.write(" src=\""); if (imagePath != null) { writer.write(imagePath); } writer.write("\""); int imageWidth = image.getWidth() - image.getLineBox().getLeftPadding().intValue() - image.getLineBox().getRightPadding().intValue(); if (imageWidth < 0) { imageWidth = 0; } int imageHeight = image.getHeight() - image.getLineBox().getTopPadding().intValue() - image.getLineBox().getBottomPadding().intValue(); if (imageHeight < 0) { imageHeight = 0; } switch (scaleImage) { case FILL_FRAME : { writer.write(" style=\"width: "); writer.write(toSizeUnit(imageWidth)); writer.write("; height: "); writer.write(toSizeUnit(imageHeight)); writer.write("\""); break; } case CLIP : //FIXMEIMAGE image clip could be achieved by cutting the image and preserving the image type case RETAIN_SHAPE : default : { double normalWidth = imageWidth; double normalHeight = imageHeight; if (!image.isLazy()) { // Image load might fail. Renderable tmpRenderer = RenderableUtil.getInstance(jasperReportsContext).getOnErrorRendererForDimension(renderer, image.getOnErrorTypeValue()); Dimension2D dimension = tmpRenderer == null ? null : tmpRenderer.getDimension(jasperReportsContext); // If renderer was replaced, ignore image dimension. if (tmpRenderer == renderer && dimension != null) { normalWidth = dimension.getWidth(); normalHeight = dimension.getHeight(); } } if (imageHeight > 0) { double ratio = normalWidth / normalHeight; if( ratio > (double)imageWidth / (double)imageHeight ) { writer.write(" style=\"width: "); writer.write(toSizeUnit(imageWidth)); writer.write("\""); } else { writer.write(" style=\"height: "); writer.write(toSizeUnit(imageHeight)); writer.write("\""); } } } } if (imageMapName != null) { writer.write(" usemap=\"#" + imageMapName + "\""); } writer.write(" alt=\"\""); if (hasHyperlinks) { writer.write(" border=\"0\""); } if (image.getHyperlinkTooltip() != null) { writer.write(" title=\""); writer.write(JRStringUtil.xmlEncode(image.getHyperlinkTooltip())); writer.write("\""); } writer.write("/>"); endHyperlink(); if (imageMapAreas != null) { writer.write("\n"); writeImageMap(imageMapName, image, imageMapAreas); } } writeCellEnd(gridCell); } protected JRPrintElementIndex getElementIndex(JRExporterGridCell gridCell) { JRPrintElementIndex imageIndex = new JRPrintElementIndex( reportIndex, pageIndex, gridCell.getWrapper().getAddress() ); return imageIndex; } protected void writeImageMap(String imageMapName, JRPrintImage image, List imageMapAreas) throws IOException { writer.write("\n"); for (ListIterator it = imageMapAreas.listIterator(imageMapAreas.size()); it.hasPrevious();) { JRPrintImageAreaHyperlink areaHyperlink = it.previous(); JRPrintImageArea area = areaHyperlink.getArea(); writer.write(" \n"); } if (image.getHyperlinkTypeValue() != HyperlinkTypeEnum.NONE) { writer.write(" \n"); } writer.write("\n"); } protected void writeImageAreaCoordinates(int[] coords) throws IOException { if (coords != null && coords.length > 0) { StringBuffer coordsEnum = new StringBuffer(coords.length * 4); coordsEnum.append(toZoom(coords[0])); for (int i = 1; i < coords.length; i++) { coordsEnum.append(','); coordsEnum.append(toZoom(coords[i])); } writer.write(" coords=\"" + coordsEnum + "\""); } } protected void writeImageAreaHyperlink(JRPrintHyperlink hyperlink) throws IOException { String href = getHyperlinkURL(hyperlink); if (href == null) { writer.write(" nohref=\"nohref\""); } else { writer.write(" href=\"" + href + "\""); String target = getHyperlinkTarget(hyperlink); if (target != null) { writer.write(" target=\""); writer.write(target); writer.write("\""); } } if (hyperlink.getHyperlinkTooltip() != null) { writer.write(" title=\""); writer.write(JRStringUtil.xmlEncode(hyperlink.getHyperlinkTooltip())); writer.write("\""); } } /** * */ protected void loadPxImage() throws JRException { isPxImageLoaded = true; //backward compatibility with the IMAGE_MAP parameter if (imageNameToImageDataMap != null && !imageNameToImageDataMap.containsKey("px")) { Renderable pxRenderer = RenderableUtil.getInstance(jasperReportsContext).getRenderable("net/sf/jasperreports/engine/images/pixel.GIF"); rendererToImagePathMap.put(pxRenderer.getId(), imagesURI + "px"); imageNameToImageDataMap.put("px", pxRenderer.getImageData(jasperReportsContext)); } //END - backward compatibility with the IMAGE_MAP parameter } /** * */ protected static interface StringProvider { /** * */ public String getStringForCollapsedTD(Object value, int width, int height); /** * */ public String getStringForEmptyTD(Object value); public String getReportTableStyle(); } /** * */ private boolean appendPadding(StringBuffer sb, Integer padding, String side) { boolean addedToStyle = false; if (padding.intValue() > 0) { sb.append("padding"); if (side != null) { sb.append("-"); sb.append(side); } sb.append(": "); sb.append(toSizeUnit(padding.intValue())); sb.append("; "); addedToStyle = true; } return addedToStyle; } /** * */ private boolean appendPen(StringBuffer sb, JRPen pen, String side) { boolean addedToStyle = false; float borderWidth = pen.getLineWidth().floatValue(); if (0f < borderWidth && borderWidth < 1f) { borderWidth = 1f; } String borderStyle = null; switch (pen.getLineStyleValue()) { case DOUBLE : { borderStyle = "double"; break; } case DOTTED : { borderStyle = "dotted"; break; } case DASHED : { borderStyle = "dashed"; break; } case SOLID : default : { borderStyle = "solid"; break; } } if (borderWidth > 0f) { sb.append("border"); if (side != null) { sb.append("-"); sb.append(side); } sb.append(": "); sb.append(toSizeUnit((int)borderWidth)); sb.append(" "); sb.append(borderStyle); sb.append(" #"); sb.append(JRColorUtil.getColorHexa(pen.getLineColor())); sb.append("; "); addedToStyle = true; } return addedToStyle; } /** * */ public static String getImageName(JRPrintElementIndex printElementIndex) { return IMAGE_NAME_PREFIX + printElementIndex.toString(); } /** * */ public static JRPrintElementIndex getPrintElementIndex(String imageName) { if (!imageName.startsWith(IMAGE_NAME_PREFIX)) { throw new JRRuntimeException("Invalid image name: " + imageName); } return JRPrintElementIndex.parsePrintElementIndex(imageName.substring(IMAGE_NAME_PREFIX_LEGTH)); } protected void exportFrame(JRPrintFrame frame, JRExporterGridCell gridCell) throws IOException, JRException { writeCellStart(gridCell); StringBuffer styleBuffer = new StringBuffer(); Color frameBackcolor = appendBackcolorStyle(gridCell, styleBuffer); appendBorderStyle(gridCell.getBox(), styleBuffer); //no padding style for frames, because padding is in the grid if (styleBuffer.length() > 0) { writer.write(" style=\""); writer.write(styleBuffer.toString()); writer.write("\""); } writer.write(">\n"); if (frameBackcolor != null) { setBackcolor(frameBackcolor); } try { exportGrid(gridCell.getLayout(), false); } finally { if (frameBackcolor != null) { restoreBackcolor(); } } writeCellEnd(gridCell); } protected void setBackcolor(Color color) { backcolorStack.addLast(backcolor); backcolor = color; } protected void restoreBackcolor() { backcolor = backcolorStack.removeLast(); } protected void exportGenericElement(JRGenericPrintElement element, JRExporterGridCell gridCell, int rowHeight) throws IOException { GenericElementHtmlHandler handler = (GenericElementHtmlHandler) GenericElementHandlerEnviroment.getInstance(getJasperReportsContext()).getElementHandler( element.getGenericType(), HTML_EXPORTER_KEY); if (handler == null) { if (log.isDebugEnabled()) { log.debug("No HTML generic element handler for " + element.getGenericType()); } writeEmptyCell(gridCell, rowHeight); } else { writeCellStart(gridCell); StringBuffer styleBuffer = new StringBuffer(); appendBackcolorStyle(gridCell, styleBuffer); appendBorderStyle(gridCell.getBox(), styleBuffer); if (styleBuffer.length() > 0) { writer.write(" style=\""); writer.write(styleBuffer.toString()); writer.write("\""); } writer.write(">"); String htmlFragment = handler.getHtmlFragment(exporterContext, element); if (htmlFragment != null) { writer.write(htmlFragment); } writeCellEnd(gridCell); } } public Map getExportParameters() { return parameters; } public String getExportPropertiesPrefix() { return HTML_EXPORTER_PROPERTIES_PREFIX; } protected String getExporterKey() { return HTML_EXPORTER_KEY; } public JasperPrint getExportedReport() { return jasperPrint; } public String toSizeUnit(int size) { return String.valueOf(toZoom(size)) + sizeUnit; } public int toZoom(int size) { return (int)(zoom * size); } protected JRStyledText getStyledText(JRPrintText textElement, boolean setBackcolor) { JRStyledText styledText = super.getStyledText(textElement, setBackcolor); if (styledText != null) { short[] lineBreakOffsets = textElement.getLineBreakOffsets(); if (lineBreakOffsets != null && lineBreakOffsets.length > 0) { //insert new lines at the line break positions saved at fill time //cloning the text first styledText = styledText.cloneText(); styledText.insert("\n", lineBreakOffsets); } } return styledText; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy