From: Bill Kuker Date: Fri, 5 Nov 2010 14:44:11 +0000 (+0000) Subject: Support URI or inline (built in) fuels X-Git-Url: https://git.gag.com/?a=commitdiff_plain;h=3274be75e0eda881859217a5bb4d77dced036905;p=sw%2Fmotorsim Support URI or inline (built in) fuels --- diff --git a/src/com/billkuker/rocketry/motorsim/io/MotorIO.java b/src/com/billkuker/rocketry/motorsim/io/MotorIO.java index c4b4ea5..f573257 100644 --- a/src/com/billkuker/rocketry/motorsim/io/MotorIO.java +++ b/src/com/billkuker/rocketry/motorsim/io/MotorIO.java @@ -18,6 +18,7 @@ import com.billkuker.rocketry.motorsim.Motor; import com.billkuker.rocketry.motorsim.fuel.FuelResolver; import com.billkuker.rocketry.motorsim.fuel.FuelResolver.FuelNotFound; import com.thoughtworks.xstream.XStream; +import com.thoughtworks.xstream.converters.ConversionException; import com.thoughtworks.xstream.converters.Converter; import com.thoughtworks.xstream.converters.MarshallingContext; import com.thoughtworks.xstream.converters.UnmarshallingContext; @@ -37,22 +38,41 @@ public class MotorIO { @Override public void marshal(Object o, HierarchicalStreamWriter w, MarshallingContext ctx) { - Fuel f = (Fuel)o; - w.setValue(f.getURI().toString()); + /*Fuel f = (Fuel)o; + w.setValue(f.getURI().toString());*/ + ctx.convertAnother(((Fuel)o).getURI()); } @Override public Object unmarshal(HierarchicalStreamReader r, UnmarshallingContext ctx) { - String text = r.getValue(); + Object o = ctx.currentObject(); + Object uri = null; + Class c = ctx.getRequiredType(); + try { + uri = ctx.convertAnother(o, URI.class); + } catch ( ConversionException e ){ + e.printStackTrace(); + } + if ( uri != null && uri instanceof URI ){ + try { + return FuelResolver.getFuel((URI)uri); + } catch (FuelNotFound e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + try { - URI u = new URI(text); - return FuelResolver.getFuel(u); - } catch (URISyntaxException e) { - throw new Error("Bad Fuel URI: " + text, e); + return FuelResolver.getFuel(new URI("motorsim:" + c.getSimpleName())); } catch (FuelNotFound e) { - throw new Error("Can't find fuel: " + text, e); + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (URISyntaxException e) { + // TODO Auto-generated catch block + e.printStackTrace(); } + return null; } }