001: /**
002: * ========================================
003: * JFreeReport : a free Java report library
004: * ========================================
005: *
006: * Project Info: http://reporting.pentaho.org/
007: *
008: * (C) Copyright 2000-2007, by Object Refinery Limited, Pentaho Corporation and Contributors.
009: *
010: * This library is free software; you can redistribute it and/or modify it under the terms
011: * of the GNU Lesser General Public License as published by the Free Software Foundation;
012: * either 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, but WITHOUT ANY WARRANTY;
015: * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
016: * See the GNU Lesser General Public License for more details.
017: *
018: * You should have received a copy of the GNU Lesser General Public License along with this
019: * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
020: * Boston, MA 02111-1307, USA.
021: *
022: * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
023: * in the United States and other countries.]
024: *
025: * ------------
026: * $Id: AbstractExpressionReadHandler.java 3525 2007-10-16 11:43:48Z tmorgner $
027: * ------------
028: * (C) Copyright 2000-2005, by Object Refinery Limited.
029: * (C) Copyright 2005-2007, by Pentaho Corporation.
030: */package org.jfree.report.modules.factories.report.flow;
031:
032: import java.beans.IntrospectionException;
033:
034: import org.jfree.report.expressions.Expression;
035: import org.jfree.report.expressions.FormulaExpression;
036: import org.jfree.report.expressions.FormulaFunction;
037: import org.jfree.report.util.CharacterEntityParser;
038: import org.jfree.report.util.beans.BeanUtility;
039: import org.jfree.util.ObjectUtilities;
040: import org.jfree.xmlns.parser.AbstractXmlReadHandler;
041: import org.jfree.xmlns.parser.ParseException;
042: import org.jfree.xmlns.parser.XmlReadHandler;
043: import org.xml.sax.Attributes;
044: import org.xml.sax.SAXException;
045:
046: /**
047: * Creation-Date: 09.04.2006, 13:23:32
048: *
049: * @author Thomas Morgner
050: */
051: public abstract class AbstractExpressionReadHandler extends
052: AbstractXmlReadHandler {
053: private Expression expression;
054: private BeanUtility expressionBeanUtility;
055: private CharacterEntityParser characterEntityParser;
056:
057: public AbstractExpressionReadHandler() {
058: this .characterEntityParser = CharacterEntityParser
059: .createXMLEntityParser();
060: }
061:
062: protected String getDefaultClassName() {
063: return null;
064: }
065:
066: /**
067: * Starts parsing.
068: *
069: * @param attrs the attributes.
070: * @throws SAXException if there is a parsing error.
071: */
072: protected void startParsing(final Attributes attrs)
073: throws SAXException {
074: final String name = attrs.getValue(getUri(), "name");
075: String className = attrs.getValue(getUri(), "class");
076: final String formula = attrs.getValue(getUri(), "formula");
077: if (className == null) {
078: if (formula != null) {
079: final String initial = attrs.getValue(getUri(),
080: "initial");
081: if (initial != null) {
082: final FormulaFunction function = new FormulaFunction();
083: function.setInitial(initial);
084: function.setFormula(formula);
085: this .expression = function;
086: } else {
087: final FormulaExpression expression = new FormulaExpression();
088: expression.setFormula(formula);
089: this .expression = expression;
090: }
091: } else {
092: className = getDefaultClassName();
093: if (className == null) {
094: throw new ParseException(
095: "Required attribute 'class' is missing.",
096: getRootHandler().getDocumentLocator());
097: }
098: }
099: }
100:
101: if (expression == null) {
102: expression = (Expression) ObjectUtilities
103: .loadAndInstantiate(className,
104: AbstractExpressionReadHandler.class,
105: Expression.class);
106: if (expression == null) {
107: throw new ParseException(
108: "Expression '"
109: + className
110: + "' is not valid. The specified class is not an expression or function.",
111: getRootHandler().getDocumentLocator());
112: }
113: }
114:
115: expression.setName(name);
116: expression.setDeepTraversing("true".equals(attrs.getValue(
117: getUri(), "deep-traversing")));
118: expression.setPrecompute("true".equals(attrs.getValue(getUri(),
119: "precompute")));
120: expression.setPreserve("true".equals(attrs.getValue(getUri(),
121: "preserve")));
122:
123: try {
124: expressionBeanUtility = new BeanUtility(expression);
125: } catch (ClassCastException e) {
126: throw new ParseException(
127: "Expression '"
128: + className
129: + "' is not valid. The specified class is not an expression or function.",
130: e, getRootHandler().getDocumentLocator());
131: } catch (IntrospectionException e) {
132: throw new ParseException(
133: "Expression '"
134: + className
135: + "' is not valid. Introspection failed for this expression.",
136: e, getRootHandler().getDocumentLocator());
137: }
138:
139: }
140:
141: /**
142: * Returns the handler for a child element.
143: *
144: * @param tagName the tag name.
145: * @param atts the attributes.
146: * @return the handler or null, if the tagname is invalid.
147: * @throws SAXException if there is a parsing error.
148: */
149: protected XmlReadHandler getHandlerForChild(final String uri,
150: final String tagName, final Attributes atts)
151: throws SAXException {
152: if (isSameNamespace(uri) == false) {
153: return null;
154: }
155: if ("property".equals(tagName)) {
156: return new TypedPropertyReadHandler(expressionBeanUtility,
157: expression.getName(), characterEntityParser);
158: }
159: return null;
160: }
161:
162: public Expression getExpression() {
163: return expression;
164: }
165:
166: /**
167: * Returns the object for this element or null, if this element does not
168: * create an object.
169: *
170: * @return the object.
171: * @throws SAXException if there is a parsing error.
172: */
173: public Object getObject() throws SAXException {
174: return expression;
175: }
176: }
|