diff --git a/library/src/androidTest/java/com/google/android/exoplayer/text/webvtt/WebvttCueParserTest.java b/library/src/androidTest/java/com/google/android/exoplayer/text/webvtt/WebvttCueParserTest.java index c5d1465b21..a959ff7019 100644 --- a/library/src/androidTest/java/com/google/android/exoplayer/text/webvtt/WebvttCueParserTest.java +++ b/library/src/androidTest/java/com/google/android/exoplayer/text/webvtt/WebvttCueParserTest.java @@ -27,8 +27,7 @@ import android.text.style.UnderlineSpan; public final class WebvttCueParserTest extends InstrumentationTestCase { public void testParseStrictValidClassesAndTrailingTokens() throws Exception { - WebvttCueParser parser = new WebvttCueParser(); - Spanned text = parser.parse("" + Spanned text = WebvttCueParser.parse("" + "This is text with html tags"); assertEquals("This is text with html tags", text.toString()); @@ -49,8 +48,7 @@ public final class WebvttCueParserTest extends InstrumentationTestCase { } public void testParseStrictValidUnsupportedTagsStrippedOut() throws Exception { - WebvttCueParser parser = new WebvttCueParser(); - Spanned text = parser.parse( + Spanned text = WebvttCueParser.parse( "This is text with " + "html tags"); @@ -60,8 +58,7 @@ public final class WebvttCueParserTest extends InstrumentationTestCase { } public void testParseWellFormedUnclosedEndAtCueEnd() throws Exception { - WebvttCueParser parser = new WebvttCueParser(); - Spanned text = parser.parse( + Spanned text = WebvttCueParser.parse( "An unclosed u tag with italic inside"); assertEquals("An unclosed u tag with italic inside", text.toString()); @@ -79,8 +76,7 @@ public final class WebvttCueParserTest extends InstrumentationTestCase { } public void testParseWellFormedUnclosedEndAtParent() throws Exception { - WebvttCueParser parser = new WebvttCueParser(); - Spanned text = parser.parse( + Spanned text = WebvttCueParser.parse( "An unclosed u tag with underline and italic inside"); assertEquals("An unclosed u tag with underline and italic inside", text.toString()); @@ -99,8 +95,7 @@ public final class WebvttCueParserTest extends InstrumentationTestCase { } public void testParseMalformedNestedElements() throws Exception { - WebvttCueParser parser = new WebvttCueParser(); - Spanned text = parser.parse( + Spanned text = WebvttCueParser.parse( "An unclosed u tag with italic inside"); assertEquals("An unclosed u tag with italic inside", text.toString()); @@ -126,8 +121,7 @@ public final class WebvttCueParserTest extends InstrumentationTestCase { } public void testParseCloseNonExistingTag() throws Exception { - WebvttCueParser parser = new WebvttCueParser(); - Spanned text = parser.parse("blahblahblahblah"); + Spanned text = WebvttCueParser.parse("blahblahblahblah"); assertEquals("blahblahblahblah", text.toString()); StyleSpan[] spans = getSpans(text, StyleSpan.class); @@ -138,50 +132,42 @@ public final class WebvttCueParserTest extends InstrumentationTestCase { } public void testParseEmptyTagName() throws Exception { - WebvttCueParser parser = new WebvttCueParser(); - Spanned text = parser.parse("An unclosed u tag with <>italic inside"); + Spanned text = WebvttCueParser.parse("An unclosed u tag with <>italic inside"); assertEquals("An unclosed u tag with italic inside", text.toString()); } public void testParseEntities() throws Exception { - WebvttCueParser parser = new WebvttCueParser(); - Spanned text = parser.parse("& > <  "); + Spanned text = WebvttCueParser.parse("& > <  "); assertEquals("& > < ", text.toString()); } public void testParseEntitiesUnsupported() throws Exception { - WebvttCueParser parser = new WebvttCueParser(); - Spanned text = parser.parse("&noway; &sure;"); + Spanned text = WebvttCueParser.parse("&noway; &sure;"); assertEquals(" ", text.toString()); } public void testParseEntitiesNotTerminated() throws Exception { - WebvttCueParser parser = new WebvttCueParser(); - Spanned text = parser.parse("& here comes text"); + Spanned text = WebvttCueParser.parse("& here comes text"); assertEquals("& here comes text", text.toString()); } public void testParseEntitiesNotTerminatedUnsupported() throws Exception { - WebvttCueParser parser = new WebvttCueParser(); - Spanned text = parser.parse("&surenot here comes text"); + Spanned text = WebvttCueParser.parse("&surenot here comes text"); assertEquals(" here comes text", text.toString()); } public void testParseEntitiesNotTerminatedNoSpace() throws Exception { - WebvttCueParser parser = new WebvttCueParser(); - Spanned text = parser.parse("&surenot"); + Spanned text = WebvttCueParser.parse("&surenot"); assertEquals("&surenot", text.toString()); } public void testParseVoidTag() throws Exception { - WebvttCueParser parser = new WebvttCueParser(); - Spanned text = parser.parse("here comes
text
"); + Spanned text = WebvttCueParser.parse("here comes
text
"); assertEquals("here comes text", text.toString()); } public void testParseMultipleTagsOfSameKind() { - WebvttCueParser parser = new WebvttCueParser(); - Spanned text = parser.parse("blah blah blah foo"); + Spanned text = WebvttCueParser.parse("blah blah blah foo"); assertEquals("blah blah blah foo", text.toString()); StyleSpan[] spans = getSpans(text, StyleSpan.class); @@ -195,8 +181,7 @@ public final class WebvttCueParserTest extends InstrumentationTestCase { } public void testParseInvalidVoidSlash() { - WebvttCueParser parser = new WebvttCueParser(); - Spanned text = parser.parse("blah blah"); + Spanned text = WebvttCueParser.parse("blah blah"); assertEquals("blah blah", text.toString()); StyleSpan[] spans = getSpans(text, StyleSpan.class); @@ -204,39 +189,37 @@ public final class WebvttCueParserTest extends InstrumentationTestCase { } public void testParseMonkey() throws Exception { - WebvttCueParser parser = new WebvttCueParser(); - Spanned text = parser.parse( + Spanned text = WebvttCueParser.parse( "< u>An unclosed u tag with <<<<< i>italic
inside"); assertEquals("An unclosed u tag with italic inside", text.toString()); - text = parser.parse(">>>>>>>>>An unclosed u tag with <<<<< italic" + text = WebvttCueParser.parse(">>>>>>>>>An unclosed u tag with <<<<< italic" + " inside"); assertEquals(">>>>>>>>>An unclosed u tag with inside", text.toString()); } public void testParseCornerCases() throws Exception { - WebvttCueParser parser = new WebvttCueParser(); - Spanned text = parser.parse(">"); + Spanned text = WebvttCueParser.parse(">"); assertEquals(">", text.toString()); - text = parser.parse("<"); + text = WebvttCueParser.parse("<"); assertEquals("", text.toString()); - text = parser.parse("><<<<<<<<<<"); + text = WebvttCueParser.parse("<<<<<<>><<<<<<<<<<"); assertEquals(">", text.toString()); - text = parser.parse("<>"); + text = WebvttCueParser.parse("<>"); assertEquals("", text.toString()); - text = parser.parse("&"); + text = WebvttCueParser.parse("&"); assertEquals("&", text.toString()); - text = parser.parse("&&&&&&&"); + text = WebvttCueParser.parse("&&&&&&&"); assertEquals("&&&&&&&", text.toString()); } diff --git a/library/src/main/java/com/google/android/exoplayer/text/webvtt/WebvttCueParser.java b/library/src/main/java/com/google/android/exoplayer/text/webvtt/WebvttCueParser.java index 89eedf5bf9..e459978d77 100644 --- a/library/src/main/java/com/google/android/exoplayer/text/webvtt/WebvttCueParser.java +++ b/library/src/main/java/com/google/android/exoplayer/text/webvtt/WebvttCueParser.java @@ -54,7 +54,9 @@ import java.util.Stack; private static final String TAG = "WebvttCueParser"; - public Spanned parse(String markup) { + private WebvttCueParser() {} + + public static Spanned parse(String markup) { SpannableStringBuilder spannedText = new SpannableStringBuilder(); Stack startTagStack = new Stack<>(); String[] tagTokens; @@ -126,12 +128,12 @@ import java.util.Stack; * @param startPos the position from where to start searching for the end of tag. * @return the position of the end of tag plus 1 (one). */ - private int findEndOfTag(String markup, int startPos) { + private static int findEndOfTag(String markup, int startPos) { int idx = markup.indexOf(CHAR_GREATER_THAN, startPos); return idx == -1 ? markup.length() : idx + 1; } - private void applyEntity(String entity, SpannableStringBuilder spannedText) { + private static void applyEntity(String entity, SpannableStringBuilder spannedText) { switch (entity) { case ENTITY_LESS_THAN: spannedText.append('<'); @@ -151,7 +153,7 @@ import java.util.Stack; } } - private boolean isSupportedTag(String tagName) { + private static boolean isSupportedTag(String tagName) { switch (tagName) { case TAG_BOLD: case TAG_CLASS: @@ -165,7 +167,7 @@ import java.util.Stack; } } - private void applySpansForTag(StartTag startTag, SpannableStringBuilder spannedText) { + private static void applySpansForTag(StartTag startTag, SpannableStringBuilder spannedText) { switch(startTag.name) { case TAG_BOLD: spannedText.setSpan(new StyleSpan(STYLE_BOLD), startTag.position, @@ -191,7 +193,7 @@ import java.util.Stack; * @return an array of Strings with the tag name at pos 0 followed by style classes * or null if it's an empty tag: '<>' */ - private String[] tokenizeTag(String fullTagExpression) { + private static String[] tokenizeTag(String fullTagExpression) { fullTagExpression = fullTagExpression.replace("\\s+", " ").trim(); if (fullTagExpression.length() == 0) { return null; diff --git a/library/src/main/java/com/google/android/exoplayer/text/webvtt/WebvttParser.java b/library/src/main/java/com/google/android/exoplayer/text/webvtt/WebvttParser.java index 200ba2b437..cedbfa9736 100644 --- a/library/src/main/java/com/google/android/exoplayer/text/webvtt/WebvttParser.java +++ b/library/src/main/java/com/google/android/exoplayer/text/webvtt/WebvttParser.java @@ -40,12 +40,10 @@ public final class WebvttParser implements SubtitleParser { private static final Pattern CUE_SETTING = Pattern.compile("(\\S+?):(\\S+)"); - private final WebvttCueParser cueParser; private final PositionHolder positionHolder; private final StringBuilder textBuilder; public WebvttParser() { - this.cueParser = new WebvttCueParser(); positionHolder = new PositionHolder(); textBuilder = new StringBuilder(); } @@ -131,7 +129,7 @@ public final class WebvttParser implements SubtitleParser { textBuilder.append(line.trim()); } - CharSequence cueText = cueParser.parse(textBuilder.toString()); + CharSequence cueText = WebvttCueParser.parse(textBuilder.toString()); WebvttCue cue = new WebvttCue(cueStartTime, cueEndTime, cueText, cueTextAlignment, cueLine, cueLineType, cueLineAnchor, cuePosition, cuePositionAnchor, cueWidth);