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

📄 xml2objectcontenthandler.java

📁 Java的面向对象数据库系统的源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
// You can redistribute this software and/or modify it under the terms of
// the Ozone Core License version 1 published by ozone-db.org.
//
// The original code and portions created by SMB are
// Copyright (C) 1997-@year@ by SMB GmbH. All rights reserved.
//
// $Id: XML2ObjectContentHandler.java,v 1.4 2002/12/29 11:15:57 per_nyfelt Exp $

package org.ozoneDB.core.xml;

import org.ozoneDB.OzoneProxy;
import org.ozoneDB.OzoneClassNotFoundException;
import org.ozoneDB.core.ObjectID;
import org.ozoneDB.core.Env;
import org.xml.sax.*;

import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.util.*;
/**
 * This class handles the XML and transform it into an Object.
 *
 * @version $Revision: 1.4 $
 * @author <a href="http://www.softwarebuero.de">SMB</a>
 */
public class XML2ObjectContentHandler implements ContentHandler, Consts {

    public static final boolean	debug = false;

    //
    // member
    //

    /**
     */
    protected Locator locator;

    /**
     * Cache for the ref-elements.
     */
    protected static Hashtable objCache = new Hashtable();

    /**
     * All objs, members, values etc are saved in this stack.
     */
    protected Stack stack;

    /**
     * CH (ContentHandler) is for handling a special part of the XML
     * in a special ContentHandler. (e.g. HashtableContentHandler)
     */
    protected XML2ObjectContentHandler CH = null;

    /**
     */
    protected XML2ObjectDelegate delegate;

    //
    // construcor
    //

    /**
     */
    public XML2ObjectContentHandler() {
        CH = this;
    }

    /**
     */
    public XML2ObjectContentHandler(XML2ObjectDelegate delegate) {
        this();
        this.delegate = delegate;
    }

    //
    // methods
    //

    /**
     * The method setDocumentLocator sets the locator.
     *
     * @param locator
     */
    public void setDocumentLocator(Locator locator) {
        this.locator = locator;
    }

    /**
     * Start Document parsing.
     */
    public void startDocument() {
        if (debug) {
            System.out.println("Start parsing ..");
        }
    }

    /**
     */
    public void endDocument() {
        if (debug) {
            System.out.println("End parsing ..");
        }
    }

    /**
     * The method startElement handels all startElements.
     * It refers to the methods, which process the individual startElement
     * in detail.
     *
     * @param namespaceURI
     * @param localName
     * @param rawName (the tagname)
     * @param atts (the attributes of the tag)
     */
    public void startElement (String namespaceURI, String localName,
                              String rawName, Attributes atts) {

        if (rawName.equals(TAG_OBJ)) {
            CH.objStartElement(atts);
        } else if (rawName.equals(TAG_MEMBER)) {
            CH.memberStartElement(atts);
        } else if (rawName.equals(TAG_VALUE)) {
            CH.valueStartElement(atts);
        } else if (rawName.equals(TAG_VALUEOBJ)) {
            CH.valueObjStartElement(atts);
        } else if (rawName.equals(TAG_VALUEARRAY)) {
            CH.valueArrayStartElement(atts);
        } else if (rawName.equals(TAG_SUPERCLASS)) {
            CH.superclassStartElement(atts);
        }
    }

    /**
     * The method endElement handles all endElements.
     * It refers to the methods, which process the individual endElement
     * in detail.
     *
     * @param namespaceURI
     * @param localName
     * @param rawName (the tagname)
     */
    public void endElement (String namespaceURI, String localName,
                            String rawName) {

        if (CH.stack.size() <= 1 && rawName.equals(TAG_VALUEOBJ)) {

            Hashtable hash = (Hashtable)CH.stack.pop();

            CH = this;
            stack.push(hash);
        }

        if (rawName.equals(TAG_OBJ)) {
            CH.objEndElement();
        } else if (rawName.equals(TAG_MEMBER)) {
            CH.memberEndElement();
        } else if (rawName.equals(TAG_VALUE)) {
            CH.valueEndElement();
        } else if (rawName.equals(TAG_VALUEOBJ)) {
            CH.valueObjEndElement();
        } else if (rawName.equals(TAG_VALUEARRAY)) {
            CH.valueArrayEndElement();
        } else if (rawName.equals(TAG_SUPERCLASS)) {
            CH.superclassEndElement();
        }
    }

    /**
     * The method characters handles the text-elements.
     *
     * @param ch (char-array)
     * @param start (start of the array)
     * @param end (end of the array)
     */
    public void characters (char[] ch, int start, int end) {
        CH.values(ch, start, end);
    }

