use of net.parostroj.timetable.model.ls.ModelVersion in project grafikon by jub77.
the class FileLoadSaveImpl method checkVersion.
private ModelVersion checkVersion(Properties props) throws LSException {
ModelVersion current = ModelVersion.parseModelVersion(METADATA_MODEL_VERSION);
ModelVersion loaded = ModelVersion.parseModelVersion(props.getProperty(METADATA_KEY_MODEL_VERSION));
if (current.compareTo(loaded) < 0) {
throw new LSException(String.format("Current version [%s] is older than the version of loaded file [%s].", current.toString(), loaded.toString()));
}
return loaded;
}
use of net.parostroj.timetable.model.ls.ModelVersion in project grafikon by jub77.
the class FileLoadSaveImpl method load.
@Override
public TrainDiagram load(ZipInputStream zipInput) throws LSException {
try {
ZipEntry entry = null;
TrainDiagramBuilder builder = null;
FileLoadSaveImages loadImages = new FileLoadSaveImages(DATA_IMAGES);
ModelVersion version = (ModelVersion) properties.get(VERSION_PROPERTY);
while ((entry = zipInput.getNextEntry()) != null) {
if (entry.getName().equals(METADATA)) {
// check major and minor version (do not allow load newer versions)
Properties props = new Properties();
props.load(zipInput);
version = checkVersion(props);
continue;
}
if (entry.getName().equals(DATA_TRAIN_DIAGRAM)) {
LSTrainDiagram lstd = lss.load(zipInput, LSTrainDiagram.class);
builder = new TrainDiagramBuilder(lstd);
}
// test diagram
if (builder == null) {
throw new LSException("Train diagram builder has to be first entry: " + entry.getName());
}
if (entry.getName().equals(DATA_NET)) {
builder.setNet(lss.load(zipInput, LSNet.class));
} else if (entry.getName().startsWith(DATA_ROUTES)) {
builder.setRoute(lss.load(zipInput, LSRoute.class));
} else if (entry.getName().startsWith(DATA_TRAIN_TYPES)) {
builder.setTrainType(lss.load(zipInput, LSTrainType.class));
} else if (entry.getName().startsWith(DATA_TRAINS)) {
builder.setTrain(lss.load(zipInput, LSTrain.class));
} else if (entry.getName().startsWith(DATA_ENGINE_CLASSES)) {
builder.setEngineClass(lss.load(zipInput, LSEngineClass.class));
} else if (entry.getName().startsWith(DATA_TRAINS_CYCLES)) {
builder.setTrainsCycle(lss.load(zipInput, LSTrainsCycle.class));
} else if (entry.getName().startsWith(DATA_IMAGES)) {
if (entry.getName().endsWith(".xml")) {
builder.addImage(lss.load(zipInput, LSImage.class));
} else {
builder.addImageFile(new File(entry.getName()).getName(), loadImages.loadTimetableImage(zipInput, entry));
}
}
}
TrainDiagram trainDiagram = builder.getTrainDiagram();
new LoadFilter().checkDiagram(trainDiagram, version);
return trainDiagram;
} catch (IOException e) {
throw new LSException(e);
}
}
use of net.parostroj.timetable.model.ls.ModelVersion in project grafikon by jub77.
the class TemplatePM method initVersion.
private void initVersion(Template template) {
ModelVersion ver = template.getVersion();
version.setText(ver == null ? "" : ver.toString());
}
use of net.parostroj.timetable.model.ls.ModelVersion in project grafikon by jub77.
the class LoadSave method load.
@Override
public TrainDiagram load(File file) throws LSException {
try (ZipFile zip = new ZipFile(file)) {
TrainDiagram diagram = null;
// load metadata
ZipEntry entry = zip.getEntry(METADATA);
Properties metadata = new Properties();
if (entry != null) {
// load metadata
metadata.load(zip.getInputStream(entry));
}
// set model version
ModelVersion modelVersion = null;
if (metadata.getProperty(METADATA_KEY_MODEL_VERSION) == null) {
modelVersion = ModelVersion.parseModelVersion("1.0");
} else {
modelVersion = ModelVersion.parseModelVersion(metadata.getProperty(METADATA_KEY_MODEL_VERSION));
}
ModelVersion latest = LSSerializer.getLatestVersion();
if (latest.getMajorVersion() < modelVersion.getMajorVersion() || (latest.getMajorVersion() == modelVersion.getMajorVersion() && latest.getMinorVersion() < modelVersion.getMinorVersion())) {
throw new LSException("Cannot load newer model.");
}
// load train types
entry = zip.getEntry(TRAIN_TYPES_NAME);
InputStream isTypes = null;
if (entry == null) {
isTypes = DefaultTrainTypeListSource.getDefaultTypesInputStream();
} else {
isTypes = zip.getInputStream(entry);
}
LSTrainTypeSerializer tts = LSTrainTypeSerializer.getLSTrainTypeSerializer(modelVersion);
LSTrainTypeList trainTypeList = tts.load(new InputStreamReader(isTypes, "utf-8"));
// load model
entry = zip.getEntry(TRAIN_DIAGRAM_NAME);
if (entry == null) {
throw new LSException("Model not found.");
}
diagram = this.loadTrainDiagram(modelVersion, metadata, new InputStreamReader(zip.getInputStream(entry), "utf-8"), trainTypeList);
// load images
LoadSaveImages lsImages = new LoadSaveImages();
lsImages.loadTimetableImages(diagram, zip);
return diagram;
} catch (ZipException ex) {
throw new LSException(ex);
} catch (IOException ex) {
throw new LSException(ex);
}
}
use of net.parostroj.timetable.model.ls.ModelVersion in project grafikon by jub77.
the class LoadSave method saveTrainDiagram.
private void saveTrainDiagram(Writer writer, TrainDiagram diagram, LSTrainTypeList trainTypeList) throws LSException, IOException {
LSSerializer serializer = LSSerializer.getLatestLSSerializer();
ModelVersion version = LSSerializer.getLatestVersion();
for (TrainDiagramFilter filter : saveFilters) {
diagram = filter.filter(diagram, version);
}
serializer.save(writer, diagram, trainTypeList);
writer.flush();
}
Aggregations