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;
}
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();
}
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();
}
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;
}
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;
}
}
Aggregations