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: */package org.jfree.report.modules.factories.report.flow;
031:
032: import java.util.ArrayList;
033: import java.util.Iterator;
034: import java.util.Map;
035: import java.util.Properties;
036:
037: import org.jfree.report.JFreeReport;
038: import org.jfree.report.modules.factories.data.base.DataFactoryReadHandlerFactory;
039: import org.jfree.report.modules.factories.data.base.DataFactoryReadHandler;
040: import org.jfree.report.structure.Element;
041: import org.jfree.xmlns.parser.PropertiesReadHandler;
042: import org.jfree.xmlns.parser.StringReadHandler;
043: import org.jfree.xmlns.parser.XmlReadHandler;
044: import org.jfree.xmlns.parser.ParseException;
045: import org.xml.sax.Attributes;
046: import org.xml.sax.SAXException;
047:
048: /**
049: * Creation-Date: 09.04.2006, 14:57:38
050: *
051: * @author Thomas Morgner
052: */
053: public class ReportReadHandler extends SectionReadHandler {
054: private StringReadHandler queryReadHandler;
055: private PropertiesReadHandler propertiesReadHandler;
056: private DataFactoryReadHandler datasourceFactoryReadHandler;
057: private ArrayList styleSheetReadHandlers;
058: private JFreeReport report;
059:
060: /**
061: * Creates a new generic read handler. The given namespace and tagname can be
062: * arbitary values and should not be confused with the ones provided by the
063: * XMLparser itself.
064: */
065: public ReportReadHandler() {
066: report = new JFreeReport();
067: styleSheetReadHandlers = new ArrayList();
068: }
069:
070: /**
071: * Returns the handler for a child element.
072: *
073: * @param tagName the tag name.
074: * @param atts the attributes.
075: * @return the handler or null, if the tagname is invalid.
076: * @throws SAXException if there is a parsing error.
077: */
078: protected XmlReadHandler getHandlerForChild(final String uri,
079: final String tagName, final Attributes atts)
080: throws SAXException {
081: final XmlReadHandler base = super .getHandlerForChild(uri,
082: tagName, atts);
083: if (base != null) {
084: return base;
085: }
086:
087: final DataFactoryReadHandlerFactory factory = DataFactoryReadHandlerFactory
088: .getInstance();
089: final DataFactoryReadHandler handler = (DataFactoryReadHandler) factory
090: .getHandler(uri, tagName);
091: if (handler != null) {
092: datasourceFactoryReadHandler = handler;
093: return handler;
094: }
095:
096: if (FlowReportFactoryModule.NAMESPACE.equals(uri)) {
097: if ("query".equals(tagName)) {
098: queryReadHandler = new StringReadHandler();
099: return queryReadHandler;
100: }
101: if ("configuration".equals(tagName)) {
102: propertiesReadHandler = new PropertiesReadHandler();
103: return propertiesReadHandler;
104: }
105: if ("stylesheet".equals(tagName)) {
106: final StyleSheetReadHandler srh = new StyleSheetReadHandler();
107: styleSheetReadHandlers.add(srh);
108: return srh;
109: }
110: if ("inline-stylesheet".equals(tagName)) {
111: final StyleSheetReadHandler srh = new StyleSheetReadHandler();
112: styleSheetReadHandlers.add(srh);
113: return srh;
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: if (queryReadHandler == null) {
126: throw new ParseException(
127: "Required element 'query' is missing.",
128: getLocator());
129: }
130: super .doneParsing();
131: final JFreeReport report = (JFreeReport) getElement();
132: report.setQuery(queryReadHandler.getResult());
133: if (propertiesReadHandler != null) {
134: final Properties p = propertiesReadHandler.getResult();
135: final Iterator entries = p.entrySet().iterator();
136: while (entries.hasNext()) {
137: final Map.Entry entry = (Map.Entry) entries.next();
138: report.getEditableConfiguration().setConfigProperty(
139: (String) entry.getKey(),
140: (String) entry.getValue());
141: }
142: }
143: if (datasourceFactoryReadHandler != null) {
144: report.setDataFactory(datasourceFactoryReadHandler
145: .getDataFactory());
146: }
147: for (int i = 0; i < styleSheetReadHandlers.size(); i++) {
148: final StyleSheetReadHandler handler = (StyleSheetReadHandler) styleSheetReadHandlers
149: .get(i);
150: report.addStyleSheet(handler.getStyleSheet());
151: }
152: }
153:
154: protected Element getElement() {
155: return report;
156: }
157: }
|