Example usage for java.text StringCharacterIterator next

List of usage examples for java.text StringCharacterIterator next

Introduction

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

Prototype

public char next() 

Source Link

Document

Implements CharacterIterator.next() 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;
    }/*from  w  ww  .jav a 2s .  co m*/

    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.j a v  a 2 s.co m*/
 * @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:com.jwebmp.core.utilities.EscapeChars.java

/**
 * Replace characters having special meaning in regular expressions with their escaped equivalents, preceded by a '\' character.
 *
 * @param aRegexFragment// w ww  .  j  ava 2s. c om
 *
 * @return
 */
public static String forRegex(String aRegexFragment) {
    StringBuilder result = new StringBuilder();
    StringCharacterIterator iterator = new StringCharacterIterator(aRegexFragment);
    char character = iterator.current();
    while (character != CharacterIterator.DONE) {
        if (EscapeChars.EscapeRegexCharset.contains(character)) {
            result.append('\\' + character);
        } else {
            result.append(character);
        }
        character = iterator.next();
    }
    return result.toString();
}

From source file:Main.java

static String escape(String input) {
    if (input == null) {
        return null;
    }/*from w ww. j ava2 s . co  m*/
    StringBuilder result = new StringBuilder();
    StringCharacterIterator iterator = new StringCharacterIterator(input);
    char character = iterator.current();
    while (character != CharacterIterator.DONE) {
        if (character == '<') {
            result.append("&lt;");
        } else if (character == '>') {
            result.append("&gt;");
        } else if (character == '\"') {
            result.append("&quot;");
        } else if (character == '\'') {
            result.append("&#039;");
        } else if (character == '&') {
            result.append("&amp;");
        } else {
            result.append(character);
        }
        character = iterator.next();
    }
    return result.toString();
}

From source file:Main.java

public static String escape(String input) {
    if (input == null) {
        return null;
    }// w  ww.j  a va  2 s.  c o  m
    StringBuilder result = new StringBuilder();
    StringCharacterIterator iterator = new StringCharacterIterator(input);
    char character = iterator.current();
    while (character != CharacterIterator.DONE) {
        if (character == '<') {
            result.append("&lt;");
        } else if (character == '>') {
            result.append("&gt;");
        } else if (character == '\"') {
            result.append("&quot;");
        } else if (character == '\'') {
            result.append("&#039;");
        } else if (character == '&') {
            result.append("&amp;");
        } else {
            result.append(character);
        }
        character = iterator.next();
    }
    return result.toString();
}

From source file:com.jwebmp.core.utilities.EscapeChars.java

/**
 * Returns opening and closing tags replaced with the HTML Equivalents replaced by their escaped equivalents.
 *
 * @param aText//from  ww w  . ja  va 2  s.c  om
 *
 * @return
 */
public static String disableTags(String aText) {
    StringBuilder result = new StringBuilder();
    StringCharacterIterator iterator = new StringCharacterIterator(aText);
    char character = iterator.current();
    while (character != CharacterIterator.DONE) {
        switch (character) {
        case '<':
            result.append("&lt;");
            break;
        case '>':
            result.append("&gt;");
            break;
        default:
            //the char is not a special one
            //add it to the result as is
            result.append(character);
            break;
        }
        character = iterator.next();
    }
    return result.toString();
}

From source file:Main.java

public static String escapeTextForXML(String aText) {
    final StringBuilder result = new StringBuilder();
    final StringCharacterIterator iterator = new StringCharacterIterator(aText);
    char character = iterator.current();
    while (character != CharacterIterator.DONE) {
        if (character == '<') {
            result.append("&lt;");
        } else if (character == '>') {
            result.append("&gt;");
        } else if (character == '\"') {
            result.append("&quot;");
        } else if (character == '\'') {
            result.append("&#039;");
        } else if (character == '&') {
            result.append("&amp;");
        } else {/*from w  w  w. ja v  a  2s . com*/
            //the char is not a special one
            //add it to the result as is
            result.append(character);
        }
        character = iterator.next();
    }
    return result.toString();
}

From source file:Main.java

/**
 * Escape characters for text appearing as XML data, between tags.
 * <P/>/*  w  ww.j a v a  2  s  . c om*/
 * The following characters are replaced with corresponding character entities :
 * '<' to '&lt';
 * '>' to '&gt';
 * '&' to '&amp;'
 * '"' to '&quot;'
 * "'" to "&#039;"
 * <P/>
 * Note that JSTL's {@code <c:out>} escapes the exact same set of characters as this method.
 */
public static String escapeCharsForXML(String aText) {
    final StringBuilder result = new StringBuilder();
    final StringCharacterIterator iterator = new StringCharacterIterator(aText);
    char character = iterator.current();
    while (character != CharacterIterator.DONE) {
        if (character == '<') {
            result.append("&lt;");
        } else if (character == '>') {
            result.append("&gt;");
        } else if (character == '\"') {
            result.append("&quot;");
        } else if (character == '\'') {
            result.append("&#039;");
        } else if (character == '&') {
            result.append("&amp;");
        } else {
            // the char is not a special one
            // add it to the result as is
            result.append(character);
        }
        character = iterator.next();
    }
    return result.toString();
}

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;
    }//w w  w . j  a va2s .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:Main.java

public static String escapeStringForXML(String aText) {
    final StringBuilder result = new StringBuilder();
    final StringCharacterIterator iterator = new StringCharacterIterator(aText);
    char character = iterator.current();
    while (character != CharacterIterator.DONE) {
        if (character == '<') {
            result.append("&lt;");
        } else if (character == '>') {
            result.append("&gt;");
        } else if (character == '\"') {
            result.append("&quot;");
        } else if (character == '\'') {
            result.append("&#039;");
        } else if (character == '&') {
            result.append("&amp;");
        } else {/*ww  w.  j  a  v  a 2  s.c o m*/
            //the char is not a special one
            //add it to the result as is
            result.append(character);
        }
        character = iterator.next();
    }
    return result.toString();
}