Example usage for java.util.regex Pattern DOTALL

List of usage examples for java.util.regex Pattern DOTALL

Introduction

In this page you can find the example usage for java.util.regex Pattern DOTALL.

Prototype

int DOTALL

To view the source code for java.util.regex Pattern DOTALL.

Click Source Link

Document

Enables dotall mode.

Usage

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) + "-->");
    }/*w  w w .  ja v a  2s .c om*/
    matcher.appendTail(buf);

    return buf.toString();
}

From source file:eu.nerdz.api.impl.fastreverse.messages.FastReverseConversationHandler.java

/**
 * Replaces a single tag.//from  w  w  w  .j a va 2s.  c o  m
 * @param regex A regex
 * @param message A message to be parsed
 * @return A string in which all occurrences of regex have been substituted with the contents matched
 */
private static String replaceSingle(String regex, String message) {

    Matcher matcher = Pattern.compile(regex, Pattern.DOTALL | Pattern.CASE_INSENSITIVE).matcher(message);
    StringBuffer result = new StringBuffer();

    while (matcher.find()) {
        matcher.appendReplacement(result, matcher.group(1).replace("$", "\\$"));
    }

    matcher.appendTail(result);

    return result.toString();
}

From source file:games.strategy.triplea.pbem.AxisAndAlliesForumPoster.java

/**
 * Logs into axisandallies.org/*from w w w  .j  av a2s  .  c  om*/
 * nb: Username and password are posted in clear text
 * 
 * @throws Exception
 *             if login fails
 */
private void login() throws Exception {
    // creates and configures a new http client
    m_client = new HttpClient();
    m_client.getParams().setParameter("http.protocol.single-cookie-header", true);
    m_client.getParams().setParameter("http.useragent",
            "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0)");
    m_httpState = new HttpState();
    m_hostConfiguration = new HostConfiguration();
    // add the proxy
    GameRunner2.addProxy(m_hostConfiguration);
    m_hostConfiguration.setHost("www.axisandallies.org");

    final PostMethod post = new PostMethod("http://www.axisandallies.org/forums/index.php?action=login2");
    try {
        post.addRequestHeader("Accept", "*/*");
        post.addRequestHeader("Accept-Language", "en-us");
        post.addRequestHeader("Cache-Control", "no-cache");
        post.addRequestHeader("Content-Type", "application/x-www-form-urlencoded");

        final List<NameValuePair> parameters = new ArrayList<NameValuePair>();
        parameters.add(new NameValuePair("user", getUsername()));
        parameters.add(new NameValuePair("passwrd", getPassword()));
        post.setRequestBody(parameters.toArray(new NameValuePair[parameters.size()]));

        int status = m_client.executeMethod(m_hostConfiguration, post, m_httpState);
        if (status == 200) {
            final String body = post.getResponseBodyAsString();
            if (body.toLowerCase().contains("password incorrect")) {
                throw new Exception("Incorrect Password");
            }
            // site responds with 200, and a refresh header
            final Header refreshHeader = post.getResponseHeader("Refresh");
            if (refreshHeader == null) {
                throw new Exception("Missing refresh header after login");
            }

            final String value = refreshHeader.getValue(); // refresh: 0; URL=http://...
            final Pattern p = Pattern.compile("[^;]*;\\s*url=(.*)", Pattern.DOTALL | Pattern.CASE_INSENSITIVE);
            final Matcher m = p.matcher(value);
            if (m.matches()) {
                final String url = m.group(1);
                final GetMethod getRefreshPage = new GetMethod(url);

                try {
                    status = m_client.executeMethod(m_hostConfiguration, getRefreshPage, m_httpState);
                    if (status != 200) {
                        // something is probably wrong, but there is not much we can do about it, we handle errors when we post
                    }
                } finally {
                    getRefreshPage.releaseConnection();
                }
            } else {
                throw new Exception("The refresh header didn't contain a URL");
            }
        } else {
            throw new Exception("Failed to login to forum, server responded with status code: " + status);
        }
    } finally {
        post.releaseConnection();
    }
}

From source file:org.etudes.util.HtmlHelper.java

/**
 * Remove any HTML comments from the data.
 * //from  w  w  w. ja  va 2 s .  c  o m
 * @param data
 *        the html data.
 * @return The cleaned up data.
 */
public static String stripComments(String data) {
    if (data == null)
        return data;

    // quick check for any comments
    if (data.indexOf("<!--") == -1)
        return data;

    // pattern to find html comments
    // Notes: DOTALL so the "." matches line terminators too, "*?" Reluctant quantifier so text between two different comments is not lost
    Pattern p = Pattern.compile("<!--.*?-->", Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE | Pattern.DOTALL);

    Matcher m = p.matcher(data);
    StringBuffer sb = new StringBuffer();

    while (m.find()) {
        m.appendReplacement(sb, "");
    }

    m.appendTail(sb);

    data = sb.toString();

    // if any open tags are left, likely because of missing a matching close tag, we will remove them.
    // if we leave them in, a missing close comment tag will be inserted by HtmlCleaner at the very END of the document, making the rest a big comment.
    // this fix exposes some comment text into the content, but preserves actual content.
    data = data.replaceAll("<!--", "");

    return data;
}

From source file:de.mpg.escidoc.services.exportmanager.Export.java

