Skip to content

Commit ee8a1d2

Browse files
author
zhenya
committed
Started new srt parser.
1 parent 1cde3f9 commit ee8a1d2

6 files changed

Lines changed: 213 additions & 1 deletion

File tree

src/main/java/com/iusenko/subrip/SubRipParser.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public List<Phrase> getPhrases() throws Exception {
3232

3333
while ((line = in.readLine()) != null) {
3434
line = line.trim();
35-
35+
3636
if (isPhraseNumber(line)) {
3737
phrase = new Phrase();
3838
phrase.setNumber(Integer.parseInt(line));
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.iusenko.subrip;
2+
3+
/**
4+
*
5+
* @author iusenko
6+
*/
7+
public class Utils {
8+
9+
public static boolean isBlank(String text) {
10+
return text == null || "".equals(text.trim());
11+
}
12+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.iusenko.subrip.srt;
2+
3+
/**
4+
*
5+
* @author iusenko
6+
*/
7+
public class SrtParserException extends Exception {
8+
9+
public SrtParserException(String message) {
10+
super(message);
11+
}
12+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package com.iusenko.subrip.srt;
2+
3+
/**
4+
*
5+
* @author iusenko
6+
*/
7+
public class SrtUnit {
8+
9+
private int id;
10+
private long startTime;
11+
private long endTime;
12+
private String text;
13+
14+
public long getEndTime() {
15+
return endTime;
16+
}
17+
18+
public void setEndTime(long endTime) {
19+
this.endTime = endTime;
20+
}
21+
22+
public int getId() {
23+
return id;
24+
}
25+
26+
public void setId(int id) {
27+
this.id = id;
28+
}
29+
30+
public long getStartTime() {
31+
return startTime;
32+
}
33+
34+
public void setStartTime(long startTime) {
35+
this.startTime = startTime;
36+
}
37+
38+
public String getText() {
39+
return text;
40+
}
41+
42+
public void setText(String text) {
43+
this.text = text;
44+
}
45+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package com.iusenko.subrip.srt;
2+
3+
import com.iusenko.subrip.Utils;
4+
import java.text.ParseException;
5+
import java.text.SimpleDateFormat;
6+
import java.util.Date;
7+
8+
/**
9+
*
10+
* @author iusenko
11+
*/
12+
public class SrtUnitBuilder {
13+
14+
private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("hh:mm:ss,SSS");
15+
private static final String TIME_DELIM = "-->";
16+
private String timeline;
17+
private String id;
18+
private String text;
19+
20+
public SrtUnitBuilder timeline(String timeline) {
21+
this.timeline = timeline;
22+
return this;
23+
}
24+
25+
public SrtUnitBuilder id(String id) {
26+
this.id = id;
27+
return this;
28+
}
29+
30+
public SrtUnitBuilder text(String text) {
31+
this.text = text;
32+
return this;
33+
}
34+
35+
public SrtUnit create() throws SrtParserException {
36+
SrtUnit unit = new SrtUnit();
37+
unit.setId(getId(id));
38+
String[] startEnd = splitTimeline(timeline);
39+
unit.setStartTime(getTimestamp(startEnd[0]));
40+
unit.setEndTime(getTimestamp(startEnd[1]));
41+
unit.setText(text);
42+
return unit;
43+
}
44+
45+
protected String[] splitTimeline(String timeline) throws SrtParserException {
46+
String[] startAndEnd = timeline.split(TIME_DELIM);
47+
if (startAndEnd.length != 2) {
48+
throw new SrtParserException("time line has wrong format:" + timeline);
49+
}
50+
for (int i = 0; i < startAndEnd.length; i++) {
51+
startAndEnd[i] = startAndEnd[i].trim();
52+
if (Utils.isBlank(startAndEnd[i])) {
53+
throw new SrtParserException("blank " + (i == 0 ? "start" : "end") + " timestamp value");
54+
}
55+
}
56+
return startAndEnd;
57+
}
58+
59+
protected long getTimestamp(String timestamp) throws SrtParserException {
60+
try {
61+
Date date = DATE_FORMAT.parse(timestamp);
62+
return date.getTime();
63+
} catch (ParseException ex) {
64+
throw new SrtParserException("timestamp string has wrong format:" + timestamp);
65+
}
66+
}
67+
68+
protected int getId(String id) throws SrtParserException {
69+
try {
70+
return Integer.parseInt(id);
71+
} catch (NumberFormatException e) {
72+
throw new SrtParserException("id string has wrong format(not a number):" + id);
73+
}
74+
}
75+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.iusenko.subrip.srt;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.fail;
5+
import org.junit.Test;
6+
7+
/**
8+
*
9+
* @author iusenko
10+
*/
11+
public class SrtUnitBuilderTest {
12+
13+
private SrtUnitBuilder parser = new SrtUnitBuilder();
14+
15+
@Test
16+
public void testGetTimestamp() throws Exception {
17+
long l1 = parser.getTimestamp("00:00:20,000");
18+
long l2 = parser.getTimestamp("00:00:20,010");
19+
System.out.println("l1=" + l1 + ", l2=" + l2 + ", l2-l1=" + (l2 - l1));
20+
assertEquals(10, l2 - l1);
21+
22+
l1 = parser.getTimestamp("1:2:2,00");
23+
l2 = parser.getTimestamp("1:2:2,10");
24+
System.out.println("l1=" + l1 + ", l2=" + l2 + ", l2-l1=" + (l2 - l1));
25+
assertEquals(10, l2 - l1);
26+
}
27+
28+
@Test(expected = SrtParserException.class)
29+
public void testGetTimestampError() throws Exception {
30+
parser.getTimestamp("00:0020,000");
31+
}
32+
33+
@Test
34+
public void testGetId() throws SrtParserException {
35+
assertEquals(10, parser.getId("10"));
36+
assertEquals(24, parser.getId("24"));
37+
assertEquals(31, parser.getId("31"));
38+
}
39+
40+
@Test
41+
public void testGetIdError() throws SrtParserException {
42+
String[] vals = {"a", ",", "0x1"};
43+
for (String val : vals) {
44+
try {
45+
parser.getId(val);
46+
fail("should never reach this state");
47+
} catch (Exception e) {
48+
}
49+
}
50+
}
51+
52+
@Test
53+
public void testSplitTimeline() throws SrtParserException {
54+
String[] startEnd = parser.splitTimeline("00:02:31,567 --> 00:02:37,164 ");
55+
assertEquals("00:02:31,567", startEnd[0]);
56+
assertEquals("00:02:37,164", startEnd[1]);
57+
}
58+
59+
@Test(expected = SrtParserException.class)
60+
public void testSplitTimelineErrorStart() throws SrtParserException {
61+
parser.splitTimeline(" --> 00:02:37,164 ");
62+
}
63+
64+
@Test(expected = SrtParserException.class)
65+
public void testSplitTimelineErrorEnd() throws SrtParserException {
66+
parser.splitTimeline("00:02:31,567 --> ");
67+
}
68+
}

0 commit comments

Comments
 (0)