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.adobe.acs.commons.rewriter.impl.StaticReferenceRewriteTransformerFactory.java

private String handleMatchingPatternAttribute(Pattern pattern, String attrValue) {
    String unescapedValue = StringEscapeUtils.unescapeHtml(attrValue);
    Matcher m = pattern.matcher(unescapedValue);
    StringBuffer sb = new StringBuffer(unescapedValue.length());

    while (m.find()) {
        String url = m.group(1);/*  w w w  . ja  v a  2  s.  c om*/
        for (String prefix : prefixes) {
            if (url.startsWith(prefix)) {
                // prepend host
                url = prependHostName(url);
                m.appendReplacement(sb, Matcher.quoteReplacement(url));
                // First prefix match wins
                break;
            }
        }

    }
    m.appendTail(sb);

    return sb.toString();
}

From source file:com.qumoon.commons.web.HTMLInputChecker.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();
        appendReplacement(m, buf, HTMLInputChecker.chr(decimal));
    }//from ww  w.j  av a  2s . com
    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();
        appendReplacement(m, buf, HTMLInputChecker.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();
        appendReplacement(m, buf, HTMLInputChecker.chr(decimal));
    }
    m.appendTail(buf);
    s = buf.toString();

    s = validateEntities(s);
    return s;
}

From source file:org.rhq.plugins.jbossas.script.ScriptComponent.java

private String replacePropertyPatterns(String envVars) {
    Pattern pattern = Pattern.compile("(%([^%]*)%)");
    Matcher matcher = pattern.matcher(envVars);
    Configuration parentPluginConfig = this.resourceContext.getParentResourceComponent()
            .getPluginConfiguration();/*from  w  w  w .j a  va 2  s.co m*/
    StringBuffer buffer = new StringBuffer();
    while (matcher.find()) {
        String propName = matcher.group(2);
        PropertySimple prop = parentPluginConfig.getSimple(propName);
        String propPattern = matcher.group(1);
        String replacement = (prop != null) ? prop.getStringValue() : propPattern;
        matcher.appendReplacement(buffer, Matcher.quoteReplacement(replacement));
    }

    matcher.appendTail(buffer);
    return buffer.toString();
}

From source file:org.freeplane.features.export.mindmapmode.ExportWithXSLT.java

String getProperty(final String key) {
    final String property = getProperty(key, null);
    if (property == null)
        return property;
    Matcher r = propertyReferenceEpression.matcher(property);
    r.reset();/*ww w  . java2 s .  c o  m*/
    boolean result = r.find();
    if (result) {
        StringBuffer sb = new StringBuffer();
        do {
            String propertyReference = r.group();
            String propertyName = propertyReference.substring(2, propertyReference.length() - 1);
            r.appendReplacement(sb, System.getProperty(propertyName, propertyReference));
            result = r.find();
        } while (result);
        r.appendTail(sb);
        return sb.toString();
    }
    return property;
}

From source file:com.haulmont.cuba.gui.components.filter.edit.CustomConditionFrame.java

protected String replaceParamWithQuestionMark(String where) {
    String res = StringUtils.trim(where);
    if (!StringUtils.isBlank(res)) {
        Matcher matcher = QueryParserRegex.PARAM_PATTERN.matcher(res);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            if (!matcher.group().startsWith(":session$"))
                matcher.appendReplacement(sb, "?");
        }/*  w  w w.  ja v  a 2s  .  c o m*/
        matcher.appendTail(sb);
        return sb.toString();
    }
    return res;
}

From source file:org.echocat.jomon.resources.optimizing.MakeCssResourcePathAbsoluteOptimizer.java

@Nonnull
protected String handleCssBody(@Nonnull String oldCssBody, @Nonnull String uri,
        @Nonnull OptimizationContext context) throws Exception {
    final Matcher matcher = PATTERN.matcher(oldCssBody);
    final StringBuffer sb = new StringBuffer();
    while (matcher.find()) {
        final String oldMatch = matcher.group();
        final String oldUri = matcher.group(3) != null ? matcher.group(3) : matcher.group(2);
        final String uriContent = matcher.group(1) != null ? matcher.group(1) : oldUri;

        final String newUri = newUri(uri, oldUri, context);
        if (newUri != null && !oldUri.equals(newUri)) {
            final String newReplacement = Matcher.quoteReplacement(oldMatch.replace(uriContent, newUri));
            matcher.appendReplacement(sb, newReplacement);
        }//from  w  ww.  j  av a  2s.  com
    }
    matcher.appendTail(sb);
    return sb.toString();
}

From source file:io.hops.hopsworks.api.jupyter.URITemplateProxyServlet.java

