Example usage for java.lang StringBuffer deleteCharAt

List of usage examples for java.lang StringBuffer deleteCharAt

Introduction

In this page you can find the example usage for java.lang StringBuffer deleteCharAt.

Prototype

@Override
public synchronized StringBuffer deleteCharAt(int index) 

Source Link

Usage

From source file:com.edgenius.wiki.render.MarkupUtil.java

/**
 * Markup token has odd number leading slash "\", then convert this markup to <em>HTML entity</em>.
 * @param input//  w ww .  j  a  v  a  2 s.c om
 * @return
 */
public static String escapeMarkupToEntity(String input) {
    if (input == null)
        return null;
    int len = input.length();
    StringBuffer sb = new StringBuffer();
    int slash = 0;
    int currLen;
    boolean odd;
    for (int idx = 0; idx < len; idx++) {
        char c = input.charAt(idx);
        if (c == '\\') {
            slash++;
            sb.append(c);
            continue;
        }

        odd = processDoubleSlash(sb, slash);

        //even ">" is not filter pattern keyword, but it use in markup link.
        // For example[view has \\> char>link],here must escape \> to entity, then in LinkFilter could correctly convert \&#(int >); 
        // to  ">", as it will call unescapeMarkupLink() to remove another "\" 
        if (StringUtils.contains(FilterRegxConstants.FILTER_KEYWORD + ">", c) && odd) {
            currLen = sb.length();
            sb.deleteCharAt(currLen - 1);
            //delete last slash
            sb.append(EscapeUtil.toEntity(c));
        } else
            sb.append(c);

        slash = 0;
    }

    //string end by some amount "\"
    if (slash > 0) {
        odd = processDoubleSlash(sb, slash);
        //TDB:  how to handle last single "\"  to entity?
        if (odd) {
            currLen = sb.length();
            sb.deleteCharAt(currLen - 1);
            //delete last slash
            sb.append(ENTITY_SLASH);
        }
    }
    return sb.toString();
}

From source file:com.fantasia.snakerflow.engine.SnakerHelper.java

public static String getPathJson(List<TransitionModel> tms) {
    StringBuffer buffer = new StringBuffer();
    buffer.append("paths:{");
    for (TransitionModel tm : tms) {
        buffer.append(tm.getName());//from  ww  w.  ja  v a  2  s  .c o m
        buffer.append(":{from:'");
        buffer.append(tm.getSource().getName());
        buffer.append("',to:'");
        buffer.append(tm.getTarget().getName());
        buffer.append("', dots:[");
        if (StringUtils.isNotEmpty(tm.getG())) {
            String[] bendpoints = tm.getG().split(";");
            for (String bendpoint : bendpoints) {
                buffer.append("{");
                String[] xy = bendpoint.split(",");
                buffer.append("x:").append(getNumber(xy[0]));
                buffer.append(",y:").append(xy[1]);
                buffer.append("},");
            }
            buffer.deleteCharAt(buffer.length() - 1);
        }
        buffer.append("],text:{text:'");
        buffer.append(tm.getDisplayName());
        buffer.append("'},textPos:{");
        if (StringUtils.isNotEmpty(tm.getOffset())) {
            String[] values = tm.getOffset().split(",");
            buffer.append("x:").append(values[0]).append(",");
            buffer.append("y:").append(values[1]).append("");
        }
        buffer.append("}, props:{name:{value:'" + tm.getName() + "'},expr:{value:'" + tm.getExpr() + "'}}}");
        buffer.append(",");
    }
    buffer.deleteCharAt(buffer.length() - 1);
    buffer.append("},");
    return buffer.toString();
}

From source file:edu.mayo.informatics.lexgrid.convert.directConversions.UmlsCommon.LoadRRFToDB.java

