Example usage for com.google.gwt.regexp.shared MatchResult getGroupCount

List of usage examples for com.google.gwt.regexp.shared MatchResult getGroupCount

Introduction

In this page you can find the example usage for com.google.gwt.regexp.shared MatchResult getGroupCount.

Prototype

public final int getGroupCount() 

Source Link

Usage

From source file:client.managers.history.HistoryState.java

License:Open Source License

/**
 * Create a {@code HistoryState} from a history token.
 * //ww w  .  j a  v a 2s.c o m
 * <pre>([0-9]*) : ([0-9]*) / ([^/]*) /? ([^/]*) /? ((.{2},?)*)</pre>
 *
 * <ol>
 *   <li>Interval start</li>
 *   <li>Interval end</li>
 *   <li>Series tab name</li>
 *   <li>Indicator identifier (optional)</li>
 *   <li>Comma-separated list of ISO codes (optional)</li>
 * </ol>
 *
 * Examples:
 *
 * <ul>
 *   <li>2010:2014/map:europe/ABC.DEF.1234/SE,DK,NO</li>
 *   <li>2010:2014/map:europe/ABC.DEF.1234</li>
 *   <li>2010:2014/map:europe</li>
 * </ul>
 *
 * @param historyToken History token.
 * @return Created {@code HistoryState} instance.
 *
 * @see HistoryState#REGEX
 */
public static HistoryState fromHistoryToken(String historyToken) {
    MatchResult result = REGEX.exec(historyToken);

    if (result == null || result.getGroupCount() <= 1) {
        return new HistoryState();
    }

    /*
     * Interval
     */
    Integer intervalStartYear = Integer.valueOf(result.getGroup(REGEX_INTERVAL_START_YEAR));
    Integer intervalEndYear = Integer.valueOf(result.getGroup(REGEX_INTERVAL_END_YEAR));

    /*
     * Tab name
     */
    String seriesTabName = result.getGroup(REGEX_SERIES_TAB_NAME);
    if (seriesTabName != null) {
        seriesTabName = seriesTabName.trim();

        if (seriesTabName.isEmpty()) {
            seriesTabName = null;
        }
    }

    /*
     * Indicator
     */
    String indicatorIdent = result.getGroup(REGEX_INDICATOR_IDENT);
    if (indicatorIdent != null) {
        indicatorIdent = indicatorIdent.trim();

        if (indicatorIdent.isEmpty()) {
            indicatorIdent = null;
        }
    }

    /*
     * Country ISO list
     */
    String countryISOListString = result.getGroup(REGEX_COUNTRY_ISO_LIST);
    List<String> countryISOList = null;
    if (countryISOListString != null) {
        countryISOListString = countryISOListString.trim();

        if (!countryISOListString.isEmpty()) {
            countryISOList = new ArrayList<String>(
                    Arrays.asList(countryISOListString.split(COUNTRY_ISO_LIST_SEPARATOR)));
        }
    }

    return new HistoryState(intervalStartYear, intervalEndYear, seriesTabName, indicatorIdent, countryISOList);
}

From source file:client.net.sf.saxon.ce.dom.HTMLNodeWrapper.java

License:Mozilla Public License

private String getStyleAttribute(Element elem) {
    // style attribute must exist and must be quoted properly
    // IE9 does not include style attributes text if their value is not valid
    // we therefore don't expect non-quoted styles like <h1 style=color id="abc"...
    String value = getOuterHTML(elem);

    RegExp quotesPattern = RegExp.compile("(?:\".*?\"|\'.*?\'|[^\'\"]+|['\"])", "g"); // g=global: = all-matches 
    RegExp stylePattern = RegExp.compile("[\\s]style\\s*="); // e.g. match: <h1 style=
    MatchResult m = quotesPattern.exec(value);

    int i = 0;//w w w.  j av a2  s. co m
    boolean styleFound = false;
    String styleContent = "";
    String nonQ = "";
    while (quotesPattern.getLastIndex() > 0) {
        if (i % 2 == 0) {
            nonQ = m.getGroup(0); // not in quotes - so check
            MatchResult ms = stylePattern.exec(nonQ);
            styleFound = ms.getGroupCount() > 0;
            if (!styleFound && nonQ.indexOf('>') > -1) {
                break; // no more attributes
            }
        } else if (styleFound) {
            styleContent = m.getGroup(0);
            // remove enclosing quotes
            styleContent = styleContent.substring(1, styleContent.length() - 1);
            break;
        }
        i++;
        m = quotesPattern.exec(value);
    }
    return styleContent;
}

From source file:com.axlight.gbrain.client.MainPane.java

License:Apache License

