jaroslav@813: /** jaroslav@813: * Back 2 Browser Bytecode Translator jaroslav@813: * Copyright (C) 2012 Jaroslav Tulach jaroslav@813: * jaroslav@813: * This program is free software: you can redistribute it and/or modify jaroslav@813: * it under the terms of the GNU General Public License as published by jaroslav@813: * the Free Software Foundation, version 2 of the License. jaroslav@813: * jaroslav@813: * This program is distributed in the hope that it will be useful, jaroslav@813: * but WITHOUT ANY WARRANTY; without even the implied warranty of jaroslav@813: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the jaroslav@813: * GNU General Public License for more details. jaroslav@813: * jaroslav@813: * You should have received a copy of the GNU General Public License jaroslav@813: * along with this program. Look for COPYING file in the top folder. jaroslav@813: * If not, see http://opensource.org/licenses/GPL-2.0. jaroslav@813: */ jaroslav@813: package org.apidesign.bck2brwsr.htmlpage; jaroslav@813: jaroslav@813: import org.apidesign.bck2brwsr.core.JavaScriptBody; jaroslav@813: jaroslav@813: /** jaroslav@813: * jaroslav@813: * @author Jaroslav Tulach jaroslav@813: */ jaroslav@813: public final class ConvertTypes { jaroslav@813: ConvertTypes() { jaroslav@813: } jaroslav@813: jaroslav@813: public static String toString(Object object, String property) { jaroslav@813: Object ret = getProperty(object, property); jaroslav@813: return ret == null ? null : ret.toString(); jaroslav@813: } jaroslav@813: jaroslav@813: public static double toDouble(Object object, String property) { jaroslav@813: Object ret = getProperty(object, property); jaroslav@813: return ret instanceof Number ? ((Number)ret).doubleValue() : Double.NaN; jaroslav@813: } jaroslav@813: jaroslav@813: public static int toInt(Object object, String property) { jaroslav@813: Object ret = getProperty(object, property); jaroslav@813: return ret instanceof Number ? ((Number)ret).intValue() : Integer.MIN_VALUE; jaroslav@813: } jaroslav@906: jaroslav@906: public static T toModel(Class modelClass, Object object, String property) { jaroslav@906: Object ret = getProperty(object, property); jaroslav@906: if (ret == null || modelClass.isInstance(ret)) { jaroslav@906: return modelClass.cast(ret); jaroslav@906: } jaroslav@906: throw new IllegalStateException("Value " + ret + " is not of type " + modelClass); jaroslav@906: } jaroslav@813: jaroslav@920: public static String toJSON(Object value) { jaroslav@920: if (value == null) { jaroslav@920: return "null"; jaroslav@920: } jaroslav@920: if (value instanceof String) { jaroslav@920: return '"' + jaroslav@920: ((String)value). jaroslav@920: replace("\"", "\\\""). jaroslav@920: replace("\n", "\\n"). jaroslav@920: replace("\r", "\\r"). jaroslav@920: replace("\t", "\\t") jaroslav@920: + '"'; jaroslav@920: } jaroslav@920: return value.toString(); jaroslav@920: } jaroslav@920: jaroslav@813: @JavaScriptBody(args = { "object", "property" }, jaroslav@879: body = "if (property === null) return object;\n" jaroslav@879: + "var p = object[property]; return p ? p : null;" jaroslav@813: ) jaroslav@813: private static Object getProperty(Object object, String property) { jaroslav@813: return null; jaroslav@813: } jaroslav@813: }