public String explainFormatsXML() throws ExportManagerException {

    //TODO: Revision with ValueObjects

    String citStyles;//ww  w.  ja va  2 s .c  o m
    try {
        citStyles = getCitationStyleHandler().explainStyles();
    } catch (Exception e) {
        throw new ExportManagerException("Cannot get citation styles explain", e);
    }

    String structured;
    try {
        structured = getStructuredExportHandler().explainFormats();
    } catch (Exception e) {
        throw new ExportManagerException("Cannot get structured exports explain", e);
    }

    String result;
    // get export-format elements
    String regexp = "<export-formats.*?>(.*?)</export-formats>";
    Matcher m = Pattern.compile(regexp, Pattern.CASE_INSENSITIVE | Pattern.DOTALL).matcher(citStyles);
    m.find();
    result = m.group(1);
    m = Pattern.compile(regexp, Pattern.CASE_INSENSITIVE | Pattern.DOTALL).matcher(structured);
    m.find();
    result += m.group(1);

    m = Pattern.compile("<export-format\\s+.*</export-format>", Pattern.CASE_INSENSITIVE | Pattern.DOTALL)
            .matcher(structured);
    m.find();
    result = m.replaceAll(result);

    // replace comments
    // m = Pattern
    // .compile(
    // "<!--.*?-->"
    // , Pattern.CASE_INSENSITIVE | Pattern.DOTALL
    // )
    // .matcher(result);
    // m.find();
    // result = m.replaceAll("");

    return result;
}

From source file:com.netflix.imfutility.conversion.executor.ConversionOperationParser.java

private String addQuotesIfNeeded(String param) {
    String trimmedParam = param.trim();
    Pattern p = Pattern.compile(QUOTES_TEMPLATE, Pattern.DOTALL);
    if (!p.matcher(trimmedParam).matches()) {
        if (trimmedParam.contains("\"")) {
            trimmedParam = String.format("'%s'", trimmedParam);
        } else {//from w  w w.ja  va2  s  .c  o  m
            trimmedParam = String.format("\"%s\"", trimmedParam);
        }
    }
    return trimmedParam;
}

From source file:biz.c24.io.spring.batch.reader.C24ItemReader.java

/**
 * Sets the regular expression used to quickly split up the source into individual entities for parsing
 * //  w ww  . j av  a 2s . c om
 * @param elementStartRegEx The regular expression to identify the start of a new entity in the source
 */
public void setElementStartPattern(String elementStartRegEx) {
    this.elementStartPattern = Pattern.compile(elementStartRegEx, Pattern.DOTALL);
}

From source file:com.romeikat.datamessie.core.processing.task.documentProcessing.redirecting.DocumentRedirector.java

private String getRedirectedUrl(final String content, final RedirectingRule redirectingRule) {
    // Extract URL with regex
    final String regex = redirectingRule.getRegex();
    if (StringUtils.isBlank(regex)) {
        return null;
    }/*from  w  w w  .j  ava  2 s.co m*/

    final Integer group = redirectingRule.getRegexGroup();
    if (group == null) {
        return null;
    }

    final Pattern pattern = Pattern.compile(regex, Pattern.DOTALL);
    final Matcher matcher = pattern.matcher(content);
    // If an URL can be extracted, use the extracted URL instead
    if (matcher.find()) {
        try {
            final String redirectedUrl = matcher.group(group);
            return redirectedUrl;
        } catch (final Exception e) {
            LOG.warn("Could not extradt redirected URL with regex {} and group {}", regex, group);
        }
    }

    return null;
}

From source file:mobi.jenkinsci.server.core.net.ProxyUtil.java

private String resolveCss(final String userAgent, final String pluginName, final String url,
        final String resultString) throws IOException {
    log.debug("Resolving CSS for URL " + url);
    final StringBuilder outString = new StringBuilder();
    int currPos = 0;
    final Pattern cssLinkPattern = Pattern.compile("<link" + "[^>]*" + "rel=[\"']stylesheet[\"']" + "[^>]*"
            + "href=[\"']([^>\"']*)[\"']" + "[^>]*" + "/>", Pattern.DOTALL);
    final Matcher matcher = cssLinkPattern.matcher(resultString);
    while (matcher.find(currPos)) {
        final int start = matcher.start();
        final int end = matcher.end();
        outString.append(resultString.substring(currPos, start));
        final String cssUrl = matcher.group(1);
        String cssText = retrieveCss(userAgent, pluginName, url, cssUrl);
        cssText = resolveImages(userAgent, pluginName,
                Pattern.compile(CSS_BACKGROUND_IMAGE_PATTERN, Pattern.DOTALL), 2,
                getBasePathUrl(resolveRelativeUrl(url, cssUrl)), cssText);
        outString.append(cssText);//ww  w .  j a v  a2  s  . c  o  m
        currPos = end;
    }

    outString.append(resultString.substring(currPos));
    log.debug(outString.length() + " CSS chars included for URL " + url);
    return outString.toString();
}

From source file:com.github.rwitzel.streamflyer.regex.RegexModifierTest.java

@Test
public void testExampleFromHomepage_advancedExample_thirdImprovement() throws Exception {

    // second improvement
    Modifier myModifier2 = new RegexModifier("edit(\\s++stream)", Pattern.DOTALL, "modify$1");

    // test: does not use look-behind
    assertNotEquals("credit stream", modify("credit stream", myModifier2));

    // third and final improvement
    Modifier myModifier3 = new RegexModifier("(?<=\\s)edit(\\s++stream)", Pattern.DOTALL, "modify$1", 1, 2048);

    // test: uses look-behind
    assertEquals("credit stream", modify("credit stream", myModifier3));
}