ru.pocketbyte.locolaser.ini.parser.IniResourceConfigParser.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of resource-ini Show documentation
Show all versions of resource-ini Show documentation
Implementation of platform for LocoLaser tool to work with ini resources.
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
}
}