List of usage examples for java.util.regex Matcher appendTail
public StringBuilder appendTail(StringBuilder sb)
From source file:com.salas.bb.utils.StringUtils.java
/** * Complete recoding of all HTML entities into Unicode symbols. * * @param str string./* w w w .ja va 2s. co m*/ * * @return result. */ public static String unescape(String str) { if (isEmpty(str)) return str; Pattern p = Pattern.compile("&(([^#;\\s]{3,6})|#([0-9]{1,4})|#x([0-9a-fA-F]{1,4}));"); Matcher m = p.matcher(str); StringBuffer sb = new StringBuffer(); while (m.find()) { Character c; String strEntity = m.group(2); String decEntity = m.group(3); String hexEntity = m.group(4); if (strEntity != null) { // String entity c = ENTITIES.get(strEntity); } else { c = decEntity != null ? (char) Integer.parseInt(decEntity) : (char) Integer.parseInt(hexEntity, 16); } m.appendReplacement(sb, c == null ? m.group() : c.toString()); } m.appendTail(sb); return sb.toString(); }
From source file:com.hichinaschool.flashcards.libanki.Utils.java
/** * Takes a string and replaces all the HTML symbols in it with their unescaped representation. * This should only affect substrings of the form &something; and not tags. * Internet rumour says that Html.fromHtml() doesn't cover all cases, but it doesn't get less * vague than that./*ww w . j av a 2s . co m*/ * @param html The HTML escaped text * @return The text with its HTML entities unescaped. */ private static String entsToTxt(String html) { // entitydefs defines nbsp as \xa0 instead of a standard space, so we // replace it first html = html.replace(" ", " "); Matcher htmlEntities = htmlEntitiesPattern.matcher(html); StringBuffer sb = new StringBuffer(); while (htmlEntities.find()) { htmlEntities.appendReplacement(sb, Html.fromHtml(htmlEntities.group()).toString()); } htmlEntities.appendTail(sb); return sb.toString(); }
From source file:de.mpg.mpdl.inge.citationmanager.utils.XsltHelper.java
/** * Converts snippet <span> tags to the HTML formatting, i.e. <code><b>, <i>, <u>, <s></code> * Text. Note: If at least one <span> css class will not match FontStyle css, the snippet * will be returned without any changes. * // w w w. j a va 2 s.c om * @param snippet * @return converted snippet * @throws CitationStyleManagerException */ public static String convertSnippetToHtml(String snippet) throws CitationStyleManagerException { FontStyle fs; // snippet = removeI18N(snippet); FontStylesCollection fsc = XmlHelper.loadFontStylesCollection(); if (!Utils.checkVal(snippet) || fsc == null) return snippet; // logger.info("passed str:" + snippet); StringBuffer sb = new StringBuffer(); Matcher m = SPANS_WITH_CLASS.matcher(snippet); while (m.find()) { String cssClass = m.group(1); fs = fsc.getFontStyleByCssClass(cssClass); // logger.info("fs:" + fs); // Rigorous: if at list once no css class has been found return str // as it is if (fs == null) { return snippet; } else { String str = "$2"; if (fs.getIsStrikeThrough()) { str = "<s>" + str + "</s>"; } if (fs.getIsUnderline()) { str = "<u>" + str + "</u>"; } if (fs.getIsItalic()) { str = "<i>" + str + "</i>"; } if (fs.getIsBold()) { str = "<b>" + str + "</b>"; } str = "<span class=\"" + cssClass + "\">" + str + "</span>"; m.appendReplacement(sb, str); } } snippet = m.appendTail(sb).toString(); return snippet; }
From source file:org.seedstack.w20.internal.MasterPageBuilder.java
/** * Replace ${...} placeholders in a string looking up in a replacement map. * * @param text the text to replace. * @param replacements the map of replacements. * @return the replaced text./* w w w . j a va 2s. c o m*/ */ private String replaceTokens(String text, Map<String, Object> replacements) { // TODO use a better solution for templating Pattern pattern = Pattern.compile("\\$\\{(.+?)\\}"); Matcher matcher = pattern.matcher(text); StringBuffer buffer = new StringBuffer(); while (matcher.find()) { Object replacement = replacements.get(matcher.group(1)); matcher.appendReplacement(buffer, ""); if (replacement != null) { buffer.append(replacement.toString()); } } matcher.appendTail(buffer); return buffer.toString(); }
From source file:de.mpg.escidoc.services.citationmanager.utils.XsltHelper.java
/** * Converts snippet <span> tags to the HTML formatting, * i.e. <code><b>, <i>, <u>, <s></code> * Text. Note: If at least one <span> css class will not match * FontStyle css, the snippet will be returned without any changes. * /*www .j a va 2s. c o m*/ * @param snippet * @return converted snippet * @throws CitationStyleManagerException */ public static String convertSnippetToHtml(String snippet) throws CitationStyleManagerException { FontStyle fs; // snippet = removeI18N(snippet); FontStylesCollection fsc = XmlHelper.loadFontStylesCollection(); if (!Utils.checkVal(snippet) || fsc == null) return snippet; // logger.info("passed str:" + snippet); StringBuffer sb = new StringBuffer(); Matcher m = SPANS_WITH_CLASS.matcher(snippet); while (m.find()) { String cssClass = m.group(1); fs = fsc.getFontStyleByCssClass(cssClass); // logger.info("fs:" + fs); // Rigorous: if at list once no css class has been found return str // as it is if (fs == null) { return snippet; } else { String str = "$2"; if (fs.getIsStrikeThrough()) { str = "<s>" + str + "</s>"; } if (fs.getIsUnderline()) { str = "<u>" + str + "</u>"; } if (fs.getIsItalic()) { str = "<i>" + str + "</i>"; } if (fs.getIsBold()) { str = "<b>" + str + "</b>"; } str = "<span class=\"" + cssClass + "\">" + str + "</span>"; m.appendReplacement(sb, str); } } snippet = m.appendTail(sb).toString(); return snippet; }
From source file:com.google.api.tools.framework.aspects.documentation.model.DocumentationUtil.java
/** * Given a string, searches for unqualified references to message fields and to method names and * converts them from RPC-style to REST-style. Field names are converted from lower_underscore to * lowerCamel. Method names are converted from VerbCollection-style to collection.verb style. * No work is done for qualified references; in such a case, an explicit markdown link with the * proper display text should be used in the proto comment (e.g., * [foo_bar][Path.To.Message.foo_bar], which will be converted to * [fooBar][Path.To.Message.foo_bar] by rpcToRest). *//*from w w w. j a v a 2 s . c o m*/ public static String rpcToRest(SymbolTable symbolTable, String description) { StringBuffer sb = new StringBuffer(); Matcher m = WORD.matcher(description); while (m.find()) { // Convert field names if (symbolTable.containsFieldName(m.group(0))) { m.appendReplacement(sb, CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, m.group(0))); // Convert method names } else if (symbolTable.lookupMethodSimpleName(m.group(0)) != null) { RestMethod restMethod = RestMethod .getPrimaryRestMethod(symbolTable.lookupMethodSimpleName(m.group(0)).get(0)); if (restMethod == null) { m.appendReplacement(sb, m.group(0)); } else { m.appendReplacement(sb, restMethod.getSimpleRestCollectionName() + "." + restMethod.getRestMethodName()); } } else { m.appendReplacement(sb, m.group(0)); } } m.appendTail(sb); return sb.toString(); }
From source file:dk.dma.msinm.common.time.TimeTranslator.java
/** * Translate the time description//from ww w . j a v a 2 s. co m * @param time the time description to translate * @param translateRules the translation rules to apply * @return the result */ public String translate(String time, Map<String, String> translateRules) throws TimeException { BufferedReader reader = new BufferedReader(new StringReader(time)); String line; StringBuilder result = new StringBuilder(); try { while ((line = reader.readLine()) != null) { //line = line.trim().toLowerCase(); // Replace according to replace rules for (String key : translateRules.keySet()) { String value = translateRules.get(key); Matcher m = Pattern.compile(key, Pattern.CASE_INSENSITIVE).matcher(line); StringBuffer sb = new StringBuffer(); while (m.find()) { String text = m.group(); m.appendReplacement(sb, value); } m.appendTail(sb); line = sb.toString(); } // Capitalize, unless the line starts with something like "a) xxx" if (!line.matches("\\w\\) .*")) { line = StringUtils.capitalize(line); } result.append(line + "\n"); } } catch (Exception e) { throw new TimeException("Failed translating time description", e); } return result.toString().trim(); }
From source file:com.vmware.o11n.plugin.powershell.model.generate.ScriptActionGenerator.java
private String extractParameters(String script, ScriptModuleBuilder builder) { // first escape the PS script script = script.replace("'", "\\'"); //Then look up for {#paramname#} and extract them Pattern pattern = Pattern.compile("\\{#([\\w]*)#\\}"); Matcher matcher = pattern.matcher(script); StringBuffer sb = new StringBuffer(); while (matcher.find()) { String inputName = matcher.group(1); // create input param builder.addParameter(inputName, "String"); matcher.appendReplacement(sb, Matcher.quoteReplacement("' + " + inputName + " + '")); }// w w w. j av a2s. co m matcher.appendTail(sb); return sb.toString(); }
From source file:org.opencms.search.galleries.CmsGalleryNameMacroResolver.java
/** * @see org.opencms.util.CmsMacroResolver#resolveMacros(java.lang.String) *//*from w ww. j a va2s . c o m*/ @Override public String resolveMacros(String input) { // We are overriding this method to implement the no_prefix macro. This is because // we only know what the no_prefix macro should expand to after resolving all other // macros (there could be an arbitrary number of macros before it which might potentially // all expand to the empty string). String result = super.resolveMacros(input); Matcher matcher = NO_PREFIX_PATTERN.matcher(result); if (matcher.find()) { StringBuffer resultBuffer = new StringBuffer(); matcher.appendReplacement(resultBuffer, matcher.start() == 0 ? "" : result.substring(matcher.start(1), matcher.end(1))); matcher.appendTail(resultBuffer); result = resultBuffer.toString(); } return result; }
From source file:AmazonKinesisAuditRecordProcessor.java
public void findFields() { String str = "[field1][field2][field3]field4"; Pattern pattern = Pattern.compile("\\[(.*?)\\]"); Matcher matcher = pattern.matcher(str); List<String> fields = new ArrayList<String>(); while (matcher.find()) { fields.add(matcher.group(1));/* www. j a v a 2 s.co m*/ } matcher.replaceAll(""); StringBuffer tail = new StringBuffer(); matcher.appendTail(tail); fields.add(tail.toString()); System.out.println(fields); }