Example usage for java.text StringCharacterIterator first

List of usage examples for java.text StringCharacterIterator first

Introduction

In this page you can find the example usage for java.text StringCharacterIterator first.

Prototype

public char first() 

Source Link

Document

Implements CharacterIterator.first() for String.

Usage

From source file:com.alfaariss.oa.util.web.RemoteAddrFilter.java

private static boolean matchWildcard(String s, String sMask) {
    //check empty string
    if (s.length() == 0) {
        if (sMask.length() == 0 || sMask.equals("*") || sMask.equals("?"))
            return true;
        return false;
    }// ww  w  .  j  a  va2s .com

    char ch;
    int i = 0;
    StringCharacterIterator iter = new StringCharacterIterator(sMask);

    for (ch = iter.first(); ch != StringCharacterIterator.DONE && i < s.length(); ch = iter.next()) {
        if (ch == '?')
            i++;
        else if (ch == '*') {
            int j = iter.getIndex() + 1;
            if (j >= sMask.length())
                return true;
            String sSubFilter = sMask.substring(j);
            while (i < s.length()) {
                if (matchWildcard(s.substring(i), sSubFilter))
                    return true;
                i++;
            }
            return false;
        } else if (ch == s.charAt(i)) {
            i++;
        } else
            return false;
    }
    return (i == s.length());
}

From source file:Characters.java

/**
 * Removes all characters that are not considered <a href="http://www.w3.org/TR/2000/REC-xml-20001006#charsets">XML
 * characters </a> from the input.
 * //from   w  w w .java2s. c  om
 * @param input the input to filter
 * @return the input with all non-XML characters removed
 */
public static String filterXML(String input) {
    // Null inputs return an empty string.
    if (input == null) {
        return "";
    }

    // trim() removes all whitespace, not only spaces.
    String _input = input.trim();

    // Empty inputs return an empty string.
    if (_input.equals((""))) {
        return "";
    }

    // This StringBuilder will hold the result.
    StringBuilder result = new StringBuilder(_input.length());

    // This StringCharacterIterator will iterate over the input.
    StringCharacterIterator iterator = new StringCharacterIterator(_input);

    // Iterate over the input.
    for (char aChar = iterator.first(); aChar != CharacterIterator.DONE; aChar = iterator.next()) {
        // Determine if this is a valid XML Character.
        if ((aChar == '\u0009') || (aChar == '\n') || (aChar == '\r')
                || (('\u0020' <= aChar) && (aChar <= '\uD7FF'))
                || (('\uE000' <= aChar) && (aChar <= '\uFFFD'))) {
            result.append(aChar);
        }
    }
    if (result.length() > _input.length()) {
        return result.toString();
    }
    // (otherwise...)
    return _input;
}

From source file:edu.harvard.i2b2.analysis.security.HighEncryption.java

public static String leftPad(String stringToPad, int size, String padder) {
    if (padder.length() == 0) {
        return stringToPad;
    }/*  ww  w. ja v a 2 s. co m*/
    StringBuffer strb = new StringBuffer(size);
    StringCharacterIterator sci = new StringCharacterIterator(padder);

    while (strb.length() < (size - stringToPad.length())) {
        for (char ch = sci.first(); ch != CharacterIterator.DONE; ch = sci.next()) {
            if (strb.length() < size - stringToPad.length()) {
                strb.insert(strb.length(), String.valueOf(ch));
            }
        }
    }
    return strb.append(stringToPad).toString();
}

From source file:edu.umd.cfar.lamp.viper.util.StringHelp.java

/**
 * Convert the plain text string into something that
 * is a valid HTML/XML text string (i.e. escaping angle brackets,
 * etc.).//  w w w.j  a va  2  s.  com
 * I owe a lot to the apache project for their source code
 * was an assistance. It is annoying that they don't have
 * this as a public method somewhere, though.
 * @param str the string to escape 
 * @return the string, with some characters converted to XML character entity references
 */
public static String webify(String str) {
    StringBuffer buff = new StringBuffer();
    StringCharacterIterator iter = new StringCharacterIterator(str);

    for (char c = iter.first(); c != CharacterIterator.DONE; c = iter.next()) {
        switch (c) {
        case '<':
            buff.append("&lt;");
            break;

        case '>':
            buff.append("&gt;");
            break;

        case '"':
            buff.append("&quot;");
            break;

        case '\'':
            buff.append("&apos;");
            break;

        case '&':
            buff.append("&amp;");
            break;

        default:
            if ((c >= ' ' && c < 0xF7) || c == '\n' || c == '\r' || c == '\t') {
                buff.append(c);
            } else {
                buff.append("&#x").append(Integer.toHexString(c)).append(';');
            }
        }
    }
    return buff.toString();
}

From source file:com.topsec.tsm.sim.report.util.ReportUiUtil.java

/**
 * sanitize HTML?TAG? "&" --> "&amp" "<" --> "&lt" ">" --> "&gt" "\""
 * --> "&quot" "\r\n" --> "<br>" &amp; &#39;
 * //from   w w w  .  j  a  v a  2  s  .  c o  m
 * @param str
 *            strin?
 * @return String ??
 * @version 20040810
 */

public static String sanitize(String s) {
    if (s == null)
        return null;
    StringBuffer stringbuffer = new StringBuffer();
    StringCharacterIterator stringcharacteriterator = new StringCharacterIterator(s);
    for (char c = stringcharacteriterator.first(); c != '\uFFFF'; c = stringcharacteriterator.next()) {
        String s1 = (String) sanitizeTable.get(new Character(c));
        if (s1 != null)
            stringbuffer.append(s1);
        else
            stringbuffer.append(c);
    }

    return stringbuffer.toString();
}

