ValidationUtil.java :  » J2EE » enhydra-IDE-plugin » org » enhydra » kelp » common » Java Open Source

Java Open Source » J2EE » enhydra IDE plugin 
enhydra IDE plugin » org » enhydra » kelp » common » ValidationUtil.java
/*
 * Enhydra Java Application Server Project
 *
 * The contents of this file are subject to the Enhydra Public License
 * Version 1.1 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License on
 * the Enhydra web site ( http://www.enhydra.org/ ).
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
 * the License for the specific terms governing rights and limitations
 * under the License.
 *
 * The Initial Developer of the Enhydra Application Server is Lutris
 * Technologies, Inc. The Enhydra Application Server and portions created
 * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
 * All Rights Reserved.
 *
 * Contributor(s):
 *
 */
package org.enhydra.kelp.common;
import java.io.File;
import java.util.StringTokenizer;
//
public class ValidationUtil {

    /**
     * Hidden constructor
     */
    private ValidationUtil() {}

    /**
     * Test to see if the given package is a valid java package name.
     *
     * @param pack
     * The package name to test
     *
     * @return
     * True if the specified package name is valid.
     */
    public static boolean isJavaPackage(String pack) {
        boolean         valid = true;
        StringTokenizer tokenizer = null;

        if (pack == null) {
            pack = new String();
        }
        pack = pack.trim();
        if (pack.length() == 0) {
            valid = false;
        } else if (pack.charAt(0) == '.') {
            valid = false;
        } else if (pack.charAt(pack.length() - 1) == '.') {
            valid = false;
        } else {
            // string not to be resourced
            final String DELIM = "."; // nores
            tokenizer = new StringTokenizer(pack, DELIM);
            while (tokenizer.hasMoreTokens()) {
                if (!ValidationUtil.isJavaIdentifier(tokenizer.nextToken())) {
                    valid = false;
                    break;
                }
            }
        }
        return valid;
    }

    /**
     * Test to see if the given indentifier is a valid java identifier.
     *
     * @param ident
     * The identifier to test.
     *
     * @return
     * True if the specified identifier is valid.
     */
    public static boolean isJavaIdentifier(String ident) {
        boolean valid = true;

        for (int i = 0; i < ident.length(); i++) {
            char ch = ident.charAt(i);

            if (i == 0) {
                if (!Character.isJavaIdentifierStart(ch)) {
                    valid = false;
                    break;
                }
            } else if (!Character.isJavaIdentifierPart(ch)) {
                valid = false;
                break;
            }
        }
        return valid;
    }

    /**
     * Test to see if a path is a directory.
     *
     * @param path
     * The path to test.
     *
     * @return
     * True if the specified path is a directory.
     */
    public static boolean isDirectory(String path) {
        File file = new File(path);

        return ValidationUtil.isDirectory(file);
    }

    public static boolean isDirectory(File file) {
        boolean valid = false;

        if (file != null) {
            valid = file.isDirectory();
        }
        return valid;
    }

    /**
     * Test to see if the parent of a given path is a valid directory.
     *
     * @param path
     * The path to test.
     *
     * @return
     * True if the parent of the specified path is a directory.
     */
    public static boolean isParentDirectory(String path) {
        boolean valid = false;
        File    file = new File(path);

        if (file != null && file.getParentFile() != null) {
            valid = file.getParentFile().isDirectory();
        }
        return valid;
    }

}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.