KickJava   Java API By Example, From Geeks To Geeks.

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


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
29 /*
30  * Contributors:
31  * Joakim Sandström - sanjoa@users.sourceforge.net
32  */

33 package net.sf.jasperreports.engine.export;
34
35 import java.awt.Dimension JavaDoc;
36 import java.io.BufferedWriter JavaDoc;
37 import java.io.ByteArrayInputStream JavaDoc;
38 import java.io.ByteArrayOutputStream JavaDoc;
39 import java.io.File JavaDoc;
40 import java.io.FileOutputStream JavaDoc;
41 import java.io.IOException JavaDoc;
42 import java.io.OutputStream JavaDoc;
43 import java.io.OutputStreamWriter JavaDoc;
44 import java.io.StringWriter JavaDoc;
45 import java.io.Writer JavaDoc;
46 import java.util.Collection JavaDoc;
47 import java.util.HashMap JavaDoc;
48 import java.util.Iterator JavaDoc;
49 import java.util.List JavaDoc;
50 import java.util.Map JavaDoc;
51
52 import net.sf.jasperreports.engine.JRAbstractExporter;
53 import net.sf.jasperreports.engine.JRAnchor;
54 import net.sf.jasperreports.engine.JRBox;
55 import net.sf.jasperreports.engine.JRElement;
56 import net.sf.jasperreports.engine.JRException;
57 import net.sf.jasperreports.engine.JRExporterParameter;
58 import net.sf.jasperreports.engine.JRFont;
59 import net.sf.jasperreports.engine.JRHyperlink;
60 import net.sf.jasperreports.engine.JRImage;
61 import net.sf.jasperreports.engine.JRImageRenderer;
62 import net.sf.jasperreports.engine.JRLine;
63 import net.sf.jasperreports.engine.JRPrintElement;
64 import net.sf.jasperreports.engine.JRPrintEllipse;
65 import net.sf.jasperreports.engine.JRPrintFrame;
66 import net.sf.jasperreports.engine.JRPrintGraphicElement;
67 import net.sf.jasperreports.engine.JRPrintHyperlink;
68 import net.sf.jasperreports.engine.JRPrintHyperlinkParameter;
69 import net.sf.jasperreports.engine.JRPrintHyperlinkParameters;
70 import net.sf.jasperreports.engine.JRPrintImage;
71 import net.sf.jasperreports.engine.JRPrintLine;
72 import net.sf.jasperreports.engine.JRPrintPage;
73 import net.sf.jasperreports.engine.JRPrintRectangle;
74 import net.sf.jasperreports.engine.JRPrintText;
75 import net.sf.jasperreports.engine.JRRenderable;
76 import net.sf.jasperreports.engine.JRReport;
77 import net.sf.jasperreports.engine.JRReportFont;
78 import net.sf.jasperreports.engine.JRRuntimeException;
79 import net.sf.jasperreports.engine.JRStyle;
80 import net.sf.jasperreports.engine.JRWrappingSvgRenderer;
81 import net.sf.jasperreports.engine.util.JRValueStringUtils;
82 import net.sf.jasperreports.engine.util.JRXmlWriteHelper;
83 import net.sf.jasperreports.engine.xml.JRPrintElementFactory;
84 import net.sf.jasperreports.engine.xml.JRPrintHyperlinkParameterFactory;
85 import net.sf.jasperreports.engine.xml.JRPrintHyperlinkParameterValueFactory;
86 import net.sf.jasperreports.engine.xml.JRPrintImageFactory;
87 import net.sf.jasperreports.engine.xml.JRPrintTextFactory;
88 import net.sf.jasperreports.engine.xml.JRXmlConstants;
89 import net.sf.jasperreports.engine.xml.JasperPrintFactory;
90
91 import org.w3c.tools.codec.Base64Encoder;
92
93
94 /**
95  * Exports a JasperReports document to an XML file that contains the same data as a {@link net.sf.jasperreports.engine.JasperPrint}
96  * object, but in XML format, instead of a serialized class. Such XML files can be parsed back into <tt>JasperPrint</tt>
97  * object using the {@link net.sf.jasperreports.engine.xml.JRPrintXmlLoader} utility class. Their structure is validated
98  * against an internal DTD file called jasperprint.dtd
99  *
100  * @author Teodor Danciu (teodord@users.sourceforge.net)
101  * @version $Id: JRXmlExporter.java 1536 2006-12-22 12:34:40 +0200 (Fri, 22 Dec 2006) teodord $
102  */