From source file:org.squale.welcom.outils.Util.java

public final static String formatFile(String st) {
    // Transforme les lettres accentues
    st = Util.formatAccent(st);//from  w  w  w .  j  a v a  2 s .c om

    final StringCharacterIterator iter = new StringCharacterIterator(st);
    final StringBuffer sb = new StringBuffer();

    for (char c = iter.first(); c != CharacterIterator.DONE; c = iter.next()) {
        if (!(((c >= '0') && (c <= '9')) || ((c >= 'a') && (c <= 'z'))
                || (((c >= 'A') && (c <= 'Z')) || (c == '.')))) {
            sb.append('_');
        } else {
            sb.append(c);
        }
    }

    return sb.toString();
}

From source file:org.squale.welcom.outils.Util.java

public static String formatJavaScript(String st) {
    if (st == null) {
        return "";
    }/* w ww  .j  a  v  a2  s. com*/

    st = st.replaceAll("\r\n", "\\n");
    final StringBuffer sb = new StringBuffer();
    final StringCharacterIterator iter = new StringCharacterIterator(st);

    for (char c = iter.first(); c != CharacterIterator.DONE; c = iter.next()) {
        switch (c) {
        case '\t':
            sb.append("\\t");

            break;

        case '\n':
            sb.append("\\n");

            break;

        case '\'':
            sb.append("\\&#39;");

            break;

        case '\"':
            sb.append("\\&quot;");

            break;

        case '&':
            sb.append("&amp;");

            break;

        case '>':
            sb.append("&gt;");

            break;

        case '<':
            sb.append("&lt;");

            break;

        default:
            sb.append(c);

            break;
        }
    }

    return sb.toString();
}

From source file:org.squale.welcom.outils.Util.java

public static String formatEmailList(final String st) {
    final StringBuffer sb = new StringBuffer();
    final StringCharacterIterator iter = new StringCharacterIterator(st);

    for (char c = iter.first(); c != CharacterIterator.DONE; c = iter.next()) {
        if (c != ' ') {
            sb.append(c);/*from  ww w  .ja  v a2s. com*/
        }
    }

    return sb.toString();
}

From source file:org.talend.dataprep.schema.xls.XlsUtils.java

/**
 * return the column number from a cell reference (AA242)
 *
 * @param lastCell//from w ww .  j  av  a 2s . com
 * @return
 */
public static int getColumnNumberFromCellRef(String lastCell) {

    StringBuilder letters = new StringBuilder();
    // get all letters to remove row number
    StringCharacterIterator iter = new StringCharacterIterator(lastCell);
    for (char c = iter.first(); c != StringCharacterIterator.DONE; c = iter.next()) {
        if (!NumberUtils.isNumber(String.valueOf(c))) {
            letters.append(c);
        }
    }
    // use poi api to calculate column number from an excell column format
    return CellReference.convertColStringToIndex(letters.toString());

}

From source file:org.talend.dataprep.transformation.actions.math.ExtractNumber.java

/**
 * @param value the value to parse.// w w w  .  ja va 2  s. co  m
 * @param defaultValue the value to return when no number can be extracted
 * @return the number extracted out of the given value.
 */
protected static String extractNumber(String value, String defaultValue) {

    // easy case
    if (StringUtils.isEmpty(value)) {
        return defaultValue;
    }

    // Test if the input value is a valid number before removing any characters:
    if (NumericHelper.isBigDecimal(value)) {
        // If yes (no exception thrown), return the value as it, no change required:
        return String.valueOf(BigDecimalParser.toBigDecimal(value));
    }

    StringCharacterIterator iter = new StringCharacterIterator(value);

    MetricPrefix metricPrefixBefore = null, metricPrefixAfter = null;

    boolean numberFound = false;

    // we build a new value including only number or separator as , or .
    StringBuilder reducedValue = new StringBuilder(value.length());

    for (char c = iter.first(); c != CharacterIterator.DONE; c = iter.next()) {
        // we remove all non numeric characters but keep separators
        if (NumberUtils.isNumber(String.valueOf(c)) || SEPARATORS.contains(c)) {
            reducedValue.append(c);
            numberFound = true;
        } else {
            // we take the first metric prefix found before and after a number found
            if (metricPrefixBefore == null) {
                MetricPrefix found = METRICPREFIXES.get(String.valueOf(c));
                if (found != null && !numberFound) {
                    metricPrefixBefore = found;
                }
            }
            if (metricPrefixAfter == null) {
                MetricPrefix found = METRICPREFIXES.get(String.valueOf(c));
                if (found != null && numberFound) {
                    metricPrefixAfter = found;
                }
            }

        }
    }

    if (!NumericHelper.isBigDecimal(reducedValue.toString())) {
        return defaultValue;
    }
    BigDecimal bigDecimal = BigDecimalParser.toBigDecimal(reducedValue.toString());

    if (metricPrefixBefore != null || metricPrefixAfter != null) {
        // the metrix found after use first
        MetricPrefix metricPrefix = metricPrefixAfter != null ? metricPrefixAfter : metricPrefixBefore;
        bigDecimal = bigDecimal.multiply(metricPrefix.getMultiply());
    }

    DecimalFormat decimalFormat = new DecimalFormat("0.#");
    decimalFormat.setMaximumFractionDigits(MAX_FRACTION_DIGITS_DISPLAY);
    return decimalFormat.format(bigDecimal.stripTrailingZeros());
}