Edit DASH UrlTemplate identifiers parser and add UrlTemplate tests

This commit is contained in:
hhouillon 2023-09-28 16:14:49 +02:00 committed by microkatz
parent eb5cbd902b
commit 022444e751
2 changed files with 22 additions and 1 deletions

View File

@ -17,6 +17,8 @@ package androidx.media3.exoplayer.dash.manifest;
import androidx.media3.common.util.UnstableApi; import androidx.media3.common.util.UnstableApi;
import java.util.Locale; import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/** /**
* A template from which URLs can be built. * A template from which URLs can be built.
@ -56,7 +58,10 @@ public final class UrlTemplate {
int identifiersCount = 0; int identifiersCount = 0;
String[] identifiersNames = { REPRESENTATION, NUMBER, BANDWIDTH, TIME }; String[] identifiersNames = { REPRESENTATION, NUMBER, BANDWIDTH, TIME };
for(String identifierName : identifiersNames){ for(String identifierName : identifiersNames){
identifiersCount += template.split("\\$" + identifierName + "\\$").length - 1; Pattern pattern = Pattern.compile("(\\$" + identifierName + "\\$|" + ESCAPED_DOLLAR + "\\$" + identifierName + "\\$" + ESCAPED_DOLLAR +")");
Matcher matcher = pattern.matcher(template);
while(matcher.find())
identifiersCount++;
} }
String[] urlPieces = new String[identifiersCount+1]; String[] urlPieces = new String[identifiersCount+1];
int[] identifiers = new int[identifiersCount]; int[] identifiers = new int[identifiersCount];

View File

@ -70,4 +70,20 @@ public class UrlTemplateTest {
// Expected. // Expected.
} }
} }
@Test
public void fullWithMultipleOccurrences(){
String template = "$Bandwidth$_a1_$RepresentationID$_b1_$Time$_c1_$Number$_$Bandwidth$_a2_$RepresentationID$_b2_$Time$_c2_$Number$";
UrlTemplate urlTemplate = UrlTemplate.compile(template);
String url = urlTemplate.buildUri("abc1", 10, 650000, 5000);
assertThat(url).isEqualTo("650000_a1_abc1_b1_5000_c1_10_650000_a2_abc1_b2_5000_c2_10");
}
@Test
public void fullWithMultipleOccurrencesAndDollarEscaping(){
String template = "$$$Bandwidth$$$_a1$$_$RepresentationID$_b1_$Time$_c1_$Number$$$_$$$Bandwidth$$$_a2$$_$RepresentationID$_b2_$Time$_c2_$Number$$$";
UrlTemplate urlTemplate = UrlTemplate.compile(template);
String url = urlTemplate.buildUri("abc1", 10, 650000, 5000);
assertThat(url).isEqualTo("$650000$_a1$_abc1_b1_5000_c1_10$_$650000$_a2$_abc1_b2_5000_c2_10$");
}
} }