103 public class JRXmlExporter extends JRAbstractExporter
104 {
105
106     /**
107      *
108      */

109     protected JRXmlWriteHelper xmlWriter = null;
110     protected String JavaDoc encoding = null;
111     
112     protected JRExportProgressMonitor progressMonitor = null;
113     protected Map JavaDoc rendererToImagePathMap = null;
114     protected Map JavaDoc imageNameToImageDataMap = null;
115     protected Map JavaDoc fontsMap = new HashMap JavaDoc();
116     protected Map JavaDoc stylesMap = new HashMap JavaDoc();
117
118     /**
119      *
120      */

121     protected String JavaDoc dtdLocation = null;
122     protected boolean isEmbeddingImages = true;
123     protected File JavaDoc destFile = null;
124     protected File JavaDoc imagesDir = null;
125
126     /**
127      *
128      */

129     protected static final int colorMask = Integer.parseInt("FFFFFF", 16);
130     
131     /**
132      *
133      */

134     private static int imageId = 0;
135
136
137     /**
138      *
139      */

140     public void exportReport() throws JRException
141     {
142         progressMonitor = (JRExportProgressMonitor)parameters.get(JRExporterParameter.PROGRESS_MONITOR);
143         
144         /* */
145         setOffset();
146
147         try
148         {
149             /* */
150             setExportContext();
151     
152             /* */
153             setInput();
154     
155             /* */
156             setPageRange();
157     
158             dtdLocation = (String JavaDoc)parameters.get(JRXmlExporterParameter.DTD_LOCATION);
159             if (dtdLocation == null)
160             {
161                 dtdLocation = "http://jasperreports.sourceforge.net/dtds/jasperprint.dtd";
162             }
163             
164             encoding = (String JavaDoc)parameters.get(JRExporterParameter.CHARACTER_ENCODING);
165             if (encoding == null)
166             {
167                 encoding = "UTF-8";
168             }
169             
170             StringBuffer JavaDoc sb = (StringBuffer JavaDoc)parameters.get(JRExporterParameter.OUTPUT_STRING_BUFFER);
171             if (sb != null)
172             {
173                 StringBuffer JavaDoc buffer = exportReportToBuffer();
174                 sb.append(buffer.toString());
175             }
176             else
177             {
178                 Writer JavaDoc outWriter = (Writer JavaDoc)parameters.get(JRExporterParameter.OUTPUT_WRITER);
179                 if (outWriter != null)
180                 {
181                     try
182                     {
183                         exportReportToStream(outWriter);
184                     }
185                     catch (IOException JavaDoc e)
186                     {
187                         throw new JRException("Error writing to writer : " + jasperPrint.getName(), e);
188                     }
189                 }
190                 else
191                 {
192                     OutputStream JavaDoc os = (OutputStream JavaDoc)parameters.get(JRExporterParameter.OUTPUT_STREAM);
193                     if (os != null)
194                     {
195                         try
196                         {
197                             exportReportToStream(new OutputStreamWriter JavaDoc(os, encoding));
198                         }
199                         catch (Exception JavaDoc e)
200                         {
201                             throw new JRException("Error writing to OutputStream : " + jasperPrint.getName(), e);
202                         }
203                     }
204                     else
205                     {
206                         destFile = (File JavaDoc)parameters.get(JRExporterParameter.OUTPUT_FILE);
207                         if (destFile == null)
208                         {
209                             String JavaDoc fileName = (String JavaDoc)parameters.get(JRExporterParameter.OUTPUT_FILE_NAME);
210                             if (fileName != null)
211                             {
212                                 destFile = new File JavaDoc(fileName);
213                             }
214                             else
215                             {
216                                 throw new JRException("No output specified for the exporter.");
217                             }
218                         }
219                         
220                         imagesDir = new File JavaDoc(destFile.getParent(), destFile.getName() + "_files");
221                         
222                         Boolean JavaDoc isEmbeddingImagesParameter = (Boolean JavaDoc)parameters.get(JRXmlExporterParameter.IS_EMBEDDING_IMAGES);
223                         if (isEmbeddingImagesParameter == null)
224                         {
225                             isEmbeddingImagesParameter = Boolean.TRUE;
226                         }
227                         isEmbeddingImages = isEmbeddingImagesParameter.booleanValue();
228                         
229                         exportReportToFile();
230                     }
231                 }
232             }
233         }
234         finally
235         {
236             resetExportContext();
237         }
238     }
239
240
241     /**
242      *
243      */

244     protected void exportReportToFile() throws JRException
245     {
246         //if (!isEmbeddingImages)
247
{
248             rendererToImagePathMap = new HashMap JavaDoc();
249             imageNameToImageDataMap = new HashMap JavaDoc();
250         }
251                 
252         Writer JavaDoc writer = null;
253         try
254         {
255             OutputStream JavaDoc fileOutputStream = new FileOutputStream JavaDoc(destFile);
256             writer = new BufferedWriter JavaDoc(new OutputStreamWriter JavaDoc(fileOutputStream, encoding));
257             exportReportToStream(writer);
258         }
259         catch (IOException JavaDoc e)
260         {
261             throw new JRException("Error writing to file : " + destFile, e);
262         }
263         finally
264         {
265             if (writer != null)
266             {
267                 try
268                 {
269                     writer.close();
270                 }
271                 catch(IOException JavaDoc e)
272                 {
273                 }
274             }
275         }
276         
277         if (!isEmbeddingImages)
278         {
279             Collection JavaDoc imageNames = imageNameToImageDataMap.keySet();
280             if (imageNames != null && imageNames.size() > 0)
281             {
282                 if (!imagesDir.exists())
283                 {
284                     imagesDir.mkdir();
285                 }
286     
287                 for(Iterator JavaDoc it = imageNames.iterator(); it.hasNext();)
288                 {
289                     String JavaDoc imageName = (String JavaDoc)it.next();
290                     byte[] imageData = (byte[])imageNameToImageDataMap.get(imageName);
291
292                     File JavaDoc imageFile = new File JavaDoc(imagesDir, imageName);
293
294                     OutputStream JavaDoc fos = null;
295                     try
296                     {
297                         fos = new FileOutputStream JavaDoc(imageFile);
298                         fos.write(imageData, 0, imageData.length);
299                     }
300                     catch (IOException JavaDoc e)
301                     {
302                         throw new JRException("Error writing to image file : " + imageFile, e);
303                     }
304                     finally
305                     {
306                         if (fos != null)
307                         {
308                             try
309                             {
310                                 fos.close();
311                             }
312                             catch(IOException JavaDoc e)
313                             {
314                             }
315                         }
316                     }
317                 }
318             }
319         }
320     }
321     
322     
323     /**
324      *
325      */

326     protected StringBuffer JavaDoc exportReportToBuffer() throws JRException
327     {
328         StringWriter JavaDoc buffer = new StringWriter JavaDoc();
329         try
330         {
331             exportReportToStream(buffer);
332         }
333         catch (IOException JavaDoc e)
334         {
335             throw new JRException("Error while exporting report to buffer", e);
336         }
337         return buffer.getBuffer();
338     }
339
340
341     protected void exportReportToStream(Writer JavaDoc writer) throws JRException, IOException JavaDoc
342     {
343         xmlWriter = new JRXmlWriteHelper(writer);
344         
345         xmlWriter.writeProlog(encoding);
346         xmlWriter.writePublicDoctype("jasperPrint", "-//JasperReports//DTD Report Design//EN", dtdLocation);
347
348         xmlWriter.startElement(JasperPrintFactory.ELEMENT_jasperPrint);
349         xmlWriter.addEncodedAttribute(JasperPrintFactory.ATTRIBUTE_name, jasperPrint.getName());
350         xmlWriter.addAttribute(JasperPrintFactory.ATTRIBUTE_pageWidth, jasperPrint.getPageWidth());
351         xmlWriter.addAttribute(JasperPrintFactory.ATTRIBUTE_pageHeight, jasperPrint.getPageHeight());
352         xmlWriter.addAttribute(JasperPrintFactory.ATTRIBUTE_orientation, jasperPrint.getOrientation(), JRXmlConstants.getOrientationMap(), JRReport.ORIENTATION_PORTRAIT);
353         xmlWriter.addAttribute(JasperPrintFactory.ATTRIBUTE_formatFactoryClass, jasperPrint.getFormatFactoryClass());
354         xmlWriter.addEncodedAttribute(JasperPrintFactory.ATTRIBUTE_locale, jasperPrint.getLocaleCode());
355         xmlWriter.addEncodedAttribute(JasperPrintFactory.ATTRIBUTE_timezone, jasperPrint.getTimeZoneId());
356         
357         JRReportFont[] fonts = jasperPrint.getFonts();
358         if (fonts != null && fonts.length > 0)
359         {
360             for(int i = 0; i < fonts.length; i++)
361             {
362                 fontsMap.put(fonts[i].getName(), fonts[i]);
363                 exportReportFont(fonts[i]);
364             }
365         }
366         
367         JRStyle[] styles = jasperPrint.getStyles();
368         if (styles != null && styles.length > 0)
369         {
370             for(int i = 0; i < styles.length; i++)
371             {
372                 stylesMap.put(styles[i].getName(), styles[i]);
373                 exportStyle(styles[i]);
374             }
375         }
376
377
378         List JavaDoc pages = jasperPrint.getPages();
379         if (pages != null && pages.size() > 0)
380         {
381             JRPrintPage page = null;
382             for(int i = startPageIndex; i <= endPageIndex; i++)
383             {
384                 if (Thread.currentThread().isInterrupted())
385                 {
386                     throw new JRException("Current thread interrupted.");
387                 }
388                 
389                 page = (JRPrintPage)pages.get(i);
390     
391                 /* */
392                 exportPage(page);
393             }
394         }
395
396         xmlWriter.closeElement();
397         
398         writer.flush();
399     }
400
401
402     /**
403      * @throws IOException
404      *
405      */

406     protected void exportReportFont(JRReportFont font) throws IOException JavaDoc
407     {
408         xmlWriter.startElement("reportFont");
409         xmlWriter.addEncodedAttribute("name", font.getName());
410         xmlWriter.addAttribute("isDefault", font.isDefault());
411         xmlWriter.addEncodedAttribute("fontName", font.getFontName());
412         xmlWriter.addAttribute("size", font.getFontSize());
413         xmlWriter.addAttribute("isBold", font.isBold());
414         xmlWriter.addAttribute("isItalic", font.isItalic());
415         xmlWriter.addAttribute("isUnderline", font.isUnderline());
416         xmlWriter.addAttribute("isStrikeThrough", font.isStrikeThrough());
417         xmlWriter.addEncodedAttribute("pdfFontName", font.getPdfFontName());
418         xmlWriter.addEncodedAttribute("pdfEncoding", font.getPdfEncoding());
419         xmlWriter.addAttribute("isPdfEmbedded", font.isPdfEmbedded());
420         xmlWriter.closeElement();
421     }
422
423
424     /**
425      * @throws IOException
426      */

427     protected void exportStyle(JRStyle style) throws IOException JavaDoc
428     {
429         xmlWriter.startElement("style");
430         xmlWriter.addEncodedAttribute("name", style.getName());
431         xmlWriter.addAttribute("isDefault", style.isDefault());
432
433         if (style.getStyle() != null)
434         {
435             JRStyle baseStyle =
436                 (JRStyle)stylesMap.get(
437                         style.getStyle().getName()
438                     );
439             if(baseStyle != null)
440             {
441                 xmlWriter.addEncodedAttribute("style", style.getStyle().getName());
442             }
443             else
444             {
445                 throw
446                     new JRRuntimeException(
447                         "Referenced report style not found : "
448                         + style.getStyle().getName()
449                         );
450             }
451         }
452     
453         xmlWriter.addAttribute("mode", style.getOwnMode(), JRXmlConstants.getModeMap());
454         xmlWriter.addAttribute("forecolor", style.getOwnForecolor());
455         xmlWriter.addAttribute("backcolor", style.getOwnBackcolor());
456         xmlWriter.addAttribute("pen", style.getOwnPen(), JRXmlConstants.getPenMap());
457         xmlWriter.addAttribute("fill", style.getOwnFill(), JRXmlConstants.getFillMap());
458         xmlWriter.addAttribute("radius", style.getOwnRadius());
459         xmlWriter.addAttribute("scaleImage", style.getOwnScaleImage(), JRXmlConstants.getScaleImageMap());
460         xmlWriter.addAttribute("hAlign", style.getOwnHorizontalAlignment(), JRXmlConstants.getHorizontalAlignMap());
461         xmlWriter.addAttribute("vAlign", style.getOwnVerticalAlignment(), JRXmlConstants.getVerticalAlignMap());
462         xmlWriter.addAttribute("rotation", style.getOwnRotation(), JRXmlConstants.getRotationMap());
463         xmlWriter.addAttribute("lineSpacing", style.getOwnLineSpacing(), JRXmlConstants.getLineSpacingMap());
464         xmlWriter.addAttribute("isStyledText", style.isOwnStyledText());
465         //xmlWriter.addEncodedAttribute("pattern", style.getOwnPattern());//FIXME if pattern in text field is equal to this, then it should be removed there (inheritance)
466
//xmlWriter.addAttribute("isBlankWhenNull", style.isOwnBlankWhenNull());
467

468         xmlWriter.addAttribute("border", style.getOwnBorder(), JRXmlConstants.getPenMap());
469         xmlWriter.addAttribute("borderColor", style.getOwnBorderColor());
470         xmlWriter.addAttribute("padding", style.getOwnPadding());
471         
472         xmlWriter.addAttribute("topBorder", style.getOwnTopBorder(), JRXmlConstants.getPenMap());
473         xmlWriter.addAttribute("topBorderColor", style.getOwnTopBorderColor());
474         xmlWriter.addAttribute("topPadding", style.getOwnTopPadding());
475         
476         xmlWriter.addAttribute("leftBorder", style.getOwnLeftBorder(), JRXmlConstants.getPenMap());
477         xmlWriter.addAttribute("leftBorderColor", style.getOwnLeftBorderColor());
478         xmlWriter.addAttribute("leftPadding", style.getOwnLeftPadding());
479         
480         xmlWriter.addAttribute("bottomBorder", style.getOwnBottomBorder(), JRXmlConstants.getPenMap());
481         xmlWriter.addAttribute("bottomBorderColor", style.getOwnBottomBorderColor());
482         xmlWriter.addAttribute("bottomPadding", style.getOwnBottomPadding());
483         
484         xmlWriter.addAttribute("rightBorder", style.getOwnRightBorder(), JRXmlConstants.getPenMap());
485         xmlWriter.addAttribute("rightBorderColor", style.getOwnRightBorderColor());
486         xmlWriter.addAttribute("rightPadding", style.getOwnRightPadding());
487
488         xmlWriter.addEncodedAttribute("fontName", style.getOwnFontName());
489         xmlWriter.addAttribute("fontSize", style.getOwnFontSize());
490         xmlWriter.addAttribute("isBold", style.isOwnBold());
491         xmlWriter.addAttribute("isItalic", style.isOwnItalic());
492         xmlWriter.addAttribute("isUnderline", style.isOwnUnderline());
493         xmlWriter.addAttribute("isStrikeThrough", style.isOwnStrikeThrough());
494         xmlWriter.addEncodedAttribute("pdfFontName", style.getOwnPdfFontName());
495         xmlWriter.addEncodedAttribute("pdfEncoding", style.getOwnPdfEncoding());
496         xmlWriter.addAttribute("isPdfEmbedded", style.isOwnPdfEmbedded());
497
498         xmlWriter.closeElement();
499     }
500
501
502     /**
503      * @throws IOException
504      *
505      */

506     protected void exportPage(JRPrintPage page) throws JRException, IOException JavaDoc
507     {
508         xmlWriter.startElement("page");
509
510         Collection JavaDoc elements = page.getElements();
511         exportElements(elements);
512
513         xmlWriter.closeElement();
514         
515         if (progressMonitor != null)
516         {
517             progressMonitor.afterPageExport();
518         }
519     }
520
521
522     protected void exportElements(Collection JavaDoc elements) throws IOException JavaDoc, JRException
523     {
524         if (elements != null && elements.size() > 0)
525         {
526             JRPrintElement element;
527             for(Iterator JavaDoc it = elements.iterator(); it.hasNext();)
528             {
529                 element = (JRPrintElement)it.next();
530                 
531                 if (element instanceof JRPrintLine)
532                 {
533                     exportLine((JRPrintLine)element);
534                 }
535                 else if (element instanceof JRPrintRectangle)
536                 {
537                     exportRectangle((JRPrintRectangle)element);
538                 }
539                 else if (element instanceof JRPrintEllipse)
540                 {
541                     exportEllipse((JRPrintEllipse)element);
542                 }
543                 else if (element instanceof JRPrintImage)
544                 {
545                     exportImage((JRPrintImage)element);
546                 }
547                 else if (element instanceof JRPrintText)
548                 {
549                     exportText((JRPrintText)element);
550                 }
551                 else if (element instanceof JRPrintFrame)
552                 {
553                     exportFrame((JRPrintFrame) element);
554                 }
555             }
556         }
557     }
558
559
560     /**
561      * @throws IOException
562      *
563      */

564     protected void exportLine(JRPrintLine line) throws IOException JavaDoc
565     {
566         xmlWriter.startElement("line");
567         xmlWriter.addAttribute("direction", line.getDirection(), JRXmlConstants.getDirectionMap(), JRLine.DIRECTION_TOP_DOWN);
568
569         exportReportElement(line);
570         exportGraphicElement(line);
571         
572         xmlWriter.closeElement();
573     }
574
575
576     /**
577      * @throws IOException
578      *
579      */

580     protected void exportReportElement(JRPrintElement element) throws IOException JavaDoc
581     {
582         xmlWriter.startElement("reportElement");
583         xmlWriter.addEncodedAttribute(JRPrintElementFactory.ATTRIBUTE_key, element.getKey());
584         JRStyle style = element.getStyle();
585         if (style != null)
586         {
587             xmlWriter.addEncodedAttribute("style", style.getName());
588         }
589         xmlWriter.addAttribute("mode", element.getOwnMode(), JRXmlConstants.getModeMap());
590         xmlWriter.addAttribute("x", element.getX() + getOffsetX());
591         xmlWriter.addAttribute("y", element.getY() + getOffsetY());
592         xmlWriter.addAttribute("width", element.getWidth());
593         xmlWriter.addAttribute("height", element.getHeight());
594         xmlWriter.addAttribute("forecolor", element.getOwnForecolor());
595         xmlWriter.addAttribute("backcolor", element.getOwnBackcolor());
596         xmlWriter.closeElement();
597     }
598
599
600     /**
601      * @throws IOException
602      *
603      */

604     protected void exportGraphicElement(JRPrintGraphicElement element) throws IOException JavaDoc
605     {
606         xmlWriter.startElement("graphicElement");
607         xmlWriter.addAttribute("pen", element.getOwnPen(), JRXmlConstants.getPenMap());
608         xmlWriter.addAttribute("fill", element.getOwnFill(), JRXmlConstants.getFillMap());
609         xmlWriter.closeElement();
610     }
611
612
613     /**
614      * @throws IOException
615      *
616      */

617     protected void exportRectangle(JRPrintRectangle rectangle) throws IOException JavaDoc
618     {
619         xmlWriter.startElement("rectangle");
620         xmlWriter.addAttribute("radius", rectangle.getOwnRadius());
621
622         exportReportElement(rectangle);
623         exportGraphicElement(rectangle);
624         
625         xmlWriter.closeElement();
626     }
627
628
629     /**
630      * @throws IOException
631      *
632      */

633     protected void exportEllipse(JRPrintEllipse ellipse) throws IOException JavaDoc
634     {
635         xmlWriter.startElement("ellipse");
636
637         exportReportElement(ellipse);
638         exportGraphicElement(ellipse);
639         
640         xmlWriter.closeElement();
641     }
642
643
644     /**
645      * @throws JRException
646      * @throws IOException
647      *
648      */

649     protected void exportImage(JRPrintImage image) throws JRException, IOException JavaDoc
650     {
651         xmlWriter.startElement("image");
652         xmlWriter.addAttribute("scaleImage", image.getOwnScaleImage(), JRXmlConstants.getScaleImageMap());
653         xmlWriter.addAttribute("hAlign", image.getOwnHorizontalAlignment(), JRXmlConstants.getHorizontalAlignMap());
654         xmlWriter.addAttribute("vAlign", image.getOwnVerticalAlignment(), JRXmlConstants.getVerticalAlignMap());
655         xmlWriter.addAttribute("isLazy", image.isLazy(), false);
656         xmlWriter.addAttribute("onErrorType", image.getOnErrorType(), JRXmlConstants.getOnErrorTypeMap(), JRImage.ON_ERROR_TYPE_ERROR);
657         xmlWriter.addEncodedAttribute("hyperlinkType", image.getLinkType());
658         xmlWriter.addAttribute("hyperlinkTarget", image.getHyperlinkTarget(), JRXmlConstants.getHyperlinkTargetMap(), JRHyperlink.HYPERLINK_TARGET_SELF);
659         xmlWriter.addEncodedAttribute("anchorName", image.getAnchorName());
660         xmlWriter.addEncodedAttribute("hyperlinkReference", image.getHyperlinkReference());
661         xmlWriter.addEncodedAttribute("hyperlinkAnchor", image.getHyperlinkAnchor());
662         xmlWriter.addAttribute("hyperlinkPage", image.getHyperlinkPage());
663         xmlWriter.addEncodedAttribute(JRPrintImageFactory.ATTRIBUTE_hyperlinkTooltip, image.getHyperlinkTooltip());
664         xmlWriter.addAttribute("bookmarkLevel", image.getBookmarkLevel(), JRAnchor.NO_BOOKMARK);
665
666         exportReportElement(image);
667         exportBox(image);
668         exportGraphicElement(image);
669         
670
671         JRRenderable renderer = image.getRenderer();
672         if (renderer != null)
673         {
674             xmlWriter.startElement("imageSource");
675             xmlWriter.addAttribute("isEmbedded", isEmbeddingImages && !image.isLazy(), false);
676     
677             String JavaDoc imageSource = "";
678             
679             if (renderer.getType() == JRRenderable.TYPE_SVG)
680             {
681                 renderer =
682                     new JRWrappingSvgRenderer(
683                         renderer,
684                         new Dimension JavaDoc(image.getWidth(), image.getHeight()),
685                         JRElement.MODE_OPAQUE == image.getMode() ? image.getBackcolor() : null
686                         );
687             }
688                 
689             if (isEmbeddingImages && !image.isLazy())
690             {
691                 try
692                 {
693                     ByteArrayInputStream JavaDoc bais = new ByteArrayInputStream JavaDoc(renderer.getImageData());
694                     ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
695                     
696                     Base64Encoder encoder = new Base64Encoder(bais, baos);
697                     encoder.process();
698                     
699                     imageSource = new String JavaDoc(baos.toByteArray(), "UTF-8");
700                 }
701                 catch (IOException JavaDoc e)
702                 {
703                     throw new JRException("Error embedding image into XML.", e);
704                 }
705             }
706             else
707             {
708                 if (renderer.getType() == JRRenderable.TYPE_IMAGE && rendererToImagePathMap.containsKey(renderer))
709                 {
710                     imageSource = (String JavaDoc)rendererToImagePathMap.get(renderer);
711                 }
712                 else
713                 {
714                     if (image.isLazy())
715                     {
716                         imageSource = ((JRImageRenderer)renderer).getImageLocation();
717                     }
718                     else
719                     {
720                         imageSource = "img_" + getNextImageId();
721                         imageNameToImageDataMap.put(imageSource, renderer.getImageData());
722                         
723                         imageSource = new File JavaDoc(imagesDir, imageSource).getPath();
724                     }
725
726                     rendererToImagePathMap.put(renderer, imageSource);
727                 }
728             }
729             
730             xmlWriter.writeCDATA(imageSource);
731             xmlWriter.closeElement();
732         }
733         
734         exportHyperlinkParameters(image);
735         
736         xmlWriter.closeElement();
737     }
738
739
740     /**
741      * @throws IOException
742      *
743      */

744     protected void exportText(JRPrintText text) throws IOException JavaDoc
745     {
746         xmlWriter.startElement("text");
747         xmlWriter.addAttribute("textAlignment", text.getOwnHorizontalAlignment(), JRXmlConstants.getHorizontalAlignMap());
748         xmlWriter.addAttribute("verticalAlignment", text.getOwnVerticalAlignment(), JRXmlConstants.getVerticalAlignMap());
749         xmlWriter.addAttribute("textHeight", text.getTextHeight());
750         xmlWriter.addAttribute("rotation", text.getOwnRotation(), JRXmlConstants.getRotationMap());
751         xmlWriter.addAttribute("runDirection", text.getRunDirection(), JRXmlConstants.getRunDirectionMap(), JRPrintText.RUN_DIRECTION_LTR);
752         xmlWriter.addAttribute("lineSpacing", text.getOwnLineSpacing(), JRXmlConstants.getLineSpacingMap());
753         xmlWriter.addAttribute("isStyledText", text.isOwnStyledText());
754         xmlWriter.addAttribute("lineSpacingFactor", text.getLineSpacingFactor());
755         xmlWriter.addAttribute("leadingOffset", text.getLeadingOffset());
756         xmlWriter.addEncodedAttribute("hyperlinkType", text.getLinkType());
757         xmlWriter.addAttribute("hyperlinkTarget", text.getHyperlinkTarget(), JRXmlConstants.getHyperlinkTargetMap(), JRHyperlink.HYPERLINK_TARGET_SELF);
758         xmlWriter.addEncodedAttribute("anchorName", text.getAnchorName());
759         xmlWriter.addEncodedAttribute("hyperlinkReference", text.getHyperlinkReference());
760         xmlWriter.addEncodedAttribute("hyperlinkAnchor", text.getHyperlinkAnchor());
761         xmlWriter.addAttribute("hyperlinkPage", text.getHyperlinkPage());
762         xmlWriter.addEncodedAttribute(JRPrintTextFactory.ATTRIBUTE_hyperlinkTooltip, text.getHyperlinkTooltip());
763         xmlWriter.addAttribute("bookmarkLevel", text.getBookmarkLevel(), JRAnchor.NO_BOOKMARK);
764         xmlWriter.addAttribute(JRPrintTextFactory.ATTRIBUTE_valueClass, text.getValueClassName());
765         xmlWriter.addEncodedAttribute(JRPrintTextFactory.ATTRIBUTE_pattern, text.getPattern());
766         xmlWriter.addAttribute(JRPrintTextFactory.ATTRIBUTE_formatFactoryClass, text.getFormatFactoryClass());
767         xmlWriter.addEncodedAttribute(JRPrintTextFactory.ATTRIBUTE_locale, text.getLocaleCode());
768         xmlWriter.addEncodedAttribute(JRPrintTextFactory.ATTRIBUTE_timezone, text.getTimeZoneId());
769         
770         exportReportElement(text);
771         exportBox(text);
772
773         exportFont(text);
774
775         if (text.getText() != null)
776         {
777             xmlWriter.writeCDATAElement("textContent", text.getText());
778         }
779         
780         exportHyperlinkParameters(text);
781
782         xmlWriter.closeElement();
783     }
784
785
786     /**
787      * @throws IOException
788      *
789      */

790     private void exportBox(JRBox box) throws IOException JavaDoc
791     {
792         if (box != null)
793         {
794             xmlWriter.startElement("box");
795
796             xmlWriter.addAttribute("border", box.getOwnBorder(), JRXmlConstants.getPenMap());
797             xmlWriter.addAttribute("borderColor", box.getOwnBorderColor());
798             xmlWriter.addAttribute("padding", box.getOwnPadding());
799         
800             xmlWriter.addAttribute("topBorder", box.getOwnTopBorder(), JRXmlConstants.getPenMap());
801             xmlWriter.addAttribute("topBorderColor", box.getOwnTopBorderColor());
802             xmlWriter.addAttribute("topPadding", box.getOwnTopPadding());
803         
804             xmlWriter.addAttribute("leftBorder", box.getOwnLeftBorder(), JRXmlConstants.getPenMap());
805             xmlWriter.addAttribute("leftBorderColor", box.getOwnLeftBorderColor());
806             xmlWriter.addAttribute("leftPadding", box.getOwnLeftPadding());
807         
808             xmlWriter.addAttribute("bottomBorder", box.getOwnBottomBorder(), JRXmlConstants.getPenMap());
809             xmlWriter.addAttribute("bottomBorderColor", box.getOwnBottomBorderColor());
810             xmlWriter.addAttribute("bottomPadding", box.getOwnBottomPadding());
811
812         
813             xmlWriter.addAttribute("rightBorder", box.getOwnRightBorder(), JRXmlConstants.getPenMap());
814             xmlWriter.addAttribute("rightBorderColor", box.getOwnRightBorderColor());
815             xmlWriter.addAttribute("rightPadding", box.getOwnRightPadding());
816         
817             xmlWriter.closeElement(true);
818         }
819     }
820
821
822     /**
823      *
824      */

825     protected void exportFont(JRFont font) throws IOException JavaDoc
826     {
827         if (font != null)
828         {
829             xmlWriter.startElement("font");
830
831             if(font.getReportFont() != null)
832             {
833                 JRFont baseFont =
834                     (JRFont)fontsMap.get(
835                         font.getReportFont().getName()
836                         );
837                 if(baseFont != null)
838                 {
839                     xmlWriter.addEncodedAttribute("reportFont", font.getReportFont().getName());
840                 }
841                 else
842                 {
843                     throw
844                         new JRRuntimeException(
845                             "Referenced report font not found : "
846                             + font.getReportFont().getName()
847                             );
848                 }
849             }
850         
851             xmlWriter.addEncodedAttribute("fontName", font.getOwnFontName());
852             xmlWriter.addAttribute("size", font.getOwnFontSize());
853             xmlWriter.addAttribute("isBold", font.isOwnBold());
854             xmlWriter.addAttribute("isItalic", font.isOwnItalic());
855             xmlWriter.addAttribute("isUnderline", font.isOwnUnderline());
856             xmlWriter.addAttribute("isStrikeThrough", font.isOwnStrikeThrough());
857             xmlWriter.addEncodedAttribute("pdfFontName", font.getOwnPdfFontName());
858             xmlWriter.addEncodedAttribute("pdfEncoding", font.getOwnPdfEncoding());
859             xmlWriter.addAttribute("isPdfEmbedded", font.isOwnPdfEmbedded());
860             xmlWriter.closeElement(true);
861         }
862     }
863     
864     
865     protected void exportFrame(JRPrintFrame frame) throws IOException JavaDoc, JRException
866     {
867         xmlWriter.startElement("frame");
868         
869         setFrameElementsOffset(frame, true);
870         try
871         {
872             exportReportElement(frame);
873             exportBox(frame);
874             exportElements(frame.getElements());
875
876             xmlWriter.closeElement();
877         }
878         finally
879         {
880             restoreElementOffsets();
881         }
882     }
883     
884     
885     /**
886      *
887      */

888     private static synchronized int getNextImageId(){
889         return imageId++;
890     }
891
892
893     protected void exportHyperlinkParameters(JRPrintHyperlink hyperlink) throws IOException JavaDoc
894     {
895         JRPrintHyperlinkParameters hyperlinkParameters = hyperlink.getHyperlinkParameters();
896         if (hyperlinkParameters != null)
897         {
898             for (Iterator JavaDoc it = hyperlinkParameters.getParameters().iterator(); it.hasNext();)
899             {
900                 JRPrintHyperlinkParameter parameter = (JRPrintHyperlinkParameter) it.next();
901                 exportHyperlinkParameter(parameter);
902             }
903         }
904     }
905
906
907     protected void exportHyperlinkParameter(JRPrintHyperlinkParameter parameter) throws IOException JavaDoc
908     {
909         xmlWriter.startElement(JRPrintHyperlinkParameterFactory.ELEMENT_hyperlinkParameter);
910         xmlWriter.addEncodedAttribute(JRPrintHyperlinkParameterFactory.ATTRIBUTE_name, parameter.getName());
911         xmlWriter.addAttribute(JRPrintHyperlinkParameterFactory.ATTRIBUTE_class, parameter.getValueClass(), "java.lang.String");
912         
913         if (parameter.getValue() != null)
914         {
915             String JavaDoc data = JRValueStringUtils.serialize(parameter.getValueClass(), parameter.getValue());
916             xmlWriter.writeCDATAElement(JRPrintHyperlinkParameterValueFactory.ELEMENT_hyperlinkParameterValue, data);
917         }
918         
919         xmlWriter.closeElement();
920     }
921 }
922
Popular Tags