List of usage examples for java.lang StringBuffer charAt
@Override public synchronized char charAt(int index)
From source file:org.mule.transport.jms.JmsMessageUtils.java
/** * Encode a String so that is is a valid JMS header name * * @param name the String to encode/*from w w w. j a v a 2s .c o m*/ * @return a valid JMS header name */ public static String encodeHeader(String name) { // check against JMS 1.1 spec, sections 3.5.1 (3.8.1.1) boolean nonCompliant = false; if (StringUtils.isEmpty(name)) { throw new IllegalArgumentException("Header name to encode must not be null or empty"); } int i = 0, length = name.length(); while (i < length && Character.isJavaIdentifierPart(name.charAt(i))) { // zip through i++; } if (i == length) { // String is already valid return name; } else { // make a copy, fix up remaining characters StringBuffer sb = new StringBuffer(name); for (int j = i; j < length; j++) { if (!Character.isJavaIdentifierPart(sb.charAt(j))) { sb.setCharAt(j, REPLACEMENT_CHAR); nonCompliant = true; } } if (nonCompliant) { logger.warn(MessageFormat.format( "Header: {0} is not compliant with JMS specification (sec. 3.5.1, 3.8.1.1). It will cause " + "problems in your and other applications. Please update your application code to correct this. " + "Mule renamed it to {1}", name, sb.toString())); } return sb.toString(); } }
From source file:com.kolich.curacao.mappers.request.types.body.EncodedRequestBodyMapper.java
private static final Map.Entry<String, String> getNextNameValuePair(final StringBuffer buffer, final Cursor cursor) { boolean terminated = false; int pos = cursor.getPosition(), indexFrom = cursor.getPosition(), indexTo = cursor.getUpperBound(); // Find name//from w w w . j a va 2 s.c om String name = null; while (pos < indexTo) { final char ch = buffer.charAt(pos); if (ch == KEY_VALUE_EQUALS) { break; } if (ch == DELIMITER) { terminated = true; break; } pos++; } if (pos == indexTo) { terminated = true; name = buffer.substring(indexFrom, indexTo).trim(); } else { name = buffer.substring(indexFrom, pos).trim(); pos++; } if (terminated) { cursor.updatePosition(pos); return Maps.immutableEntry(name, null); } // Find value String value = null; int i1 = pos; boolean quoted = false, escaped = false; while (pos < indexTo) { char ch = buffer.charAt(pos); if (ch == VALUE_DOUBLE_QUOTE && !escaped) { quoted = !quoted; } if (!quoted && !escaped && ch == DELIMITER) { terminated = true; break; } if (escaped) { escaped = false; } else { escaped = quoted && ch == '\\'; } pos++; } int i2 = pos; // Trim leading white space while (i1 < i2 && (Whitespace.isWhitespace(buffer.charAt(i1)))) { i1++; } // Trim trailing white space while ((i2 > i1) && (Whitespace.isWhitespace(buffer.charAt(i2 - 1)))) { i2--; } // Strip away quotes if necessary if (((i2 - i1) >= 2) && (buffer.charAt(i1) == '"') && (buffer.charAt(i2 - 1) == '"')) { i1++; i2--; } value = buffer.substring(i1, i2); if (terminated) { pos++; } cursor.updatePosition(pos); return Maps.immutableEntry(name, value); }
From source file:com.niki.normalizer.Metaphone.java
private static boolean isVowel(StringBuffer string, int index) { return VOWELS.indexOf(string.charAt(index)) >= 0; }
From source file:com.mg.framework.utils.StringUtils.java
/** * Make sure the string starts with a forward slash but does not end with one; converts * back-slashes to forward-slashes; if in String is null or empty, returns zero length string. *//* w w w .ja v a 2s.c o m*/ public static String cleanUpPathPrefix(String prefix) { if (prefix == null || prefix.length() == 0) return ""; StringBuffer cppBuff = new StringBuffer(prefix.replace('\\', '/')); if (cppBuff.charAt(0) != '/') { cppBuff.insert(0, '/'); } if (cppBuff.charAt(cppBuff.length() - 1) == '/') { cppBuff.deleteCharAt(cppBuff.length() - 1); } return cppBuff.toString(); }
From source file:org.LexGrid.util.sql.DBUtility.java
public static String escapeLdapDN(String string, boolean escapeCommas) { StringBuffer temp = new StringBuffer(string); for (int i = 0; i < temp.length(); i++) { if (temp.charAt(i) == '<') { temp.insert(i, '\\'); i++;/* w w w .j a v a2 s . co m*/ } if (temp.charAt(i) == '>') { temp.insert(i, '\\'); i++; } if (temp.charAt(i) == '/') { temp.insert(i, '\\'); i++; } if (escapeCommas && temp.charAt(i) == ',') { temp.insert(i, '\\'); i++; } } return temp.toString(); }
From source file:org.opennms.netmgt.mib2events.Mib2Events.java
public static String getTrapEventDescr(MibValueSymbol trapValueSymbol) { String description = ((SnmpType) trapValueSymbol.getType()).getDescription(); // FIXME There a lot of detail here (like removing the last \n) that can go away when we don't need to match mib2opennms exactly final String descrStartingNewlines = description.replaceAll("^", "\n<p>"); final String descrEndingNewlines = descrStartingNewlines.replaceAll("$", "</p>\n"); //final String descrTabbed = descrEndingNewlines.replaceAll("(?m)^", "\t"); //final StringBuffer dbuf = new StringBuffer(descrTabbed); final StringBuffer dbuf = new StringBuffer(descrEndingNewlines); if (dbuf.charAt(dbuf.length() - 1) == '\n') { dbuf.deleteCharAt(dbuf.length() - 1); // delete the \n at the end }/* w w w . j a va 2 s . c o m*/ //if (dbuf.lastIndexOf("\n") != -1) { // dbuf.insert(dbuf.lastIndexOf("\n") + 1, '\t'); //} //final StringBuffer dbuf = new StringBuffer(descrEndingNewlines); //dbuf.append("\n"); dbuf.append("<table>"); dbuf.append("\n"); int vbNum = 1; for (MibValue vb : getTrapVars(trapValueSymbol)) { dbuf.append("\t<tr><td><b>\n\n\t").append(vb.getName()); dbuf.append("</b></td><td>\n\t%parm[#").append(vbNum).append("]%;</td><td><p>"); SnmpObjectType snmpObjectType = ((SnmpObjectType) ((ObjectIdentifierValue) vb).getSymbol().getType()); if (snmpObjectType.getSyntax().getClass().equals(IntegerType.class)) { IntegerType integerType = (IntegerType) snmpObjectType.getSyntax(); if (integerType.getAllSymbols().length > 0) { SortedMap<Integer, String> map = new TreeMap<Integer, String>(); for (MibValueSymbol sym : integerType.getAllSymbols()) { map.put(new Integer(sym.getValue().toString()), sym.getName()); } dbuf.append("\n"); for (Entry<Integer, String> entry : map.entrySet()) { dbuf.append("\t\t").append(entry.getValue()).append("(").append(entry.getKey()) .append(")\n"); } dbuf.append("\t"); } } dbuf.append("</p></td></tr>\n"); vbNum++; } if (dbuf.charAt(dbuf.length() - 1) == '\n') { dbuf.deleteCharAt(dbuf.length() - 1); // delete the \n at the end } dbuf.append("</table>\n\t"); return dbuf.toString(); }
From source file:ac.elements.parser.ExtendedFunctions.java
/** * Trim all occurences of the supplied character from the given String. * /*www .j a va 2s . c o m*/ * @param str * the String to check * @param ter * the character to be trimmed * @return the trimmed String */ public static String trimCharacter(String str, char character) { if (str == null || str.length() == 0) { return str; } StringBuffer buf = new StringBuffer(str); while (buf.length() > 0 && buf.charAt(0) == character) { buf.deleteCharAt(0); } while (buf.length() > 0 && buf.charAt(buf.length() - 1) == character) { buf.deleteCharAt(buf.length() - 1); } return buf.toString(); }
From source file:Main.java
/** * collapse multi-whitespace into one whitespace in the original text. * Support unicode white space with isWhitespace and isSpaceChar method in * {@link #Character}//from ww w . j a va 2 s .co m * * @param oriText * @return */ public static String collapseWhiteSpace(StringBuffer oriText) { if (oriText == null) { return null; } int oriLen = oriText.length(); StringBuffer newText = new StringBuffer(oriLen); int wsCount = 0; char lastWs = ' '; for (int i = 0; i <= oriLen; i++) { if (i == oriLen) { if (wsCount == 1) { newText.append(lastWs); } if (wsCount > 1) { newText.append(' '); } wsCount = 0; break; } char c = oriText.charAt(i); if (Character.isWhitespace(c) || Character.isSpaceChar(c)) { wsCount++; lastWs = c; } else { if (wsCount == 1) { newText.append(lastWs); } if (wsCount > 1) { newText.append(' '); } wsCount = 0; newText.append(c); } } String relt = newText.toString(); return relt; }
From source file:org.regenstrief.hl7.util.HL7IO.java
public static int indexOf(final StringBuffer sb, final char ch, final int startat) { for (int i = startat; i < sb.length(); i++) { if (sb.charAt(i) == ch) { return i; }/*from w w w . java 2s. com*/ } return -1; }
From source file:com.niki.normalizer.Metaphone.java
private static boolean isPreviousChar(StringBuffer string, int index, char c) { boolean matches = false; if (index > 0 && index < string.length()) { matches = string.charAt(index - 1) == c; }/*www. j a va 2 s . c o m*/ return matches; }