Navya Sanal před 6 roky
rodič
revize
2c22f71f59

+ 58
- 7
src/main/java/HamletParser.java Zobrazit soubor

1
+import javax.sound.sampled.Line;
2
+import javax.xml.bind.Element;
1
 import java.io.File;
3
 import java.io.File;
2
 import java.io.IOException;
4
 import java.io.IOException;
3
 import java.util.Scanner;
5
 import java.util.Scanner;
6
+import java.util.regex.Matcher;
7
+import java.util.regex.Pattern;
4
 
8
 
5
 /**
9
 /**
6
  * Created by thook on 10/7/15.
10
  * Created by thook on 10/7/15.
9
 
13
 
10
     private String hamletData;
14
     private String hamletData;
11
 
15
 
12
-    public HamletParser(){
16
+    StringBuilder result = new StringBuilder("");
17
+
18
+
19
+    public HamletParser()
20
+    {
13
         this.hamletData = loadFile();
21
         this.hamletData = loadFile();
14
     }
22
     }
15
 
23
 
16
     private String loadFile(){
24
     private String loadFile(){
17
         ClassLoader classLoader = getClass().getClassLoader();
25
         ClassLoader classLoader = getClass().getClassLoader();
18
         File file = new File(classLoader.getResource("hamlet.txt").getFile());
26
         File file = new File(classLoader.getResource("hamlet.txt").getFile());
19
-        StringBuilder result = new StringBuilder("");
20
 
27
 
21
-        try(Scanner scanner = new Scanner(file)){
28
+        try
29
+        {
30
+            Scanner scanner = new Scanner(file);
22
             while(scanner.hasNextLine()){
31
             while(scanner.hasNextLine()){
23
-                String line = scanner.nextLine();
24
-                result.append(line).append("\n");
32
+                /*//String line = scanner.nextLine();
33
+                //String updated  = line.replaceAll("Hamlet","Leon");
34
+                //line = updated.replaceAll("Horatio","Tariq");
35
+                //result.append(line).append("\n");*/
36
+                break;
25
             }
37
             }
26
-
27
             scanner.close();
38
             scanner.close();
28
-        }catch(IOException e){
39
+        }
40
+        catch(IOException e)
41
+        {
29
             e.printStackTrace();
42
             e.printStackTrace();
30
         }
43
         }
31
 
44
 
35
     public String getHamletData(){
48
     public String getHamletData(){
36
         return hamletData;
49
         return hamletData;
37
     }
50
     }
51
+    public String ChangeHamletToLeon(String inputLine)
52
+    {
53
+        //String line ;
54
+        Pattern p = Pattern.compile("hamlet", Pattern.CASE_INSENSITIVE);
55
+        Matcher m = p.matcher(inputLine);
56
+
57
+        StringBuilder s = result.append(m.replaceAll("Leon"));
58
+        System.out.println(s);
59
+        return s.toString();
60
+
61
+    }
62
+    public StringBuilder ChangeHoratioToTariq(String inputLine)
63
+    {
64
+        //String line ;
65
+        Pattern p = Pattern.compile("\\Horatio\\b", Pattern.CASE_INSENSITIVE);
66
+        Matcher m = p.matcher(inputLine);
67
+        StringBuilder s = result.append(m.replaceAll("<h1>Tariq</h1>"));
68
+        return s;
69
+
70
+    }
71
+    public boolean findHoratio(String inputLine)
72
+    {
73
+
74
+      Pattern  p = Pattern.compile("Horatio",Pattern.CASE_INSENSITIVE);
75
+      Matcher m = p.matcher(inputLine);
76
+      return m.find();
77
+
78
+
79
+    }
80
+    public boolean findHamlet(String inputLine)
81
+    {
82
+
83
+       Pattern  p = Pattern.compile("Tariq",Pattern.CASE_INSENSITIVE);
84
+       Matcher m  = p.matcher(inputLine);
85
+       return m.find();
86
+
87
+    }
88
+
38
 
89
 
39
 }
90
 }

+ 24
- 0
src/test/java/HamletParserTest.java Zobrazit soubor

1
+import org.junit.Assert;
1
 import org.junit.Before;
2
 import org.junit.Before;
2
 import org.junit.Test;
3
 import org.junit.Test;
3
 
4
 
7
     private String hamletText;
8
     private String hamletText;
8
     private HamletParser hamletParser;
9
     private HamletParser hamletParser;
9
 
10
 
11
+    HamletParser hp = new HamletParser();
12
+
13
+
10
     @Before
14
     @Before
11
     public void setUp() {
15
     public void setUp() {
12
         this.hamletParser = new HamletParser();
16
         this.hamletParser = new HamletParser();
15
 
19
 
16
     @Test
20
     @Test
17
     public void testChangeHamletToLeon() {
21
     public void testChangeHamletToLeon() {
22
+        String expected = "This is Leon";
23
+        String actual = hp.ChangeHamletToLeon(expected);
24
+        Assert.assertEquals(expected,actual);
25
+
18
     }
26
     }
19
 
27
 
20
     @Test
28
     @Test
21
     public void testChangeHoratioToTariq() {
29
     public void testChangeHoratioToTariq() {
30
+        String expected = "This is Horatio";
31
+        String actual = hp.ChangeHamletToLeon(expected);
32
+        Assert.assertEquals(expected,actual);
33
+
22
     }
34
     }
23
 
35
 
24
     @Test
36
     @Test
25
     public void testFindHoratio() {
37
     public void testFindHoratio() {
38
+
39
+        boolean expected = true;
40
+       boolean actual = hp.findHoratio("This is horatio");
41
+
42
+        assertEquals(actual, expected);
43
+
26
     }
44
     }
27
 
45
 
28
     @Test
46
     @Test
29
     public void testFindHamlet() {
47
     public void testFindHamlet() {
48
+
49
+        boolean expected = false;
50
+        boolean actual = hp.findHamlet("This is leon");
51
+
52
+        assertEquals(actual,expected);
53
+
30
     }
54
     }
31
 }
55
 }

+ 5383
- 0
target/classes/hamlet.txt
Diff nebyl zobrazen, protože je příliš veliký
Zobrazit soubor