|
@@ -1,6 +1,8 @@
|
1
|
1
|
import java.io.File;
|
2
|
2
|
import java.io.IOException;
|
3
|
3
|
import java.util.Scanner;
|
|
4
|
+import java.util.regex.Matcher;
|
|
5
|
+import java.util.regex.Pattern;
|
4
|
6
|
|
5
|
7
|
/**
|
6
|
8
|
* Created by thook on 10/7/15.
|
|
@@ -8,31 +10,65 @@ import java.util.Scanner;
|
8
|
10
|
public class HamletParser {
|
9
|
11
|
|
10
|
12
|
private String hamletData;
|
|
13
|
+ private Pattern pattern;
|
|
14
|
+ private Matcher matcher;
|
11
|
15
|
|
12
|
|
- public HamletParser(){
|
|
16
|
+ public HamletParser() {
|
13
|
17
|
this.hamletData = loadFile();
|
14
|
18
|
}
|
15
|
19
|
|
16
|
|
- private String loadFile(){
|
|
20
|
+ public void changeHamletToLeon() {
|
|
21
|
+ String hamlet = "(Hamlet|HAMLET)";
|
|
22
|
+ this.hamletData = change(hamlet, "LEON");
|
|
23
|
+ }
|
|
24
|
+
|
|
25
|
+ public void changeHoratioToTariq() {
|
|
26
|
+ String horatio = "(Horatio|HORATIO)";
|
|
27
|
+ this.hamletData = change(horatio, "TARIQ");
|
|
28
|
+ }
|
|
29
|
+
|
|
30
|
+ public boolean findHoratio() {
|
|
31
|
+ String horatio = "(Horatio|HORATIO)";
|
|
32
|
+ return find(horatio);
|
|
33
|
+ }
|
|
34
|
+
|
|
35
|
+ public boolean findHamlet() {
|
|
36
|
+ String hamlet = "(Hamlet|HAMLET)";
|
|
37
|
+ return find(hamlet);
|
|
38
|
+ }
|
|
39
|
+
|
|
40
|
+ private boolean find(String aString) {
|
|
41
|
+ pattern = Pattern.compile(aString);
|
|
42
|
+ matcher = pattern.matcher(hamletData);
|
|
43
|
+ return matcher.find();
|
|
44
|
+ }
|
|
45
|
+
|
|
46
|
+ private String change(String original, String convertTo) {
|
|
47
|
+ pattern = Pattern.compile(original);
|
|
48
|
+ matcher = pattern.matcher(hamletData);
|
|
49
|
+ return matcher.replaceAll(convertTo);
|
|
50
|
+ }
|
|
51
|
+
|
|
52
|
+ private String loadFile() {
|
17
|
53
|
ClassLoader classLoader = getClass().getClassLoader();
|
18
|
54
|
File file = new File(classLoader.getResource("hamlet.txt").getFile());
|
19
|
55
|
StringBuilder result = new StringBuilder("");
|
20
|
56
|
|
21
|
|
- try(Scanner scanner = new Scanner(file)){
|
22
|
|
- while(scanner.hasNextLine()){
|
|
57
|
+ try (Scanner scanner = new Scanner(file)) {
|
|
58
|
+ while (scanner.hasNextLine()) {
|
23
|
59
|
String line = scanner.nextLine();
|
24
|
60
|
result.append(line).append("\n");
|
25
|
61
|
}
|
26
|
62
|
|
27
|
63
|
scanner.close();
|
28
|
|
- }catch(IOException e){
|
|
64
|
+ } catch (IOException e) {
|
29
|
65
|
e.printStackTrace();
|
30
|
66
|
}
|
31
|
67
|
|
32
|
68
|
return result.toString();
|
33
|
69
|
}
|
34
|
70
|
|
35
|
|
- public String getHamletData(){
|
|
71
|
+ public String getHamletData() {
|
36
|
72
|
return hamletData;
|
37
|
73
|
}
|
38
|
74
|
|