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

   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: ReportReadHandler.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: import java.util.Iterator;
  35: import java.util.Map;
  36: import java.util.Properties;
  37: 
  38: import org.jfree.report.JFreeReport;
  39: import org.jfree.report.modules.factories.data.base.DataFactoryReadHandlerFactory;
  40: import org.jfree.report.modules.factories.data.base.DataFactoryReadHandler;
  41: import org.jfree.report.structure.Element;
  42: import org.jfree.xmlns.parser.PropertiesReadHandler;
  43: import org.jfree.xmlns.parser.StringReadHandler;
  44: import org.jfree.xmlns.parser.XmlReadHandler;
  45: import org.jfree.xmlns.parser.ParseException;
  46: import org.xml.sax.Attributes;
  47: import org.xml.sax.SAXException;
  48: 
  49: /**
  50:  * Creation-Date: 09.04.2006, 14:57:38
  51:  *
  52:  * @author Thomas Morgner
  53:  */
  54: public class ReportReadHandler extends SectionReadHandler
  55: {
  56:   private StringReadHandler queryReadHandler;
  57:   private PropertiesReadHandler propertiesReadHandler;
  58:   private DataFactoryReadHandler datasourceFactoryReadHandler;
  59:   private ArrayList styleSheetReadHandlers;
  60:   private JFreeReport report;
  61: 
  62:   /**
  63:    * Creates a new generic read handler. The given namespace and tagname can be
  64:    * arbitary values and should not be confused with the ones provided by the
  65:    * XMLparser itself.
  66:    */
  67:   public ReportReadHandler()
  68:   {
  69:     report = new JFreeReport();
  70:     styleSheetReadHandlers = new ArrayList();
  71:   }
  72: 
  73:   /**
  74:    * Returns the handler for a child element.
  75:    *
  76:    * @param tagName the tag name.
  77:    * @param atts    the attributes.
  78:    * @return the handler or null, if the tagname is invalid.
  79:    * @throws SAXException       if there is a parsing error.
  80:    */
  81:   protected XmlReadHandler getHandlerForChild(final String uri,
  82:                                               final String tagName,
  83:                                               final Attributes atts)
  84:           throws SAXException
  85:   {
  86:     final XmlReadHandler base = super.getHandlerForChild(uri, tagName, atts);
  87:     if (base != null)
  88:     {
  89:       return base;
  90:     }
  91: 
  92:     final DataFactoryReadHandlerFactory factory = DataFactoryReadHandlerFactory.getInstance();
  93:     final DataFactoryReadHandler handler = (DataFactoryReadHandler) factory.getHandler(uri, tagName);
  94:     if (handler != null)
  95:     {
  96:       datasourceFactoryReadHandler = handler;
  97:       return handler;
  98:     }
  99: 
 100: 
 101:     if (FlowReportFactoryModule.NAMESPACE.equals(uri))
 102:     {
 103:       if ("query".equals(tagName))
 104:       {
 105:         queryReadHandler = new StringReadHandler();
 106:         return queryReadHandler;
 107:       }
 108:       if ("configuration".equals(tagName))
 109:       {
 110:         propertiesReadHandler = new PropertiesReadHandler();
 111:         return propertiesReadHandler;
 112:       }
 113:       if ("stylesheet".equals(tagName))
 114:       {
 115:         final StyleSheetReadHandler srh = new StyleSheetReadHandler();
 116:         styleSheetReadHandlers.add(srh);
 117:         return srh;
 118:       }
 119:       if ("inline-stylesheet".equals(tagName))
 120:       {
 121:         final StyleSheetReadHandler srh = new StyleSheetReadHandler();
 122:         styleSheetReadHandlers.add(srh);
 123:         return srh;
 124:       }
 125:     }
 126:     return null;
 127:   }
 128: 
 129:   /**
 130:    * Done parsing.
 131:    *
 132:    * @throws SAXException       if there is a parsing error.
 133:    */
 134:   protected void doneParsing() throws SAXException
 135:   {
 136:     if (queryReadHandler == null)
 137:     {
 138:       throw new ParseException
 139:               ("Required element 'query' is missing.", getLocator());
 140:     }
 141:     super.doneParsing();
 142:     final JFreeReport report = (JFreeReport) getElement();
 143:     report.setQuery(queryReadHandler.getResult());
 144:     if (propertiesReadHandler != null)
 145:     {
 146:       final Properties p = propertiesReadHandler.getResult();
 147:       final Iterator entries = p.entrySet().iterator();
 148:       while (entries.hasNext())
 149:       {
 150:         final Map.Entry entry = (Map.Entry) entries.next();
 151:         report.getEditableConfiguration().setConfigProperty
 152:                 ((String) entry.getKey(), (String) entry.getValue());
 153:       }
 154:     }
 155:     if (datasourceFactoryReadHandler != null)
 156:     {
 157:       report.setDataFactory(datasourceFactoryReadHandler.getDataFactory());
 158:     }
 159:     for (int i = 0; i < styleSheetReadHandlers.size(); i++)
 160:     {
 161:       final StyleSheetReadHandler handler = (StyleSheetReadHandler)
 162:               styleSheetReadHandlers.get(i);
 163:       report.addStyleSheet(handler.getStyleSheet());
 164:     }
 165:   }
 166: 
 167:   protected Element getElement()
 168:   {
 169:     return report;
 170:   }
 171: }