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

   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: ElementReadHandler.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.util.ArrayList;
  34: 
  35: import org.jfree.report.structure.Element;
  36: import org.jfree.xmlns.parser.AbstractXmlReadHandler;
  37: import org.jfree.xmlns.parser.PropertyReadHandler;
  38: import org.jfree.xmlns.parser.XmlReadHandler;
  39: import org.xml.sax.Attributes;
  40: import org.xml.sax.SAXException;
  41: 
  42: /**
  43:  * Creation-Date: 09.04.2006, 13:55:36
  44:  *
  45:  * @author Thomas Morgner
  46:  */
  47: public abstract class ElementReadHandler extends AbstractXmlReadHandler
  48: {
  49:   private boolean virtual;
  50:   private boolean enabled;
  51:   private String style;
  52:   private ArrayList expressionHandlers;
  53:   private ArrayList styleExpressionHandlers;
  54:   private ArrayList attributeExpressionHandlers;
  55:   private ArrayList attributeHandlers;
  56:   private ArrayList stylePropertyHandlers;
  57:   private DisplayConditionReadHandler displayConditionReadHandler;
  58: 
  59:   protected ElementReadHandler()
  60:   {
  61:     expressionHandlers = new ArrayList();
  62:     styleExpressionHandlers = new ArrayList();
  63:     attributeExpressionHandlers = new ArrayList();
  64:     stylePropertyHandlers = new ArrayList();
  65:     attributeHandlers = new ArrayList();
  66:   }
  67: 
  68:   public boolean isEnabled()
  69:   {
  70:     return enabled;
  71:   }
  72: 
  73:   public String getStyle()
  74:   {
  75:     return style;
  76:   }
  77: 
  78:   /**
  79:    * Starts parsing.
  80:    *
  81:    * @param attrs the attributes.
  82:    * @throws SAXException if there is a parsing error.
  83:    */
  84:   protected void startParsing(final Attributes attrs) throws SAXException
  85:   {
  86:     super.startParsing(attrs);
  87:     style = attrs.getValue(FlowReportFactoryModule.NAMESPACE, "style");
  88:     final String enabledValue = attrs.getValue(FlowReportFactoryModule.NAMESPACE, "enabled");
  89:     if (enabledValue != null)
  90:     {
  91:       enabled = "true".equals(enabledValue);
  92:     }
  93:     else
  94:     {
  95:       enabled = true;
  96:     }
  97: 
  98:     final String virtualValue = attrs.getValue(FlowReportFactoryModule.NAMESPACE, "virtual");
  99:     if (virtualValue != null)
 100:     {
 101:       virtual = "true".equals(virtualValue);
 102:     }
 103:     else
 104:     {
 105:       virtual = false;
 106:     }
 107:   }
 108: 
 109:   /**
 110:    * Returns the handler for a child element.
 111:    *
 112:    * @param tagName the tag name.
 113:    * @param atts    the attributes.
 114:    * @return the handler or null, if the tagname is invalid.
 115:    * @throws SAXException       if there is a parsing error.
 116:    * @throws XmlReaderException if there is a reader error.
 117:    */
 118:   protected XmlReadHandler getHandlerForChild(final String uri,
 119:                                               final String tagName,
 120:                                               final Attributes atts)
 121:           throws SAXException
 122:   {
 123:     if (FlowReportFactoryModule.NAMESPACE.equals(uri))
 124:     {
 125:       if ("expression".equals(tagName))
 126:       {
 127:         final ExpressionReadHandler erh = new ExpressionReadHandler();
 128:         expressionHandlers.add(erh);
 129:         return erh;
 130:       }
 131:       if ("style-expression".equals(tagName))
 132:       {
 133:         final StyleExpressionReadHandler erh = new StyleExpressionReadHandler();
 134:         styleExpressionHandlers.add(erh);
 135:         return erh;
 136:       }
 137:       if ("style-property".equals(tagName))
 138:       {
 139:         final PropertyReadHandler erh = new PropertyReadHandler();
 140:         stylePropertyHandlers.add(erh);
 141:         return erh;
 142:       }
 143:       if ("attribute-expression".equals(tagName))
 144:       {
 145:         final AttributeExpressionReadHandler erh = new AttributeExpressionReadHandler();
 146:         attributeExpressionHandlers.add(erh);
 147:         return erh;
 148:       }
 149:       if ("attribute".equals(tagName))
 150:       {
 151:         final AttributeReadHandler erh = new AttributeReadHandler();
 152:         attributeHandlers.add(erh);
 153:         return erh;
 154:       }
 155:       if ("display-condition".equals(tagName))
 156:       {
 157:         displayConditionReadHandler = new DisplayConditionReadHandler();
 158:         return displayConditionReadHandler;
 159:       }
 160:     }
 161:     return null;
 162:   }
 163: 
 164:   protected void configureElement(final Element e)
 165:   {
 166:     if (displayConditionReadHandler != null)
 167:     {
 168:       e.setDisplayCondition(displayConditionReadHandler.getExpression());
 169:     }
 170:     for (int i = 0; i < expressionHandlers.size(); i++)
 171:     {
 172:       final ExpressionReadHandler handler =
 173:               (ExpressionReadHandler) expressionHandlers.get(i);
 174:       e.addExpression(handler.getExpression());
 175:     }
 176:     for (int i = 0; i < styleExpressionHandlers.size(); i++)
 177:     {
 178:       final StyleExpressionReadHandler handler =
 179:               (StyleExpressionReadHandler) styleExpressionHandlers .get(i);
 180:       e.setStyleExpression(handler.getStyleKey(), handler.getExpression());
 181:     }
 182:     for (int i = 0; i < stylePropertyHandlers.size(); i++)
 183:     {
 184: 
 185:       final PropertyReadHandler handler =
 186:               (PropertyReadHandler) stylePropertyHandlers .get(i);
 187:       e.getStyle().setPropertyValueAsString(handler.getName(), handler.getResult());
 188:     }
 189:     for (int i = 0; i < attributeExpressionHandlers.size(); i++)
 190:     {
 191:       final AttributeExpressionReadHandler handler =
 192:               (AttributeExpressionReadHandler) attributeExpressionHandlers .get(
 193:                       i);
 194:       e.setAttributeExpression(handler.getAttributeName(),
 195:               handler.getExpression());
 196:     }
 197:     for (int i = 0; i < attributeHandlers.size(); i++)
 198:     {
 199:       final AttributeReadHandler handler =
 200:               (AttributeReadHandler) attributeHandlers .get(i);
 201:       e.setAttribute(handler.getNamespace(), handler.getName(), handler.getObject());
 202:     }
 203:     e.setEnabled(enabled);
 204:     e.setVirtual(virtual);
 205:     if (style != null)
 206:     {
 207:       e.setAttribute(FlowReportFactoryModule.NAMESPACE,"style", style);
 208:     }
 209:   }
 210: 
 211:   protected abstract Element getElement();
 212: 
 213:   /**
 214:    * Returns the object for this element or null, if this element does not
 215:    * create an object.
 216:    *
 217:    * @return the object.
 218:    * @throws XmlReaderException if there is a parsing error.
 219:    */
 220:   public Object getObject() throws SAXException
 221:   {
 222:     return getElement();
 223:   }
 224: }