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

📄 abstractmodelreader.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.]
 * 
 * ------------------------
 * AbstractModelReader.java
 * ------------------------
 * (C)opyright 2003-2005, by Thomas Morgner and Contributors.
 *
 * Original Author:  Thomas Morgner;
 * Contributor(s):   David Gilbert (for Object Refinery Limited);
 *
 * $Id: AbstractModelReader.java,v 1.8 2005/10/18 13:33:53 mungady Exp $
 *
 * Changes
 * -------
 * 12-Nov-2003 : Initial version
 * 25-Nov-2003 : Updated header (DG);
 *
 */

package org.jfree.xml.util;

import java.io.BufferedInputStream;
import java.io.InputStream;
import java.net.URL;
import java.util.Stack;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.jfree.util.Log;
import org.jfree.util.ObjectUtilities;
import org.jfree.xml.CommentHandler;
import org.jfree.xml.ElementDefinitionException;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;

/**
 * Loads the class model from an previously written xml file set.
 * This class provides abstract methods which get called during the parsing
 * (similiar to the SAX parsing, but slightly easier to code).
 *
 * This will need a rewrite in the future, when the structure is finished.
 */
public abstract class AbstractModelReader {

    /** The 'START' state. */
    private static final int STATE_START = 0;
    
    /** The 'IN_OBJECT' state. */
    private static final int IN_OBJECT = 1;

    /** The 'IGNORE_OBJECT' state. */
    private static final int IGNORE_OBJECT = 2;
    
    /** The 'MAPPING' state. */
    private static final int MAPPING_STATE = 3;
    
    /** The 'CONSTRUCTOR' state. */
    private static final int CONSTRUCTOR_STATE = 4;

    /**
     * The SAX2 callback implementation used for parsing the model xml files.
     */
    private class SAXModelHandler extends DefaultHandler {

        /** The resource URL. */
        private URL resource;

        /** The current state. */
        private int state;
        
        /** Open comments. */
        private Stack openComments;
        
        /** Flag to track includes. */
        private boolean isInclude;

        /**
         * Creates a new SAX handler for parsing the model.
         *
         * @param resource  the resource URL.
         * @param isInclude  an include?
         */
        public SAXModelHandler(final URL resource, final boolean isInclude) {
            if (resource == null) {
                throw new NullPointerException();
            }
            this.resource = resource;
            this.openComments = new Stack();
            this.isInclude = isInclude;
        }

        /**
         * Receive notification of the start of an element.
         *
         * @param uri The Namespace URI, or the empty string if the
         *        element has no Namespace URI or if Namespace
         *        processing is not being performed.
         * @param localName The local name (without prefix), or the
         *        empty string if Namespace processing is not being
         *        performed.
         * @param qName The qualified name (with prefix), or the
         *        empty string if qualified names are not available.
         * @param attributes The attributes attached to the element.  If
         *        there are no attributes, it shall be an empty
         *        Attributes object.
         * @exception SAXException Any SAX exception, possibly
         *            wrapping another exception.
         * 
         * @see org.xml.sax.ContentHandler#startElement
         */
        public void startElement(final String uri, final String localName,
                                 final String qName, final Attributes attributes)
            throws SAXException {

            setOpenComment(getCommentHandler().getComments());
            this.openComments.push(getOpenComment());
            setCloseComment(null);

            try {

                if (!this.isInclude && qName.equals(ClassModelTags.OBJECTS_TAG)) {
                    //Log.debug ("Open Comments: " + openComment);
                    startRootDocument();
                    return;
                }

                if (getState() == STATE_START) {
                    startRootElement(qName, attributes);
                }
                else if (getState() == IGNORE_OBJECT) {
                    return;
                }
                else if (getState() == IN_OBJECT) {
                    startObjectElement(qName, attributes);
                }
                else if (getState() == MAPPING_STATE) {
                    if (!qName.equals(ClassModelTags.TYPE_TAG)) {
                        throw new SAXException("Expected 'type' tag");
                    }
                    final String name = attributes.getValue(ClassModelTags.NAME_ATTR);
                    final String target = attributes.getValue(ClassModelTags.CLASS_ATTR);
                    handleMultiplexMapping(name, target);
                }
                else if (getState() == CONSTRUCTOR_STATE) {
                    if (!qName.equals(ClassModelTags.PARAMETER_TAG)) {
                        throw new SAXException("Expected 'parameter' tag");
                    }
                    final String parameterClass = attributes.getValue(ClassModelTags.CLASS_ATTR);
                    final String tagName = attributes.getValue(ClassModelTags.PROPERTY_ATTR); // optional
                    handleConstructorDefinition(tagName, parameterClass);
                }
            }
            catch (ObjectDescriptionException e) {
                throw new SAXException(e);
            }
            finally {
                getCommentHandler().clearComments();
            }
        }

