Source for org.jfree.report.modules.factories.report.flow.SubReportReadHandler

   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: SubReportReadHandler.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.report.flow;
  32: 
  33: import java.util.ArrayList;
  34: 
  35: import org.jfree.report.structure.Element;
  36: import org.jfree.report.structure.SubReport;
  37: import org.jfree.xmlns.parser.StringReadHandler;
  38: import org.jfree.xmlns.parser.XmlReadHandler;
  39: import org.jfree.xmlns.parser.ParseException;
  40: import org.xml.sax.Attributes;
  41: import org.xml.sax.SAXException;
  42: 
  43: /**
  44:  * Creation-Date: 09.04.2006, 14:57:38
  45:  *
  46:  * @author Thomas Morgner
  47:  */
  48: public class SubReportReadHandler extends SectionReadHandler
  49: {
  50:   private SubReport subReport;
  51:   private ArrayList importParameters;
  52:   private ArrayList exportParameters;
  53:   private StringReadHandler queryReadHandler;
  54: 
  55:   public SubReportReadHandler()
  56:   {
  57:     subReport = new SubReport();
  58:     importParameters = new ArrayList();
  59:     exportParameters = new ArrayList();
  60:   }
  61: 
  62:   /**
  63:    * Starts parsing.
  64:    *
  65:    * @param attrs the attributes.
  66:    * @throws org.xml.sax.SAXException if there is a parsing error.
  67:    */
  68:   protected void startParsing(final Attributes attrs) throws SAXException
  69:   {
  70:     super.startParsing(attrs);
  71:     final String source = attrs.getValue(getUri(), "href");
  72:     if (source != null)
  73:     {
  74:       // start parsing ..
  75:     }
  76:   }
  77: 
  78:   /**
  79:    * Returns the handler for a child element.
  80:    *
  81:    * @param tagName the tag name.
  82:    * @param atts    the attributes.
  83:    * @return the handler or null, if the tagname is invalid.
  84:    * @throws SAXException       if there is a parsing error.
  85:    */
  86:   protected XmlReadHandler getHandlerForChild(final String uri,
  87:                                               final String tagName,
  88:                                               final Attributes atts)
  89:           throws SAXException
  90:   {
  91:     final XmlReadHandler base = super.getHandlerForChild(uri, tagName, atts);
  92:     if (base != null)
  93:     {
  94:       return base;
  95:     }
  96:     if (FlowReportFactoryModule.NAMESPACE.equals(uri))
  97:     {
  98:       if ("import-parameter".equals(tagName))
  99:       {
 100:         final ParameterMappingReadHandler handler = new ParameterMappingReadHandler();
 101:         importParameters.add(handler);
 102:         return handler;
 103:       }
 104:       if ("export-parameter".equals(tagName))
 105:       {
 106:         final ParameterMappingReadHandler handler = new ParameterMappingReadHandler();
 107:         exportParameters.add(handler);
 108:         return handler;
 109:       }
 110:       if ("query".equals(tagName))
 111:       {
 112:         queryReadHandler = new StringReadHandler();
 113:         return queryReadHandler;
 114:       }
 115:     }
 116:     return null;
 117:   }
 118: 
 119:   /**
 120:    * Done parsing.
 121:    *
 122:    * @throws SAXException       if there is a parsing error.
 123:    */
 124:   protected void doneParsing() throws SAXException
 125:   {
 126:     super.doneParsing();
 127:     final SubReport report = (SubReport) getElement();
 128:     for (int i = 0; i < importParameters.size(); i++)
 129:     {
 130:       final ParameterMappingReadHandler handler =
 131:               (ParameterMappingReadHandler) importParameters.get(i);
 132:       report.addInputParameter(handler.getName(), handler.getAlias());
 133:     }
 134:     for (int i = 0; i < exportParameters.size(); i++)
 135:     {
 136:       final ParameterMappingReadHandler handler =
 137:               (ParameterMappingReadHandler) exportParameters.get(i);
 138:       report.addExportParameter(handler.getAlias(), handler.getName());
 139:     }
 140:     if (queryReadHandler == null)
 141:     {
 142:       throw new ParseException("Query is not specified.", getLocator());
 143:     }
 144:     final String result = queryReadHandler.getResult();
 145:     report.setQuery(result);
 146:   }
 147: 
 148:   protected Element getElement()
 149:   {
 150:     return subReport;
 151:   }
 152: }