Search in sources :

Example 31 with PatternSyntaxException

use of java.util.regex.PatternSyntaxException in project jdk8u_jdk by JetBrains.

the class SynthParser method startBind.

private void startBind(Attributes attributes) throws SAXException {
    ParsedSynthStyle style = null;
    String path = null;
    int type = -1;
    for (int i = attributes.getLength() - 1; i >= 0; i--) {
        String key = attributes.getQName(i);
        if (key.equals(ATTRIBUTE_STYLE)) {
            style = (ParsedSynthStyle) lookup(attributes.getValue(i), ParsedSynthStyle.class);
        } else if (key.equals(ATTRIBUTE_TYPE)) {
            String typeS = attributes.getValue(i).toUpperCase();
            if (typeS.equals("NAME")) {
                type = DefaultSynthStyleFactory.NAME;
            } else if (typeS.equals("REGION")) {
                type = DefaultSynthStyleFactory.REGION;
            } else {
                throw new SAXException("bind: unknown type " + typeS);
            }
        } else if (key.equals(ATTRIBUTE_KEY)) {
            path = attributes.getValue(i);
        }
    }
    if (style == null || path == null || type == -1) {
        throw new SAXException("bind: you must specify a style, type " + "and key");
    }
    try {
        _factory.addStyle(style, path, type);
    } catch (PatternSyntaxException pse) {
        throw new SAXException("bind: " + path + " is not a valid " + "regular expression");
    }
}
Also used : SAXException(org.xml.sax.SAXException) PatternSyntaxException(java.util.regex.PatternSyntaxException)

Example 32 with PatternSyntaxException

use of java.util.regex.PatternSyntaxException in project JMRI by JMRI.

the class DCCppMessage method match.

/**
     * matches the given string against the given Regex pattern.
     * 
     * @param s    string to be matched
     * @param pat  Regex string to match against
     * @param name Text name to use in debug messages.
     * @return Matcher or null if no match
     */
private static Matcher match(String s, String pat, String name) {
    try {
        Pattern p = Pattern.compile(pat);
        Matcher m = p.matcher(s);
        if (!m.matches()) {
            //log.warn("No Match {} Command: {} Pattern: {}",name, s, pat);
            return (null);
        }
        return (m);
    } catch (PatternSyntaxException e) {
        log.error("Malformed DCC++ message syntax! s = ", pat);
        return (null);
    } catch (IllegalStateException e) {
        log.error("Group called before match operation executed string= " + s);
        return (null);
    } catch (IndexOutOfBoundsException e) {
        log.error("Index out of bounds string= " + s);
        return (null);
    }
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) PatternSyntaxException(java.util.regex.PatternSyntaxException)

Example 33 with PatternSyntaxException

use of java.util.regex.PatternSyntaxException in project JMRI by JMRI.

the class DCCppReply method match.

protected static Matcher match(String s, String pat, String name) {
    try {
        Pattern p = Pattern.compile(pat);
        Matcher m = p.matcher(s);
        if (!m.matches()) {
            //log.debug("No Match {} Command: {} pattern {}",name, s, pat);
            return (null);
        }
        return (m);
    } catch (PatternSyntaxException e) {
        log.error("Malformed DCC++ reply syntax! s = ", pat);
        return (null);
    } catch (IllegalStateException e) {
        log.error("Group called before match operation executed string= " + s);
        return (null);
    } catch (IndexOutOfBoundsException e) {
        log.error("Index out of bounds string= " + s);
        return (null);
    }
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) PatternSyntaxException(java.util.regex.PatternSyntaxException)

Example 34 with PatternSyntaxException

use of java.util.regex.PatternSyntaxException in project dhis2-core by dhis2.

the class DateUnitPeriodTypeParser method parse.

