Source for org.jfree.report.modules.factories.report.flow.AbstractExpressionReadHandler

   1: /**
   2:  * ========================================
   3:  * JFreeReport : a free Java report library
   4:  * ========================================
   5:  *
   6:  * Project Info:  http://reporting.pentaho.org/
   7:  *
   8:  * (C) Copyright 2000-2007, by Object Refinery Limited, Pentaho Corporation and Contributors.
   9:  *
  10:  * This library is free software; you can redistribute it and/or modify it under the terms
  11:  * of the GNU Lesser General Public License as published by the Free Software Foundation;
  12:  * either 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, but WITHOUT ANY WARRANTY;
  15:  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  16:  * See the GNU Lesser General Public License for more details.
  17:  *
  18:  * You should have received a copy of the GNU Lesser General Public License along with this
  19:  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  20:  * Boston, MA 02111-1307, USA.
  21:  *
  22:  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
  23:  * in the United States and other countries.]
  24:  *
  25:  * ------------
  26:  * $Id: AbstractExpressionReadHandler.java 3525 2007-10-16 11:43:48Z tmorgner $
  27:  * ------------
  28:  * (C) Copyright 2000-2005, by Object Refinery Limited.
  29:  * (C) Copyright 2005-2007, by Pentaho Corporation.
  30:  */
  31: package org.jfree.report.modules.factories.report.flow;
  32: 
  33: import java.beans.IntrospectionException;
  34: 
  35: import org.jfree.report.expressions.Expression;
  36: import org.jfree.report.expressions.FormulaExpression;
  37: import org.jfree.report.expressions.FormulaFunction;
  38: import org.jfree.report.util.CharacterEntityParser;
  39: import org.jfree.report.util.beans.BeanUtility;
  40: import org.jfree.util.ObjectUtilities;
  41: import org.jfree.xmlns.parser.AbstractXmlReadHandler;
  42: import org.jfree.xmlns.parser.ParseException;
  43: import org.jfree.xmlns.parser.XmlReadHandler;
  44: import org.xml.sax.Attributes;
  45: import org.xml.sax.SAXException;
  46: 
  47: /**
  48:  * Creation-Date: 09.04.2006, 13:23:32
  49:  *
  50:  * @author Thomas Morgner
  51:  */
  52: public abstract class AbstractExpressionReadHandler
  53:     extends AbstractXmlReadHandler
  54: {
  55:   private Expression expression;
  56:   private BeanUtility expressionBeanUtility;
  57:   private CharacterEntityParser characterEntityParser;
  58: 
  59:   public AbstractExpressionReadHandler()
  60:   {
  61:     this.characterEntityParser = CharacterEntityParser.createXMLEntityParser();
  62:   }
  63: 
  64:   protected String getDefaultClassName()
  65:   {
  66:     return null;
  67:   }
  68: 
  69:   /**
  70:    * Starts parsing.
  71:    *
  72:    * @param attrs the attributes.
  73:    * @throws SAXException if there is a parsing error.
  74:    */
  75:   protected void startParsing(final Attributes attrs) throws SAXException
  76:   {
  77:     final String name = attrs.getValue(getUri(), "name");
  78:     String className = attrs.getValue(getUri(), "class");
  79:     final String formula = attrs.getValue(getUri(), "formula");
  80:     if (className == null)
  81:     {
  82:       if (formula != null)
  83:       {
  84:         final String initial = attrs.getValue(getUri(), "initial");
  85:         if (initial != null)
  86:         {
  87:           final FormulaFunction function = new FormulaFunction();
  88:           function.setInitial(initial);
  89:           function.setFormula(formula);
  90:           this.expression = function;
  91:         }
  92:         else
  93:         {
  94:           final FormulaExpression expression = new FormulaExpression();
  95:           expression.setFormula(formula);
  96:           this.expression = expression;
  97:         }
  98:       }
  99:       else
 100:       {
 101:         className = getDefaultClassName();
 102:         if (className == null)
 103:         {
 104:           throw new ParseException("Required attribute 'class' is missing.",
 105:               getRootHandler().getDocumentLocator());
 106:         }
 107:       }
 108:     }
 109: 
 110:     if (expression == null)
 111:     {
 112:       expression = (Expression) ObjectUtilities.loadAndInstantiate
 113:         (className, AbstractExpressionReadHandler.class, Expression.class);
 114:       if (expression == null)
 115:       {
 116:         throw new ParseException("Expression '" + className +
 117:             "' is not valid. The specified class is not an expression or function.",
 118:              getRootHandler().getDocumentLocator());
 119:       }
 120:     }
 121: 
 122:     expression.setName(name);
 123:     expression.setDeepTraversing("true".equals(attrs.getValue(getUri(), "deep-traversing")));
 124:     expression.setPrecompute("true".equals(attrs.getValue(getUri(), "precompute")));
 125:     expression.setPreserve("true".equals(attrs.getValue(getUri(), "preserve")));
 126: 
 127:     try
 128:     {
 129:       expressionBeanUtility = new BeanUtility(expression);
 130:     }
 131:     catch (ClassCastException e)
 132:     {
 133:       throw new ParseException("Expression '" + className +
 134:           "' is not valid. The specified class is not an expression or function.",
 135:           e, getRootHandler().getDocumentLocator());
 136:     }
 137:     catch (IntrospectionException e)
 138:     {
 139:       throw new ParseException("Expression '" + className +
 140:           "' is not valid. Introspection failed for this expression.",
 141:           e, getRootHandler().getDocumentLocator());
 142:     }
 143: 
 144:   }
 145: 
 146:   /**
 147:    * Returns the handler for a child element.
 148:    *
 149:    * @param tagName the tag name.
 150:    * @param atts    the attributes.
 151:    * @return the handler or null, if the tagname is invalid.
 152:    * @throws SAXException if there is a parsing error.
 153:    */
 154:   protected XmlReadHandler getHandlerForChild(final String uri,
 155:                                               final String tagName,
 156:                                               final Attributes atts)
 157:       throws SAXException
 158:   {
 159:     if (isSameNamespace(uri) == false)
 160:     {
 161:       return null;
 162:     }
 163:     if ("property".equals(tagName))
 164:     {
 165:       return new TypedPropertyReadHandler
 166:           (expressionBeanUtility, expression.getName(),
 167:               characterEntityParser);
 168:     }
 169:     return null;
 170:   }
 171: 
 172:   public Expression getExpression()
 173:   {
 174:     return expression;
 175:   }
 176: 
 177:   /**
 178:    * Returns the object for this element or null, if this element does not
 179:    * create an object.
 180:    *
 181:    * @return the object.
 182:    * @throws SAXException if there is a parsing error.
 183:    */
 184:   public Object getObject() throws SAXException
 185:   {
 186:     return expression;
 187:   }
 188: }