Search in sources :

Example 11 with Matcher

use of java.util.regex.Matcher in project buck by facebook.

the class CxxBoostTest method parseResults.

@Override
protected ImmutableList<TestResultSummary> parseResults(Path exitCode, Path output, Path results) throws Exception {
    ImmutableList.Builder<TestResultSummary> summariesBuilder = ImmutableList.builder();
    // Process the test run output to grab the individual test stdout/stderr and
    // test run times.
    Map<String, String> messages = Maps.newHashMap();
    Map<String, List<String>> stdout = Maps.newHashMap();
    Map<String, Long> times = Maps.newHashMap();
    try (BufferedReader reader = Files.newBufferedReader(output, Charsets.US_ASCII)) {
        Stack<String> testSuite = new Stack<>();
        Optional<String> currentTest = Optional.empty();
        String line;
        while ((line = reader.readLine()) != null) {
            Matcher matcher;
            if ((matcher = SUITE_START.matcher(line)).matches()) {
                String suite = matcher.group(1);
                testSuite.push(suite);
            } else if ((matcher = SUITE_END.matcher(line)).matches()) {
                String suite = matcher.group(1);
                Preconditions.checkState(testSuite.peek().equals(suite));
                testSuite.pop();
            } else if ((matcher = CASE_START.matcher(line)).matches()) {
                String test = Joiner.on(".").join(testSuite) + "." + matcher.group(1);
                currentTest = Optional.of(test);
                stdout.put(test, Lists.newArrayList());
            } else if ((matcher = CASE_END.matcher(line)).matches()) {
                String test = Joiner.on(".").join(testSuite) + "." + matcher.group(1);
                Preconditions.checkState(currentTest.isPresent() && currentTest.get().equals(test));
                String time = matcher.group(2);
                times.put(test, time == null ? 0 : Long.valueOf(time));
                currentTest = Optional.empty();
            } else if (currentTest.isPresent()) {
                if (ERROR.matcher(line).matches()) {
                    messages.put(currentTest.get(), line);
                } else {
                    Preconditions.checkNotNull(stdout.get(currentTest.get())).add(line);
                }
            }
        }
    }
    // Parse the XML result file for the actual test result summaries.
    Document doc = XmlDomParser.parse(results);
    Node testResult = doc.getElementsByTagName("TestResult").item(0);
    Node testSuite = testResult.getFirstChild();
    visitTestSuite(summariesBuilder, messages, stdout, times, "", testSuite);
    return summariesBuilder.build();
}
Also used : Matcher(java.util.regex.Matcher) ImmutableList(com.google.common.collect.ImmutableList) Node(org.w3c.dom.Node) TestResultSummary(com.facebook.buck.test.TestResultSummary) Document(org.w3c.dom.Document) Stack(java.util.Stack) BufferedReader(java.io.BufferedReader) ImmutableList(com.google.common.collect.ImmutableList) NodeList(org.w3c.dom.NodeList) List(java.util.List)

Example 12 with Matcher

use of java.util.regex.Matcher in project buck by facebook.

the class CxxDescriptionEnhancer method getNonDefaultSharedLibrarySoname.

@VisibleForTesting
static String getNonDefaultSharedLibrarySoname(String declared, String sharedLibraryExtension, String sharedLibraryVersionedExtensionFormat) {
    Matcher match = SONAME_EXT_MACRO_PATTERN.matcher(declared);
    if (!match.find()) {
        return declared;
    }
    String version = match.group(1);
    if (version == null) {
        return match.replaceFirst(sharedLibraryExtension);
    }
    return match.replaceFirst(String.format(sharedLibraryVersionedExtensionFormat, version));
}
Also used : Matcher(java.util.regex.Matcher) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 13 with Matcher

use of java.util.regex.Matcher in project buck by facebook.

the class VerbosityParser method parse.

public static Verbosity parse(String... args) {
    for (int i = 0; i < args.length && !"--".equals(args[i]); i++) {
        String arg = args[i];
        if ((VERBOSE_LONG_ARG.equals(arg) || VERBOSE_SHORT_ARG.equals(arg)) && i < args.length - 1) {
            String nextArg = args[i + 1];
            int verbosityLevel = Integer.parseInt(nextArg, /* radix */
            10);
            return getVerbosityForLevel(verbosityLevel);
        }
        Matcher matcher = VERBOSE_ARG_PATTERN.matcher(arg);
        if (matcher.matches()) {
            int verbosityLevel = Integer.parseInt(matcher.group(1), /* radix */
            10);
            return getVerbosityForLevel(verbosityLevel);
        }
    }
    return DEFAULT_VERBOSITY;
}
Also used : Matcher(java.util.regex.Matcher)

