Example usage for java.lang StringBuffer replace

List of usage examples for java.lang StringBuffer replace

Introduction

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

Prototype

@Override
public synchronized StringBuffer replace(int start, int end, String str) 

Source Link

Usage

From source file:com.ai.smart.common.helper.util.RequestUtils.java

/**
 * /*  w w  w . j av a 2  s .co  m*/
 * <p>
 * HttpServletRequest.getRequestURL+"?"+HttpServletRequest.getQueryString
 *
 * @param request
 * @return
 */
public static String getLocation(HttpServletRequest request) {
    UrlPathHelper helper = new UrlPathHelper();
    StringBuffer buff = request.getRequestURL();
    String uri = request.getRequestURI();
    String origUri = helper.getOriginatingRequestUri(request);
    buff.replace(buff.length() - uri.length(), buff.length(), origUri);
    String queryString = helper.getOriginatingQueryString(request);
    if (queryString != null) {
        buff.append("?").append(queryString);
    }
    return buff.toString();
}

From source file:org.vosao.utils.StrUtil.java

public static String removeJavascript(String s) {
    StringBuffer buf = new StringBuffer(s);
    int scriptStart = buf.indexOf("<script");
    while (scriptStart > 0) {
        int scriptEnd = buf.indexOf("</script>", scriptStart) + 9;
        if (scriptEnd > 0) {
            buf.replace(scriptStart, scriptEnd, "");
        }/*from www. j  a va  2  s.c  o m*/
        scriptStart = buf.indexOf("<script", scriptStart);
    }
    return buf.toString();
}

From source file:Main.java

/**
 * Escapes characters that are not allowed in various places
 * in XML by replacing all invalid characters with
 * <code>getEscape(c)</code>.
 * //from   w  w  w . ja v a2 s . c  o  m
 * @param buffer The <code>StringBuffer</code> containing
 * the text to escape in place.
 */
public static void XMLEscape(StringBuffer buffer) {
    if (buffer == null) {
        return;
    }
    char c;
    String escape;
    for (int i = 0; i < buffer.length();) {
        c = buffer.charAt(i);
        escape = getEscape(c);
        if (escape == null) {
            i++;
        } else {
            buffer.replace(i, i + 1, escape);
            i += escape.length();
        }
    }
}

From source file:org.eclim.util.StringUtils.java

/**
 * Replaces placeholders of the form ${key} in the supplied string using
 * key / value pairs from the Map provided.
 *
 * @param string the String to evaluate.
 * @param values the values to use in the string.
 * @return The evaluation result.//from   ww w .j  a  v  a 2s.  com
 */
@SuppressWarnings("rawtypes")
public static String replacePlaceholders(String string, Map values) {
    if (string == null || values == null) {
        return string;
    }

    StringBuffer buffer = new StringBuffer(string);

    int start = buffer.indexOf(PLACEHOLDER_PREFIX);
    int end = buffer.indexOf(PLACEHOLDER_SUFFIX);
    while (start != -1 && end != -1) {
        String placeholder = buffer.substring(start + PLACEHOLDER_PREFIX.length(), end);

        Object value = values.get(placeholder);
        if (value != null) {
            buffer.replace(start, end + 1, value.toString());
        }

        start = buffer.indexOf(PLACEHOLDER_PREFIX, start + 1);
        end = buffer.indexOf(PLACEHOLDER_SUFFIX, start + 1);
    }

    return buffer.toString();
}

From source file:Main.java

/**
 * Returns attribute's getter method. If the method not found then
 * NoSuchMethodException will be thrown.
 * // w  w  w . j a  v  a 2 s .  c o m
 * @param cls
 *          the class the attribute belongs too
 * @param attr
 *          the attribute's name
 * @return attribute's getter method
 * @throws NoSuchMethodException
 *           if the getter was not found
 */
public final static Method getAttributeGetter(Class cls, String attr) throws NoSuchMethodException {
    StringBuffer buf = new StringBuffer(attr.length() + 3);
    buf.append("get");
    if (Character.isLowerCase(attr.charAt(0))) {
        buf.append(Character.toUpperCase(attr.charAt(0))).append(attr.substring(1));
    } else {
        buf.append(attr);
    }

    try {
        return cls.getMethod(buf.toString(), (Class[]) null);
    } catch (NoSuchMethodException e) {
        buf.replace(0, 3, "is");
        return cls.getMethod(buf.toString(), (Class[]) null);
    }
}

From source file:org.apache.hadoop.vertica.VerticaUtil.java

