KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jasperreports > engine > export > JRXlsAbstractExporter


1 /*
2  * ============================================================================
3  * GNU Lesser General Public License
4  * ============================================================================
5  *
6  * JasperReports - Free Java report-generating library.
7  * Copyright (C) 2001-2006 JasperSoft Corporation http://www.jaspersoft.com
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22  *
23  * JasperSoft Corporation
24  * 303 Second Street, Suite 450 North
25  * San Francisco, CA 94107
26  * http://www.jaspersoft.com
27  */

28 package net.sf.jasperreports.engine.export;
29
30 import java.io.File JavaDoc;
31 import java.io.FileOutputStream JavaDoc;
32 import java.io.IOException JavaDoc;
33 import java.io.OutputStream JavaDoc;
34 import java.util.ArrayList JavaDoc;
35 import java.util.Collection JavaDoc;
36 import java.util.HashMap JavaDoc;
37 import java.util.Iterator JavaDoc;
38 import java.util.List JavaDoc;
39 import java.util.Map JavaDoc;
40
41 import net.sf.jasperreports.engine.JRAbstractExporter;
42 import net.sf.jasperreports.engine.JRAlignment;
43 import net.sf.jasperreports.engine.JRException;
44 import net.sf.jasperreports.engine.JRExporterParameter;
45 import net.sf.jasperreports.engine.JRFont;
46 import net.sf.jasperreports.engine.JRPrintElement;
47 import net.sf.jasperreports.engine.JRPrintEllipse;
48 import net.sf.jasperreports.engine.JRPrintFrame;
49 import net.sf.jasperreports.engine.JRPrintImage;
50 import net.sf.jasperreports.engine.JRPrintLine;
51 import net.sf.jasperreports.engine.JRPrintPage;
52 import net.sf.jasperreports.engine.JRPrintRectangle;
53 import net.sf.jasperreports.engine.JRPrintText;
54 import net.sf.jasperreports.engine.JRTextElement;
55 import net.sf.jasperreports.engine.JasperPrint;
56 import net.sf.jasperreports.engine.base.JRBasePrintPage;
57 import net.sf.jasperreports.engine.base.JRBasePrintText;
58 import net.sf.jasperreports.engine.util.JRStyledText;
59 import net.sf.jasperreports.engine.util.JRStyledTextParser;
60
61 import org.xml.sax.SAXException JavaDoc;
62
63 /**
64  * @author Teodor Danciu (teodord@users.sourceforge.net)
65  * @version $Id: JRXlsAbstractExporter.java 1483 2006-11-13 11:56:53 +0200 (Mon, 13 Nov 2006) teodord $
66  */

