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

📄 object2xml.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: Object2XML.java,v 1.3 2002/09/18 06:54:17 per_nyfelt Exp $

package org.ozoneDB.core.xml;

import org.ozoneDB.OzoneInterface;
import org.ozoneDB.OzoneProxy;
import org.ozoneDB.core.ObjectContainer;
import org.xml.sax.*;
import org.xml.sax.helpers.AttributesImpl;

import java.lang.reflect.*;
import java.util.Hashtable;


/**
 * This class transform an Object into XML.
 *
 * @version $Revision: 1.3 $
 * @author <a href="http://www.softwarebuero.de">SMB</a>
 */


public class Object2XML implements Consts {

    //
    // member
    //

    /**
     * ObjCache saves the references of the processed objects.
     */
    private Hashtable objCache = new Hashtable();

    /**
     * This flag decides if transient member will be serialized or not.
     */
    private boolean serializeTransientFlag = false;

    /**
     * The SAX handler for the XML content.
     */
    private ContentHandler ch;

    /**
     * Factory for the attributes of the first obj-Tag.
     */
    private ObjAttsFactory oaf;


    //
    // construcor
    //


    /**
     * @param contHandler
     */
    public Object2XML( ContentHandler contHandler ) {
        ch = contHandler;
        oaf = new OzoneObjAttsFactory();
    }


    /**
     * @param contHandler
     * @param serializeTransientFlag
     */
    public Object2XML( ContentHandler contHandler, boolean serializeTransientFlag ) {
        this( contHandler );
        this.serializeTransientFlag = serializeTransientFlag;
    }

    //
    // methods
    //


    /**
     * ToXML(obj) gets an object and serialize this object into XML.
     *
     * @param obj (the Object)
     */
    public void toXML( Object obj ) throws SAXException {
       // ch.startDocument();
        writeObjStartTag( obj.getClass().getName(), getID( obj ), oaf.additionallyAtts( obj ) );
        objCache.put( getID( obj ), getID( obj ) );

        getMember( obj, obj.getClass() );

        writeObjEndTag();
       // ch.endDocument();
    }


    /**
     * GetMember gets the informations (name/type/value)
     * of all members (private and protected too) !!
     *
     * @param obj (the Object)
     * @param objClass (the Class of the Object)
     */
    protected void getMember( Object obj, Class objClass ) throws SAXException {
       // System.out.println( "getMember(): " + objClass.getName() );

        Field[] fds = objClass.getDeclaredFields();

        AccessibleObject.setAccessible( fds, true );
        for (int i = 0; i < fds.length; i++) {
            Field fd = fds[i];

            // avoid converting the container of a database object
            if (ObjectContainer.class.isAssignableFrom( fd.getType() )) {
                continue;
            }
            if (OzoneInterface.class.isAssignableFrom( fd.getType() )) {
                continue;
            }
            if (Modifier.isStatic( fd.getModifiers() )) {
                continue;
            }
            if (!serializeTransientFlag && Modifier.isTransient( fd.getModifiers() )) {
                continue;
            }

            String memberName = fd.getName();

            try {

                Object value = fd.get( obj );
                if (value instanceof org.ozoneDB.OzoneProxy)
                    handleOzoneProxyMember( memberName, (OzoneProxy)value);
                else {
                    writeMemberStartTag( memberName );
                    getValue( value , fd.getType() );
                }

                writeMemberEndTag();

            } catch (IllegalAccessException iae) {
                System.err.println( "(getMember) EXCEPTION: " + iae );
            }
        }

        superClass( obj, objClass );
    }


    /**
     * SuperClass gets the informations about the Superclass of the Object.
     *
     * @param obj (the Object)
     * @param objClass (the Class of the Object)
     */
    private void superClass( Object obj, Class objClass ) throws SAXException {

        Class superClass = objClass.getSuperclass();
        if (superClass == null) {
            return;
        }

        writeSuperClStartTag( superClass.getName() );

        getMember( obj, superClass );
        writeSuperClEndTag();
    }