@Override
public DateInterval parse(Calendar calendar, String period) {
    DateUnitType dateUnitType = DateUnitType.find(period);
    if (dateUnitType == null) {
        return null;
    }
    if (compileCache.get(dateUnitType.getName()) == null) {
        try {
            Pattern pattern = Pattern.compile(dateUnitType.getPattern());
            compileCache.put(dateUnitType.getName(), pattern);
        } catch (PatternSyntaxException ex) {
            return null;
        }
    }
    Pattern pattern = compileCache.get(dateUnitType.getName());
    Matcher matcher = pattern.matcher(period);
    boolean match = matcher.find();
    if (!match) {
        return null;
    }
    if (DateUnitType.DAILY == dateUnitType) {
        int year = Integer.parseInt(matcher.group(1));
        int month = Integer.parseInt(matcher.group(2));
        int day = Integer.parseInt(matcher.group(3));
        DateTimeUnit dateTimeUnit = new DateTimeUnit(year, month, day, calendar.isIso8601());
        dateTimeUnit.setDayOfWeek(calendar.weekday(dateTimeUnit));
        return new DateInterval(dateTimeUnit, dateTimeUnit);
    } else if (DateUnitType.WEEKLY == dateUnitType || DateUnitType.WEEKLY_WEDNESDAY == dateUnitType || DateUnitType.WEEKLY_THURSDAY == dateUnitType || DateUnitType.WEEKLY_SATURDAY == dateUnitType || DateUnitType.WEEKLY_SUNDAY == dateUnitType) {
        int year = Integer.parseInt(matcher.group(1));
        int week = Integer.parseInt(matcher.group(2));
        WeeklyAbstractPeriodType periodType = (WeeklyAbstractPeriodType) PeriodType.getByNameIgnoreCase(dateUnitType.getName());
        if (periodType == null || week < 1 || week > calendar.weeksInYear(year)) {
            return null;
        }
        DateTimeUnit start = new DateTimeUnit(year, 1, 1, calendar.isIso8601());
        start = periodType.adjustToStartOfWeek(start, calendar);
        // since we rewind to start of week, we might end up in the previous years weeks, so we check and forward if needed
        if (calendar.isoWeek(start) == calendar.weeksInYear(year)) {
            start = calendar.plusWeeks(start, 1);
        }
        start = calendar.plusWeeks(start, week - 1);
        DateTimeUnit end = new DateTimeUnit(start);
        end = calendar.plusWeeks(end, 1);
        end = calendar.minusDays(end, 1);
        start.setDayOfWeek(calendar.weekday(start));
        end.setDayOfWeek(calendar.weekday(end));
        return new DateInterval(start, end);
    } else if (DateUnitType.MONTHLY == dateUnitType) {
        int year = Integer.parseInt(matcher.group(1));
        int month = Integer.parseInt(matcher.group(2));
        DateTimeUnit start = new DateTimeUnit(year, month, 1, calendar.isIso8601());
        DateTimeUnit end = new DateTimeUnit(year, month, calendar.daysInMonth(start.getYear(), start.getMonth()), calendar.isIso8601());
        start.setDayOfWeek(calendar.weekday(start));
        end.setDayOfWeek(calendar.weekday(end));
        return new DateInterval(start, end);
    } else if (DateUnitType.BI_MONTHLY == dateUnitType) {
        int year = Integer.parseInt(matcher.group(1));
        int month = Integer.parseInt(matcher.group(2));
        if (month < 1 || month > 6) {
            return null;
        }
        DateTimeUnit start = new DateTimeUnit(year, (month * 2) - 1, 1, calendar.isIso8601());
        DateTimeUnit end = new DateTimeUnit(start);
        end = calendar.plusMonths(end, 2);
        end = calendar.minusDays(end, 1);
        start.setDayOfWeek(calendar.weekday(start));
        end.setDayOfWeek(calendar.weekday(end));
        return new DateInterval(start, end);
    } else if (DateUnitType.QUARTERLY == dateUnitType) {
        int year = Integer.parseInt(matcher.group(1));
        int quarter = Integer.parseInt(matcher.group(2));
        // valid quarters are from 1 - 4
        if (quarter < 1 || quarter > 4) {
            return null;
        }
        DateTimeUnit start = new DateTimeUnit(year, ((quarter - 1) * 3) + 1, 1, calendar.isIso8601());
        DateTimeUnit end = new DateTimeUnit(start);
        end = calendar.plusMonths(end, 3);
        end = calendar.minusDays(end, 1);
        start.setDayOfWeek(calendar.weekday(start));
        end.setDayOfWeek(calendar.weekday(end));
        return new DateInterval(start, end);
    } else if (DateUnitType.SIX_MONTHLY == dateUnitType) {
        int year = Integer.parseInt(matcher.group(1));
        int semester = Integer.parseInt(matcher.group(2));
        // valid six-monthly are from 1 - 2
        if (semester < 1 || semester > 2) {
            return null;
        }
        DateTimeUnit start = new DateTimeUnit(year, semester == 1 ? 1 : 7, 1, calendar.isIso8601());
        DateTimeUnit end = new DateTimeUnit(start);
        end = calendar.plusMonths(end, 6);
        end = calendar.minusDays(end, 1);
        start.setDayOfWeek(calendar.weekday(start));
        end.setDayOfWeek(calendar.weekday(end));
        return new DateInterval(start, end);
    } else if (DateUnitType.SIX_MONTHLY_APRIL == dateUnitType) {
        int year = Integer.parseInt(matcher.group(1));
        int semester = Integer.parseInt(matcher.group(2));
        // valid six-monthly are from 1 - 2
        if (semester < 1 || semester > 2) {
            return null;
        }
        DateTimeUnit start = new DateTimeUnit(year, semester == 1 ? 4 : 10, 1, calendar.isIso8601());
        DateTimeUnit end = new DateTimeUnit(start);
        end = calendar.plusMonths(end, 6);
        end = calendar.minusDays(end, 1);
        start.setDayOfWeek(calendar.weekday(start));
        end.setDayOfWeek(calendar.weekday(end));
        return new DateInterval(start, end);
    } else if (DateUnitType.YEARLY == dateUnitType) {
        int year = Integer.parseInt(matcher.group(1));
        DateTimeUnit start = new DateTimeUnit(year, 1, 1, calendar.isIso8601());
        DateTimeUnit end = new DateTimeUnit(year, calendar.monthsInYear(), calendar.daysInMonth(start.getYear(), calendar.monthsInYear()), calendar.isIso8601());
        start.setDayOfWeek(calendar.weekday(start));
        end.setDayOfWeek(calendar.weekday(end));
        return new DateInterval(start, end);
    } else if (DateUnitType.FINANCIAL_APRIL == dateUnitType) {
        int year = Integer.parseInt(matcher.group(1));
        DateTimeUnit start = new DateTimeUnit(year, 4, 1, calendar.isIso8601());
        DateTimeUnit end = new DateTimeUnit(start);
        end = calendar.plusYears(end, 1);
        end = calendar.minusDays(end, 1);
        start.setDayOfWeek(calendar.weekday(start));
        end.setDayOfWeek(calendar.weekday(end));
        return new DateInterval(start, end);
    } else if (DateUnitType.FINANCIAL_JULY == dateUnitType) {
        int year = Integer.parseInt(matcher.group(1));
        DateTimeUnit start = new DateTimeUnit(year, 7, 1, calendar.isIso8601());
        DateTimeUnit end = new DateTimeUnit(start);
        end = calendar.plusYears(end, 1);
        end = calendar.minusDays(end, 1);
        start.setDayOfWeek(calendar.weekday(start));
        end.setDayOfWeek(calendar.weekday(end));
        return new DateInterval(start, end);
    } else if (DateUnitType.FINANCIAL_OCTOBER == dateUnitType) {
        int year = Integer.parseInt(matcher.group(1));
        DateTimeUnit start = new DateTimeUnit(year, 10, 1, calendar.isIso8601());
        DateTimeUnit end = new DateTimeUnit(start);
        end = calendar.plusYears(end, 1);
        end = calendar.minusDays(end, 1);
        start.setDayOfWeek(calendar.weekday(start));
        end.setDayOfWeek(calendar.weekday(end));
        return new DateInterval(start, end);
    }
    return null;
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) WeeklyAbstractPeriodType(org.hisp.dhis.period.WeeklyAbstractPeriodType) PatternSyntaxException(java.util.regex.PatternSyntaxException)