private void jumpToUrl(NeuronNode n) {
    String text = n.getContent();
    MatchResult match = URL_REGEX.exec(text);
    if (match.getGroupCount() > 0) {
        Window.open(match.getGroup(0), "_blank", "");
    }//w  ww.  j  a  va 2s  . c  om
}

From source file:com.brazoft.foundation.gwt.client.ui.Widgets.java

License:Apache License

public static boolean mustBeWhiteIcon(String className) {
    MatchResult matcher = RegExp.compile("primary|info|success|warning|danger|inverse").exec(className);
    return matcher != null && matcher.getGroupCount() > 0;
}

From source file:com.cgxlib.xq.client.plugins.deferred.PromiseReqBuilderJSONP.java

License:Apache License

public PromiseReqBuilderJSONP(String url, String callbackParam, int timeout) {
    JsonpRequestBuilder builder = new JsonpRequestBuilder();
    if (timeout > 0) {
        builder.setTimeout(timeout);//from   w ww .  j a va2 s  . co  m
    }
    // jQuery allows a parameter callback=? to figure out the callback parameter
    if (callbackParam == null) {
        MatchResult tmp = callbackRegex.exec(url);
        if (tmp != null && tmp.getGroupCount() == 4) {
            callbackParam = tmp.getGroup(2);
            url = tmp.getGroup(1) + tmp.getGroup(3);
        }
    }
    if (callbackParam != null) {
        builder.setCallbackParam(callbackParam);
    }
    send(builder, url, new AsyncCallback<Object>() {
        public void onFailure(Throwable caught) {
            dfd.reject(caught);
        }

        public void onSuccess(Object result) {
            dfd.resolve(result);
        }
    });
}

From source file:com.client.hp.hpl.jena.iri.impl.ComponentPattern.java

License:Apache License

public void analyse(Parser parser, int range) {

    MatchResult m = pattern.exec(parser.get(range));
    //      Matcher m = pattern.matcher(parser.get(range));
    //      if (!m.matches()) {

    if (!pattern.test(parser.get(range))) {
        parser.recordError(range, SCHEME_PATTERN_MATCH_FAILED);
        return;/*w  ww. java2  s .com*/
    }
    for (int g = 1; g <= m.getGroupCount(); g++)
        //         if (m.start(g) != -1)
        actions[g].check(m, parser, range);

}

From source file:com.client.hp.hpl.jena.iri.impl.ComponentPatternParser.java

License:Apache License

static private String[] mySplit(String p) {
    //return splitter.split(p); 

    MatchResult m = keyword.exec(p);
    //        Matcher m = keyword.matcher(p);

    List<String> rslt = new ArrayList<String>();
    int pos = 0;//from   w w  w  . ja va 2 s .  c o  m
    //        rslt.add("");

    for (int i = 0; i < m.getGroupCount(); i++) {
        String group = m.getGroup(i);
        rslt.add(group);
    }
    //        while (m.find()) {
    //           
    //            if (m.start()>pos || pos==0) {
    //                rslt.add(p.substring(pos,m.start()));
    //            }
    //            rslt.add(p.substring(m.start(),m.end()));
    //            pos = m.end();
    //        }
    if (pos < p.length())
        rslt.add(p.substring(pos));

    return rslt.toArray(emptyStringArray);
}

From source file:com.client.hp.hpl.jena.iri.impl.ComponentPatternParser.java

License:Apache License

private int classify(String string) {
    RegExp re = RegExp.compile(separators);
    MatchResult m = keyword.exec(string);
    //        Matcher m = keyword.matcher(string);

    if (!re.test(string))
        return OTHER;
    for (int i = 1; i <= m.getGroupCount(); i++)
        //            if (m.start(i) != -1)
        return i;
    throw new IllegalStateException("IRI code internal error: no group matched.");
}

From source file:com.client.hp.hpl.jena.reasoner.rulesys.builtins.Regex.java

License:Apache License

/**
 * This method is invoked when the builtin is called in a rule body.
 * // w  w w . j  av a2  s.co  m
 * @param args
 *            the array of argument values for the builtin, this is an array
 *            of Nodes, some of which may be Node_RuleVariables.
 * @param length
 *            the length of the argument list, may be less than the length
 *            of the args array for some rule engines
 * @param context
 *            an execution context giving access to other relevant data
 * @return return true if the buildin predicate is deemed to have succeeded
 *         in the current environment
 */