public static void checkOutputSpecs(Configuration conf) throws IOException {
    VerticaConfiguration vtconfig = new VerticaConfiguration(conf);

    String writerTable = vtconfig.getOutputTableName();
    if (writerTable == null)
        throw new IOException("Vertica output requires a table name defined by "
                + VerticaConfiguration.OUTPUT_TABLE_NAME_PROP);
    String[] def = vtconfig.getOutputTableDef();
    boolean dropTable = vtconfig.getDropTable();

    String schema = null;/*from  w  w w  .  j  av  a  2 s  .co  m*/
    String table = null;
    String[] schemaTable = writerTable.split("\\.");
    if (schemaTable.length == 2) {
        schema = schemaTable[0];
        table = schemaTable[1];
    } else
        table = schemaTable[0];

    Statement stmt = null;
    try {
        Connection conn = vtconfig.getConnection(true);
        DatabaseMetaData dbmd = conn.getMetaData();
        ResultSet rs = dbmd.getTables(null, schema, table, null);
        boolean tableExists = rs.next();

        stmt = conn.createStatement();

        if (tableExists && dropTable) {
            if (verticaVersion(conf, true) >= 305) {
                stmt = conn.createStatement();
                stmt.execute("TRUNCATE TABLE " + writerTable);
            } else {
                // for version < 3.0 drop the table if it exists
                // if def is empty, grab the columns first to redfine the table
                if (def == null) {
                    rs = dbmd.getColumns(null, schema, table, null);
                    ArrayList<String> defs = new ArrayList<String>();
                    while (rs.next())
                        defs.add(rs.getString(4) + " " + rs.getString(5));
                    def = defs.toArray(new String[0]);
                }

                stmt = conn.createStatement();
                stmt.execute("DROP TABLE " + writerTable + " CASCADE");
                tableExists = false; // force create
            }
        }

        // create table if it doesn't exist
        if (!tableExists) {
            if (def == null)
                throw new RuntimeException(
                        "Table " + writerTable + " does not exist and no table definition provided");
            if (schema != null) {
                rs = dbmd.getSchemas(null, schema);
                if (!rs.next())
                    stmt.execute("CREATE SCHEMA " + schema);
            }
            StringBuffer tabledef = new StringBuffer("CREATE TABLE ").append(writerTable).append(" (");
            for (String column : def)
                tabledef.append(column).append(",");
            tabledef.replace(tabledef.length() - 1, tabledef.length(), ")");

            stmt.execute(tabledef.toString());
            // TODO: create segmented projections
            stmt.execute("select implement_temp_design('" + writerTable + "')");
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        if (stmt != null)
            try {
                stmt.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
    }
}

From source file:org.eclim.plugin.jdt.command.junit.JUnitUtils.java

private static void replaceIllegalCharacters(StringBuffer buffer) {
    char character = 0;
    for (int index = buffer.length() - 1; index >= 0; index--) {
        character = buffer.charAt(index);
        if (Character.isWhitespace(character)) {
            buffer.deleteCharAt(index);/* w  w w.  j  ava  2  s  .c o  m*/
        } else if (character == '<') {
            buffer.replace(index, index + 1, OF_TAG);
        } else if (character == '?') {
            buffer.replace(index, index + 1, QUESTION_MARK_TAG);
            // Skipping this for now so we don't rely on sun packages.
            /*}else if (!Character.isJavaIdentifierPart(character)) {
              // Check for surrogates
              if (!UTF16.isSurrogate(character)) {
                buffer.deleteCharAt(index);
              }*/
        }
    }
}

From source file:com.tacitknowledge.util.discovery.ClasspathUtils.java

/**
 * If the system is running on Tomcat, this method will parse the
 * <code>common.loader</code> property to reach deeper into the
 * classpath to get Tomcat common paths/*w  ww  .  j a v  a  2 s. c  o m*/
 *
 * @return a list of paths or null if tomcat paths not found
 */
private static List getTomcatPaths() {
    String tomcatPath = System.getProperty("catalina.home");
    if (tomcatPath == null) {
        //not running Tomcat
        return null;
    }
    String commonClasspath = System.getProperty("common.loader");
    if (commonClasspath == null) {
        //didn't find the common classpath
        return null;
    }
    StringBuffer buffer = new StringBuffer(commonClasspath);
    String pathDeclaration = "${catalina.home}";
    int length = pathDeclaration.length();
    boolean doneReplace = false;
    do {
        int start = commonClasspath.indexOf(pathDeclaration);
        if (start >= 0) {
            buffer.replace(start, (start + length), tomcatPath);
            commonClasspath = buffer.toString();
        } else {
            doneReplace = true;
        }
    } while (!doneReplace);
    String[] paths = commonClasspath.split(",");

    List pathList = new ArrayList(paths.length);
    for (int i = 0; i < paths.length; i++) {
        String path = paths[i];
        pathList.add(getCanonicalPath(path));
    }
    return pathList;
}

From source file:org.jmaki.model.impl.WidgetFactory.java

public static void replace(StringBuffer buff, String target, String replacement) {
    if (buff == null || target == null || replacement == null) {
        return;//from  w ww  .j a  v a  2  s.  c  o  m
    }
    int index = 0;
    while (index < buff.length()) {
        index = buff.indexOf(target);
        if (index == -1) {
            break;
        }
        buff.replace(index, index + target.length(), replacement);
        index += replacement.length() + 1;
    }
}

From source file:pl.psnc.synat.wrdz.zmkd.invocation.RestServiceCallerUtils.java

/**
 * Constructs URI of the service based upon passed information.
 * /*  ww  w . j  a v  a2s.  c o m*/
 * @param address
 *            template address of the service
 * @param templateParams
 *            template parameters
 * @param queryParams
 *            query parameters
 * @return URI of the service
 * @throws URISyntaxException
 *             when correct URI cannot be constructed
 */
private static URI constructServiceURI(String address, List<ExecutionTemplateParam> templateParams,
        List<ExecutionQueryParam> queryParams) throws URISyntaxException {
    StringBuffer sb = new StringBuffer(address);
    for (ExecutionTemplateParam templateParam : templateParams) {
        String varParam = "{" + templateParam.getName() + "}";
        int idx = sb.indexOf(varParam);
        if (idx != -1) {
            sb.replace(idx, idx + varParam.length(), templateParam.getValue());
        }
    }
    boolean first = true;
    for (ExecutionQueryParam queryParam : queryParams) {
        if (first) {
            sb.append("?");
            first = false;
        } else {
            sb.append("&");
        }
        if (queryParam.getValues() != null) {
            for (String value : queryParam.getValues()) {
                sb.append(queryParam.getName()).append("=").append(value);
            }
        } else {
            sb.append(queryParam.getName());
        }
    }
    return new URI(sb.toString());
}