Search in sources :

Example 16 with LSException

use of net.parostroj.timetable.model.ls.LSException in project grafikon by jub77.

the class LoadDiagramUrlModelAction method backgroundAction.

@Override
protected void backgroundAction() {
    url = (String) getActionContext().getAttribute("diagramUrl");
    if (url == null) {
        // skip
        return;
    }
    setWaitMessage(ResourceLoader.getString("wait.message.loadmodel"));
    setWaitDialogVisible(true);
    long time = System.currentTimeMillis();
    try {
        try (ZipInputStream is = new ZipInputStream(new URL(url).openStream())) {
            LSFile ls = LSFileFactory.getInstance().createForLoad(is);
            context.setAttribute("diagram", ls.load(is));
        } catch (LSException e) {
            log.warn("Error loading model.", e);
            if (e.getCause() instanceof FileNotFoundException) {
                errorMessage = ResourceLoader.getString("dialog.error.filenotfound");
            } else {
                errorMessage = ResourceLoader.getString("dialog.error.loading");
            }
        } catch (Exception e) {
            log.warn("Error loading model.", e);
            errorMessage = ResourceLoader.getString("dialog.error.loading");
        }
    } finally {
        log.debug("Library loaded in {}ms", System.currentTimeMillis() - time);
        setWaitDialogVisible(false);
    }
}
Also used : LSFile(net.parostroj.timetable.model.ls.LSFile) ZipInputStream(java.util.zip.ZipInputStream) FileNotFoundException(java.io.FileNotFoundException) URL(java.net.URL) LSException(net.parostroj.timetable.model.ls.LSException) LSException(net.parostroj.timetable.model.ls.LSException) FileNotFoundException(java.io.FileNotFoundException)

Example 17 with LSException

use of net.parostroj.timetable.model.ls.LSException in project grafikon by jub77.

the class LoadLibraryUrlModelAction method backgroundAction.

@Override
protected void backgroundAction() {
    url = (String) getActionContext().getAttribute("libraryUrl");
    if (url == null) {
        // skip
        return;
    }
    setWaitMessage(ResourceLoader.getString("wait.message.loadlibrary"));
    setWaitDialogVisible(true);
    long time = System.currentTimeMillis();
    try {
        try (ZipInputStream is = new ZipInputStream(new URL(url).openStream())) {
            LSLibrary ls = LSLibraryFactory.getInstance().createForLoad(is);
            context.setAttribute("library", ls.load(is));
        } catch (LSException e) {
            log.warn("Error loading model.", e);
            if (e.getCause() instanceof FileNotFoundException) {
                errorMessage = ResourceLoader.getString("dialog.error.filenotfound");
            } else {
                errorMessage = ResourceLoader.getString("dialog.error.loading");
            }
        } catch (Exception e) {
            log.warn("Error loading model.", e);
            errorMessage = ResourceLoader.getString("dialog.error.loading");
        }
    } finally {
        log.debug("Library loaded in {}ms", System.currentTimeMillis() - time);
        setWaitDialogVisible(false);
    }
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) LSLibrary(net.parostroj.timetable.model.ls.LSLibrary) FileNotFoundException(java.io.FileNotFoundException) URL(java.net.URL) LSException(net.parostroj.timetable.model.ls.LSException) LSException(net.parostroj.timetable.model.ls.LSException) FileNotFoundException(java.io.FileNotFoundException)

Example 18 with LSException

use of net.parostroj.timetable.model.ls.LSException in project grafikon by jub77.

the class LoadLibraryModelAction method backgroundAction.

