Source for org.jfree.report.modules.factories.data.sql.SQLDataFactoryReadHandler

   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: SQLDataFactoryReadHandler.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.data.sql;
  32: 
  33: import java.util.ArrayList;
  34: 
  35: import org.jfree.report.ReportDataFactory;
  36: import org.jfree.report.modules.data.sql.ConnectionProvider;
  37: import org.jfree.report.modules.data.sql.SQLReportDataFactory;
  38: import org.jfree.report.modules.factories.data.base.DataFactoryReadHandler;
  39: import org.jfree.xmlns.parser.AbstractXmlReadHandler;
  40: import org.jfree.xmlns.parser.PropertyReadHandler;
  41: import org.jfree.xmlns.parser.XmlReadHandler;
  42: import org.jfree.xmlns.parser.ParseException;
  43: import org.xml.sax.Attributes;
  44: import org.xml.sax.SAXException;
  45: 
  46: /**
  47:  * Creation-Date: 07.04.2006, 17:47:53
  48:  *
  49:  * @author Thomas Morgner
  50:  */
  51: public class SQLDataFactoryReadHandler extends AbstractXmlReadHandler
  52:   implements DataFactoryReadHandler
  53: {
  54:   private ConnectionReadHandler connectionProviderReadHandler;
  55:   private ArrayList queries;
  56:   private ConfigReadHandler configReadHandler;
  57:   private ReportDataFactory dataFactory;
  58: 
  59:   public SQLDataFactoryReadHandler()
  60:   {
  61:     queries = new ArrayList();
  62:   }
  63: 
  64:   /**
  65:    * Returns the handler for a child element.
  66:    *
  67:    * @param tagName the tag name.
  68:    * @param atts    the attributes.
  69:    * @return the handler or null, if the tagname is invalid.
  70:    * @throws SAXException       if there is a parsing error.
  71:    */
  72:   protected XmlReadHandler getHandlerForChild(final String uri,
  73:                                               final String tagName,
  74:                                               final Attributes atts)
  75:           throws SAXException
  76:   {
  77: 
  78:     final ConnectionReadHandlerFactory factory = ConnectionReadHandlerFactory.getInstance();
  79:     final ConnectionReadHandler handler = (ConnectionReadHandler) factory.getHandler(uri, tagName);
  80:     if (handler != null)
  81:     {
  82:       connectionProviderReadHandler = handler;
  83:       return connectionProviderReadHandler;
  84:     }
  85: 
  86:     if (isSameNamespace(uri) == false)
  87:     {
  88:       return null;
  89:     }
  90:     if ("config".equals(tagName))
  91:     {
  92:       configReadHandler = new ConfigReadHandler();
  93:       return configReadHandler;
  94:     }
  95:     if ("query".equals(tagName))
  96:     {
  97:       final XmlReadHandler queryReadHandler = new PropertyReadHandler();
  98:       queries.add(queryReadHandler);
  99:       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: }