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 javax.xml.parsers.ParserConfigurationException;
031:
032: import net.sf.jasperreports.engine.JRRuntimeException;
033: import net.sf.jasperreports.engine.JRSimpleTemplate;
034: import net.sf.jasperreports.engine.JRStyle;
035: import net.sf.jasperreports.engine.JRTemplate;
036: import net.sf.jasperreports.engine.util.JRProperties;
037:
038: import org.apache.commons.digester.Digester;
039: import org.apache.commons.digester.RuleSet;
040: import org.apache.commons.digester.RuleSetBase;
041: import org.xml.sax.ErrorHandler;
042: import org.xml.sax.SAXException;
043: import org.xml.sax.SAXParseException;
044:
045: /**
046: * Factory for template XML digesters.
047: *
048: * @author Lucian Chirita (lucianc@users.sourceforge.net)
049: * @version $Id: JRXmlTemplateDigesterFactory.java 1763 2007-06-21 08:47:21Z lucianc $
050: * @see JRTemplate
051: */
052: public class JRXmlTemplateDigesterFactory implements ErrorHandler {
053:
054: private static final JRXmlTemplateDigesterFactory instance = new JRXmlTemplateDigesterFactory();
055:
056: private final RuleSet rules;
057:
058: protected JRXmlTemplateDigesterFactory() {
059: rules = readRuleSet();
060: }
061:
062: /**
063: * Returns the singleton instance.
064: *
065: * @return the singleton instance
066: */
067: public static JRXmlTemplateDigesterFactory instance() {
068: return instance;
069: }
070:
071: protected RuleSet readRuleSet() {
072: return new RuleSetBase() {
073: public void addRuleInstances(Digester digester) {
074: String rootPattern = JRXmlConstants.TEMPLATE_ELEMENT_ROOT;
075: digester.addObjectCreate(rootPattern,
076: JRSimpleTemplate.class);
077:
078: String includedTemplatePattern = rootPattern
079: + "/"
080: + JRXmlConstants.TEMPLATE_ELEMENT_INCLUDED_TEMPLATE;
081: digester.addCallMethod(includedTemplatePattern,
082: "addIncludedTemplate", 0);
083:
084: String stylePattern = rootPattern + "/"
085: + JRXmlConstants.ELEMENT_style;
086: digester.addFactoryCreate(stylePattern,
087: JRTemplateStyleFactory.class);
088: digester.addSetNext(stylePattern, "addStyle",
089: JRStyle.class.getName());
090: }
091: };
092: }
093:
094: /**
095: * Creates and configures a digester for template XML.
096: *
097: * @return a template XML digester
098: */
099: public JRXmlDigester createDigester() {
100: JRXmlDigester digester = new JRXmlDigester();
101: try {
102: configureDigester(digester);
103: } catch (SAXException e) {
104: throw new JRRuntimeException(e);
105: } catch (ParserConfigurationException e) {
106: throw new JRRuntimeException(e);
107: }
108: return digester;
109: }
110:
111: protected void configureDigester(Digester digester)
112: throws SAXException, ParserConfigurationException {
113: boolean validating = JRProperties
114: .getBooleanProperty(JRProperties.COMPILER_XML_VALIDATION);
115:
116: digester.setErrorHandler(this );
117: digester.setValidating(validating);
118: digester.setFeature("http://xml.org/sax/features/validation",
119: validating);
120:
121: digester.addRuleSet(rules);
122: }
123:
124: public void error(SAXParseException exception) throws SAXException {
125: throw exception;
126: }
127:
128: public void fatalError(SAXParseException exception)
129: throws SAXException {
130: throw exception;
131: }
132:
133: public void warning(SAXParseException exception)
134: throws SAXException {
135: throw exception;
136: }
137:
138: }
|