List of usage examples for java.lang StringBuilder replace
@Override public StringBuilder replace(int start, int end, String str)
From source file:com.github.abel533.mapperhelper.EntityHelper.java
/** * ?/*www . ja v a2 s . com*/ */ public static String underlineToCamelhump(String str) { Matcher matcher = Pattern.compile("_[a-z]").matcher(str); StringBuilder builder = new StringBuilder(str); for (int i = 0; matcher.find(); i++) { builder.replace(matcher.start() - i, matcher.end() - i, matcher.group().substring(1).toUpperCase()); } if (Character.isUpperCase(builder.charAt(0))) { builder.replace(0, 1, String.valueOf(Character.toLowerCase(builder.charAt(0)))); } return builder.toString(); }
From source file:org.jfrog.hudson.util.ExtractorUtils.java
private static String publicGitUrl(String gitUrl) { if (gitUrl != null && gitUrl.contains("https://") && gitUrl.contains("@")) { StringBuilder sb = new StringBuilder(gitUrl); int start = sb.indexOf("https://"); int end = sb.indexOf("@") + 1; sb = sb.replace(start, end, StringUtils.EMPTY); return "https://" + sb.toString(); }//from w w w . j a v a 2 s . c o m return gitUrl; }
From source file:DOMTreeTest.java
public static String characterString(CharacterData node) { StringBuilder builder = new StringBuilder(node.getData()); for (int i = 0; i < builder.length(); i++) { if (builder.charAt(i) == '\r') { builder.replace(i, i + 1, "\\r"); i++;//from w w w .j a v a2 s .c om } else if (builder.charAt(i) == '\n') { builder.replace(i, i + 1, "\\n"); i++; } else if (builder.charAt(i) == '\t') { builder.replace(i, i + 1, "\\t"); i++; } } if (node instanceof CDATASection) builder.insert(0, "CDATASection: "); else if (node instanceof Text) builder.insert(0, "Text: "); else if (node instanceof Comment) builder.insert(0, "Comment: "); return builder.toString(); }
From source file:com.jroossien.boxx.util.Str.java
/** * Wrap the specified string to multiple lines by adding a newline symbol '\n' * <p/>/*from w ww .j a va2s. com*/ * <p>This does not break up words. * Which means, if there is a word that is longer than the wrap limit it will exceed the limit. * * @param string The string that needs to be wrapped. * @param length The maximum length for each line. * @return String with linebreaks. */ public static String wrapString(String string, int length) { StringBuilder sb = new StringBuilder(string); int i = 0; while ((i = sb.indexOf(" ", i + length)) != -1) { sb.replace(i, i + 1, "\n"); } return sb.toString(); }
From source file:org.jumpmind.metl.core.runtime.component.ModelAttributeScriptHelper.java
public static String[] getSignatures() { List<String> signatures = new ArrayList<String>(); Method[] methods = ModelAttributeScriptHelper.class.getMethods(); LocalVariableTableParameterNameDiscoverer discoverer = new LocalVariableTableParameterNameDiscoverer(); for (Method method : methods) { if (method.getDeclaringClass().equals(ModelAttributeScriptHelper.class) && Modifier.isPublic(method.getModifiers()) && !Modifier.isStatic(method.getModifiers())) { StringBuilder sig = new StringBuilder(method.getName()); sig.append("("); String[] names = discoverer.getParameterNames(method); for (String name : names) { sig.append(name);// ww w .j av a 2 s.c o m sig.append(","); } if (names.length > 0) { sig.replace(sig.length() - 1, sig.length(), ")"); } else { sig.append(")"); } signatures.add(sig.toString()); } } Collections.sort(signatures); return signatures.toArray(new String[signatures.size()]); }
From source file:org.w3.i18n.I18nTestRunnerTest.java
private static String toString(List<Assertion> assertions) { // Will look like: "[[charset_meta, INFO, [context]], ... ]". StringBuilder sb = new StringBuilder("["); for (Assertion assertion : assertions) { sb.append("[").append(assertion.getId()).append(", ").append(assertion.getLevel()).append(", ") .append(assertion.getContexts()).append("], "); }//from w w w.j a va2 s. com if (!assertions.isEmpty()) { sb.replace(sb.length() - 2, sb.length() - 1, "]"); } else { sb.append("]"); } return sb.toString(); }
From source file:com.tvh.gmaildrafter.Drafter.java
private static void composeMail(Credentials credentials, String subjectText, String bodyText, String[] attachments, String[] attachmentnames, String[] destinations, String[] cc, String[] bcc, Boolean sendImmediately) throws IOException, AuthenticationFailedException { if (subjectText == null) { subjectText = ""; }//w w w .ja v a 2s .c o m if (bodyText == null) { bodyText = ""; } try { Properties props = null; Session session = null; if (!sendImmediately) { props = System.getProperties(); props.setProperty("mail.store.protocol", "imaps"); session = Session.getDefaultInstance(props, null); } else { props = new Properties(); props.put("mail.smtp.host", "smtp.gmail.com"); props.put("mail.smtp.socketFactory.port", "465"); props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.port", "465"); final String username = credentials.getUsername(); final String password = credentials.getPassword(); session = Session.getInstance(props, new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username, password); } }); } String signature = Signature.getSignature(credentials); if (signature == null) signature = ""; // Create the message Message draftMail = new MimeMessage(session); draftMail.setSubject(subjectText); Multipart parts = new MimeMultipart(); BodyPart body = new MimeBodyPart(); if (bodyText.toLowerCase().indexOf("<body") < 0) // rough guess to see if the body is html { bodyText = "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"></head><body>" + StringEscapeUtils.escapeHtml(bodyText).replace("\n", "<br />" + "\n") + "<br>" + "</body></html>"; } if (signature != null && signature != "") { StringBuilder b = new StringBuilder(bodyText); if (signature.indexOf("</") < 0) // assume it's html if there's no </, rough guess { signature = StringEscapeUtils.escapeHtml(signature); } b.replace(bodyText.lastIndexOf("</body>"), bodyText.lastIndexOf("</body>") + 7, "<br>" + signature + "</body>"); bodyText = b.toString(); } body.setContent(bodyText, "text/html; charset=utf-8"); body.setDisposition("inline"); parts.addBodyPart(body); if (attachments != null) { for (int i = 0; i < attachments.length; i++) { BodyPart attachment = new MimeBodyPart(); DataSource source = new FileDataSource(attachments[i]); attachment.setDataHandler(new DataHandler(source)); if (attachmentnames != null && attachmentnames.length > i) { attachment.setFileName(attachmentnames[i]); } else { File file = new File(attachments[i]); attachment.setFileName(file.getName()); } parts.addBodyPart(attachment); } } draftMail.setContent(parts); if (destinations != null && destinations.length > 0) draftMail.setRecipients(Message.RecipientType.TO, stringToInternetAddress(destinations)); if (cc != null && cc.length > 0) draftMail.setRecipients(Message.RecipientType.CC, stringToInternetAddress(cc)); if (bcc != null && bcc.length > 0) draftMail.setRecipients(Message.RecipientType.BCC, stringToInternetAddress(bcc)); draftMail.setFlag(Flags.Flag.SEEN, true); if (sendImmediately) { Transport.send(draftMail); } else { URLName url = new URLName("imaps://imap.gmail.com"); IMAPSSLStore store = new IMAPSSLStore(session, url); store.connect(credentials.getUsername(), credentials.getPassword()); Folder[] f = store.getDefaultFolder().xlist("*"); long threadId = 0; for (Folder fd : f) { IMAPFolder folder = (IMAPFolder) fd; boolean thisIsDrafts = false; String atts[] = folder.getAttributes(); for (String a : atts) { if (a.equalsIgnoreCase("\\Drafts")) { thisIsDrafts = true; break; } } if (thisIsDrafts) { folder.open(Folder.READ_WRITE); Message[] messages = new Message[1]; messages[0] = draftMail; folder.appendMessages(messages); /* * Determine the Google Message Id, needed to open it in the * browser. Because we just created the message it is * reasonable to assume it is the last message in the draft * folder. If this turns out not to be the case we could * start creating the message with a random unique dummy * subject, find it using that subject and then modify the * subject to what it was supposed to be. */ messages[0] = folder.getMessage(folder.getMessageCount()); FetchProfile fp = new FetchProfile(); fp.add(IMAPFolder.FetchProfileItem.X_GM_THRID); folder.fetch(messages, fp); IMAPMessage googleMessage = (IMAPMessage) messages[0]; threadId = googleMessage.getGoogleMessageThreadId(); folder.close(false); } } if (threadId == 0) { System.exit(6); } store.close(); // Open the message in the default browser Runtime rt = Runtime.getRuntime(); String drafturl = "https://mail.google.com/mail/#drafts/" + Long.toHexString(threadId); File chrome = new File("C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe"); if (!chrome.exists()) chrome = new File("C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe"); if (!chrome.exists()) { // Chrome not found, using default browser rt.exec("rundll32 url.dll,FileProtocolHandler " + drafturl); } else { String[] commandLine = new String[2]; commandLine[0] = chrome.getPath(); commandLine[1] = drafturl; rt.exec(commandLine); } } // else branch for sendImmediately } catch (NoSuchProviderException e) { e.printStackTrace(); System.exit(4); } catch (AuthenticationFailedException e) { throw (e); } catch (MessagingException e) { e.printStackTrace(); System.exit(5); } }
From source file:grails.plugin.searchable.internal.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//w w w . j a v a 2s. 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); StringBuilder diff = new StringBuilder(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:fr.landel.utils.io.FileUtils.java
/** * Convert all newline characters into Unix newlines. * //from w ww.j av a 2 s. c o m * @param input * The text to convert * @return The text converted */ public static StringBuilder convertToUnix(final StringBuilder input) { final StringBuilder output = new StringBuilder(input); int pos = 0; while ((pos = output.indexOf(LFCR, pos)) > -1) { output.replace(pos, pos + 2, L); pos++; } pos = 0; while ((pos = output.indexOf(CRLF, pos)) > -1) { output.replace(pos, pos + 2, L); pos++; } pos = 0; while ((pos = output.indexOf(C, pos)) > -1) { output.replace(pos, pos + 1, L); pos++; } return output; }
From source file:fr.landel.utils.io.FileUtils.java
/** * Convert all newline characters into Windows newlines. * // w w w .j a v a2s . c o m * @param input * The text to convert * @return The text converted */ public static StringBuilder convertToWindows(final StringBuilder input) { final StringBuilder output = new StringBuilder(input); int pos = 0; while ((pos = output.indexOf(LFCR, pos)) > -1) { output.replace(pos, pos + 2, CRLF); pos++; } pos = 0; while ((pos = output.indexOf(L, pos)) > -1) { if (pos == 0 || output.charAt(pos - 1) != CR) { output.replace(pos, pos + 1, CRLF); } pos++; } pos = 0; final int len = output.length(); while ((pos = output.indexOf(C, pos)) > -1) { if (pos == len - 1 || output.charAt(pos + 1) != LF) { output.replace(pos, pos + 1, CRLF); } pos++; } return output; }