package step.core; import java.util.*; import java.io.*; public abstract class StepCoreObjectReaderWriter { public abstract StepCoreObject coreObject(); public abstract String entityName(); public void initializeCoreObject(StepCoreRepository repo, StepCoreObject coreObj, StepGenericInstance si) throws Exception { } public StepGenericInstance genericInstance(StepCoreRepository repo, StepCoreObject coreObj) throws Exception { return new StepInternalRepresentation(entityName()); } public String toString (StepCoreRepository crepo, StepCoreObject coreObj) throws Exception { StepInternalRepresentation gi = (StepInternalRepresentation) genericInstance(crepo, coreObj); StringWriter res = new StringWriter(); res.write("#" + crepo.oidOf(coreObj) + "="); res.write(coreObj.entityName() + "("); StepGenericWriter fw = new StepGenericWriter(null, res); for (int i = 0; i < gi.size(); i++) { gi.get ( i ).accept ( fw ); if (i < gi.size() - 1) { fw.write(","); } } res.write ( ");" ); return res.toString ( ); } protected StepValue stepValueOf(StepCoreRepository repo, Object o) throws Exception { StepValue result; if (o instanceof Collection) { Collection c = (Collection) o; result = new StepAggregationValue(); for (Iterator itor = c.iterator(); itor.hasNext();) { ((StepAggregationValue)result).add(stepValueOf(repo, itor.next())); } } else if (o == null) { result = new StepNoValue(); } else if (o instanceof String) { result = new StepStringValue(((String)o)); } else if (o instanceof Enum) { result = new StepEnumerationValue(((Enum)o).toString()); } else if (o instanceof StepCoreObject) { result = new StepGenericInstanceReference(repo.oidOf((StepCoreObject)o)); } else if (o instanceof Integer) { result = new StepIntegerValue(((Integer)o).intValue()); } else if (o instanceof Boolean) { result = new StepLogicalValue((Boolean)o); } else if (o instanceof Double) { result = new StepRealValue((Double)o); } else { throw new Exception ("can't build a StepValue from argument"); } return result; } public Object convertedStepValue(StepCoreRepository repo, StepValue v) throws Exception { StepValueConverter converter = new StepValueConverter(repo); v.accept(converter); return converter.result().get(0); } }