Package net.sf.jasperreports.engine.export.oasis

Source Code of net.sf.jasperreports.engine.export.oasis.JROpenDocumentExporter

/*
* JasperReports - Free Java Reporting Library.
* Copyright (C) 2001 - 2009 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 <http://www.gnu.org/licenses/>.
*/

/*
* Special thanks to Google 'Summer of Code 2005' program for supporting this development
*
* Contributors:
* Majid Ali Khan - majidkk@users.sourceforge.net
* Frank Sch�nheit - Frank.Schoenheit@Sun.COM
*/
package net.sf.jasperreports.engine.export.oasis;

import java.awt.Color;
import java.awt.Dimension;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.Writer;
import java.text.AttributedCharacterIterator;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Map;

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.JRPrintHyperlink;
import net.sf.jasperreports.engine.JRPrintImage;
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.JRRenderable;
import net.sf.jasperreports.engine.JRRuntimeException;
import net.sf.jasperreports.engine.JRWrappingSvgRenderer;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.base.JRBaseLineBox;
import net.sf.jasperreports.engine.export.CutsInfo;
import net.sf.jasperreports.engine.export.ExporterFilter;
import net.sf.jasperreports.engine.export.ExporterNature;
import net.sf.jasperreports.engine.export.JRExportProgressMonitor;
import net.sf.jasperreports.engine.export.JRExporterGridCell;
import net.sf.jasperreports.engine.export.JRGridLayout;
import net.sf.jasperreports.engine.export.JRHyperlinkProducer;
import net.sf.jasperreports.engine.export.zip.ExportZipEntry;
import net.sf.jasperreports.engine.export.zip.FileBufferedZipEntry;
import net.sf.jasperreports.engine.type.ModeEnum;
import net.sf.jasperreports.engine.util.JRStringUtil;
import net.sf.jasperreports.engine.util.JRStyledText;
import net.sf.jasperreports.engine.util.JRTextAttribute;


/**
* Exports a JasperReports document to ODF format. It has character output type and exports the document to a
* grid-based layout.
* @author Teodor Danciu (teodord@users.sourceforge.net)
* @version $Id: JROpenDocumentExporter.java 4292 2011-04-15 13:35:01Z teodord $
*/
public abstract class JROpenDocumentExporter extends JRAbstractExporter
{
  /**
   *
   */
  protected static final String JR_PAGE_ANCHOR_PREFIX = "JR_PAGE_ANCHOR_";

  public static final String IMAGE_NAME_PREFIX = "img_";
  protected static final int IMAGE_NAME_PREFIX_LEGTH = IMAGE_NAME_PREFIX.length();

  /**
   *
   */
  protected Writer tempBodyWriter;
  protected Writer tempStyleWriter;

  protected JRExportProgressMonitor progressMonitor;
  protected Map rendererToImagePathMap;
  protected Map imageMaps;
  protected List imagesToProcess;

  protected int reportIndex;
  protected int pageIndex;
  protected int tableIndex;
  protected boolean startPage;

  /**
   *
   */
  protected String encoding;


  /**
   * @deprecated
   */
  protected Map fontMap;

  protected LinkedList backcolorStack;
  protected Color backcolor;

  protected StyleCache styleCache;

  protected ExporterNature nature;

  public JROpenDocumentExporter()
  {
    backcolorStack = new LinkedList();
    backcolor = null;
  }


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

    /*   */
    setOffset();

