Example usage for java.util.regex Matcher appendTail

List of usage examples for java.util.regex Matcher appendTail

Introduction

In this page you can find the example usage for java.util.regex Matcher appendTail.

Prototype

public StringBuilder appendTail(StringBuilder sb) 

Source Link

Document

Implements a terminal append-and-replace step.

Usage

From source file:org.encuestame.core.util.HTMLInputFilter.java

protected String decodeEntities(String string) {
    StringBuffer buf = new StringBuffer();

    Pattern pattern = Pattern.compile("&#(\\d+);?");
    Matcher matcher = pattern.matcher(string);
    while (matcher.find()) {
        String match = matcher.group(1);
        int decimal = Integer.decode(match).intValue();
        matcher.appendReplacement(buf, chr(decimal));
    }// w  w  w .j  a  v a2 s .com
    matcher.appendTail(buf);
    string = buf.toString();

    buf = new StringBuffer();
    pattern = Pattern.compile("&#x([0-9a-f]+);?");
    matcher = pattern.matcher(string);
    while (matcher.find()) {
        String match = matcher.group(1);
        int decimal = Integer.decode(match).intValue();
        matcher.appendReplacement(buf, chr(decimal));
    }
    matcher.appendTail(buf);
    string = buf.toString();

    buf = new StringBuffer();
    pattern = Pattern.compile("%([0-9a-f]{2});?");
    matcher = pattern.matcher(string);
    while (matcher.find()) {
        String match = matcher.group(1);
        int decimal = Integer.decode(match).intValue();
        matcher.appendReplacement(buf, chr(decimal));
    }
    matcher.appendTail(buf);
    string = buf.toString();

    string = validateEntities(string);
    return string;
}

From source file:org.encuestame.core.util.HTMLInputFilter.java

protected String escapeComments(String string) {
    final Pattern pattern = Pattern.compile("<!--(.*?)-->", Pattern.DOTALL);
    final Matcher matcher = pattern.matcher(string);
    StringBuffer buf = new StringBuffer();
    if (matcher.find()) {
        String match = matcher.group(1); // (.*?)
        matcher.appendReplacement(buf, "<!--" + htmlSpecialChars(match) + "-->");
    }/*from  w  ww.  j  a va2  s  . co m*/
    matcher.appendTail(buf);

    return buf.toString();
}

From source file:com.gewara.util.XSSFilter.java

protected String decodeEntities(String s) {
    StringBuffer buf = new StringBuffer();

    Pattern p = Pattern.compile("&#(\\d+);?");
    Matcher m = p.matcher(s);
    while (m.find()) {
        String match = m.group(1);
        int decimal = Integer.decode(match).intValue();
        m.appendReplacement(buf, chr(decimal));
    }//w w w  .  j  a v a  2s .c  om
    m.appendTail(buf);
    s = buf.toString();

    buf = new StringBuffer();
    p = Pattern.compile("&#x([0-9a-f]+);?");
    m = p.matcher(s);
    while (m.find()) {
        String match = m.group(1);
        int decimal = Integer.decode(match).intValue();
        m.appendReplacement(buf, chr(decimal));
    }
    m.appendTail(buf);
    s = buf.toString();

    buf = new StringBuffer();
    p = Pattern.compile("%([0-9a-f]{2});?");
    m = p.matcher(s);
    while (m.find()) {
        String match = m.group(1);
        int decimal = Integer.decode(match).intValue();
        m.appendReplacement(buf, chr(decimal));
    }
    m.appendTail(buf);
    s = buf.toString();

    s = validateEntities(s);
    return s;
}

From source file:com.norconex.commons.lang.url.URLNormalizer.java

/**
 * Converts letters in URL-encoded escape sequences to upper case.<p>
 * <code>http://www.example.com/a%c2%b1b &rarr; 
 *       http://www.example.com/a%C2%B1b</code>
 * @return this instance/*  ww  w  . j a  v  a  2  s.  c  o m*/
 */
public URLNormalizer upperCaseEscapeSequence() {
    if (url.contains("%")) {
        StringBuffer sb = new StringBuffer();
        Matcher m = PATTERN_PERCENT_ENCODED_CHAR.matcher(url);
        while (m.find()) {
            m.appendReplacement(sb, m.group(1).toUpperCase());
        }
        url = m.appendTail(sb).toString();
    }
    return this;
}

