KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jasperreports > engine > xml > JRPrintXmlLoader


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.xml;
29
30 import java.io.FileInputStream JavaDoc;
31 import java.io.IOException JavaDoc;
32 import java.io.InputStream JavaDoc;
33 import java.util.ArrayList JavaDoc;
34 import java.util.List JavaDoc;
35
36 import javax.xml.parsers.ParserConfigurationException JavaDoc;
37 import javax.xml.parsers.SAXParser JavaDoc;
38 import javax.xml.parsers.SAXParserFactory JavaDoc;
39
40 import net.sf.jasperreports.engine.JRBox;
41 import net.sf.jasperreports.engine.JRException;
42 import net.sf.jasperreports.engine.JRFont;
43 import net.sf.jasperreports.engine.JRPrintElement;
44 import net.sf.jasperreports.engine.JRPrintHyperlinkParameter;
45 import net.sf.jasperreports.engine.JRPrintPage;
46 import net.sf.jasperreports.engine.JRReportFont;
47 import net.sf.jasperreports.engine.JRStyle;
48 import net.sf.jasperreports.engine.JasperPrint;
49 import net.sf.jasperreports.engine.util.JRProperties;
50
51 import org.apache.commons.digester.SetNestedPropertiesRule;
52 import org.xml.sax.ErrorHandler JavaDoc;
53 import org.xml.sax.SAXException JavaDoc;
54 import org.xml.sax.SAXParseException JavaDoc;
55 import org.xml.sax.XMLReader JavaDoc;
56
57
58 /**
59  * @author Teodor Danciu (teodord@users.sourceforge.net)
60  * @version $Id: JRPrintXmlLoader.java 1536 2006-12-22 12:34:40 +0200 (Fri, 22 Dec 2006) teodord $
61  */

