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

📄 xml2objectcontenthandler.java

📁 Java的面向对象数据库系统的源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:

        ObjectID objID = new ObjectID((new Long(href)).longValue());

        try {

            Class proxyClass = Thread.currentThread().getContextClassLoader().loadClass(proxyType);
            OzoneProxy proxy = (OzoneProxy)proxyClass.newInstance();

            Field remoteID = proxyClass.getField( REMOTE_ID );
            remoteID.set(proxy, objID);

            ValueObjElement voe = new ValueObjElement(proxy);
            stack.push(voe);

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

    /**
     * The method handleMemberStartElement creates a MemberElement
     * and put it the stack.
     *
     * @param atts (the attributes)
     */
    protected void handleMemberStartElement(Attributes atts) {
        MemberElement me = new MemberElement(atts);
        stack.push(me);

        if (atts.getValue(ATTR_PROXY_TYPE) != null) //member is an OzoneProxy
            handleOzoneProxyMember(atts);
    }

    /**
     * The method handleMemberEndElement gets the finished MemberElement and the value
     * from the stack and put it in the object.
     */
    protected void handleMemberEndElement() {
        Object value = null;

        if (stack.peek() instanceof ValueObjElement)
            value = ((ValueObjElement)stack.pop()).getObject();
        else if (stack.peek() instanceof MemberElement)
            value = null;
        else
            value = stack.pop();

        if (stack.peek() instanceof MemberElement) {

            MemberElement me = (MemberElement)stack.pop();

            Object obj = stack.peek();
            Class objClass = obj.getClass();

            if (stack.peek() instanceof ObjElement) {
                obj = ((ObjElement)stack.peek()).getObject();
                objClass = obj.getClass();
            } else if (stack.peek() instanceof ValueObjElement) {
                obj = ((ValueObjElement)stack.peek()).getObject();
                objClass = obj.getClass();
            } else if (stack.peek() instanceof SuperclassElement) {
                obj = ((SuperclassElement)stack.peek()).getObject();
                objClass = ((SuperclassElement)stack.peek()).getSuperclass();
            }

            try {

                Field fd = objClass.getDeclaredField(me.getName());
                fd.setAccessible(true);

                fd.set(obj, value);

            } catch (NoSuchFieldException nsfe) { // wrong Class
                System.err.println("handleMemberEndElement: " + nsfe);
            } catch (IllegalAccessException iae) { //no access for this member
                System.err.println("handleMemberEndElement: " + iae);
            }
        }
    }

    /**
     * The method handleValueStartElement creates a ValueElement
     * and put it the stack.
     *
     * @param atts (the attributes)
     */
    protected void handleValueStartElement(Attributes atts) {
        String ref = atts.getValue(ATTR_REF);
        if (ref != null) {
            Object refObj = objCache.get(ref);
            stack.push(new RefElement(refObj));
            return;
        }

        ValueElement ve = new ValueElement(atts);

        stack.push(ve);
    }

    /**
     * The method handleValues gets the ValueElement from the stack.
     * All values in the ValueElement are saved as String.
     *
     * @param ch (char-array)
     * @param start (start of the array)
     * @param end (end of the array)
     */
    public void handleValues (char[] ch, int start, int end) {
        if (stack == null || stack.empty())
            return;

        if ((stack.peek() instanceof ValueElement)) {
            String value = new String(ch,start,end);
            ValueElement ve = (ValueElement)stack.pop();

            if (ve.getStrValue() != null)           // if ValueElement.value has already a value,
                value = ve.getStrValue() + value;   // then append (new)value to (old)value

            ve.setStrValue(value);

            stack.push(ve);
        }
    }

    /**
     * The method handleValueEndElement gets the ValueElement from the stack.
     * It casts the value (as String) in the special type and put the real
     * value in the stack back.
     */
    protected void handleValueEndElement() {
        Object value;

        if (stack.peek() instanceof RefElement) {
            value = ((RefElement)(stack.pop())).getRefObj();

        } else {
            ValueElement ve = (ValueElement) stack.pop();

            if (ve.getStrValue() == null) value = null;
            else {
                value = castValue (ve.getType(), ve.getStrValue());
                objCache.put(ve.getId(), value);
            }
        }

        stack.push(value);
    }

    /**
     * The method castValues casts the valueString into the real type.
     *
     * @param type (the type)
     * @param valueString (start of the array)
     */
    protected Object castValue(String type, String valueString) {

        if ((type.equals("java.lang.Boolean")) ||
            (type.equals("boolean"))) {
            Boolean value = new Boolean(valueString);
            return value;
        }

        if ((type.equals("java.lang.Byte")) ||
            (type.equals("byte"))) {
            Byte value = new Byte(valueString);
            return value;
        }

        if ((type.equals("java.lang.Character")) ||
            (type.equals("char"))) {
            Character value = new Character(valueString.charAt(0));
            return value;
        }

        if ((type.equals("java.lang.Short")) ||
            (type.equals("short"))) {
            Short value = new Short(valueString);
            return value;
        }

        if ((type.equals("java.lang.Integer")) ||
            (type.equals("int"))) {
            Integer value = new Integer(valueString);
            return value;
        }

        if ((type.equals("java.lang.Long")) ||
            (type.equals("long"))) {
            Long value = new Long(valueString);
            return value;
        }

        if ((type.equals("java.lang.Float")) ||
            (type.equals("float"))) {
            Float value = new Float(valueString);
            return value;
        }

        if ((type.equals("java.lang.Double")) ||
            (type.equals("double"))) {
            Double value = new Double(valueString);
            return value;
        }

        if (type.equals("java.lang.String")) {
            String value = new String(valueString);
            return value;
        }

        return null;
    }

    /**
     * The method handleValueObjStartElement creates a ValueObjElement
     * and put it in the stack.
     * If the valueObj is from type java.util.Hashtable:
     * -> new ContentHandler which creates a Hashtable
     *    (It is because of can not creating innerClasses!!)
     *
     * @param atts (the attributes)
     */
    protected void handleValueObjStartElement(Attributes atts) {
        String ref = atts.getValue("ref");
        if (ref != null) {
            Object refObj = objCache.get(ref);
            stack.push(refObj);
            return;
        }

        try {

            ValueObjElement voe = new ValueObjElement(atts);

            if (voe.getType().equals("java.util.Hashtable")) {
                System.out.println("new HashContentHandler");
                CH = new HashtableContentHandler(locator, (Hashtable)voe.getObject());
            } else {
                stack.push(voe);
                objCache.put(voe.getId(), voe.getObject());
            }

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

    /**
     */
    protected void handleValueObjEndElement() {
    }

    /**
     * The method handleValueArrayStartElement creates a ValueArrayElement
     * and put it in the stack.
     *
     * @param atts (the attributes)
     */
    protected void handleValueArrayStartElement(Attributes atts) {
        String ref = atts.getValue("ref");
        if (ref != null) {
            Object refObj = objCache.get(ref);
            stack.push(new RefElement(refObj));
            return;
        }

        ValueArrayElement vae = new ValueArrayElement(atts);
        stack.push(vae);
    }

    /**
     * The method handleValueArrayEndElement joins the values to an array.
     */
    protected void handleValueArrayEndElement() {
        Object array;
        if (stack.peek() instanceof RefElement) {
            array = ((RefElement)(stack.pop())).getRefObj();
            stack.push(array);
            return;
        }

        try {
            Vector vaValues = new Vector();
            while (!(stack.peek() instanceof ValueArrayElement)) {
                vaValues.addElement(stack.pop());
            }

            ValueArrayElement vae = (ValueArrayElement) stack.pop();

            // todo: uncommented until verified that it works
            //Class vaClass = Class.forName(vae.getType()).getComponentType();
            Class vaClass = Env.currentEnv().classManager.classForName(vae.getType()).getComponentType();

            array = Array.newInstance(vaClass, vaValues.size());

            int k = vaValues.size()-1; // k-- otherwise -> wrong sequence
            for (int j = 0; j < vaValues.size(); j++, k--) {
                Array.set(array, j, vaValues.elementAt(k));
            }

            objCache.put(vae.getId(), array);
            stack.push(array);

        } catch (OzoneClassNotFoundException cnfe)  {
            System.err.println("handleValueArrayStartElement: " + cnfe);
        }
    }

    /**
     * The method handleSuperclassStartElement creates a SuperclassElement
     * and put into the stack.
     *
     * @param atts (the attributes)
     */
    protected void handleSuperclassStartElement(Attributes atts) {
        try {
            Object obj = stack.peek();

            if (stack.peek() instanceof ObjElement) {
                obj = ((ObjElement)stack.peek()).getObject();
            } else if (stack.peek() instanceof ValueObjElement) {
                obj = ((ValueObjElement)stack.peek()).getObject();
            } else if (stack.peek() instanceof SuperclassElement) {
                obj = ((SuperclassElement)stack.peek()).getObject();
            }

            SuperclassElement sce = new SuperclassElement(obj, atts);
            stack.push(sce);
        } catch (ClassNotFoundException cnfe)  {
            System.err.println("handleSuperclassStartElement: " + cnfe);
        }
    }

    /**
     * The method handleSuperclassEndElement pop the superclass in the stack.
     */
    protected void handleSuperclassEndElement() {
        stack.pop();
    }


// ------------ TEST -------------
// -- System.out.println(STACK) --
// -------------------------------
    protected void showStack(Stack s) {
        try {
            System.out.println("------------------------------ (start)");
            for (int i=s.size()-1; i>=0; i--) {
                System.out.print("  " + i+". " + s.elementAt(i));
                System.out.println();
            }
            System.out.println("------------------------------ (end)");
        } catch (Exception e) {
                System.err.println("showStack: " + e);
        }
    }

}

⌨️ 快捷键说明

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