From source file:org.ballerinalang.composer.server.launcher.log.LogManagerUtils.java

/**
 * Substituting environment variables./*from  www  .  j ava 2s.c o  m*/
 * @param value The value to be replaced
 * @return The updated value.
 */
private String substituteVariables(String value) {
    Matcher matcher = ENV_VAR_PATTERN.matcher(value);
    boolean found = matcher.find();
    if (!found) {
        return value;
    }
    StringBuffer buffer = new StringBuffer();
    do {
        String sysPropertyKey = matcher.group(1);
        String sysPropertyValue = getSystemVariableValue(sysPropertyKey);
        if (sysPropertyValue != null && !sysPropertyValue.isEmpty()) {
            sysPropertyValue = sysPropertyValue.replace("\\", "\\\\");
            matcher.appendReplacement(buffer, sysPropertyValue);
        }
    } while (matcher.find());
    matcher.appendTail(buffer);
    return buffer.toString();
}

From source file:com.gs.obevo.db.apps.reveng.AquaRevengMain.java

private MutableList<File> preprocessSchemaTokens(MutableList<File> files, String dbSchema,
        final File interimFolder, DbPlatform dbPlatform) {
    String schemaSeparatorRegex = dbPlatform.getSchemaSeparator().replace(".", "\\.");
    if (schemaSeparatorRegex.equals("\\.\\.")) {
        schemaSeparatorRegex = "\\.(?:dbo)?\\."; // adding DBO to help w/ Sybase ASE; we should make this code more polymorphic
    }/*from   w w w  .ja v  a 2 s . c o  m*/
    final Pattern dbSchemaPattern = Pattern
            .compile(String.format("(?i)%1$s%2$s(\\w+)", dbSchema, schemaSeparatorRegex));
    return files.collect(new Function<File, File>() {
        @Override
        public File valueOf(File file) {
            String fileContent = FileUtilsCobra.readFileToString(file);
            final Matcher matcher = dbSchemaPattern.matcher(fileContent);
            StringBuffer sb = new StringBuffer(fileContent.length());

            while (matcher.find()) {
                matcher.appendReplacement(sb, matcher.group(1));
            }
            matcher.appendTail(sb);

            File tempFile = new File(interimFolder, file.getName());
            FileUtilsCobra.writeStringToFile(tempFile, sb.toString());
            return tempFile;
        }
    });
}

From source file:org.commonjava.maven.galley.maven.parse.XMLInfrastructure.java

private String escapeNonXMLEntityRefs(final String xml) {
    final Matcher m = Pattern.compile("&([^\\s;]+;)").matcher(xml);

    final StringBuffer sb = new StringBuffer();
    while (m.find()) {
        String value = m.group();
        if (!XML_ENTITIES.contains(value)) {
            value = "&amp;" + m.group(1);
        }/*ww w.jav a  2  s  .co  m*/

        m.appendReplacement(sb, value);
    }

    m.appendTail(sb);

    return sb.toString();
}

From source file:org.ourbeehive.mbp.util.StringHelper.java

/**
 * Remove non-eligible characters in phase. Please note that the order of regular expressions in this method could not be changed.
 * //from ww  w.  j  av a2 s .c o  m
 * @param phase
 * @return
 */