62 public class JRPrintXmlLoader implements ErrorHandler JavaDoc
63 {
64
65     /**
66      *
67      */

68     private JasperPrint jasperPrint = null;
69     private List JavaDoc errors = new ArrayList JavaDoc();
70
71
72     /**
73      *
74      */

75     protected JRPrintXmlLoader()
76     {
77     }
78     
79
80     /**
81      *
82      */

83     public void setJasperPrint(JasperPrint jasperPrint)
84     {
85         this.jasperPrint = jasperPrint;
86     }
87
88
89     /**
90      *
91      */

92     public static JasperPrint load(String JavaDoc sourceFileName) throws JRException
93     {
94         JasperPrint jasperPrint = null;
95
96         FileInputStream JavaDoc fis = null;
97
98         try
99         {
100             fis = new FileInputStream JavaDoc(sourceFileName);
101             JRPrintXmlLoader printXmlLoader = new JRPrintXmlLoader();
102             jasperPrint = printXmlLoader.loadXML(fis);
103         }
104         catch(IOException JavaDoc e)
105         {
106             throw new JRException(e);
107         }
108         finally
109         {
110             if (fis != null)
111             {
112                 try
113                 {
114                     fis.close();
115                 }
116                 catch(IOException JavaDoc e)
117                 {
118                 }
119             }
120         }
121
122         return jasperPrint;
123     }
124
125
126     /**
127      *
128      */

129     public static JasperPrint load(InputStream JavaDoc is) throws JRException
130     {
131         JasperPrint jasperPrint = null;
132
133         JRPrintXmlLoader printXmlLoader = new JRPrintXmlLoader();
134         jasperPrint = printXmlLoader.loadXML(is);
135
136         return jasperPrint;
137     }
138
139
140     /**
141      *
142      */

143     private JasperPrint loadXML(InputStream JavaDoc is) throws JRException
144     {
145         try
146         {
147             JRXmlDigester digester = prepareDigester();
148
149             /* */
150             digester.parse(is);
151         }
152         catch(ParserConfigurationException JavaDoc e)
153         {
154             throw new JRException(e);
155         }
156         catch(SAXException JavaDoc e)
157         {
158             throw new JRException(e);
159         }
160         catch(IOException JavaDoc e)
161         {
162             throw new JRException(e);
163         }
164         
165         if (errors.size() > 0)
166         {
167             Exception JavaDoc e = (Exception JavaDoc)errors.get(0);
168             if (e instanceof JRException)
169             {
170                 throw (JRException)e;
171             }
172             throw new JRException(e);
173         }
174
175         return this.jasperPrint;
176     }
177
178
179     /**
180      *
181      */

182     private JRXmlDigester prepareDigester() throws ParserConfigurationException JavaDoc, SAXException JavaDoc
183     {
184         SAXParserFactory JavaDoc saxParserFactory = SAXParserFactory.newInstance();
185         
186         boolean validating = JRProperties.getBooleanProperty(JRProperties.EXPORT_XML_VALIDATION);
187         saxParserFactory.setValidating(validating);
188
189         SAXParser JavaDoc saxParser = saxParserFactory.newSAXParser();
190         //XMLReader xmlReader = XMLReaderFactory.createXMLReader();
191
XMLReader JavaDoc xmlReader = saxParser.getXMLReader();
192
193         xmlReader.setFeature("http://xml.org/sax/features/validation", validating);
194
195         JRXmlDigester digester = new JRXmlDigester(xmlReader);
196         digester.push(this);
197         //digester.setDebug(3);
198
digester.setErrorHandler(this);
199         digester.setValidating(true);
200         
201         /* */
202         digester.addFactoryCreate("jasperPrint", JasperPrintFactory.class.getName());
203         digester.addSetNext("jasperPrint", "setJasperPrint", JasperPrint.class.getName());
204
205         /* */
206         digester.addFactoryCreate("jasperPrint/reportFont", JRReportFontFactory.class.getName());
207         digester.addSetNext("jasperPrint/reportFont", "addFont", JRReportFont.class.getName());
208
209         /* */
210         digester.addFactoryCreate("jasperPrint/style", JRStyleFactory.class.getName());
211         digester.addSetNext("jasperPrint/style", "addStyle", JRStyle.class.getName());
212
213         /* */
214         digester.addFactoryCreate("jasperPrint/page", JRPrintPageFactory.class.getName());
215         digester.addSetNext("jasperPrint/page", "addPage", JRPrintPage.class.getName());
216
217         /* */
218         digester.addFactoryCreate("*/line", JRPrintLineFactory.class.getName());
219         digester.addSetNext("*/line", "addElement", JRPrintElement.class.getName());
220
221         /* */
222         digester.addFactoryCreate("*/reportElement", JRPrintElementFactory.class.getName());
223
224         /* */
225         digester.addFactoryCreate("*/graphicElement", JRPrintGraphicElementFactory.class.getName());
226
227         /* */
228         digester.addFactoryCreate("*/rectangle", JRPrintRectangleFactory.class.getName());
229         digester.addSetNext("*/rectangle", "addElement", JRPrintElement.class.getName());
230
231         /* */
232         digester.addFactoryCreate("*/ellipse", JRPrintEllipseFactory.class.getName());
233         digester.addSetNext("*/ellipse", "addElement", JRPrintElement.class.getName());
234
235         /* */
236         digester.addFactoryCreate("*/image", JRPrintImageFactory.class.getName());
237         digester.addSetNext("*/image", "addElement", JRPrintElement.class.getName());
238
239         /* */
240         digester.addFactoryCreate("*/box", JRBoxFactory.class.getName());
241         digester.addSetNext("*/box", "setBox", JRBox.class.getName());
242
243         /* */
244         digester.addFactoryCreate("*/image/imageSource", JRPrintImageSourceFactory.class.getName());
245         digester.addCallMethod("*/image/imageSource", "setImageSource", 0);
246
247         /* */
248         digester.addFactoryCreate("*/text", JRPrintTextFactory.class.getName());
249         digester.addSetNext("*/text", "addElement", JRPrintElement.class.getName());
250         SetNestedPropertiesRule textRule = new SetNestedPropertiesRule(new String JavaDoc[]{"textContent", "reportElement", "box", "font"}, new String JavaDoc[]{"text"});
251         textRule.setTrimData(false);
252         textRule.setAllowUnknownChildElements(true);
253         digester.addRule("*/text", textRule);
254
255         /* */
256         digester.addFactoryCreate("*/text/font", JRPrintFontFactory.class.getName());
257         digester.addSetNext("*/text/font", "setFont", JRFont.class.getName());
258         
259         addFrameRules(digester);
260
261         addHyperlinkParameterRules(digester);
262         
263         return digester;
264     }
265
266
267     private void addFrameRules(JRXmlDigester digester)
268     {
269         digester.addFactoryCreate("*/frame", JRPrintFrameFactory.class.getName());
270         digester.addSetNext("*/frame", "addElement", JRPrintElement.class.getName());
271     }
272
273
274     protected void addHyperlinkParameterRules(JRXmlDigester digester)
275     {
276         String JavaDoc parameterPattern = "*/" + JRPrintHyperlinkParameterFactory.ELEMENT_hyperlinkParameter;
277         digester.addFactoryCreate(parameterPattern, JRPrintHyperlinkParameterFactory.class);
278         digester.addSetNext(parameterPattern, "addHyperlinkParameter", JRPrintHyperlinkParameter.class.getName());
279         
280         String JavaDoc parameterValuePattern = parameterPattern + "/" + JRPrintHyperlinkParameterValueFactory.ELEMENT_hyperlinkParameterValue;
281         digester.addFactoryCreate(parameterValuePattern, JRPrintHyperlinkParameterValueFactory.class);
282         digester.addCallMethod(parameterValuePattern, "setData", 0);
283     }
284
285
286     /**
287      *
288      */

289     public void addError(Exception JavaDoc e)
290     {
291         this.errors.add(e);
292     }
293     
294     /**
295      *
296      */

297     public void error(SAXParseException JavaDoc e)
298     {
299         this.errors.add(e);
300     }
301     
302     /**
303      *
304      */

305     public void fatalError(SAXParseException JavaDoc e)
306     {
307         this.errors.add(e);
308     }
309     
310     /**
311      *
312      */

313     public void warning(SAXParseException JavaDoc e)
314     {
315         this.errors.add(e);
316     }
317
318
319 }
320
Popular Tags