        /**
         * Receive notification of the end of an element.
         *
         * @param uri The Namespace URI, or the empty string if the
         *        element has no Namespace URI or if Namespace
         *        processing is not being performed.
         * @param localName The local name (without prefix), or the
         *        empty string if Namespace processing is not being
         *        performed.
         * @param qName The qualified name (with prefix), or the
         *        empty string if qualified names are not available.
         * @exception SAXException Any SAX exception, possibly
         *            wrapping another exception.
         * @see org.xml.sax.ContentHandler#endElement
         */
        public void endElement(final String uri, final String localName, final String qName)
            throws SAXException {

            setOpenComment((String[]) this.openComments.pop());
            setCloseComment(getCommentHandler().getComments());

            try {
                if (!this.isInclude && qName.equals(ClassModelTags.OBJECTS_TAG)) {
                    endRootDocument();
                    return;
                }

                if (qName.equals(ClassModelTags.OBJECT_TAG)) {
                    if (getState() != IGNORE_OBJECT) {
                        endObjectDefinition();
                    }
                    setState(STATE_START);
                }
                else if (qName.equals(ClassModelTags.MAPPING_TAG)) {
                    setState(STATE_START);
                    endMultiplexMapping();
                }
                else if (qName.equals(ClassModelTags.CONSTRUCTOR_TAG)) {
                    if (getState() != IGNORE_OBJECT) {
                        setState(IN_OBJECT);
                    }
                }
            }
            catch (ObjectDescriptionException e) {
                throw new SAXException(e);
            }
            finally {
                getCommentHandler().clearComments();
            }
        }

        /**
         * Handles the start of an element within an object definition.
         *
         * @param qName The qualified name (with prefix), or the
         *        empty string if qualified names are not available.
         * @param attributes The attributes attached to the element.  If
         *        there are no attributes, it shall be an empty
         *        Attributes object.
         * @throws ObjectDescriptionException if an error occured while
         *        handling this tag
         */
        private void startObjectElement(final String qName, final Attributes attributes)
            throws ObjectDescriptionException {
            if (qName.equals(ClassModelTags.CONSTRUCTOR_TAG)) {
                setState(CONSTRUCTOR_STATE);
            }
            else if (qName.equals(ClassModelTags.LOOKUP_PROPERTY_TAG)) {
                final String name = attributes.getValue(ClassModelTags.NAME_ATTR);
                final String lookupKey = attributes.getValue(ClassModelTags.LOOKUP_ATTR);
                handleLookupDefinition(name, lookupKey);
            }
            else if (qName.equals(ClassModelTags.IGNORED_PROPERTY_TAG)) {
                final String name = attributes.getValue(ClassModelTags.NAME_ATTR);
                handleIgnoredProperty(name);
            }
            else if (qName.equals(ClassModelTags.ELEMENT_PROPERTY_TAG)) {
                final String elementAtt = attributes.getValue(ClassModelTags.ELEMENT_ATTR);
                final String name = attributes.getValue(ClassModelTags.NAME_ATTR);
                handleElementDefinition(name, elementAtt);
            }
            else if (qName.equals(ClassModelTags.ATTRIBUTE_PROPERTY_TAG)) {
                final String name = attributes.getValue(ClassModelTags.NAME_ATTR);
                final String attribName = attributes.getValue(ClassModelTags.ATTRIBUTE_ATTR);
                final String handler = attributes.getValue(ClassModelTags.ATTRIBUTE_HANDLER_ATTR);
                handleAttributeDefinition(name, attribName, handler);
            }
        }

        /**
         * Handles the include or object tag.
         *
         * @param qName The qualified name (with prefix), or the
         *        empty string if qualified names are not available.
         * @param attributes The attributes attached to the element.  If
         *        there are no attributes, it shall be an empty
         *        Attributes object.
         * @throws SAXException if an parser error occured
         * @throws ObjectDescriptionException if an object model related
         *        error occured.
         */
        private void startRootElement(final String qName, final Attributes attributes)
            throws SAXException, ObjectDescriptionException {

            if (qName.equals(ClassModelTags.INCLUDE_TAG)) {
                if (this.isInclude) {
                    Log.warn("Ignored nested include tag.");
                    return;
                }
                final String src = attributes.getValue(ClassModelTags.SOURCE_ATTR);
                try {
                    final URL url = new URL(this.resource, src);
                    startIncludeHandling(url);
                    parseXmlDocument(url, true);
                    endIncludeHandling();
                }
                catch (Exception ioe) {
                    throw new ElementDefinitionException
                        (ioe, "Unable to include file from " + src);
                }
            }
            else if (qName.equals(ClassModelTags.OBJECT_TAG)) {
                setState(IN_OBJECT);
                final String className = attributes.getValue(ClassModelTags.CLASS_ATTR);
                String register = attributes.getValue(ClassModelTags.REGISTER_NAMES_ATTR);
                if (register != null && register.length() == 0) {
                    register = null;
                }
                final boolean ignored = "true".equals(attributes.getValue(ClassModelTags.IGNORE_ATTR));
                if (!startObjectDefinition(className, register, ignored)) {
                    setState(IGNORE_OBJECT);
                }
            }
            else if (qName.equals(ClassModelTags.MANUAL_TAG)) {
                final String className = attributes.getValue(ClassModelTags.CLASS_ATTR);
                final String readHandler = attributes.getValue(ClassModelTags.READ_HANDLER_ATTR);

⌨️ 快捷键说明

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