You are viewing a plain text version of this content. The canonical link for it is here.
Posted to derby-commits@db.apache.org by Apache Wiki <wi...@apache.org> on 2006/04/28 22:42:14 UTC

[Db-derby Wiki] Update of "DatabaseUtilitiesSource" by DonaldMcLean

Dear Wiki user,

You have subscribed to a wiki page or wiki category on "Db-derby Wiki" for change notification.

The following page has been changed by DonaldMcLean:
http://wiki.apache.org/db-derby/DatabaseUtilitiesSource

New page:
## Please edit system and help pages ONLY in the moinmaster wiki! For more
## information, please see MoinMaster:MoinPagesEditorGroup.
##master-page:Unknown-Page
##master-date:Unknown-Date
##acl MoinPagesEditorGroup:read,write,delete,revert All:read
#format wiki
#language en
== Database Utilites source for DatabaseManager ==
{{{
package your.package.here;

import org.jdom.Document;
import org.jdom.Element;

import org.jdom.input.SAXBuilder;

import java.io.File;
import java.io.InputStream;

import java.util.ArrayList;

/**
 * Created:
 * Date: Sep 4, 2005
 * Time: 10:01:39 PM
 */
public class DatabaseUtilities
{
    public static boolean deleteAnyFile(File file)
    {
        if (!file.exists())
        {
            return true;
        }

        if (!file.isDirectory())
        {
            return file.delete();
        }

        return deleteDirectory(file);
    }

    /**
     * Loads the .xml file with the same SEEN_NAME as the class from the
     * same package as the class.
     *
     * @param interestedClass
     * @return XML document for interestedClass
     */
    public static Document getXMLResource(Class interestedClass)
    {
        String className = interestedClass.getName();
        int dotIndex = className.lastIndexOf('.') + 1;
        String resourceName = className.substring(dotIndex) + ".xml";
        return getXMLResource(resourceName, interestedClass);

    }

    public static Document getXMLResource(String resourceName, Class interestedClass)
    {
        System.out.println("[Utilities : getXMLResource]: resourceName: " +
                resourceName);
        InputStream stream = interestedClass.getResourceAsStream(resourceName);
        if (stream == null)
        {
            return null;
        }

        return loadDomDocument(stream);
    }

    /**
     * Loads and returns the JDOM Document from the given file.
     */
    private static Document loadDomDocument(InputStream iFile)
    {
        SAXBuilder builder = new SAXBuilder();
        Document lDocument = null;
        builder.setValidation(false);

        try
        {
            lDocument = builder.build(iFile);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

        return lDocument;
    } // loadDomDocument

    /**
     * @param directory - item to be deleted.
     * @return success or failure
     */
    private static boolean deleteDirectory(File directory)
    {
        File[] contents = directory.listFiles();
        for (int i = 0; i < contents.length; i++)
        {
            File next = contents[i];
            boolean result = deleteAnyFile(next);
            if (!result)
            {
                return false;
            }
        }

        return directory.delete();
    }

    static public Class getClass(Element classNameElement)
    {
        Class result = null;
        String className = classNameElement.getText();
        System.out.println("[Utilities.getClass] getting class '" +
                className + "'.");

        result = loadClass(className);

        System.out.println("[ColumnType.getClass] got '" + result + "'.");
        return result;
    }

    public static Class loadClass(String className)
    {
        Class result = null;
        try
        {
            result = Class.forName(className);
        }
        catch (ClassNotFoundException e)
        {
            System.out.println("[ColumnType.ColumnType] Unable to load class '" +
                    className + "'.");
        }
        return result;
    }
}
}}}