Example usage for java.util.regex Matcher reset

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

Introduction

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

Prototype

public Matcher reset(CharSequence input) 

Source Link

Document

Resets this matcher with a new input sequence.

Usage

From source file:org.apache.wookie.util.WidgetJavascriptSyntaxAnalyzer.java

/**
 * Find occurrences of incompatible setter syntax for Internet explorer
 * i.e. Widget.preferences.foo=bar;/*from  w w w.j av a  2  s . c o m*/
 * 
 * @throws IOException
 */
private void parseIEIncompatibilities() throws IOException {
    // Pattern match on the syntax 'widget.preferemces.name=value' - including optional quotes & spaces around the value
    Pattern pattern = Pattern.compile("widget.preferences.\\w+\\s*=\\s*\\\"??\\'??.+\\\"??\\'??",
            Pattern.CASE_INSENSITIVE);
    Matcher matcher = pattern.matcher("");
    // Search .js files, but also any html files
    Iterator<?> iter = FileUtils.iterateFiles(_searchFolder, new String[] { "js", "htm", "html" }, true);
    while (iter.hasNext()) {
        File file = (File) iter.next();
        LineNumberReader lineReader = new LineNumberReader(new FileReader(file));
        String line = null;
        while ((line = lineReader.readLine()) != null) {
            matcher.reset(line);
            if (matcher.find()) {
                String message = "\n(Line " + lineReader.getLineNumber() + ") in file " + file;
                message += "\n\t " + line + "\n";
                message += "This file contains preference setter syntax which may not behave correctly in Internet Explorer version 8 and below.\n";
                message += "See https://cwiki.apache.org/confluence/display/WOOKIE/FAQ#FAQ-ie8prefs for more information.\n";
                FlashMessage.getInstance().message(formatWebMessage(message));
                _logger.warn(message);
            }
        }
    }
}

From source file:com.hdfs.concat.crush.CrushReducer.java

/**
 * Converts the name of a directory to a path to the crush output file using the specs at the given index. The path will the
 * directory and file name separated by a slash /. Performs placeholder substitution on the corresponding replacement string in
 * {@link #outputReplacementList}. The final replacement string is then used to form the final path.
 *///from w  w w . j  a v  a2 s  . c o  m
String calculateOutputFile(int idx, String srcDir) {

    StringBuffer sb = new StringBuffer(srcDir);
    sb.append("/");

    String replacement = outputReplacementList.get(idx);

    placeHolderToValue.put("crush.file.num", Integer.toString(fileNum++));

    placeholderMatcher.reset(replacement);

    while (placeholderMatcher.find()) {
        String key = placeholderMatcher.group(1);

        String value = placeHolderToValue.get(key);

        if (null == value) {
            throw new IllegalArgumentException("No value for key: " + key);
        }

        placeholderMatcher.appendReplacement(sb, value);
    }

    placeholderMatcher.appendTail(sb);

    Matcher matcher = inputRegexList.get(idx);
    matcher.reset(srcDir);

    String finalOutputName = matcher.replaceAll(sb.toString());

    return finalOutputName;
}

From source file:org.pharmgkb.Genotype.java

protected void reorder() {
    if (getStrings().size() == 2) {

        try {/*  w  w  w. j  a  va2s.co m*/
            Integer int0, int1;
            Matcher matcher = sf_numberPattern.matcher(getStrings().get(0));
            if (matcher.find()) {
                int0 = Integer.parseInt(matcher.group(1));
            } else {
                return;
            }

            matcher.reset(getStrings().get(1));
            if (matcher.find()) {
                int1 = Integer.parseInt(matcher.group(1));
            } else {
                return;
            }

            if (int0 > int1) {
                Collections.reverse(getStrings());
            }
        } catch (Exception ex) {
            if (sf_logger.isDebugEnabled()) {
                sf_logger.debug("Error reordering: " + getStrings());
            }
        }

    }
}

From source file:cytoscape.CyMain.java

License:asdf

private Properties createProperties(String[] potentialProps) {
    // for ( String asdf: potentialProps)
    // logger.info("prop: '" + asdf + "'");
    Properties props = new Properties();
    Properties argProps = new Properties();

    Matcher propPattern = Pattern.compile("^((\\w+\\.*)+)\\=(.+)$").matcher("");

    for (int i = 0; i < potentialProps.length; i++) {
        propPattern.reset(potentialProps[i]);

        // check to see if the string is a key value pair
        if (propPattern.matches()) {
            argProps.setProperty(propPattern.group(1), propPattern.group(3));

            // otherwise assume it's a file/url
        } else {//w  w w .  j a  v  a2 s . c o  m
            try {
                InputStream in = null;

                try {
                    in = FileUtil.getInputStream(potentialProps[i]);
                    if (in != null)
                        props.load(in);
                    else
                        logger.info("Couldn't load property: " + potentialProps[i]);
                } finally {
                    if (in != null) {
                        in.close();
                    }
                }
            } catch (IOException e) {
                logger.warn("Couldn't load property '" + potentialProps[i] + "' from file: " + e.getMessage(),
                        e);
            }
        }
    }

    // Transfer argument properties into the full properties.
    // We do this so that anything specified on the command line
    // overrides anything specified in a file.
    props.putAll(argProps);

    addDefaultProps(props);

    return props;
}

From source file:Repackage.java

public StringBuffer repackage(StringBuffer sb) {
    StringBuffer result = null;/*from www  .j  av a  2s. c  om*/

    for (int i = 0; i < _fromMatchers.length; i++) {
        Matcher m = (Matcher) _fromMatchers[i];

        m.reset(sb);

        for (boolean found = m.find(); found; found = m.find()) {
            if (result == null)
                result = new StringBuffer();

            m.appendReplacement(result, _toPackageNames[i]);
        }

        if (result != null) {
            m.appendTail(result);
            sb = result;
            result = null;
        }
    }

    return sb;
}