public static String removeNonEligibleChar(String phase) {

    String result = phase;

    // Replace invalid characters with " ".
    Pattern pattern = Pattern.compile("[-]|['\"_,%?./&():]");
    Matcher matcher = pattern.matcher(result);
    result = matcher.replaceAll(" ");

    // Add "_" before number if number is the first character.
    pattern = Pattern.compile("^\\d");
    matcher = pattern.matcher(result);
    if (matcher.find()) {
        result = "_" + result;
    }

    // Replace "#" followed by number.
    pattern = Pattern.compile("#[ ]*[0-9]");
    Pattern subPat = Pattern.compile("#[ ]*");
    matcher = pattern.matcher(result);
    String currMat = null;
    Matcher subMat = null;
    StringBuffer sb = new StringBuffer();
    int start, end = 0;
    while (matcher.find()) {
        start = matcher.start();
        end = matcher.end();
        currMat = result.substring(start, end);
        subMat = subPat.matcher(currMat);
        matcher.appendReplacement(sb, subMat.replaceAll(""));
    }
    matcher.appendTail(sb);

    // Replace "#" followed by character with " Number ".
    pattern = Pattern.compile("#");
    matcher = pattern.matcher(result);
    result = matcher.replaceAll(" Number ");

    // Replace "Y/N" with " ".
    pattern = Pattern.compile("Y/N", Pattern.CASE_INSENSITIVE);
    matcher = pattern.matcher(result);
    result = matcher.replaceAll(" ");

    // Replace "A/C" with "Account".
    pattern = Pattern.compile("A/C", Pattern.CASE_INSENSITIVE);
    matcher = pattern.matcher(result);
    result = matcher.replaceAll("Acount");

    // Replace "DDMMYYYY" and "DDMMYY" with " ".
    pattern = Pattern.compile("DDMMY*", Pattern.CASE_INSENSITIVE);
    matcher = pattern.matcher(result);
    result = matcher.replaceAll(" ");

    // Replace "HHMMSS" with " ".
    pattern = Pattern.compile("HHMMSS", Pattern.CASE_INSENSITIVE);
    matcher = pattern.matcher(result);
    result = matcher.replaceAll(" ");

    // Remove extra white space.
    pattern = Pattern.compile("[ ]+");
    matcher = pattern.matcher(result);
    result = matcher.replaceAll(" ");

    return result.trim().replace("*", " ");

}

From source file:com.dtolabs.rundeck.core.dispatcher.DataContextUtils.java

/**
 * Replace the embedded  properties of the form '${key.name}' in the input Strings with the value from the data
 * context/*from  www . j a  v  a2s  . c  om*/
 *
 *
 * @param input input string
 * @param data  data context map
 *              @param converter converter to encode/convert the expanded values
 *
 * @param failOnUnexpanded
 * @return string with values substituted, or original string
 */
public static String replaceDataReferences(final String input, final Map<String, Map<String, String>> data,
        final Converter<String, String> converter, boolean failOnUnexpanded, boolean blankIfUnexpanded) {
    if (null == data) {
        return input;
    }
    final Pattern p = Pattern.compile(PROPERTY_REF_REGEX);
    final Matcher m = p.matcher(input);
    final StringBuffer sb = new StringBuffer();
    while (m.find()) {
        final String key = m.group(1);
        final String nm = m.group(2);
        if (null != key && null != nm && null != data.get(key) && null != data.get(key).get(nm)) {
            String value = data.get(key).get(nm);
            if (null != converter) {
                value = converter.convert(value);
            }
            m.appendReplacement(sb, Matcher.quoteReplacement(value));
        } else if (failOnUnexpanded && null != key && null != nm
                && (null == data.get(key) || null == data.get(key).get(nm))) {
            throw new UnresolvedDataReferenceException(input, m.group());
        } else if (blankIfUnexpanded) {
            m.appendReplacement(sb, "");
        } else {
            String value = m.group(0);
            if (null != converter) {
                value = converter.convert(value);
            }
            m.appendReplacement(sb, Matcher.quoteReplacement(value));
        }
    }
    m.appendTail(sb);
    return sb.toString();
}

From source file:com.gewara.util.XSSFilter.java

protected String checkTags(String s) {
    Pattern p = Pattern.compile("<(.*?)>", Pattern.DOTALL);
    Matcher m = p.matcher(s);

    StringBuffer buf = new StringBuffer();
    while (m.find()) {
        String replaceStr = m.group(1);
        replaceStr = processTag(replaceStr);
        m.appendReplacement(buf, replaceStr);
    }//  w  w  w.j  a va2s  .c  o m
    m.appendTail(buf);

    s = buf.toString();

    // these get tallied in processTag
    // (remember to reset before subsequent calls to filter method)
    for (String key : vTagCounts.keySet()) {
        for (int ii = 0; ii < vTagCounts.get(key); ii++) {
            s += "</" + key + ">";
        }
    }

    return s;
}