67 public abstract class JRXlsAbstractExporter extends JRAbstractExporter
68 {
69
70     protected static class TextAlignHolder
71     {
72         public final short horizontalAlignment;
73         public final short verticalAlignment;
74         public final short rotation;
75
76         public TextAlignHolder(short horizontalAlignment, short verticalAlignment, short rotation)
77         {
78             this.horizontalAlignment = horizontalAlignment;
79             this.verticalAlignment = verticalAlignment;
80             this.rotation = rotation;
81         }
82     }
83     
84     /**
85      *
86      */

87     protected int pageHeight = 0;
88
89     /**
90      *
91      */

92     protected List JavaDoc loadedFonts = new ArrayList JavaDoc();
93
94     /**
95      *
96      */

97     protected boolean isOnePagePerSheet = false;
98
99     protected boolean isRemoveEmptySpace = false;
100
101     protected boolean isWhitePageBackground = true;
102
103     protected boolean isAutoDetectCellType = true;
104     protected boolean isDetectCellType = false;
105
106     protected boolean isFontSizeFixEnabled = false;
107     
108     protected String JavaDoc[] sheetNames = null;
109
110     /**
111      *
112      */

113     protected JRStyledTextParser styledTextParser = new JRStyledTextParser();
114
115     protected JRExportProgressMonitor progressMonitor = null;
116
117     protected int reportIndex = 0;
118     
119
120     protected Map JavaDoc fontMap = null;
121
122     private int skippedRows = 0;
123     
124     /**
125      *
126      */

127     protected JRFont defaultFont = null;
128
129     /**
130      * used for counting the total number of sheets
131      */

132     protected int sheetIndex = 0;
133
134     /**
135      * used when indexing the identical sheet generated names with ordering numbers;
136      * contains sheet names as keys and the number of occurences of each sheet name as values
137      */

138     protected Map JavaDoc sheetNamesMap = null;
139     
140     /**
141      *
142      */

143     protected JRFont getDefaultFont()
144     {
145         return defaultFont;
146     }
147
148     
149     /**
150      *
151      */

152     public void exportReport() throws JRException
153     {
154         progressMonitor = (JRExportProgressMonitor)parameters.get(JRExporterParameter.PROGRESS_MONITOR);
155         
156         /* */
157         setOffset();
158
159         try
160         {
161             /* */
162             setExportContext();
163     
164             /* */
165             setInput();
166     
167             /* */
168             if (!isModeBatch)
169             {
170                 setPageRange();
171             }
172     
173             setParameters();
174             
175             OutputStream JavaDoc os = (OutputStream JavaDoc)parameters.get(JRExporterParameter.OUTPUT_STREAM);
176             if (os != null)
177             {
178                 exportReportToStream(os);
179             }
180             else
181             {
182                 File JavaDoc destFile = (File JavaDoc)parameters.get(JRExporterParameter.OUTPUT_FILE);
183                 if (destFile == null)
184                 {
185                     String JavaDoc fileName = (String JavaDoc)parameters.get(JRExporterParameter.OUTPUT_FILE_NAME);
186                     if (fileName != null)
187                     {
188                         destFile = new File JavaDoc(fileName);
189                     }
190                     else
191                     {
192                         throw new JRException("No output specified for the exporter.");
193                     }
194                 }
195     
196                 try
197                 {
198                     os = new FileOutputStream JavaDoc(destFile);
199                     exportReportToStream(os);
200                     os.flush();
201                 }
202                 catch (IOException JavaDoc e)
203                 {
204                     throw new JRException("Error trying to export to file : " + destFile, e);
205                 }
206                 finally
207                 {
208                     if (os != null)
209                     {
210                         try
211                         {
212                             os.close();
213                         }
214                         catch(IOException JavaDoc e)
215                         {
216                         }
217                     }
218                 }
219             }
220         }
221         finally
222         {
223             resetExportContext();
224         }
225     }
226
227     protected void setParameters()
228     {
229         Boolean JavaDoc isOnePagePerSheetParameter = (Boolean JavaDoc)parameters.get(JRXlsAbstractExporterParameter.IS_ONE_PAGE_PER_SHEET);
230         if (isOnePagePerSheetParameter != null)
231         {
232             isOnePagePerSheet = isOnePagePerSheetParameter.booleanValue();
233         }
234
235         Boolean JavaDoc isRemoveEmptySpaceParameter = (Boolean JavaDoc)parameters.get(JRXlsAbstractExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS);
236         if (isRemoveEmptySpaceParameter != null)
237         {
238             isRemoveEmptySpace = isRemoveEmptySpaceParameter.booleanValue();
239         }
240         
241         Boolean JavaDoc isWhitePageBackgroundParameter = (Boolean JavaDoc)parameters.get(JRXlsAbstractExporterParameter.IS_WHITE_PAGE_BACKGROUND);
242         if (isWhitePageBackgroundParameter != null)
243         {
244             isWhitePageBackground = isWhitePageBackgroundParameter.booleanValue();
245             setBackground();
246         }
247         
248         Boolean JavaDoc isAutoDetectCellTypeParameter = (Boolean JavaDoc)parameters.get(JRXlsAbstractExporterParameter.IS_AUTO_DETECT_CELL_TYPE);
249         if (isAutoDetectCellTypeParameter != null)
250         {
251             isAutoDetectCellType = isAutoDetectCellTypeParameter.booleanValue();
252         }
253         
254         Boolean JavaDoc isDetectCellTypeParameter = (Boolean JavaDoc) parameters.get(JRXlsAbstractExporterParameter.IS_DETECT_CELL_TYPE);
255         isDetectCellType = isDetectCellTypeParameter != null && isDetectCellTypeParameter.booleanValue();
256
257         Boolean JavaDoc isFontSizeFixEnabledParameter = (Boolean JavaDoc) this.parameters.get(JRXlsAbstractExporterParameter.IS_FONT_SIZE_FIX_ENABLED);
258         if (isFontSizeFixEnabledParameter != null)
259         {
260             isFontSizeFixEnabled = isFontSizeFixEnabledParameter.booleanValue();
261         }
262
263         sheetNames = (String JavaDoc[])parameters.get(JRXlsAbstractExporterParameter.SHEET_NAMES);
264
265         fontMap = (Map JavaDoc) parameters.get(JRExporterParameter.FONT_MAP);
266     }
267
268     protected abstract void setBackground();
269     
270     protected void exportReportToStream(OutputStream JavaDoc os) throws JRException
271     {
272         openWorkbook(os);
273         sheetNamesMap = new HashMap JavaDoc();
274         
275         for(reportIndex = 0; reportIndex < jasperPrintList.size(); reportIndex++)
276         {
277             jasperPrint = (JasperPrint)jasperPrintList.get(reportIndex);
278             defaultFont = new JRBasePrintText(jasperPrint.getDefaultStyleProvider());
279
280             List JavaDoc pages = jasperPrint.getPages();
281             if (pages != null && pages.size() > 0)
282             {
283                 if (isModeBatch)
284                 {
285                     startPageIndex = 0;
286                     endPageIndex = pages.size() - 1;
287                 }
288
289                 if (isOnePagePerSheet)
290                 {
291                     pageHeight = jasperPrint.getPageHeight();
292                     
293                     for(int pageIndex = startPageIndex; pageIndex <= endPageIndex; pageIndex++)
294                     {
295                         if (Thread.currentThread().isInterrupted())
296                         {
297                             throw new JRException("Current thread interrupted.");
298                         }
299                         
300                         JRPrintPage page = (JRPrintPage)pages.get(pageIndex);
301                         
302                         if (sheetNames != null && sheetIndex < sheetNames.length)
303                         {
304                             createSheet(getSheetName(sheetNames[sheetIndex]));
305                         }
306                         else
307                         {
308                             createSheet("Page " + (sheetIndex + 1));
309                         }
310
311                         // we need to count all sheets generated for all exported documents
312
sheetIndex++;
313                         
314                         /* */
315                         exportPage(null, page);
316                     }
317                 }
318                 else
319                 {
320                     pageHeight = jasperPrint.getPageHeight() * (endPageIndex - startPageIndex + 1);
321
322                     List JavaDoc alterYs = new ArrayList JavaDoc();
323                     JRPrintPage allPages = new JRBasePrintPage();
324                     Collection JavaDoc elements = null;
325                     JRPrintElement element = null;
326                     for(int pageIndex = startPageIndex; pageIndex <= endPageIndex; pageIndex++)
327                     {
328                         if (Thread.currentThread().isInterrupted())
329                         {
330                             throw new JRException("Current thread interrupted.");
331                         }
332             
333                         JRPrintPage page = (JRPrintPage)pages.get(pageIndex);
334
335                         elements = page.getElements();
336                         if (elements != null && elements.size() > 0)
337                         {
338                             for(Iterator JavaDoc it = elements.iterator(); it.hasNext();)
339                             {
340                                 element = (JRPrintElement)it.next();
341                                 allPages.addElement(element);
342                             
343                                 alterYs.add(new Integer JavaDoc(element.getY() + jasperPrint.getPageHeight() * pageIndex));
344                             }
345                         }
346                     }
347                     
348                     if (sheetNames != null && sheetIndex < sheetNames.length)
349                     {
350                         createSheet(getSheetName(sheetNames[sheetIndex]));
351                     }
352                     else
353                     {
354                         createSheet(getSheetName(jasperPrint.getName()));
355                     }
356                     
357                     // we need to count all sheets generated for all exported documents
358
sheetIndex++;
359
360                     /* */
361                     exportPage(alterYs, allPages);
362                 }
363             }
364         }
365
366         closeWorkbook(os);
367     }
368     
369     /**
370      *
371      */

372     protected void exportPage(List JavaDoc alterYs, JRPrintPage page) throws JRException
373     {
374         JRGridLayout layout = new JRGridLayout(page.getElements(), alterYs,
375                 jasperPrint.getPageWidth(), pageHeight,
376                 globalOffsetX, globalOffsetY, getExporterElements(), true, true, false, null);
377
378         JRExporterGridCell grid[][] = layout.getGrid();
379         boolean isRowNotEmpty[] = layout.getIsRowNotEmpty();
380         List JavaDoc xCuts = layout.getXCuts();
381
382         int width = 0;
383         for(int i = 1; i < xCuts.size(); i++)
384         {
385             width = ((Integer JavaDoc)xCuts.get(i)).intValue() - ((Integer JavaDoc)xCuts.get(i - 1)).intValue();
386             setColumnWidth((short)(i - 1), (short)(width * 43));
387         }
388
389         skippedRows = 0;
390         for(int y = 0; y < grid.length; y++)
391         {
392             int rowIndex = y - skippedRows;
393             
394             if (isRowNotEmpty[y] || !isRemoveEmptySpace)
395             {
396                 JRExporterGridCell[] gridRow = grid[y];
397                 
398                 int emptyCellColSpan = 0;
399                 int emptyCellWidth = 0;
400                 int lastRowHeight = JRGridLayout.getRowHeight(gridRow);
401                 
402                 setRowHeight(rowIndex, lastRowHeight);
403     
404                 for(int x = 0; x < gridRow.length; x++)
405                 {
406                     setCell(x, rowIndex);
407     
408                     JRExporterGridCell gridCell = gridRow[x];
409                     if(gridCell.element != null)
410                     {
411                         if (emptyCellColSpan > 0)
412                         {
413                             if (emptyCellColSpan > 1)
414                             {
415                                 //sbuffer.append(" colspan=" + emptyCellColSpan);
416
//sheet.addMergedRegion(new Region(y, (short)(x - emptyCellColSpan - 1), y, (short)(x - 1)));
417
}
418                             emptyCellColSpan = 0;
419                             emptyCellWidth = 0;
420                         }
421     
422                         JRPrintElement element = gridCell.element;
423     
424                         if (element instanceof JRPrintLine)
425                         {
426                             exportLine((JRPrintLine)element, gridCell, x, rowIndex);
427                         }
428                         else if (element instanceof JRPrintRectangle)
429                         {
430                             exportRectangle(element, gridCell, x, rowIndex);
431                         }
432                         else if (element instanceof JRPrintEllipse)
433                         {
434                             exportRectangle(element, gridCell, x, rowIndex);
435                         }
436                         else if (element instanceof JRPrintImage)
437                         {
438                             exportImage((JRPrintImage) element, gridCell, x, rowIndex);
439                         }
440                         else if (element instanceof JRPrintText)
441                         {
442                             exportText((JRPrintText)element, gridCell, x, rowIndex);
443                         }
444                         else if (element instanceof JRPrintFrame)
445                         {
446                             exportFrame((JRPrintFrame) element, gridCell, x, y);//FIXME rowIndex?
447
}
448
449                         x += gridCell.colSpan - 1;
450                     }
451                     else
452                     {
453                         emptyCellColSpan++;
454                         emptyCellWidth += gridCell.width;
455                         addBlankCell(gridCell, x, rowIndex);
456                     }
457                 }
458     
459                 if (emptyCellColSpan > 0)
460                 {
461                     if (emptyCellColSpan > 1)
462                     {
463                         //sbuffer.append(" colspan=" + emptyCellColSpan);
464
//sheet.addMergedRegion(new Region(y, (short)x, y, (short)(x + emptyCellColSpan - 1)));
465
}
466                 }
467             }
468             else
469             {
470                 skippedRows++;
471 // setRowHeight(y, 0);
472
//
473
// for(int x = 0; x < grid[y].length; x++)
474
// {
475
// addBlankCell(x, y);
476
// setCell(x, y);
477
// }
478
}
479         }
480         
481         if (progressMonitor != null)
482         {
483             progressMonitor.afterPageExport();
484         }
485     }
486
487     /**
488      *
489      */

490     protected JRStyledText getStyledText(JRPrintText textElement)
491     {
492         JRStyledText styledText = null;
493
494         String JavaDoc text = textElement.getText();
495         if (text != null)
496         {
497             if (textElement.isStyledText())
498             {
499                 try
500                 {
501                     styledText = styledTextParser.parse(null, text);
502                 }
503                 catch (SAXException JavaDoc e)
504                 {
505                     //ignore if invalid styled text and treat like normal text
506
}
507             }
508         
509             if (styledText == null)
510             {
511                 styledText = new JRStyledText();
512                 styledText.append(text);
513                 styledText.addRun(new JRStyledText.Run(null, 0, text.length()));
514             }
515         }
516         
517         return styledText;
518     }
519
520     protected static TextAlignHolder getTextAlignHolder(JRPrintText textElement)
521     {
522         short horizontalAlignment;
523         short verticalAlignment;
524         short rotation = textElement.getRotation();
525
526         switch (textElement.getRotation())
527         {
528             case JRTextElement.ROTATION_LEFT :
529             {
530                 switch (textElement.getHorizontalAlignment())
531                 {
532                     case JRAlignment.HORIZONTAL_ALIGN_LEFT :
533                     {
534                         verticalAlignment = JRAlignment.VERTICAL_ALIGN_BOTTOM;
535                         break;
536                     }
537                     case JRAlignment.HORIZONTAL_ALIGN_CENTER :
538                     {
539                         verticalAlignment = JRAlignment.VERTICAL_ALIGN_MIDDLE;
540                         break;
541                     }
542                     case JRAlignment.HORIZONTAL_ALIGN_RIGHT :
543                     {
544                         verticalAlignment = JRAlignment.VERTICAL_ALIGN_TOP;
545                         break;
546                     }
547                     case JRAlignment.HORIZONTAL_ALIGN_JUSTIFIED :
548                     {
549                         verticalAlignment = JRAlignment.VERTICAL_ALIGN_JUSTIFIED;
550                         break;
551                     }
552                     default :
553                     {
554                         verticalAlignment = JRAlignment.VERTICAL_ALIGN_BOTTOM;
555                     }
556                 }
557
558                 switch (textElement.getVerticalAlignment())
559                 {
560                     case JRAlignment.VERTICAL_ALIGN_TOP :
561                     {
562                         horizontalAlignment = JRAlignment.HORIZONTAL_ALIGN_LEFT;
563                         break;
564                     }
565                     case JRAlignment.VERTICAL_ALIGN_MIDDLE :
566                     {
567                         horizontalAlignment = JRAlignment.HORIZONTAL_ALIGN_CENTER;
568                         break;
569                     }
570                     case JRAlignment.VERTICAL_ALIGN_BOTTOM :
571                     {
572                         horizontalAlignment = JRAlignment.HORIZONTAL_ALIGN_RIGHT;
573                         break;
574                     }
575                     default :
576                     {
577                         horizontalAlignment = JRAlignment.HORIZONTAL_ALIGN_LEFT;
578                     }
579                 }
580
581                 break;
582             }
583             case JRTextElement.ROTATION_RIGHT :
584             {
585                 switch (textElement.getHorizontalAlignment())
586                 {
587                     case JRAlignment.HORIZONTAL_ALIGN_LEFT :
588                     {
589                         verticalAlignment = JRAlignment.VERTICAL_ALIGN_TOP;
590                         break;
591                     }
592                     case JRAlignment.HORIZONTAL_ALIGN_CENTER :
593                     {
594                         verticalAlignment = JRAlignment.VERTICAL_ALIGN_MIDDLE;
595                         break;
596                     }
597                     case JRAlignment.HORIZONTAL_ALIGN_RIGHT :
598                     {
599                         verticalAlignment = JRAlignment.VERTICAL_ALIGN_BOTTOM;
600                         break;
601                     }
602                     case JRAlignment.HORIZONTAL_ALIGN_JUSTIFIED :
603                     {
604                         verticalAlignment = JRAlignment.VERTICAL_ALIGN_JUSTIFIED;
605                         break;
606                     }
607                     default :
608                     {
609                         verticalAlignment = JRAlignment.VERTICAL_ALIGN_TOP;
610                     }
611                 }
612
613                 switch (textElement.getVerticalAlignment())
614                 {
615                     case JRAlignment.VERTICAL_ALIGN_TOP :
616                     {
617                         horizontalAlignment = JRAlignment.HORIZONTAL_ALIGN_RIGHT;
618                         break;
619                     }
620                     case JRAlignment.VERTICAL_ALIGN_MIDDLE :
621                     {
622                         horizontalAlignment = JRAlignment.HORIZONTAL_ALIGN_CENTER;
623                         break;
624                     }
625                     case JRAlignment.VERTICAL_ALIGN_BOTTOM :
626                     {
627                         horizontalAlignment = JRAlignment.HORIZONTAL_ALIGN_LEFT;
628                         break;
629                     }
630                     default :
631                     {
632                         horizontalAlignment = JRAlignment.HORIZONTAL_ALIGN_RIGHT;
633                     }
634                 }
635
636                 break;
637             }
638             case JRTextElement.ROTATION_UPSIDE_DOWN:
639             case JRTextElement.ROTATION_NONE :
640             default :
641             {
642                 horizontalAlignment = textElement.getHorizontalAlignment();
643                 verticalAlignment = textElement.getVerticalAlignment();
644             }
645         }
646
647         return new TextAlignHolder(horizontalAlignment, verticalAlignment, rotation);
648     }
649     
650     /**
651      *
652      */

653     private String JavaDoc getSheetName(String JavaDoc sheetName)
654     {
655         // sheet names must be unique
656
if(!sheetNamesMap.containsKey(sheetName))
657         {
658             // first time this sheet name is found;
659
sheetNamesMap.put(sheetName, new Integer JavaDoc(1));
660             return sheetName;
661         }
662
663         int currentIndex = ((Integer JavaDoc)sheetNamesMap.get(sheetName)).intValue() + 1;
664         sheetNamesMap.put(sheetName, new Integer JavaDoc(currentIndex));
665         return sheetName + " " + currentIndex;
666     }
667     
668     protected abstract JRGridLayout.ExporterElements getExporterElements();
669
670     protected abstract void openWorkbook(OutputStream JavaDoc os) throws JRException;
671     
672     protected abstract void createSheet(String JavaDoc name);
673
674     protected abstract void closeWorkbook(OutputStream JavaDoc os) throws JRException;
675     
676     protected abstract void setColumnWidth (short index, short width);
677
678     protected abstract void setRowHeight(int rowIndex, int lastRowHeight) throws JRException;
679
680     protected abstract void setCell(int colIndex, int rowIndex);
681
682     protected abstract void addBlankCell(JRExporterGridCell gridCell, int colIndex, int rowIndex) throws JRException;
683
684     protected abstract void exportText(JRPrintText text, JRExporterGridCell cell, int colIndex, int rowIndex) throws JRException;
685
686     protected abstract void exportImage(JRPrintImage image, JRExporterGridCell cell, int colIndex, int rowIndex) throws JRException;
687
688     protected abstract void exportRectangle(JRPrintElement element, JRExporterGridCell cell, int colIndex, int rowIndex) throws JRException;
689
690     protected abstract void exportLine(JRPrintLine line, JRExporterGridCell cell, int colIndex, int rowIndex) throws JRException;
691     
692     protected abstract void exportFrame(JRPrintFrame frame, JRExporterGridCell cell, int colIndex, int rowIndex) throws JRException;
693 }
694
Popular Tags