|
@@ -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,32 +10,57 @@ import java.util.Scanner;
|
8
|
10
|
public class HamletParser {
|
9
|
11
|
|
10
|
12
|
private String hamletData;
|
|
13
|
+ private String result;
|
11
|
14
|
|
12
|
|
- public HamletParser(){
|
|
15
|
+ public HamletParser() {
|
13
|
16
|
this.hamletData = loadFile();
|
14
|
17
|
}
|
15
|
18
|
|
16
|
|
- private String loadFile(){
|
|
19
|
+ private String loadFile() {
|
17
|
20
|
ClassLoader classLoader = getClass().getClassLoader();
|
18
|
21
|
File file = new File(classLoader.getResource("hamlet.txt").getFile());
|
19
|
22
|
StringBuilder result = new StringBuilder("");
|
20
|
23
|
|
21
|
|
- try(Scanner scanner = new Scanner(file)){
|
22
|
|
- while(scanner.hasNextLine()){
|
|
24
|
+ try (Scanner scanner = new Scanner(file)) {
|
|
25
|
+ while (scanner.hasNextLine()) {
|
23
|
26
|
String line = scanner.nextLine();
|
24
|
27
|
result.append(line).append("\n");
|
25
|
28
|
}
|
26
|
29
|
|
27
|
30
|
scanner.close();
|
28
|
|
- }catch(IOException e){
|
|
31
|
+ } catch (IOException e) {
|
29
|
32
|
e.printStackTrace();
|
30
|
33
|
}
|
31
|
34
|
|
32
|
35
|
return result.toString();
|
33
|
36
|
}
|
34
|
37
|
|
35
|
|
- public String getHamletData(){
|
|
38
|
+ public String getHamletData() {
|
36
|
39
|
return hamletData;
|
37
|
40
|
}
|
38
|
41
|
|
39
|
|
-}
|
|
42
|
+ public String changeHamlet(String h) {
|
|
43
|
+ StringBuffer sb = new StringBuffer();
|
|
44
|
+ Pattern p = Pattern.compile("Hamlet|HAMLET");
|
|
45
|
+ Matcher m = p.matcher(h);
|
|
46
|
+ while (m.find()) {
|
|
47
|
+ m.appendReplacement(sb, Matcher.quoteReplacement("Leon"));
|
|
48
|
+ }
|
|
49
|
+ m.appendTail(sb);
|
|
50
|
+ result = sb.toString();
|
|
51
|
+ return result;
|
|
52
|
+
|
|
53
|
+ }
|
|
54
|
+
|
|
55
|
+ public String changeHoratio(String h){
|
|
56
|
+ StringBuffer sb = new StringBuffer();
|
|
57
|
+ Pattern p = Pattern.compile("Horatio|HORATIO");
|
|
58
|
+ Matcher m = p.matcher(h);
|
|
59
|
+ while (m.find()) {
|
|
60
|
+ m.appendReplacement(sb, Matcher.quoteReplacement("Tariq"));
|
|
61
|
+ }
|
|
62
|
+ m.appendTail(sb);
|
|
63
|
+ result = sb.toString();
|
|
64
|
+ return result;
|
|
65
|
+ }
|
|
66
|
+}
|