Example usage for java.text CharacterIterator DONE

List of usage examples for java.text CharacterIterator DONE

Introduction

In this page you can find the example usage for java.text CharacterIterator DONE.

Prototype

char DONE

To view the source code for java.text CharacterIterator DONE.

Click Source Link

Document

Constant that is returned when the iterator has reached either the end or the beginning of the text.

Usage

From source file:com.websystique.springmvc.youtube_api.Function.java

public String replace_all(String token) {
    StringBuilder s = new StringBuilder(token.length());
    CharacterIterator it = new StringCharacterIterator(token);
    for (char ch = it.first(); ch != CharacterIterator.DONE; ch = it.next()) {
        switch (ch) {
        case '?':
            s.append("?####");
            break;
        case '.':
            s.append(".####");
            break;
        case ';':
            s.append(";####");
            break;
        default:/*from ww w .  j ava  2 s . c o m*/
            s.append(ch);
            break;
        }
    }

    token = s.toString();
    return token;
}

From source file:stg.utils.StringUtils.java

/**
 * Private method that performs split and returns the tokens in the form of List.
 * /*from w  w  w .ja v a2s .c o m*/
 * @param text String to be tokenized.
 * @param separatorChar used as a delimiter.
 * @param escapeChar used to escape delimiter.
 * @return List.
 */
private static List<String> psplit(String text, char separatorChar, char escapeChar) {
    if (text == null) {
        return new ArrayList<String>();
    }
    if (separatorChar == escapeChar) {
        throw new IllegalArgumentException("Separator Char and Escape Char must be different.");
    }
    StringCharacterIterator iter = new StringCharacterIterator(text);
    char c = iter.first();
    ArrayList<String> list = new ArrayList<String>();
    StringBuilder sb = new StringBuilder();
    while (c != CharacterIterator.DONE) {
        if (c == escapeChar) {
            c = iter.next();
            if (c != CharacterIterator.DONE) {
                sb.append(c);
            } else {
                throw new IllegalArgumentException(
                        "Escape Character found without the character to be ignored.");
            }
        } else if (c == separatorChar) {
            list.add(sb.toString());
            sb.delete(0, sb.length());
        } else {
            sb.append(c);
        }
        c = iter.next();
    }
    if (sb.length() > -1) {
        list.add(sb.toString());
    }
    return list;
}

From source file:stg.utils.RandomStringGenerator.java

/**
 * Generates the random string as per the format.
 * // w ww  .  j av a2s  . c o m
 * @return String
 */
public String generate() {
    CharacterIterator iter = new StringCharacterIterator(format);
    char c = iter.first();
    StringBuilder sb = new StringBuilder();
    List<Character> tempLowerCaseCharsList = cloneList(lowerCaseCharsList);
    List<Character> tempUpperCaseCharsList = cloneList(upperCaseCharsList);
    List<Character> tempNumericCharsList = cloneList(numericCharsList);
    List<Character> tempSpecialCharsList = cloneList(specialCharsList);

    boolean constantStarted = false;
    while (c != CharacterIterator.DONE) {
        switch (c) {
        case ESCAPE:
            c = iter.next();
            if (c == CharacterIterator.DONE) {
                throw new IllegalArgumentException(
                        "Invalid format, escape character found without any associated character that was to be escaped.");
            }
            sb.append(c);
            break;
        case LOWER_CASE:
            if (!constantStarted) {
                switch (option) {
                case NON_UNIQUE:
                    sb.append(RandomStringUtils.random(1, toCharArray(lowerCaseCharsList)));
                    break;
                case UNIQUE_CASE_SENSITIVE:
                    sb.append(generateUniqueCharacter(c, tempLowerCaseCharsList));
                    break;
                default:
                    String str = generateUniqueCharacter(c, tempLowerCaseCharsList);
                    sb.append(str);
                    if (!tempUpperCaseCharsList.contains(Character.valueOf(str.toUpperCase().charAt(0)))) {
                        System.out.println(tempLowerCaseCharsList + " \t " + str);
                    }
                    if (!tempUpperCaseCharsList.remove(Character.valueOf(str.toUpperCase().charAt(0)))) { //remove it from the upper case char set.
                        System.out.println("Problem unable to remove " + tempUpperCaseCharsList + "\t" + str);
                    }
                    break;
                }
            } else {
                sb.append(c);
            }
            break;
        case UPPER_CASE:
            if (!constantStarted) {
                switch (option) {
                case NON_UNIQUE:
                    sb.append(RandomStringUtils.random(1, toCharArray(upperCaseCharsList)));
                    break;
                case UNIQUE_CASE_SENSITIVE:
                    sb.append(generateUniqueCharacter(c, tempUpperCaseCharsList));
                    break;
                default:
                    String str = generateUniqueCharacter(c, tempUpperCaseCharsList);
                    sb.append(str);
                    if (!tempLowerCaseCharsList.contains(Character.valueOf(str.toLowerCase().charAt(0)))) {
                        System.out.println(tempLowerCaseCharsList + " \t " + str);
                    }
                    if (!tempLowerCaseCharsList.remove(Character.valueOf(str.toLowerCase().charAt(0)))) {
                        System.out.println("Problem unable to remove " + tempLowerCaseCharsList + "\t" + str);
                    }
                    break;
                }
            } else {
                sb.append(c);
            }
            break;
        case NUMBER:
            if (!constantStarted) {
                switch (option) {
                case NON_UNIQUE:
                    sb.append(RandomStringUtils.random(1, toCharArray(numericCharsList)));
                    break;
                default:
                    sb.append(generateUniqueCharacter(c, tempNumericCharsList));
                    break;
                }
            } else {
                sb.append(c);
            }
            break;
        case SPECIAL:
            if (!constantStarted) {
                switch (option) {
                case NON_UNIQUE:
                    sb.append(RandomStringUtils.random(1, toCharArray(specialCharsList)));
                    break;
                default:
                    sb.append(generateUniqueCharacter(c, tempSpecialCharsList));
                    break;
                }
            } else {
                sb.append(c);
            }
            break;
        case START_CONSTANT:
            if (constantStarted) {
                throw new IllegalArgumentException("Special { character found without an escape character");
            }
            if (!constantStarted)
                constantStarted = true;
            break;
        case END_CONSTANT:
            if (!constantStarted)
                throw new IllegalArgumentException("Special } character found without an escape character");
            if (constantStarted)
                constantStarted = false;
            break;
        default:
            sb.append(c);
        }
        c = iter.next();
    }
    return sb.toString();
}

