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:com.googlecode.osde.internal.builders.GadgetBuilder.java

private void compileGadgetSpec(IFile source, IFile target, IProject project, IProgressMonitor monitor)
        throws CoreException, ParserException, UnsupportedEncodingException {
    Parser<Module> parser = ParserFactory.gadgetSpecParser();

    InputStream fileContent = source.getContents();
    Module module;/*from  w w  w  .  j  a v a  2 s . com*/
    try {
        module = parser.parse(fileContent);
    } finally {
        IOUtils.closeQuietly(fileContent);
    }

    List<Content> contents = module.getContent();
    Random rnd = new Random();
    for (Content content : contents) {
        if (ViewType.html.toString().equals(content.getType())) {
            String value = content.getValue();
            Pattern pattern = Pattern.compile("http://localhost:[0-9]+/" + project.getName()
                    + "/[-_.!~*\\'()a-zA-Z0-9;\\/?:\\@&=+\\$,%#]+\\.js");
            Matcher matcher = pattern.matcher(value);
            StringBuffer sb = new StringBuffer();
            while (matcher.find()) {
                matcher.appendReplacement(sb,
                        value.substring(matcher.start(), matcher.end()) + "?rnd=" + Math.abs(rnd.nextInt()));
            }
            matcher.appendTail(sb);
            content.setValue(sb.toString());
        }
    }
    String serialized = GadgetXmlSerializer.serialize(module);
    ByteArrayInputStream content = new ByteArrayInputStream(serialized.getBytes("UTF-8"));
    target.create(content, IResource.DERIVED | IResource.FORCE, monitor);
}

From source file:aurelienribon.utils.TemplateManager.java

/**
 * Processes the variables over the given string, and returns the result.
 *///from www.j  a v a2 s.  com
public String process(String input) {
    for (String var : replacements.keySet()) {
        input = input.replaceAll("@\\{" + var + "\\}", replacements.get(var));
    }

    {
        Pattern p = Pattern.compile("@\\{ifdef (" + varPattern + ")\\}(.*?)@\\{endif\\}", Pattern.DOTALL);
        Matcher m = p.matcher(input);
        StringBuffer sb = new StringBuffer();

        while (m.find()) {
            String var = m.group(1);
            String content = m.group(2);
            if (replacements.containsKey(var))
                m.appendReplacement(sb, content);
            else
                m.appendReplacement(sb, "");
        }

        m.appendTail(sb);
        input = sb.toString();
    }

    {
        Pattern p = Pattern.compile("@\\{ifndef (" + varPattern + ")\\}(.*?)@\\{endif\\}", Pattern.DOTALL);
        Matcher m = p.matcher(input);
        StringBuffer sb = new StringBuffer();

        while (m.find()) {
            String var = m.group(1);
            String content = m.group(2);
            if (!replacements.containsKey(var))
                m.appendReplacement(sb, content);
            else
                m.appendReplacement(sb, "");
        }

        m.appendTail(sb);
        input = sb.toString();
    }

    return input;
}

From source file:org.lockss.util.UrlUtil.java

/** Remove a subdomain from the host part of a URL
 * @param url the URL string/* w w w .j  a  v a  2 s  .  c o  m*/
 * @param subdomain the (case insensitive) subdomain to remove (no
 * trailing dot)
 * @return the URL string with the subdomain removed from the beginning
 * of the host
 */
public static String delSubDomain(String url, String subdomain) {
    Matcher m = SCHEME_HOST_PAT.matcher(url);
    if (m.find()) {
        String host = m.group(2);
        if (StringUtil.startsWithIgnoreCase(host, subdomain) && '.' == host.charAt(subdomain.length())) {
            StringBuffer sb = new StringBuffer();
            m.appendReplacement(sb, "$1" + host.substring(subdomain.length() + 1));
            m.appendTail(sb);
            return sb.toString();
        }
    }
    return url;
}

From source file:uk.ac.ox.oucs.vle.XcriOxCapPopulatorImpl.java

/**
 *  * Processing of descriptivetext fields where descriptiveTextType.isXhtml=false
 * //from w  w  w  .ja v a 2s  .  com
 * @param data
 * @return
 */
static String parse(String data) {

    data = data.replaceAll("<", "&lt;");
    data = data.replaceAll(">", "&gt;");
    data = FormattedText.convertPlaintextToFormattedText(data);

    Pattern pattern = Pattern.compile("[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,4}", Pattern.CASE_INSENSITIVE);
    Matcher matcher = pattern.matcher(data);

    StringBuffer sb = new StringBuffer(data.length());
    while (matcher.find()) {
        String text = matcher.group(0);
        matcher.appendReplacement(sb, "<a class=\"email\" href=\"mailto:" + text + "\">" + text + "</a>");
    }
    matcher.appendTail(sb);

    pattern = Pattern.compile("(https?|ftps?):\\/\\/[a-z_0-9\\\\\\-]+(\\.([\\w#!:?+=&%@!\\-\\/])+)+",
            Pattern.CASE_INSENSITIVE);
    matcher = pattern.matcher(sb.toString());

    sb = new StringBuffer(data.length());
    while (matcher.find()) {
        String text = matcher.group(0);
        matcher.appendReplacement(sb,
                "<a class=\"url\" href=\"" + text + "\" target=\"_blank\">" + text + "</a>");
    }
    matcher.appendTail(sb);
    return sb.toString();
}

From source file:com.gargoylesoftware.htmlunit.javascript.IEConditionalCompilationScriptPreProcessor.java

