Remove WebvttCueParser from null-checking blacklist

PiperOrigin-RevId: 277916639
This commit is contained in:
ibaker 2019-11-01 13:33:20 +00:00 committed by Oliver Woodman
parent 129efa2ebf
commit 616f4774e1

View File

@ -16,6 +16,7 @@
package com.google.android.exoplayer2.text.webvtt; package com.google.android.exoplayer2.text.webvtt;
import android.graphics.Typeface; import android.graphics.Typeface;
import android.text.Layout;
import android.text.Spannable; import android.text.Spannable;
import android.text.SpannableStringBuilder; import android.text.SpannableStringBuilder;
import android.text.Spanned; import android.text.Spanned;
@ -30,13 +31,14 @@ import android.text.style.StyleSpan;
import android.text.style.TypefaceSpan; import android.text.style.TypefaceSpan;
import android.text.style.UnderlineSpan; import android.text.style.UnderlineSpan;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.google.android.exoplayer2.text.Cue; import com.google.android.exoplayer2.text.Cue;
import com.google.android.exoplayer2.util.Assertions;
import com.google.android.exoplayer2.util.Log; import com.google.android.exoplayer2.util.Log;
import com.google.android.exoplayer2.util.ParsableByteArray; import com.google.android.exoplayer2.util.ParsableByteArray;
import com.google.android.exoplayer2.util.Util; import com.google.android.exoplayer2.util.Util;
import java.util.ArrayDeque; import java.util.ArrayDeque;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.regex.Matcher; import java.util.regex.Matcher;
@ -155,7 +157,7 @@ public final class WebvttCueParser {
* @param builder Output builder. * @param builder Output builder.
*/ */
/* package */ static void parseCueText( /* package */ static void parseCueText(
String id, String markup, WebvttCue.Builder builder, List<WebvttCssStyle> styles) { @Nullable String id, String markup, WebvttCue.Builder builder, List<WebvttCssStyle> styles) {
SpannableStringBuilder spannedText = new SpannableStringBuilder(); SpannableStringBuilder spannedText = new SpannableStringBuilder();
ArrayDeque<StartTag> startTagStack = new ArrayDeque<>(); ArrayDeque<StartTag> startTagStack = new ArrayDeque<>();
List<StyleMatch> scratchStyleMatches = new ArrayList<>(); List<StyleMatch> scratchStyleMatches = new ArrayList<>();
@ -174,8 +176,11 @@ public final class WebvttCueParser {
boolean isVoidTag = markup.charAt(pos - 2) == CHAR_SLASH; boolean isVoidTag = markup.charAt(pos - 2) == CHAR_SLASH;
String fullTagExpression = markup.substring(ltPos + (isClosingTag ? 2 : 1), String fullTagExpression = markup.substring(ltPos + (isClosingTag ? 2 : 1),
isVoidTag ? pos - 2 : pos - 1); isVoidTag ? pos - 2 : pos - 1);
if (fullTagExpression.trim().isEmpty()) {
continue;
}
String tagName = getTagName(fullTagExpression); String tagName = getTagName(fullTagExpression);
if (tagName == null || !isSupportedTag(tagName)) { if (!isSupportedTag(tagName)) {
continue; continue;
} }
if (isClosingTag) { if (isClosingTag) {
@ -223,8 +228,13 @@ public final class WebvttCueParser {
builder.setText(spannedText); builder.setText(spannedText);
} }
private static boolean parseCue(String id, Matcher cueHeaderMatcher, ParsableByteArray webvttData, private static boolean parseCue(
WebvttCue.Builder builder, StringBuilder textBuilder, List<WebvttCssStyle> styles) { @Nullable String id,
Matcher cueHeaderMatcher,
ParsableByteArray webvttData,
WebvttCue.Builder builder,
StringBuilder textBuilder,
List<WebvttCssStyle> styles) {
try { try {
// Parse the cue start and end times. // Parse the cue start and end times.
builder.setStartTime(WebvttParserUtil.parseTimestampUs(cueHeaderMatcher.group(1))) builder.setStartTime(WebvttParserUtil.parseTimestampUs(cueHeaderMatcher.group(1)))
@ -238,8 +248,9 @@ public final class WebvttCueParser {
// Parse the cue text. // Parse the cue text.
textBuilder.setLength(0); textBuilder.setLength(0);
String line; for (String line = webvttData.readLine();
while (!TextUtils.isEmpty(line = webvttData.readLine())) { !TextUtils.isEmpty(line);
line = webvttData.readLine()) {
if (textBuilder.length() > 0) { if (textBuilder.length() > 0) {
textBuilder.append("\n"); textBuilder.append("\n");
} }
@ -362,8 +373,12 @@ public final class WebvttCueParser {
} }
} }
private static void applySpansForTag(String cueId, StartTag startTag, SpannableStringBuilder text, private static void applySpansForTag(
List<WebvttCssStyle> styles, List<StyleMatch> scratchStyleMatches) { @Nullable String cueId,
StartTag startTag,
SpannableStringBuilder text,
List<WebvttCssStyle> styles,
List<StyleMatch> scratchStyleMatches) {
int start = startTag.position; int start = startTag.position;
int end = text.length(); int end = text.length();
switch(startTag.name) { switch(startTag.name) {
@ -421,9 +436,10 @@ public final class WebvttCueParser {
spannedText.setSpan(new TypefaceSpan(style.getFontFamily()), start, end, spannedText.setSpan(new TypefaceSpan(style.getFontFamily()), start, end,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
} }
if (style.getTextAlign() != null) { Layout.Alignment textAlign = style.getTextAlign();
spannedText.setSpan(new AlignmentSpan.Standard(style.getTextAlign()), start, end, if (textAlign != null) {
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); spannedText.setSpan(
new AlignmentSpan.Standard(textAlign), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
} }
switch (style.getFontSizeUnit()) { switch (style.getFontSizeUnit()) {
case WebvttCssStyle.FONT_SIZE_UNIT_PIXEL: case WebvttCssStyle.FONT_SIZE_UNIT_PIXEL:
@ -452,14 +468,15 @@ public final class WebvttCueParser {
*/ */
private static String getTagName(String tagExpression) { private static String getTagName(String tagExpression) {
tagExpression = tagExpression.trim(); tagExpression = tagExpression.trim();
if (tagExpression.isEmpty()) { Assertions.checkArgument(!tagExpression.isEmpty());
return null;
}
return Util.splitAtFirst(tagExpression, "[ \\.]")[0]; return Util.splitAtFirst(tagExpression, "[ \\.]")[0];
} }
private static void getApplicableStyles(List<WebvttCssStyle> declaredStyles, String id, private static void getApplicableStyles(
StartTag tag, List<StyleMatch> output) { List<WebvttCssStyle> declaredStyles,
@Nullable String id,
StartTag tag,
List<StyleMatch> output) {
int styleCount = declaredStyles.size(); int styleCount = declaredStyles.size();
for (int i = 0; i < styleCount; i++) { for (int i = 0; i < styleCount; i++) {
WebvttCssStyle style = declaredStyles.get(i); WebvttCssStyle style = declaredStyles.get(i);
@ -506,9 +523,7 @@ public final class WebvttCueParser {
public static StartTag buildStartTag(String fullTagExpression, int position) { public static StartTag buildStartTag(String fullTagExpression, int position) {
fullTagExpression = fullTagExpression.trim(); fullTagExpression = fullTagExpression.trim();
if (fullTagExpression.isEmpty()) { Assertions.checkArgument(!fullTagExpression.isEmpty());
return null;
}
int voiceStartIndex = fullTagExpression.indexOf(" "); int voiceStartIndex = fullTagExpression.indexOf(" ");
String voice; String voice;
if (voiceStartIndex == -1) { if (voiceStartIndex == -1) {
@ -521,7 +536,7 @@ public final class WebvttCueParser {
String name = nameAndClasses[0]; String name = nameAndClasses[0];
String[] classes; String[] classes;
if (nameAndClasses.length > 1) { if (nameAndClasses.length > 1) {
classes = Arrays.copyOfRange(nameAndClasses, 1, nameAndClasses.length); classes = Util.nullSafeArrayCopyOfRange(nameAndClasses, 1, nameAndClasses.length);
} else { } else {
classes = NO_CLASSES; classes = NO_CLASSES;
} }