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: package net.sf.jasperreports.engine.xml;
029:
030: import java.io.FileInputStream;
031: import java.io.IOException;
032: import java.io.InputStream;
033: import java.util.ArrayList;
034: import java.util.List;
035:
036: import javax.xml.parsers.ParserConfigurationException;
037: import javax.xml.parsers.SAXParser;
038: import javax.xml.parsers.SAXParserFactory;
039:
040: import net.sf.jasperreports.engine.JRBox;
041: import net.sf.jasperreports.engine.JRException;
042: import net.sf.jasperreports.engine.JRFont;
043: import net.sf.jasperreports.engine.JRPrintElement;
044: import net.sf.jasperreports.engine.JRPrintHyperlinkParameter;
045: import net.sf.jasperreports.engine.JRPrintPage;
046: import net.sf.jasperreports.engine.JRReportFont;
047: import net.sf.jasperreports.engine.JRStyle;
048: import net.sf.jasperreports.engine.JasperPrint;
049: import net.sf.jasperreports.engine.util.JRProperties;
050:
051: import org.apache.commons.digester.SetNestedPropertiesRule;
052: import org.xml.sax.ErrorHandler;
053: import org.xml.sax.SAXException;
054: import org.xml.sax.SAXParseException;
055: import org.xml.sax.XMLReader;
056:
057: /**
058: * @author Teodor Danciu (teodord@users.sourceforge.net)
059: * @version $Id: JRPrintXmlLoader.java 1818 2007-08-22 13:46:00Z teodord $
060: */
061: public class JRPrintXmlLoader implements ErrorHandler {
062:
063: /**
064: *
065: */
066: private JasperPrint jasperPrint = null;
067: private List errors = new ArrayList();
068:
069: /**
070: *
071: */
072: protected JRPrintXmlLoader() {
073: }
074:
075: /**
076: *
077: */
078: public void setJasperPrint(JasperPrint jasperPrint) {
079: this .jasperPrint = jasperPrint;
080: }
081:
082: /**
083: *
084: */
085: public static JasperPrint load(String sourceFileName)
086: throws JRException {
087: JasperPrint jasperPrint = null;
088:
089: FileInputStream fis = null;
090:
091: try {
092: fis = new FileInputStream(sourceFileName);
093: JRPrintXmlLoader printXmlLoader = new JRPrintXmlLoader();
094: jasperPrint = printXmlLoader.loadXML(fis);
095: } catch (IOException e) {
096: throw new JRException(e);
097: } finally {
098: if (fis != null) {
099: try {
100: fis.close();
101: } catch (IOException e) {
102: }
103: }
104: }
105:
106: return jasperPrint;
107: }
108:
109: /**
110: *
111: */
112: public static JasperPrint load(InputStream is) throws JRException {
113: JasperPrint jasperPrint = null;
114:
115: JRPrintXmlLoader printXmlLoader = new JRPrintXmlLoader();
116: jasperPrint = printXmlLoader.loadXML(is);
117:
118: return jasperPrint;
119: }
120:
121: /**
122: *
123: */
124: private JasperPrint loadXML(InputStream is) throws JRException {
125: try {
126: JRXmlDigester digester = prepareDigester();
127:
128: /* */
129: digester.parse(is);
130: } catch (ParserConfigurationException e) {
131: throw new JRException(e);
132: } catch (SAXException e) {
133: throw new JRException(e);
134: } catch (IOException e) {
135: throw new JRException(e);
136: }
137:
138: if (errors.size() > 0) {
139: Exception e = (Exception) errors.get(0);
140: if (e instanceof JRException) {
141: throw (JRException) e;
142: }
143: throw new JRException(e);
144: }
145:
146: return this .jasperPrint;
147: }
148:
149: /**
150: *
151: */
152: private JRXmlDigester prepareDigester()
153: throws ParserConfigurationException, SAXException {
154: SAXParserFactory saxParserFactory = SAXParserFactory
155: .newInstance();
156:
157: boolean validating = JRProperties
158: .getBooleanProperty(JRProperties.EXPORT_XML_VALIDATION);
159: saxParserFactory.setValidating(validating);
160:
161: SAXParser saxParser = saxParserFactory.newSAXParser();
162: //XMLReader xmlReader = XMLReaderFactory.createXMLReader();
163: XMLReader xmlReader = saxParser.getXMLReader();
164:
165: xmlReader.setFeature("http://xml.org/sax/features/validation",
166: validating);
167:
168: JRXmlDigester digester = new JRXmlDigester(xmlReader);
169: digester.push(this );
170: //digester.setDebug(3);
171: digester.setErrorHandler(this );
172: digester.setValidating(true);
173:
174: /* */
175: digester.addFactoryCreate("jasperPrint",
176: JasperPrintFactory.class.getName());
177: digester.addSetNext("jasperPrint", "setJasperPrint",
178: JasperPrint.class.getName());
179:
180: /* */
181: digester.addRule("*/property", new JRPropertyDigesterRule());
182:
183: /* */
184: digester.addFactoryCreate("jasperPrint/reportFont",
185: JRReportFontFactory.class.getName());
186: digester.addSetNext("jasperPrint/reportFont", "addFont",
187: JRReportFont.class.getName());
188:
189: /* */
190: digester.addFactoryCreate("jasperPrint/style",
191: JRPrintStyleFactory.class.getName());
192: digester.addSetNext("jasperPrint/style", "addStyle",
193: JRStyle.class.getName());
194:
195: /* */
196: digester.addFactoryCreate("jasperPrint/page",
197: JRPrintPageFactory.class.getName());
198: digester.addSetNext("jasperPrint/page", "addPage",
199: JRPrintPage.class.getName());
200:
201: /* */
202: digester.addFactoryCreate("*/line", JRPrintLineFactory.class
203: .getName());
204: digester.addSetNext("*/line", "addElement",
205: JRPrintElement.class.getName());
206:
207: /* */
208: digester.addFactoryCreate("*/reportElement",
209: JRPrintElementFactory.class.getName());
210:
211: /* */
212: digester.addFactoryCreate("*/graphicElement",
213: JRPrintGraphicElementFactory.class.getName());
214:
215: /* */
216: digester.addFactoryCreate("*/rectangle",
217: JRPrintRectangleFactory.class.getName());
218: digester.addSetNext("*/rectangle", "addElement",
219: JRPrintElement.class.getName());
220:
221: /* */
222: digester.addFactoryCreate("*/ellipse",
223: JRPrintEllipseFactory.class.getName());
224: digester.addSetNext("*/ellipse", "addElement",
225: JRPrintElement.class.getName());
226:
227: /* */
228: digester.addFactoryCreate("*/image", JRPrintImageFactory.class
229: .getName());
230: digester.addSetNext("*/image", "addElement",
231: JRPrintElement.class.getName());
232:
233: /* */
234: digester
235: .addFactoryCreate("*/box", JRBoxFactory.class.getName());
236: digester.addSetNext("*/box", "setBox", JRBox.class.getName());
237:
238: /* */
239: digester.addFactoryCreate("*/image/imageSource",
240: JRPrintImageSourceFactory.class.getName());
241: digester.addCallMethod("*/image/imageSource", "setImageSource",
242: 0);
243:
244: /* */
245: digester.addFactoryCreate("*/text", JRPrintTextFactory.class
246: .getName());
247: digester.addSetNext("*/text", "addElement",
248: JRPrintElement.class.getName());
249: SetNestedPropertiesRule textRule = new SetNestedPropertiesRule(
250: new String[] { "textContent", "reportElement", "box",
251: "font" }, new String[] { "text" });
252: textRule.setTrimData(false);
253: textRule.setAllowUnknownChildElements(true);
254: digester.addRule("*/text", textRule);
255:
256: /* */
257: digester.addFactoryCreate("*/text/font",
258: JRPrintFontFactory.class.getName());
259: digester.addSetNext("*/text/font", "setFont", JRFont.class
260: .getName());
261:
262: addFrameRules(digester);
263:
264: addHyperlinkParameterRules(digester);
265:
266: return digester;
267: }
268:
269: private void addFrameRules(JRXmlDigester digester) {
270: digester.addFactoryCreate("*/frame", JRPrintFrameFactory.class
271: .getName());
272: digester.addSetNext("*/frame", "addElement",
273: JRPrintElement.class.getName());
274: }
275:
276: protected void addHyperlinkParameterRules(JRXmlDigester digester) {
277: String parameterPattern = "*/"
278: + JRXmlConstants.ELEMENT_hyperlinkParameter;
279: digester.addFactoryCreate(parameterPattern,
280: JRPrintHyperlinkParameterFactory.class);
281: digester.addSetNext(parameterPattern, "addHyperlinkParameter",
282: JRPrintHyperlinkParameter.class.getName());
283:
284: String parameterValuePattern = parameterPattern + "/"
285: + JRXmlConstants.ELEMENT_hyperlinkParameterValue;
286: digester.addFactoryCreate(parameterValuePattern,
287: JRPrintHyperlinkParameterValueFactory.class);
288: digester.addCallMethod(parameterValuePattern, "setData", 0);
289: }
290:
291: /**
292: *
293: */
294: public void addError(Exception e) {
295: this .errors.add(e);
296: }
297:
298: /**
299: *
300: */
301: public void error(SAXParseException e) {
302: this .errors.add(e);
303: }
304:
305: /**
306: *
307: */
308: public void fatalError(SAXParseException e) {
309: this .errors.add(e);
310: }
311:
312: /**
313: *
314: */
315: public void warning(SAXParseException e) {
316: this.errors.add(e);
317: }
318:
319: }
|