private String processSet(final String body) {
    final Matcher m = SET_PATTERN.matcher(body);
    final StringBuffer sb = new StringBuffer();
    while (m.find()) {
        setVariables_.add(m.group(1));/*from  w  ww. j  ava 2  s  . c  om*/
        m.appendReplacement(sb, CC_VARIABLE_PREFIX + m.group(1).substring(1) + m.group(2) + ";");
    }
    m.appendTail(sb);
    return sb.toString();
}

From source file:org.jbosson.plugins.amq.ArtemisMBeanDiscoveryComponent.java

private String substituteConfigProperties(String objectName, Configuration configuration, boolean isParent) {

    final Pattern p;
    if (isParent) {
        p = PROPERTY_NAME_PATTERN;//from w w  w  . j  av  a  2s.c o m
    } else {
        p = OBJECT_NAME_PROPERTY_PATTERN;
    }

    StringBuffer buffer = new StringBuffer();
    final Matcher m = p.matcher(objectName);
    while (m.find()) {
        String name = m.group(1);
        m.appendReplacement(buffer, configuration.getSimpleValue(name));
    }
    m.appendTail(buffer);
    return buffer.toString();
}

From source file:net.firejack.platform.installation.processor.TemplateApplicationProcessor.java

public InputStream modify(InputStream stream, TemplateEvent event) throws IOException {
    String xml = IOUtils.toString(stream);
    IOUtils.closeQuietly(stream);//w  w  w  . jav a 2s . co m

    Pattern pattern = Pattern.compile("package\\s+path\\s*=\\s*\"(.*?)\"\\s+name\\s*=\\s*\"(.*?)\"");

    Matcher matcher = pattern.matcher(xml);
    StringBuffer output = new StringBuffer();
    while (matcher.find()) {
        event.setLookup(DiffUtils.lookup(matcher.group(1), matcher.group(2)));
        matcher.appendReplacement(output,
                "package path=\"" + event.getPath() + "\" name=\"" + event.getName() + "\"");
    }
    matcher.appendTail(output);

    pattern = Pattern.compile("uid\\s*=\\s*\"(.*?)\"");
    matcher = pattern.matcher(output);

    output = new StringBuffer();
    while (matcher.find()) {
        matcher.appendReplacement(output, "uid=\"" + SecurityHelper.generateSecureId() + "\"");
    }
    matcher.appendTail(output);

    xml = output.toString().replaceAll(event.getLookup(), DiffUtils.lookup(event.getPath(), event.getName()));
    return IOUtils.toInputStream(xml);
}

From source file:com.impetus.kundera.utils.KunderaCoreUtils.java

/**
 * Resolves variable in path given as string
 * /*from  w ww.  ja  va 2 s. c  om*/
 * @param input
 *            String input url Code inspired by
 *            :http://stackoverflow.com/questions/2263929/
 *            regarding-application-properties-file-and-environment-variable
 */
public static String resolvePath(String input) {
    if (null == input) {
        return input;
    }

    // matching for 2 groups match ${VAR_NAME} or $VAR_NAME
    Pattern pathPattern = Pattern.compile("\\$\\{(.+?)\\}");
    Matcher matcherPattern = pathPattern.matcher(input); // get a matcher
                                                         // object
    StringBuffer sb = new StringBuffer();
    EnvironmentConfiguration config = new EnvironmentConfiguration();
    SystemConfiguration sysConfig = new SystemConfiguration();

    while (matcherPattern.find()) {

        String confVarName = matcherPattern.group(1) != null ? matcherPattern.group(1)
                : matcherPattern.group(2);
        String envConfVarValue = config.getString(confVarName);
        String sysVarValue = sysConfig.getString(confVarName);

        if (envConfVarValue != null) {

            matcherPattern.appendReplacement(sb, envConfVarValue);

        } else if (sysVarValue != null) {

            matcherPattern.appendReplacement(sb, sysVarValue);

        } else {
            matcherPattern.appendReplacement(sb, "");
        }
    }
    matcherPattern.appendTail(sb);
    return sb.toString();
}

From source file:org.eclipse.virgo.kernel.osgi.provisioning.tools.DependencyLocator10.java

private static String expandProperties(String value) {
    Pattern regex = PROPERTY_PATTERN;
    StringBuffer buffer = new StringBuffer(value.length());
    Matcher matcher = regex.matcher(value);
    int propertyGroup = matcher.groupCount();
    String key, property = "";
    while (matcher.find()) {
        key = matcher.group(propertyGroup);
        property = "";
        if (key.contains("::")) {
            String[] keyDefault = key.split("::");
            property = System.getProperty(keyDefault[0]);
            if (property == null) {
                property = keyDefault[1];
            } else {
                property = property.replace('\\', '/');
            }/*from  ww  w .jav  a  2s . c  o  m*/
        } else {
            property = System.getProperty(matcher.group(propertyGroup)).replace('\\', '/');
        }
        matcher.appendReplacement(buffer, property);
    }
    matcher.appendTail(buffer);
    return buffer.toString();
}

From source file:org.pentaho.cdf.context.autoinclude.DashboardMatchRule.java

private Pattern replaceTokens(Matcher cdaMatcher, String regex) {
    // replace $1 for group 1 in regex etc
    Matcher token = REPLACE_TOKEN.matcher(regex);
    StringBuffer sb = new StringBuffer();
    while (token.find()) {
        int group = Integer.parseInt(token.group(1));
        if (group < cdaMatcher.groupCount()) {
            token.appendReplacement(sb, Matcher.quoteReplacement(Pattern.quote(cdaMatcher.group(group))));
        } else {/*from ww w .j  a  v a  2  s  . c  o  m*/
            log.error(String.format("Error processing rule '%s', group %d does not exist.", regex, group));
        }
    }
    token.appendTail(sb);
    return Pattern.compile(sb.toString());
}