    /**
     * GetValue gets the value of the certain member.
     *
     * @param value (value of the member)
     * @param valueType (the Class of the certain member)
     */
    protected void getValue( Object value, Class valueType ) throws SAXException {
        if (value == null) {
            return;
        }

        if (objCache.containsKey( getID( value ) )) {
            writeObjRefElement( getID( value ) );
            return;
        }

        objCache.put( getID( value ), getID( value ) );

        if (valueType.isPrimitive()) {
            writeValueStartTag( valueType.getName(), getID( value ) );
            writeValue( value.toString() );
            writeValueEndTag();
            return;
        }

        if (   value instanceof Boolean || value instanceof Byte  || value instanceof Character
            || value instanceof Double  || value instanceof Float || value instanceof Long
            || value instanceof Integer || value instanceof Short || value instanceof String) {

            writeValueStartTag( value.getClass().getName(), getID( value ) );
            writeValue( value.toString() );
            writeValueEndTag();
            return;
        }


        if (valueType.isArray()) {
            int length = Array.getLength( value );
            Class subType = value.getClass().getComponentType();
            writeArrayStartTag( valueType.getName(), getID( value ) );
            for (int l = 0; l < length; l++) {

                Object subValue = Array.get( value, l );
                if(subValue == null)
                    continue;

                if (subType.isPrimitive()) {
                    getValue( subValue , subType );
                } else {
                    getValue( subValue , subValue.getClass());
                }
            }
            writeArrayEndTag();

        } else {
            writeValueObjStartTag( value.getClass().getName(), getID( value ) );

            getMember( value, value.getClass() );
            writeValueObjEndTag();
        }
    }

    /**
     * This methode handles an OzoneProxy member.
     *
     * @param memberName (name of the member)
     * @param proxy (the OzoneProxy object)
     */
    protected void handleOzoneProxyMember( String memberName, OzoneProxy proxy) throws SAXException {
        String proxyType = proxy.getClass().getName();
        String objectID;

        try {

            Field remoteID = proxy.getClass().getField( REMOTE_ID );
            Object valueID = remoteID.get( proxy );
            objectID = valueID.toString();

            writeMemberTagForOzoneProxy( memberName, proxyType, objectID );

        } catch (NoSuchFieldException nsfe) {
            System.err.println( "(handleOzoneProxyMember) EXCEPTION: " + nsfe );
        } catch (IllegalAccessException iae) {
            System.err.println( "(handleOzoneProxyMember) EXCEPTION: " + iae );
        }
    }

    /**
     * GetId gets the reference/address of the Object.
     *
     * @param obj (the Object)
     * @return id
     */
    protected Integer getID( Object obj ) {
        Integer id = new Integer( System.identityHashCode( obj ) );
        return id;
    }


    //
    // writeMethods
    // The writeMethods helps to write XML/SAX.
    //
    protected void writeObjStartTag( String classname, Integer id, Attributes additionallyAtts ) throws SAXException {

        AttributesImpl atts;
        if (additionallyAtts != null) {
            atts = new AttributesImpl( additionallyAtts );
        } else {
            atts = new AttributesImpl();
        }

        atts.addAttribute( "", ATTR_TYPE, ATTR_TYPE, "String", classname );
        atts.addAttribute( "", ATTR_ID, ATTR_ID, "Integer", id + "" );

        ch.startElement( "", TAG_OBJ, TAG_OBJ, atts );
    }


    protected void writeObjEndTag() throws SAXException {
        ch.endElement( "", TAG_OBJ, TAG_OBJ );
    }


    protected void writeSuperClStartTag( String classname ) throws SAXException {
        AttributesImpl atts = new AttributesImpl();
        atts.addAttribute( "", ATTR_TYPE, ATTR_TYPE, "String", classname );

        ch.startElement( "", TAG_SUPERCLASS, TAG_SUPERCLASS, atts );
    }