@Override
protected void service(HttpServletRequest servletRequest, HttpServletResponse servletResponse)
        throws ServletException, IOException {

    //First collect params
    /*/*from   w  w  w  .  j  a  va  2s. c om*/
     * Do not use servletRequest.getParameter(arg) because that will
     * typically read and consume the servlet InputStream (where our
     * form data is stored for POST). We need the InputStream later on.
     * So we'll parse the query string ourselves. A side benefit is
     * we can keep the proxy parameters in the query string and not
     * have to add them to a URL encoded form attachment.
     */
    String queryString = "?" + servletRequest.getQueryString();//no "?" but might have "#"
    int hash = queryString.indexOf('#');
    if (hash >= 0) {
        queryString = queryString.substring(0, hash);
    }
    List<NameValuePair> pairs;
    try {
        //note: HttpClient 4.2 lets you parse the string without building the URI
        pairs = URLEncodedUtils.parse(new URI(queryString), "UTF-8");
    } catch (URISyntaxException e) {
        throw new ServletException("Unexpected URI parsing error on " + queryString, e);
    }
    LinkedHashMap<String, String> params = new LinkedHashMap<>();
    for (NameValuePair pair : pairs) {
        params.put(pair.getName(), pair.getValue());
    }

    //Now rewrite the URL
    StringBuffer urlBuf = new StringBuffer();//note: StringBuilder isn't supported by Matcher
    Matcher matcher = TEMPLATE_PATTERN.matcher(targetUriTemplate);
    while (matcher.find()) {
        String arg = matcher.group(1);
        String replacement = params.remove(arg);//note we remove
        if (replacement != null) {
            matcher.appendReplacement(urlBuf, replacement);
            port = replacement;
        } else if (port != null) {
            matcher.appendReplacement(urlBuf, port);
        } else {
            throw new ServletException("Missing HTTP parameter " + arg + " to fill the template");
        }
    }
    matcher.appendTail(urlBuf);
    String newTargetUri = urlBuf.toString();
    servletRequest.setAttribute(ATTR_TARGET_URI, newTargetUri);
    try {
        targetUriObj = new URI(newTargetUri);
    } catch (Exception e) {
        throw new ServletException("Rewritten targetUri is invalid: " + newTargetUri, e);
    }
    servletRequest.setAttribute(ATTR_TARGET_HOST, URIUtils.extractHost(targetUriObj));

    //Determine the new query string based on removing the used names
    StringBuilder newQueryBuf = new StringBuilder(queryString.length());
    for (Map.Entry<String, String> nameVal : params.entrySet()) {
        if (newQueryBuf.length() > 0) {
            newQueryBuf.append('&');
        }
        newQueryBuf.append(nameVal.getKey()).append('=');
        if (nameVal.getValue() != null) {
            newQueryBuf.append(nameVal.getValue());
        }
    }
    servletRequest.setAttribute(ATTR_QUERY_STRING, newQueryBuf.toString());

    // Create Exchange object with targetUriObj
    // create transport object
    //    ServiceProxy sp = new ServiceProxy();
    //    sp.setTargetUrl(ATTR_TARGET_URI);
    //    super.service(servletRequest, servletResponse);

    //              RouterUtil.initializeRoutersFromSpringWebContext(appCtx, config.
    //              getServletContext(), getProxiesXmlLocation(config));
}

From source file:org.pentaho.platform.plugin.services.metadata.SessionAwareRowLevelSecurityHelper.java

@Override
protected String expandFunctions(String formula, String user, List<String> roles) {
    formula = super.expandFunctions(formula, user, roles);

    // "expand" any SESSION('var')

    IPentahoSession session = PentahoSessionHolder.getSession();

    Pattern p = Pattern.compile("SESSION\\(\"(.*?)\"\\)"); //$NON-NLS-1$
    Matcher m = p.matcher(formula);
    StringBuffer sb = new StringBuffer(formula.length());
    while (m.find()) {
        String text = m.group(1);
        String value = null;//from  w ww  .  j  a va 2  s  . com
        if (session.getAttribute(text) != null) {
            value = session.getAttribute(text).toString();
        } else {
            logger.warn(Messages.getInstance()
                    .getString("SessionAwareRowLevelSecurityHelper.WARN_0001_NULL_ATTRIBUTE", text, user)); //$NON-NLS-1$
            return "FALSE()"; //$NON-NLS-1$
        }
        // escape string if necessary (double quote quotes)
        m.appendReplacement(sb, Matcher.quoteReplacement(value.replaceAll("\"", "\"\""))); //$NON-NLS-1$ //$NON-NLS-2$
    }
    m.appendTail(sb);
    return sb.toString();
}

From source file:com.qumoon.commons.web.HTMLInputChecker.java

protected String escapeComments(String s) {
    Matcher m = escapeCommentsPattern.matcher(s);
    StringBuffer buf = new StringBuffer();
    if (m.find()) {
        String match = m.group(1); // (.*?)
        appendReplacement(m, buf, "<!--" + HTMLInputChecker.htmlSpecialChars(match) + "-->");
    }/*from  w w  w .  j  a  v a 2  s. c o  m*/
    m.appendTail(buf);

    return buf.toString();
}

From source file:com.qumoon.commons.web.HTMLInputChecker.java

protected String validateEntities(String s) {
    // validate entities throughout the string
    Pattern p = Pattern.compile("&([^&;]*)(?=(;|&|$))");
    Matcher m = p.matcher(s);
    StringBuffer buf = new StringBuffer();
    if (m.find()) {
        String one = m.group(1); // ([^&;]*)
        String two = m.group(2); // (?=(;|&|$))
        appendReplacement(m, buf, checkEntity(one, two));
    }/* w  ww  .j a  va  2s  .com*/
    m.appendTail(buf);
    s = buf.toString();

    // validate quotes outside of tags
    p = Pattern.compile("(>|^)([^<]+?)(<|$)", Pattern.DOTALL);
    m = p.matcher(s);
    buf = new StringBuffer();
    if (m.find()) {
        String one = m.group(1); // (>|^)
        String two = m.group(2); // ([^<]+?)
        String three = m.group(3); // (<|$)
        appendReplacement(m, buf, one + two.replaceAll("\"", "&quot;") + three);
    }
    m.appendTail(buf);

    return s;
}