All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ru.pocketbyte.locolaser.ini.parser.IniResourceConfigParser.kt Maven / Gradle / Ivy

There is a newer version: 2.4.3
Show newest version
package ru.pocketbyte.locolaser.ini.parser

import org.json.simple.JSONObject
import ru.pocketbyte.locolaser.config.parser.ConfigParser
import ru.pocketbyte.locolaser.config.parser.ResourcesConfigParser
import ru.pocketbyte.locolaser.config.resources.BaseResourcesConfig
import ru.pocketbyte.locolaser.exception.InvalidConfigException
import ru.pocketbyte.locolaser.ini.IniResourceConfig
import ru.pocketbyte.locolaser.utils.json.JsonParseUtils

class IniResourceConfigParser : ResourcesConfigParser {

    companion object {
        const val RESOURCE_NAME = "res_name"
        const val RESOURCES_DIR = "res_dir"
    }

    @Throws(InvalidConfigException::class)
    override fun parse(resourceObject: Any?, throwIfWrongType: Boolean): BaseResourcesConfig? {

        if (resourceObject is String) {
            if (checkType(resourceObject as String?, throwIfWrongType))
                return IniResourceConfig()
        } else if (resourceObject is JSONObject) {
            val platformJSON = resourceObject as JSONObject?

            if (checkType(JsonParseUtils.getString(platformJSON!!, ResourcesConfigParser.RESOURCE_TYPE, ConfigParser.PLATFORM, true), throwIfWrongType)) {
                val platform = IniResourceConfig()

                platform.resourceName = JsonParseUtils.getString(
                        platformJSON, RESOURCE_NAME, ConfigParser.PLATFORM, false)

                platform.resourcesDir = JsonParseUtils.getFile(
                        platformJSON, RESOURCES_DIR, ConfigParser.PLATFORM, false)

                return platform
            }
        }

        if (throwIfWrongType)
            throw InvalidConfigException("Property \"" + ConfigParser.PLATFORM + "\" must be a String or JSON object.")

        return null
    }

    @Throws(InvalidConfigException::class)
    private fun checkType(type: String?, throwIfWrongType: Boolean): Boolean {
        if (IniResourceConfig.TYPE != type) {
            if (throwIfWrongType)
                throw InvalidConfigException("Source type is \"" + type + "\", but expected \"" + IniResourceConfig.TYPE + "\".")

            return false
        }

        return true
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy