Example usage for java.util.regex Matcher end

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

Introduction

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

Prototype

public int end() 

Source Link

Document

Returns the offset after the last character matched.

Usage

From source file:com.xpn.xwiki.render.XWikiMacrosMappingRenderer.java

private String convertSingleLines(String content, XWikiContext context) {
    StringBuffer result = new StringBuffer();
    Matcher m = SINGLE_LINE_MACRO_PATTERN.matcher(content);
    int current = 0;
    while (m.find()) {
        result.append(content.substring(current, m.start()));
        current = m.end();
        String macroname = m.group(1);
        String params = m.group(3);
        String allcontent = m.group(0);

        XWikiVirtualMacro macro = this.macros_mappings.get(macroname);
        if ((macro != null) && (macro.isSingleLine())) {
            result.append(context.getWiki().getRenderingEngine().convertSingleLine(macroname, params,
                    allcontent, macro, context));
        } else {/* w  ww  .j a v  a 2  s . c  om*/
            result.append(allcontent);
        }
    }
    if (current == 0) {
        return content;
    }

    result.append(content.substring(current));

    return result.toString();
}

From source file:com.xpn.xwiki.render.XWikiMacrosMappingRenderer.java

private String convertMultiLines(String content, XWikiContext context) {
    StringBuffer result = new StringBuffer();
    Matcher m = MULTI_LINE_MACRO_PATTERN.matcher(content);
    int current = 0;
    while (m.find()) {
        result.append(content.substring(current, m.start()));
        current = m.end();
        String macroname = m.group(1);
        String params = m.group(3);
        String data = m.group(4);
        String allcontent = m.group(0);

        XWikiVirtualMacro macro = this.macros_mappings.get(macroname);
        if ((macro != null) && (macro.isMultiLine())) {
            result.append(context.getWiki().getRenderingEngine().convertMultiLine(macroname, params, data,
                    allcontent, macro, context));
        } else {/* www  .  j  ava  2 s .  c  o m*/
            result.append(allcontent);
        }
    }
    if (current == 0) {
        return content;
    }

    result.append(content.substring(current));

    return result.toString();
}

From source file:com.sonicle.webtop.mail.ICalendarRequest.java

