package step.core; import java.util.*; import java.io.*; public class StepCoreRepository extends StepAbstractRepository { private StepGenericRepository rwrepo = new StepGenericRepository(); public StepGenericRepository genericRepository() { return rwrepo; } public void parseFileNamed(String filename) throws Exception { rwrepo.parseFileNamed(filename); recordInstancesFromGenericRepository(); } public void parseString(String contents) throws Exception { rwrepo.parseString(contents); recordInstancesFromGenericRepository(); } public void parseStream(java.io.InputStream stream) throws Exception { rwrepo.parseStream(stream); recordInstancesFromGenericRepository(); } public boolean unRecord(StepCoreObject coreObj) { int oid = oidOf(coreObj); if (oid > -1) { instanceByOidIndex.remove(oid); oidByInstanceIndex.remove(coreObj); Collection instances = getDataInstances(); for (Iterator itor = instances.iterator(); itor.hasNext(); ) { StepCoreObject inst = (StepCoreObject) itor.next(); inst.replaceReferenceWith(coreObj, null); } return true; } return false; } public void write(Writer writer) throws Exception { buildGenericRepository(); rwrepo.write(writer); rwrepo.clear(); } public void buildGenericRepository() throws Exception { Collection instances = getDataInstances(); for (Iterator itor = instances.iterator(); itor.hasNext(); ) { StepCoreObject inst = (StepCoreObject) itor.next(); StepCoreObjectReaderWriter rw = inst.readerWriter(); rwrepo.record(rw.genericInstance(this, inst), oidOf(inst)); } StepGenericRepositoryResolver resolver = new StepGenericRepositoryResolver(rwrepo); rwrepo.accept(resolver); } private void recordInstanceFrom(StepInternalRepresentation ginternal) throws Exception { StepCoreObjectReaderWriter rw = rwByEntityNameIndex.get(ginternal.getEntityName().toUpperCase()); if (rw != null) { record(rw.coreObject(), rwrepo.oidOf(ginternal)); } else { throw new Exception("unregistered entity builder for " + ginternal.getEntityName()); } } private void recordInstancesFromGenericRepository() throws Exception{ Collection ginstances = rwrepo.getDataInstances(); for (Iterator itor = ginstances.iterator(); itor.hasNext(); ) { StepObject ginst = itor.next(); if (ginst instanceof StepInternalRepresentation) { recordInstanceFrom((StepInternalRepresentation) ginst); } else if (ginst instanceof StepExternalRepresentation) { throw new Exception("unable to read external representation"); } else { throw new Exception("unknown instance representation"); } } for (Iterator itor = ginstances.iterator(); itor.hasNext(); ) { StepInternalRepresentation ginst = (StepInternalRepresentation) itor.next(); StepCoreObject coreObj = (StepCoreObject) instanceAt(rwrepo.oidOf(ginst)); StepCoreObjectReaderWriter rw = coreObj.readerWriter(); rw.initializeCoreObject(this, coreObj, ginst); } rwrepo.clear(); } public List select(StepCoreObjectCondition condition) { ArrayList result = new ArrayList(); Collection instances = getDataInstances(); for (Iterator itor = instances.iterator(); itor.hasNext(); ) { StepCoreObject inst = (StepCoreObject) itor.next(); if (condition.accept(inst)) { result.add(inst); } } return result; } public StepCoreObject detect(StepCoreObjectCondition condition) { Collection instances = getDataInstances(); for (Iterator itor = instances.iterator(); itor.hasNext(); ) { StepCoreObject inst = (StepCoreObject) itor.next(); if (condition.accept(inst)) { return (inst); } } return null; } public int instancesOf (String entityName, boolean withSubtypes, Collection result) { List founds = select(new StepKindOfCondition(entityName, withSubtypes)); result.addAll(founds); return founds.size(); } protected HashMap rwByEntityNameIndex = new HashMap(); }