KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jasperreports > engine > JRAbstractExporter


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;
29
30 import java.awt.font.TextAttribute JavaDoc;
31 import java.io.File JavaDoc;
32 import java.io.InputStream JavaDoc;
33 import java.math.BigDecimal JavaDoc;
34 import java.math.BigInteger JavaDoc;
35 import java.net.URL JavaDoc;
36 import java.net.URLStreamHandlerFactory JavaDoc;
37 import java.text.DateFormat JavaDoc;
38 import java.text.NumberFormat JavaDoc;
39 import java.text.ParseException JavaDoc;
40 import java.util.ArrayList JavaDoc;
41 import java.util.Date JavaDoc;
42 import java.util.HashMap JavaDoc;
43 import java.util.LinkedList JavaDoc;
44 import java.util.List JavaDoc;
45 import java.util.Locale JavaDoc;
46 import java.util.Map JavaDoc;
47 import java.util.TimeZone JavaDoc;
48
49 import net.sf.jasperreports.engine.base.JRBaseBox;
50 import net.sf.jasperreports.engine.export.data.BooleanTextValue;
51 import net.sf.jasperreports.engine.export.data.DateTextValue;
52 import net.sf.jasperreports.engine.export.data.NumberTextValue;
53 import net.sf.jasperreports.engine.export.data.StringTextValue;
54 import net.sf.jasperreports.engine.export.data.TextValue;
55 import net.sf.jasperreports.engine.util.DefaultFormatFactory;
56 import net.sf.jasperreports.engine.util.FormatFactory;
57 import net.sf.jasperreports.engine.util.JRClassLoader;
58 import net.sf.jasperreports.engine.util.JRDataUtils;
59 import net.sf.jasperreports.engine.util.JRFontUtil;
60 import net.sf.jasperreports.engine.util.JRLoader;
61 import net.sf.jasperreports.engine.util.JRResourcesUtil;
62 import net.sf.jasperreports.engine.util.JRStyledText;
63 import net.sf.jasperreports.engine.util.JRStyledTextParser;
64
65 import org.xml.sax.SAXException JavaDoc;
66
67
68 /**
69  * @author Teodor Danciu (teodord@users.sourceforge.net)
70  * @version $Id: JRAbstractExporter.java 1507 2006-11-27 17:12:17 +0200 (Mon, 27 Nov 2006) teodord $
71  */

