List of usage examples for java.util.regex Matcher appendTail
public StringBuilder appendTail(StringBuilder sb)
From source file:org.mule.transport.ldap.LdapConnector.java
public String parseQuery(final String query, final Object[] values) { logger.debug(query);// w ww . j av a 2 s . com logger.debug(Arrays.asList(values).toString()); if (query == null) { return query; } final Matcher m = STATEMENT_ARGS.matcher(query); final StringBuffer sb = new StringBuffer(200); int i = 0; while (m.find()) { m.appendReplacement(sb, values[i] == null ? "null" : values[i].toString()); i++; } m.appendTail(sb); return sb.toString(); }
From source file:com.agimatec.validation.jsr303.DefaultMessageInterpolator.java
private String replaceAnnotationAttributes(String message, Map<String, Object> annotationParameters) { Matcher matcher = messageParameterPattern.matcher(message); StringBuffer sb = new StringBuffer(); while (matcher.find()) { String resolvedParameterValue; String parameter = matcher.group(1); Object variable = annotationParameters.get(removeCurlyBrace(parameter)); if (variable != null) { if (variable.getClass().isArray()) { resolvedParameterValue = ArrayUtils.toString(variable); } else { resolvedParameterValue = variable.toString(); }/* w ww . j a v a 2 s. c o m*/ } else { resolvedParameterValue = message; } matcher.appendReplacement(sb, resolvedParameterValue); } matcher.appendTail(sb); return sb.toString(); }
From source file:org.rakam.client.builder.document.SlateDocumentGenerator.java
public void generateTo(InputStream template, OutputStream dest) throws IOException { this.templates = generateExampleUsages(); try (BufferedReader reader = new BufferedReader(new InputStreamReader(template)); PrintWriter writer = new PrintWriter(new OutputStreamWriter(dest))) { String line;/*from w ww .j a va2 s . co m*/ while ((line = reader.readLine()) != null) { Matcher matcher = PLACEHOLDER_PATTERN.matcher(line); StringBuffer buffer = new StringBuffer(); while (matcher.find()) { String key = matcher.group(1); System.out.println("replacing:" + key); matcher.appendReplacement(buffer, generateContent(key)); } matcher.appendTail(buffer); writer.println(buffer); } } catch (Exception e) { System.out.println("failed generating slate:" + e); e.printStackTrace(); } }
From source file:com.microsoft.tfs.client.common.ui.helpers.HTMLIncludeHelper.java
/** * <p>//from w ww .j ava 2 s .c o m * Transforms the given line, including referenced localized messages by * delegating to the {@link HTMLIncludeResourceProvider}. * </p> * * <p> * Lines with text matching: %%%MESSAGE(key)%%% will be transformed by * replacing the line with the results of the * {@link HTMLIncludeResourceProvider}'s response to the given key. * * <p> * Example: * </p> * * <p> * %%%MESSAGE(ClassName.MessageKey)%%% will include the given message for * the key ClassName.MessageKey. * </p> * * @param input * An input line from a resource * @return The line with any message statements transformed. * @throws IOException * If any included resources could not be read */ private String transformMessages(final String input) throws IOException { Check.notNull(input, "input"); //$NON-NLS-1$ final Matcher messageMatcher = messagePattern.matcher(input); final StringBuffer transformation = new StringBuffer(); while (messageMatcher.find()) { String replacement = ""; //$NON-NLS-1$ if (messageMatcher.groupCount() == 1) { replacement = resourceProvider.getMessage(messageMatcher.group(1)); } else { log.warn(MessageFormat.format("Could not transform message constant {0}: no messages class defined", //$NON-NLS-1$ messageMatcher.group(0))); } messageMatcher.appendReplacement(transformation, replacement); } messageMatcher.appendTail(transformation); return transformation.toString(); }
From source file:org.gvnix.jpa.geo.hibernatespatial.util.EWKTReader.java
/** * Removes all whitespace between Geometry type and <code>M</code> as this * parser expects. </br> Example: POINT M (111.1 111.1 5) ==> POINTM (111.1 * 111.1 5)//from ww w .j a v a 2 s . c o m * * @param wkt * @return */ private String fixMNames(String wkt) { Pattern p = Pattern.compile("(\\s*M\\s*[(])"); Matcher m = p.matcher(wkt); StringBuffer s = new StringBuffer(); while (m.find()) { m.appendReplacement(s, "M ("); } m.appendTail(s); return s.toString(); }
From source file:org.finra.dm.service.helper.Hive13DdlGenerator.java
/** * Escapes single quote characters, if not already escaped, with an extra backslash. * * @param string the input text// w ww. j a v a 2 s . c o m * * @return the output text with all single quote characters escaped by an extra backslash */ public String escapeSingleQuotes(String string) { Pattern pattern = Pattern.compile("(?<!\\\\)(')"); Matcher matcher = pattern.matcher(string); StringBuffer stringBuffer = new StringBuffer(); while (matcher.find()) { matcher.appendReplacement(stringBuffer, matcher.group(1).replace("'", "\\\\'")); } matcher.appendTail(stringBuffer); return stringBuffer.toString(); }
From source file:au.edu.anu.portal.portlets.tweetal.servlet.TweetalServlet.java
private String replaceLinks(String statusText) { Pattern p = Pattern.compile("http[s]{0,1}://[A-Za-z0-9\\._\\-~#/]+"); Matcher m = p.matcher(statusText); // if we have a match anywhere in the string StringBuffer buf = new StringBuffer(); while (m.find()) { //replace that portion that matched, with the link String matched = m.group(); m.appendReplacement(buf, String.format("<a target=\"_blank\" href=\"%s\">%s</a>", matched, matched)); }//from w ww .j a v a 2s . c o m m.appendTail(buf); return buf.toString(); }
From source file:info.magnolia.cms.gui.dialog.DialogFckEdit.java
/** * @param value//from ww w . jav a2 s. c o m * @return */ private String convertToView(String value) { String tmp = value; if (tmp != null) { tmp = tmp.replaceAll("\r\n", "<br />"); //$NON-NLS-1$ //$NON-NLS-2$ tmp = tmp.replaceAll("\n", "<br />"); //$NON-NLS-1$ //$NON-NLS-2$ value = LinkUtil.convertUUIDsToAbsoluteLinks(value); Pattern imagePattern = Pattern.compile("(<(a|img)[^>]+(src|href)[ ]*=[ ]*\")([^\"]*)(\"[^>]*>)"); //$NON-NLS-1$ Matcher matcher = imagePattern.matcher(value); StringBuffer res = new StringBuffer(); while (matcher.find()) { String src = matcher.group(4); // process only internal and relative links if (!Pattern.matches("^\\w*://.*", src) && !src.startsWith("/")) { String link = this.getRequest().getContextPath() + this.getTopParent().getConfigValue("path") + "/" + StringUtils.substringAfter(src, "/"); matcher.appendReplacement(res, "$1" + link + "$5"); //$NON-NLS-1$ } } matcher.appendTail(res); return res.toString(); } return StringUtils.EMPTY; }
From source file:org.projectforge.scripting.GroovyEngine.java
private String replaceIncludes(final String template) { if (template == null) { return null; }/*from w w w . j a v a 2s . c o m*/ final Pattern p = Pattern.compile("#INCLUDE\\{([0-9\\.a-zA-Z/]*)\\}", Pattern.MULTILINE); final StringBuffer buf = new StringBuffer(); final Matcher m = p.matcher(template); while (m.find()) { if (m.group(1) != null) { final String filename = m.group(1); final Object[] res = ConfigXml.getInstance().getContent(filename); String content = (String) res[0]; if (content != null) { content = replaceIncludes(content).replaceAll("\\$", "#HURZ#"); m.appendReplacement(buf, content); // Doesn't work with $ in content } else { m.appendReplacement(buf, "*** " + filename + " not found! ***"); } } } m.appendTail(buf); return buf.toString(); }
From source file:org.cesecore.configuration.ConfigurationTestHolder.java
private String interpolate(final String orderString) { final Pattern PATTERN = Pattern.compile("\\$\\{(.+?)\\}"); final Matcher m = PATTERN.matcher(orderString); final StringBuffer sb = new StringBuffer(orderString.length()); m.reset();/*from w ww . j a va 2s . c o m*/ while (m.find()) { // when the pattern is ${identifier}, group 0 is 'identifier' final String key = m.group(1); final String value = getExpandedString(key, ""); // if the pattern does exists, replace it by its value // otherwise keep the pattern ( it is group(0) ) if (value != null) { m.appendReplacement(sb, value); } else { // I'm doing this to avoid the backreference problem as there will be a $ // if I replace directly with the group 0 (which is also a pattern) m.appendReplacement(sb, ""); final String unknown = m.group(0); sb.append(unknown); } } m.appendTail(sb); return sb.toString(); }