@Override
public boolean bodyCall(ASubCallHandler handler, Node[] args, int length, RuleContext context) {
    if (length < 2)
        throw new BuiltinException(this, context, "Must have at least 2 arguments to " + getName());
    String text = getString(getArg(0, args, context), context);
    String pattern = getString(getArg(1, args, context), context);
    MatchResult m = RegExp.compile(pattern).exec(text);

    if (m.getGroupCount() < 1)
        return false;
    if (length > 2) {
        // bind any capture groups
        BindingEnvironment env = context.getEnv();
        for (int i = 0; i < Math.min(length - 2, m.getGroupCount()); i++) {
            String gm = m.getGroup(i + 1);
            Node match = (gm != null) ? Node.createLiteral(gm) : Node.createLiteral("");
            if (!env.bind(args[i + 2], match))
                return false;
        }
    }
    return true;
}

From source file:com.codenvy.ide.ext.java.jdt.text.FindReplaceDocumentAdapter.java

License:Open Source License

/**
 * Stateful findReplace executes a FIND, REPLACE, REPLACE_FIND or FIND_FIRST operation. In case of REPLACE and REPLACE_FIND it
 * sends a <code>DocumentEvent</code> to all registered <code>IDocumentListener</code>.
 *
 * @param startOffset//  w  w  w .  ja  va2s.  c om
 *         document offset at which search starts this value is only used in the FIND_FIRST operation and otherwise
 *         ignored
 * @param findString
 *         the string to find this value is only used in the FIND_FIRST operation and otherwise ignored
 * @param replaceText
 *         the string to replace the current match this value is only used in the REPLACE and REPLACE_FIND
 *         operations and otherwise ignored
 * @param forwardSearch
 *         the search direction
 * @param caseSensitive
 *         indicates whether lower and upper case should be distinguished
 * @param wholeWord
 *         indicates whether the findString should be limited by white spaces as defined by Character.isWhiteSpace.
 *         Must not be used in combination with <code>regExSearch</code>.
 * @param regExSearch
 *         if <code>true</code> this operation represents a regular expression Must not be used in combination with
 *         <code>wholeWord</code>.
 * @param operationCode
 *         specifies what kind of operation is executed
 * @return the find or replace region or <code>null</code> if there was no match
 * @throws com.codenvy.ide.api.text.BadLocationException
 *         if startOffset is an invalid document offset
 * @throws IllegalStateException
 *         if a REPLACE or REPLACE_FIND operation is not preceded by a successful FIND operation
 * @throws PatternSyntaxException
 *         if a regular expression has invalid syntax
 */
private Region findReplace(final FindReplaceOperationCode operationCode, int startOffset, String findString,
        String replaceText, boolean forwardSearch, boolean caseSensitive, boolean wholeWord)
        throws BadLocationException {
    // Validate state
    if ((operationCode == REPLACE || operationCode == REPLACE_FIND_NEXT)
            && (fFindReplaceState != FIND_FIRST && fFindReplaceState != FIND_NEXT))
        throw new IllegalStateException("illegal findReplace state: cannot replace without preceding find"); //$NON-NLS-1$

    if (operationCode == FIND_FIRST) {
        // Reset

        if (findString == null || findString.length() == 0)
            return null;

        // Validate start offset
        if (startOffset < 0 || startOffset >= length())
            throw new BadLocationException();

        String patternFlags = "g";

        if (caseSensitive)
            patternFlags += "i";

        if (wholeWord)
            findString = "\\b" + findString + "\\b"; //$NON-NLS-1$ //$NON-NLS-2$

        if (!wholeWord)
            findString = asRegPattern(findString);

        fFindReplaceMatchOffset = startOffset;
        regExp = RegExp.compile(findString, patternFlags);
        regExp.setLastIndex(fFindReplaceMatchOffset);
    }

    // Set state
    fFindReplaceState = operationCode;

    if (operationCode != REPLACE) {

        if (forwardSearch) {
            MatchResult matchResult = regExp.exec(String.valueOf(this));
            if (matchResult != null && matchResult.getGroupCount() > 0 && !matchResult.getGroup(0).isEmpty())
                return new RegionImpl(matchResult.getIndex(), matchResult.getGroup(0).length());
            return null;
        }
        // backward search
        regExp.setLastIndex(0);
        MatchResult matchResult = regExp.exec(String.valueOf(this));

        boolean found = matchResult != null;
        int index = -1;
        int length = -1;
        while (found
                && matchResult.getIndex() + matchResult.getGroup(0).length() <= fFindReplaceMatchOffset + 1) {
            index = matchResult.getIndex();
            length = matchResult.getGroup(0).length();
            regExp.setLastIndex(index + 1);
            matchResult = regExp.exec(String.valueOf(this));
            found = matchResult != null;
        }
        fFindReplaceMatchOffset = index;
        if (index > -1) {
            // must set matcher to correct position
            regExp.setLastIndex(index);
            matchResult = regExp.exec(String.valueOf(this));
            return new RegionImpl(index, length);
        }
        return null;
    }

    return null;
}