From 7f74b9dd43fff1cefd6c53a0b32220f17438345f Mon Sep 17 00:00:00 2001 From: Krom Date: Sun, 13 May 2018 21:24:07 +0300 Subject: [PATCH] Module public API added --- build.gradle | 32 ++++++---------- .../grushetsky/m3uparser/M3uParserWrapper.kt | 37 +++++++++++++++++++ .../ru/grushetsky/m3uparser/PlaylistEntry.kt | 8 ++++ 3 files changed, 56 insertions(+), 21 deletions(-) create mode 100644 src/main/kotlin/ru/grushetsky/m3uparser/M3uParserWrapper.kt create mode 100644 src/main/kotlin/ru/grushetsky/m3uparser/PlaylistEntry.kt diff --git a/build.gradle b/build.gradle index e18bf54..7b25c3c 100644 --- a/build.gradle +++ b/build.gradle @@ -1,23 +1,18 @@ -buildscript { - ext.kotlin_version = "1.2.41" - ext.junitPlatformVersion = "1.2.0" - ext.junitJupiterVersion = "5.2.0" - ext.antlrVersion = "4.7" - ext.assertJVersion = "3.9.1" - - repositories { - jcenter() - } - dependencies { - classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version") - } -} - plugins { id "org.jetbrains.kotlin.jvm" version "1.2.41" id "com.gradle.build-scan" version "1.13.1" } +group "ru.grushetsky" +version "1.0" + +ext.kotlin_version = "1.2.41" +ext.junitPlatformVersion = "1.2.0" +ext.junitJupiterVersion = "5.2.0" +ext.antlrVersion = "4.7" +ext.assertJVersion = "3.9.1" +ext.javaVersion = JavaVersion.VERSION_1_8 + test { useJUnitPlatform() } @@ -31,9 +26,6 @@ apply plugin: "kotlin" apply plugin: "antlr" apply plugin: "idea" -group "ru.grushetsky" -version "0.0-SNAPSHOT" - repositories { jcenter() } @@ -52,8 +44,6 @@ dependencies { testRuntime("org.junit.platform:junit-platform-launcher:$junitPlatformVersion") } -ext.javaVersion = JavaVersion.VERSION_1_8 - sourceCompatibility = ext.javaVersion targetCompatibility = ext.javaVersion @@ -71,4 +61,4 @@ generateGrammarSource { outputDirectory = new File("${project.buildDir}/generated-src/antlr/main/ru/grushetsky/m3uparser") } -compileJava.dependsOn generateGrammarSource +compileKotlin.dependsOn generateGrammarSource diff --git a/src/main/kotlin/ru/grushetsky/m3uparser/M3uParserWrapper.kt b/src/main/kotlin/ru/grushetsky/m3uparser/M3uParserWrapper.kt new file mode 100644 index 0000000..b9655ca --- /dev/null +++ b/src/main/kotlin/ru/grushetsky/m3uparser/M3uParserWrapper.kt @@ -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 { + return createParameterMap(parser.file_header().parameters().parameter()) + } + + fun getPlaylistEntries(): List { + 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): Map { + return parsedParameters.associateBy({ it.key().text }, { it.value().text }) + } +} \ No newline at end of file diff --git a/src/main/kotlin/ru/grushetsky/m3uparser/PlaylistEntry.kt b/src/main/kotlin/ru/grushetsky/m3uparser/PlaylistEntry.kt new file mode 100644 index 0000000..17ce039 --- /dev/null +++ b/src/main/kotlin/ru/grushetsky/m3uparser/PlaylistEntry.kt @@ -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) \ No newline at end of file