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

📄 hashtablecontenthandler.java

📁 Java的面向对象数据库系统的源代码
💻 JAVA
字号:
// 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: HashtableContentHandler.java,v 1.3 2002/09/18 06:54:17 per_nyfelt Exp $

package org.ozoneDB.core.xml;

import org.xml.sax.Attributes;
import org.xml.sax.Locator;

import java.util.Hashtable;
import java.util.Stack;

/**
 * This class handles a special part of the XML and transform it into an Hashtable.
 *
 * @version $Revision: 1.3 $
 * @author <a href="http://www.softwarebuero.de">SMB</a>
 */
public class HashtableContentHandler extends XML2ObjectContentHandler {

    //
    // member
    //

    /**
     */
    protected Stack hashEndStack;

    /**
     */
    private int memberStartStackSize = 0;

    /**
     * Mode contains the special mode (NOMODE/KEYMODE/VALUEMODE/NEXTMODE).
     */
    private int mode = NOMODE;

    final static int NOMODE = 0;
    final static int KEYMODE = 1;
    final static int VALUEMODE = 2;
    final static int NEXTMODE = 3;

    //
    // construcor
    //

    /**
     */
    public HashtableContentHandler() {
    }

    /**
     * @param locator
     * @param hash
     */
    public HashtableContentHandler(Locator locator, Hashtable hash) {
        super();

        stack.push(hash);

        setDocumentLocator (locator);

        hashEndStack = new Stack();
        hashEndStack.push("START");

    }

    /**
     * The method memberStartElement set the mode on KEYMODE, VALUEMODE or NEXTMODE
     * if the member has the attribute name with the value "key", "value" or "next".
     * And only if mode is on KEYMODE or VALUEMODE it refers to handleMemberStartElement.
     *
     * @param atts (the attributes)
     */
    protected void memberStartElement(Attributes atts) {

        if ((atts.getValue("name").equals("key") ||
             atts.getValue("name").equals("value") ||
             atts.getValue("name").equals("next")) && mode != NOMODE) {

            System.out.println("Object Hashtable has member with name key/value/next !");
        }

        if (atts.getValue("name").equals("key")) {
            mode = KEYMODE;
            memberStartStackSize = stack.size();
        }

        if (atts.getValue("name").equals("value")) {
            mode = VALUEMODE;
            memberStartStackSize = stack.size();
        }

        if (atts.getValue("name").equals("next")) {
            mode = NEXTMODE;
            memberStartStackSize = stack.size();
        }

        if (mode != KEYMODE && mode != VALUEMODE) {
            stack.push("Member " + atts.getValue("name"));
            return;
        }

        handleMemberStartElement(atts);
    }

    /**
     * This methode handles the memberEndElement.
     *
     */
    protected void memberEndElement() {

        if (mode == NEXTMODE) {
            stack.pop(); // Member next

            Object value = stack.pop();
            Object key = stack.pop();

            Hashtable hash = (Hashtable)stack.elementAt(0);
            hash.put (key, value);

            stack.remove(0);
            stack.add(0, hash);

            mode = NOMODE;
            memberStartStackSize = 0;
            return;
        }

        int currentStackSize = stack.size();
        if ((mode == KEYMODE || mode == VALUEMODE) && (currentStackSize-2) == memberStartStackSize) {

            Object value = stack.pop();

            stack.pop();
            stack.push(value);
            mode = NOMODE;
            return;
        }

		if ((mode == KEYMODE || mode == VALUEMODE) && (currentStackSize-2) != memberStartStackSize) {

            handleMemberEndElement();

		} else
			stack.pop();
    }

    /**
     * The method valueStartElement refers to handleValueStartElement
     * if mode is on KEYMODE or VALUEMODE.
     *
     * @param atts (the attributes)
     */
    protected void valueStartElement(Attributes atts) {

        if (mode != KEYMODE && mode != VALUEMODE) {
            stack.push("Value");
            return;
        }

        handleValueStartElement(atts);
    }

    /**
     * The method valueEndElement refers to handleValueEndElement
     * if mode is on KEYMODE or VALUEMODE.
     */
    protected void valueEndElement() {
        if (mode != KEYMODE && mode != VALUEMODE) {
            stack.pop();
            return;
        }

        handleValueEndElement();
    }

    /**
     * The method values refers to handleValues if mode is on KEYMODE or VALUEMODE.
     *
     * @param ch (char-array)
     * @param start (start of the array)
     * @param end (end of the array)
     */
    public void values (char[] ch, int start, int end) {
        if (mode != KEYMODE && mode != VALUEMODE)
            return;

        handleValues(ch, start, end);
    }

    /**
     * The method valueObjStartElement refers to handleValueObjStartElement
     * if mode is on KEYMODE or VALUEMODE.
     *
     * @param atts (the attributes)
     */
    protected void valueObjStartElement(Attributes atts) {
        if (mode != KEYMODE && mode != VALUEMODE) {
            stack.push("ValueObj");
            return;
        }

        handleValueObjStartElement(atts);
    }

    /**
     * The method valueObjEndElement refers to handleValueObjEndElement
     * if mode is on KEYMODE or VALUEMODE.
     */
    protected void valueObjEndElement() {
        if (mode != KEYMODE && mode != VALUEMODE) {
            stack.pop();
            return;
        }

        handleValueObjEndElement();
    }


    /**
     * The method valueArrayStartElement refers to handleValueArrayStartElement
     * if mode is on KEYMODE or VALUEMODE.
     *
     * @param atts (the attributes)
     */
    protected void valueArrayStartElement(Attributes atts) {
        if (mode != KEYMODE && mode != VALUEMODE) {
            stack.push("ValueArray");
            return;
        }

        handleValueArrayStartElement(atts);
    }

    /**
     * The method valueArrayEndElement refers to handleValueArrayEndElement
     * if mode is on KEYMODE or VALUEMODE.
     */
    protected void valueArrayEndElement() {

        if (mode != KEYMODE && mode != VALUEMODE) {
            stack.pop();
            return;
        }

        handleValueArrayEndElement();
    }

    /**
     * This method returns the stack.
     *
     * @return Stack
     */
    public Stack getStack() {
        return stack;
    }

}

⌨️ 快捷键说明

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