From source file:org.bireme.interop.toJson.Xml2Json.java

private boolean contains(final File in, final Matcher mat) throws IOException {
    assert in != null;
    assert mat != null;

    boolean ret = false;

    try (BufferedReader reader = new BufferedReader(new FileReader(in))) {
        while (true) {
            final String line = reader.readLine();
            if (line == null) {
                break;
            }/*from   w w w  . j a v a2  s .c  o m*/

            mat.reset(line);
            if (mat.find()) {
                ret = true;
                break;
            }
        }
    }
    return ret;
}

From source file:org.openadaptor.auxil.connector.iostream.reader.DirectoryReadConnector.java

/**
 * sets the filename filter that restricts the files that are read to those
 * whose unqualified name matches this regular expression
 *//*from   w ww  . ja v  a2  s .  c o  m*/
public void setFilenameRegex(String regex) {
    final Matcher matcher = Pattern.compile(regex).matcher("");
    setFilenameFilter(new FilenameFilter() {
        public boolean accept(File dir, String name) {
            return matcher.reset(name).matches();
        }
    });
}

From source file:de.tudarmstadt.ukp.csniper.webapp.search.cqp.CqpQuery.java

private List<EvaluationItem> parseOutput(List<String> aOutput) {
    List<EvaluationItem> items = new ArrayList<EvaluationItem>();
    String regexp = "\\s*(\\d+):\\s*<" + E_TEXT + "_" + ATTR_ID + "\\s(.+)>:\\s*(.*?)"
            + Pattern.quote(leftDelim) + "(.*?)" + Pattern.quote(rightDelim) + "(.*?)";
    Pattern p = Pattern.compile(regexp);
    Matcher m = p.matcher("");

    // parse results and create EvaluationItems
    for (String line : aOutput) {
        m.reset(line);
        if (m.matches() /* && m.groupCount() == 5 */) {
            int position = Integer.valueOf(m.group(1));
            String documentId = m.group(2).trim();
            String lc = m.group(3).trim();
            String match = m.group(4).trim();
            String rc = m.group(5).trim();
            int begin = getBegin(lc, match);
            int originalMatchBegin = getBegin("", match);
            int end = getEnd(rc, match);
            int originalMatchEnd = getEnd("", match);

            if (!lc.isEmpty()) {
                lc = getText(lc).trim() + " ";
            }/*from  w w w .  j a  v  a2s .c  om*/
            match = getText(match);
            if (!rc.isEmpty()) {
                rc = " " + getText(rc).trim();
            }

            String coveredText = (lc + match + rc);
            if (coveredText.length() < EvaluationItem.MAX_COLUMN_LENGTH) {
                EvaluationItem item = new EvaluationItem(corpus, documentId, type, begin, end, coveredText);
                item.setMatchOnItemText(lc.length(), lc.length() + match.length());
                item.setMatchOnOriginalText(originalMatchBegin, originalMatchEnd);
                items.add(item);
            } else {
                log.warn("Ignored oversized match in collection [" + corpus + "] document [" + documentId
                        + "] at [" + begin + "-" + end + "]");
            }
        } else {
            log.debug("Regexp [" + regexp + "] did not match on [" + line + "]");
        }
    }
    return items;
}

From source file:org.springjutsu.validation.spel.SPELResolver.java

/**
 * Resolves one or more SPEL expressions in the given string.
 * @param elContainng A string potentially containing one or more SPEL expressions
 * @return Either an object represented by the expression (if the entire string
 *  was an expression) or a new String with all SPEL expressions replaced by
 *  the string value of the respective resolved objects.
 */// ww w. j a  v  a2  s. c o m
public Object resolveSPELString(String elContaining) {
    // if the whole thing is a single EL string, try to get the object.
    if (elContaining.matches(EXPRESSION_MATCHER)) {
        String resolvableElString = elContaining.substring(2, elContaining.length() - 1) + "?: null";
        Object elResult = getBySpel(resolvableElString);
        return elResult;
    } else {
        // otherwise, do string value substitution to build a value.
        String elResolvable = elContaining;
        Matcher matcher = Pattern.compile(EXPRESSION_MATCHER).matcher(elResolvable);
        while (matcher.find()) {
            String elString = matcher.group();
            String resolvableElString = elString.substring(2, elString.length() - 1) + "?: null";
            Object elResult = getBySpel(resolvableElString);
            String resolvedElString = elResult != null ? String.valueOf(elResult) : "";
            elResolvable = elResolvable.replace(elString, resolvedElString);
            matcher.reset(elResolvable);
        }
        return elResolvable;
    }
}

From source file:org.geoserver.web.wicket.GeoServerDataProvider.java

private List<T> filterByKeywords(List<T> items) {
    List<T> result = new ArrayList<T>();

    final Matcher[] matchers = getMatchers();

    List<Property<T>> properties = getProperties();
    for (T item : items) {
        ITEM://from   ww  w  .ja  v a2  s  . c om
        // find any match of any pattern over any property
        for (Property<T> property : properties) {
            if (property.isSearchable()) {
                Object value = property.getPropertyValue(item);
                if (value != null) {
                    // brute force check for keywords
                    for (Matcher matcher : matchers) {
                        matcher.reset(String.valueOf(value));
                        if (matcher.matches()) {
                            result.add(item);
                            break ITEM;
                        }
                    }
                }
            }
        }
    }

    return result;
}