Package org.ozoneDB.tools.OPP

Source Code of org.ozoneDB.tools.OPP.CDHelper

// You can redistribute this software and/or modify it under the terms of
// the Ozone Library 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: CDHelper.java,v 1.2 2002/04/17 09:29:39 per_nyfelt Exp $

package org.ozoneDB.tools.OPP;

import java.io.*;
import org.ozoneDB.DxLib.*;
import org.ozoneDB.*;
import org.xml.sax.InputSource;
import org.exolab.castor.xml.Marshaller;
import org.exolab.castor.xml.Unmarshaller;
import org.ozoneDB.tools.OPP.castor.*;


/**
* @author <a href="http://www.softwarebuero.de/">SMB</a>
* @author <a href="http://www.medium.net/">Medium.net</a>
* @version $Revision: 1.2 $Date: 2002/04/17 09:29:39 $
*/
public class CDHelper {
   
    public final static String DEFAULTLOCK = "READ";
    public final static String WRITELOCK = "WRITE";
    public final static String UPGRADELOCK = "UPGRADE";
   
    protected Class cl;
   
    protected boolean quiet;
   
    protected String outputDir;
   
   
    public CDHelper( Class _cl, String _outputDir, boolean _quiet ) {
        cl = _cl;
        outputDir = _outputDir;
        quiet = _quiet;
    }
   
   
    /**
     * Try to load the XML class descriptor to...
     */
    public void searchUpdateMethods( DxHashMap updateMethodsIf ) throws Exception {
       
        //search *.ocd XML file in current and in output directory
        String filename = OPP.sourceDirName + OPPHelper.classFileBasename( cl ) + ".ocd";
       
        if (!quiet) {
            System.out.print( "      trying to process " + filename + "... " );
        }
       
        if (!new File( filename ).exists()) {
            filename = outputDir + OPPHelper.classFileBasename( cl ) + ".ocd";
            if (!new File( filename ).exists()) {
                if (!quiet) {
                    System.out.println( "(-)." );                   
                }
                return;
            }
        }
       
        if (!quiet) {
            System.out.println( "(" + filename + ")" );
        }
       
        OzoneClassDescriptor descriptor = xml2Descriptor( filename );

        if (!descriptor.getName().equals( OPPHelper.classFileBasename( cl ) )) {
            throw new Exception( "Descriptor name does not match class name." );
        }
       
        Methods mths = descriptor.getMethods();
        PublicMethod[] m = mths.getPublicMethod();
        for (int i = 0; i < m.length; i++) {
            String methodName = m[i].getName();
            String locklevel = m[i].getLocklevel();
           
            boolean isUpdate = locklevel.equals( WRITELOCK ) || locklevel.equals( UPGRADELOCK );
           
            if (isUpdate && updateMethodsIf.contains( methodName )) {
                System.out.println( OPPHelper.classFileBasename( cl ) + ".ocd" + ":0: warning: All '" + methodName
                        + "' methods will be marked as update methods." );
            }
           
            if (isUpdate) {
                updateMethodsIf.addForKey( methodName, methodName );
            }
        }
    }
   
   
    protected static void showDescriptor( OzoneClassDescriptor descriptor ) {
        System.out.println( "The OzoneClassDescriptor:" );
        System.out.println( "Name:       " + descriptor.getName() );
        System.out.println( "Desc:       " + descriptor.getDescription() );
        System.out.println( "Package:    " + descriptor.getPackage() );
        System.out.println( "Superclass: " + descriptor.getSuperclass() );
        System.out.println( "Interfaces: " );
        String[] _I = descriptor.getInterface();
        if (_I != null) {
            for (int i = 0, l = _I.length; i < l; ++i) {
                System.out.println( "            " + _I[i] );
            }
        }
       
        System.out.println( "Constructors: " );
        Constructors ctors = descriptor.getConstructors();
        PublicConstructor[] ctor = ctors.getPublicConstructor();
        for (int i = 0, l = ctor.length; i < l; ++i) {
            System.out.println( " - parameters: " + ctor[i].getParameter() );
            if (ctor[i].getDescription() != null) {
                System.out.println( "         desc: " + ctor[i].getDescription() );
            }
        }
       
        System.out.println( "Methods: " );
        Methods mths = descriptor.getMethods();
        PublicMethod[] m = mths.getPublicMethod();
        for (int i = 0, l = m.length; i < l; ++i) {
            System.out.println( " - name:       " + m[i].getName() );
            System.out.println( "   parameters: " + m[i].getParameter() );
            System.out.println( "   locklevel : " + m[i].getLocklevel() );
            if (m[i].getDescription() != null) {
                System.out.println( "         desc: " + m[i].getDescription() );
            }
        }
   
    }
   
   
    public static OzoneClassDescriptor xml2Descriptor( String _sourcefile ) throws Exception {
        return xml2Descriptor( new InputSource( _sourcefile ) );
    }
   
   
    public static OzoneClassDescriptor xml2Descriptor( InputSource _source ) throws Exception {
        Unmarshaller umrs = new Unmarshaller( OzoneClassDescriptor.class );
        //umrs.setLogWriter( logger );
        return (OzoneClassDescriptor)umrs.unmarshal( _source );
    }
   
   
    /**
     * Build OzoneClassDescriptor for the given class.
     */
    public static void class2xml( Class _clazz, PrintWriter _pw, boolean quiet ) throws Exception {
       
        OzoneClassDescriptor result = new OzoneClassDescriptor();
        String classname = _clazz.getName();
        int rcClassname = classname.lastIndexOf( "." );
       
        result.setName( classname.substring( rcClassname + 1 ) );
        if (!quiet) {
            System.out.println( "analyzing class: " + classname + " ..." );
        }
       
        if (rcClassname != -1) {
            result.setPackage( classname.substring( 0, rcClassname ) );
        }
       
        Class cl = _clazz.getSuperclass();
        if (cl != null) {
            result.setSuperclass( cl.getName() );
        }
       
        Class[] interf = _clazz.getInterfaces();
        for (int i = 0, l = interf.length; i < l; ++i) {
            result.addInterface( interf[i].getName() );
        }
       
        OPPHelper.progressMsg( "  processing constructors...", quiet );
        Constructors constructors = new Constructors();
        result.setConstructors( constructors );
        java.lang.reflect.Constructor[] constr = _clazz.getConstructors();
       
        if (constr.length == 0) {
            throw new Exception( "class needs at least one public constructor!" );
        }
       
        for (int i = 0, l = constr.length; i < l; ++i) {
            PublicConstructor c = new PublicConstructor();
            constructors.addPublicConstructor( c );
           
            Class[] params = constr[i].getParameterTypes();
            for (int j = 0; j < params.length; j++) {
                c.addParameter( params[j].getName() );
            }
        }
       
        OPPHelper.progressMsg( "  processing methods...", quiet );
        Methods methods = new Methods();
        result.setMethods( methods );
       
        java.lang.reflect.Method[] meth = _clazz.getMethods();
        if (meth.length == 0) {
            throw new Exception( "class needs at least one public method!" );
        }
       
        for (int i = 0, l = meth.length; i < l; ++i) {
            String name = meth[i].getName();
           
            // filter the system methods inherited from java.lang.Object
            Class declaringClass = meth[i].getDeclaringClass();
            if (meth[i].getDeclaringClass().equals( Object.class ) || meth[i].getDeclaringClass().equals(
                    OzoneObject.class )) {
                continue;
            }
           
            PublicMethod m = new PublicMethod();
            methods.addPublicMethod( m );
           
            m.setName( name );
           
            Class[] params = meth[i].getParameterTypes();
            for (int j = 0; j < params.length; j++) {
                m.addParameter( params[j].getName() );
            }
           
            m.setLocklevel( DEFAULTLOCK );
        }
       
        // now dump as XML...
        Marshaller.marshal( result, _pw );
    }
   
    /**
     * Builds a valid signaturestring from the given classarray.
     *
     * THIS CODE IS TAKEN (and modified) FROM OPP AND SHOULD BECOME UNIQUE FOR BOTH!!!
     * (See also filtering systemmethods few lines ahead!)
     *
     *
     * @param _parameter array of classes representing the parametertypes
     * @return the signature or null if _parameter was an empty array (null is needed
     * to unset the attribute >signatur< of element xmethod)
     */
    private static String buildSignature( Class[] _parameter ) {
        if (_parameter.length == 0) {
            return null;
        }
       
        StringBuffer result = new StringBuffer( "" );
        for (int i = 0, l = _parameter.length; i < l; i++) {
            result.append( i > 0 ? "|" : "" );
            result.append( _parameter[i].getName() );
        }
        return result.toString();
    }
   
}
TOP

Related Classes of org.ozoneDB.tools.OPP.CDHelper

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.