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: ElementReadHandler.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     */
031    package org.jfree.report.modules.factories.report.flow;
032    
033    import java.util.ArrayList;
034    
035    import org.jfree.report.structure.Element;
036    import org.jfree.xmlns.parser.AbstractXmlReadHandler;
037    import org.jfree.xmlns.parser.PropertyReadHandler;
038    import org.jfree.xmlns.parser.XmlReadHandler;
039    import org.xml.sax.Attributes;
040    import org.xml.sax.SAXException;
041    
042    /**
043     * Creation-Date: 09.04.2006, 13:55:36
044     *
045     * @author Thomas Morgner
046     */
047    public abstract class ElementReadHandler extends AbstractXmlReadHandler
048    {
049      private boolean virtual;
050      private boolean enabled;
051      private String style;
052      private ArrayList expressionHandlers;
053      private ArrayList styleExpressionHandlers;
054      private ArrayList attributeExpressionHandlers;
055      private ArrayList attributeHandlers;
056      private ArrayList stylePropertyHandlers;
057      private DisplayConditionReadHandler displayConditionReadHandler;
058    
059      protected ElementReadHandler()
060      {
061        expressionHandlers = new ArrayList();
062        styleExpressionHandlers = new ArrayList();
063        attributeExpressionHandlers = new ArrayList();
064        stylePropertyHandlers = new ArrayList();
065        attributeHandlers = new ArrayList();
066      }
067    
068      public boolean isEnabled()
069      {
070        return enabled;
071      }
072    
073      public String getStyle()
074      {
075        return style;
076      }
077    
078      /**
079       * Starts parsing.
080       *
081       * @param attrs the attributes.
082       * @throws SAXException if there is a parsing error.
083       */
084      protected void startParsing(final Attributes attrs) throws SAXException
085      {
086        super.startParsing(attrs);
087        style = attrs.getValue(FlowReportFactoryModule.NAMESPACE, "style");
088        final String enabledValue = attrs.getValue(FlowReportFactoryModule.NAMESPACE, "enabled");
089        if (enabledValue != null)
090        {
091          enabled = "true".equals(enabledValue);
092        }
093        else
094        {
095          enabled = true;
096        }
097    
098        final String virtualValue = attrs.getValue(FlowReportFactoryModule.NAMESPACE, "virtual");
099        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    }