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:stg.pr.engine.startstop.CStartEngine.java

/**
 * Command builder.//from w  w  w . j a v  a2s.c om
 * 
 * @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:stg.utils.StringUtils.java

/**
 * Joins the given tokens and forms the string by appropriately adding the sepeartorChar and the escapeChar wherever necessary.
 * /*from   w ww  .  j  a v  a 2s. c om*/
 * Returns null if array is null or empty string if the array is empty.
 * 
 * @param array Tokens
 * @param separatorChar delimiter character
 * @param escapeChar escape character
 * @return String
 */
public static String join(String[] array, char separatorChar, char escapeChar) {
    if (array == null) {
        return null;
    }
    StringBuilder sb = new StringBuilder();
    for (String str : array) {
        StringCharacterIterator iter = new StringCharacterIterator(str);
        char c = iter.first();
        while (c != CharacterIterator.DONE) {
            if (c == separatorChar) {
                if (escapeChar != CharacterIterator.DONE) {
                    sb.append(escapeChar);
                }
            }
            sb.append(c);
            c = iter.next();
        }
        sb.append(separatorChar);
    }
    if (sb.length() > 0) {
        sb.deleteCharAt(sb.length() - 1);
    }
    return sb.toString();
}

From source file:stg.utils.StringUtils.java

/**
 * Private method that performs split and returns the tokens in the form of List.
 * /*  w  ww .  j av a2 s  .com*/
 * @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.StringUtils.java

/**
 * Private method to extract the tokens as list.
 * @param text/*from   w ww  . j av a2s. co m*/
 * @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;
}