List of usage examples for java.util.regex Matcher appendTail
public StringBuilder appendTail(StringBuilder sb)
From source file:org.opencastproject.composer.impl.AbstractCmdlineEmbedderEngine.java
/** * Builds command list out of template command by substituting input parameters in form #{<parameter>} with * values from properties. If for some parameter there is no matching value, parameter is removed. Parameters that are * set via switches are represented as #{<switch> <key>}. Arrays of parameters are represented #< * parameters(s) >//from ww w. j a va2 s. c o m * * @param properties * map that contains key/values pairs for building command. Unused pairs are ignored. * @return built list that represents command */ protected List<String> buildCommandFromTemplate(Map<String, String> properties) { // add binary List<String> commandList = new LinkedList<String>(); commandList.add(binary); // process command line StringBuffer buffer = new StringBuffer(); // process array parameters Pattern pattern = Pattern.compile("#<.+?>"); Matcher matcher = pattern.matcher(cmdTemplate); while (matcher.find()) { String processedArray = buildArrayCommandFromTemplate( cmdTemplate.substring(matcher.start() + 2, matcher.end() - 1), properties); matcher.appendReplacement(buffer, processedArray); } matcher.appendTail(buffer); String arrayProcessedCmd = buffer.toString(); // process normal parameters buffer = new StringBuffer(); pattern = Pattern.compile("#\\{.+?\\}"); matcher = pattern.matcher(arrayProcessedCmd); while (matcher.find()) { String match = arrayProcessedCmd.substring(matcher.start() + 2, matcher.end() - 1); if (match.contains(" ")) { String value = properties.get(match.split(" ")[1].trim()); if (value == null) { matcher.appendReplacement(buffer, ""); } else { matcher.appendReplacement(buffer, match.split(" ")[0] + " " + value); } } else { String value = properties.get(match.trim()); if (value == null) { matcher.appendReplacement(buffer, ""); } else { matcher.appendReplacement(buffer, value); } } } matcher.appendTail(buffer); // split and convert to array list String[] cmdArray = buffer.toString().split(" "); for (String e : cmdArray) { if (!"".equals(e)) commandList.add(e); } return commandList; }
From source file:org.olat.modules.fo.archiver.formatters.ForumStreamedRTFFormatter.java
/** * /*from w w w . j a v a 2 s .c o m*/ * @param originalText * @return */ private String convertHTMLMarkupToRTF(String originalText) { String htmlText = originalText; Matcher mb = PATTERN_HTML_BOLD.matcher(htmlText); StringBuffer bolds = new StringBuffer(); while (mb.find()) { mb.appendReplacement(bolds, "{\\\\b $1} "); } mb.appendTail(bolds); htmlText = bolds.toString(); Matcher mi = PATTERN_HTML_ITALIC.matcher(htmlText); StringBuffer italics = new StringBuffer(); while (mi.find()) { mi.appendReplacement(italics, "{\\\\i $1} "); } mi.appendTail(italics); htmlText = italics.toString(); Matcher mbr = PATTERN_HTML_BREAK.matcher(htmlText); StringBuffer breaks = new StringBuffer(); while (mbr.find()) { mbr.appendReplacement(breaks, "\\\\line "); } mbr.appendTail(breaks); htmlText = breaks.toString(); Matcher mofo = PATTERN_CSS_O_FOQUOTE.matcher(htmlText); StringBuffer foquotes = new StringBuffer(); while (mofo.find()) { mofo.appendReplacement(foquotes, "\\\\line {\\\\i $1} {\\\\pard $2\\\\par}"); } mofo.appendTail(foquotes); htmlText = foquotes.toString(); Matcher mp = PATTERN_HTML_PARAGRAPH.matcher(htmlText); StringBuffer paragraphs = new StringBuffer(); while (mp.find()) { mp.appendReplacement(paragraphs, "\\\\line $1 \\\\line"); } mp.appendTail(paragraphs); htmlText = paragraphs.toString(); Matcher mahref = PATTERN_HTML_AHREF.matcher(htmlText); StringBuffer ahrefs = new StringBuffer(); while (mahref.find()) { mahref.appendReplacement(ahrefs, "{\\\\field{\\\\*\\\\fldinst{HYPERLINK\"$1\"}}{\\\\fldrslt{\\\\ul $2}}}"); } mahref.appendTail(ahrefs); htmlText = ahrefs.toString(); Matcher mli = PATTERN_HTML_LIST.matcher(htmlText); StringBuffer lists = new StringBuffer(); while (mli.find()) { mli.appendReplacement(lists, "$1\\\\line "); } mli.appendTail(lists); htmlText = lists.toString(); Matcher mtp = PATTERN_THREEPOINTS.matcher(htmlText); StringBuffer tps = new StringBuffer(); while (mtp.find()) { mtp.appendReplacement(tps, THREEPOINTS); } mtp.appendTail(tps); htmlText = tps.toString(); // strip all other html-fragments, because not convertable that easy htmlText = FilterFactory.getHtmlTagsFilter().filter(htmlText); // Remove all Matcher tmp = HTML_SPACE_PATTERN.matcher(htmlText); htmlText = tmp.replaceAll(" "); htmlText = StringEscapeUtils.unescapeHtml(htmlText); return htmlText; }
From source file:org.kuali.ole.docstore.service.IngestNIndexHandlerService_UT.java
@Ignore @Test//from ww w.j a v a 2s . c o m public final void testMarcIngestWithSpecialChars() { List<String> bibIds = new ArrayList<String>(); List<String> instanceIds = new ArrayList<String>(); try { URL resource = getClass().getResource("/org/kuali/ole/repository/request.xml"); File file = new File(resource.toURI()); String input = FileUtils.readFileToString(file); StringBuffer stringBuffer = new StringBuffer(); String regex = "Sandburg, Carl"; String replace = "San~X1< 9+&!5#%^,&*(2)>{6}[8]!?H"; Pattern pattern = Pattern.compile(regex); Matcher matcher = pattern.matcher(input); while (matcher.find()) { matcher.appendReplacement(stringBuffer, replace); } matcher.appendTail(stringBuffer); String inputFile = stringBuffer.toString(); RequestHandler rh = new RequestHandler(); Request request = rh.toObject(inputFile); Response response = ingestNIndexHandlerService.ingestNIndexRequestDocuments(request); for (ResponseDocument resDoc : response.getDocuments()) { bibIds.add(resDoc.getUuid()); for (ResponseDocument linkedDoc : resDoc.getLinkedDocuments()) { instanceIds.add(linkedDoc.getUuid()); } } LOG.info("Bib Ids:" + bibIds); LOG.info("Instance ids:" + instanceIds); CheckoutManager checkoutManager = new CheckoutManager(); for (String bibId : bibIds) { String checkedOutContent = checkoutManager.checkOut(bibId, "mockUser", "checkout"); LOG.info("Bib Id:" + bibId); LOG.info("checkedOutContent for Bibliographic " + checkedOutContent); } for (String instanceId : instanceIds) { String checkedOutContent = checkoutManager.checkOut(instanceId, "mockUser", "checkout"); LOG.info("Instance Id:" + instanceId); LOG.info("checkedOutContent fro Instance " + checkedOutContent); } } catch (Exception e) { LOG.info(e.getMessage(), e); } }
From source file:h2weibo.utils.filters.TcoStatusFilter.java
public String filter(String input) { // Create a pattern to match cat Pattern p = Pattern.compile("http://t.co/\\w+"); // Create a matcher with an input string Matcher m = p.matcher(input); StringBuffer sb = new StringBuffer(); boolean result = m.find(); // Loop through and create a new String with the replacements while (result) { String tcoUrl = m.group(); try {//from w w w . jav a 2 s .c om String redirectUrl = getRedirectUrl(tcoUrl); if (redirectUrl != null) m.appendReplacement(sb, redirectUrl); } catch (IOException e) { // do nothing, skip } result = m.find(); } // Add the last segment of input to the new String m.appendTail(sb); return sb.toString(); }
From source file:com.ejushang.steward.common.genericdao.dao.hibernate.GeneralDAO.java
/** * ql?order by?//ww w. ja v a2 s . c o m * @param ql ? * @return ?? */ private String removeOrderBy(String ql) { if (ql != null && !"".equals(ql)) { Matcher m = removeOrderByPattern.matcher(ql); StringBuffer sb = new StringBuffer(); while (m.find()) { m.appendReplacement(sb, ""); } m.appendTail(sb); return sb.toString(); } return ""; }
From source file:org.eclim.plugin.jdt.command.doc.GetElementDocCommand.java
@Override public Object execute(CommandLine commandLine) throws Exception { IJavaElement[] elements = null;//from w w w . ja v a 2 s . c om Object url = commandLine.getRawValue(Options.URL_OPTION); if (url != null) { IJavaElement target = JavaElementLinks.parseURI(new URI((String) url)); if (target != null) { elements = new IJavaElement[] { target }; } } else { String project = commandLine.getValue(Options.PROJECT_OPTION); String file = commandLine.getValue(Options.FILE_OPTION); int offset = getOffset(commandLine); int length = commandLine.getIntValue(Options.LENGTH_OPTION); ICompilationUnit src = JavaUtils.getCompilationUnit(project, file); elements = src.codeSelect(offset, length); } if (elements == null || elements.length == 0) { return null; } Method getHoverInfo = JavadocHover.class.getDeclaredMethod("getHoverInfo", IJavaElement[].class, ITypeRoot.class, IRegion.class, JavadocBrowserInformationControlInput.class); getHoverInfo.setAccessible(true); JavadocBrowserInformationControlInput info = (JavadocBrowserInformationControlInput) getHoverInfo .invoke(null, elements, null, null, null); if (info == null) { return null; } if (commandLine.hasOption(Options.HTML_OPTION)) { return info.getHtml(); } ArrayList<HashMap<String, String>> links = new ArrayList<HashMap<String, String>>(); Matcher matcher = LINKS.matcher(info.getHtml()); StringBuffer buffer = new StringBuffer(); int index = 0; while (matcher.find()) { String href = matcher.group(2); String text = matcher.group(3); HashMap<String, String> link = new HashMap<String, String>(); link.put("href", href); link.put("text", text); links.add(link); matcher.appendReplacement(buffer, "|$3[" + (index++) + "]|"); } matcher.appendTail(buffer); String html = buffer.toString(); String result = IOUtils.toString(new HTML2TextReader( new StringReader(html != null ? html : StringUtils.EMPTY), new TextPresentation())); // remove \r for windows result = result.replaceAll("\r", ""); // compact excessive spacing between sig and doc. result = result.replaceFirst("\n\n\n", "\n\n"); HashMap<String, Object> response = new HashMap<String, Object>(); response.put("text", result); response.put("links", links); return response; }
From source file:CssCompressor.java
public void compress(Writer out, int linebreakpos) throws IOException { Pattern p;/*from w w w. j a v a 2 s . com*/ Matcher m; String css; StringBuffer sb; int startIndex, endIndex; // Remove all comment blocks... startIndex = 0; boolean iemac = false; boolean preserve = false; sb = new StringBuffer(srcsb.toString()); while ((startIndex = sb.indexOf("/*", startIndex)) >= 0) { preserve = sb.length() > startIndex + 2 && sb.charAt(startIndex + 2) == '!'; endIndex = sb.indexOf("*/", startIndex + 2); if (endIndex < 0) { if (!preserve) { sb.delete(startIndex, sb.length()); } } else if (endIndex >= startIndex + 2) { if (sb.charAt(endIndex - 1) == '\\') { // Looks like a comment to hide rules from IE Mac. // Leave this comment, and the following one, alone... startIndex = endIndex + 2; iemac = true; } else if (iemac) { startIndex = endIndex + 2; iemac = false; } else if (!preserve) { sb.delete(startIndex, endIndex + 2); } else { startIndex = endIndex + 2; } } } css = sb.toString(); // Normalize all whitespace strings to single spaces. Easier to work // with that way. css = css.replaceAll("\\s+", " "); // Make a pseudo class for the Box Model Hack css = css.replaceAll("\"\\\\\"}\\\\\"\"", "___PSEUDOCLASSBMH___"); // ---------where zk modify it sb = new StringBuffer(); p = Pattern.compile("\\$\\{([^\\}]+)\\}"); Matcher m1 = p.matcher(css); while (m1.find()) { String s1 = m1.group(); s1 = s1.replaceAll("\\$\\{", "__EL__"); s1 = s1.replaceAll(":", "__ELSP__"); s1 = s1.replaceAll("\\}", "__ELEND__"); m1.appendReplacement(sb, s1); } m1.appendTail(sb); css = sb.toString(); // ---------where zk modify it----end // Remove the spaces before the things that should not have spaces // before them. // But, be careful not to turn "p :link {...}" into "p:link{...}" // Swap out any pseudo-class colons with the token, and then swap back. sb = new StringBuffer(); p = Pattern.compile("(^|\\})(([^\\{:])+:)+([^\\{]*\\{)"); m = p.matcher(css); while (m.find()) { String s = m.group(); s = s.replaceAll(":", "___PSEUDOCLASSCOLON___"); m.appendReplacement(sb, s); } m.appendTail(sb); css = sb.toString(); css = css.replaceAll("\\s+([!{};:>+\\(\\)\\],])", "$1"); css = css.replaceAll("___PSEUDOCLASSCOLON___", ":"); // Remove the spaces after the things that should not have spaces after // them. css = css.replaceAll("([!{}:;>+\\(\\[,])\\s+", "$1"); // Add the semicolon where it's missing. css = css.replaceAll("([^;\\}])}", "$1;}"); // Replace 0(px,em,%) with 0. css = css.replaceAll("([\\s:])(0)(px|em|%|in|cm|mm|pc|pt|ex)", "$1$2"); // Replace 0 0 0 0; with 0. css = css.replaceAll(":0 0 0 0;", ":0;"); css = css.replaceAll(":0 0 0;", ":0;"); css = css.replaceAll(":0 0;", ":0;"); // Replace background-position:0; with background-position:0 0; css = css.replaceAll("background-position:0;", "background-position:0 0;"); // Replace 0.6 to .6, but only when preceded by : or a white-space css = css.replaceAll("(:|\\s)0+\\.(\\d+)", "$1.$2"); // Shorten colors from rgb(51,102,153) to #336699 // This makes it more likely that it'll get further compressed in the // next step. p = Pattern.compile("rgb\\s*\\(\\s*([0-9,\\s]+)\\s*\\)"); m = p.matcher(css); sb = new StringBuffer(); while (m.find()) { String[] rgbcolors = m.group(1).split(","); StringBuffer hexcolor = new StringBuffer("#"); for (int i = 0; i < rgbcolors.length; i++) { int val = Integer.parseInt(rgbcolors[i]); if (val < 16) { hexcolor.append("0"); } hexcolor.append(Integer.toHexString(val)); } m.appendReplacement(sb, hexcolor.toString()); } m.appendTail(sb); css = sb.toString(); // Shorten colors from #AABBCC to #ABC. Note that we want to make sure // the color is not preceded by either ", " or =. Indeed, the property // filter: chroma(color="#FFFFFF"); // would become // filter: chroma(color="#FFF"); // which makes the filter break in IE. p = Pattern.compile( "([^\"'=\\s])(\\s*)#([0-9a-fA-F])([0-9a-fA-F])([0-9a-fA-F])([0-9a-fA-F])([0-9a-fA-F])([0-9a-fA-F])"); m = p.matcher(css); sb = new StringBuffer(); while (m.find()) { // Test for AABBCC pattern if (m.group(3).equalsIgnoreCase(m.group(4)) && m.group(5).equalsIgnoreCase(m.group(6)) && m.group(7).equalsIgnoreCase(m.group(8))) { m.appendReplacement(sb, m.group(1) + m.group(2) + "#" + m.group(3) + m.group(5) + m.group(7)); } else { m.appendReplacement(sb, m.group()); } } m.appendTail(sb); css = sb.toString(); // Remove empty rules. css = css.replaceAll("[^\\}]+\\{;\\}", ""); if (linebreakpos >= 0) { // Some source control tools don't like it when files containing // lines longer // than, say 8000 characters, are checked in. The linebreak option // is used in // that case to split long lines after a specific column. int i = 0; int linestartpos = 0; sb = new StringBuffer(css); while (i < sb.length()) { char c = sb.charAt(i++); if (c == '}' && i - linestartpos > linebreakpos) { sb.insert(i, '\n'); linestartpos = i; } } css = sb.toString(); } // Replace the pseudo class for the Box Model Hack css = css.replaceAll("___PSEUDOCLASSBMH___", "\"\\\\\"}\\\\\"\""); // Replace multiple semi-colons in a row by a single one // See SF bug #1980989 css = css.replaceAll(";;+", ";"); // ---------where zk modify it css = css.replaceAll("__EL__", "\\$\\{"); css = css.replaceAll("__ELSP__", ":"); css = css.replaceAll("__ELEND__", "\\}"); // ---------where zk modify it----end // Trim the final string (for any leading or trailing white spaces) css = css.trim(); // Write the output... out.write(css); }
From source file:org.fao.unredd.portal.Config.java
public String getLocalizedFileContents(File file, Locale locale) throws IOException, ConfigurationException { try {//from w w w . j a v a2s .c o m BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file)); String template = IOUtils.toString(bis, "UTF-8"); bis.close(); Pattern patt = Pattern.compile("\\$\\{([\\w.]*)\\}"); Matcher m = patt.matcher(template); StringBuffer sb = new StringBuffer(template.length()); ResourceBundle messages = getMessages(locale); while (m.find()) { String text; try { text = messages.getString(m.group(1)); m.appendReplacement(sb, text); } catch (MissingResourceException e) { // do not replace } } m.appendTail(sb); return sb.toString(); } catch (UnsupportedEncodingException e) { logger.error("Unsupported encoding", e); return ""; } }
From source file:edu.umd.cs.findbugs.util.Strings.java
/** * Unescape XML entities and illegal characters in the given string. This * enhances the functionality of//from w ww . ja va2 s .co m * org.apache.commons.lang.StringEscapeUtils.unescapeXml by unescaping * low-valued unprintable characters, which are not permitted by the W3C XML * 1.0 specification. * * @param s * a string * @return the same string with XML entities/escape sequences unescaped * @see <a href="http://www.w3.org/TR/REC-xml/#charsets">Extensible Markup * Language (XML) 1.0 (Fifth Edition)</a> * @see <a * href="http://commons.apache.org/lang/api/org/apache/commons/lang/StringEscapeUtils.html#unescapeXml(java.lang.String)">org.apache.commons.lang.StringEscapeUtils * javadoc</a> */ public static String unescapeXml(String s) { initializeEscapeMap(); /* * we can't escape the string if the pattern doesn't compile! (but that * should never happen since the pattern is static) */ if (!initializeUnescapePattern()) { return s; } if (s == null || s.length() == 0) { return s; } /* * skip this expensive check entirely if there are no substrings * resembling Unicode escape sequences in the string to be unescaped */ if (s.contains("\\u")) { StringBuffer sUnescaped = new StringBuffer(); Matcher m = unescapePattern.matcher(s); while (m.find() == true) { String slashes = m.group(1); String digits = m.group(3); int escapeCode; try { escapeCode = Integer.parseInt(digits, 16); } catch (NumberFormatException nfe) { /* * the static regular expression string should guarantee * that this exception is never thrown */ System.err.println("Impossible error: escape sequence '" + digits + "' is not a valid hex number! " + "Exception: " + nfe.toString()); return s; } if (slashes != null && slashes.length() % 2 == 0 && isInvalidXMLCharacter(escapeCode)) { Character escapedSequence = Character.valueOf((char) escapeCode); /* * slashes are apparently escaped when the string buffer is * converted to a string, so double them to make sure the * correct number appear in the final representation */ m.appendReplacement(sUnescaped, slashes + slashes + escapedSequence.toString()); } } m.appendTail(sUnescaped); s = sUnescaped.toString(); } return StringEscapeUtils.unescapeXml(s); }
From source file:info.magnolia.link.LinkUtil.java
/** * Parses provided html and transforms all the links to the magnolia format. Used during storing. * @param html html code with links to be converted * @return html with changed hrefs//from w w w. j av a 2 s. com */ public static String convertAbsoluteLinksToUUIDs(String html) { // get all link tags Matcher matcher = LINK_OR_IMAGE_PATTERN.matcher(html); StringBuffer res = new StringBuffer(); while (matcher.find()) { final String href = matcher.group(4); if (!isExternalLinkOrAnchor(href)) { try { Link link = parseLink(href); String linkStr = toPattern(link); linkStr = StringUtils.replace(linkStr, "\\", "\\\\"); linkStr = StringUtils.replace(linkStr, "$", "\\$"); matcher.appendReplacement(res, "$1" + linkStr + "$5"); } catch (LinkException e) { // this is expected if the link is an absolute path to something else // than content stored in the repository matcher.appendReplacement(res, "$0"); log.debug("can't parse link", e); } } else { matcher.appendReplacement(res, "$0"); } } matcher.appendTail(res); return res.toString(); }