From source file:com.itude.mobile.android.util.StringUtil.java

/**
 * Strip a {@link String} by removing HTML elements
 * //from  w  ww  .  j  a  v a2 s .  c o m
 * @param textToStrip {@link String} to strip
 * @return stripped {@link String}
 */
public static String stripHTMLTags(String textToStrip) {

    StringBuffer returnText = new StringBuffer(textToStrip.length());

    CharacterIterator iterator = new StringCharacterIterator(textToStrip);

    boolean finished = true;
    boolean started = false;
    for (char ch = iterator.first(); ch != CharacterIterator.DONE; ch = iterator.next()) {
        if (ch == '<') {
            started = true;
        } else if (ch == '>') {
            started = false;
            finished = true;
        } else if (finished && !started) {
            returnText.append(ch);
        }

    }

    return returnText.toString().trim();
}

From source file:stg.pr.engine.startstop.CStartEngine.java

/**
 * Command builder./*w  w  w. j  av  a  2  s.  com*/
 * 
 * @param extraCommands
 *            if any
 * @param args
 *            Program arguments for PRE.
 * @return Command
 */
public String[] buildCommand(ArrayList<String> extraCommands, String[] args) {
    long lCurrentTimestamp = System.currentTimeMillis();
    ArrayList<String> commandBuilder = new ArrayList<String>();
    if (logger_.isEnabledFor(LogLevel.FINER)) {
        logger_.log(LogLevel.FINER, "Building command.");
    }
    commandBuilder.add(System.getProperty("java.home") + FILE_SEPARATOR + "bin" + FILE_SEPARATOR + "java");
    if (CSettings.get("pr.javaruntimevmargs", null) != null) {
        // following code is to handle the space delimiter within double
        // quotes. Example value for this property
        // can be =-Xms128M -Xmx128M -D.pre.home="/u0201/apps/stg pre"
        StringCharacterIterator sci = new java.text.StringCharacterIterator(
                CSettings.get("pr.javaruntimevmargs"));
        boolean bEscapeCharacter = false;
        boolean bQuoted = false;
        StringBuffer cmdBuffer = new StringBuffer();
        for (char c = sci.first(); c != CharacterIterator.DONE; c = sci.next()) {
            switch (c) {
            case '\\':
                if (bEscapeCharacter) {
                    cmdBuffer.append(c + "" + c);
                    bEscapeCharacter = false;
                } else {
                    bEscapeCharacter = true;
                }
                break;
            case ' ':
                if (!bQuoted) {
                    commandBuilder.add(cmdBuffer.toString());
                    cmdBuffer.delete(0, cmdBuffer.length());
                } else {
                    cmdBuffer.append(c);
                }
                bEscapeCharacter = false;
                break;
            case '"':
                if (!bEscapeCharacter) {
                    if (!bQuoted) {
                        bQuoted = true;
                    } else {
                        bQuoted = false;
                    }
                }
                bEscapeCharacter = false;
                break;
            default:
                cmdBuffer.append(c);
                break;
            } // end of switch case.
        } // end for string character iterator
        if (cmdBuffer.length() > 0) {
            commandBuilder.add(cmdBuffer.toString());
        }
    } // pr.javaruntimevmarg != null
    if (extraCommands != null) {
        if (logger_.isEnabledFor(LogLevel.FINEST)) {
            logger_.log(LogLevel.FINEST, "Adding extra commands if any.");
        }
        commandBuilder.addAll(extraCommands);
    }
    if (logger_.isEnabledFor(LogLevel.FINER)) {
        logger_.log(LogLevel.FINER, "Adding constants.");
    }
    commandBuilder.add("-classpath");
    String classpath = "";
    //        if (CSettings.get("pr.reportService","OFF").equalsIgnoreCase("ON")) {
    //           File directory = new File(CSettings.get("pr.birt.home") + "/lib");
    //           if (!directory.exists()) {
    //              throw new IllegalArgumentException("Directory is non-existant. Check property birt.home " + CSettings.getInstance().getSource("pr").getConfigFile().getAbsolutePath());
    //           }
    //           ArrayList<String> list = new ArrayList<String>();
    //           list.add(".jar");
    //           list.add(".zip");
    //           classpath = getExtraClasspath(directory, list);
    //           System.out.println(classpath);
    //        }
    if (CSettings.get("pr.javaextraclasspath", null) != null) {
        commandBuilder.add(System.getProperty("java.class.path") + File.pathSeparatorChar
                + CSettings.get("pr.javaextraclasspath") + File.pathSeparatorChar + classpath);
    } else {
        commandBuilder.add(System.getProperty("java.class.path") + File.pathSeparatorChar + classpath);
    }
    commandBuilder.add("stg.pr.engine.CProcessRequestEngine");
    commandBuilder.add(args[0]);
    commandBuilder.add(args[1]);
    String[] cmd = new String[commandBuilder.size()];
    commandBuilder.toArray(cmd);
    if (logger_.isEnabledFor(LogLevel.FINEST)) {
        logger_.log(LogLevel.FINEST, "Command " + commandBuilder);
        logger_.log(LogLevel.FINEST, "Elapsed Time taken to build command "
                + (System.currentTimeMillis() - lCurrentTimestamp) + " ms.");
    }
    return cmd;
}

