Search in sources :

Example 16 with PatternSyntaxException

use of java.util.regex.PatternSyntaxException in project lucene-solr by apache.

the class FieldMutatingUpdateProcessorFactory method parseSelectorParams.

public static SelectorParams parseSelectorParams(NamedList args) {
    SelectorParams params = new SelectorParams();
    params.fieldName = new HashSet<>(args.removeConfigArgs("fieldName"));
    params.typeName = new HashSet<>(args.removeConfigArgs("typeName"));
    // we can compile the patterns now
    Collection<String> patterns = args.removeConfigArgs("fieldRegex");
    if (!patterns.isEmpty()) {
        params.fieldRegex = new ArrayList<>(patterns.size());
        for (String s : patterns) {
            try {
                params.fieldRegex.add(Pattern.compile(s));
            } catch (PatternSyntaxException e) {
                throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Invalid 'fieldRegex' pattern: " + s, e);
            }
        }
    }
    // resolve this into actual Class objects later
    params.typeClass = args.removeConfigArgs("typeClass");
    // Returns null if the arg is not specified
    params.fieldNameMatchesSchemaField = args.removeBooleanArg("fieldNameMatchesSchemaField");
    return params;
}
Also used : SolrException(org.apache.solr.common.SolrException) PatternSyntaxException(java.util.regex.PatternSyntaxException)

Example 17 with PatternSyntaxException

use of java.util.regex.PatternSyntaxException in project lucene-solr by apache.

the class RegexReplaceProcessorFactory method init.

@SuppressWarnings("unchecked")
@Override
public void init(NamedList args) {
    Object patternParam = args.remove(PATTERN_PARAM);
    if (patternParam == null) {
        throw new SolrException(ErrorCode.SERVER_ERROR, "Missing required init parameter: " + PATTERN_PARAM);
    }
    try {
        pattern = Pattern.compile(patternParam.toString());
    } catch (PatternSyntaxException e) {
        throw new SolrException(ErrorCode.SERVER_ERROR, "Invalid regex: " + patternParam, e);
    }
    Object replacementParam = args.remove(REPLACEMENT_PARAM);
    if (replacementParam == null) {
        throw new SolrException(ErrorCode.SERVER_ERROR, "Missing required init parameter: " + REPLACEMENT_PARAM);
    }
    Boolean literalReplacement = args.removeBooleanArg(LITERAL_REPLACEMENT_PARAM);
    if (literalReplacement != null) {
        literalReplacementEnabled = literalReplacement;
    }
    if (literalReplacementEnabled) {
        replacement = Matcher.quoteReplacement(replacementParam.toString());
    } else {
        replacement = replacementParam.toString();
    }
    super.init(args);
}
Also used : SolrException(org.apache.solr.common.SolrException) PatternSyntaxException(java.util.regex.PatternSyntaxException)

Example 18 with PatternSyntaxException

use of java.util.regex.PatternSyntaxException in project lucene-solr by apache.

the class CloneFieldUpdateProcessorFactory method initSourceSelectorSyntax.

/**
   * init helper method that should only be called when we know for certain that both the 
   * "source" and "dest" init params <em>do</em> exist.
   */
@SuppressWarnings("unchecked")
private void initSourceSelectorSyntax(NamedList args) {
    // message than "unexpected"
    if (0 <= args.indexOf(PATTERN_PARAM, 0) || 0 <= args.indexOf(REPLACEMENT_PARAM, 0)) {
        throw new SolrException(SERVER_ERROR, "Short hand syntax must not be mixed with full syntax. Found " + SOURCE_PARAM + " and " + DEST_PARAM + " but also found " + PATTERN_PARAM + " or " + REPLACEMENT_PARAM);
    }
    Object d = args.remove(DEST_PARAM);
    assert null != d;
    List<Object> sources = args.getAll(SOURCE_PARAM);
    assert null != sources;
    if (1 == sources.size()) {
        if (sources.get(0) instanceof NamedList) {
            // nested set of selector options
            NamedList selectorConfig = (NamedList) args.remove(SOURCE_PARAM);
            srcInclusions = parseSelectorParams(selectorConfig);
            List<Object> excList = selectorConfig.getAll("exclude");
            for (Object excObj : excList) {
                if (null == excObj) {
                    throw new SolrException(SERVER_ERROR, "Init param '" + SOURCE_PARAM + "' child 'exclude' can not be null");
                }
                if (!(excObj instanceof NamedList)) {
                    throw new SolrException(SERVER_ERROR, "Init param '" + SOURCE_PARAM + "' child 'exclude' must be <lst/>");
                }
                NamedList exc = (NamedList) excObj;
                srcExclusions.add(parseSelectorParams(exc));
                if (0 < exc.size()) {
                    throw new SolrException(SERVER_ERROR, "Init param '" + SOURCE_PARAM + "' has unexpected 'exclude' sub-param(s): '" + selectorConfig.getName(0) + "'");
                }
                // call once per instance
                selectorConfig.remove("exclude");
            }
            if (0 < selectorConfig.size()) {
                throw new SolrException(SERVER_ERROR, "Init param '" + SOURCE_PARAM + "' contains unexpected child param(s): '" + selectorConfig.getName(0) + "'");
            }
            // consume from the named list so it doesn't interfere with subsequent processing
            sources.remove(0);
        }
    }
    if (1 <= sources.size()) {
        // source better be one or more strings
        srcInclusions.fieldName = new HashSet<>(args.removeConfigArgs("source"));
    }
    if (srcInclusions == null) {
        throw new SolrException(SERVER_ERROR, "Init params do not specify anything to clone, please supply either " + SOURCE_PARAM + " and " + DEST_PARAM + " or " + PATTERN_PARAM + " and " + REPLACEMENT_PARAM + ". See javadocs" + "for CloneFieldUpdateProcessorFactory for further details.");
    }
    if (d instanceof NamedList) {
        NamedList destList = (NamedList) d;
        Object patt = destList.remove(PATTERN_PARAM);
        Object replacement = destList.remove(REPLACEMENT_PARAM);
        if (null == patt || null == replacement) {
            throw new SolrException(SERVER_ERROR, "Init param '" + DEST_PARAM + "' children '" + PATTERN_PARAM + "' and '" + REPLACEMENT_PARAM + "' are both mandatoryand can not be null");
        }
        if (!(patt instanceof String && replacement instanceof String)) {
            throw new SolrException(SERVER_ERROR, "Init param '" + DEST_PARAM + "' children '" + PATTERN_PARAM + "' and '" + REPLACEMENT_PARAM + "' must both be strings (i.e. <str>)");
        }
        if (0 != destList.size()) {
            throw new SolrException(SERVER_ERROR, "Init param '" + DEST_PARAM + "' has unexpected children: '" + destList.getName(0) + "'");
        }
        try {
            this.pattern = Pattern.compile(patt.toString());
        } catch (PatternSyntaxException pe) {
            throw new SolrException(SERVER_ERROR, "Init param '" + DEST_PARAM + "' child '" + PATTERN_PARAM + " is not a valid regex pattern: " + patt, pe);
        }
        dest = replacement.toString();
    } else if (d instanceof String) {
        dest = d.toString();
    } else {
        throw new SolrException(SERVER_ERROR, "Init param '" + DEST_PARAM + "' must either be a string " + "(i.e. <str>) or a list (i.e. <lst>) containing '" + PATTERN_PARAM + "' and '" + REPLACEMENT_PARAM);
    }
}
Also used : NamedList(org.apache.solr.common.util.NamedList) SolrException(org.apache.solr.common.SolrException) PatternSyntaxException(java.util.regex.PatternSyntaxException)

Example 19 with PatternSyntaxException

use of java.util.regex.PatternSyntaxException in project intellij-community by JetBrains.

the class FindDialog method getValidationInfo.

