List of usage examples for java.lang StringBuffer replace
@Override public synchronized StringBuffer replace(int start, int end, String str)
From source file:org.codehaus.groovy.grails.plugins.searchable.util.StringQueryUtils.java
/** * Highlights the different terms in the second query and returns a new query string. * This method is intended to be used with suggested queries to display the suggestion * to the user in highlighted format, as per Google, so the queries are expected to roughly match * @param first the original query/*from www . j a v a 2 s. c o m*/ * @param second the second query, in which to highlight differences * @param highlightPattern the pattern used to highlight; should be a {@link MessageFormat} pattern where argument * zero is the highlighted term text * @return a new copy of second with term differences highlighted * @throws ParseException if either first or second query is invalid * @see #highlightTermDiffs(String, String) */ public static String highlightTermDiffs(String first, String second, String highlightPattern) throws ParseException { final String defaultField = "$StringQueryUtils_highlightTermDiffs$"; Term[] firstTerms = LuceneUtils.realTermsForQueryString(defaultField, first, WhitespaceAnalyzer.class); Term[] secondTerms = LuceneUtils.realTermsForQueryString(defaultField, second, WhitespaceAnalyzer.class); if (firstTerms.length != secondTerms.length) { LOG.warn("Expected the same number of terms for first query [" + first + "] and second query [" + second + "], " + "but first query has [" + firstTerms.length + "] terms and second query has [" + secondTerms.length + "] terms " + "so unable to provide user friendly version. Returning second query as-is."); return second; } MessageFormat format = new MessageFormat(highlightPattern); StringBuffer diff = new StringBuffer(second); int offset = 0; for (int i = 0; i < secondTerms.length; i++) { Term firstTerm = firstTerms[i]; Term secondTerm = secondTerms[i]; boolean noField = defaultField.equals(secondTerm.field()); String snippet = noField ? secondTerm.text() : secondTerm.field() + ":" + secondTerm.text(); int pos = diff.indexOf(snippet, offset); if (!firstTerm.text().equals(secondTerm.text())) { if (!noField) { pos += secondTerm.field().length() + 1; } diff.replace(pos, pos + secondTerm.text().length(), format.format(new Object[] { secondTerm.text() })); } offset = pos; } return diff.toString(); }
From source file:org.nuxeo.apidoc.introspection.XMLWriter.java
/** * <p>// w w w .j a va 2s . c o m * Escape the <code>toString</code> of the given object. For use as body * text. * </p> * * @param value * escape <code>value.toString()</code> * @return text with escaped delimiters */ public static final String escapeBodyValue(Object value) { StringBuffer buffer = new StringBuffer(value.toString()); for (int i = 0, size = buffer.length(); i < size; i++) { switch (buffer.charAt(i)) { case '<': buffer.replace(i, i + 1, LESS_THAN_ENTITY); size += 3; i += 3; break; case '>': buffer.replace(i, i + 1, GREATER_THAN_ENTITY); size += 3; i += 3; break; case '&': buffer.replace(i, i + 1, AMPERSAND_ENTITY); size += 4; i += 4; break; } } return buffer.toString(); }
From source file:org.nuxeo.apidoc.introspection.XMLWriter.java
/** * <p>/*w w w . jav a 2 s . co m*/ * Escape the <code>toString</code> of the given object. For use in an * attribute value. * </p> * * @param value * escape <code>value.toString()</code> * @return text with characters restricted (for use in attributes) escaped */ public static final String escapeAttributeValue(Object value) { StringBuffer buffer = new StringBuffer(value.toString()); for (int i = 0, size = buffer.length(); i < size; i++) { switch (buffer.charAt(i)) { case '<': buffer.replace(i, i + 1, LESS_THAN_ENTITY); size += 3; i += 3; break; case '>': buffer.replace(i, i + 1, GREATER_THAN_ENTITY); size += 3; i += 3; break; case '&': buffer.replace(i, i + 1, AMPERSAND_ENTITY); size += 4; i += 4; break; case '\'': buffer.replace(i, i + 1, APOSTROPHE_ENTITY); size += 5; i += 5; break; case '\"': buffer.replace(i, i + 1, QUOTE_ENTITY); size += 5; i += 5; break; } } return buffer.toString(); }
From source file:org.apache.axis2.jaxws.runtime.description.marshal.impl.ArtifactProcessor.java
/** * @param methodName//from www . ja v a2 s . c om * @return method name converted into a class name */ private static String javaMethodToClassName(String methodName) { String className = null; if (methodName != null) { StringBuffer buildClassName = new StringBuffer(methodName); buildClassName.replace(0, 1, methodName.substring(0, 1).toUpperCase()); className = buildClassName.toString(); } return className; }
From source file:Main.java
/** * convert the reference of the numerical value letter into ths substance. * @param xml/*w w w . ja v a2 s .com*/ * @return */ public static String resolveNumEntities(String xml) { StringBuffer strb = new StringBuffer(xml); int index; int index2 = 0; int index3 = 0; while ((index = strb.toString().indexOf("&#", index2)) != -1) { index2 = strb.toString().indexOf(';', index + 1); index3 = strb.toString().indexOf("&#", index + 1); if (index2 == -1) { break; } else if (index3 != -1 && index2 > index3) { //We pass the entity description that is not right. index2 = index3; } else { try { char numericChar = (char) Integer.parseInt(strb.substring(index + 2, index2)); String numericStr = String.valueOf(numericChar); strb.replace(index, index2 + 1, numericStr); index2 = index + numericStr.length(); } catch (NumberFormatException e) { //We pass reference of the numerical value letter that is not right. } } } return strb.toString(); }
From source file:org.exoplatform.platform.common.software.register.UnlockService.java
private static int decodeKey(String productCode, String Key) { try {//from w w w .jav a 2 s. co m StringBuffer keyBuffer = new StringBuffer(new String(Base64.decodeBase64(Key.getBytes()))); String keyLengthString = keyBuffer.substring(8, 10); int length = Integer.parseInt(keyBuffer.substring(4, 6)); keyBuffer.replace(4, 6, ""); String productCodeHashed = keyBuffer.substring(0, length); if (!productCodeHashed.equals(Utils.getModifiedMD5Code(productCode.getBytes()))) { keyBuffer.replace(6, 8, ""); productCodeHashed = keyBuffer.substring(0, length); if (!productCodeHashed.equals(Utils.getModifiedMD5Code(productCode.getBytes()))) { return 0; } } String productInfoString = keyBuffer.substring(length); String[] productInfo = productInfoString.split(","); if ((productInfo.length == 3)) { int keyLength = Integer.parseInt(keyLengthString); boolean validLicence = (keyLength == keyBuffer.toString().length() + 4); if (!validLicence) return 0; String nbUser = productInfo[0]; String duration = productInfo[1]; String keyDate = productInfo[2]; DateFormat d = new SimpleDateFormat("dd/MM/yyyy"); try { d.parse(keyDate); } catch (ParseException e) { LOG.info("UNVALID KEY"); return 0; } String edition = ""; int period = 0; try { period = Integer.parseInt(duration); } catch (NumberFormatException exp) { LOG.info("INVALID KAY"); return 0; } if (period == -1) { duration = Utils.UNLIMITED; nbUser = new String(Base64.decodeBase64(nbUser.getBytes())); int userNumber = 0; try { userNumber = Integer.parseInt(nbUser) / 3; } catch (NumberFormatException exp) { LOG.info("INVALID KAY"); return 0; } if (userNumber == -1) { edition = ProductInformations.ENTERPRISE_EDITION; nbUser = Utils.UNLIMITED; } else { edition = ProductInformations.EXPRESS_EDITION; nbUser = String.valueOf(userNumber); } } persistInfo(edition, nbUser, keyDate, duration, productCode, Key); return period; } else if ((productInfo.length == 1) || (productInfo.length == 0)) { String periodString = new String(Base64.decodeBase64(productInfoString.getBytes())); int period = Integer.parseInt(periodString) / 3; return period; } else return 0; } catch (Exception e) { return 0; } }
From source file:org.springframework.util.SystemPropertyUtils.java
/** * Resolve ${...} placeholders in the given text, * replacing them with corresponding system property values. * @param text the String to resolve// w w w . j av a 2s . c om * @return the resolved String * @see #PLACEHOLDER_PREFIX * @see #PLACEHOLDER_SUFFIX */ public static String resolvePlaceholders(String text) { 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); String propVal = System.getProperty(placeholder); if (propVal != null) { buf.replace(startIndex, endIndex + PLACEHOLDER_SUFFIX.length(), propVal); startIndex = buf.toString().indexOf(PLACEHOLDER_PREFIX, startIndex + propVal.length()); } else { if (logger.isWarnEnabled()) { logger.warn("Could not resolve placeholder '" + placeholder + "' in [" + text + "] as system property"); } startIndex = buf.toString().indexOf(PLACEHOLDER_PREFIX, endIndex + PLACEHOLDER_SUFFIX.length()); } } else { startIndex = -1; } } return buf.toString(); }
From source file:org.locationtech.jtstest.util.StringUtil.java
/** * Replaces all instances of the String o with the String n in the * StringBuffer orig if all is true, or only the first instance if all is * false. Posted by Steve Chapel <schapel@breakthr.com> on UseNet *//* w w w . ja v a2s . co m*/ public static void replace(StringBuffer orig, String o, String n, boolean all) { if (orig == null || o == null || o.length() == 0 || n == null) { throw new IllegalArgumentException("Null or zero-length String"); } int i = 0; while (i + o.length() <= orig.length()) { if (orig.substring(i, i + o.length()).equals(o)) { orig.replace(i, i + o.length(), n); if (!all) { break; } else { i += n.length(); } } else { i++; } } }
From source file:net.sf.excelutils.ExcelParser.java
/** * parse complex expression ${${}}aaa${} * //from w ww . java 2s. c o m * @param context * @param str * @param quot string needs quotation or not * @return value of the str */ public static Object parseStr(Object context, String str, boolean quot) { int exprCount = 0; int valueFrom = -1; int valueTo = -1; int valueCount = 0; int pos = 0; Object value = null; int indexValued = str.indexOf(VALUED_DELIM); int indexValued2 = str.lastIndexOf(VALUED_DELIM2); boolean bJustExpr = str.length() == indexValued2 + VALUED_DELIM2.length() - indexValued; while (pos < str.length()) { if (pos + VALUED_DELIM.length() <= str.length()) { if (VALUED_DELIM.equals(str.substring(pos, pos + VALUED_DELIM.length()))) { if (valueCount == 0) { valueFrom = pos; } valueCount++; pos = pos + VALUED_DELIM.length(); continue; } } if (VALUED_DELIM2.equals(str.substring(pos, pos + VALUED_DELIM2.length()))) { valueCount--; if (valueCount == 0) { valueTo = pos; String expr = str.substring(valueFrom, valueTo + VALUED_DELIM2.length()); value = parseExpr(context, expr); exprCount++; // replace the string StringBuffer sbuf = new StringBuffer(str); if (null != value) { String rep = value.toString(); // need quotation if (quot) { rep = "\"" + rep + "\""; } sbuf.replace(valueFrom, valueTo + VALUED_DELIM2.length(), rep); pos += VALUED_DELIM2.length() + value.toString().length() - expr.length(); } else { String rep = ""; // need quotation if (quot) { rep = "\"" + rep + "\""; } sbuf.replace(valueFrom, valueTo + VALUED_DELIM2.length(), rep); pos += VALUED_DELIM2.length() + 0 - expr.length(); } str = sbuf.toString(); continue; } else { pos += VALUED_DELIM2.length(); continue; } } pos++; } if (exprCount == 1 && bJustExpr) { if (null != value) { if (quot && "java.lang.String".equals(value.getClass().getName())) { return "\"" + value.toString() + "\""; } return value; } return value; } else { return str; } }
From source file:com.sshtools.j2ssh.authentication.UserGridCredential.java
private static void rmPass(StringBuffer password) { for (int i = 0; i < password.length(); i++) { password.replace(i, i + 1, "*"); }//from ww w. j av a2s.c o m password = new StringBuffer(); ; }