KickJava   Java API By Example, From Geeks To Geeks.

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


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  * Artur Biesiadowski - abies@users.sourceforge.net
32  */

33 package net.sf.jasperreports.engine.xml;
34
35 import java.io.File JavaDoc;
36 import java.io.FileInputStream JavaDoc;
37 import java.io.IOException JavaDoc;
38 import java.io.InputStream JavaDoc;
39 import java.util.ArrayList JavaDoc;
40 import java.util.Collection JavaDoc;
41 import java.util.HashSet JavaDoc;
42 import java.util.Iterator JavaDoc;
43 import java.util.List JavaDoc;
44 import java.util.Map JavaDoc;
45 import java.util.Set JavaDoc;
46
47 import javax.xml.parsers.ParserConfigurationException JavaDoc;
48
49 import net.sf.jasperreports.engine.JRDatasetRun;
50 import net.sf.jasperreports.engine.JRException;
51 import net.sf.jasperreports.engine.JRGroup;
52 import net.sf.jasperreports.engine.JRVariable;
53 import net.sf.jasperreports.engine.design.JRDesignChart;
54 import net.sf.jasperreports.engine.design.JRDesignDataset;
55 import net.sf.jasperreports.engine.design.JRDesignElement;
56 import net.sf.jasperreports.engine.design.JRDesignElementDataset;
57 import net.sf.jasperreports.engine.design.JRDesignImage;
58 import net.sf.jasperreports.engine.design.JRDesignTextField;
59 import net.sf.jasperreports.engine.design.JRDesignVariable;
60 import net.sf.jasperreports.engine.design.JasperDesign;
61
62 import org.apache.commons.digester.Digester;
63 import org.xml.sax.InputSource JavaDoc;
64 import org.xml.sax.SAXException JavaDoc;
65
66
67 /**
68  * @author Teodor Danciu (teodord@users.sourceforge.net)
69  * @version $Id: JRXmlLoader.java 1229 2006-04-19 13:27:35 +0300 (Wed, 19 Apr 2006) teodord $
70  */

