-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSearch.java
More file actions
154 lines (140 loc) · 5.14 KB
/
Search.java
File metadata and controls
154 lines (140 loc) · 5.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package ru.job4j.searchtool;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Predicate;
import java.util.regex.Pattern;
public class Search {
private final Map<String, String> values = new HashMap<>();
private boolean readyToSave = true;
public Map<String, String> getValues() {
return values;
}
public List<Path> search(Path root, Predicate<Path> condition) throws IOException {
SearchFiles searcher = new SearchFiles(condition);
Files.walkFileTree(root, searcher);
return searcher.getPaths();
}
public void savePaths(List<Path> pathList, String file) {
try (PrintWriter out = new PrintWriter(
new BufferedOutputStream(
new FileOutputStream(file)
))) {
for (Path str : pathList) {
out.println(str);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public Predicate<Path> searchType(String type, String inputSearch) {
Pattern pattern;
switch (type) {
case ("name"):
return p -> p.toFile().getName().contains(inputSearch);
case ("mask"):
pattern = Pattern.compile(("^" + inputSearch + "$")
.replace(".", "\\.")
.replace("?", ".")
.replace("*", ".*")
);
return p -> pattern.matcher(p.toFile().getName()).find();
case ("regex"):
pattern = Pattern.compile(inputSearch);
return p -> pattern.matcher(p.toFile().getName()).find();
default:
System.out.println("Search type is wrong");
readyToSave = false;
return null;
}
}
private void argsParser(String[] args) {
if (args.length > 0) {
for (String ar : args) {
if (ar.startsWith("-")) {
String[] keyValue = ar.substring(1).trim().split("=");
if (keyValue.length == 2) {
values.put(keyValue[0].toLowerCase(), keyValue[1].toLowerCase());
}
}
}
}
}
public boolean argsVerification(String[] args) {
boolean isDir;
boolean isName;
boolean isType;
boolean isLogPath;
argsParser(args);
isDir = values.containsKey("d");
isName = values.containsKey("n");
isType = values.containsKey("t");
isLogPath = values.containsKey("o");
if (isDir && isName && isType && isLogPath) {
//Path validator
Path dirPath = Path.of(values.get("d"));
isDir = Files.isDirectory(dirPath);
//Name(by mask) validator
if (values.get("t").equals("mask")) {
String nValue = values.get("n");
if (!nValue.contains("*") && !nValue.contains("?")) {
isName = false;
}
}
//Mask validator
String tValue = values.get("t");
isType = tValue.equals("name") || tValue.equals("mask") || tValue.equals("regex");
//Log path validator
String logPath = values.get("o");
String temp;
if (!logPath.startsWith("/")) {
temp = logPath.trim().split("/")[0];
} else {
temp = "/" + logPath.trim().split("/")[1];
}
isLogPath = Files.isDirectory(Path.of(temp));
if (isDir && isName && isType && isLogPath) {
return true;
} else {
if (!isDir) {
System.out.println("Searching path does not exists");
}
if (!isName) {
System.out.println("Incorrect mask template. "
+ "\nUse special symbols"
+ "\n ? - replaces single character"
+ "\n * - replaces multiple characters");
}
if (!isType) {
System.out.println("Wrong type format."
+ "\nPlease use \"name\", \"mask\" or \"regex\"");
}
if (!isLogPath) {
System.out.println("Log destination is incorrect");
}
}
} else {
System.out.println("Missing arguments:");
System.out.println();
if (!isDir) {
System.out.println("-d=c:/Windows(searching directory path)");
}
if (!isName) {
System.out.println("-n=filename(name of the object you willing to find)");
}
if (!isType) {
System.out.println("-t=name(3 options: name, mask, regex)");
}
if (!isLogPath) {
System.out.println("-o=c:/Windows/log.txt(log file path)");
}
}
return false;
}
public boolean isReadyToSave() {
return readyToSave;
}
}