72 public abstract class JRAbstractExporter implements JRExporter
73 {
74
75     //FIXMENOW this would make the applet require logging library
76
//private final static Log log = LogFactory.getLog(JRAbstractExporter.class);
77

78     /**
79      *
80      */

81     protected Map JavaDoc parameters = new HashMap JavaDoc();
82
83     /**
84      *
85      */

86     protected List JavaDoc jasperPrintList = null;
87     protected JasperPrint jasperPrint = null;
88     protected boolean isModeBatch = true;
89     protected int startPageIndex = 0;
90     protected int endPageIndex = 0;
91     protected int globalOffsetX = 0;
92     protected int globalOffsetY = 0;
93     protected ClassLoader JavaDoc classLoader = null;
94     protected boolean classLoaderSet = false;
95     protected URLStreamHandlerFactory JavaDoc urlHandlerFactory = null;
96     protected boolean urlHandlerFactorySet = false;
97
98     /**
99      *
100      */

101     private LinkedList JavaDoc elementOffsetStack = new LinkedList JavaDoc();
102     private int elementOffsetX = globalOffsetX;
103     private int elementOffsetY = globalOffsetY;
104
105     private Map JavaDoc penBoxes = new HashMap JavaDoc();//FIXMENOW is this working properly? forecolor is not part of the key
106

107     /**
108      *
109      */

110     protected final JRStyledTextParser styledTextParser = new JRStyledTextParser();
111
112     /**
113      *
114      */

115     protected Map JavaDoc dateFormatCache = new HashMap JavaDoc();
116     protected Map JavaDoc numberFormatCache = new HashMap JavaDoc();
117
118     
119     /**
120      *
121      */

122     protected JRAbstractExporter()
123     {
124     }
125     
126     
127     /**
128      *
129      */

130     public void reset()
131     {
132         parameters = new HashMap JavaDoc();
133         elementOffsetStack = new LinkedList JavaDoc();
134         penBoxes = new HashMap JavaDoc();
135     }
136     
137     
138     /**
139      *
140      */

141     public void setParameter(JRExporterParameter parameter, Object JavaDoc value)
142     {
143         parameters.put(parameter, value);
144     }
145
146
147     /**
148      *
149      */

150     public Object JavaDoc getParameter(JRExporterParameter parameter)
151     {
152         return parameters.get(parameter);
153     }
154
155
156     /**
157      *
158      */

159     public void setParameters(Map JavaDoc parameters)
160     {
161         this.parameters = parameters;
162     }
163     
164
165     /**
166      *
167      */

168     public Map JavaDoc getParameters()
169     {
170         return parameters;
171     }
172     
173
174     /**
175      *
176      */

177     public abstract void exportReport() throws JRException;
178
179
180     /**
181      *
182      */

183     protected void setOffset()
184     {
185         Integer JavaDoc offsetX = (Integer JavaDoc)parameters.get(JRExporterParameter.OFFSET_X);
186         if (offsetX != null)
187         {
188             globalOffsetX = offsetX.intValue();
189         }
190         else
191         {
192             globalOffsetX = 0;
193         }
194
195         Integer JavaDoc offsetY = (Integer JavaDoc)parameters.get(JRExporterParameter.OFFSET_Y);
196         if (offsetY != null)
197         {
198             globalOffsetY = offsetY.intValue();
199         }
200         else
201         {
202             globalOffsetY = 0;
203         }
204         
205         elementOffsetX = globalOffsetX;
206         elementOffsetY = globalOffsetY;
207     }
208     
209
210     /**
211      *
212      */

213     protected void setExportContext()
214     {
215         classLoaderSet = false;
216         urlHandlerFactorySet = false;
217         
218         classLoader = (ClassLoader JavaDoc)parameters.get(JRExporterParameter.CLASS_LOADER);
219         if (classLoader != null)
220         {
221             JRResourcesUtil.setThreadClassLoader(classLoader);
222             classLoaderSet = true;
223         }
224
225         urlHandlerFactory = (URLStreamHandlerFactory JavaDoc) parameters.get(JRExporterParameter.URL_HANDLER_FACTORY);
226         if (urlHandlerFactory != null)
227         {
228             JRResourcesUtil.setThreadURLHandlerFactory(urlHandlerFactory);
229             urlHandlerFactorySet = true;
230         }
231     }
232         
233
234     /**
235      *
236      */

237     protected void resetExportContext()
238     {
239         if (classLoaderSet)
240         {
241             JRResourcesUtil.resetClassLoader();
242         }
243         
244         if (urlHandlerFactorySet)
245         {
246             JRResourcesUtil.resetThreadURLHandlerFactory();
247         }
248     }
249
250     
251     /**
252      * @deprecated replaced by {@link #setExportContext() setExportContext}
253      */

254     protected void setClassLoader()
255     {
256         setExportContext();
257     }
258
259     
260     /**
261      * @deprecated replaced by {@link #resetExportContext() resetExportContext}
262      */

263     protected void resetClassLoader()
264     {
265         resetExportContext();
266     }
267
268
269     /**
270      *
271      */

272     protected void setInput() throws JRException
273     {
274         jasperPrintList = (List JavaDoc)parameters.get(JRExporterParameter.JASPER_PRINT_LIST);
275         if (jasperPrintList == null)
276         {
277             isModeBatch = false;
278             
279             jasperPrint = (JasperPrint)parameters.get(JRExporterParameter.JASPER_PRINT);
280             if (jasperPrint == null)
281             {
282                 InputStream JavaDoc is = (InputStream JavaDoc)parameters.get(JRExporterParameter.INPUT_STREAM);
283                 if (is != null)
284                 {
285                     jasperPrint = (JasperPrint)JRLoader.loadObject(is);
286                 }
287                 else
288                 {
289                     URL JavaDoc url = (URL JavaDoc)parameters.get(JRExporterParameter.INPUT_URL);
290                     if (url != null)
291                     {
292                         jasperPrint = (JasperPrint)JRLoader.loadObject(url);
293                     }
294                     else
295                     {
296                         File JavaDoc file = (File JavaDoc)parameters.get(JRExporterParameter.INPUT_FILE);
297                         if (file != null)
298                         {
299                             jasperPrint = (JasperPrint)JRLoader.loadObject(file);
300                         }
301                         else
302                         {
303                             String JavaDoc fileName = (String JavaDoc)parameters.get(JRExporterParameter.INPUT_FILE_NAME);
304                             if (fileName != null)
305                             {
306                                 jasperPrint = (JasperPrint)JRLoader.loadObject(fileName);
307                             }
308                             else
309                             {
310                                 throw new JRException("No input source supplied to the exporter.");
311                             }
312                         }
313                     }
314                 }
315             }
316
317             jasperPrintList = new ArrayList JavaDoc();
318             jasperPrintList.add(jasperPrint);
319         }
320         else
321         {
322             isModeBatch = true;
323
324             if (jasperPrintList.size() == 0)
325             {
326                 throw new JRException("Empty input source supplied to the exporter in batch mode.");
327             }
328
329             jasperPrint = (JasperPrint)jasperPrintList.get(0);
330         }
331     }
332     
333
334     /**
335      *
336      */

337     protected void setPageRange() throws JRException
338     {
339         int lastPageIndex = -1;
340         if (jasperPrint.getPages() != null)
341         {
342             lastPageIndex = jasperPrint.getPages().size() - 1;
343         }
344
345         Integer JavaDoc start = (Integer JavaDoc)parameters.get(JRExporterParameter.START_PAGE_INDEX);
346         if (start == null)
347         {
348             startPageIndex = 0;
349         }
350         else
351         {
352             startPageIndex = start.intValue();
353             if (startPageIndex < 0 || startPageIndex > lastPageIndex)
354             {
355                 throw new JRException("Start page index out of range : " + startPageIndex + " of " + lastPageIndex);
356             }
357         }
358
359         Integer JavaDoc end = (Integer JavaDoc)parameters.get(JRExporterParameter.END_PAGE_INDEX);
360         if (end == null)
361         {
362             endPageIndex = lastPageIndex;
363         }
364         else
365         {
366             endPageIndex = end.intValue();
367             if (endPageIndex < startPageIndex || endPageIndex > lastPageIndex)
368             {
369                 throw new JRException("End page index out of range : " + endPageIndex + " (" + startPageIndex + " : " + lastPageIndex + ")");
370             }
371         }
372
373         Integer JavaDoc index = (Integer JavaDoc)parameters.get(JRExporterParameter.PAGE_INDEX);
374         if (index != null)
375         {
376             int pageIndex = index.intValue();
377             if (pageIndex < 0 || pageIndex > lastPageIndex)
378             {
379                 throw new JRException("Page index out of range : " + pageIndex + " of " + lastPageIndex);
380             }
381             startPageIndex = pageIndex;
382             endPageIndex = pageIndex;
383         }
384     }
385     
386
387     /**
388      *
389      */

390     protected JRStyledText getStyledText(JRPrintText textElement, boolean setBackcolor)
391     {
392         JRStyledText styledText = null;
393
394         String JavaDoc text = textElement.getText();
395         if (text != null)
396         {
397             Map JavaDoc attributes = new HashMap JavaDoc();
398             attributes.putAll(JRFontUtil.setAttributes(attributes, textElement));
399             attributes.put(TextAttribute.FOREGROUND, textElement.getForecolor());
400             if (setBackcolor && textElement.getMode() == JRElement.MODE_OPAQUE)
401             {
402                 attributes.put(TextAttribute.BACKGROUND, textElement.getBackcolor());
403             }
404
405             if (textElement.isStyledText())
406             {
407                 try
408                 {
409                     styledText = styledTextParser.parse(attributes, text);
410                 }
411                 catch (SAXException JavaDoc e)
412                 {
413                     //ignore if invalid styled text and treat like normal text
414
}
415             }
416         
417             if (styledText == null)
418             {
419                 styledText = new JRStyledText();
420                 styledText.append(text);
421                 styledText.addRun(new JRStyledText.Run(attributes, 0, text.length()));
422             }
423         }
424         
425         return styledText;
426     }
427
428     
429     protected JRStyledText getStyledText(JRPrintText textElement)
430     {
431         return getStyledText(textElement, true);
432     }
433
434     
435     /**
436      *
437      */

438     protected void setOutput()
439     {
440     }
441
442
443     /**
444      * Returns the X axis offset used for element export.
445      * <p>
446      * This method should be used istead of {@link #globalOffsetX globalOffsetX} when
447      * exporting elements.
448      *
449      * @return the X axis offset
450      */

451     protected int getOffsetX()
452     {
453         return elementOffsetX;
454     }
455
456
457     /**
458      * Returns the Y axis offset used for element export.
459      * <p>
460      * This method should be used istead of {@link #globalOffsetY globalOffsetY} when
461      * exporting elements.
462      *
463      * @return the Y axis offset
464      */

465     protected int getOffsetY()
466     {
467         return elementOffsetY;
468     }
469
470     
471     /**
472      * Sets the offsets for exporting elements from a {@link JRPrintFrame frame}.
473      * <p>
474      * After the frame elements are exported, a call to {@link #restoreElementOffsets() popElementOffsets} is required
475      * so that the previous offsets are resored.
476      *
477      * @param frame
478      * @param relative
479      * @see #getOffsetX()
480      * @see #getOffsetY()
481      * @see #restoreElementOffsets()
482      */

483     protected void setFrameElementsOffset(JRPrintFrame frame, boolean relative)
484     {
485         if (relative)
486         {
487             setElementOffsets(0, 0);
488         }
489         else
490         {
491             int topPadding = frame.getTopPadding();
492             int leftPadding = frame.getLeftPadding();
493
494             setElementOffsets(getOffsetX() + frame.getX() + leftPadding, getOffsetY() + frame.getY() + topPadding);
495         }
496     }
497     
498     
499     private void setElementOffsets(int offsetX, int offsetY)
500     {
501         elementOffsetStack.addLast(new int[]{elementOffsetX, elementOffsetY});
502         
503         elementOffsetX = offsetX;
504         elementOffsetY = offsetY;
505     }
506
507     
508     /**
509      * Restores offsets after a call to
510      * {@link #setFrameElementsOffset(JRPrintFrame, boolean) setFrameElementsOffset}.
511      */

512     protected void restoreElementOffsets()
513     {
514         int[] offsets = (int[]) elementOffsetStack.removeLast();
515         elementOffsetX = offsets[0];
516         elementOffsetY = offsets[1];
517     }
518
519     
520     protected JRBox getBox(JRPrintGraphicElement element)
521     {
522         byte pen = element.getPen();
523         Object JavaDoc key = new Byte JavaDoc(pen);
524         JRBox box = (JRBox) penBoxes.get(key);
525         
526         if (box == null)
527         {
528             box = new JRBaseBox(pen, element.getForecolor());
529             
530             penBoxes.put(key, box);
531         }
532         
533         return box;
534     }
535
536     
537     protected String JavaDoc getTextFormatFactoryClass(JRPrintText text)
538     {
539         String JavaDoc formatFactoryClass = text.getFormatFactoryClass();
540         if (formatFactoryClass == null)
541         {
542             formatFactoryClass = jasperPrint.getFormatFactoryClass();
543         }
544         return formatFactoryClass;
545     }
546
547     protected Locale JavaDoc getTextLocale(JRPrintText text)
548     {
549         String JavaDoc localeCode = text.getLocaleCode();
550         if (localeCode == null)
551         {
552             localeCode = jasperPrint.getLocaleCode();
553         }
554         return localeCode == null ? null : JRDataUtils.getLocale(localeCode);
555     }
556
557     protected TimeZone JavaDoc getTextTimeZone(JRPrintText text)
558     {
559         String JavaDoc tzId = text.getTimeZoneId();
560         if (tzId == null)
561         {
562             tzId = jasperPrint.getTimeZoneId();
563         }
564         return tzId == null ? null : JRDataUtils.getTimeZone(tzId);
565     }
566     
567     protected TextValue getTextValue(JRPrintText text, String JavaDoc textStr)
568     {
569         TextValue textValue;
570         if (text.getValueClassName() == null)
571         {
572             textValue = getTextValueString(text, textStr);
573         }
574         else
575         {
576             try
577             {
578                 Class JavaDoc valueClass = JRClassLoader.loadClassForName(text.getValueClassName());
579                 if (java.lang.Number JavaDoc.class.isAssignableFrom(valueClass))
580                 {
581                     textValue = getNumberCellValue(text, textStr);
582                 }
583                 else if (Date JavaDoc.class.isAssignableFrom(valueClass))
584                 {
585                     textValue = getDateCellValue(text, textStr);
586                 }
587                 else if (Boolean JavaDoc.class.equals(valueClass))
588                 {
589                     textValue = getBooleanCellValue(text, textStr);
590                 }
591                 else
592                 {
593                     textValue = getTextValueString(text, textStr);
594                 }
595             }
596             catch (ParseException JavaDoc e)
597             {
598                 //log.warn("Error parsing text value", e);
599
textValue = getTextValueString(text, textStr);
600             }
601             catch (ClassNotFoundException JavaDoc e)
602             {
603                 //log.warn("Error loading text value class", e);
604
textValue = getTextValueString(text, textStr);
605             }
606         }
607         return textValue;
608     }
609
610     protected TextValue getTextValueString(JRPrintText text, String JavaDoc textStr)
611     {
612         return new StringTextValue(textStr);
613     }
614
615     protected TextValue getBooleanCellValue(JRPrintText text, String JavaDoc textStr)
616     {
617         Boolean JavaDoc value = null;
618         if (textStr != null || textStr.length() > 0)
619         {
620             value = Boolean.valueOf(textStr);
621         }
622         return new BooleanTextValue(textStr, value);
623     }
624
625     protected TextValue getDateCellValue(JRPrintText text, String JavaDoc textStr) throws ParseException JavaDoc
626     {
627         TextValue textValue;
628         String JavaDoc pattern = text.getPattern();
629         if (pattern == null || pattern.trim().length() == 0)
630         {
631             textValue = getTextValueString(text, textStr);
632         }
633         else
634         {
635             DateFormat JavaDoc dateFormat = getDateFormat(getTextFormatFactoryClass(text), pattern, getTextLocale(text), getTextTimeZone(text));
636             
637             Date JavaDoc value = null;
638             if (textStr != null && textStr.length() > 0)
639             {
640                 value = dateFormat.parse(textStr);
641             }
642             textValue = new DateTextValue(textStr, value, pattern);
643         }
644         return textValue;
645     }
646
647     protected TextValue getNumberCellValue(JRPrintText text, String JavaDoc textStr) throws ParseException JavaDoc, ClassNotFoundException JavaDoc
648     {
649         TextValue textValue;
650         String JavaDoc pattern = text.getPattern();
651         if (pattern == null || pattern.trim().length() == 0)
652         {
653             if (textStr != null && textStr.length() > 0)
654             {
655                 Number JavaDoc value = defaultParseNumber(textStr, JRClassLoader.loadClassForName(text.getValueClassName()));
656
657                 if (value != null)
658                 {
659                     textValue = new NumberTextValue(textStr, value, null);
660                 }
661                 else
662                 {
663                     textValue = getTextValueString(text, textStr);
664                 }
665             }
666             else
667             {
668                 textValue = new NumberTextValue(textStr, null, null);
669             }
670         }
671         else
672         {
673             NumberFormat JavaDoc numberFormat = getNumberFormat(getTextFormatFactoryClass(text), pattern, getTextLocale(text));
674             
675             Number JavaDoc value = null;
676             if (textStr != null && textStr.length() > 0)
677             {
678                 value = numberFormat.parse(textStr);
679             }
680             textValue = new NumberTextValue(textStr, value, pattern);
681         }
682         return textValue;
683     }
684
685
686     protected Number JavaDoc defaultParseNumber(String JavaDoc textStr, Class JavaDoc valueClass)
687     {
688         Number JavaDoc value = null;
689         try
690         {
691             if (valueClass.equals(Byte JavaDoc.class))
692             {
693                 value = Byte.valueOf(textStr);
694             }
695             else if (valueClass.equals(Short JavaDoc.class))
696             {
697                 value = Short.valueOf(textStr);
698             }
699             else if (valueClass.equals(Integer JavaDoc.class))
700             {
701                 value = Integer.valueOf(textStr);
702             }
703             else if (valueClass.equals(Long JavaDoc.class))
704             {
705                 value = Long.valueOf(textStr);
706             }
707             else if (valueClass.equals(Float JavaDoc.class))
708             {
709                 value = Float.valueOf(textStr);
710             }
711             else if (valueClass.equals(Double JavaDoc.class))
712             {
713                 value = Double.valueOf(textStr);
714             }
715             else if (valueClass.equals(BigInteger JavaDoc.class))
716             {
717                 value = new BigInteger JavaDoc(textStr);
718             }
719             else if (valueClass.equals(BigDecimal JavaDoc.class))
720             {
721                 value = new BigDecimal JavaDoc(textStr);
722             }
723         }
724         catch (NumberFormatException JavaDoc e)
725         {
726             //skip
727
}
728         return value;
729     }
730     
731
732     protected DateFormat JavaDoc getDateFormat(String JavaDoc formatFactoryClass, String JavaDoc pattern, Locale JavaDoc lc, TimeZone JavaDoc tz)
733     {
734         String JavaDoc key = formatFactoryClass
735             + "|" + pattern
736             + "|" + JRDataUtils.getLocaleCode(lc)
737             + "|" + JRDataUtils.getTimeZoneId(tz);
738         DateFormat JavaDoc dateFormat = (DateFormat JavaDoc)dateFormatCache.get(key);
739         if (dateFormat == null)
740         {
741             FormatFactory formatFactory = DefaultFormatFactory.createFormatFactory(formatFactoryClass);//FIXMEFORMAT cache this too
742
dateFormat = formatFactory.createDateFormat(pattern, lc, tz);
743             dateFormatCache.put(key, dateFormat);
744         }
745         return dateFormat;
746     }
747     
748
749     protected NumberFormat JavaDoc getNumberFormat(String JavaDoc formatFactoryClass, String JavaDoc pattern, Locale JavaDoc lc)
750     {
751         String JavaDoc key = formatFactoryClass
752             + "|" + pattern
753             + "|" + JRDataUtils.getLocaleCode(lc);
754         NumberFormat JavaDoc numberFormat = (NumberFormat JavaDoc)numberFormatCache.get(key);
755         if (numberFormat == null)
756         {
757             FormatFactory formatFactory = DefaultFormatFactory.createFormatFactory(formatFactoryClass);//FIXMEFORMAT cache this too
758
numberFormat = formatFactory.createNumberFormat(pattern, lc);
759             dateFormatCache.put(key, numberFormat);
760         }
761         return numberFormat;
762     }
763
764 }
765
Popular Tags