71 public class JRXmlLoader
72 {
73
74     /**
75      *
76      */

77     private JasperDesign jasperDesign = null;
78     private Collection JavaDoc groupReprintedElements = new ArrayList JavaDoc();
79     private Collection JavaDoc groupEvaluatedImages = new ArrayList JavaDoc();
80     private Collection JavaDoc groupEvaluatedTextFields = new ArrayList JavaDoc();
81     private Collection JavaDoc groupEvaluatedCharts = new ArrayList JavaDoc();
82     private Set JavaDoc groupBoundDatasets = new HashSet JavaDoc();
83     private List JavaDoc errors = new ArrayList JavaDoc();
84
85     private Digester digester = null;
86
87     private boolean ignoreConsistencyProblems;
88         
89     /**
90      *
91      */

92     public JRXmlLoader(Digester digester)
93     {
94         this.digester = digester;
95     }
96
97     /**
98      *
99      */

100     public void setJasperDesign(JasperDesign jasperDesign)
101     {
102         this.jasperDesign = jasperDesign;
103     }
104
105     /**
106      *
107      */

108     public Collection JavaDoc getGroupReprintedElements()
109     {
110         return this.groupReprintedElements;
111     }
112
113     /**
114      *
115      */

116     public Collection JavaDoc getGroupEvaluatedImages()
117     {
118         return this.groupEvaluatedImages;
119     }
120
121     /**
122      *
123      */

124     public Collection JavaDoc getGroupEvaluatedTextFields()
125     {
126         return this.groupEvaluatedTextFields;
127     }
128
129     /**
130      *
131      */

132     public Collection JavaDoc getGroupEvaluatedCharts()
133     {
134         return groupEvaluatedCharts;
135     }
136
137     /**
138     *
139     */

140     public Set JavaDoc getGroupBoundDatasets()
141     {
142         return groupBoundDatasets;
143     }
144
145
146     /**
147      *
148      */

149     public static JasperDesign load(String JavaDoc sourceFileName) throws JRException
150     {
151         return load(new File JavaDoc(sourceFileName));
152     }
153
154
155     /**
156      *
157      */

158     public static JasperDesign load(File JavaDoc file) throws JRException
159     {
160         JasperDesign jasperDesign = null;
161
162         FileInputStream JavaDoc fis = null;
163
164         try
165         {
166             fis = new FileInputStream JavaDoc(file);
167             jasperDesign = JRXmlLoader.load(fis);
168         }
169         catch(IOException JavaDoc e)
170         {
171             throw new JRException(e);
172         }
173         finally
174         {
175             if (fis != null)
176             {
177                 try
178                 {
179                     fis.close();
180                 }
181                 catch(IOException JavaDoc e)
182                 {
183                 }
184             }
185         }
186
187         return jasperDesign;
188     }
189
190
191     /**
192      *
193      */

194     public static JasperDesign load(InputStream JavaDoc is) throws JRException
195     {
196         JasperDesign jasperDesign = null;
197
198         JRXmlLoader xmlLoader = null;
199
200         try
201         {
202             xmlLoader = new JRXmlLoader(JRXmlDigesterFactory.createDigester());
203         }
204         catch (ParserConfigurationException JavaDoc e)
205         {
206             throw new JRException(e);
207         }
208         catch (SAXException JavaDoc e)
209         {
210             throw new JRException(e);
211         }
212         
213         jasperDesign = xmlLoader.loadXML(is);
214
215         return jasperDesign;
216     }
217
218
219
220     /**
221      *
222      */

223     public JasperDesign loadXML(InputStream JavaDoc is) throws JRException
224     {
225         return loadXML(new InputSource JavaDoc(is));
226     }
227
228     /**
229      *
230      */

231     public JasperDesign loadXML(InputSource JavaDoc is) throws JRException
232     {
233         try
234         {
235             digester.push(this);
236
237             /* */
238             digester.parse(is);
239         }
240         catch(SAXException JavaDoc e)
241         {
242             throw new JRException(e);
243         }
244         catch(IOException JavaDoc e)
245         {
246             throw new JRException(e);
247         }
248         finally
249         {
250             digester.clear();
251         }
252         
253         if (errors.size() > 0)
254         {
255             Exception JavaDoc e = (Exception JavaDoc)errors.get(0);
256             if (e instanceof JRException)
257             {
258                 throw (JRException)e;
259             }
260             throw new JRException(e);
261         }
262
263         /* */
264         assignGroupsToVariables(jasperDesign.getMainDesignDataset());
265         for (Iterator JavaDoc it = jasperDesign.getDatasetsList().iterator(); it.hasNext();)
266         {
267             JRDesignDataset dataset = (JRDesignDataset) it.next();
268             assignGroupsToVariables(dataset);
269         }
270         
271         this.assignGroupsToElements();
272         this.assignGroupsToImages();
273         this.assignGroupsToTextFields();
274         this.assignGroupsToCharts();
275         this.assignGroupsToDatasets();
276         
277         return this.jasperDesign;
278     }
279
280     /**
281      *
282      */

283     private void assignGroupsToVariables(JRDesignDataset dataset) throws JRException
284     {
285         JRVariable[] variables = dataset.getVariables();
286         if (variables != null && variables.length > 0)
287         {
288             Map JavaDoc groupsMap = dataset.getGroupsMap();
289             for(int i = 0; i < variables.length; i++)
290             {
291                 JRDesignVariable variable = (JRDesignVariable)variables[i];
292                 if (variable.getResetType() == JRVariable.RESET_TYPE_GROUP)
293                 {
294                     String JavaDoc groupName = null;
295                     JRGroup group = variable.getResetGroup();
296                     if (group != null)
297                     {
298                         groupName = group.getName();
299                         group = (JRGroup)groupsMap.get(groupName);
300                     }
301
302                     if (!ignoreConsistencyProblems && group == null)
303                     {
304                         throw
305                             new JRException(
306                                 "Unknown reset group '" + groupName
307                                 + "' for variable : " + variable.getName()
308                                 );
309                     }
310
311                     variable.setResetGroup(group);
312                 }
313                 else
314                 {
315                     variable.setResetGroup(null);
316                 }
317
318                 if (variable.getIncrementType() == JRVariable.RESET_TYPE_GROUP)
319                 {
320                     String JavaDoc groupName = null;
321                     JRGroup group = variable.getIncrementGroup();
322                     if (group != null)
323                     {
324                         groupName = group.getName();
325                         group = (JRGroup)groupsMap.get(groupName);
326                     }
327
328                     if (!ignoreConsistencyProblems && group == null)
329                     {
330                         throw
331                             new JRException(
332                                 "Unknown increment group '" + groupName
333                                 + "' for variable : " + variable.getName()
334                                 );
335                     }
336
337                     variable.setIncrementGroup(group);
338                 }
339                 else
340                 {
341                     variable.setIncrementGroup(null);
342                 }
343             }
344         }
345     }
346
347
348     /**
349      *
350      */

351     private void assignGroupsToElements() throws JRException
352     {
353         Map JavaDoc groupsMap = jasperDesign.getGroupsMap();
354         JRDesignElement element = null;
355         String JavaDoc groupName = null;
356         JRGroup group = null;
357         for(Iterator JavaDoc it = groupReprintedElements.iterator(); it.hasNext();)
358         {
359             element = (JRDesignElement)it.next();
360
361             groupName = null;
362             group = element.getPrintWhenGroupChanges();
363             if (group != null)
364             {
365                 groupName = group.getName();
366                 group = (JRGroup)groupsMap.get(group.getName());
367             }
368
369             if (!ignoreConsistencyProblems && group == null)
370             {
371                 throw new JRException("Unknown reprint group '" + groupName + "' for element.");
372             }
373
374             element.setPrintWhenGroupChanges(group);
375         }
376     }
377
378
379     /**
380      *
381      */

382     private void assignGroupsToImages() throws JRException
383     {
384         Map JavaDoc groupsMap = jasperDesign.getGroupsMap();
385         JRDesignImage image = null;
386         String JavaDoc groupName = null;
387         JRGroup group = null;
388         for(Iterator JavaDoc it = groupEvaluatedImages.iterator(); it.hasNext();)
389         {
390             image = (JRDesignImage)it.next();
391
392             groupName = null;
393             group = image.getEvaluationGroup();
394             if (group != null)
395             {
396                 groupName = group.getName();
397                 group = (JRGroup)groupsMap.get(group.getName());
398             }
399
400             if (!ignoreConsistencyProblems && group == null)
401             {
402                 throw new JRException("Unknown evaluation group '" + groupName + "' for image.");
403             }
404
405             image.setEvaluationGroup(group);
406         }
407     }
408
409
410     /**
411      *
412      */

413     private void assignGroupsToTextFields() throws JRException
414     {
415         Map JavaDoc groupsMap = jasperDesign.getGroupsMap();
416         JRDesignTextField textField = null;
417         String JavaDoc groupName = null;
418         JRGroup group = null;
419         for(Iterator JavaDoc it = groupEvaluatedTextFields.iterator(); it.hasNext();)
420         {
421             textField = (JRDesignTextField)it.next();
422
423             groupName = null;
424             group = textField.getEvaluationGroup();
425             if (group != null)
426             {
427                 groupName = group.getName();
428                 group = (JRGroup)groupsMap.get(group.getName());
429             }
430
431             if (!ignoreConsistencyProblems && group == null)
432             {
433                 throw new JRException("Unknown evaluation group '" + groupName + "' for text field.");
434             }
435
436             textField.setEvaluationGroup(group);
437         }
438     }
439
440
441     /**
442      *
443      */

444     private void assignGroupsToCharts() throws JRException
445     {
446         Map JavaDoc groupsMap = jasperDesign.getGroupsMap();
447         for(Iterator JavaDoc it = groupEvaluatedCharts.iterator(); it.hasNext();)
448         {
449             JRDesignChart chart = (JRDesignChart)it.next();
450
451             String JavaDoc groupName = null;
452             JRGroup group = chart.getEvaluationGroup();
453             if (group != null)
454             {
455                 groupName = group.getName();
456                 group = (JRGroup)groupsMap.get(group.getName());
457             }
458
459             if (!ignoreConsistencyProblems && group == null)
460             {
461                 throw new JRException("Unknown evaluation group '" + groupName + "' for chart.");
462             }
463
464             chart.setEvaluationGroup(group);
465         }
466     }
467
468
469     /**
470      *
471      */

472     private void assignGroupsToDatasets() throws JRException
473     {
474         for(Iterator JavaDoc it = groupBoundDatasets.iterator(); it.hasNext();)
475         {
476             JRDesignElementDataset dataset = (JRDesignElementDataset) it.next();
477             
478             JRDatasetRun datasetRun = dataset.getDatasetRun();
479             Map JavaDoc groupsMap;
480             if (datasetRun == null)
481             {
482                 groupsMap = jasperDesign.getGroupsMap();
483             }
484             else
485             {
486                 Map JavaDoc datasetMap = jasperDesign.getDatasetMap();
487                 String JavaDoc datasetName = datasetRun.getDatasetName();
488                 JRDesignDataset subDataset = (JRDesignDataset) datasetMap.get(datasetName);
489                 if (subDataset == null)
490                 {
491                     throw new JRException("Unknown sub dataset '" + datasetName + "' for chart dataset.");
492                 }
493                 groupsMap = subDataset.getGroupsMap();
494             }
495
496             if (dataset.getIncrementType() == JRVariable.RESET_TYPE_GROUP)
497             {
498                 String JavaDoc groupName = null;
499                 JRGroup group = dataset.getIncrementGroup();
500                 if (group != null)
501                 {
502                     groupName = group.getName();
503                     group = (JRGroup)groupsMap.get(group.getName());
504                 }
505
506                 if (!ignoreConsistencyProblems && group == null)
507                 {
508                     throw new JRException("Unknown increment group '" + groupName + "' for chart dataset.");
509                 }
510
511                 dataset.setIncrementGroup(group);
512             }
513             else
514             {
515                 dataset.setIncrementGroup(null);
516             }
517
518             if (dataset.getResetType() == JRVariable.RESET_TYPE_GROUP)
519             {
520                 String JavaDoc groupName = null;
521                 JRGroup group = dataset.getResetGroup();
522                 if (group != null)
523                 {
524                     groupName = group.getName();
525                     group = (JRGroup)groupsMap.get(group.getName());
526                 }
527
528                 if (!ignoreConsistencyProblems && group == null)
529                 {
530                     throw new JRException("Unknown reset group '" + groupName + "' for chart dataset.");
531                 }
532
533                 dataset.setResetGroup(group);
534             }
535             else
536             {
537                 dataset.setResetGroup(null);
538             }
539         }
540     }
541
542     
543     /**
544      *
545      */

546     public void addError(Exception JavaDoc e)
547     {
548         if(!ignoreConsistencyProblems)
549             this.errors.add(e);
550     }
551     
552     /**
553      * Returns true if the loader is set to ignore consistency problems
554      * @return the ignoreConsistencyProblems flag.
555      */

556     public boolean isIgnoreConsistencyProblems() {
557         return ignoreConsistencyProblems;
558     }
559     
560     /**
561      * Allows to enable or disable the reporting of consistency problems. Consistency
562      * problems are problems in the logical structure of the report such as references
563      * to missing groups and fonts.
564      *
565      * @param ignoreConsistencyProblems The ignoreConsistencyProblems value to set.
566      */

567     public void setIgnoreConsistencyProblems(boolean ignoreConsistencyProblems) {
568         this.ignoreConsistencyProblems = ignoreConsistencyProblems;
569     }
570
571 }
572
Popular Tags