Search in sources :

Example 86 with PatternSyntaxException

use of java.util.regex.PatternSyntaxException in project DiscordSailv2 by Vaerys-Dawn.

the class TagRegex method execute.

@Override
public String execute(String from, CommandObject command, String args, List<ReplaceObject> toReplace) {
    List<String> splitArgs = getSplit(from);
    String test = "Testing.";
    try {
        test.replaceAll(splitArgs.get(0), test);
    } catch (PatternSyntaxException e) {
        from = replaceFirstTag(from, error);
        return from;
    }
    from = removeFirstTag(from);
    toReplace.add(new ReplaceObject(splitArgs.get(0), splitArgs.get(1)));
    return from;
}
Also used : ReplaceObject(com.github.vaerys.objects.ReplaceObject) TagReplaceObject(com.github.vaerys.templates.TagReplaceObject) PatternSyntaxException(java.util.regex.PatternSyntaxException)

Example 87 with PatternSyntaxException

use of java.util.regex.PatternSyntaxException in project application by collectionspace.

the class GenericSearch method escapeUnescapedChars.

/**
 * Escapes unescaped characters within the text of a services advanced search string.
 *
 * For the match patterns and replacement algorithm, see;
 * http://stackoverflow.com/a/5937852 and
 * http://docs.oracle.com/javase/6/docs/api/java/util/regex/Matcher.html#quoteReplacement%28java.lang.String%29
 *
 * @param value         the original text of the search string.
 * @param matchPattern  a regex pattern to be matched.  This pattern MUST contain exactly
 *                      one matching group; text will be found and replaced within that group.
 * @param findText      some text to find within the matching group of the search string.
 * @param replaceText   the replacement text for the text found, if any, in that group.
 * @return  the text of the search string, with any character escaping performed.
 */
private static String escapeUnescapedChars(String value, String matchPattern, String findText, String replaceText) {
    if (value == null || value.isEmpty()) {
        return value;
    }
    StringBuffer sb = new StringBuffer("");
    try {
        final Pattern pattern = Pattern.compile(UNESCAPED_PREVIOUS_CHAR_PATTERN + matchPattern);
        final Matcher matcher = pattern.matcher(value);
        int groupCount = matcher.groupCount();
        if (groupCount != 1) {
            log.warn("Match pattern must contain exactly one matching group. Pattern " + matchPattern + " contains " + groupCount + " matching groups.");
            return value;
        }
        while (matcher.find()) {
            if (matcher.groupCount() >= 1) {
                matcher.appendReplacement(sb, matcher.group(1).replace(findText, Matcher.quoteReplacement(replaceText)));
            }
        }
        matcher.appendTail(sb);
    } catch (PatternSyntaxException pse) {
        log.warn("Invalid regular expression pattern " + matchPattern + ": " + pse.getMessage());
    }
    return sb.toString();
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) PatternSyntaxException(java.util.regex.PatternSyntaxException)

Example 88 with PatternSyntaxException

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

the class UserModule method init.

@Override
public void init() {
    int count = 0;
    for (String regexp : loginBlacklist) {
        try {
            Pattern.compile(regexp);
            loginBlacklistChecked.add(regexp);
        } catch (PatternSyntaxException pse) {
            log.error("Invalid pattern syntax in blacklist. Pattern: " + regexp + ". Removing from this entry from list ");
        }
        count++;
    }
    log.info("Successfully added " + count + " entries to login blacklist.");
    String userEmailOptionalValue = getStringPropertyValue(USER_EMAIL_MANDATORY, false);
    if (StringHelper.containsNonWhitespace(userEmailOptionalValue)) {
        isEmailMandatory = "true".equalsIgnoreCase(userEmailOptionalValue);
    }
    String userEmailUniquenessOptionalValue = getStringPropertyValue(USER_EMAIL_UNIQUE, false);
    if (StringHelper.containsNonWhitespace(userEmailUniquenessOptionalValue)) {
        isEmailUnique = "true".equalsIgnoreCase(userEmailUniquenessOptionalValue);
    }
    // Check if user manager is configured properly and has user property
    // handlers for the mandatory user properties used in OLAT
    checkMandatoryUserProperty(UserConstants.FIRSTNAME);
    checkMandatoryUserProperty(UserConstants.LASTNAME);
    if (isEmailMandatory()) {
        checkMandatoryUserProperty(UserConstants.EMAIL);
    }
    // Add controller factory extension point to launch user profile controller
    NewControllerFactory.getInstance().addContextEntryControllerCreator(Identity.class.getSimpleName(), new IdentityContextEntryControllerCreator());
    NewControllerFactory.getInstance().addContextEntryControllerCreator("HomeSite", new IdentityContextEntryControllerCreator());
    NewControllerFactory.getInstance().addContextEntryControllerCreator("HomePage", new HomePageContextEntryControllerCreator());
    NewControllerFactory.getInstance().addContextEntryControllerCreator(User.class.getSimpleName(), new UserAdminContextEntryControllerCreator());
    NewControllerFactory.getInstance().addContextEntryControllerCreator(UserAdminSite.class.getSimpleName(), new UserAdminContextEntryControllerCreator());
}
Also used : User(org.olat.core.id.User) UserAdminContextEntryControllerCreator(org.olat.admin.user.UserAdminContextEntryControllerCreator) UserAdminSite(org.olat.admin.site.UserAdminSite) Identity(org.olat.core.id.Identity) PatternSyntaxException(java.util.regex.PatternSyntaxException)

Example 89 with PatternSyntaxException

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

the class SakaiUserProviderInstance method verifySakaiUser.

/*
   ** Verify that the user exists
   ** Query with /direct/user/:ID:/exists
   */
private boolean verifySakaiUser(String userId) {
    logger.debug("verifySakaiUser({})", userId);
    try {
        if ((userPattern != null) && !userId.matches(userPattern)) {
            logger.debug("verify user {} failed regexp {}", userId, userPattern);
            return false;
        }
    } catch (PatternSyntaxException e) {
        logger.warn("Invalid regular expression for user pattern {} - disabling checks", userPattern);
        userPattern = null;
    }
    int code;
    try {
        // This webservice does not require authentication
        URL url = new URL(sakaiUrl + "/direct/user/" + userId + "/exists");
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");
        connection.setRequestProperty("User-Agent", OC_USERAGENT);
        connection.connect();
        code = connection.getResponseCode();
    } catch (Exception e) {
        logger.warn("Exception verifying Sakai user " + userId + " at " + sakaiUrl + ": " + e.getMessage());
        return false;
    }
    // HTTP OK 200 for site exists, return false for everything else (typically 404 not found)
    return (code == 200);
}
Also used : HttpURLConnection(java.net.HttpURLConnection) URL(java.net.URL) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) InstanceNotFoundException(javax.management.InstanceNotFoundException) PatternSyntaxException(java.util.regex.PatternSyntaxException) FileNotFoundException(java.io.FileNotFoundException) PatternSyntaxException(java.util.regex.PatternSyntaxException)

Example 90 with PatternSyntaxException

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

the class TextFileImporter 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)

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