private static String[] createAndLoadTables(URI rrfLocation, boolean skipNonLexGridFiles,
        boolean recalcRootOnly, String dbServer, String dbDriver, String username, String password,
        LgMessageDirectorIF md, boolean validateMode) throws Exception {
    md.info("Connecting to RRF Files");
    BufferedReader reader = getReader(rrfLocation.resolve("MRFILES.RRF"));

    md.info("Connecting to db Files");
    Connection sqlConnection = DBUtility.connectToDatabase(dbServer, dbDriver, username, password);

    GenericSQLModifier gsm = new GenericSQLModifier(sqlConnection);

    Hashtable columnTypeMap = readMRCols(rrfLocation);
    Hashtable tableColumnMap = new Hashtable();

    List tables = new ArrayList();

    if (skipNonLexGridFiles) {
        // the only tables that I need to load
        tables.add("MRCONSO");
        tables.add("MRDOC");
        tables.add("MRREL");
        tables.add("MRSAB");
        tables.add("MRRANK");

        if (!recalcRootOnly) {
            tables.add("MRDEF");
            tables.add("MRSTY");
            tables.add("MRSAT");
            tables.add("MRHIER");
        }//from   ww w.  j  a  va2  s. c  o m
    }

    md.info("Creating SQL database tables");

    PreparedStatement create = null;
    PreparedStatement drop = null;
    String line = reader.readLine();

    int mrhierHCDCol = -1;
    while (line != null) {
        String[] vals = stringToArray(line, '|');

        // for MRFILES, all I care about is the following
        String file = vals[0];
        String tableName = file.substring(0, file.indexOf('.'));

        // if file is MRHIER, remember HCD column number (base 0)
        if ("MRHIER".equalsIgnoreCase(tableName) && vals.length > 1) {
            mrhierHCDCol = Arrays.asList(vals[2].split(",")).indexOf("HCD");
        }

        if (skipNonLexGridFiles || recalcRootOnly) {
            if (!tables.contains(tableName)) {
                line = reader.readLine();
                continue;
            }
        } else {
            if (file.indexOf('/') != -1) {
                // skip files in subfolders.
                line = reader.readLine();
                continue;
            }
            if (!tables.contains(tableName))
                tables.add(tableName);
        }

        String[] columns = stringToArray(vals[2], ',');

        tableColumnMap.put(file, columns);

        StringBuffer tableCreateSQL = new StringBuffer();
        tableCreateSQL.append("CREATE TABLE {IF NOT EXISTS} ^" + tableName + "^ (");

        for (int i = 0; i < columns.length; i++) {

            tableCreateSQL.append(" ^" + columns[i] + "^ "
                    + mapUMLSType((String) columnTypeMap.get(columns[i] + "|" + file)) + " default NULL,");
        }

        // chop the last comma
        tableCreateSQL.deleteCharAt(tableCreateSQL.length() - 1);
        tableCreateSQL.append(") {TYPE}");

        // make sure the table doesn't exist
        try {
            drop = sqlConnection.prepareStatement(gsm.modifySQL("DROP TABLE " + tableName + " {CASCADE}"));
            drop.executeUpdate();
            drop.close();
        } catch (SQLException e) {
            // most likely means that the table didn't exist.
        }

        create = sqlConnection.prepareStatement(gsm.modifySQL(tableCreateSQL.toString()));
        create.executeUpdate();

        create.close();

        line = reader.readLine();
    }
    reader.close();

    md.info("Creating indexes");

    PreparedStatement createIndex = null;

    createIndex = sqlConnection
            .prepareStatement(gsm.modifySQL("CREATE INDEX ^mi1^ ON ^MRCONSO^ (^CUI^, ^SAB^)"));
    createIndex.executeUpdate();
    createIndex.close();

    createIndex = sqlConnection
            .prepareStatement(gsm.modifySQL("CREATE INDEX ^mi2^ ON ^MRCONSO^ (^CUI^, ^AUI^)"));
    createIndex.executeUpdate();
    createIndex.close();

    createIndex = sqlConnection
            .prepareStatement(gsm.modifySQL("CREATE INDEX ^mi3^ ON ^MRCONSO^ (^AUI^, ^CODE^)"));
    createIndex.executeUpdate();
    createIndex.close();

    createIndex = sqlConnection.prepareStatement(gsm.modifySQL("CREATE INDEX ^mi4^ ON ^MRREL^ (^RELA^)"));
    createIndex.executeUpdate();
    createIndex.close();

    createIndex = sqlConnection.prepareStatement(gsm.modifySQL("CREATE INDEX ^mi5^ ON ^MRREL^ (^REL^)"));
    createIndex.executeUpdate();
    createIndex.close();

    createIndex = sqlConnection.prepareStatement(gsm.modifySQL("CREATE INDEX ^mi6^ ON ^MRREL^ (^RUI^)"));
    createIndex.executeUpdate();
    createIndex.close();

    createIndex = sqlConnection
            .prepareStatement(gsm.modifySQL("CREATE INDEX ^mi7^ ON ^MRREL^ (^SAB^, ^RELA^)"));
    createIndex.executeUpdate();
    createIndex.close();

    createIndex = sqlConnection.prepareStatement(gsm.modifySQL("CREATE INDEX ^mi8^ ON ^MRSAB^ (^RSAB^)"));
    createIndex.executeUpdate();
    createIndex.close();

    createIndex = sqlConnection.prepareStatement(gsm.modifySQL("CREATE INDEX ^mi9^ ON ^MRRANK^ (^SAB^)"));
    createIndex.executeUpdate();
    createIndex.close();

    createIndex = sqlConnection.prepareStatement(gsm.modifySQL("CREATE INDEX ^mi10^ ON ^MRRANK^ (^TTY^)"));
    createIndex.executeUpdate();
    createIndex.close();

    if (!recalcRootOnly) {
        createIndex = sqlConnection
                .prepareStatement(gsm.modifySQL("CREATE INDEX ^mi11^ ON ^MRDEF^ (^CUI^, ^SAB^)"));
        createIndex.executeUpdate();
        createIndex.close();

        createIndex = sqlConnection
                .prepareStatement(gsm.modifySQL("CREATE INDEX ^mi12^ ON ^MRSAT^ (^METAUI^)"));
        createIndex.executeUpdate();
        createIndex.close();

        createIndex = sqlConnection
                .prepareStatement(gsm.modifySQL("CREATE INDEX ^mi13^ ON ^MRSAT^ (^CUI^, ^SAB^)"));
        createIndex.executeUpdate();
        createIndex.close();

        createIndex = sqlConnection
                .prepareStatement(gsm.modifySQL("CREATE INDEX ^mi14^ ON ^MRSAT^ (^CODE^, ^SAB^)"));
        createIndex.executeUpdate();
        createIndex.close();

        createIndex = sqlConnection.prepareStatement(gsm.modifySQL("CREATE INDEX ^mi15^ ON ^MRSTY^ (^CUI^)"));
        createIndex.executeUpdate();
        createIndex.close();

        createIndex = sqlConnection.prepareStatement(
                gsm.modifySQL("CREATE INDEX ^mi16^ ON ^MRHIER^ (^CUI^, ^AUI^, ^HCD^, ^SAB^, ^CXN^)"));
        createIndex.executeUpdate();
        createIndex.close();

        createIndex = sqlConnection
                .prepareStatement(gsm.modifySQL("CREATE INDEX ^mi17^ ON ^MRHIER^ (^CUI^, ^SAB^, ^CXN^)"));
        createIndex.executeUpdate();
        createIndex.close();
    }

    PreparedStatement insert = null;

    Iterator allTables = tables.iterator();
    Set rootCUIs = new HashSet();
    while (allTables.hasNext()) {
        System.gc();
        String table = (String) allTables.next();
        md.info("Loading " + table);

        boolean loadingMrHier = table.equalsIgnoreCase("MRHIER");

        StringBuffer insertSQL = new StringBuffer();

        insertSQL.append("INSERT INTO " + table + " (");

        String[] vals = (String[]) tableColumnMap.get(table + ".RRF");
        for (int i = 0; i < vals.length; i++) {
            if (gsm.getDatabaseType().equals("ACCESS") && vals[i].equals("VALUE")) {
                // reserved word in MSAccess
                insertSQL.append("\"" + vals[i] + "\", ");
            } else {
                insertSQL.append(vals[i] + ", ");
            }
        }

        // chop the last comma and space
        insertSQL.deleteCharAt(insertSQL.length() - 2);
        insertSQL.append(") VALUES (");

        for (int i = 0; i < vals.length; i++) {
            insertSQL.append("?, ");
        }

        // chop the last comma and space
        insertSQL.deleteCharAt(insertSQL.length() - 2);
        insertSQL.append(")");
        insert = sqlConnection.prepareStatement(gsm.modifySQL(insertSQL.toString()));

        URI tableURI = rrfLocation.resolve(table + ".RRF");

        if (verifyTableExists(tableURI)) {
            try {
                reader = getReader(tableURI);

                int count = 1;
                line = reader.readLine();
                boolean restrictToRootCUIs = recalcRootOnly && table.equalsIgnoreCase("MRCONSO");
                boolean restrictToRootRels = recalcRootOnly && table.equalsIgnoreCase("MRREL");
                while (line != null && line.length() > 0) {
                    // Note: If we are only using the data to recalculate
                    // root nodes,
                    // we only need CUIs defining root hierarchical terms
                    // and any related
                    // relationships.
                    if (restrictToRootCUIs && !line.contains("|SRC|RHT|")) {
                        line = reader.readLine();
                        continue;
                    }
                    String[] data = stringToArray(line, '|');

                    // If processing MRHIER, we only care about entries
                    // relevant to
                    // the specified MRHIER processing option. Many entries
                    // in this file
                    // we do not require since they can be derived from
                    // MRREL.
                    // MRHIER typically is much larger since it pre-computes
                    // the entire
                    // hierarchy, so we want to conserve time and space by
                    // loading only
                    // those entries that require special handling.
                    if (loadingMrHier && mrhierHCDCol > 0 && data.length > mrhierHCDCol
                            && StringUtils.isBlank(data[mrhierHCDCol])) {
                        line = reader.readLine();
                        continue;
                    }

                    if (restrictToRootCUIs && data.length >= 1)
                        rootCUIs.add(data[0]);
                    if (restrictToRootRels && (data.length < 5
                            || (!rootCUIs.contains(data[0]) && !rootCUIs.contains(data[4])))) {
                        line = reader.readLine();
                        continue;
                    }

                    for (int i = 0; i < vals.length; i++) {
                        insert.setString(i + 1, data[i]);
                    }
                    insert.executeUpdate();

                    count++;
                    line = reader.readLine();

                    if (validateMode && count > 100) {
                        line = null;
                    }

                    if (count % 10000 == 0) {
                        md.busy();
                    }

                    if (count % 100000 == 0) {
                        md.info("Loaded " + count + " into " + table);
                    }
                }
                reader.close();
            } catch (Exception e) {
                md.fatalAndThrowException("problem loading the table " + table, e);
            }

        } else {
            md.warn("Could not load table " + table + ". This" + "most likely means the corresponding RRF file"
                    + "was not found in the source.");
        }

        insert.close();
        System.gc();
    }

    sqlConnection.close();
    return (String[]) tables.toArray(new String[tables.size()]);
}