    try
    {
      /*   */
      setExportContext();

      /*   */
      setInput();

      if (!parameters.containsKey(JRExporterParameter.FILTER))
      {
        filter = createFilter(getExporterPropertiesPrefix());
      }

      /*   */
      if (!isModeBatch)
      {
        setPageRange();
      }

      encoding =
        getStringParameterOrDefault(
          JRExporterParameter.CHARACTER_ENCODING,
          JRExporterParameter.PROPERTY_CHARACTER_ENCODING
          );

      rendererToImagePathMap = new HashMap();
      imageMaps = new HashMap();
      imagesToProcess = new ArrayList();

      fontMap = (Map) parameters.get(JRExporterParameter.FONT_MAP);

      setHyperlinkProducerFactory();

      nature = getExporterNature(filter);

      OutputStream os = (OutputStream)parameters.get(JRExporterParameter.OUTPUT_STREAM);
      if (os != null)
      {
        try
        {
          exportReportToOasisZip(os);
        }
        catch (IOException e)
        {
          throw new JRException("Error trying to export to output stream : " + 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);
          exportReportToOasisZip(os);
        }
        catch (IOException e)
        {
          throw new JRException("Error trying to export to file : " + destFile, e);
        }
        finally
        {
          if (os != null)
          {
            try
            {
              os.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 = (JasperPrint)jasperPrintList.get(imageIndex.getReportIndex());
    JRPrintPage page = (JRPrintPage)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 exportReportToOasisZip(OutputStream os) throws JRException, IOException
  {
    OasisZip oasisZip = new FileBufferedOasisZip(((JROpenDocumentExporterNature)nature).getOpenDocumentNature());

    ExportZipEntry tempBodyEntry = new FileBufferedZipEntry(null);
    ExportZipEntry tempStyleEntry = new FileBufferedZipEntry(null);

    tempBodyWriter = tempBodyEntry.getWriter();
    tempStyleWriter = tempStyleEntry.getWriter();

    styleCache = new StyleCache(tempStyleWriter, fontMap, getExporterKey());

    Writer stylesWriter = oasisZip.getStylesEntry().getWriter();

    StyleBuilder styleBuilder = new StyleBuilder(jasperPrintList, stylesWriter);
    styleBuilder.build();

    stylesWriter.close();

    for(reportIndex = 0; reportIndex < jasperPrintList.size(); reportIndex++)
    {
      setJasperPrint((JasperPrint)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 = (JRPrintPage)pages.get(pageIndex);

          exportPage(page);
        }
      }
    }


    tempBodyWriter.flush();
    tempStyleWriter.flush();


    tempBodyWriter.close();
    tempStyleWriter.close();


    /*   */
    ContentBuilder contentBuilder =
      new ContentBuilder(
        oasisZip.getContentEntry(),
        tempStyleEntry,
        tempBodyEntry,
        styleCache.getFontFaces(),
        ((JROpenDocumentExporterNature)nature).getOpenDocumentNature()
        );
    contentBuilder.build();

    tempStyleEntry.dispose();
    tempBodyEntry.dispose();

    if ((imagesToProcess != null && imagesToProcess.size() > 0))
    {
      for(Iterator it = imagesToProcess.iterator(); it.hasNext();)
      {
        JRPrintElementIndex imageIndex = (JRPrintElementIndex)it.next();

        JRPrintImage image = getImage(jasperPrintList, imageIndex);
        JRRenderable renderer = image.getRenderer();
        if (renderer.getType() == JRRenderable.TYPE_SVG)
        {
          renderer =
            new JRWrappingSvgRenderer(
              renderer,
              new Dimension(image.getWidth(), image.getHeight()),
              ModeEnum.OPAQUE == image.getModeValue() ? image.getBackcolor() : null
              );
        }

        oasisZip.addEntry(//FIXMEODT optimize with a different implementation of entry
          new FileBufferedZipEntry(
            "Pictures/" + getImageName(imageIndex),
            renderer.getImageData()
            )
          );
      }
    }

    oasisZip.zipEntries(os);

    oasisZip.dispose();
  }


  /**
   *
   */
  protected void exportPage(JRPrintPage page) throws JRException, IOException
  {
    startPage = true;
    JRGridLayout layout =
      new JRGridLayout(
        nature,
        page.getElements(),
        jasperPrint.getPageWidth(),
        jasperPrint.getPageHeight(),
        globalOffsetX,
        globalOffsetY,
        null //address
        );

    exportGrid(layout, null);

    if (progressMonitor != null)
    {
      progressMonitor.afterPageExport();
    }
  }


  /**
   *
   */
  protected void exportGrid(JRGridLayout gridLayout, JRPrintElementIndex frameIndex) throws IOException, JRException
  {
    CutsInfo xCuts = gridLayout.getXCuts();
    JRExporterGridCell[][] grid = gridLayout.getGrid();

    TableBuilder tableBuilder = frameIndex == null
      ? new TableBuilder(reportIndex, pageIndex, tempBodyWriter, tempStyleWriter)
      : new TableBuilder(frameIndex.toString(), tempBodyWriter, tempStyleWriter);

   
    tableBuilder.buildTableStyle(gridLayout.getWidth());
    tableBuilder.buildTableHeader();

    for(int col = 1; col < xCuts.size(); col++)
    {
      tableBuilder.buildColumnStyle(
          col - 1,
          xCuts.getCut(col) - xCuts.getCut(col - 1)
          );
      tableBuilder.buildColumnHeader(col - 1);
      tableBuilder.buildColumnFooter();
    }

    JRPrintElement element = null;
    for(int row = 0; row < grid.length; row++)
    {
      int emptyCellColSpan = 0;
      int emptyCellWidth = 0;
      int rowHeight = gridLayout.getRowHeight(row);

      tableBuilder.buildRowStyle(row, rowHeight);
      tableBuilder.buildRowHeader(row);

      for(int col = 0; col < grid[0].length; col++)
      {
        JRExporterGridCell gridCell = grid[row][col];
        if (gridCell.getType() == JRExporterGridCell.TYPE_OCCUPIED_CELL)
        {
          if (emptyCellColSpan > 0)
          {
            //writeEmptyCell(gridCell, emptyCellColSpan, emptyCellWidth, rowHeight);
            emptyCellColSpan = 0;
            emptyCellWidth = 0;
          }

          //writeOccupiedCells(1);
          exportOccupiedCells(1);
//          OccupiedGridCell occupiedGridCell = (OccupiedGridCell)gridCell;
//          ElementGridCell elementGridCell = (ElementGridCell)grid[occupiedGridCell.getRow()][occupiedGridCell.getCol()];
//          exportOccupiedCells(elementGridCell);
//          col += elementGridCell.getColSpan() - 1;
        }
        else if(gridCell.getWrapper() != null)
        {
          if (emptyCellColSpan > 0)
          {
            //writeEmptyCell(gridCell, emptyCellColSpan, emptyCellWidth, rowHeight);
            emptyCellColSpan = 0;
            emptyCellWidth = 0;
          }

          element = gridCell.getWrapper().getElement();

          if (element instanceof JRPrintLine)
          {
            exportLine(tableBuilder, (JRPrintLine)element, gridCell);
          }
          else if (element instanceof JRPrintRectangle)
          {
            exportRectangle(tableBuilder, (JRPrintRectangle)element, gridCell);
          }
          else if (element instanceof JRPrintEllipse)
          {
            exportEllipse(tableBuilder, (JRPrintEllipse)element, gridCell);
          }
          else if (element instanceof JRPrintImage)
          {
            exportImage(tableBuilder, (JRPrintImage)element, gridCell);
          }
          else if (element instanceof JRPrintText)
          {
            exportText(tableBuilder, (JRPrintText)element, gridCell);
          }
          else if (element instanceof JRPrintFrame)
          {
            exportFrame(tableBuilder, (JRPrintFrame)element, gridCell);
          }
          else if (element instanceof JRGenericPrintElement)
          {
            exportGenericElement(tableBuilder, (JRGenericPrintElement)element, gridCell);
          }

          // //x += gridCell.colSpan - 1;
          //col += gridCell.getColSpan() - 1;
        }
        else
        {
          emptyCellColSpan++;
          emptyCellWidth += gridCell.getWidth();
          exportEmptyCell(gridCell, 1);
        }
      }

//      if (emptyCellColSpan > 0)
//      {
//        //writeEmptyCell(null, emptyCellColSpan, emptyCellWidth, rowHeight);
//      }

      tableBuilder.buildRowFooter();
    }

    tableBuilder.buildTableFooter();
  }


//  private void writeEmptyCell(JRExporterGridCell gridCell, int emptyCellColSpan, int emptyCellWidth, int rowHeight) throws IOException
//  {
//    tempBodyWriter.write("<table:table-cell");
//    //tempBodyWriter.write(" office:value-type=\"string\"");
//    tempBodyWriter.write(" table:style-name=\"empty-cell\"");
//    if (emptyCellColSpan > 1)
//    {
//      tempBodyWriter.write(" table:number-columns-spanned=\"" + emptyCellColSpan + "\"");
//    }
//    tempBodyWriter.write("/>\n");
//
//    writeOccupiedCells(emptyCellColSpan - 1);
//  }
//
//
//  private void writeOccupiedCells(int count) throws IOException
//  {
//    for(int i = 0; i < count; i++)
//    {
//      tempBodyWriter.write("<table:covered-table-cell/>\n");
//    }
//  }


  private void exportEmptyCell(JRExporterGridCell gridCell, int emptyCellColSpan) throws IOException
  {
    tempBodyWriter.write("<table:table-cell");
    //tempBodyWriter.write(" office:value-type=\"string\"");
//    if (gridCell == null)
//    {
//      tempBodyWriter.write(" table:style-name=\"empty-cell\"");
//    }
//    else
    {
      tempBodyWriter.write(" table:style-name=\"" + styleCache.getCellStyle(gridCell) + "\"");
    }
    if (emptyCellColSpan > 1)
    {
      tempBodyWriter.write(" table:number-columns-spanned=\"" + emptyCellColSpan + "\"");
    }
    tempBodyWriter.write("/>\n");

    exportOccupiedCells(emptyCellColSpan - 1);
  }


//  private void exportOccupiedCells(JRExporterGridCell gridCell) throws IOException
//  {
//    tempBodyWriter.write("<table:table-cell");
//    //tempBodyWriter.write(" office:value-type=\"string\"");
//    tempBodyWriter.write(" table:style-name=\"" + styleCache.getCellStyle(gridCell.getElement(), gridCell) + "\"");
//    if (gridCell.getColSpan() > 1)
//    {
//      tempBodyWriter.write(" table:number-columns-spanned=\"" + gridCell.getColSpan() + "\"");
//    }
//    tempBodyWriter.write("/>\n");
//
//    exportOccupiedCells(gridCell.getColSpan() - 1);
//  }


//  private void exportOccupiedCells(JRExporterGridCell gridCell) throws IOException
//  {
//    exportOccupiedCells(gridCell.getColSpan());
//  }


  private void exportOccupiedCells(int count) throws IOException
  {
    for(int i = 0; i < count; i++)
    {
      tempBodyWriter.write("<table:covered-table-cell/>\n");
    }
  }


  /**
   *
   */
  protected abstract void exportLine(TableBuilder tableBuilder, JRPrintLine line, JRExporterGridCell gridCell) throws IOException;


  /**
   *
   */
  protected void exportRectangle(TableBuilder tableBuilder, JRPrintRectangle rectangle, JRExporterGridCell gridCell) throws IOException
  {
    JRLineBox box = new JRBaseLineBox(null);
    JRPen pen = box.getPen();
    pen.setLineColor(rectangle.getLinePen().getLineColor());
    pen.setLineStyle(rectangle.getLinePen().getLineStyleValue());
    pen.setLineWidth(rectangle.getLinePen().getLineWidth());

    gridCell.setBox(box);//CAUTION: only some exporters set the cell box

    tableBuilder.buildCellHeader(styleCache.getCellStyle(gridCell), gridCell.getColSpan(), gridCell.getRowSpan());
    tableBuilder.buildCellFooter();
  }


  /**
   *
   */
  protected abstract void exportEllipse(TableBuilder tableBuilder, JRPrintEllipse ellipse, JRExporterGridCell gridCell) throws IOException;


  /**
   *
   */
  public void exportText(TableBuilder tableBuilder, JRPrintText text, JRExporterGridCell gridCell) throws IOException
  {
    tableBuilder.buildCellHeader(styleCache.getCellStyle(gridCell), gridCell.getColSpan(), gridCell.getRowSpan());

    JRStyledText styledText = getStyledText(text);

    int textLength = 0;

    if (styledText != null)
    {
      textLength = styledText.length();
    }

    tempBodyWriter.write("<text:p text:style-name=\"");
    tempBodyWriter.write(styleCache.getParagraphStyle(text));
    tempBodyWriter.write("\">");
    insertPageAnchor();
    if (text.getAnchorName() != null)
    {
      exportAnchor(JRStringUtil.xmlEncode(text.getAnchorName()));
    }

    boolean startedHyperlink = startHyperlink(text, true);

    if (textLength > 0)
    {
      exportStyledText(styledText, getTextLocale(text), startedHyperlink);
    }

    if (startedHyperlink)
    {
      endHyperlink(true);
    }

    tempBodyWriter.write("</text:p>\n");

    tableBuilder.buildCellFooter();
  }


  /**
   *
   */
  protected void exportStyledText(JRStyledText styledText, Locale locale, boolean startedHyperlink) throws IOException
  {
    String text = styledText.getText();

    int runLimit = 0;

    AttributedCharacterIterator iterator = styledText.getAttributedString().getIterator();

    while(runLimit < styledText.length() && (runLimit = iterator.getRunLimit()) <= styledText.length())
    {
      exportStyledTextRun(
        iterator.getAttributes(),
        text.substring(iterator.getIndex(), runLimit),
        locale,
        startedHyperlink
        );

      iterator.setIndex(runLimit);
    }
  }


  /**
   *
   */
  protected void exportStyledTextRun(Map attributes, String text, Locale locale, boolean startedHyperlink) throws IOException
  {
    String textSpanStyleName = styleCache.getTextSpanStyle(attributes, text, locale);

    tempBodyWriter.write("<text:span");
    tempBodyWriter.write(" text:style-name=\"" + textSpanStyleName + "\"");
    tempBodyWriter.write(">");

    boolean localHyperlink = false;

    if (!startedHyperlink)
    {
      JRPrintHyperlink hyperlink = (JRPrintHyperlink)attributes.get(JRTextAttribute.HYPERLINK);
      if (hyperlink != null)
      {
        localHyperlink = startHyperlink(hyperlink, true);
      }
    }
   
    if (text != null)
    {
      tempBodyWriter.write(Utility.replaceNewLineWithLineBreak(JRStringUtil.xmlEncode(text)));//FIXMEODT try something nicer for replace
    }

    if (localHyperlink)
    {
      endHyperlink(true);
    }

    tempBodyWriter.write("</text:span>");
  }

  /**
   *
   */
  protected abstract void exportImage(TableBuilder tableBuilder, JRPrintImage image, JRExporterGridCell gridCell) throws JRException, IOException;


  /**
   *
   */
  protected String getImagePath(JRRenderable renderer, boolean isLazy, JRExporterGridCell gridCell)
  {
    String imagePath = null;

    if (renderer != null)
    {
      if (renderer.getType() == JRRenderable.TYPE_IMAGE && rendererToImagePathMap.containsKey(renderer.getId()))
      {
        imagePath = (String)rendererToImagePathMap.get(renderer.getId());
      }
      else
      {
        if (isLazy)
        {
          imagePath = ((JRImageRenderer)renderer).getImageLocation();
        }
        else
        {
          JRPrintElementIndex imageIndex = getElementIndex(gridCell);
          imagesToProcess.add(imageIndex);

          String imageName = getImageName(imageIndex);
          imagePath = "Pictures/" + imageName;
        }

        rendererToImagePathMap.put(renderer.getId(), imagePath);
      }
    }

    return imagePath;
  }


  protected JRPrintElementIndex getElementIndex(JRExporterGridCell gridCell)
  {
    JRPrintElementIndex imageIndex =
      new JRPrintElementIndex(
          reportIndex,
          pageIndex,
          gridCell.getWrapper().getAddress()
          );
    return imageIndex;
  }


  /**
   *
   *
  protected void writeImageMap(String imageMapName, JRPrintHyperlink mainHyperlink, List imageMapAreas) throws IOException
  {
    writer.write("<map name=\"" + imageMapName + "\">\n");

    for (Iterator it = imageMapAreas.iterator(); it.hasNext();)
    {
      JRPrintImageAreaHyperlink areaHyperlink = (JRPrintImageAreaHyperlink) it.next();
      JRPrintImageArea area = areaHyperlink.getArea();

      writer.write("  <area shape=\"" + JRPrintImageArea.getHtmlShape(area.getShape()) + "\"");
      writeImageAreaCoordinates(area);
      writeImageAreaHyperlink(areaHyperlink.getHyperlink());
      writer.write("/>\n");
    }

    if (mainHyperlink.getHyperlinkTypeValue() != NONE)
    {
      writer.write("  <area shape=\"default\"");
      writeImageAreaHyperlink(mainHyperlink);
      writer.write("/>\n");
    }

    writer.write("</map>\n");
  }


  protected void writeImageAreaCoordinates(JRPrintImageArea area) throws IOException
  {
    int[] coords = area.getCoordinates();
    if (coords != null && coords.length > 0)
    {
      StringBuffer coordsEnum = new StringBuffer(coords.length * 4);
      coordsEnum.append(coords[0]);
      for (int i = 1; i < coords.length; i++)
      {
        coordsEnum.append(',');
        coordsEnum.append(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("\"");
    }
  }


  /**
   *
   */
  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(TableBuilder tableBuilder, JRPrintFrame frame, JRExporterGridCell gridCell) throws IOException, JRException
  {
    tableBuilder.buildCellHeader(styleCache.getCellStyle(gridCell), gridCell.getColSpan(), gridCell.getRowSpan());

    boolean appendBackcolor =
      frame.getModeValue() == ModeEnum.OPAQUE
      && (backcolor == null || frame.getBackcolor().getRGB() != backcolor.getRGB());

    if (appendBackcolor)
    {
      setBackcolor(frame.getBackcolor());
    }

    try
    {
      JRGridLayout layout = gridCell.getLayout();
      JRPrintElementIndex frameIndex =
        new JRPrintElementIndex(
            reportIndex,
            pageIndex,
            gridCell.getWrapper().getAddress()
            );
      exportGrid(layout, frameIndex);
    }
    finally
    {
      if (appendBackcolor)
      {
        restoreBackcolor();
      }
    }

    tableBuilder.buildCellFooter();
  }


  /**
   *
   */
  protected abstract void exportGenericElement(TableBuilder tableBuilder, JRGenericPrintElement element, JRExporterGridCell gridCell) throws IOException, JRException;


  /**
   *
   */
  protected void setBackcolor(Color color)
  {
    backcolorStack.addLast(backcolor);

    backcolor = color;
  }


  protected void restoreBackcolor()
  {
    backcolor = (Color) backcolorStack.removeLast();
  }


  protected float getXAlignFactor(JRPrintImage image)
  {
    float xalignFactor = 0f;
    switch (image.getHorizontalAlignmentValue())
    {
      case RIGHT :
      {
        xalignFactor = 1f;
        break;
      }
      case CENTER :
      {
        xalignFactor = 0.5f;
        break;
      }
      case LEFT :
      default :
      {
        xalignFactor = 0f;
        break;
      }
    }
    return xalignFactor;
  }


  protected float getYAlignFactor(JRPrintImage image)
  {
    float yalignFactor = 0f;
    switch (image.getVerticalAlignmentValue())
    {
      case BOTTOM :
      {
        yalignFactor = 1f;
        break;
      }
      case MIDDLE :
      {
        yalignFactor = 0.5f;
        break;
      }
      case TOP :
      default :
      {
        yalignFactor = 0f;
        break;
      }
    }
    return yalignFactor;
  }

  protected boolean startHyperlink(JRPrintHyperlink link, boolean isText) throws IOException
  {
    String href = getHyperlinkURL(link);

    if (href != null)
    {
      if(isText)
      {
        tempBodyWriter.write("<text:a xlink:href=\"");
      }
      else
      {
        tempBodyWriter.write("<draw:a xlink:type=\"simple\" xlink:href=\"");
      }
      tempBodyWriter.write(JRStringUtil.xmlEncode(href));
      tempBodyWriter.write("\"");


      String target = getHyperlinkTarget(link);//FIXMETARGET
      if (target != null)
      {
        tempBodyWriter.write(" office:target-frame-name=\"");
        tempBodyWriter.write(target);
        tempBodyWriter.write("\"");
        if(target.equals("_blank"))
        {
          tempBodyWriter.write(" xlink:show=\"new\"");
        }
      }
/*
* tooltips are unavailable for the moment
*
      if (link.getHyperlinkTooltip() != null)
      {
        tempBodyWriter.write(" xlink:title=\"");
        tempBodyWriter.write(JRStringUtil.xmlEncode(link.getHyperlinkTooltip()));
        tempBodyWriter.write("\"");
      }
*/
      tempBodyWriter.write(">");
    }

    return href != null;
  }


  protected String getHyperlinkTarget(JRPrintHyperlink link)
  {
    String target = null;
    switch(link.getHyperlinkTargetValue())
    {
      case SELF :
      {
        target = "_self";
        break;
      }
      case BLANK :
      default :
      {
        target = "_blank";
        break;
      }
    }
    return target;
  }


  protected String getHyperlinkURL(JRPrintHyperlink link)
  {
    String href = null;
    JRHyperlinkProducer customHandler = getCustomHandler(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(boolean isText) throws IOException
  {
    if(isText)
    {
      tempBodyWriter.write("</text:a>");
    }
    else
    {
      tempBodyWriter.write("</draw:a>");
    }
  }

  protected void insertPageAnchor() throws IOException
  {
    if(startPage)
    {
      exportAnchor(JR_PAGE_ANCHOR_PREFIX + reportIndex + "_" + (pageIndex + 1));
      startPage = false;
    }
  }
 
  protected abstract void exportAnchor(String anchorName) throws IOException;

  protected abstract ExporterNature getExporterNature(ExporterFilter filter);

  protected abstract String getExporterPropertiesPrefix();

}
TOP

Related Classes of net.sf.jasperreports.engine.export.oasis.JROpenDocumentExporter

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.