From source file:HexFormat.java

/**
 * Parse a hexadecimal number, skipping leading whitespace. Does not throw
 * an exception; if no object can be parsed, index is unchanged! Hexadecimal
 * numbers may be indicated with a leading character designation of '0x'.
 * /*from w  ww . j  av a2 s  .  co  m*/
 * @param source
 *            the string to parse
 * @param status
 *            the string index to start at
 * @return The hexadecimal number as a Long object.
 * 
 * @since 1.0
 */
public Object parseObject(String source, ParsePosition status) {
    int start = status.getIndex();
    boolean success = false;
    StringBuffer buffer = new StringBuffer();
    char c, c2;
    long result;

    StringCharacterIterator iter = new StringCharacterIterator(source, start);

    for (c = iter.current(); c != CharacterIterator.DONE; c = iter.next()) {
        if (Character.isWhitespace(c)) {
            // skip whitespace
            continue;
        }
        break;
    }

    if (c == CharacterIterator.DONE) {
        return (null);
    }

    if (c == '0') {
        c2 = iter.next();

        if (c2 == CharacterIterator.DONE) {
            return (null);
        }

        if (c2 == 'x') {
            // has a leading '0x' designation, so skip over it
        } else {
            // replace the two characters
            iter.previous();
            iter.previous();
        }
    } else {
        // skip back one character
        iter.previous();
    }

    // gather valid hex digits
    for (c = iter.next(); c != CharacterIterator.DONE; c = iter.next()) {
        if (hexDigits.indexOf(c) != -1) {
            success = true;
            buffer.append(c);
        } else {
            break;
        }
    }

    if (!success) {
        // no valid hex digits
        return (null);
    }

    // convert hex to long
    if (buffer.length() > 16) {
        // larger than a long, error
        // with a buffer full of nibbles, the maximum nibbles in a
        // 64 bit number is 16 nibbles
        return (null);
    }

    // parse number
    try {
        result = Long.parseLong(buffer.toString(), 16);
    } catch (NumberFormatException e) {
        // unable to parse number
        return (null);
    }

    status.setIndex(iter.getIndex());
    return (new Long(result));
}

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  .  j a va  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:Unsigned.java