From source file:com.liferay.util.Http.java

public static String parameterMapToString(Map parameterMap, boolean addQuestion) {

    StringBuffer sb = new StringBuffer();

    if (parameterMap.size() > 0) {
        if (addQuestion) {
            sb.append(StringPool.QUESTION);
        }/*from w  w  w  .  j a  v  a  2s  . c o  m*/

        Iterator itr = parameterMap.entrySet().iterator();

        while (itr.hasNext()) {
            Map.Entry entry = (Map.Entry) itr.next();

            String name = (String) entry.getKey();
            String[] values = (String[]) entry.getValue();

            for (int i = 0; i < values.length; i++) {
                sb.append(name);
                sb.append(StringPool.EQUAL);
                sb.append(Http.encodeURL(values[i]));
                sb.append(StringPool.AMPERSAND);
            }
        }

        sb.deleteCharAt(sb.length() - 1);
    }

    return sb.toString();
}

From source file:org.talend.designer.hdfsbrowse.manager.HadoopServerUtil.java

public static String extractGroups(HDFSConnectionBean connection, ClassLoader classLoader)
        throws HadoopServerException {
    StringBuffer groupBuf = new StringBuffer();
    try {/*www  .  ja v  a2  s  .  com*/
        Object ugi = ReflectionUtils.invokeStaticMethod("org.apache.hadoop.security.UserGroupInformation", //$NON-NLS-1$
                classLoader, "getCurrentUser", new String[0]);
        if (ugi != null) {
            String[] groups = (String[]) ReflectionUtils.invokeMethod(ugi, "getGroupNames", new Object[0]); //$NON-NLS-1$
            if (groups != null && groups.length > 0) {
                for (String group : groups) {
                    groupBuf.append(group).append(GROUP_SEPARATOR);
                }
                groupBuf.deleteCharAt(groups.length - 1);
            }
        }
    } catch (Exception e) {
        // ignore it.
    }

    return groupBuf.toString();
}

