Добавлена возможность делать паттерны регулярками
This commit is contained in:
@@ -30,6 +30,8 @@ public class Pattern {
|
||||
|
||||
private String color; // для вставки в css
|
||||
|
||||
private boolean isRegex;
|
||||
|
||||
@ManyToMany(mappedBy = "foundPatterns", cascade = CascadeType.ALL)
|
||||
@JsonIgnore
|
||||
private List<Stream> matchedStreams;
|
||||
|
||||
@@ -7,8 +7,9 @@ import org.springframework.stereotype.Service;
|
||||
import ru.serega6531.packmate.model.Pattern;
|
||||
import ru.serega6531.packmate.repository.PatternRepository;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@Service
|
||||
@@ -17,6 +18,8 @@ public class PatternService {
|
||||
|
||||
private final PatternRepository repository;
|
||||
|
||||
private Map<String, java.util.regex.Pattern> compiledPatterns = new HashMap<>();
|
||||
|
||||
@Autowired
|
||||
public PatternService(PatternRepository repository) {
|
||||
this.repository = repository;
|
||||
@@ -30,10 +33,19 @@ public class PatternService {
|
||||
String content = new String(bytes);
|
||||
|
||||
return findAll().stream()
|
||||
.filter(pattern -> StringUtils.containsIgnoreCase(content, pattern.getValue()))
|
||||
.filter(pattern -> matches(pattern, content))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
private boolean matches(Pattern pattern, String content) {
|
||||
if(pattern.isRegex()) {
|
||||
final java.util.regex.Pattern regex = compilePattern(pattern);
|
||||
return regex.matcher(content).find();
|
||||
} else {
|
||||
return StringUtils.containsIgnoreCase(content, pattern.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
public void deleteById(int id) {
|
||||
repository.deleteById(id);
|
||||
}
|
||||
@@ -43,4 +55,8 @@ public class PatternService {
|
||||
return repository.save(pattern);
|
||||
}
|
||||
|
||||
public java.util.regex.Pattern compilePattern(Pattern pattern) {
|
||||
return compiledPatterns.computeIfAbsent(pattern.getValue(), java.util.regex.Pattern::compile);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user