Search in sources :

Example 96 with Pattern

use of java.util.regex.Pattern in project hackpad by dropbox.

the class CssCompressor method extractDataUrls.

// Leave data urls alone to increase parse performance.
protected String extractDataUrls(String css, ArrayList preservedTokens) {
    int maxIndex = css.length() - 1;
    int appendIndex = 0;
    StringBuffer sb = new StringBuffer();
    Pattern p = Pattern.compile("(?i)url\\(\\s*([\"']?)data\\:");
    Matcher m = p.matcher(css);
    while (m.find()) {
        // "url(".length()
        int startIndex = m.start() + 4;
        // ', " or empty (not quoted)
        String terminator = m.group(1);
        if (terminator.length() == 0) {
            terminator = ")";
        }
        boolean foundTerminator = false;
        int endIndex = m.end() - 1;
        while (foundTerminator == false && endIndex + 1 <= maxIndex) {
            endIndex = css.indexOf(terminator, endIndex + 1);
            if ((endIndex > 0) && (css.charAt(endIndex - 1) != '\\')) {
                foundTerminator = true;
                if (!")".equals(terminator)) {
                    endIndex = css.indexOf(")", endIndex);
                }
            }
        }
        // Enough searching, start moving stuff over to the buffer
        sb.append(css.substring(appendIndex, m.start()));
        if (foundTerminator) {
            String token = css.substring(startIndex, endIndex);
            token = token.replaceAll("\\s+", "");
            preservedTokens.add(token);
            String preserver = "url(___YUICSSMIN_PRESERVED_TOKEN_" + (preservedTokens.size() - 1) + "___)";
            sb.append(preserver);
            appendIndex = endIndex + 1;
        } else {
            // No end terminator found, re-add the whole match. Should we throw/warn here?
            sb.append(css.substring(m.start(), m.end()));
            appendIndex = m.end();
        }
    }
    sb.append(css.substring(appendIndex));
    return sb.toString();
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher)

Example 97 with Pattern

use of java.util.regex.Pattern in project deeplearning4j by deeplearning4j.

the class StringUtils method allMatches.

public static List<Matcher> allMatches(String str, String regex) {
    Pattern p = Pattern.compile(regex);
    List<Matcher> matches = new ArrayList<>();
    while (true) {
        Matcher m = p.matcher(str);
        if (!m.find())
            break;
        matches.add(m);
        str = str.substring(m.end());
    }
    return matches;
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher)

Example 98 with Pattern

use of java.util.regex.Pattern in project druid by druid-io.

the class Granularity method getDateValues.

// Used by the toDate implementations.
final Integer[] getDateValues(String filePath, Formatter formatter) {
    Pattern pattern = defaultPathPattern;
    switch(formatter) {
        case DEFAULT:
        case LOWER_DEFAULT:
            break;
        case HIVE:
            pattern = hivePathPattern;
            break;
        default:
            throw new IAE("Format %s not supported", formatter);
    }
    Matcher matcher = pattern.matcher(filePath);
    // The size is "7" b/c this array contains standard
    // datetime field values namely:
    // year, monthOfYear, dayOfMonth, hourOfDay, minuteOfHour, secondOfMinute,
    // and index 0 is unused.
    Integer[] vals = new Integer[7];
    if (matcher.matches()) {
        for (int i = 1; i <= matcher.groupCount(); i++) {
            vals[i] = (matcher.group(i) != null) ? Integer.parseInt(matcher.group(i)) : null;
        }
    }
    return vals;
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) IAE(io.druid.java.util.common.IAE)

Example 99 with Pattern

use of java.util.regex.Pattern in project druid by druid-io.

the class S3TimestampVersionedDataFinderTest method testMissing.

@Test
public void testMissing() throws S3ServiceException {
    String bucket = "bucket";
    String keyPrefix = "prefix/dir/0";
    RestS3Service s3Client = EasyMock.createStrictMock(RestS3Service.class);
    S3Object object0 = new S3Object(), object1 = new S3Object();
    object0.setBucketName(bucket);
    object0.setKey(keyPrefix + "/renames-0.gz");
    object0.setLastModifiedDate(new Date(0));
    object1.setBucketName(bucket);
    object1.setKey(keyPrefix + "/renames-1.gz");
    object1.setLastModifiedDate(new Date(1));
    EasyMock.expect(s3Client.listObjects(EasyMock.eq(bucket), EasyMock.anyString(), EasyMock.<String>isNull())).andReturn(null).once();
    S3TimestampVersionedDataFinder finder = new S3TimestampVersionedDataFinder(s3Client);
    Pattern pattern = Pattern.compile("renames-[0-9]*\\.gz");
    EasyMock.replay(s3Client);
    URI latest = finder.getLatestVersion(URI.create(String.format("s3://%s/%s", bucket, keyPrefix)), pattern);
    EasyMock.verify(s3Client);
    Assert.assertEquals(null, latest);
}
Also used : Pattern(java.util.regex.Pattern) RestS3Service(org.jets3t.service.impl.rest.httpclient.RestS3Service) S3Object(org.jets3t.service.model.S3Object) URI(java.net.URI) Date(java.util.Date) Test(org.junit.Test)

Example 100 with Pattern

use of java.util.regex.Pattern in project druid by druid-io.

the class S3TimestampVersionedDataFinderTest method testFindSelf.

@Test
public void testFindSelf() throws S3ServiceException {
    String bucket = "bucket";
    String keyPrefix = "prefix/dir/0";
    RestS3Service s3Client = EasyMock.createStrictMock(RestS3Service.class);
    S3Object object0 = new S3Object();
    object0.setBucketName(bucket);
    object0.setKey(keyPrefix + "/renames-0.gz");
    object0.setLastModifiedDate(new Date(0));
    EasyMock.expect(s3Client.listObjects(EasyMock.eq(bucket), EasyMock.anyString(), EasyMock.<String>isNull())).andReturn(new S3Object[] { object0 }).once();
    S3TimestampVersionedDataFinder finder = new S3TimestampVersionedDataFinder(s3Client);
    Pattern pattern = Pattern.compile("renames-[0-9]*\\.gz");
    EasyMock.replay(s3Client);
    URI latest = finder.getLatestVersion(URI.create(String.format("s3://%s/%s", bucket, keyPrefix)), pattern);
    EasyMock.verify(s3Client);
    URI expected = URI.create(String.format("s3://%s/%s", bucket, object0.getKey()));
    Assert.assertEquals(expected, latest);
}
Also used : Pattern(java.util.regex.Pattern) RestS3Service(org.jets3t.service.impl.rest.httpclient.RestS3Service) S3Object(org.jets3t.service.model.S3Object) URI(java.net.URI) Date(java.util.Date) Test(org.junit.Test)

Aggregations

Pattern (java.util.regex.Pattern)2745 Matcher (java.util.regex.Matcher)1806 ArrayList (java.util.ArrayList)334 Test (org.junit.Test)209 IOException (java.io.IOException)206 File (java.io.File)172 HashMap (java.util.HashMap)144 Field (java.lang.reflect.Field)118 BufferedReader (java.io.BufferedReader)108 Map (java.util.Map)97 PatternSyntaxException (java.util.regex.PatternSyntaxException)97 List (java.util.List)81 HashSet (java.util.HashSet)69 InputStreamReader (java.io.InputStreamReader)59 FileInputStream (java.io.FileInputStream)40 InputStream (java.io.InputStream)39 FileReader (java.io.FileReader)36 SmallTest (android.test.suitebuilder.annotation.SmallTest)31 URL (java.net.URL)29 LinkedHashMap (java.util.LinkedHashMap)28