com.silverwrist.dynamo.util.IDUtils.java Source code

Java tutorial

Introduction

Here is the source code for com.silverwrist.dynamo.util.IDUtils.java

Source

/*
 * The contents of this file are subject to the Mozilla 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 at <http://www.mozilla.org/MPL/>.
 * 
 * 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
 * language governing rights and limitations under the License.
 * 
 * The Original Code is the Venice Web Communities System.
 * 
 * The Initial Developer of the Original Code is Eric J. Bowersox <erbo@silcom.com>,
 * for Silverwrist Design Studios.  Portions created by Eric J. Bowersox are
 * Copyright (C) 2002 Eric J. Bowersox/Silverwrist Design Studios.  All Rights Reserved.
 * 
 * Contributor(s): 
 */
package com.silverwrist.dynamo.util;

import javax.mail.internet.InternetAddress;
import javax.mail.internet.AddressException;
import org.apache.commons.lang.CharSet;
import org.apache.commons.lang.CharSetUtils;

public class IDUtils {
    /*--------------------------------------------------------------------------------
     * Static data members
     *--------------------------------------------------------------------------------
     */

    private static final String[] ID_CHARS = { "A-Z", "a-z", "0-9", "-", "_~*'$" };

    private static final CharSet s_id_chars;

    /*--------------------------------------------------------------------------------
     * Constructor
     *--------------------------------------------------------------------------------
     */

    public IDUtils() { // do nothing
    } // end constructor

    /*--------------------------------------------------------------------------------
     * External static operations
     *--------------------------------------------------------------------------------
     */

    public static final boolean isValidDynamoID(String s) {
        if ((s == null) || (s.length() == 0))
            return false; // null values and null strings are NEVER valid
        for (int i = 0; i < s.length(); i++) { // test each character in turn
            if (!(s_id_chars.contains(s.charAt(i))))
                return false; // found a bogus character

        } // end for

        return true; // all tests passed - ship it!

    } // end isValidDynamoID

    public static final boolean isValidDynamoID(String s, int max_len) {
        if ((s == null) || (s.length() == 0) || (s.length() > max_len))
            return false; // null values and null strings are NEVER valid
        for (int i = 0; i < s.length(); i++) { // test each character in turn
            if (!(s_id_chars.contains(s.charAt(i))))
                return false; // found a bogus character

        } // end for

        return true; // all tests passed - ship it!

    } // end isValidDynamoID

    public static final boolean isValidDynamoIDChar(char c) {
        return s_id_chars.contains(c);

    } // end isValidDynamoIDChar

    public static boolean isValidEmailAddress(String addr) {
        try { // take advantage of the JavaMail address parser
            InternetAddress tmp = new InternetAddress(addr);

        } // end try
        catch (AddressException e) { // if we get an AddressException, it's not valid, is it?
            return false;

        } // end catch

        return true; // if get here, it's valid

    } // end isValidEmailAddress

    /*--------------------------------------------------------------------------------
     * Static initializer
     *--------------------------------------------------------------------------------
     */

    static { // evaluate the character sets
        s_id_chars = CharSetUtils.evaluateSet(ID_CHARS);

    } // end static initializer

} // end class IDUtils