⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 objectfactoryloader.java

📁 该源代码为一些通用的程序
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* ========================================================================
 * JCommon : a free general purpose class library for the Java(tm) platform
 * ========================================================================
 *
 * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
 * 
 * Project Info:  http://www.jfree.org/jcommon/index.html
 *
 * This library is free software; you can redistribute it and/or modify it 
 * under the terms of the GNU Lesser General Public License as published by 
 * the Free Software Foundation; either version 2.1 of the License, or 
 * (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but 
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
 * License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 
 * USA.  
 *
 * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 
 * in the United States and other countries.]
 * 
 * ------------------------
 * ObjectFactoryLoader.java
 * ------------------------
 * (C) Copyright 2002-2005, by Thomas Morgner and Contributors.
 *
 * Original Author:  Thomas Morgner;
 * Contributor(s):   -;
 *
 * $Id: ObjectFactoryLoader.java,v 1.4 2005/10/18 13:33:53 mungady Exp $
 *
 * Changes
 * -------
 * 24-Sep-2003: Initial version
 *
 */

package org.jfree.xml.util;

import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;

import org.jfree.util.Log;
import org.jfree.xml.attributehandlers.AttributeHandler;

/**
 * The object factory loader loads the xml specification for the generic
 * handlers. The specification may be distributed over multiple files.
 * <p>
 * This class provides the model management for the reader and writer.
 * The instantiation of the handlers is done elsewhere.
 *
 * @author TM
 */
public class ObjectFactoryLoader extends AbstractModelReader implements ObjectFactory {

    /** Maps classes to GenericObjectFactory instances. */
    private HashMap objectMappings;
    
    /** Manual mappings. */
    private HashMap manualMappings;
    
    /** Multiplex mappings. */
    private HashMap multiplexMappings;

    /** The target class. */
    private Class target;
    
    /** The register name. */
    private String registerName;
    
    /** The property definition. */
    private ArrayList propertyDefinition;
    
    /** The attribute definition. */
    private ArrayList attributeDefinition;
    
    /** The constructor definition. */
    private ArrayList constructorDefinition;
    
    /** The lookup definitions. */
    private ArrayList lookupDefinitions;
    
    /** The ordered names. */
    private ArrayList orderedNames;

    /** The base class. */
    private String baseClass;
    
    /** The attribute name. */
    private String attributeName;
    
    /** The multiplex entries. */
    private ArrayList multiplexEntries;

    /**
     * Creates a new object factory loader for the given base file.
     *
     * @param resourceName the URL of the initial specification file.
     * 
     * @throws ObjectDescriptionException if the file could not be parsed.
     */
    public ObjectFactoryLoader(final URL resourceName) throws ObjectDescriptionException {
        this.objectMappings = new HashMap();
        this.manualMappings = new HashMap();
        this.multiplexMappings = new HashMap();
        parseXml(resourceName);
        rebuildSuperClasses();
    }

    private void rebuildSuperClasses() throws ObjectDescriptionException {
        this.propertyDefinition = new ArrayList();
        this.attributeDefinition = new ArrayList();
        this.constructorDefinition = new ArrayList();
        this.lookupDefinitions = new ArrayList();
        this.orderedNames = new ArrayList();

        final HashMap newObjectDescriptions = new HashMap();
        final Iterator it = this.objectMappings.keySet().iterator();
        while (it.hasNext()) {
            final Object key = it.next();
            final GenericObjectFactory gef = (GenericObjectFactory) this.objectMappings.get(key);
            performSuperClassUpdate(gef);

            final PropertyDefinition[] propertyDefs = (PropertyDefinition[])
            this.propertyDefinition.toArray(new PropertyDefinition[0]);
            final LookupDefinition[] lookupDefs = (LookupDefinition[])
            this.lookupDefinitions.toArray(new LookupDefinition[0]);
            final AttributeDefinition[] attribDefs = (AttributeDefinition[])
            this.attributeDefinition.toArray(new AttributeDefinition[0]);
            final ConstructorDefinition[] constructorDefs = (ConstructorDefinition[])
            this.constructorDefinition.toArray(new ConstructorDefinition[0]);
            final String[] orderedNamesDefs = (String[])
            this.orderedNames.toArray(new String[0]);

            final GenericObjectFactory objectFactory = new GenericObjectFactory
                (gef.getBaseClass(), gef.getRegisterName(), constructorDefs,
                    propertyDefs, lookupDefs, attribDefs, orderedNamesDefs);
            newObjectDescriptions.put(key, objectFactory);

            this.propertyDefinition.clear();
            this.attributeDefinition.clear();
            this.constructorDefinition.clear();
            this.lookupDefinitions.clear();
            this.orderedNames.clear();
        }

        this.objectMappings.clear();
        this.objectMappings = newObjectDescriptions;

        this.propertyDefinition = null;
        this.attributeDefinition = null;
        this.constructorDefinition = null;
        this.lookupDefinitions = null;
        this.orderedNames = null;
    }

    private void performSuperClassUpdate(final GenericObjectFactory gef) {
        // first handle the super classes, ...
        final Class superClass = gef.getBaseClass().getSuperclass();
        if (superClass != null && !superClass.equals(Object.class)) {
            final GenericObjectFactory superGef = (GenericObjectFactory) this.objectMappings.get(
                superClass
            );
            if (superGef != null) {
                performSuperClassUpdate(superGef);
            }
        }

        // and finally append all local properties ...
        this.propertyDefinition.addAll(Arrays.asList(gef.getPropertyDefinitions()));
        this.attributeDefinition.addAll(Arrays.asList(gef.getAttributeDefinitions()));
        this.constructorDefinition.addAll(Arrays.asList(gef.getConstructorDefinitions()));
        this.lookupDefinitions.addAll(Arrays.asList(gef.getLookupDefinitions()));
        this.orderedNames.addAll(Arrays.asList(gef.getOrderedPropertyNames()));
    }

    /**
     * Starts a object definition. The object definition collects all properties of
     * an bean-class and defines, which constructor should be used when creating the
     * class.
     *
     * @param className the class name of the defined object
     * @param register the (optional) register name, to lookup and reference the object later.
     * @param ignore  ignore?
     * 
     * @return true, if the definition was accepted, false otherwise.
     * @throws ObjectDescriptionException if an unexpected error occured.
     */
    protected boolean startObjectDefinition(final String className, final String register, final boolean ignore)
        throws ObjectDescriptionException {

        if (ignore) {
            return false;
        }
        this.target = loadClass(className);
        if (this.target == null) {
            Log.warn(new Log.SimpleMessage("Failed to load class ", className));
            return false;
        }
        this.registerName = register;
        this.propertyDefinition = new ArrayList();
        this.attributeDefinition = new ArrayList();
        this.constructorDefinition = new ArrayList();
        this.lookupDefinitions = new ArrayList();
        this.orderedNames = new ArrayList();
        return true;
    }

    /**
     * Handles an attribute definition. This method gets called after the object definition
     * was started. The method will be called for every defined attribute property.
     *

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -