Search in sources :

Example 1 with Generex

use of com.mifmif.common.regex.Generex in project kylo by Teradata.

the class FeedRestController method generateDropZonePath.

/**
 * Generate a dropzone path. Will retry multiple times in case of file name collision.
 * Random file name generation based on regular expression is a feature to facilitate
 * business analysts who are not familiar with regular expression, so they can upload
 * file whose name doesn't match regular expression defined in feed.
 *
 * @param context   file upload context
 * @return          generated path if successful, null otherwise
 */
private java.nio.file.Path generateDropZonePath(FileUploadContext context) {
    for (int i = 0; i < FILE_UPLOAD_RETRY; i++) {
        Generex fileNameGenerator = new Generex(context.getRegexFileFilter());
        String fileName = fileNameGenerator.random();
        // Cleanup oddball characters generated by generex
        fileName = fileName.replaceAll("[^A-Za-z0-9\\.\\_\\+\\%\\-\\|]+", "\\.");
        java.nio.file.Path path = Paths.get(context.getDropzone(), fileName);
        if (!path.toFile().exists()) {
            return path;
        }
    }
    return null;
}
Also used : Generex(com.mifmif.common.regex.Generex)

Example 2 with Generex

use of com.mifmif.common.regex.Generex in project mockneat by nomemory.

the class Regex method supplier.

@Override
public Supplier<String> supplier() {
    ValidationUtils.notNull(regex, "regex");
    validRegex(regex);
    return () -> new Generex(regex).random();
}
Also used : Generex(com.mifmif.common.regex.Generex)

Example 3 with Generex

use of com.mifmif.common.regex.Generex in project java-faker by DiUS.

the class FakeValuesService method regexify.

/**
     * Generates a String that matches the given regular expression.
     */
public String regexify(String regex) {
    Generex generex = new Generex(regex);
    generex.setSeed(randomService.nextLong());
    return generex.random();
}
Also used : Generex(com.mifmif.common.regex.Generex)

Example 4 with Generex

use of com.mifmif.common.regex.Generex in project substitution-schedule-parser by vertretungsplanme.

the class BaseParser method handleClassRanges.

static Set<String> handleClassRanges(Set<String> classes, JSONObject data) throws JSONException {
    if (data == null || !data.has(PARAM_CLASS_RANGES))
        return classes;
    JSONObject options = data.getJSONObject(PARAM_CLASS_RANGES);
    String rangeFormat = options.getString(CLASS_RANGES_RANGE_FORMAT);
    String singleFormat = options.getString(CLASS_RANGES_SINGLE_FORMAT);
    String classRegex = options.getString(CLASS_RANGES_CLASS_REGEX);
    String gradeRegex = options.getString(CLASS_RANGES_GRADE_REGEX);
    int gradePos = -1;
    int minClassPos = -1;
    int maxClassPos = -1;
    StringBuilder regex = new StringBuilder();
    int i = 0;
    for (char c : rangeFormat.toCharArray()) {
        switch(c) {
            case 'g':
                if (gradePos == -1) {
                    regex.append("(").append(gradeRegex).append(")");
                    i++;
                    gradePos = i;
                } else {
                    regex.append("\\").append(gradePos);
                }
                break;
            case 'c':
                regex.append("(").append(classRegex).append(")");
                i++;
                if (minClassPos == -1) {
                    minClassPos = i;
                } else if (maxClassPos == -1) {
                    maxClassPos = i;
                } else {
                    throw new IllegalArgumentException("more than two classes in classRanges.rangeFormat");
                }
                break;
            default:
                regex.append(c);
                break;
        }
    }
    Pattern pattern = Pattern.compile(regex.toString());
    Set<String> processedClasses = new HashSet<>();
    for (String klasse : classes) {
        Matcher matcher = pattern.matcher(klasse);
        if (matcher.matches()) {
            String grade = matcher.group(gradePos);
            String minClass = matcher.group(minClassPos);
            String maxClass = matcher.group(maxClassPos);
            StringBuilder rangeRegex = new StringBuilder();
            for (char c : singleFormat.toCharArray()) {
                switch(c) {
                    case 'g':
                        rangeRegex.append(grade);
                        break;
                    case 'c':
                        rangeRegex.append("[").append(minClass).append("-").append(maxClass).append("]");
                        break;
                    default:
                        rangeRegex.append(c);
                        break;
                }
            }
            processedClasses.addAll(new Generex(rangeRegex.toString()).getAllMatchedStrings());
        } else {
            processedClasses.add(klasse);
        }
    }
    return processedClasses;
}
Also used : Pattern(java.util.regex.Pattern) Generex(com.mifmif.common.regex.Generex) JSONObject(org.json.JSONObject) Matcher(java.util.regex.Matcher) HashSet(java.util.HashSet)

Example 5 with Generex

use of com.mifmif.common.regex.Generex in project substitution-schedule-parser by vertretungsplanme.

the class ParserUtils method getClassesFromJson.

@Nullable
static List<String> getClassesFromJson(JSONObject data) throws JSONException {
    if (data.has("classes")) {
        if (data.get("classes") instanceof JSONArray) {
            JSONArray classesJson = data.getJSONArray("classes");
            List<String> classes = new ArrayList<>();
            for (int i = 0; i < classesJson.length(); i++) {
                classes.add(classesJson.getString(i));
            }
            return classes;
        } else if (data.get("classes") instanceof String) {
            String regex = data.getString("classes");
            Generex generex = new Generex(regex);
            final List<String> classes = generex.getAllMatchedStrings();
            Collections.sort(classes, new NaturalOrderComparator());
            return classes;
        } else {
            return null;
        }
    } else {
        return null;
    }
}
Also used : Generex(com.mifmif.common.regex.Generex) JSONArray(org.json.JSONArray) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) NaturalOrderComparator(com.paour.comparator.NaturalOrderComparator) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

Generex (com.mifmif.common.regex.Generex)5 NaturalOrderComparator (com.paour.comparator.NaturalOrderComparator)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 Nullable (org.jetbrains.annotations.Nullable)1 JSONArray (org.json.JSONArray)1 JSONObject (org.json.JSONObject)1