Skip to content

Commit

Permalink
Module public API added
Browse files Browse the repository at this point in the history
  • Loading branch information
kirill-gr committed May 13, 2018
1 parent f3e88cf commit 7f74b9d
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 21 deletions.
32 changes: 11 additions & 21 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -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()
}
Expand All @@ -31,9 +26,6 @@ apply plugin: "kotlin"
apply plugin: "antlr"
apply plugin: "idea"

group "ru.grushetsky"
version "0.0-SNAPSHOT"

repositories {
jcenter()
}
Expand All @@ -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

Expand All @@ -71,4 +61,4 @@ generateGrammarSource {
outputDirectory = new File("${project.buildDir}/generated-src/antlr/main/ru/grushetsky/m3uparser")
}

compileJava.dependsOn generateGrammarSource
compileKotlin.dependsOn generateGrammarSource
37 changes: 37 additions & 0 deletions src/main/kotlin/ru/grushetsky/m3uparser/M3uParserWrapper.kt
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 })
}
}
8 changes: 8 additions & 0 deletions src/main/kotlin/ru/grushetsky/m3uparser/PlaylistEntry.kt
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>)

0 comments on commit 7f74b9d

Please sign in to comment.