001: /*
002: * ============================================================================
003: * GNU Lesser General Public License
004: * ============================================================================
005: *
006: * JasperReports - Free Java report-generating library.
007: * Copyright (C) 2001-2006 JasperSoft Corporation http://www.jaspersoft.com
008: *
009: * This library is free software; you can redistribute it and/or
010: * modify it under the terms of the GNU Lesser General Public
011: * License as published by the Free Software Foundation; either
012: * version 2.1 of the License, or (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017: * Lesser General Public License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library; if not, write to the Free Software
021: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
022: *
023: * JasperSoft Corporation
024: * 303 Second Street, Suite 450 North
025: * San Francisco, CA 94107
026: * http://www.jaspersoft.com
027: */
028:
029: /*
030: * Contributors:
031: * Artur Biesiadowski - abies@users.sourceforge.net
032: */
033: package net.sf.jasperreports.engine.xml;
034:
035: import java.io.File;
036: import java.io.FileInputStream;
037: import java.io.IOException;
038: import java.io.InputStream;
039: import java.util.ArrayList;
040: import java.util.Collection;
041: import java.util.HashSet;
042: import java.util.Iterator;
043: import java.util.List;
044: import java.util.Map;
045: import java.util.Set;
046:
047: import javax.xml.parsers.ParserConfigurationException;
048:
049: import net.sf.jasperreports.engine.JRDatasetRun;
050: import net.sf.jasperreports.engine.JRException;
051: import net.sf.jasperreports.engine.JRGroup;
052: import net.sf.jasperreports.engine.JRVariable;
053: import net.sf.jasperreports.engine.design.JRDesignChart;
054: import net.sf.jasperreports.engine.design.JRDesignDataset;
055: import net.sf.jasperreports.engine.design.JRDesignElement;
056: import net.sf.jasperreports.engine.design.JRDesignElementDataset;
057: import net.sf.jasperreports.engine.design.JRDesignImage;
058: import net.sf.jasperreports.engine.design.JRDesignTextField;
059: import net.sf.jasperreports.engine.design.JRDesignVariable;
060: import net.sf.jasperreports.engine.design.JRValidationException;
061: import net.sf.jasperreports.engine.design.JasperDesign;
062:
063: import org.apache.commons.digester.Digester;
064: import org.xml.sax.InputSource;
065: import org.xml.sax.SAXException;
066:
067: /**
068: * @author Teodor Danciu (teodord@users.sourceforge.net)
069: * @version $Id: JRXmlLoader.java 1625 2007-03-09 17:21:31Z lucianc $
070: */
071: public class JRXmlLoader {
072:
073: /**
074: *
075: */
076: private JasperDesign jasperDesign = null;
077: private Collection groupReprintedElements = new ArrayList();
078: private Collection groupEvaluatedImages = new ArrayList();
079: private Collection groupEvaluatedTextFields = new ArrayList();
080: private Collection groupEvaluatedCharts = new ArrayList();
081: private Set groupBoundDatasets = new HashSet();
082: private List errors = new ArrayList();
083:
084: private Digester digester = null;
085:
086: private boolean ignoreConsistencyProblems;
087:
088: /**
089: *
090: */
091: public JRXmlLoader(Digester digester) {
092: this .digester = digester;
093: }
094:
095: /**
096: *
097: */
098: public void setJasperDesign(JasperDesign jasperDesign) {
099: this .jasperDesign = jasperDesign;
100: }
101:
102: /**
103: *
104: */
105: public Collection getGroupReprintedElements() {
106: return this .groupReprintedElements;
107: }
108:
109: /**
110: *
111: */
112: public Collection getGroupEvaluatedImages() {
113: return this .groupEvaluatedImages;
114: }
115:
116: /**
117: *
118: */
119: public Collection getGroupEvaluatedTextFields() {
120: return this .groupEvaluatedTextFields;
121: }
122:
123: /**
124: *
125: */
126: public Collection getGroupEvaluatedCharts() {
127: return groupEvaluatedCharts;
128: }
129:
130: /**
131: *
132: */
133: public Set getGroupBoundDatasets() {
134: return groupBoundDatasets;
135: }
136:
137: /**
138: *
139: */
140: public static JasperDesign load(String sourceFileName)
141: throws JRException {
142: return load(new File(sourceFileName));
143: }
144:
145: /**
146: *
147: */
148: public static JasperDesign load(File file) throws JRException {
149: JasperDesign jasperDesign = null;
150:
151: FileInputStream fis = null;
152:
153: try {
154: fis = new FileInputStream(file);
155: jasperDesign = JRXmlLoader.load(fis);
156: } catch (IOException e) {
157: throw new JRException(e);
158: } finally {
159: if (fis != null) {
160: try {
161: fis.close();
162: } catch (IOException e) {
163: }
164: }
165: }
166:
167: return jasperDesign;
168: }
169:
170: /**
171: *
172: */
173: public static JasperDesign load(InputStream is) throws JRException {
174: JasperDesign jasperDesign = null;
175:
176: JRXmlLoader xmlLoader = null;
177:
178: try {
179: xmlLoader = new JRXmlLoader(JRXmlDigesterFactory
180: .createDigester());
181: } catch (ParserConfigurationException e) {
182: throw new JRException(e);
183: } catch (SAXException e) {
184: throw new JRException(e);
185: }
186:
187: jasperDesign = xmlLoader.loadXML(is);
188:
189: return jasperDesign;
190: }
191:
192: /**
193: *
194: */
195: public JasperDesign loadXML(InputStream is) throws JRException {
196: return loadXML(new InputSource(is));
197: }
198:
199: /**
200: *
201: */
202: public JasperDesign loadXML(InputSource is) throws JRException {
203: try {
204: digester.push(this );
205:
206: /* */
207: digester.parse(is);
208: } catch (SAXException e) {
209: throw new JRException(e);
210: } catch (IOException e) {
211: throw new JRException(e);
212: } finally {
213: digester.clear();
214: }
215:
216: if (errors.size() > 0) {
217: Exception e = (Exception) errors.get(0);
218: if (e instanceof JRException) {
219: throw (JRException) e;
220: }
221: throw new JRException(e);
222: }
223:
224: /* */
225: assignGroupsToVariables(jasperDesign.getMainDesignDataset());
226: for (Iterator it = jasperDesign.getDatasetsList().iterator(); it
227: .hasNext();) {
228: JRDesignDataset dataset = (JRDesignDataset) it.next();
229: assignGroupsToVariables(dataset);
230: }
231:
232: this .assignGroupsToElements();
233: this .assignGroupsToImages();
234: this .assignGroupsToTextFields();
235: this .assignGroupsToCharts();
236: this .assignGroupsToDatasets();
237:
238: return this .jasperDesign;
239: }
240:
241: /**
242: *
243: */
244: private void assignGroupsToVariables(JRDesignDataset dataset)
245: throws JRException {
246: JRVariable[] variables = dataset.getVariables();
247: if (variables != null && variables.length > 0) {
248: Map groupsMap = dataset.getGroupsMap();
249: for (int i = 0; i < variables.length; i++) {
250: JRDesignVariable variable = (JRDesignVariable) variables[i];
251: if (variable.getResetType() == JRVariable.RESET_TYPE_GROUP) {
252: String groupName = null;
253: JRGroup group = variable.getResetGroup();
254: if (group != null) {
255: groupName = group.getName();
256: group = (JRGroup) groupsMap.get(groupName);
257: }
258:
259: if (!ignoreConsistencyProblems && group == null) {
260: throw new JRValidationException(
261: "Unknown reset group '" + groupName
262: + "' for variable : "
263: + variable.getName(), variable);
264: }
265:
266: variable.setResetGroup(group);
267: } else {
268: variable.setResetGroup(null);
269: }
270:
271: if (variable.getIncrementType() == JRVariable.RESET_TYPE_GROUP) {
272: String groupName = null;
273: JRGroup group = variable.getIncrementGroup();
274: if (group != null) {
275: groupName = group.getName();
276: group = (JRGroup) groupsMap.get(groupName);
277: }
278:
279: if (!ignoreConsistencyProblems && group == null) {
280: throw new JRValidationException(
281: "Unknown increment group '" + groupName
282: + "' for variable : "
283: + variable.getName(), variable);
284: }
285:
286: variable.setIncrementGroup(group);
287: } else {
288: variable.setIncrementGroup(null);
289: }
290: }
291: }
292: }
293:
294: /**
295: *
296: */
297: private void assignGroupsToElements() throws JRException {
298: Map groupsMap = jasperDesign.getGroupsMap();
299: for (Iterator it = groupReprintedElements.iterator(); it
300: .hasNext();) {
301: JRDesignElement element = (JRDesignElement) it.next();
302:
303: String groupName = null;
304: JRGroup group = element.getPrintWhenGroupChanges();
305: if (group != null) {
306: groupName = group.getName();
307: group = (JRGroup) groupsMap.get(group.getName());
308: }
309:
310: if (!ignoreConsistencyProblems && group == null) {
311: throw new JRValidationException(
312: "Unknown reprint group '" + groupName
313: + "' for element.", element);
314: }
315:
316: element.setPrintWhenGroupChanges(group);
317: }
318: }
319:
320: /**
321: *
322: */
323: private void assignGroupsToImages() throws JRException {
324: Map groupsMap = jasperDesign.getGroupsMap();
325: for (Iterator it = groupEvaluatedImages.iterator(); it
326: .hasNext();) {
327: JRDesignImage image = (JRDesignImage) it.next();
328:
329: String groupName = null;
330: JRGroup group = image.getEvaluationGroup();
331: if (group != null) {
332: groupName = group.getName();
333: group = (JRGroup) groupsMap.get(group.getName());
334: }
335:
336: if (!ignoreConsistencyProblems && group == null) {
337: throw new JRValidationException(
338: "Unknown evaluation group '" + groupName
339: + "' for image.", image);
340: }
341:
342: image.setEvaluationGroup(group);
343: }
344: }
345:
346: /**
347: *
348: */
349: private void assignGroupsToTextFields() throws JRException {
350: Map groupsMap = jasperDesign.getGroupsMap();
351: for (Iterator it = groupEvaluatedTextFields.iterator(); it
352: .hasNext();) {
353: JRDesignTextField textField = (JRDesignTextField) it.next();
354:
355: String groupName = null;
356: JRGroup group = textField.getEvaluationGroup();
357: if (group != null) {
358: groupName = group.getName();
359: group = (JRGroup) groupsMap.get(group.getName());
360: }
361:
362: if (!ignoreConsistencyProblems && group == null) {
363: throw new JRValidationException(
364: "Unknown evaluation group '" + groupName
365: + "' for text field.", textField);
366: }
367:
368: textField.setEvaluationGroup(group);
369: }
370: }
371:
372: /**
373: *
374: */
375: private void assignGroupsToCharts() throws JRException {
376: Map groupsMap = jasperDesign.getGroupsMap();
377: for (Iterator it = groupEvaluatedCharts.iterator(); it
378: .hasNext();) {
379: JRDesignChart chart = (JRDesignChart) it.next();
380:
381: String groupName = null;
382: JRGroup group = chart.getEvaluationGroup();
383: if (group != null) {
384: groupName = group.getName();
385: group = (JRGroup) groupsMap.get(group.getName());
386: }
387:
388: if (!ignoreConsistencyProblems && group == null) {
389: throw new JRValidationException(
390: "Unknown evaluation group '" + groupName
391: + "' for chart.", chart);
392: }
393:
394: chart.setEvaluationGroup(group);
395: }
396: }
397:
398: /**
399: *
400: */
401: private void assignGroupsToDatasets() throws JRException {
402: for (Iterator it = groupBoundDatasets.iterator(); it.hasNext();) {
403: JRDesignElementDataset dataset = (JRDesignElementDataset) it
404: .next();
405:
406: JRDatasetRun datasetRun = dataset.getDatasetRun();
407: Map groupsMap;
408: if (datasetRun == null) {
409: groupsMap = jasperDesign.getGroupsMap();
410: } else {
411: Map datasetMap = jasperDesign.getDatasetMap();
412: String datasetName = datasetRun.getDatasetName();
413: JRDesignDataset subDataset = (JRDesignDataset) datasetMap
414: .get(datasetName);
415: if (subDataset == null) {
416: throw new JRException("Unknown sub dataset '"
417: + datasetName + "' for chart dataset.");
418: }
419: groupsMap = subDataset.getGroupsMap();
420: }
421:
422: if (dataset.getIncrementType() == JRVariable.RESET_TYPE_GROUP) {
423: String groupName = null;
424: JRGroup group = dataset.getIncrementGroup();
425: if (group != null) {
426: groupName = group.getName();
427: group = (JRGroup) groupsMap.get(group.getName());
428: }
429:
430: if (!ignoreConsistencyProblems && group == null) {
431: throw new JRValidationException(
432: "Unknown increment group '" + groupName
433: + "' for chart dataset.", dataset);
434: }
435:
436: dataset.setIncrementGroup(group);
437: } else {
438: dataset.setIncrementGroup(null);
439: }
440:
441: if (dataset.getResetType() == JRVariable.RESET_TYPE_GROUP) {
442: String groupName = null;
443: JRGroup group = dataset.getResetGroup();
444: if (group != null) {
445: groupName = group.getName();
446: group = (JRGroup) groupsMap.get(group.getName());
447: }
448:
449: if (!ignoreConsistencyProblems && group == null) {
450: throw new JRValidationException(
451: "Unknown reset group '" + groupName
452: + "' for chart dataset.", dataset);
453: }
454:
455: dataset.setResetGroup(group);
456: } else {
457: dataset.setResetGroup(null);
458: }
459: }
460: }
461:
462: /**
463: *
464: */
465: public void addError(Exception e) {
466: if (!ignoreConsistencyProblems)
467: this .errors.add(e);
468: }
469:
470: /**
471: * Returns true if the loader is set to ignore consistency problems
472: * @return the ignoreConsistencyProblems flag.
473: */
474: public boolean isIgnoreConsistencyProblems() {
475: return ignoreConsistencyProblems;
476: }
477:
478: /**
479: * Allows to enable or disable the reporting of consistency problems. Consistency
480: * problems are problems in the logical structure of the report such as references
481: * to missing groups and fonts.
482: *
483: * @param ignoreConsistencyProblems The ignoreConsistencyProblems value to set.
484: */
485: public void setIgnoreConsistencyProblems(
486: boolean ignoreConsistencyProblems) {
487: this.ignoreConsistencyProblems = ignoreConsistencyProblems;
488: }
489:
490: }
|