-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
56 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
src/main/kotlin/ru/grushetsky/m3uparser/M3uParserWrapper.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package ru.grushetsky.m3uparser | ||
|
||
import org.antlr.v4.runtime.CharStreams | ||
import org.antlr.v4.runtime.CommonTokenStream | ||
import java.net.URI | ||
|
||
class M3uParserWrapper(input: String) { | ||
|
||
private val parser: M3uParser | ||
|
||
init { | ||
val m3uLexer = M3uLexer(CharStreams.fromString(input)) | ||
parser = M3uParser(CommonTokenStream(m3uLexer)) | ||
} | ||
|
||
fun getPlaylistParameters(): Map<String, String> { | ||
return createParameterMap(parser.file_header().parameters().parameter()) | ||
} | ||
|
||
fun getPlaylistEntries(): List<PlaylistEntry> { | ||
return parser.entries().entry_info() | ||
.map { createPlaylistEntry(it) } | ||
} | ||
|
||
private fun createPlaylistEntry(parsedEntry: M3uParser.Entry_infoContext): PlaylistEntry { | ||
return PlaylistEntry( | ||
parsedEntry.enrty_basic_info().entry_name().text, | ||
URI(parsedEntry.enrty_basic_info().entry_uri().text), | ||
parsedEntry.length().text.toInt(), | ||
createParameterMap(parsedEntry.parameters().parameter()) | ||
) | ||
} | ||
|
||
private fun createParameterMap(parsedParameters: List<M3uParser.ParameterContext>): Map<String, String> { | ||
return parsedParameters.associateBy({ it.key().text }, { it.value().text }) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package ru.grushetsky.m3uparser | ||
|
||
import java.net.URI | ||
|
||
data class PlaylistEntry(val name: String, | ||
val uri: URI, | ||
val length: Int, | ||
val parameters: Map<String, String>) |