Vincent Sima il y a 6 ans
Parent
révision
5c1c089902
4 fichiers modifiés avec 5449 ajouts et 7 suppressions
  1. 12
    0
      pom.xml
  2. 34
    7
      src/main/java/HamletParser.java
  3. 20
    0
      src/test/java/HamletParserTest.java
  4. 5383
    0
      target/classes/hamlet.txt

+ 12
- 0
pom.xml Voir le fichier

@@ -7,6 +7,18 @@
7 7
     <groupId>com.zipcodewilmington</groupId>
8 8
     <artifactId>regex</artifactId>
9 9
     <version>1.0-SNAPSHOT</version>
10
+    <build>
11
+        <plugins>
12
+            <plugin>
13
+                <groupId>org.apache.maven.plugins</groupId>
14
+                <artifactId>maven-compiler-plugin</artifactId>
15
+                <configuration>
16
+                    <source>7</source>
17
+                    <target>7</target>
18
+                </configuration>
19
+            </plugin>
20
+        </plugins>
21
+    </build>
10 22
 
11 23
     <dependencies>
12 24
         <dependency>

+ 34
- 7
src/main/java/HamletParser.java Voir le fichier

@@ -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
+}

+ 20
- 0
src/test/java/HamletParserTest.java Voir le fichier

@@ -1,6 +1,9 @@
1 1
 import org.junit.Before;
2 2
 import org.junit.Test;
3 3
 
4
+import java.util.regex.Matcher;
5
+import java.util.regex.Pattern;
6
+
4 7
 import static org.junit.Assert.*;
5 8
 
6 9
 public class HamletParserTest {
@@ -15,6 +18,7 @@ public class HamletParserTest {
15 18
 
16 19
     @Test
17 20
     public void testChangeHamletToLeon() {
21
+
18 22
     }
19 23
 
20 24
     @Test
@@ -23,9 +27,25 @@ public class HamletParserTest {
23 27
 
24 28
     @Test
25 29
     public void testFindHoratio() {
30
+        String noHamOrHoratio = hamletParser.changeHoratio(hamletText);
31
+        Pattern p = Pattern.compile("Horatio");
32
+        Matcher m = p.matcher(noHamOrHoratio);
33
+        boolean actual = m.find();
34
+        boolean expected = false;
35
+        assertEquals(expected, actual);
36
+        System.out.println(noHamOrHoratio);
37
+
26 38
     }
27 39
 
28 40
     @Test
29 41
     public void testFindHamlet() {
42
+        String noHam = hamletParser.changeHamlet(hamletText);
43
+        Pattern p = Pattern.compile("Hamlet");
44
+        Matcher m = p.matcher(noHam);
45
+        boolean actual = m.find();
46
+        boolean expected = false;
47
+        assertEquals(expected, actual);
48
+        System.out.println(noHam);
49
+
30 50
     }
31 51
 }

+ 5383
- 0
target/classes/hamlet.txt
Fichier diff supprimé car celui-ci est trop grand
Voir le fichier