Source for org.jfree.report.modules.factories.data.beans.StaticDataSourceReadHandler

   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: StaticDataSourceReadHandler.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: 
  32: package org.jfree.report.modules.factories.data.beans;
  33: 
  34: import java.util.ArrayList;
  35: 
  36: import org.jfree.xmlns.parser.AbstractXmlReadHandler;
  37: import org.jfree.xmlns.parser.PropertyReadHandler;
  38: import org.jfree.xmlns.parser.XmlReadHandler;
  39: import org.jfree.report.modules.factories.data.base.DataFactoryReadHandler;
  40: import org.jfree.report.modules.data.beans.NamedStaticReportDataFactory;
  41: import org.jfree.report.ReportDataFactory;
  42: import org.xml.sax.Attributes;
  43: import org.xml.sax.SAXException;
  44: 
  45: /**
  46:  * Creation-Date: 07.04.2006, 17:47:53
  47:  *
  48:  * @author Thomas Morgner
  49:  */
  50: public class StaticDataSourceReadHandler extends AbstractXmlReadHandler
  51:   implements DataFactoryReadHandler
  52: {
  53:   private ArrayList queries;
  54:   private ReportDataFactory dataFactory;
  55: 
  56:   public StaticDataSourceReadHandler()
  57:   {
  58:     queries = new ArrayList();
  59:   }
  60: 
  61:   /**
  62:    * Returns the handler for a child element.
  63:    *
  64:    * @param tagName the tag name.
  65:    * @param atts    the attributes.
  66:    * @return the handler or null, if the tagname is invalid.
  67:    * @throws org.xml.sax.SAXException       if there is a parsing error.
  68:    * @throws XmlReaderException if there is a reader error.
  69:    */
  70:   protected XmlReadHandler getHandlerForChild(final String uri,
  71:                                               final String tagName,
  72:                                               final Attributes atts)
  73:           throws SAXException
  74:   {
  75:     if (isSameNamespace(uri) == false)
  76:     {
  77:       return null;
  78:     }
  79: 
  80:     if ("query".equals(tagName))
  81:     {
  82:       final XmlReadHandler queryReadHandler = new PropertyReadHandler();
  83:       queries.add(queryReadHandler);
  84:       return queryReadHandler;
  85:     }
  86:     return null;
  87:   }
  88: 
  89:   /**
  90:    * Done parsing.
  91:    *
  92:    * @throws org.xml.sax.SAXException       if there is a parsing error.
  93:    * @throws XmlReaderException if there is a reader error.
  94:    */
  95:   protected void doneParsing() throws SAXException
  96:   {
  97:     final NamedStaticReportDataFactory srdf = new NamedStaticReportDataFactory();
  98:     for (int i = 0; i < queries.size(); i++)
  99:     {
 100:       final PropertyReadHandler handler = (PropertyReadHandler) queries.get(i);
 101:       srdf.setQuery(handler.getName(), handler.getResult());
 102:     }
 103: 
 104:     srdf.setContentBase(getRootHandler().getSource());
 105:     dataFactory = srdf;
 106:   }
 107: 
 108:   /**
 109:    * Returns the object for this element or null, if this element does not
 110:    * create an object.
 111:    *
 112:    * @return the object.
 113:    * @throws XmlReaderException if there is a parsing error.
 114:    */
 115:   public Object getObject() throws SAXException
 116:   {
 117:     return dataFactory;
 118:   }
 119: 
 120:   public ReportDataFactory getDataFactory()
 121:   {
 122:     return dataFactory;
 123:   }
 124: }