/**
 * <p>/*from   ww  w  . ja v  a 2s  .c  om*/
 * Finds all "URL"s in the given _rawText, wraps them in 
 * HTML link tags and returns the result (with the rest of the text
 * html encoded).
 * </p>
 * <p>
 * We employ the procedure described at:
 * http://www.codinghorror.com/blog/2008/10/the-problem-with-urls.html
 * which is a <b>must-read</b>.
 * </p>
 * Basically, we allow any number of left parenthesis (which will get stripped away)
 * followed by http:// or https://.  Then any number of permitted URL characters
 * (based on http://www.ietf.org/rfc/rfc1738.txt) followed by a single character
 * of that set (basically, those minus typical punctuation).  We remove all sets of 
 * matching left & right parentheses which surround the URL.
 *</p>
 * <p>
 * This method *must* be called from a tag/component which will NOT
 * end up escaping the output.  For example:
 * <PRE>
 * <h:outputText ... escape="false" value="#{core:hyperlinkText(textThatMayHaveURLs, '_blank')}"/>
 * </pre>
 * </p>
 * <p>
 * Reason: we are adding <code>&lt;a href="..."&gt;</code> tags to the output *and*
 * encoding the rest of the string.  So, encoding the outupt will result in
 * double-encoding data which was already encoded - and encoding the <code>a href</code>
 * (which will render it useless).
 * </p>
 * <p>
 * 
 * @param   _rawText  - if <code>null</code>, returns <code>""</code> (empty string).
 * @param   _target   - if not <code>null</code> or <code>""</code>, adds a target attributed to the generated link, using _target as the attribute value.
 */
public static final String hyperlinkText(final String _rawText, final String _target) {

    String returnValue = null;

    if (!StringUtils.isBlank(_rawText)) {

        final Matcher matcher = URI_FINDER_PATTERN.matcher(_rawText);

        if (matcher.find()) {

            final int originalLength = _rawText.length();

            final String targetText = (StringUtils.isBlank(_target)) ? ""
                    : " target=\"" + _target.trim() + "\"";
            final int targetLength = targetText.length();

            // Counted 15 characters aside from the target + 2 of the URL (max if the whole string is URL)
            // Rough guess, but should keep us from expanding the Builder too many times.
            final StringBuilder returnBuffer = new StringBuilder(originalLength * 2 + targetLength + 15);

            int currentStart;
            int currentEnd;
            int lastEnd = 0;

            String currentURL;

            do {
                currentStart = matcher.start();
                currentEnd = matcher.end();
                currentURL = matcher.group();

                // Adjust for URLs wrapped in ()'s ... move start/end markers
                //      and substring the _rawText for new URL value.
                while (currentURL.startsWith("(") && currentURL.endsWith(")")) {
                    currentStart = currentStart + 1;
                    currentEnd = currentEnd - 1;

                    currentURL = _rawText.substring(currentStart, currentEnd);
                }

                while (currentURL.startsWith("(")) {
                    currentStart = currentStart + 1;

                    currentURL = _rawText.substring(currentStart, currentEnd);
                }

                // Text since last match
                returnBuffer.append(StringEscapeUtils.escapeHtml4(_rawText.substring(lastEnd, currentStart)));

                // Wrap matched URL
                returnBuffer.append("<a href=\"" + currentURL + "\"" + targetText + ">" + currentURL + "</a>");

                lastEnd = currentEnd;

            } while (matcher.find());

            if (lastEnd < originalLength) {
                returnBuffer.append(StringEscapeUtils.escapeHtml4(_rawText.substring(lastEnd)));
            }

            returnValue = returnBuffer.toString();
        }
    }

    if (returnValue == null) {
        returnValue = StringEscapeUtils.escapeHtml4(_rawText);
    }

    return returnValue;

}

From source file:com.puppycrawl.tools.checkstyle.api.FileText.java

/**
 * Find positions of line breaks in the full text.
 * @return an array giving the first positions of each line.
 *///from w w w . ja  va  2  s  .  c  om
private int[] findLineBreaks() {
    if (lineBreaks == null) {
        final int[] lineBreakPositions = new int[size() + 1];
        lineBreakPositions[0] = 0;
        int lineNo = 1;
        final Matcher matcher = LINE_TERMINATOR.matcher(fullText);
        while (matcher.find()) {
            lineBreakPositions[lineNo] = matcher.end();
            lineNo++;
        }
        if (lineNo < lineBreakPositions.length) {
            lineBreakPositions[lineNo] = fullText.length();
        }
        lineBreaks = lineBreakPositions;
    }
    return lineBreaks;
}

From source file:com.qmetry.qaf.automation.step.StringTestStep.java

private void absractArgsAandSetDesciption() {
    String prefix = BDDKeyword.getKeywordFrom(name);
    if (actualArgs == null || actualArgs.length == 0) {
        final String REGEX = ParamType.getParamValueRegx();
        List<String> arguments = new ArrayList<String>();

        description = removePrefix(prefix, name);

        Pattern p = Pattern.compile(REGEX);
        Matcher m = p.matcher(description); // get a matcher object
        int count = 0;

        while (m.find()) {
            String arg = description.substring(m.start(), m.end());
            arguments.add(arg);//from   w w w.ja v a 2 s.c  om
            count++;
        }
        for (int i = 0; i < count; i++) {
            description = description.replaceFirst(Pattern.quote(arguments.get(i)),
                    Matcher.quoteReplacement("{" + i + "}"));
        }
        actualArgs = arguments.toArray(new String[] {});
    }
    name = StringUtil.toCamelCaseIdentifier(description.length() > 0 ? description : name);
}

From source file:at.ac.tuwien.inso.subcat.utility.commentparser.Parser.java

License:asdf

public CommentNode<T> parse(String content) {
    int patchId = -1;

    Matcher m = pReview.matcher(content);
    if (m.find() && m.group(1) != null) {
        patchId = Integer.parseInt(m.group(1));
        content = content.substring(m.end());
    }/*from  w  w  w  .  jav a2  s  . co m*/

    List<ContentNode<T>> ast = new LinkedList<ContentNode<T>>();
    parseQuotes(ast, content);

    return new CommentNode<T>(ast, patchId);
}

From source file:org.ops4j.pax.web.itest.jetty.WarJSFIntegrationTest.java

@Test
public void testJSF() throws Exception {
    // needed to wait for fully initializing the container
    Thread.sleep(1000);/* ww w .j a v a 2s .co m*/

    LOG.debug("Testing JSF workflow!");
    String response = testClient.testWebPath("http://127.0.0.1:8181/war-jsf-sample", "Please enter your name");

    LOG.debug("Found JSF starting page: {}", response);

    Pattern patternViewState = Pattern.compile("id=\\\"j_id_.*:javax.faces.ViewState:\\w\\\"");
    Matcher viewStateMatcher = patternViewState.matcher(response);
    if (!viewStateMatcher.find()) {
        fail("Didn't find required ViewState ID!");
    }
    String viewStateID = response.substring(viewStateMatcher.start() + 4, viewStateMatcher.end() - 1);

    String substring = response.substring(viewStateMatcher.end() + 8);
    int indexOf = substring.indexOf("\"");
    String viewStateValue = substring.substring(0, indexOf);

    // int indexOf =
    // response.indexOf("id=\"javax.faces.ViewState\" value=");
    // String substring = response.substring(indexOf + 34);
    // indexOf = substring.indexOf("\"");
    // substring = substring.substring(0, indexOf);

    Pattern pattern = Pattern.compile("(input id=\"mainForm:j_id_\\w*)");
    Matcher matcher = pattern.matcher(response);
    if (!matcher.find())
        fail("Didn't find required input id!");

    String inputID = response.substring(matcher.start(), matcher.end());
    inputID = inputID.substring(inputID.indexOf('"') + 1);
    LOG.debug("Found ID: {}", inputID);

    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
    nameValuePairs.add(new BasicNameValuePair("mainForm:name", "Dummy-User"));

    nameValuePairs.add(new BasicNameValuePair(viewStateID, viewStateValue));

    nameValuePairs.add(new BasicNameValuePair(inputID, "Press me"));

    nameValuePairs.add(new BasicNameValuePair("javax.faces.ViewState", viewStateValue));

    // nameValuePairs.add(new BasicNameValuePair("mainForm", inputID));

    nameValuePairs.add(new BasicNameValuePair("mainForm_SUBMIT", "1"));

    LOG.debug("Will send the following NameValuePairs: {}", nameValuePairs);

    testClient.testPost("http://127.0.0.1:8181/war-jsf-sample/faces/helloWorld.jsp", nameValuePairs,
            "Hello Dummy-User. We hope you enjoy Apache MyFaces", 200);

}

From source file:net.java.sip.communicator.impl.gui.main.chat.replacers.KeywordReplacer.java

/**
 * Replace operation. Searches for the keyword in the provided piece of
 * content and replaces it with the piece of content surrounded by &lt;b&gt;
 * tags./*  w  w  w. java 2s.  c  o  m*/
 *
 * @param target the destination to write the result to
 * @param piece the piece of content to process
 */
@Override
public void replace(final StringBuilder target, final String piece) {
    if (this.keyword == null || this.keyword.isEmpty()) {
        target.append(StringEscapeUtils.escapeHtml4(piece));
        return;
    }

    final Matcher m = Pattern
            .compile("(^|\\W)(" + Pattern.quote(keyword) + ")(\\W|$)", Pattern.CASE_INSENSITIVE).matcher(piece);
    int prevEnd = 0;
    while (m.find()) {
        target.append(StringEscapeUtils.escapeHtml4(
                piece.substring(prevEnd, m.start() + m.group(INDEX_OPTIONAL_PREFIX_GROUP).length())));
        prevEnd = m.end() - m.group(INDEX_OPTIONAL_SUFFIX_GROUP).length();
        final String keywordMatch = m.group(INDEX_KEYWORD_MATCH_GROUP).trim();
        target.append("<b>");
        target.append(StringEscapeUtils.escapeHtml4(keywordMatch));
        target.append("</b>");
    }
    target.append(StringEscapeUtils.escapeHtml4(piece.substring(prevEnd)));
}

From source file:com.squarespace.less.core.MapFormat.java

/**
 * Extracts the keys from the pattern and generates the format string.
 *///from ww  w  .j a va 2s . c o m
private String init(String raw) {
    StringBuffer buf = new StringBuffer();
    Matcher matcher = KEY_ESCAPE.matcher(raw);
    int mark = 0;
    int length = raw.length();
    while (matcher.find()) {
        int start0 = matcher.start();
        if (mark < start0) {
            buf.append(raw.substring(mark, start0));
        }
        buf.append(raw.charAt(start0));
        keys.add(raw.substring(start0 + 2, matcher.end() - 1));
        mark = matcher.end();
    }
    if (mark < length) {
        buf.append(raw.substring(mark, length));
    }
    return buf.toString();
}

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  .ja v  a 2 s  . c o m
    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);
}