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: SQLDataFactoryReadHandler.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.data.sql;
032    
033    import java.util.ArrayList;
034    
035    import org.jfree.report.ReportDataFactory;
036    import org.jfree.report.modules.data.sql.ConnectionProvider;
037    import org.jfree.report.modules.data.sql.SQLReportDataFactory;
038    import org.jfree.report.modules.factories.data.base.DataFactoryReadHandler;
039    import org.jfree.xmlns.parser.AbstractXmlReadHandler;
040    import org.jfree.xmlns.parser.PropertyReadHandler;
041    import org.jfree.xmlns.parser.XmlReadHandler;
042    import org.jfree.xmlns.parser.ParseException;
043    import org.xml.sax.Attributes;
044    import org.xml.sax.SAXException;
045    
046    /**
047     * Creation-Date: 07.04.2006, 17:47:53
048     *
049     * @author Thomas Morgner
050     */
051    public class SQLDataFactoryReadHandler extends AbstractXmlReadHandler
052      implements DataFactoryReadHandler
053    {
054      private ConnectionReadHandler connectionProviderReadHandler;
055      private ArrayList queries;
056      private ConfigReadHandler configReadHandler;
057      private ReportDataFactory dataFactory;
058    
059      public SQLDataFactoryReadHandler()
060      {
061        queries = new ArrayList();
062      }
063    
064      /**
065       * Returns the handler for a child element.
066       *
067       * @param tagName the tag name.
068       * @param atts    the attributes.
069       * @return the handler or null, if the tagname is invalid.
070       * @throws SAXException       if there is a parsing error.
071       */
072      protected XmlReadHandler getHandlerForChild(final String uri,
073                                                  final String tagName,
074                                                  final Attributes atts)
075              throws SAXException
076      {
077    
078        final ConnectionReadHandlerFactory factory = ConnectionReadHandlerFactory.getInstance();
079        final ConnectionReadHandler handler = (ConnectionReadHandler) factory.getHandler(uri, tagName);
080        if (handler != null)
081        {
082          connectionProviderReadHandler = handler;
083          return connectionProviderReadHandler;
084        }
085    
086        if (isSameNamespace(uri) == false)
087        {
088          return null;
089        }
090        if ("config".equals(tagName))
091        {
092          configReadHandler = new ConfigReadHandler();
093          return configReadHandler;
094        }
095        if ("query".equals(tagName))
096        {
097          final XmlReadHandler queryReadHandler = new PropertyReadHandler();
098          queries.add(queryReadHandler);
099          return queryReadHandler;
100        }
101        return null;
102      }
103    
104      /**
105       * Done parsing.
106       *
107       * @throws SAXException       if there is a parsing error.
108       */
109      protected void doneParsing() throws SAXException
110      {
111        ConnectionProvider provider = null;
112        if (connectionProviderReadHandler != null)
113        {
114          provider = connectionProviderReadHandler.getProvider();
115        }
116        if (provider == null)
117        {
118          provider = (ConnectionProvider)
119                  getRootHandler().getHelperObject("connection-provider");
120        }
121        if (provider == null)
122        {
123          throw new ParseException
124              ("Unable to create SQL Factory: No connection provider.", getLocator());
125        }
126    
127        final SQLReportDataFactory srdf = new SQLReportDataFactory(provider);
128        if (configReadHandler != null)
129        {
130          final Boolean labelMapping = configReadHandler.isLabelMapping();
131          if (labelMapping != null)
132          {
133            srdf.setLabelMapping(labelMapping.booleanValue());
134          }
135        }
136        for (int i = 0; i < queries.size(); i++)
137        {
138          final PropertyReadHandler handler = (PropertyReadHandler) queries.get(i);
139          srdf.setQuery(handler.getName(), handler.getResult());
140        }
141        dataFactory = srdf;
142      }
143    
144      /**
145       * Returns the object for this element or null, if this element does not
146       * create an object.
147       *
148       * @return the object.
149       * @throws SAXException if there is a parsing error.
150       */
151      public Object getObject() throws SAXException
152      {
153        return dataFactory;
154      }
155    
156      public ReportDataFactory getDataFactory()
157      {
158        return dataFactory;
159      }
160    }