/** * Copyright 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"). * You may not use this file except in compliance with the License. * A copy of the License is located at * * http://www.apache.org/licenses/LICENSE-2.0 * * or in the "license" file accompanying this file. This file is distributed * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either * express or implied. See the License for the specific language governing * permissions and limitations under the License. */ package com.amazon.styledictionaryexample.util import android.content.Context import com.amazon.styledictionaryexample.models.Property import com.amazon.styledictionaryexample.models.StyleDictionaryNode import com.fasterxml.jackson.databind.ObjectMapper import com.fasterxml.jackson.datatype.jsonorg.JsonOrgModule import org.json.JSONException import org.json.JSONObject import java.nio.charset.Charset import java.util.* object StyleDictionaryHelper { private var MAPPER: ObjectMapper? = null private var DICTIONARY_JSON_STRING: String = "" var DICTIONARY_JSON: JSONObject? = null fun loadJSON(context: Context) { DICTIONARY_JSON_STRING = loadJsonFromAsset("data/properties.json", context) try { DICTIONARY_JSON = JSONObject(DICTIONARY_JSON_STRING) } catch (e: JSONException) { e.printStackTrace() } } fun getNodeArrayForObject(json: JSONObject?): ArrayList { val nodeList = ArrayList() val keys = json!!.keys() while (keys.hasNext()) { val key = keys.next() var isLeaf = false var count = 0 try { val jsonNode = json.getJSONObject(key) if (jsonNode.has("value")) { isLeaf = true } else { count = jsonNode.length() } } catch (e: JSONException) { e.printStackTrace() } nodeList.add(StyleDictionaryNode(key, count, isLeaf)) } return nodeList } fun getArrayAtPath(path: List): ArrayList { val nodeList = ArrayList() var json = DICTIONARY_JSON try { for (pathPart in path) { json = json!!.getJSONObject(pathPart) } return getNodeArrayForObject(json) } catch (e: JSONException) { e.printStackTrace() } return nodeList } private fun getProperty(path: ArrayList, jsonObject: JSONObject?): Property { var json = jsonObject val property: Property try { for (pathPart in path) { json = json!!.getJSONObject(pathPart) } return if (json!!.has("value")) { property = MAPPER!!.convertValue(json, Property::class.java) property } else { throw RuntimeException("Property doesn't exist") } } catch (e: JSONException) { e.printStackTrace() throw e } } fun getProperty(path: ArrayList): Property { return getProperty(path, DICTIONARY_JSON) } fun getArrayOfProps(path: ArrayList): ArrayList { val propertyList = ArrayList() var json = DICTIONARY_JSON try { for (pathPart in path) { json = json!!.getJSONObject(pathPart) } val keys = json!!.keys() while (keys.hasNext()) { val key = keys.next() val jsonProperty = json.getJSONObject(key) if (jsonProperty.has("value")) { val property = MAPPER!!.convertValue(jsonProperty, Property::class.java) propertyList.add(property) } } } catch (e: JSONException) { e.printStackTrace() } return propertyList } @Suppress("SameParameterValue") private fun loadJsonFromAsset(filename: String, context: Context): String { val inputStream = context.assets.open(filename) val size = inputStream.available() val buffer = ByteArray(size) inputStream.read(buffer) inputStream.close() return String(buffer, Charset.forName("UTF-8")) } init { MAPPER = ObjectMapper().registerModule(JsonOrgModule()) } }