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: DefaultLayoutControllerFactory.java 3048 2007-07-28 18:02:42Z tmorgner $
027: * ------------
028: * (C) Copyright 2000-2005, by Object Refinery Limited.
029: * (C) Copyright 2005-2007, by Pentaho Corporation.
030: */package org.jfree.report.flow.layoutprocessor;
031:
032: import java.util.HashMap;
033: import java.util.Iterator;
034:
035: import org.jfree.report.DataSourceException;
036: import org.jfree.report.ReportDataFactoryException;
037: import org.jfree.report.ReportProcessingException;
038: import org.jfree.report.flow.FlowController;
039: import org.jfree.report.flow.ReportJob;
040: import org.jfree.report.structure.Node;
041: import org.jfree.util.Configuration;
042: import org.jfree.util.ObjectUtilities;
043:
044: /**
045: * A layout controller factory that selects layout controllers by their node
046: * implementation type.
047: *
048: * @author Thomas Morgner
049: */
050: public class DefaultLayoutControllerFactory implements
051: LayoutControllerFactory {
052: private HashMap registry;
053: private static final String PREFIX = "org.jfree.report.flow.structure.";
054:
055: public DefaultLayoutControllerFactory() {
056: registry = new HashMap();
057:
058: }
059:
060: public void initialize(final ReportJob job) {
061: final Configuration configuration = job.getConfiguration();
062:
063: final Iterator propertyKeys = configuration
064: .findPropertyKeys(DefaultLayoutControllerFactory.PREFIX);
065: while (propertyKeys.hasNext()) {
066: final String key = (String) propertyKeys.next();
067: final String nodeClassName = key
068: .substring(DefaultLayoutControllerFactory.PREFIX
069: .length());
070: final String procClassName = configuration
071: .getConfigProperty(key);
072:
073: final Class nodeClass = load(nodeClassName);
074: final Object processor = ObjectUtilities
075: .loadAndInstantiate(procClassName,
076: DefaultLayoutControllerFactory.class,
077: LayoutController.class);
078: if (nodeClass == null || processor == null) {
079: // sanity check ..
080: continue;
081: }
082:
083: registry.put(nodeClassName, procClassName);
084: }
085: }
086:
087: private Class load(final String className) {
088: if (className == null) {
089: return null;
090: }
091:
092: final ClassLoader classLoader = ObjectUtilities
093: .getClassLoader(DefaultLayoutControllerFactory.class);
094: try {
095: return classLoader.loadClass(className);
096: } catch (ClassNotFoundException e) {
097: return null;
098: }
099: }
100:
101: public LayoutController create(final FlowController controller,
102: final Object node, final LayoutController parent)
103: throws ReportProcessingException,
104: ReportDataFactoryException, DataSourceException {
105: Class nodeClass = node.getClass();
106: while (Node.class.isAssignableFrom(nodeClass)) {
107: final String targetClass = (String) registry.get(nodeClass
108: .getName());
109: if (targetClass != null) {
110: final LayoutController lc = (LayoutController) ObjectUtilities
111: .loadAndInstantiate(targetClass,
112: DefaultLayoutControllerFactory.class,
113: LayoutController.class);
114: if (lc != null) {
115: lc.initialize(node, controller, parent);
116: return lc;
117: }
118: }
119: nodeClass = nodeClass.getSuperclass();
120: }
121:
122: throw new ReportProcessingException("No processor for node "
123: + node);
124: }
125: }
|