@Nullable("null means OK")
private ValidationInfo getValidationInfo(@NotNull FindModel model) {
    if (myRbDirectory != null && myRbDirectory.isEnabled() && myRbDirectory.isSelected()) {
        VirtualFile directory = FindInProjectUtil.getDirectory(model);
        if (directory == null) {
            return new ValidationInfo(FindBundle.message("find.directory.not.found.error", getDirectory()), myDirectoryComboBox);
        }
    }
    if (!myHelper.canSearchThisString()) {
        return new ValidationInfo("String to find is empty", myInputComboBox);
    }
    if (myCbRegularExpressions != null && myCbRegularExpressions.isSelected() && myCbRegularExpressions.isEnabled()) {
        String toFind = getStringToFind();
        try {
            boolean isCaseSensitive = myCbCaseSensitive != null && myCbCaseSensitive.isSelected() && myCbCaseSensitive.isEnabled();
            Pattern pattern = Pattern.compile(toFind, isCaseSensitive ? Pattern.MULTILINE : Pattern.MULTILINE | Pattern.CASE_INSENSITIVE);
            if (pattern.matcher("").matches() && !toFind.endsWith("$") && !toFind.startsWith("^")) {
                return new ValidationInfo(FindBundle.message("find.empty.match.regular.expression.error"), myInputComboBox);
            }
        } catch (PatternSyntaxException e) {
            return new ValidationInfo(FindBundle.message("find.invalid.regular.expression.error", toFind, e.getDescription()), myInputComboBox);
        }
    }
    final String mask = getFileTypeMask();
    if (mask != null) {
        if (mask.isEmpty()) {
            return new ValidationInfo(FindBundle.message("find.filter.empty.file.mask.error"), myFileFilter);
        }
        if (mask.contains(";")) {
            return new ValidationInfo("File masks should be comma-separated", myFileFilter);
        } else {
            try {
                // verify that the regexp compiles
                FindInProjectUtil.createFileMaskCondition(mask);
            } catch (PatternSyntaxException ex) {
                return new ValidationInfo(FindBundle.message("find.filter.invalid.file.mask.error", mask), myFileFilter);
            }
        }
    }
    return null;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) Pattern(java.util.regex.Pattern) PatternSyntaxException(java.util.regex.PatternSyntaxException) Nullable(org.jetbrains.annotations.Nullable)

Example 20 with PatternSyntaxException

use of java.util.regex.PatternSyntaxException in project intellij-community by JetBrains.

the class FindPopupPanel method getValidationInfo.

@Nullable("null means OK")
private ValidationInfo getValidationInfo(@NotNull FindModel model) {
    ValidationInfo scopeValidationInfo = myScopeUI.validate(model, mySelectedScope);
    if (scopeValidationInfo != null) {
        return scopeValidationInfo;
    }
    if (!myHelper.canSearchThisString()) {
        return new ValidationInfo("String to find is empty", mySearchComponent);
    }
    if (myCbRegularExpressions != null && myCbRegularExpressions.isSelected() && myCbRegularExpressions.isEnabled()) {
        String toFind = getStringToFind();
        try {
            boolean isCaseSensitive = myCbCaseSensitive != null && myCbCaseSensitive.isSelected() && myCbCaseSensitive.isEnabled();
            Pattern pattern = Pattern.compile(toFind, isCaseSensitive ? Pattern.MULTILINE : Pattern.MULTILINE | Pattern.CASE_INSENSITIVE);
            if (pattern.matcher("").matches() && !toFind.endsWith("$") && !toFind.startsWith("^")) {
                return new ValidationInfo(FindBundle.message("find.empty.match.regular.expression.error"), mySearchComponent);
            }
        } catch (PatternSyntaxException e) {
            return new ValidationInfo(FindBundle.message("find.invalid.regular.expression.error", toFind, e.getDescription()), mySearchComponent);
        }
    }
    final String mask = getFileTypeMask();
    if (mask != null) {
        if (mask.isEmpty()) {
            return new ValidationInfo(FindBundle.message("find.filter.empty.file.mask.error"), myFileMaskField);
        }
        if (mask.contains(";")) {
            return new ValidationInfo("File masks should be comma-separated", myFileMaskField);
        } else {
            try {
                // verify that the regexp compiles
                FindInProjectUtil.createFileMaskRegExp(mask);
            } catch (PatternSyntaxException ex) {
                return new ValidationInfo(FindBundle.message("find.filter.invalid.file.mask.error", mask), myFileMaskField);
            }
        }
    }
    return null;
}
Also used : ValidationInfo(com.intellij.openapi.ui.ValidationInfo) Pattern(java.util.regex.Pattern) PatternSyntaxException(java.util.regex.PatternSyntaxException) Nullable(org.jetbrains.annotations.Nullable)

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