    protected void writeSuperClEndTag() throws SAXException {
        ch.endElement( "", TAG_SUPERCLASS, TAG_SUPERCLASS );
    }


    protected void writeMemberStartTag( String name ) throws SAXException {
        AttributesImpl atts = new AttributesImpl();
        atts.addAttribute( "", ATTR_NAME, ATTR_NAME, "String", name );

        ch.startElement( "", TAG_MEMBER, TAG_MEMBER, atts );
    }

    protected void writeMemberEndTag() throws SAXException {
        ch.endElement( "", TAG_MEMBER, TAG_MEMBER );
    }

    protected void writeMemberTagForOzoneProxy( String name, String proxyType, String objectID ) throws SAXException {
        AttributesImpl atts = new AttributesImpl();
        atts.addAttribute( "", ATTR_NAME, ATTR_NAME, "String", name );
        atts.addAttribute( "", ATTR_PROXY_TYPE, ATTR_PROXY_TYPE, "java.lang.Class", proxyType );
        atts.addAttribute( ATTR_XLINK_NAMESPACE, ATTR_XLINK_TYPE_LOCAL, ATTR_XLINK_TYPE_RAW,
                           "String", ATTR_XLINK_TYPE_VALUE );
        atts.addAttribute( ATTR_XLINK_NAMESPACE, ATTR_XLINK_HREF_LOCAL, ATTR_XLINK_HREF_RAW,
                           "long", objectID );

        ch.startElement( "", TAG_MEMBER, TAG_MEMBER, atts );
    }

    protected void writeValue( String value ) throws SAXException {
        ch.characters( value.toCharArray(), 0, value.length() );
    }


    protected void writeValueStartTag( String type, Integer id ) throws SAXException {
        AttributesImpl atts = new AttributesImpl();
        atts.addAttribute( "", ATTR_TYPE, ATTR_TYPE, "String", type );
        atts.addAttribute( "", ATTR_ID, ATTR_ID, "Integer", id + "" );

        ch.startElement( "", TAG_VALUE, TAG_VALUE, atts );
    }


    protected void writeValueEndTag() throws SAXException {
        ch.endElement( "", TAG_VALUE, TAG_VALUE );
    }


    protected void writeValueObjStartTag( String type, Integer id ) throws SAXException {
        AttributesImpl atts = new AttributesImpl();
        atts.addAttribute( "", ATTR_TYPE, ATTR_TYPE, "String", type );
        atts.addAttribute( "", ATTR_ID, ATTR_ID, "Integer", id + "" );

        ch.startElement( "", TAG_VALUEOBJ, TAG_VALUEOBJ, atts );
    }


    protected void writeValueObjEndTag() throws SAXException {
        ch.endElement( "", TAG_VALUEOBJ, TAG_VALUEOBJ );
    }


    protected void writeArrayStartTag( String type, Integer id ) throws SAXException {
        AttributesImpl atts = new AttributesImpl();
        atts.addAttribute( "", ATTR_TYPE, ATTR_TYPE, "String", type );
        atts.addAttribute( "", ATTR_ID, ATTR_ID, "Integer", id + "" );

        ch.startElement( "", TAG_VALUEARRAY, TAG_VALUEARRAY, atts );
    }


    protected void writeArrayEndTag() throws SAXException {
        ch.endElement( "", TAG_VALUEARRAY, TAG_VALUEARRAY );
    }


    protected void writeObjRefElement( Integer sourceId ) throws SAXException {
        AttributesImpl atts = new AttributesImpl();
        atts.addAttribute( "", ATTR_REF, ATTR_REF, "Integer", sourceId + "" );

        ch.startElement( "", TAG_VALUE, TAG_VALUE, atts );
        ch.endElement( "", TAG_VALUE, TAG_VALUE );
    }
}

⌨️ 快捷键说明

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