Differences between revisions 3 and 4
Revision 3 as of 2006-05-02 17:32:21
Size: 4683
Editor: DonaldMcLean
Comment:
Revision 4 as of 2009-09-20 22:11:46
Size: 4683
Editor: localhost
Comment: converted to 1.6 markup
No differences found!

Database Utilites source for DatabaseManager

/*
 *  Copyright (c) 2006, Donald McLean
 *
 *  This software is provided 'as-is', without any express or implied warranty.
 *  In no event will the authors be held liable for any damages arising from the
 *  use of this software.
 *
 *  Permission is granted to anyone to use this software for any purpose, including
 *  commercial applications, and to alter it and redistribute it freely, subject to
 *  the following restrictions:
 *
 *     1. The origin of this software must not be misrepresented; you must not claim
 *        that you wrote the original software. If you use this software in a product,
 *        an acknowledgment in the product documentation would be appreciated but is
 *        not required.
 *
 *    2. Altered source versions must be plainly marked as such, and must not be
 *       misrepresented as being the original software.
 *
 *    3. This notice may not be removed or altered from any source distribution.
 *
 */

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;
    }
}

DatabaseUtilitiesSource (last edited 2009-09-20 22:11:46 by localhost)