From source file:ca.simplegames.micro.utils.StringUtils.java

/**
 * Trim all occurences of the supplied leading character from the given String.
 *
 * @param str              the String to check
 * @param leadingCharacter the leading character to be trimmed
 * @return the trimmed String/*from   ww w  .ja  v a 2s.  c om*/
 */
public static String trimLeadingCharacter(String str, char leadingCharacter) {
    if (!hasLength(str)) {
        return str;
    }
    StringBuffer buf = new StringBuffer(str);
    while (buf.length() > 0 && buf.charAt(0) == leadingCharacter) {
        buf.deleteCharAt(0);
    }
    return buf.toString();
}

From source file:ca.simplegames.micro.utils.StringUtils.java

/**
 * Trim leading whitespace from the given String.
 *
 * @param str the String to check/* ww  w  . j av  a2 s.  c om*/
 * @return the trimmed String
 * @see java.lang.Character#isWhitespace
 */
public static String trimLeadingWhitespace(String str) {
    if (!hasLength(str)) {
        return str;
    }
    StringBuffer buf = new StringBuffer(str);
    while (buf.length() > 0 && Character.isWhitespace(buf.charAt(0))) {
        buf.deleteCharAt(0);
    }
    return buf.toString();
}

From source file:edu.uci.ics.asterix.event.service.AsterixEventServiceUtil.java

