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: ReportReadHandler.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.report.flow; 032 033 import java.util.ArrayList; 034 import java.util.Iterator; 035 import java.util.Map; 036 import java.util.Properties; 037 038 import org.jfree.report.JFreeReport; 039 import org.jfree.report.modules.factories.data.base.DataFactoryReadHandlerFactory; 040 import org.jfree.report.modules.factories.data.base.DataFactoryReadHandler; 041 import org.jfree.report.structure.Element; 042 import org.jfree.xmlns.parser.PropertiesReadHandler; 043 import org.jfree.xmlns.parser.StringReadHandler; 044 import org.jfree.xmlns.parser.XmlReadHandler; 045 import org.jfree.xmlns.parser.ParseException; 046 import org.xml.sax.Attributes; 047 import org.xml.sax.SAXException; 048 049 /** 050 * Creation-Date: 09.04.2006, 14:57:38 051 * 052 * @author Thomas Morgner 053 */ 054 public class ReportReadHandler extends SectionReadHandler 055 { 056 private StringReadHandler queryReadHandler; 057 private PropertiesReadHandler propertiesReadHandler; 058 private DataFactoryReadHandler datasourceFactoryReadHandler; 059 private ArrayList styleSheetReadHandlers; 060 private JFreeReport report; 061 062 /** 063 * Creates a new generic read handler. The given namespace and tagname can be 064 * arbitary values and should not be confused with the ones provided by the 065 * XMLparser itself. 066 */ 067 public ReportReadHandler() 068 { 069 report = new JFreeReport(); 070 styleSheetReadHandlers = new ArrayList(); 071 } 072 073 /** 074 * Returns the handler for a child element. 075 * 076 * @param tagName the tag name. 077 * @param atts the attributes. 078 * @return the handler or null, if the tagname is invalid. 079 * @throws SAXException if there is a parsing error. 080 */ 081 protected XmlReadHandler getHandlerForChild(final String uri, 082 final String tagName, 083 final Attributes atts) 084 throws SAXException 085 { 086 final XmlReadHandler base = super.getHandlerForChild(uri, tagName, atts); 087 if (base != null) 088 { 089 return base; 090 } 091 092 final DataFactoryReadHandlerFactory factory = DataFactoryReadHandlerFactory.getInstance(); 093 final DataFactoryReadHandler handler = (DataFactoryReadHandler) factory.getHandler(uri, tagName); 094 if (handler != null) 095 { 096 datasourceFactoryReadHandler = handler; 097 return handler; 098 } 099 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 }