Refactored WebvttCueParser methods to be static

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=111402555
This commit is contained in:
aquilescanta 2016-01-05 04:16:20 -08:00 committed by Oliver Woodman
parent 7c103ca5f8
commit 1e4f2f6a1f
3 changed files with 34 additions and 51 deletions

View File

@ -27,8 +27,7 @@ import android.text.style.UnderlineSpan;
public final class WebvttCueParserTest extends InstrumentationTestCase { public final class WebvttCueParserTest extends InstrumentationTestCase {
public void testParseStrictValidClassesAndTrailingTokens() throws Exception { public void testParseStrictValidClassesAndTrailingTokens() throws Exception {
WebvttCueParser parser = new WebvttCueParser(); Spanned text = WebvttCueParser.parse("<v.first.loud Esme>"
Spanned text = parser.parse("<v.first.loud Esme>"
+ "This <u.style1.style2 some stuff>is</u> text with <b.foo><i.bar>html</i></b> tags"); + "This <u.style1.style2 some stuff>is</u> text with <b.foo><i.bar>html</i></b> tags");
assertEquals("This is text with html tags", text.toString()); assertEquals("This is text with html tags", text.toString());
@ -49,8 +48,7 @@ public final class WebvttCueParserTest extends InstrumentationTestCase {
} }
public void testParseStrictValidUnsupportedTagsStrippedOut() throws Exception { public void testParseStrictValidUnsupportedTagsStrippedOut() throws Exception {
WebvttCueParser parser = new WebvttCueParser(); Spanned text = WebvttCueParser.parse(
Spanned text = parser.parse(
"<v.first.loud Esme>This <unsupported>is</unsupported> text with " "<v.first.loud Esme>This <unsupported>is</unsupported> text with "
+ "<notsupp><invalid>html</invalid></notsupp> tags"); + "<notsupp><invalid>html</invalid></notsupp> tags");
@ -60,8 +58,7 @@ public final class WebvttCueParserTest extends InstrumentationTestCase {
} }
public void testParseWellFormedUnclosedEndAtCueEnd() throws Exception { public void testParseWellFormedUnclosedEndAtCueEnd() throws Exception {
WebvttCueParser parser = new WebvttCueParser(); Spanned text = WebvttCueParser.parse(
Spanned text = parser.parse(
"An <u some trailing stuff>unclosed u tag with <i>italic</i> inside"); "An <u some trailing stuff>unclosed u tag with <i>italic</i> inside");
assertEquals("An unclosed u tag with italic inside", text.toString()); 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 { public void testParseWellFormedUnclosedEndAtParent() throws Exception {
WebvttCueParser parser = new WebvttCueParser(); Spanned text = WebvttCueParser.parse(
Spanned text = parser.parse(
"An unclosed u tag with <i><u>underline and italic</i> inside"); "An unclosed u tag with <i><u>underline and italic</i> inside");
assertEquals("An unclosed u tag with underline and italic inside", text.toString()); 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 { public void testParseMalformedNestedElements() throws Exception {
WebvttCueParser parser = new WebvttCueParser(); Spanned text = WebvttCueParser.parse(
Spanned text = parser.parse(
"<b><u>An unclosed u tag with <i>italic</u> inside</i></b>"); "<b><u>An unclosed u tag with <i>italic</u> inside</i></b>");
assertEquals("An unclosed u tag with italic inside", text.toString()); 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 { public void testParseCloseNonExistingTag() throws Exception {
WebvttCueParser parser = new WebvttCueParser(); Spanned text = WebvttCueParser.parse("blah<b>blah</i>blah</b>blah");
Spanned text = parser.parse("blah<b>blah</i>blah</b>blah");
assertEquals("blahblahblahblah", text.toString()); assertEquals("blahblahblahblah", text.toString());
StyleSpan[] spans = getSpans(text, StyleSpan.class); StyleSpan[] spans = getSpans(text, StyleSpan.class);
@ -138,50 +132,42 @@ public final class WebvttCueParserTest extends InstrumentationTestCase {
} }
public void testParseEmptyTagName() throws Exception { public void testParseEmptyTagName() throws Exception {
WebvttCueParser parser = new WebvttCueParser(); Spanned text = WebvttCueParser.parse("An unclosed u tag with <>italic inside");
Spanned text = parser.parse("An unclosed u tag with <>italic inside");
assertEquals("An unclosed u tag with italic inside", text.toString()); assertEquals("An unclosed u tag with italic inside", text.toString());
} }
public void testParseEntities() throws Exception { public void testParseEntities() throws Exception {
WebvttCueParser parser = new WebvttCueParser(); Spanned text = WebvttCueParser.parse("&amp; &gt; &lt; &nbsp;");
Spanned text = parser.parse("&amp; &gt; &lt; &nbsp;");
assertEquals("& > < ", text.toString()); assertEquals("& > < ", text.toString());
} }
public void testParseEntitiesUnsupported() throws Exception { public void testParseEntitiesUnsupported() throws Exception {
WebvttCueParser parser = new WebvttCueParser(); Spanned text = WebvttCueParser.parse("&noway; &sure;");
Spanned text = parser.parse("&noway; &sure;");
assertEquals(" ", text.toString()); assertEquals(" ", text.toString());
} }
public void testParseEntitiesNotTerminated() throws Exception { public void testParseEntitiesNotTerminated() throws Exception {
WebvttCueParser parser = new WebvttCueParser(); Spanned text = WebvttCueParser.parse("&amp here comes text");
Spanned text = parser.parse("&amp here comes text");
assertEquals("& here comes text", text.toString()); assertEquals("& here comes text", text.toString());
} }
public void testParseEntitiesNotTerminatedUnsupported() throws Exception { public void testParseEntitiesNotTerminatedUnsupported() throws Exception {
WebvttCueParser parser = new WebvttCueParser(); Spanned text = WebvttCueParser.parse("&surenot here comes text");
Spanned text = parser.parse("&surenot here comes text");
assertEquals(" here comes text", text.toString()); assertEquals(" here comes text", text.toString());
} }
public void testParseEntitiesNotTerminatedNoSpace() throws Exception { public void testParseEntitiesNotTerminatedNoSpace() throws Exception {
WebvttCueParser parser = new WebvttCueParser(); Spanned text = WebvttCueParser.parse("&surenot");
Spanned text = parser.parse("&surenot");
assertEquals("&surenot", text.toString()); assertEquals("&surenot", text.toString());
} }
public void testParseVoidTag() throws Exception { public void testParseVoidTag() throws Exception {
WebvttCueParser parser = new WebvttCueParser(); Spanned text = WebvttCueParser.parse("here comes<br/> text<br/>");
Spanned text = parser.parse("here comes<br/> text<br/>");
assertEquals("here comes text", text.toString()); assertEquals("here comes text", text.toString());
} }
public void testParseMultipleTagsOfSameKind() { public void testParseMultipleTagsOfSameKind() {
WebvttCueParser parser = new WebvttCueParser(); Spanned text = WebvttCueParser.parse("blah <b>blah</b> blah <b>foo</b>");
Spanned text = parser.parse("blah <b>blah</b> blah <b>foo</b>");
assertEquals("blah blah blah foo", text.toString()); assertEquals("blah blah blah foo", text.toString());
StyleSpan[] spans = getSpans(text, StyleSpan.class); StyleSpan[] spans = getSpans(text, StyleSpan.class);
@ -195,8 +181,7 @@ public final class WebvttCueParserTest extends InstrumentationTestCase {
} }
public void testParseInvalidVoidSlash() { public void testParseInvalidVoidSlash() {
WebvttCueParser parser = new WebvttCueParser(); Spanned text = WebvttCueParser.parse("blah <b/.st1.st2 trailing stuff> blah");
Spanned text = parser.parse("blah <b/.st1.st2 trailing stuff> blah");
assertEquals("blah blah", text.toString()); assertEquals("blah blah", text.toString());
StyleSpan[] spans = getSpans(text, StyleSpan.class); StyleSpan[] spans = getSpans(text, StyleSpan.class);
@ -204,39 +189,37 @@ public final class WebvttCueParserTest extends InstrumentationTestCase {
} }
public void testParseMonkey() throws Exception { public void testParseMonkey() throws Exception {
WebvttCueParser parser = new WebvttCueParser(); Spanned text = WebvttCueParser.parse(
Spanned text = parser.parse(
"< u>An unclosed u tag with <<<<< i>italic</u></u></u></u ></i><u><u> inside"); "< u>An unclosed u tag with <<<<< i>italic</u></u></u></u ></i><u><u> inside");
assertEquals("An unclosed u tag with italic inside", text.toString()); assertEquals("An unclosed u tag with italic inside", text.toString());
text = parser.parse(">>>>>>>>>An unclosed u tag with <<<<< italic</u></u></u></u >" text = WebvttCueParser.parse(">>>>>>>>>An unclosed u tag with <<<<< italic</u></u></u></u >"
+ "</i><u><u> inside"); + "</i><u><u> inside");
assertEquals(">>>>>>>>>An unclosed u tag with inside", text.toString()); assertEquals(">>>>>>>>>An unclosed u tag with inside", text.toString());
} }
public void testParseCornerCases() throws Exception { public void testParseCornerCases() throws Exception {
WebvttCueParser parser = new WebvttCueParser(); Spanned text = WebvttCueParser.parse(">");
Spanned text = parser.parse(">");
assertEquals(">", text.toString()); assertEquals(">", text.toString());
text = parser.parse("<"); text = WebvttCueParser.parse("<");
assertEquals("", text.toString()); assertEquals("", text.toString());
text = parser.parse("<b.st1.st2 annotation"); text = WebvttCueParser.parse("<b.st1.st2 annotation");
assertEquals("", text.toString()); assertEquals("", text.toString());
text = parser.parse("<<<<<<<<<<<<<<<<"); text = WebvttCueParser.parse("<<<<<<<<<<<<<<<<");
assertEquals("", text.toString()); assertEquals("", text.toString());
text = parser.parse("<<<<<<>><<<<<<<<<<"); text = WebvttCueParser.parse("<<<<<<>><<<<<<<<<<");
assertEquals(">", text.toString()); assertEquals(">", text.toString());
text = parser.parse("<>"); text = WebvttCueParser.parse("<>");
assertEquals("", text.toString()); assertEquals("", text.toString());
text = parser.parse("&"); text = WebvttCueParser.parse("&");
assertEquals("&", text.toString()); assertEquals("&", text.toString());
text = parser.parse("&&&&&&&"); text = WebvttCueParser.parse("&&&&&&&");
assertEquals("&&&&&&&", text.toString()); assertEquals("&&&&&&&", text.toString());
} }

View File

@ -54,7 +54,9 @@ import java.util.Stack;
private static final String TAG = "WebvttCueParser"; private static final String TAG = "WebvttCueParser";
public Spanned parse(String markup) { private WebvttCueParser() {}
public static Spanned parse(String markup) {
SpannableStringBuilder spannedText = new SpannableStringBuilder(); SpannableStringBuilder spannedText = new SpannableStringBuilder();
Stack<StartTag> startTagStack = new Stack<>(); Stack<StartTag> startTagStack = new Stack<>();
String[] tagTokens; String[] tagTokens;
@ -126,12 +128,12 @@ import java.util.Stack;
* @param startPos the position from where to start searching for the end of tag. * @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). * @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); int idx = markup.indexOf(CHAR_GREATER_THAN, startPos);
return idx == -1 ? markup.length() : idx + 1; return idx == -1 ? markup.length() : idx + 1;
} }
private void applyEntity(String entity, SpannableStringBuilder spannedText) { private static void applyEntity(String entity, SpannableStringBuilder spannedText) {
switch (entity) { switch (entity) {
case ENTITY_LESS_THAN: case ENTITY_LESS_THAN:
spannedText.append('<'); spannedText.append('<');
@ -151,7 +153,7 @@ import java.util.Stack;
} }
} }
private boolean isSupportedTag(String tagName) { private static boolean isSupportedTag(String tagName) {
switch (tagName) { switch (tagName) {
case TAG_BOLD: case TAG_BOLD:
case TAG_CLASS: 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) { switch(startTag.name) {
case TAG_BOLD: case TAG_BOLD:
spannedText.setSpan(new StyleSpan(STYLE_BOLD), startTag.position, spannedText.setSpan(new StyleSpan(STYLE_BOLD), startTag.position,
@ -191,7 +193,7 @@ import java.util.Stack;
* @return an array of <code>String</code>s with the tag name at pos 0 followed by style classes * @return an array of <code>String</code>s with the tag name at pos 0 followed by style classes
* or null if it's an empty tag: '&lt;&gt;' * or null if it's an empty tag: '&lt;&gt;'
*/ */
private String[] tokenizeTag(String fullTagExpression) { private static String[] tokenizeTag(String fullTagExpression) {
fullTagExpression = fullTagExpression.replace("\\s+", " ").trim(); fullTagExpression = fullTagExpression.replace("\\s+", " ").trim();
if (fullTagExpression.length() == 0) { if (fullTagExpression.length() == 0) {
return null; return null;

View File

@ -40,12 +40,10 @@ public final class WebvttParser implements SubtitleParser {
private static final Pattern CUE_SETTING = Pattern.compile("(\\S+?):(\\S+)"); private static final Pattern CUE_SETTING = Pattern.compile("(\\S+?):(\\S+)");
private final WebvttCueParser cueParser;
private final PositionHolder positionHolder; private final PositionHolder positionHolder;
private final StringBuilder textBuilder; private final StringBuilder textBuilder;
public WebvttParser() { public WebvttParser() {
this.cueParser = new WebvttCueParser();
positionHolder = new PositionHolder(); positionHolder = new PositionHolder();
textBuilder = new StringBuilder(); textBuilder = new StringBuilder();
} }
@ -131,7 +129,7 @@ public final class WebvttParser implements SubtitleParser {
textBuilder.append(line.trim()); 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, WebvttCue cue = new WebvttCue(cueStartTime, cueEndTime, cueText, cueTextAlignment, cueLine,
cueLineType, cueLineAnchor, cuePosition, cuePositionAnchor, cueWidth); cueLineType, cueLineAnchor, cuePosition, cuePositionAnchor, cueWidth);