public static String getNodeDirectories(String asterixInstanceName, Node node, Cluster cluster) {
    String storeDataSubDir = asterixInstanceName + File.separator + "data" + File.separator;
    String[] storeDirs = null;//from  w w  w.ja v  a2  s.c  o  m
    StringBuffer nodeDataStore = new StringBuffer();
    String storeDirValue = node.getStore();
    if (storeDirValue == null) {
        storeDirValue = cluster.getStore();
        if (storeDirValue == null) {
            throw new IllegalStateException(" Store not defined for node " + node.getId());
        }
        storeDataSubDir = node.getId() + File.separator + storeDataSubDir;
    }

    storeDirs = storeDirValue.split(",");
    for (String ns : storeDirs) {
        nodeDataStore.append(ns + File.separator + storeDataSubDir.trim());
        nodeDataStore.append(",");
    }
    nodeDataStore.deleteCharAt(nodeDataStore.length() - 1);
    return nodeDataStore.toString();
}

From source file:ca.simplegames.micro.utils.StringUtils.java

/**
 * Trim leading and trailing whitespace from the given String.
 *
 * @param str the String to check//ww  w .  j  a  v a2 s .c o  m
 * @return the trimmed String
 * @see java.lang.Character#isWhitespace
 */
public static String trimWhitespace(String str) {
    if (!hasLength(str)) {
        return str;
    }
    StringBuffer buf = new StringBuffer(str);
    while (buf.length() > 0 && Character.isWhitespace(buf.charAt(0))) {
        buf.deleteCharAt(0);
    }
    while (buf.length() > 0 && Character.isWhitespace(buf.charAt(buf.length() - 1))) {
        buf.deleteCharAt(buf.length() - 1);
    }
    return buf.toString();
}

From source file:ca.simplegames.micro.utils.StringUtils.java

/**
 * Trim all occurences of the supplied trailing character from the given String.
 *
 * @param str               the String to check
 * @param trailingCharacter the trailing character to be trimmed
 * @return the trimmed String/*from  w  w w. j a v  a  2 s  .  c o m*/
 */
public static String trimTrailingCharacter(String str, char trailingCharacter) {
    if (!hasLength(str)) {
        return str;
    }
    StringBuffer buf = new StringBuffer(str);
    while (buf.length() > 0 && buf.charAt(buf.length() - 1) == trailingCharacter) {
        buf.deleteCharAt(buf.length() - 1);
    }
    return buf.toString();
}