List of usage examples for java.lang StringBuffer replace
@Override public synchronized StringBuffer replace(int start, int end, String str)
From source file:org.eclipse.jubula.tools.internal.utils.StringParsing.java
/** * Checks <code>str</code> for a sequence number, based on the * <code>sequencePrefix</code>. If a sequence number is found, it is * incremented, and the incremented string is returned. If no sequence is * found, a new sequence is started, and the string with the newly started * sequence is returned. Only positive integers are recognized as a * valid sequence./*from w w w.jav a 2s. c om*/ * * <pre> * StringParsing.incrementSequence("abc", "_") = "abc_1" * StringParsing.incrementSequence("abc_5", "_") = "abc_6" * StringParsing.incrementSequence("_2", "_") = "_3" * StringParsing.incrementSequence("1", "_") = "1_1" * StringParsing.incrementSequence("abc_-1", "_") = "abc_-1_1" * StringParsing.incrementSequence("abc_0", "_") = "abc_0_1" * StringParsing.incrementSequence("abc_1_1", "_") = "abc_1_2" * </pre> * * @param str The string for which the sequence should be incremented. * Must not be empty (<code>null</code> or empty string). * @param sequencePrefix The string that precedes the sequence. Must not be * empty (<code>null</code> or empty string). * @return a string that represents the given string <code>str</code> with * the sequence number incremented. */ public static String incrementSequence(String str, String sequencePrefix) { Validate.notEmpty(str); Validate.notEmpty(sequencePrefix); StringBuffer builder = new StringBuffer(str); String suffix = StringUtils.substringAfterLast(str, "_"); //$NON-NLS-1$ // parse suffix to integer and increment if possible. // if we can't parse it, then we just start a new sequence. int sequence = -1; try { sequence = Integer.parseInt(suffix); if (sequence > 0) { sequence++; } } catch (NumberFormatException nfe) { // Could not parse the suffix to an integer. // The sequence will remain at its initialized value. } if (sequence > 0) { builder.replace(builder.lastIndexOf(suffix), builder.length(), String.valueOf(sequence)); } else { builder.append(sequencePrefix).append(1); } return builder.toString(); }
From source file:org.guzz.util.PropertyUtil.java
/** * >= JDK1.4//from w w w . jav a 2s . c o m * resolve ${...} placeholders in the @param text , then trim() the value and return. * @param props Map * @param text the String text to replace * @return the resolved value * * @see #PLACEHOLDER_PREFIX * @see #PLACEHOLDER_SUFFIX */ public static String getTrimStringMatchPlaceholdersInMap(Map props, String text) { if (text == null || props == null || props.isEmpty()) { return text; } StringBuffer buf = new StringBuffer(text); int startIndex = buf.indexOf(PLACEHOLDER_PREFIX); while (startIndex != -1) { int endIndex = buf.indexOf(PLACEHOLDER_SUFFIX, startIndex + PLACEHOLDER_PREFIX.length()); if (endIndex != -1) { String placeholder = buf.substring(startIndex + PLACEHOLDER_PREFIX.length(), endIndex); String propVal = (String) props.get(placeholder); if (propVal != null) { buf.replace(startIndex, endIndex + PLACEHOLDER_SUFFIX.length(), propVal); startIndex = buf.indexOf(PLACEHOLDER_PREFIX, startIndex + propVal.length()); } else { log.warn("Could not resolve placeholder '" + placeholder + "' in [" + text + "]"); startIndex = buf.indexOf(PLACEHOLDER_PREFIX, endIndex + PLACEHOLDER_SUFFIX.length()); } } else { startIndex = -1; } } return buf.toString().trim(); }
From source file:org.soitoolkit.commons.mule.util.MiscUtil.java
static public String parseStringValue(String strVal, Properties props) { StringBuffer buf = new StringBuffer(strVal); int startIndex = strVal.indexOf(placeholderPrefix); while (startIndex != -1) { int endIndex = findPlaceholderEndIndex(buf, startIndex); if (endIndex != -1) { String placeholder = buf.substring(startIndex + placeholderPrefix.length(), endIndex); String propVal = props.getProperty(placeholder); if (propVal != null) { // Recursive invocation, parsing placeholders contained in the previously resolved placeholder value. // E.g. a variable value like: VARIABLE1=Var${VARIABLE2}Value propVal = parseStringValue(propVal, props); buf.replace(startIndex, endIndex + placeholderSuffix.length(), propVal); if (logger.isTraceEnabled()) { logger.trace("Resolved placeholder '" + placeholder + "'"); }/*from ww w . j a v a 2 s. co m*/ startIndex = buf.indexOf(placeholderPrefix, startIndex + propVal.length()); } else { throw new RuntimeException("Could not resolve placeholder '" + placeholder + "'"); } } else { startIndex = -1; } } return buf.toString(); }
From source file:util.io.IOUtilities.java
/** * Replaces in <code>buffer</code> the <code>pattern</code> by <code>str</code>. * * @param buffer The buffer to replace in. * @param pattern The pattern to replace. * @param str The str that should replace the pattern. *//*from w w w . j a v a2 s. c om*/ public static void replace(StringBuffer buffer, String pattern, String str) { int offset = 0; int patternIdx; do { patternIdx = buffer.indexOf(pattern, offset); if (patternIdx != -1) { buffer.replace(patternIdx, patternIdx + pattern.length(), str); } offset = patternIdx + str.length(); } while (patternIdx != -1); }
From source file:org.ops4j.pax.runner.commons.properties.SystemPropertyUtils.java
/** * Resolve ${...} placeholders in the given text, replacing them with corresponding property values or system * property values.//from w w w . j a va 2 s. c om * * @param text the String to resolve * @param properties properties to be searched beside system properties * * @return the resolved String * * @see #PLACEHOLDER_PREFIX * @see #PLACEHOLDER_SUFFIX */ public static String resolvePlaceholders(final String text, final Properties properties) { if (text == null) { return null; } StringBuffer buf = new StringBuffer(text); int startIndex = buf.indexOf(PLACEHOLDER_PREFIX); while (startIndex != -1) { int endIndex = buf.indexOf(PLACEHOLDER_SUFFIX, startIndex + PLACEHOLDER_PREFIX.length()); if (endIndex != -1) { String placeholder = buf.substring(startIndex + PLACEHOLDER_PREFIX.length(), endIndex); int nextIndex = endIndex + PLACEHOLDER_SUFFIX.length(); try { String propVal = properties.getProperty(placeholder); if (propVal == null) { propVal = System.getProperty(placeholder); if (propVal == null) { // Fall back to searching the system environment. propVal = System.getenv(placeholder); } } if (propVal != null) { buf.replace(startIndex, endIndex + PLACEHOLDER_SUFFIX.length(), propVal); nextIndex = startIndex + propVal.length(); } else { if (LOGGER.isWarnEnabled()) { LOGGER.warn("Could not resolve placeholder '" + placeholder + "' in [" + text + "] as system property: neither system property nor environment variable found"); } } } catch (Throwable ex) { if (LOGGER.isWarnEnabled()) { LOGGER.warn("Could not resolve placeholder '" + placeholder + "' in [" + text + "] as system property: " + ex); } } startIndex = buf.indexOf(PLACEHOLDER_PREFIX, nextIndex); } else { startIndex = -1; } } return buf.toString(); }
From source file:TypeConversionHelper.java
/** * Utility to convert a short into a 4-char hex String * @param val The short/*from ww w . j a v a 2s .com*/ * @return The hex String form of the short */ public static String getHexFromShort(short val) { StringBuffer str = new StringBuffer("0000"); String hexstr = Integer.toHexString(val); str.replace(4 - hexstr.length(), 4, hexstr); return str.toString(); }
From source file:TypeConversionHelper.java
/** * Utility to convert an int into a 8-char hex String * @param val The int//from w ww . j a v a2s . c o m * @return The hex String form of the int */ public static String getHexFromInt(int val) { StringBuffer str = new StringBuffer("00000000"); String hexstr = Integer.toHexString(val); str.replace(8 - hexstr.length(), 8, hexstr); return str.toString(); }
From source file:com.qpark.maven.plugin.flowmapper.AbstractGenerator.java
public static String toJavadocHeader(final String documentation) { final int lenght = 77; String s = documentation.replaceAll("\\t", " ").replaceAll("\\n", " ").replaceAll("( )+", " "); final StringBuffer sb = new StringBuffer(); while (s.length() > 0) { final int index = s.substring(0, Math.min(lenght, s.length())).lastIndexOf(' '); if (s.length() < lenght || index < 0) { if (sb.length() > 0) { sb.append("\n * "); }/*from w ww . j ava 2 s. c om*/ sb.append(s.trim()); s = ""; } else { if (index > 0) { sb.append("\n * "); sb.append(s.substring(0, index).trim()); s = s.substring(index + 1, s.length()); } } } s = sb.toString().trim(); if (s.length() > 0 && s.charAt(s.length() - 1) != '.') { sb.append(".\n"); } else { sb.append("\n"); } s = sb.toString(); if (s.charAt(0) == '\'') { sb.replace(0, 1, ""); } return sb.toString(); }
From source file:org.tobarsegais.webapp.RedirectFilter.java
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { if (StringUtils.isEmpty(domain) || !(request instanceof HttpServletRequest)) { chain.doFilter(request, response); } else {/*from w ww. j a va 2 s . com*/ final HttpServletRequest req = (HttpServletRequest) request; final HttpServletResponse resp = (HttpServletResponse) response; final String serverName = req.getServerName(); if (domain.equalsIgnoreCase(serverName)) { chain.doFilter(request, response); } else { StringBuffer requestURL = req.getRequestURL(); int index = requestURL.indexOf(serverName); requestURL.replace(index, index + serverName.length(), domain); final String queryString = req.getQueryString(); if (queryString != null) { requestURL.append('?').append(queryString); } resp.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY); resp.setHeader("Location", requestURL.toString()); } } }
From source file:org.compass.core.util.SystemPropertyUtils.java
/** * Resolve ${...} placeholders in the given text, * replacing them with corresponding system property values. * * @param text the String to resolve//w ww. ja va2s. c om * @return the resolved String * @see #PLACEHOLDER_PREFIX * @see #PLACEHOLDER_SUFFIX */ public static String resolvePlaceholders(String text) { if (text == null) { return null; } StringBuffer buf = new StringBuffer(text); // The following code does not use JDK 1.4's StringBuffer.indexOf(String) // method to retain JDK 1.3 compatibility. The slight loss in performance // is not really relevant, as this code will typically just run on startup. int startIndex = text.indexOf(PLACEHOLDER_PREFIX); while (startIndex != -1) { int endIndex = buf.toString().indexOf(PLACEHOLDER_SUFFIX, startIndex + PLACEHOLDER_PREFIX.length()); if (endIndex != -1) { String placeholder = buf.substring(startIndex + PLACEHOLDER_PREFIX.length(), endIndex); int nextIndex = endIndex + PLACEHOLDER_SUFFIX.length(); try { String propVal = System.getProperty(placeholder); if (propVal == null) { // Fall back to searching the system environment. propVal = System.getenv(placeholder); } if (propVal != null) { buf.replace(startIndex, endIndex + PLACEHOLDER_SUFFIX.length(), propVal); nextIndex = startIndex + propVal.length(); } else { if (logger.isDebugEnabled()) { logger.debug("Could not resolve placeholder '" + placeholder + "' in [" + text + "] as system property: neither system property nor environment variable found"); } } } catch (Throwable ex) { if (logger.isDebugEnabled()) { logger.debug("Could not resolve placeholder '" + placeholder + "' in [" + text + "] as system property: " + ex); } } startIndex = buf.toString().indexOf(PLACEHOLDER_PREFIX, nextIndex); } else { startIndex = -1; } } return buf.toString(); }