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: DriverConnectionReadHandler.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.Iterator;
034    import java.util.Map;
035    import java.util.Properties;
036    
037    import org.jfree.report.modules.data.sql.DriverConnectionProvider;
038    import org.jfree.report.modules.data.sql.ConnectionProvider;
039    import org.jfree.xmlns.parser.AbstractXmlReadHandler;
040    import org.jfree.xmlns.parser.PropertiesReadHandler;
041    import org.jfree.xmlns.parser.StringReadHandler;
042    import org.jfree.xmlns.parser.XmlReadHandler;
043    import org.xml.sax.Attributes;
044    import org.xml.sax.SAXException;
045    
046    /**
047     * Creation-Date: 07.04.2006, 18:09:25
048     *
049     * @author Thomas Morgner
050     */
051    public class DriverConnectionReadHandler extends AbstractXmlReadHandler
052      implements ConnectionReadHandler
053    {
054      private StringReadHandler driverReadHandler;
055      private StringReadHandler urlReadHandler;
056      private PropertiesReadHandler propertiesReadHandler;
057      private ConnectionProvider driverConnectionProvider;
058    
059      public DriverConnectionReadHandler()
060      {
061      }
062    
063      /**
064       * Returns the handler for a child element.
065       *
066       * @param tagName the tag name.
067       * @param atts    the attributes.
068       * @return the handler or null, if the tagname is invalid.
069       * @throws SAXException       if there is a parsing error.
070       * @throws XmlReaderException if there is a reader error.
071       */
072      protected XmlReadHandler getHandlerForChild(final String uri,
073                                                  final String tagName,
074                                                  final Attributes atts)
075              throws SAXException
076      {
077        if (isSameNamespace(uri) == false)
078        {
079          return null;
080        }
081        if ("driver".equals(tagName))
082        {
083          driverReadHandler = new StringReadHandler();
084          return driverReadHandler;
085        }
086        if ("url".equals(tagName))
087        {
088          urlReadHandler = new StringReadHandler();
089          return urlReadHandler;
090        }
091        if ("properties".equals(tagName))
092        {
093          propertiesReadHandler = new PropertiesReadHandler();
094          return propertiesReadHandler;
095        }
096        return null;
097      }
098    
099      /**
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    }