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

📄 rootxmlreadhandler.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.]
 * 
 * -----------------------
 * RootXmlReadHandler.java
 * -----------------------
 * (C)opyright 2003, 2004, by Thomas Morgner and Contributors.
 *
 * Original Author:  Thomas Morgner;
 * Contributor(s):   David Gilbert (for Object Refinery Limited);
 *
 * $Id: RootXmlReadHandler.java,v 1.8 2005/10/18 13:32:52 mungady Exp $
 *
 * Changes (from 25-Nov-2003)
 * --------------------------
 * 25-Nov-2003 : Added Javadocs (DG);
 * 22-Feb-2005 : Fixed a bug when ending nested tags with the same tagname.  
 */
package org.jfree.xml.parser;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Insets;
import java.awt.Paint;
import java.awt.RenderingHints;
import java.awt.Stroke;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Stack;
import java.util.Vector;

import org.jfree.util.ObjectUtilities;
import org.jfree.xml.FrontendDefaultHandler;
import org.jfree.xml.ParseException;
import org.jfree.xml.ElementDefinitionException;
import org.jfree.xml.parser.coretypes.BasicStrokeReadHandler;
import org.jfree.xml.parser.coretypes.ColorReadHandler;
import org.jfree.xml.parser.coretypes.FontReadHandler;
import org.jfree.xml.parser.coretypes.GenericReadHandler;
import org.jfree.xml.parser.coretypes.GradientPaintReadHandler;
import org.jfree.xml.parser.coretypes.InsetsReadHandler;
import org.jfree.xml.parser.coretypes.ListReadHandler;
import org.jfree.xml.parser.coretypes.Point2DReadHandler;
import org.jfree.xml.parser.coretypes.Rectangle2DReadHandler;
import org.jfree.xml.parser.coretypes.RenderingHintsReadHandler;
import org.jfree.xml.parser.coretypes.StringReadHandler;
import org.jfree.xml.util.ManualMappingDefinition;
import org.jfree.xml.util.MultiplexMappingDefinition;
import org.jfree.xml.util.MultiplexMappingEntry;
import org.jfree.xml.util.ObjectFactory;
import org.jfree.xml.util.SimpleObjectFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

/**
 * A base root SAX handler.
 */
public abstract class RootXmlReadHandler extends FrontendDefaultHandler {

    /** The current handlers. */
    private Stack currentHandlers;

    /** ??. */
    private Stack outerScopes;

    /** The root handler. */
    private XmlReadHandler rootHandler;

    /** The object registry. */
    private HashMap objectRegistry;

    /** Maps classes to handlers. */
    private SimpleObjectFactory classToHandlerMapping;

    private boolean rootHandlerInitialized;

    /**
     * Creates a new root SAX handler.
     */
    public RootXmlReadHandler() {
        this.objectRegistry = new HashMap();
        this.classToHandlerMapping = new SimpleObjectFactory();
    }

    protected void addDefaultMappings () {

        final MultiplexMappingEntry[] paintEntries = new MultiplexMappingEntry[2];
        paintEntries[0] = new MultiplexMappingEntry("color", Color.class.getName());
        paintEntries[1] = new MultiplexMappingEntry("gradientPaint", GradientPaint.class.getName());
        addMultiplexMapping(Paint.class, "type", paintEntries);
        addManualMapping(Color.class, ColorReadHandler.class);
        addManualMapping(GradientPaint.class, GradientPaintReadHandler.class);

        final MultiplexMappingEntry[] point2DEntries = new MultiplexMappingEntry[2];
        point2DEntries[0] = new MultiplexMappingEntry("float", Point2D.Float.class.getName());
        point2DEntries[1] = new MultiplexMappingEntry("double", Point2D.Double.class.getName());
        addMultiplexMapping(Point2D.class, "type", point2DEntries);
        addManualMapping(Point2D.Float.class, Point2DReadHandler.class);
        addManualMapping(Point2D.Double.class, Point2DReadHandler.class);

        final MultiplexMappingEntry[] rectangle2DEntries = new MultiplexMappingEntry[2];
        rectangle2DEntries[0] = new MultiplexMappingEntry(
            "float", Rectangle2D.Float.class.getName()
        );
        rectangle2DEntries[1] = new MultiplexMappingEntry(
            "double", Rectangle2D.Double.class.getName()
        );
        addMultiplexMapping(Rectangle2D.class, "type", rectangle2DEntries);
        addManualMapping(Rectangle2D.Float.class, Rectangle2DReadHandler.class);
        addManualMapping(Rectangle2D.Double.class, Rectangle2DReadHandler.class);

        // Handle list types
        final MultiplexMappingEntry[] listEntries = new MultiplexMappingEntry[4];
        listEntries[0] = new MultiplexMappingEntry("array-list", ArrayList.class.getName());
        listEntries[1] = new MultiplexMappingEntry("linked-list", LinkedList.class.getName());
        listEntries[2] = new MultiplexMappingEntry("vector", Vector.class.getName());
        listEntries[3] = new MultiplexMappingEntry("stack", Stack.class.getName());
        addMultiplexMapping(List.class, "type", listEntries);
        addManualMapping(LinkedList.class, ListReadHandler.class);
        addManualMapping(Vector.class, ListReadHandler.class);
        addManualMapping(ArrayList.class, ListReadHandler.class);
        addManualMapping(Stack.class, ListReadHandler.class);

        final MultiplexMappingEntry[] strokeEntries = new MultiplexMappingEntry[1];
        strokeEntries[0] = new MultiplexMappingEntry("basic", BasicStroke.class.getName());
        addMultiplexMapping(Stroke.class, "type", strokeEntries);
        addManualMapping(BasicStroke.class, BasicStrokeReadHandler.class);

        addManualMapping(Font.class, FontReadHandler.class);
        addManualMapping(Insets.class, InsetsReadHandler.class);
        addManualMapping(RenderingHints.class, RenderingHintsReadHandler.class);
        addManualMapping(String.class, StringReadHandler.class);
    }

