Search in sources :

Example 91 with PatternSyntaxException

use of java.util.regex.PatternSyntaxException in project dna by leifeld.

the class ImportOldDNA method patternToString.

public String patternToString(String text, String pattern) {
    Pattern p;
    try {
        p = Pattern.compile(pattern);
    } catch (PatternSyntaxException e) {
        return ("");
    }
    Matcher m = p.matcher(text);
    if (m.find()) {
        try {
            String string = m.group(0);
            return string;
        } catch (IndexOutOfBoundsException e) {
            return ("");
        }
    } else {
        return "";
    }
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) PatternSyntaxException(java.util.regex.PatternSyntaxException)

Example 92 with PatternSyntaxException

use of java.util.regex.PatternSyntaxException in project dna by leifeld.

the class RecodeDialog method patternToString.

public String patternToString(String text, String pattern) {
    Pattern p;
    try {
        p = Pattern.compile(pattern);
    } catch (PatternSyntaxException e) {
        return ("");
    }
    Matcher m = p.matcher(text);
    if (m.find()) {
        try {
            String string = m.group(0);
            return string;
        } catch (IndexOutOfBoundsException e) {
            return ("");
        }
    } else {
        return "";
    }
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) PatternSyntaxException(java.util.regex.PatternSyntaxException)

Example 93 with PatternSyntaxException

use of java.util.regex.PatternSyntaxException in project whole by wholeplatform.

the class GrammarsValidatorVisitor method checkPatterns.

public void checkPatterns(Grammar entity) {
    AbstractPatternFilterIterator<RegExp> i = IteratorFactory.<RegExp>descendantOrSelfMatcherIterator().withPattern(GrammarsEntityDescriptorEnum.RegExp);
    i.reset(entity);
    for (RegExp regex : i) {
        try {
            Pattern.compile(regex.getValue());
        } catch (PatternSyntaxException e) {
            getDecorationManager().addError(regex, e.toString(), regex.getValue());
        }
    }
}
Also used : RegExp(org.whole.lang.grammars.model.RegExp) PatternSyntaxException(java.util.regex.PatternSyntaxException)

Example 94 with PatternSyntaxException

use of java.util.regex.PatternSyntaxException in project pwm by pwm-project.

the class LocalDBLogger method checkEventForParams.

boolean checkEventForParams(final PwmLogEvent event, final LocalDBSearchQuery searchParameters) {
    if (event == null) {
        return false;
    }
    boolean eventMatchesParams = true;
    if (searchParameters.getMinimumLevel() != null) {
        if (event.getLevel().compareTo(searchParameters.getMinimumLevel()) <= -1) {
            eventMatchesParams = false;
        }
    }
    Pattern pattern = null;
    try {
        if (searchParameters.getUsername() != null && searchParameters.getUsername().length() > 0) {
            pattern = Pattern.compile(searchParameters.getUsername());
        }
    } catch (PatternSyntaxException e) {
        LOGGER.trace("invalid regex syntax for " + searchParameters.getUsername() + ", reverting to plaintext search");
    }
    if (pattern != null) {
        final Matcher matcher = pattern.matcher(event.getActor() == null ? "" : event.getActor());
        if (!matcher.find()) {
            eventMatchesParams = false;
        }
    } else if (eventMatchesParams && (searchParameters.getUsername() != null && searchParameters.getUsername().length() > 1)) {
        final String eventUsername = event.getActor();
        if (eventUsername == null || !eventUsername.equalsIgnoreCase(searchParameters.getUsername())) {
            eventMatchesParams = false;
        }
    }
    if (eventMatchesParams && (searchParameters.getText() != null && searchParameters.getText().length() > 0)) {
        final String eventMessage = event.getMessage();
        final String textLowercase = searchParameters.getText().toLowerCase();
        boolean isAMatch = false;
        if (eventMessage != null && eventMessage.length() > 0) {
            if (eventMessage.toLowerCase().contains(textLowercase)) {
                isAMatch = true;
            } else if (event.getTopic() != null && event.getTopic().length() > 0) {
                if (event.getTopic().toLowerCase().contains(textLowercase)) {
                    isAMatch = true;
                }
            }
            if (!isAMatch) {
                eventMatchesParams = false;
            }
        }
    }
    if (searchParameters.getEventType() != null) {
        if (searchParameters.getEventType() == EventType.System) {
            if (event.getActor() != null && event.getActor().length() > 0) {
                eventMatchesParams = false;
            }
        } else if (searchParameters.getEventType() == EventType.User) {
            if (event.getActor() == null || event.getActor().length() < 1) {
                eventMatchesParams = false;
            }
        }
    }
    return eventMatchesParams;
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) PatternSyntaxException(java.util.regex.PatternSyntaxException)

Example 95 with PatternSyntaxException

use of java.util.regex.PatternSyntaxException in project pwm by pwm-project.

the class UrlShortenerService method shortenUrlInText.

public String shortenUrlInText(final String text) throws PwmUnrecoverableException {
    final String urlRegex = pwmApplication.getConfig().readAppProperty(AppProperty.URL_SHORTNER_URL_REGEX);
    try {
        final Pattern p = Pattern.compile(urlRegex);
        final Matcher m = p.matcher(text);
        final StringBuilder result = new StringBuilder();
        Boolean found = m.find();
        if (found) {
            int start = 0;
            int end = m.start();
            result.append(text.substring(start, end));
            start = end;
            end = m.end();
            while (found) {
                result.append(shortenUrl(text.substring(start, end)));
                start = end;
                found = m.find();
                if (found) {
                    end = m.start();
                    result.append(text.substring(start, end));
                    start = end;
                    end = m.end();
                }
            }
            result.append(text.substring(end));
            return result.toString();
        }
    } catch (PatternSyntaxException e) {
        LOGGER.error("Error compiling pattern: " + e.getMessage());
    }
    return text;
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) 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