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

📄 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-2000 by SMB GmbH. All rights reserved.//// $Id: Object2XML.java,v 1.6 2000/10/28 16:55:19 daniela Exp $package org.ozoneDB.core.xml;import java.util.*;import java.io.*;import java.lang.reflect.*;import org.xml.sax.*;import org.xml.sax.helpers.*;import org.ozoneDB.core.ObjectContainer;import org.ozoneDB.OzoneInterface;/** * This class transform an Object into XML. *  * @version $Revision: 1.6 $ * @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;        /**     * ... for making SAX.     */    private ContentHandler ch;        /**     * Factory for the attributes of the first obj-Tag.     */    private ObjAttsFactory oaf;            //    // construcor    //            /**     * @param docHandler     */    public Object2XML( ContentHandler contHandler ) {        ch = contHandler;        oaf = new OzoneObjAttsFactory();    }            /**     * @param docHandler     * @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(): " + obj.getClass().getName() + ", " + 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();            writeMemberStartTag( memberName );                        try {                                Object value = fd.get( obj );                getValue( value );                        } catch (IllegalAccessException iae) {                System.err.println( "(getMember) EXCEPTION: " + iae );            }             writeMemberEndTag();        }                 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)     */    protected void getValue( Object value ) throws SAXException {        if (value == null) {            return;        }                 getValue( value, value.getClass() );    }             /**     * GetValue gets the value of the certain member.     *     * @param value (value of the member)     * @param valueType (the Class of the certainMember)     */    protected void getValue( Object value, Class valueType ) throws SAXException {        if (value == null) {            return;        }                 if (valueType.isPrimitive() || 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) {                        if (valueType.isPrimitive()) {                writeValueStartTag( valueType.getName(), getID( value ) );            } else {                if (objCache.containsKey( getID( value ) )) {                    writeObjRefElement( getID( value ) );                    return;                }                 writeValueStartTag( value.getClass().getName(), getID( value ) );                objCache.put( getID( value ), getID( value ) );            }                         writeValue( value.toString() );            writeValueEndTag();                        return;        }                         if (objCache.containsKey( getID( value ) )) {            writeObjRefElement( getID( value ) );            return;        }                         if (valueType.isArray() && value != null) {            int length = Array.getLength( value );            Class subType = value.getClass().getComponentType();            writeArrayStartTag( valueType.getName(), getID( value ) );            objCache.put( getID( value ), getID( value ) );            for (int l = 0; l < length; l++) {                Object subValue = Array.get( value, l );                                if (subType.isPrimitive()) {                    getValue( subValue, subType );                } else {                    getValue( subValue );                }             }             writeArrayEndTag();                } else {            if (value != null) {                writeValueObjStartTag( value.getClass().getName(), getID( value ) );                objCache.put( getID( value ), getID( value ) );                                getMember( value, value.getClass() );                writeValueObjEndTag();            }         }     }             /**     * 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 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 + -