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

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