Search in sources :

Example 6 with ModelVersion

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;
}
Also used : ModelVersion(net.parostroj.timetable.model.ls.ModelVersion) LSException(net.parostroj.timetable.model.ls.LSException)

Example 7 with ModelVersion

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);
    }
}
Also used : Properties(java.util.Properties) ModelVersion(net.parostroj.timetable.model.ls.ModelVersion) LSFile(net.parostroj.timetable.model.ls.LSFile) LSException(net.parostroj.timetable.model.ls.LSException)

Example 8 with ModelVersion

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());
}
Also used : ModelVersion(net.parostroj.timetable.model.ls.ModelVersion)

Example 9 with ModelVersion

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);
    }
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) ZipEntry(java.util.zip.ZipEntry) ZipException(java.util.zip.ZipException) Properties(java.util.Properties) TrainDiagram(net.parostroj.timetable.model.TrainDiagram) ZipFile(java.util.zip.ZipFile) ModelVersion(net.parostroj.timetable.model.ls.ModelVersion) LSException(net.parostroj.timetable.model.ls.LSException)

Example 10 with ModelVersion

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();
}
Also used : ModelVersion(net.parostroj.timetable.model.ls.ModelVersion)

Aggregations

ModelVersion (net.parostroj.timetable.model.ls.ModelVersion)13 LSException (net.parostroj.timetable.model.ls.LSException)6 Properties (java.util.Properties)4 ZipEntry (java.util.zip.ZipEntry)3 IOException (java.io.IOException)2 Node (net.parostroj.timetable.model.Node)2 Region (net.parostroj.timetable.model.Region)2 Train (net.parostroj.timetable.model.Train)2 TrainDiagram (net.parostroj.timetable.model.TrainDiagram)2 TrainType (net.parostroj.timetable.model.TrainType)2 LSFile (net.parostroj.timetable.model.ls.LSFile)2 File (java.io.File)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1 ZipException (java.util.zip.ZipException)1 ZipFile (java.util.zip.ZipFile)1 ZipInputStream (java.util.zip.ZipInputStream)1 Attributes (net.parostroj.timetable.model.Attributes)1 FreightColor (net.parostroj.timetable.model.FreightColor)1 GrafikonException (net.parostroj.timetable.model.GrafikonException)1