001/**
002 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003 *
004 * Copyright (c) 2005 Sun Microsystems Inc. All Rights Reserved
005 *
006 * The contents of this file are subject to the terms
007 * of the Common Development and Distribution License
008 * (the License). You may not use this file except in
009 * compliance with the License.
010 *
011 * You can obtain a copy of the License at
012 * https://opensso.dev.java.net/public/CDDLv1.0.html or
013 * opensso/legal/CDDLv1.0.txt
014 * See the License for the specific language governing
015 * permission and limitations under the License.
016 *
017 * When distributing Covered Code, include this CDDL
018 * Header Notice in each file and include the License file
019 * at opensso/legal/CDDLv1.0.txt.
020 * If applicable, add the following below the CDDL Header,
021 * with the fields enclosed by brackets [] replaced by
022 * your own identifying information:
023 * "Portions Copyrighted [year] [name of copyright owner]"
024 *
025 * $Id: SetValidator.java,v 1.4 2008/06/25 05:41:48 qcheng Exp $
026 *
027 */
028
029package com.iplanet.ums.validation;
030
031import java.util.StringTokenizer;
032
033/**
034 * Validates if given value is in the set. This class implements IValidator
035 * interface. Default constructor should be used to create this object. Pass the
036 * string value to be validated and set of Stringvalues against which the value
037 * should be validated to validate function, true is returned if the value is
038 * present in the set.
039 * @supported.all.api
040 */
041public class SetValidator implements IValidator {
042
043    /**
044     * Checks if the value is in the set. Validates the string value against the
045     * rule, which is a set of elements in the form of a String, with each
046     * elements separated by comma.
047     * 
048     * <pre>
049     * Example: validate(&quot;A&quot;, &quot;A,B,C,D,F&quot;); // returns true
050     * validate(&quot;408&quot;, &quot;415,650,408,510&quot;); // returns true
051     * validate(&quot;770&quot;, &quot;415,650,408,510&quot;); // returns false
052     * validate(408, &quot;415,650,408,510&quot;); // exception
053     * </pre>
054     * 
055     * @param value Value to validate.
056     * @param set Set used by the validation, in the form of a String with
057     *            each elements separated by comma.
058     * @return true if value is in the set
059     */
060    public boolean validate(String value, String set) {
061        String[] range = stringToArray(normalize(set));
062        for (int i = 0; i < range.length; i++) {
063            if (value.equals(range[i])) {
064                return true;
065            }
066        }
067        return false;
068    }
069
070    private String[] stringToArray(String s) {
071        if ((s == null) || (s.trim().length() == 0)) {
072            return new String[0];
073        }
074
075        StringTokenizer st = new StringTokenizer(s, ",");
076        String vals[] = new String[st.countTokens()];
077        int i = 0;
078        while (st.hasMoreTokens()) {
079            vals[i++] = st.nextToken();
080        }
081        return vals;
082
083    }
084
085    private String normalize(String s) {
086        String n = "";
087        char[] array = s.toCharArray();
088        for (int i = 0; i < array.length; i++) {
089            if (!(array[i] == ' ')) {
090                n = n + array[i];
091            }
092        }
093        return n;
094    }
095
096}




























































Copyright © 2010-2017, ForgeRock All Rights Reserved.