    /**
     */
    public void processingInstruction(String target, String data) {
        if (debug) {
            System.out.println("Target: " + target + " Data: " + data);
        }
    }

    /**
     */
    public void startPrefixMapping (String prefix, String uri) {
        if (debug) {
            System.out.println("Prefix: " + prefix + " Uri: " + uri);
        }
    }

    /**
     */
    public void endPrefixMapping (String prefix) {
        if (debug) {
            System.out.println("EndPrefix: " + prefix);
        }
    }

    /**
     */
    public void ignorableWhitespace (char[] ch, int start, int end) {
        if (debug) {
            String s = new String(ch,start,end);
            System.out.println("Ignor chars: "+s);
        }
    }

    /**
     */
    public void skippedEntity (String name) {
        if (debug) {
            System.out.println("Skip entity "+name);
        }
    }


    //
    // handle elements
    //

    /**
     * The method objStartElement refers to handleObjStartElement.
     * @param atts (the attributes)
     */
    protected void objStartElement(Attributes atts) {
        handleObjStartElement(atts);
    }

    /**
     * The method memberStartElement refers to handleMemberStartElement.
     * @param atts (the attributes)
     */
    protected void memberStartElement(Attributes atts) {
        handleMemberStartElement(atts);
    }

    /**
     * The method valueStartElement refers to handleValueStartElement.
     * @param atts (the attributes)
     */
    protected void valueStartElement(Attributes atts) {
        handleValueStartElement(atts);
    }

    /**
     * The method valueObjStartElement refers to handleValueObjStartElement.
     * @param atts (the attributes)
     */
    protected void valueObjStartElement(Attributes atts) {
        handleValueObjStartElement(atts);
    }

    /**
     * The method valueArrayStartElement refers to handleValueArrayStartElement.
     * @param atts (the attributes)
     */
    protected void valueArrayStartElement(Attributes atts) {
        handleValueArrayStartElement(atts);
    }

    /**
     * The method superclassStartElement refers to handleSuperclassStartElement.
     * @param atts (the attributes)
     */
    protected void superclassStartElement(Attributes atts) {
        handleSuperclassStartElement(atts);
    }

    /**
     * The method values refers to handleValues.
     *
     * @param ch (char-array)
     * @param start (start of the array)
     * @param end (end of the array)
     */
    protected void values(char[] ch, int start, int end) {
        handleValues(ch, start, end);
    }

    /**
     * The method objEndElement refers to handleObjEndElement.
     */
    protected void objEndElement() {
        handleObjEndElement();
    }

    /**
     * The method memberEndElement refers to handleMemberEndElement.
     */
    protected void memberEndElement() {
        handleMemberEndElement ();
    }

    /**
     * The method valueEndElement refers to handleValueEndElement.
     */
    protected void valueEndElement() {
        handleValueEndElement();
    }

    /**
     * The method valueObjEndElement refers to handleValueObjEndElement.
     */
    protected void valueObjEndElement() {
        handleValueObjEndElement();
    }

    /**
     * The method valueArrayEndElement refers to handleValueArrayEndElement.
     */
    protected void valueArrayEndElement() {
        handleValueArrayEndElement();
    }

    /**
     * The method superclassEndElement refers to handleSuperclassEndElement.
     */
    protected void superclassEndElement() {
        handleSuperclassEndElement();
    }


    /**
     * The method handleObjStartElement creates a new object
     * and put it in the stack.
     *
     * @param atts (the attributes)
     */
    protected void handleObjStartElement(Attributes atts) {
        ObjElement oe;

        try {

            stack = new Stack();
            oe = new ObjElement(atts);

            stack.push(oe);
            objCache.put(oe.getId(), oe.getObject());

        } catch (ClassNotFoundException cnfe)  {
            System.err.println("handleObjStartElement: " + cnfe);
        } catch (InstantiationException ie) {
            System.err.println("handleObjStartElement: " + ie);
        } catch (IllegalAccessException iae) {
            System.err.println("handleObjStartElement: " + iae);
        }
    }

    /**
     * The method handleObjEndElement gets the finished Object from the stack.
     */
    protected void handleObjEndElement() {
        ObjElement oe = (ObjElement)stack.pop();
        delegate.handleObject(oe);
    }

    /**
     * This methode handles an OzoneProxy member.
     *
     * @param atts
     */
    protected void handleOzoneProxyMember(Attributes atts) {
        String proxyType = atts.getValue(ATTR_PROXY_TYPE);
        String href = atts.getValue(ATTR_XLINK_HREF_RAW);

⌨️ 快捷键说明

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