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: SubReportReadHandler.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.report.structure.SubReport;
037    import org.jfree.xmlns.parser.StringReadHandler;
038    import org.jfree.xmlns.parser.XmlReadHandler;
039    import org.jfree.xmlns.parser.ParseException;
040    import org.xml.sax.Attributes;
041    import org.xml.sax.SAXException;
042    
043    /**
044     * Creation-Date: 09.04.2006, 14:57:38
045     *
046     * @author Thomas Morgner
047     */
048    public class SubReportReadHandler extends SectionReadHandler
049    {
050      private SubReport subReport;
051      private ArrayList importParameters;
052      private ArrayList exportParameters;
053      private StringReadHandler queryReadHandler;
054    
055      public SubReportReadHandler()
056      {
057        subReport = new SubReport();
058        importParameters = new ArrayList();
059        exportParameters = new ArrayList();
060      }
061    
062      /**
063       * Starts parsing.
064       *
065       * @param attrs the attributes.
066       * @throws org.xml.sax.SAXException if there is a parsing error.
067       */
068      protected void startParsing(final Attributes attrs) throws SAXException
069      {
070        super.startParsing(attrs);
071        final String source = attrs.getValue(getUri(), "href");
072        if (source != null)
073        {
074          // start parsing ..
075        }
076      }
077    
078      /**
079       * Returns the handler for a child element.
080       *
081       * @param tagName the tag name.
082       * @param atts    the attributes.
083       * @return the handler or null, if the tagname is invalid.
084       * @throws SAXException       if there is a parsing error.
085       */
086      protected XmlReadHandler getHandlerForChild(final String uri,
087                                                  final String tagName,
088                                                  final Attributes atts)
089              throws SAXException
090      {
091        final XmlReadHandler base = super.getHandlerForChild(uri, tagName, atts);
092        if (base != null)
093        {
094          return base;
095        }
096        if (FlowReportFactoryModule.NAMESPACE.equals(uri))
097        {
098          if ("import-parameter".equals(tagName))
099          {
100            final ParameterMappingReadHandler handler = new ParameterMappingReadHandler();
101            importParameters.add(handler);
102            return handler;
103          }
104          if ("export-parameter".equals(tagName))
105          {
106            final ParameterMappingReadHandler handler = new ParameterMappingReadHandler();
107            exportParameters.add(handler);
108            return handler;
109          }
110          if ("query".equals(tagName))
111          {
112            queryReadHandler = new StringReadHandler();
113            return queryReadHandler;
114          }
115        }
116        return null;
117      }
118    
119      /**
120       * Done parsing.
121       *
122       * @throws SAXException       if there is a parsing error.
123       */
124      protected void doneParsing() throws SAXException
125      {
126        super.doneParsing();
127        final SubReport report = (SubReport) getElement();
128        for (int i = 0; i < importParameters.size(); i++)
129        {
130          final ParameterMappingReadHandler handler =
131                  (ParameterMappingReadHandler) importParameters.get(i);
132          report.addInputParameter(handler.getName(), handler.getAlias());
133        }
134        for (int i = 0; i < exportParameters.size(); i++)
135        {
136          final ParameterMappingReadHandler handler =
137                  (ParameterMappingReadHandler) exportParameters.get(i);
138          report.addExportParameter(handler.getAlias(), handler.getName());
139        }
140        if (queryReadHandler == null)
141        {
142          throw new ParseException("Query is not specified.", getLocator());
143        }
144        final String result = queryReadHandler.getResult();
145        report.setQuery(result);
146      }
147    
148      protected Element getElement()
149      {
150        return subReport;
151      }
152    }