Search in sources :

Example 1 with LSException

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

the class LSLibraryImpl method load.

@Override
public Library load(ZipInputStream is) throws LSException {
    try {
        ZipEntry entry = null;
        ModelVersion version = (ModelVersion) properties.get(VERSION_PROPERTY);
        LibraryBuilder libraryBuilder = new LibraryBuilder();
        while ((entry = is.getNextEntry()) != null) {
            if (entry.getName().equals(METADATA)) {
                // check major and minor version (do not allow load newer versions)
                Properties props = new Properties();
                props.load(is);
                version = checkVersion(METADATA_KEY_LIBRARY_VERSION, props);
                continue;
            }
            LSLibraryItem lsItem = lss.load(is, LSLibraryItem.class);
            lsItem.createLibraryItem(libraryBuilder);
        }
        log.debug("Loaded version: {}", version != null ? version : "<missing>");
        return libraryBuilder.build();
    } catch (IOException e) {
        throw new LSException(e);
    }
}
Also used : LibraryBuilder(net.parostroj.timetable.model.library.LibraryBuilder) ZipEntry(java.util.zip.ZipEntry) ModelVersion(net.parostroj.timetable.model.ls.ModelVersion) IOException(java.io.IOException) Properties(java.util.Properties) LSException(net.parostroj.timetable.model.ls.LSException)

Example 2 with LSException

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

the class LSLibraryImpl method save.

@Override
public void save(Library library, ZipOutputStream zipOutput) throws LSException {
    try {
        // save metadata
        zipOutput.putNextEntry(new ZipEntry(METADATA));
        this.createMetadata(METADATA_KEY_LIBRARY_VERSION).store(zipOutput, null);
        for (LibraryItemType itemType : LibraryItemType.values()) {
            for (LibraryItem item : library.getItems().get(itemType)) {
                this.save(zipOutput, String.format("%s/%s.%s", LSLibraryTypeMapping.typeToDirectory(item.getType()), item.getObject().getId(), "xml"), new LSLibraryItem(item));
            }
        }
    } catch (IOException e) {
        throw new LSException(e);
    }
}
Also used : LibraryItem(net.parostroj.timetable.model.library.LibraryItem) LibraryItemType(net.parostroj.timetable.model.library.LibraryItemType) ZipEntry(java.util.zip.ZipEntry) IOException(java.io.IOException) LSException(net.parostroj.timetable.model.ls.LSException)

Example 3 with LSException

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

the class AbstractLSImpl method checkVersion.

protected ModelVersion checkVersion(String versionKey, Properties props) throws LSException {
    ModelVersion current = getSaveVersion();
    ModelVersion loaded = ModelVersion.parseModelVersion(props.getProperty(versionKey));
    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 4 with LSException

use of net.parostroj.timetable.model.ls.LSException 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);
        FileLoadSaveAttachments attachments = new FileLoadSaveAttachments(DATA_ATTACHMENTS);
        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(METADATA_KEY_MODEL_VERSION, props);
                continue;
            }
            if (entry.getName().equals(DATA_TRAIN_DIAGRAM)) {
                LSTrainDiagram lstd = lss.load(zipInput, LSTrainDiagram.class);
                builder = new TrainDiagramBuilder(lstd, attachments);
            }
            // test diagram
            if (builder == null) {
                throw new LSException("Train diagram builder has to be first entry: " + entry.getName());
            }
            if (entry.getName().equals(DATA_PENALTY_TABLE)) {
                builder.setPenaltyTable(lss.load(zipInput, LSPenaltyTable.class));
            } else 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_TYPE_CATEGORIES)) {
                builder.setTrainTypeCategory(lss.load(zipInput, LSTrainTypeCategory.class));
            } else if (entry.getName().startsWith(DATA_TRAIN_TYPES)) {
                builder.setTrainType(lss.load(zipInput, LSTrainType.class));
            } else if (entry.getName().startsWith(DATA_TEXT_ITEMS)) {
                builder.setTextItem(lss.load(zipInput, LSTextItem.class));
            } else if (entry.getName().startsWith(DATA_OUTPUT_TEMPLATES)) {
                builder.setOutputTemplate(lss.load(zipInput, LSOutputTemplate.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_CHANGES)) {
                builder.setDiagramChangeSet(lss.load(zipInput, LSDiagramChangeSet.class));
            } else if (entry.getName().startsWith(FREIGHT_NET)) {
                builder.setFreightNet(lss.load(zipInput, LSFreightNet.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));
                }
            } else if (entry.getName().startsWith(DATA_ATTACHMENTS)) {
                attachments.load(zipInput, entry);
            } else if (entry.getName().startsWith(DATA_OUTPUTS)) {
                builder.setOutput(lss.load(zipInput, LSOutput.class));
            }
        }
        TrainDiagram trainDiagram = builder.getTrainDiagram();
        for (LoadFilter filter : loadFilters) {
            filter.checkDiagram(trainDiagram, version);
        }
        log.debug("Loaded version: {}", version != null ? version : "<missing>");
        return trainDiagram;
    } catch (IOException e) {
        throw new LSException(e);
    }
}
Also used : ZipEntry(java.util.zip.ZipEntry) IOException(java.io.IOException) Properties(java.util.Properties) TrainDiagram(net.parostroj.timetable.model.TrainDiagram) ModelVersion(net.parostroj.timetable.model.ls.ModelVersion) File(java.io.File) LSFile(net.parostroj.timetable.model.ls.LSFile) LSException(net.parostroj.timetable.model.ls.LSException)

Example 5 with LSException

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

the class LSSerializer method save.

public <T> void save(OutputStream out, T saved) throws LSException {
    Writer writer = null;
    try {
        writer = new OutputStreamWriter(out, "utf-8");
        marshaller.marshal(saved, writer);
    } catch (UnsupportedEncodingException e) {
        throw new LSException("Cannot save train diagram: Unsupported enconding.", e);
    } catch (JAXBException e) {
        throw new LSException("Cannot save train diagram: JAXB exception.", e);
    }
}
Also used : JAXBException(javax.xml.bind.JAXBException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) OutputStreamWriter(java.io.OutputStreamWriter) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter) 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