Example 14 with Matcher

use of java.util.regex.Matcher in project android by cSploit.

the class NetworkHelper method getIfaceGateway.

public static String getIfaceGateway(String iface) {
    Pattern pattern = Pattern.compile(String.format("^%s\\t+00000000\\t+([0-9A-F]{8})", iface), Pattern.CASE_INSENSITIVE);
    BufferedReader reader = null;
    String line;
    try {
        reader = new BufferedReader(new InputStreamReader(new FileInputStream("/proc/net/route")));
        while ((line = reader.readLine()) != null) {
            Matcher matcher = pattern.matcher(line);
            if (!matcher.find()) {
                continue;
            }
            String rawAddress = matcher.group(1);
            StringBuilder sb = new StringBuilder();
            for (int i = 6; ; i -= 2) {
                String part = rawAddress.substring(i, i + 2);
                sb.append(Integer.parseInt(part, 16));
                if (i > 0) {
                    sb.append('.');
                } else {
                    break;
                }
            }
            String res = sb.toString();
            Logger.debug("found system default gateway for interface " + iface + ": " + res);
            return res;
        }
    } catch (IOException e) {
        System.errorLogging(e);
    } finally {
        IOUtils.closeQuietly(reader);
    }
    Logger.warning("falling back to ip");
    return getIfaceGatewayUsingIp(iface);
}
Also used : Pattern(java.util.regex.Pattern) InputStreamReader(java.io.InputStreamReader) Matcher(java.util.regex.Matcher) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream)

Example 15 with Matcher

use of java.util.regex.Matcher in project lucida by claritylab.

the class IndriDocumentKM method transformQueryString.

/**
	 * Returns a representation of the query string that is suitable for Indri.
	 * 
	 * @param qs query string
	 * @return query string for Indri
	 */
public static String transformQueryString(String qs) {
    // drop characters that are not properly supported by Indri
    // ('.' is only allowed in between digits)
    qs = qs.replaceAll("&\\w++;", " ");
    qs = qs.replaceAll(FORBIDDEN_CHAR, " ");
    String dotsRemoved = "";
    for (int i = 0; i < qs.length(); i++) if (qs.charAt(i) != '.' || (i > 0 && i < qs.length() - 1 && Character.isDigit(qs.charAt(i - 1)) && Character.isDigit(qs.charAt(i + 1))))
        dotsRemoved += qs.charAt(i);
    qs = dotsRemoved;
    // replace ... OR ... by #or(... ...)
    Matcher m = Pattern.compile("((\\([^\\(\\)]*+\\)|\\\"[^\\\"]*+\\\"|[^\\s\\(\\)]++) OR )++" + "(\\([^\\(\\)]*+\\)|\\\"[^\\\"]*+\\\"|[^\\s\\(\\)]++)").matcher(qs);
    while (m.find()) qs = qs.replace(m.group(0), "#or(" + m.group(0) + ")");
    qs = qs.replace(" OR", "");
    // replace ... AND ... by #combine(... ...)
    m = Pattern.compile("((\\([^\\(\\)]*+\\)|\\\"[^\\\"]*+\\\"|[^\\s\\(\\)]++) AND )++" + "(\\([^\\(\\)]*+\\)|\\\"[^\\\"]*+\\\"|[^\\s\\(\\)]++)").matcher(qs);
    while (m.find()) qs = qs.replace(m.group(0), "#combine(" + m.group(0) + ")");
    qs = qs.replace(" AND", "");
    // replace "..." by #1(...)
    m = Pattern.compile("\"([^\"]*+)\"").matcher(qs);
    while (m.find()) qs = qs.replace(m.group(0), "#1(" + m.group(1) + ")");
    return qs;
}
Also used : Matcher(java.util.regex.Matcher)

Aggregations

Matcher (java.util.regex.Matcher)12640 Pattern (java.util.regex.Pattern)5059 ArrayList (java.util.ArrayList)1525 IOException (java.io.IOException)913 HashMap (java.util.HashMap)575 File (java.io.File)490 Test (org.junit.Test)448 BufferedReader (java.io.BufferedReader)433 Map (java.util.Map)369 List (java.util.List)292 InputStreamReader (java.io.InputStreamReader)268 HashSet (java.util.HashSet)237 MalformedURLException (java.net.MalformedURLException)164 URL (java.net.URL)157 Date (java.util.Date)153 InputStream (java.io.InputStream)148 Field (java.lang.reflect.Field)130 ParseException (java.text.ParseException)130 PatternSyntaxException (java.util.regex.PatternSyntaxException)128 LinkedHashMap (java.util.LinkedHashMap)122