@Override
protected void backgroundAction() {
    selectedFile = (File) getActionContext().getAttribute("libraryFile");
    if (selectedFile == null) {
        // skip
        return;
    }
    setWaitMessage(ResourceLoader.getString("wait.message.loadlibrary"));
    setWaitDialogVisible(true);
    long time = System.currentTimeMillis();
    try {
        try {
            LSLibrary ls = LSLibraryFactory.getInstance().createForLoad(selectedFile);
            try (ZipInputStream stream = new ZipInputStream(new FileInputStream(selectedFile))) {
                context.setAttribute("library", ls.load(stream));
            }
        } catch (LSException e) {
            log.warn("Error loading model.", e);
            if (e.getCause() instanceof FileNotFoundException) {
                errorMessage = ResourceLoader.getString("dialog.error.filenotfound");
            } else {
                errorMessage = ResourceLoader.getString("dialog.error.loading");
            }
        } catch (Exception e) {
            log.warn("Error loading model.", e);
            errorMessage = ResourceLoader.getString("dialog.error.loading");
        }
    } finally {
        log.debug("Library loaded in {}ms", System.currentTimeMillis() - time);
        setWaitDialogVisible(false);
    }
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) LSLibrary(net.parostroj.timetable.model.ls.LSLibrary) FileNotFoundException(java.io.FileNotFoundException) FileInputStream(java.io.FileInputStream) LSException(net.parostroj.timetable.model.ls.LSException) FileNotFoundException(java.io.FileNotFoundException) LSException(net.parostroj.timetable.model.ls.LSException)

Example 19 with LSException

use of net.parostroj.timetable.model.ls.LSException 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 20 with LSException

use of net.parostroj.timetable.model.ls.LSException in project grafikon by jub77.

the class LoadSave method save.

@Override
public void save(TrainDiagram diagram, File file) throws LSException {
    try (ZipOutputStream zipOutput = new ZipOutputStream(new FileOutputStream(file))) {
        // save metadata
        zipOutput.putNextEntry(new ZipEntry(METADATA));
        this.createMetadata().store(zipOutput, null);
        // save train types
        LSTrainTypeList trainTypeList = new LSTrainTypeList(diagram.getTrainTypes(), diagram.getTrainsData());
        zipOutput.putNextEntry(new ZipEntry(TRAIN_TYPES_NAME));
        LSTrainTypeSerializer tts = LSTrainTypeSerializer.getLSTrainTypeSerializer(LSSerializer.getLatestVersion());
        tts.save(new OutputStreamWriter(zipOutput, "utf-8"), trainTypeList);
        // save diagram
        zipOutput.putNextEntry(new ZipEntry(TRAIN_DIAGRAM_NAME));
        this.saveTrainDiagram(new OutputStreamWriter(zipOutput, "utf-8"), diagram, trainTypeList);
        // save images
        LoadSaveImages lsImages = new LoadSaveImages();
        lsImages.saveTimetableImages(diagram, zipOutput);
    } catch (IOException ex) {
        throw new LSException(ex);
    }
}
Also used : ZipOutputStream(java.util.zip.ZipOutputStream) ZipEntry(java.util.zip.ZipEntry) LSException(net.parostroj.timetable.model.ls.LSException)

Aggregations

LSException (net.parostroj.timetable.model.ls.LSException)38 IOException (java.io.IOException)8 JAXBException (javax.xml.bind.JAXBException)7 LSFile (net.parostroj.timetable.model.ls.LSFile)7 FileNotFoundException (java.io.FileNotFoundException)6 ZipEntry (java.util.zip.ZipEntry)6 ModelVersion (net.parostroj.timetable.model.ls.ModelVersion)6 ZipInputStream (java.util.zip.ZipInputStream)5 TrainDiagram (net.parostroj.timetable.model.TrainDiagram)5 Properties (java.util.Properties)4 TrainType (net.parostroj.timetable.model.TrainType)4 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 OutputTemplate (net.parostroj.timetable.model.OutputTemplate)3 Route (net.parostroj.timetable.model.Route)3 File (java.io.File)2 OutputStreamWriter (java.io.OutputStreamWriter)2 Writer (java.io.Writer)2 URL (java.net.URL)2 EngineClass (net.parostroj.timetable.model.EngineClass)2 DiagramChangeSet (net.parostroj.timetable.model.changes.DiagramChangeSet)2