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: AutoTableElementReadHandler.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    
032    package org.jfree.report.modules.misc.autotable.xml;
033    
034    import java.util.ArrayList;
035    
036    import org.jfree.report.modules.factories.report.flow.AbstractElementReadHandler;
037    import org.jfree.report.modules.factories.report.base.NodeReadHandlerFactory;
038    import org.jfree.report.modules.factories.report.base.NodeReadHandler;
039    import org.jfree.report.modules.misc.autotable.AutoTableElement;
040    import org.jfree.report.modules.misc.autotable.AutoTableModule;
041    import org.jfree.report.structure.Element;
042    import org.jfree.report.structure.Section;
043    import org.jfree.xmlns.parser.XmlReadHandler;
044    import org.xml.sax.Attributes;
045    import org.xml.sax.SAXException;
046    
047    /**
048     * Creation-Date: Dec 9, 2006, 5:47:25 PM
049     *
050     * @author Thomas Morgner
051     */
052    public class AutoTableElementReadHandler extends AbstractElementReadHandler
053    {
054      private AutoTableElement autoTableElement;
055      private ArrayList headerSections;
056      private ArrayList footerSections;
057      private ArrayList contentSections;
058    
059      public AutoTableElementReadHandler()
060      {
061        autoTableElement = new AutoTableElement();
062        headerSections = new ArrayList();
063        contentSections = new ArrayList();
064        footerSections = new ArrayList();
065      }
066    
067      protected Element getElement()
068      {
069        return autoTableElement;
070      }
071    
072      /**
073       * Returns the handler for a child element.
074       *
075       * @param tagName the tag name.
076       * @param atts    the attributes.
077       * @return the handler or null, if the tagname is invalid.
078       * @throws org.xml.sax.SAXException if there is a parsing error.
079       * @throws XmlReaderException       if there is a reader error.
080       */
081      protected XmlReadHandler getHandlerForChild(final String uri,
082                                                  final String tagName,
083                                                  final Attributes atts)
084          throws SAXException
085      {
086        if (AutoTableModule.AUTOTABLE_NAMESPACE.equals(uri) == false)
087        {
088          return super.getHandlerForChild(uri, tagName, atts);
089        }
090    
091        final NodeReadHandlerFactory factory = NodeReadHandlerFactory.getInstance();
092        final NodeReadHandler handler = (NodeReadHandler) factory.getHandler(uri, tagName);
093        if (handler == null)
094        {
095          return null;
096        }
097    
098        if ("auto-table-header".equals(tagName))
099        {
100          headerSections.add(handler);
101          return handler;
102        }
103    
104        if ("auto-table-footer".equals(tagName))
105        {
106          footerSections.add(handler);
107          return handler;
108        }
109    
110        if ("auto-table-cell".equals(tagName))
111        {
112          contentSections.add(handler);
113          return handler;
114        }
115    
116        return null;
117      }
118    
119    
120      /**
121       * Done parsing.
122       *
123       * @throws org.xml.sax.SAXException if there is a parsing error.
124       */
125      protected void doneParsing() throws SAXException
126      {
127        for (int i = 0; i < headerSections.size(); i++)
128        {
129          final NodeReadHandler handler = (NodeReadHandler) headerSections.get(i);
130          autoTableElement.addHeader((Section) handler.getNode());
131        }
132    
133        for (int i = 0; i < footerSections.size(); i++)
134        {
135          final NodeReadHandler handler = (NodeReadHandler) footerSections.get(i);
136          autoTableElement.addFooter((Section) handler.getNode());
137        }
138    
139        for (int i = 0; i < contentSections.size(); i++)
140        {
141          final NodeReadHandler handler = (NodeReadHandler) contentSections.get(i);
142          autoTableElement.addContent((Section) handler.getNode());
143        }
144      }
145    }