/* * ParseModel.fx * * Created on Dec 18, 2008, 4:37:19 PM */ package jsonweather; import javafx.data.pull.PullParser; import javafx.data.pull.Event; import javafx.async.RemoteTextDocument; import javafx.reflect.*; import java.util.Map; import java.util.HashMap; import java.io.ByteArrayInputStream; import java.io.InputStream; import javafx.stage.Stage; import javafx.scene.Scene; /** * @author jclarke */ public class ParseModel { def context:FXLocal.Context = FXLocal.getContext(); var classRef: FXClassType; var fxModel: FXObjectValue; public-init var model:Object on replace { if(model != null) { classRef = context.findClass(model.getClass().getName()); fxModel = context.mirrorOf(model); }else { classRef = null; fxModel = null;} }; public-init var map: Map; public-init var url:String; public-init var documentType:String; public-init var onDone: function(model:Object) : Void; function handler(event: Event) : Void { if(event.type == PullParser.TEXT or event.type == PullParser.INTEGER or event.type == PullParser.NUMBER ) { var field:String; if(map != null and map.containsKey(event.name)) { field = map.get(event.name) as String; } else { field = event.name; } var member = classRef.getVariable(field); // println("{event}"); var type = "{member.getType()}"; // try to guess the conversion if(event.type == PullParser.TEXT){ // assume text from stream // println("{event.name} = {event.text}"); if(type == "Integer") { member.setValue(fxModel, context.mirrorOf(java.lang.Integer.valueOf(event.text))); } else if (type == "Number") { member.setValue(fxModel, context.mirrorOf(java.lang.Double.valueOf(event.text))); } else if (type == "Boolean") { member.setValue(fxModel, context.mirrorOf(java.lang.Boolean.valueOf(event.text))); }else { member.setValue(fxModel, context.mirrorOf(event.text)); } } else if(event.type == PullParser.INTEGER) { if(type == "Number") { // if number value != 0 then convert it, else integer member.setValue(fxModel, context.mirrorOf(java.lang.Double.valueOf(event.integerValue))); } else { member.setValue(fxModel, context.mirrorOf(java.lang.Integer.valueOf(event.integerValue))); } } else if(event.type == PullParser.NUMBER) { if(type == "Integer") {// if integer value != 0 then convert it, else number member.setValue(fxModel, context.mirrorOf(java.lang.Integer.valueOf(event.numberValue.intValue()))); } else { member.setValue(fxModel, context.mirrorOf(java.lang.Double.valueOf(event.numberValue))); } } else if(event.type == PullParser.TRUE or event.type == PullParser.FALSE) { member.setValue(fxModel, context.mirrorOf(event.booleanValue)); } } } init { var input:InputStream; println("Starting"); var rtd:RemoteTextDocument = RemoteTextDocument { url: bind url onDone: function(success:Boolean):Void { if(success) { var doc = rtd.document; //println(doc); input = new ByteArrayInputStream(doc.getBytes()); var parser: PullParser = PullParser { documentType: documentType input: input onEvent: handler }; parser.parse(); input.close(); onDone(model); }else { println("failure = {rtd.failureText}"); } println("Done"); } }; } } class Weather { public var station:String; public var clouds:String; public var windDirection:Integer; public var windSpeed:Number; public var temperature:Number; public var dewPoint:Number; public var humidity:Integer; public var seaLevelPressure:Number; public var observation:String; override function toString(): String { "clouds = {clouds} " "stationName = {station} " "windDirection = {windDirection} " "windSpeed = {windSpeed} " "temperature = {temperature} " "dewPoint = {dewPoint} " "humidity = {humidity} " "seaLevelPressure = {seaLevelPressure} " "observation = {observation} "; } } function run(args:String[]) : Void { var w = Weather {}; var m: Map = HashMap{}; //m.put("clouds", "clouds"); m.put("stationName", "station"); //m.put("windDirection", "windDirection"); //m.put("windSpeed", "windSpeed"); //m.put("temperature", "temperature"); //m.put("dewPoint", "dewPoint"); // m.put("humidity", "humidity"); //m.put("seaLevelPressure", "seaLevelPressure"); //m.put("observation", "observation"); var modl = ParseModel { model: w map: m documentType: PullParser.JSON url: "http://ws.geonames.org/weatherIcaoJSON?ICAO=KMCO" onDone: function(obj: Object ) : Void { var weather = obj as Weather; println("MODEL: {weather}"); } }; // this is only necessary so that the program keeps running // so that the RemoteTextDocument gets time to finish. Stage { title: "MyApp" scene: Scene { width: 200 height: 200 content: [ ] } } }