Example 35 with PatternSyntaxException

use of java.util.regex.PatternSyntaxException in project sling by apache.

the class BundleTestsProvider method getTestClasses.

/** Get test classes that bundle b provides (as done in Felix/Sigil) */
private List<String> getTestClasses(Bundle b) {
    final List<String> result = new ArrayList<String>();
    Pattern testClassRegexp = null;
    final String headerValue = getSlingTestRegexp(b);
    if (headerValue != null) {
        try {
            testClassRegexp = Pattern.compile(headerValue);
        } catch (PatternSyntaxException pse) {
            log.warn("Invalid pattern '" + headerValue + "' for bundle " + b.getSymbolicName() + ", ignored", pse);
        }
    }
    if (testClassRegexp == null) {
        log.info("Bundle {} does not have {} header, not looking for test classes", SLING_TEST_REGEXP);
    } else if (Bundle.ACTIVE != b.getState()) {
        log.info("Bundle {} is not active, no test classes considered", b.getSymbolicName());
    } else {
        @SuppressWarnings("unchecked") Enumeration<URL> classUrls = b.findEntries("", "*.class", true);
        while (classUrls.hasMoreElements()) {
            URL url = classUrls.nextElement();
            final String name = toClassName(url);
            if (testClassRegexp.matcher(name).matches()) {
                result.add(name);
            } else {
                log.debug("Class {} does not match {} pattern {} of bundle {}, ignored", new Object[] { name, SLING_TEST_REGEXP, testClassRegexp, b.getSymbolicName() });
            }
        }
        log.info("{} test classes found in bundle {}", result.size(), b.getSymbolicName());
    }
    return result;
}
Also used : Pattern(java.util.regex.Pattern) Enumeration(java.util.Enumeration) ArrayList(java.util.ArrayList) URL(java.net.URL) PatternSyntaxException(java.util.regex.PatternSyntaxException)

Aggregations

PatternSyntaxException (java.util.regex.PatternSyntaxException)355 Pattern (java.util.regex.Pattern)190 Matcher (java.util.regex.Matcher)115 ArrayList (java.util.ArrayList)46 IOException (java.io.IOException)25 List (java.util.List)19 File (java.io.File)14 Map (java.util.Map)12 HashMap (java.util.HashMap)9 URL (java.net.URL)7 HashSet (java.util.HashSet)7 Iterator (java.util.Iterator)7 InvalidSettingsException (org.knime.core.node.InvalidSettingsException)7 BufferedReader (java.io.BufferedReader)6 Collection (java.util.Collection)6 LinkedList (java.util.LinkedList)5 Test (org.junit.Test)5 InputStreamReader (java.io.InputStreamReader)4 SQLException (java.sql.SQLException)4 LinkedHashMap (java.util.LinkedHashMap)4