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

   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: DriverConnectionReadHandler.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.Iterator;
  34: import java.util.Map;
  35: import java.util.Properties;
  36: 
  37: import org.jfree.report.modules.data.sql.DriverConnectionProvider;
  38: import org.jfree.report.modules.data.sql.ConnectionProvider;
  39: import org.jfree.xmlns.parser.AbstractXmlReadHandler;
  40: import org.jfree.xmlns.parser.PropertiesReadHandler;
  41: import org.jfree.xmlns.parser.StringReadHandler;
  42: import org.jfree.xmlns.parser.XmlReadHandler;
  43: import org.xml.sax.Attributes;
  44: import org.xml.sax.SAXException;
  45: 
  46: /**
  47:  * Creation-Date: 07.04.2006, 18:09:25
  48:  *
  49:  * @author Thomas Morgner
  50:  */
  51: public class DriverConnectionReadHandler extends AbstractXmlReadHandler
  52:   implements ConnectionReadHandler
  53: {
  54:   private StringReadHandler driverReadHandler;
  55:   private StringReadHandler urlReadHandler;
  56:   private PropertiesReadHandler propertiesReadHandler;
  57:   private ConnectionProvider driverConnectionProvider;
  58: 
  59:   public DriverConnectionReadHandler()
  60:   {
  61:   }
  62: 
  63:   /**
  64:    * Returns the handler for a child element.
  65:    *
  66:    * @param tagName the tag name.
  67:    * @param atts    the attributes.
  68:    * @return the handler or null, if the tagname is invalid.
  69:    * @throws SAXException       if there is a parsing error.
  70:    * @throws XmlReaderException if there is a reader error.
  71:    */
  72:   protected XmlReadHandler getHandlerForChild(final String uri,
  73:                                               final String tagName,
  74:                                               final Attributes atts)
  75:           throws SAXException
  76:   {
  77:     if (isSameNamespace(uri) == false)
  78:     {
  79:       return null;
  80:     }
  81:     if ("driver".equals(tagName))
  82:     {
  83:       driverReadHandler = new StringReadHandler();
  84:       return driverReadHandler;
  85:     }
  86:     if ("url".equals(tagName))
  87:     {
  88:       urlReadHandler = new StringReadHandler();
  89:       return urlReadHandler;
  90:     }
  91:     if ("properties".equals(tagName))
  92:     {
  93:       propertiesReadHandler = new PropertiesReadHandler();
  94:       return propertiesReadHandler;
  95:     }
  96:     return null;
  97:   }
  98: 
  99:   /**
 100:    * Done parsing.
 101:    *
 102:    * @throws SAXException       if there is a parsing error.
 103:    * @throws XmlReaderException if there is a reader error.
 104:    */
 105:   protected void doneParsing() throws SAXException
 106:   {
 107:     final DriverConnectionProvider provider = new DriverConnectionProvider();
 108:     if (driverReadHandler != null)
 109:     {
 110:       provider.setDriver(driverReadHandler.getResult());
 111:     }
 112:     if (urlReadHandler != null)
 113:     {
 114:       provider.setUrl(urlReadHandler.getResult());
 115:     }
 116:     if (propertiesReadHandler != null)
 117:     {
 118:       final Properties p = (Properties) propertiesReadHandler.getObject();
 119:       final Iterator it = p.entrySet().iterator();
 120:       while (it.hasNext())
 121:       {
 122:         final Map.Entry entry = (Map.Entry) it.next();
 123:         provider.setProperty((String) entry.getKey(), (String) entry.getValue());
 124:       }
 125:     }
 126:     driverConnectionProvider = provider;
 127:   }
 128: 
 129:   /**
 130:    * Returns the object for this element or null, if this element does not
 131:    * create an object.
 132:    *
 133:    * @return the object.
 134:    * @throws XmlReaderException if there is a parsing error.
 135:    */
 136:   public Object getObject() throws SAXException
 137:   {
 138:     return driverConnectionProvider;
 139:   }
 140: 
 141:   public ConnectionProvider getProvider()
 142:   {
 143:     return driverConnectionProvider;
 144:   }
 145: }