All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.identityconnectors.common.StringUtil Maven / Gradle / Ivy

/*
 * ====================
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 * 
 * Copyright 2008-2009 Sun Microsystems, Inc. All rights reserved.     
 * 
 * The contents of this file are subject to the terms of the Common Development 
 * and Distribution License("CDDL") (the "License").  You may not use this file 
 * except in compliance with the License.
 * 
 * You can obtain a copy of the License at 
 * http://IdentityConnectors.dev.java.net/legal/license.txt
 * See the License for the specific language governing permissions and limitations 
 * under the License. 
 * 
 * When distributing the Covered Code, include this CDDL Header Notice in each file
 * and include the License file at identityconnectors/legal/license.txt.
 * If applicable, add the following below this CDDL Header, with the fields 
 * enclosed by brackets [] replaced by your own identifying information: 
 * "Portions Copyrighted [year] [name of copyright owner]"
 * ====================
 */
package org.identityconnectors.common;

import java.io.ByteArrayInputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.Random;
import java.util.regex.Matcher;

public class StringUtil {

    /**
     * Never allow this to be instantiated.
     */
    private StringUtil() {
        throw new AssertionError();
    }

    /**
     * Finds the index of the first digit and starts from the index specified.
     * 
     * @param str
     *            String to search for a digit.
     * @param startidx
     *            Starting index from which to search
     * @return -1 if not found otherwise the index.
     */
    public static int indexOfDigit(String str, int startidx) {
        int ret = -1;
        if (str != null) {
            for (int i = startidx; i < str.length(); i++) {
                // get the first digit..
                if (Character.isDigit(str.charAt(i))) {
                    ret = i;
                    break;
                }
            }
        }
        return ret;
    }

    /**
     * Finds the index of the first digit.
     * 
     * @param str
     *            String to seach for a digit.
     * @return -1 if not found otherwise the index.
     */
    public static int indexOfDigit(String str) {
        return indexOfDigit(str, 0);
    }

    /**
     * Finds the index of the first non digit and starts from the index
     * specified.
     * 
     * @param str
     *            String to seach for a non digit.
     * @param startidx
     *            Starting index from which to search.
     * @return -1 if not found otherwise the index.
     */
    public static int indexOfNonDigit(String str, int startidx) {
        int ret = -1;
        if (str != null) {
            for (int i = startidx; i < str.length(); i++) {
                // get the first digit..
                if (!Character.isDigit(str.charAt(i))) {
                    ret = i;
                    break;
                }
            }
        }
        return ret;
    }

    /**
     * Finds the index of the first non digit.
     * 
     * @param str
     *            String to seach for a non digit.
     * @return -1 if not found otherwise the index.
     */
    public static int indexOfNonDigit(String str) {
        return indexOfNonDigit(str, 0);
    }

    /**
     * Return the string of digits from string.
     * 
     * @param str
     *            Source string to search.
     */
    public static String subDigitString(String str) {
        return subDigitString(str, 0);
    }

    /**
     * Return the string of digits from string.
     * 
     * @param str
     *            Source string to search.
     * @param idx
     *            Start index from which to search.
     */
    public static String subDigitString(String str, int idx) {
        String ret = null;
        int sidx = indexOfDigit(str, idx);
        if (sidx != -1) {
            int eidx = indexOfNonDigit(str, sidx);
            ret = (eidx == -1) ? str.substring(sidx) : str
                    .substring(sidx, eidx);
        }
        return ret;
    }

    /**
     * Removes the attribute from the source string and returns.
     */
    public static String stripXmlAttribute(String src, String attrName) {
        String ret = null;
        // quick exit..
        if (src == null) {
            return null;
        }
        // find the attribute and remove all occurances of it..
        final char[] QUOTE = new char[] { '\'', '"' };
        ret = src;
        while (true) {
            int start = ret.indexOf(attrName);
            // no more attributes
            if (start == -1) {
                break;
            }
            // find the end of the attribute
            int openQuote = indexOf(ret, QUOTE, start);
            // there a problem because there's no open quote..
            if (openQuote == -1) {
                break;
            }
            // look for the closed quote
            int closeQuote = indexOf(ret, QUOTE, openQuote + 1);
            if (closeQuote == -1) {
                break;
            }
            // remove the space either before or after the attribute
            if (start - 1 >= 0 && ret.charAt(start - 1) == ' ') {
                start -= 1;
            } else if (closeQuote + 1 < ret.length()
                    && ret.charAt(closeQuote + 1) == ' ') {
                closeQuote += 1;
            }
            // construct new string from parts..
            StringBuffer buf = new StringBuffer();
            buf.append(ret.substring(0, start));
            buf.append(ret.substring(closeQuote + 1));
            ret = buf.toString();
        }
        return ret;
    }

    /**
     * Removes newline characters (0x0a and 0x0d) from a string.
     */
    public static String stripNewlines(String src) {
        String dest = null;
        if (src != null) {
            StringBuffer b = new StringBuffer();
            int max = src.length();
            for (int i = 0; i < max; i++) {
                char c = src.charAt(i);
                if (c != 0x0a && c != 0x0d)
                    b.append(c);
            }
            dest = b.toString();
        }
        return dest;
    }

    /**
     * Finds the start index of the comparison string regards of case.
     * 
     * @param src
     *            String to search.
     * @param cmp
     *            Comparsion string to find.
     * @return -1 if not found otherwise the index of the starting character.
     */
    public static int indexOfIgnoreCase(String src, String cmp) {
        // quick check exit...
        if (src == null || cmp == null) {
            return -1;
        }
        String isrc = src.toUpperCase();
        String icmp = cmp.toUpperCase();
        return isrc.indexOf(icmp);
    }

    /**
     * Strip XML comments
     */
    public static String stripXmlComments(String src) {
        final String END_XMLCOMMENT = "-->";
        final String START_XMLCOMMENT = "