    /** 
     * Returns the object factory. 
     * 
     * @return The object factory.
     */
    public abstract ObjectFactory getFactoryLoader();

    /**
     * Adds a mapping between a class and the handler for the class.
     *
     * @param classToRead  the class.
     * @param handler  the handler class.
     */
    protected void addManualMapping(final Class classToRead, final Class handler) {
        if (handler == null) {
            throw new NullPointerException("handler must not be null.");
        }
        if (classToRead == null) {
            throw new NullPointerException("classToRead must not be null.");
        }
        if (!XmlReadHandler.class.isAssignableFrom(handler)) {
            throw new IllegalArgumentException("The given handler is no XmlReadHandler.");
        }
        this.classToHandlerMapping.addManualMapping
            (new ManualMappingDefinition(classToRead, handler.getName(), null));
    }

    /**
     * Adds a multiplex mapping.
     * 
     * @param baseClass  the base class.
     * @param typeAttr  the type attribute.
     * @param mdef  the mapping entry.
     */
    protected void addMultiplexMapping(final Class baseClass,
                                       final String typeAttr,
                                       final MultiplexMappingEntry[] mdef) {
        
        this.classToHandlerMapping.addMultiplexMapping(
            new MultiplexMappingDefinition(baseClass, typeAttr, mdef)
        );
    }

    /**
     * Adds an object to the registry.
     * 
     * @param key  the key.
     * @param value  the object.
     */
    public void setHelperObject(final String key, final Object value) {
        if (value == null) {
            this.objectRegistry.remove(key);
        }
        else {
            this.objectRegistry.put(key, value);
        }
    }

    /**
     * Returns an object from the registry.
     * 
     * @param key  the key.
     * 
     * @return The object.
     */
    public Object getHelperObject(final String key) {
        return this.objectRegistry.get(key);
    }

    /**
     * Creates a SAX handler for the specified class.
     *
     * @param classToRead  the class.
     * @param tagName  the tag name.
     * @param atts  the attributes.
     *
     * @return a SAX handler.
     *
     * @throws XmlReaderException if there is a problem with the reader.
     */
    public XmlReadHandler createHandler(final Class classToRead, final String tagName, final Attributes atts)
        throws XmlReaderException {

        final XmlReadHandler retval = findHandlerForClass(classToRead, atts, new ArrayList());
        if (retval == null) {
            throw new NullPointerException("Unable to find handler for class: " + classToRead);
        }
        retval.init(this, tagName);
        return retval;
    }

    /**
     * Finds a handler for the specified class.
     * 
     * @param classToRead  the class to be read.
     * @param atts  the attributes.
     * @param history  the history.
     * 
     * @return A handler for the specified class.
     * 
     * @throws XmlReaderException if there is a problem with the reader.
     */
    private XmlReadHandler findHandlerForClass(final Class classToRead, final Attributes atts,
                                               final ArrayList history)
        throws XmlReaderException {
        final ObjectFactory genericFactory = getFactoryLoader();

        if (history.contains(classToRead)) {
            throw new IllegalStateException("Circular reference detected: " + history);
        }
        history.add(classToRead);
        // check the manual mappings ...

⌨️ 快捷键说明

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