List of usage examples for java.util.regex Matcher appendTail
public StringBuilder appendTail(StringBuilder sb)
From source file:com.aurel.track.exchange.docx.exporter.DocxExportBL.java
/** * Serializes the docx content into the response's output stream * @param response//from w ww .jav a 2 s . c o m * @param wordMLPackage * @return */ static String prepareReportResponse(HttpServletResponse response, String baseFileName, WordprocessingMLPackage wordMLPackage) { if (baseFileName == null) { baseFileName = "TrackReport"; } else { baseFileName = StringEscapeUtils.unescapeHtml4(baseFileName); StringBuffer sb = new StringBuffer(baseFileName.replaceAll("[^a-zA-Z0-9_]", "_")); Pattern regex = Pattern.compile("(_[a-z])"); Matcher regexMatcher = regex.matcher(sb); StringBuffer sb2 = new StringBuffer(); try { while (regexMatcher.find()) { regexMatcher.appendReplacement(sb2, regexMatcher.group(1).toUpperCase()); } regexMatcher.appendTail(sb2); } catch (Exception e) { LOGGER.error(e.getMessage()); } baseFileName = sb2.toString().replaceAll("_", ""); } response.reset(); response.setHeader("Content-Type", "application/vnd.openxmlformats-officedocument.wordprocessingml.document"); response.setHeader("Content-Disposition", "attachment; filename=\"" + baseFileName + ".docx\""); DownloadUtil.prepareCacheControlHeader(ServletActionContext.getRequest(), response); OutputStream outputStream = null; try { outputStream = response.getOutputStream(); } catch (IOException e) { LOGGER.error("Getting the output stream failed with " + e.getMessage()); LOGGER.error(ExceptionUtils.getStackTrace(e)); } try { Docx4J.save(wordMLPackage, outputStream, Docx4J.FLAG_NONE); /*SaveToZipFile saver = new SaveToZipFile(wordMLPackage); saver.save(outputStream);*/ } catch (Exception e) { LOGGER.error("Exporting the docx failed with throwable " + e.getMessage()); LOGGER.debug(ExceptionUtils.getStackTrace(e)); } return null; }
From source file:Main.java
/** * Substitutes links in the given text with valid HTML mark-up. For instance, * http://dhis2.org is replaced with <a href="http://dhis2.org">http://dhis2.org</a>, * and www.dhis2.org is replaced with <a href="http://dhis2.org">www.dhis2.org</a>. * * @param text the text to substitute links for. * @return the substituted text./*from w ww .j a va2 s .c o m*/ */ public static String htmlLinks(String text) { if (text == null || text.trim().isEmpty()) { return null; } Matcher matcher = LINK_PATTERN.matcher(text); StringBuffer buffer = new StringBuffer(); while (matcher.find()) { String url = matcher.group(1); String suffix = matcher.group(3); String ref = url.startsWith("www.") ? "http://" + url : url; url = "<a href=\"" + ref + "\">" + url + "</a>" + suffix; matcher.appendReplacement(buffer, url); } return matcher.appendTail(buffer).toString(); }
From source file:com.datumbox.framework.common.utilities.PHPMethods.java
/** * Matches a string with a pattern and replaces the matched components with * a provided string.// w w w.jav a 2s . c o m * * @param pattern * @param replacement * @param subject * @return */ public static String preg_replace(Pattern pattern, String replacement, String subject) { Matcher m = pattern.matcher(subject); StringBuffer sb = new StringBuffer(subject.length()); while (m.find()) { m.appendReplacement(sb, replacement); } m.appendTail(sb); return sb.toString(); }
From source file:framework.retrieval.engine.common.RetrievalUtil.java
/** * HTML?/* www . j a v a2 s . c o m*/ * @param htmlContent * @param charsetName * @return */ public static String parseHTML(String htmlContent, String charsetName) { if (null == htmlContent || "".equals(htmlContent.trim())) { return htmlContent; } StringBuffer txt = new StringBuffer(); Pattern pattern = Pattern.compile("<[^<|^>]*>", Pattern.CASE_INSENSITIVE); Matcher matcher = pattern.matcher(htmlContent); while (matcher.find()) { String group = matcher.group(); if (group.matches("<[\\s]*>")) { matcher.appendReplacement(txt, group); } else { matcher.appendReplacement(txt, ""); } } matcher.appendTail(txt); String str = txt.toString(); return str; }
From source file:org.energyos.espi.common.test.FixtureFactory.java
public static String newXML(UUID uuid, String fileName) throws IOException { String s = newXML(fileName);/*from w w w . j ava 2 s . c o m*/ Pattern pattern = Pattern.compile("<id>urn:uuid:([A-Z0-9-]+)</id>"); StringBuffer myStringBuffer = new StringBuffer(); Matcher matcher = pattern.matcher(s); int i = 0; while (matcher.find()) { UUID replacement; if (i == 1) { replacement = uuid; } else { replacement = UUID.randomUUID(); } matcher.appendReplacement(myStringBuffer, "<id>urn:uuid:" + replacement.toString().toUpperCase() + "</id>"); i++; } matcher.appendTail(myStringBuffer); String subscription = myStringBuffer.toString().replaceAll("9B6C7066", uuid.toString()); return subscription; }
From source file:org.b3log.symphony.util.MP3Players.java
/** * Renders the specified content with MP3 player if need. * * @param content the specified content/*w ww .ja v a2s . c o m*/ * @return rendered content */ public static final String render(final String content) { final StringBuffer contentBuilder = new StringBuffer(); final Matcher m = PATTERN.matcher(content); while (m.find()) { String mp3URL = m.group(); String mp3Name = StringUtils.substringBetween(mp3URL, "\">", ".mp3</a>"); mp3URL = StringUtils.substringBetween(mp3URL, "href=\"", "\" rel="); m.appendReplacement(contentBuilder, "<div class=\"aplayer content-audio\" data-title=\"" + mp3Name + "\" data-url=\"" + mp3URL + "\" ></div>\n"); } m.appendTail(contentBuilder); return contentBuilder.toString(); }
From source file:org.kitodo.production.plugin.opac.pica.ConfigOpacCatalogue.java
/** * The function fillIn() replaces marks in a given string by values derived * from match results. There are two different mechanisms available for * replacement./*from ww w .ja va2 s .c om*/ * * <p> * If the marked string contains the replacement mark <code>{\\@}</code>, * the matchers find() operation will be invoked over and over again and * all match results are concatenated and inserted in place of the * replacement marks. * </p> * * <p> * Otherwise, all replacement marks <code>{1}</code>, <code>{2}</code>, * <code>{3}</code>, will be replaced by the capturing groups matched by * the matcher. * </p> * * @param markedString * a string with replacement markers * @param matcher * a matcher whos values shall be inserted * @return the string with the replacements filled in * @throws IndexOutOfBoundsException * If there is no capturing group in the pattern with the given * index */ private static String fillIn(String markedString, Matcher matcher) { if (matcher == null) { return markedString; } if (markedString.contains("{@}")) { StringBuilder composer = new StringBuilder(); composer.append(matcher.group()); while (matcher.find()) { composer.append(matcher.group()); } return markedString.replaceAll("\\{@\\}", composer.toString()); } else { StringBuffer replaced = new StringBuffer(); Matcher replacer = Pattern.compile("\\{(\\d+)\\}").matcher(markedString); while (replacer.find()) { replacer.appendReplacement(replaced, matcher.group(Integer.parseInt(replacer.group(1)))); } replacer.appendTail(replaced); return replaced.toString(); } }
From source file:jetbrick.tools.chm.reader.AnchorNameManager.java
public static void addAnchor(File file, String encoding) throws IOException { String html = FileUtils.readFileToString(file, encoding); html = html.replace('$', '\uFFE5'); Pattern p = Config.style.getAnchorNameRegex(); Matcher m = p.matcher(html); int findCount = 0; StringBuffer sb = new StringBuffer(); while (m.find()) { findCount++;/* w ww . j a v a2 s .co m*/ String anchor = m.group(1); // String oldAnchor = m.group(); String oldAnchor = "<A HH=\"1\" NAME=\"" + anchor + "\">"; String newAnchor = "<A HH=\"1\" NAME=\"" + getNewAnchorName(anchor) + "\"></A>"; m.appendReplacement(sb, newAnchor + oldAnchor); } m.appendTail(sb); System.out.println("addAnchor(" + findCount + ") : " + file); if (findCount > 0) { html = sb.toString().replace('\uFFE5', '$'); FileUtils.writeStringToFile(file, html, encoding); } html = null; }
From source file:eu.nerdz.api.impl.fastreverse.messages.FastReverseConversationHandler.java
/** * Replaces a single tag.// w w w .ja v a2 s. co m * @param regex A regex * @param message A message to be parsed * @return A string in which all occurrences of regex have been substituted with the contents matched */ private static String replaceSingle(String regex, String message) { Matcher matcher = Pattern.compile(regex, Pattern.DOTALL | Pattern.CASE_INSENSITIVE).matcher(message); StringBuffer result = new StringBuffer(); while (matcher.find()) { matcher.appendReplacement(result, matcher.group(1).replace("$", "\\$")); } matcher.appendTail(result); return result.toString(); }
From source file:org.sakaiproject.kernel.util.ISO9075.java
/** * Decodes the <code>name</code>. * * @param name//from w w w . j a v a2 s. c om * the <code>String</code> to decode. * @return the decoded <code>String</code>. */ public static String decode(String name) { // quick check if (name.indexOf("_x") < 0) { // not encoded return name; } StringBuffer decoded = new StringBuffer(); Matcher m = ENCODE_PATTERN.matcher(name); while (m.find()) { char ch = (char) Integer.parseInt(m.group().substring(2, 6), 16); if (ch == '$' || ch == '\\') { m.appendReplacement(decoded, "\\" + ch); } else { m.appendReplacement(decoded, Character.toString(ch)); } } m.appendTail(decoded); return decoded.toString(); }