/**
 * Parse a binary number into a Number object. If up to 8 bits are parsed,
 * returns a Byte. If more than 8 and up to 16 bits are parsed, return a
 * Short. If more than 16 and up to 32 bits are parsed, return an Integer.
 * If more than 32 and up to 64 bits are parsed, return a Long.
 * //  ww  w.j av  a  2s .c o  m
 * @param text
 *            a binary number
 * @param parsePosition
 *            position to start parsing from
 * @return return an integer form of Number object if parse is successful;
 *         <CODE>null</CODE> otherwise
 * 
 * @since 1.0
 */
public Number parse(String text, ParsePosition parsePosition) {
    boolean skipWhitespace = true;
    int startIndex, bits;

    // remove whitespace
    StringCharacterIterator iter = new StringCharacterIterator(text, parsePosition.getIndex());
    for (char c = iter.current(); c != CharacterIterator.DONE; c = iter.next()) {
        if (skipWhitespace && Character.isWhitespace(c)) {
            // skip whitespace
            continue;
        }
    }
    parsePosition.setIndex(iter.getIndex());

    startIndex = parsePosition.getIndex();
    Number result = (Number) parseObject(text, parsePosition);

    if (result == null) {
        return (result);
    }

    bits = parsePosition.getIndex() - startIndex;
    if (bits <= 8) {
        result = new Byte(result.byteValue());
    } else if (bits <= 16) {
        result = new Short(result.shortValue());
    } else if (bits <= 32) {
        result = new Integer(result.intValue());
    } else if (bits <= 64) {
        result = new Long(result.longValue());
    }
    return (result);
}

From source file:org.rhq.enterprise.gui.legacy.action.resource.common.monitor.visibility.IndicatorChartsAction.java

/**
 * Find characters having special meaning <em>inside</em> HTML tags and URLs.
 * <p/>//from w  w  w  . j ava2 s.c o  m
 * <p/>
 * The special characters are : <ul> <li>< <li>> <li>" <li>' <li>\ <li>& <li>| <li>? </ul>
 * <p/>
 * <p/>
 */
private static int indexOfSpecialChars(String aTagFragment) {
    final StringCharacterIterator iterator = new StringCharacterIterator(aTagFragment);

    int i = 0;
    for (char character = iterator.current(); character != CharacterIterator.DONE; character = iterator
            .next(), i++) {
        switch (character) {
        case '<':
        case '>':
        case '\"':
        case '\'':
        case '\\':
        case '&':
        case '|':
        case '?':
            return i;
        default:
            break;
        }
    }
    return -1;
}

From source file:stg.utils.StringUtils.java

/**
 * Private method to extract the tokens as list.
 * @param text/* w  w  w.j  av  a 2 s .  com*/
 * @param separatorCharStart
 * @param separatorCharEnd
 * @param escapeChar
 * @param includeSeparators
 * @return list of strings
 */
private static List<String> pExtract(String text, final char separatorCharStart, final char separatorCharEnd,
        final char escapeChar, final boolean includeSeparators) {
    if (text == null) {
        return new ArrayList<String>();
    }
    if (separatorCharStart == separatorCharEnd) {
        return psplit(text, separatorCharStart, escapeChar);
    }
    boolean escapeAndSeparatorCharAreSame = (separatorCharStart == escapeChar);
    StringCharacterIterator iter = new StringCharacterIterator(text);
    char c = iter.first();
    ArrayList<String> list = new ArrayList<String>();
    StringBuilder sb = new StringBuilder();
    boolean toBeAppended = false;
    while (c != CharacterIterator.DONE) {
        if (c == escapeChar && !escapeAndSeparatorCharAreSame) {
            c = iter.next();
            if (c == CharacterIterator.DONE) {
                throw new IllegalArgumentException(
                        "Escape Character found without the character to be ignored.");
            }
        } else if (c == separatorCharEnd) {
            if (toBeAppended) {
                if (includeSeparators) {
                    sb.append(c);
                }
                list.add(sb.toString());
                sb.delete(0, sb.length());
                toBeAppended = false;
            }
        } else if (c == separatorCharStart) {
            c = iter.next();
            if (c == CharacterIterator.DONE) {
                throw new IllegalArgumentException(
                        "Separator Character Start found without any enclosing text.");
            }
            toBeAppended = true;
            if (c == separatorCharStart && escapeAndSeparatorCharAreSame) {
                toBeAppended = false;
                c = iter.next();
            }
            if (toBeAppended && includeSeparators) {
                sb.append(separatorCharStart);
            }
        }
        if (toBeAppended) {
            sb.append(c);
        }
        c = iter.next();
    }
    return list;
}