
net.vectorpublish.desktop.vp.io.VPFileReader Maven / Gradle / Ivy
/*
* Copyright (c) 2016, Peter Rader. All rights reserved.
* ___ ___ __ ______ __ __ __ __
* | | |.-----..----.| |_ .-----..----.| __ \.--.--.| |--.| ||__|.-----.| |--.
* | | || -__|| __|| _|| _ || _|| __/| | || _ || || ||__ --|| |
* \_____/ |_____||____||____||_____||__| |___| |_____||_____||__||__||_____||__|__|
*
* http://www.gnu.org/licenses/gpl-3.0.html
*/
package net.vectorpublish.desktop.vp.io;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.concurrent.Future;
import javax.inject.Inject;
import javax.inject.Named;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import net.vectorpublish.desktop.vp.DefaultDocumentWriterImpl;
import net.vectorpublish.desktop.vp.History;
import net.vectorpublish.desktop.vp.History.HistoryStep;
import net.vectorpublish.desktop.vp.api.history.ReadOnlyHistoryStepDataBean;
import net.vectorpublish.desktop.vp.api.io.DrawArea;
import net.vectorpublish.desktop.vp.api.io.Open;
import net.vectorpublish.desktop.vp.api.ui.KeyframeSlider;
import net.vectorpublish.desktop.vp.log.Log;
import net.vectorpublish.desktop.vp.pd.official.DrawPanel;
/**
* The reader for the {@link DefaultDocumentWriterImpl writer}.
*/
@Named
public class VPFileReader {
@Inject
private final Log log = null;
@Inject
private final History history = null;
@Inject
private final KeyframeSlider keyframer = null;
@Async
public Future readFully(File f, VPDocumentNode doc) throws IOException, ClassNotFoundException, NoSuchMethodException, SecurityException,
InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
Thread.currentThread().setName(Open.THREAD_NAME_FILE_LOAD);
final FileInputStream fis = new FileInputStream(f);
final ObjectInputStream is = new ObjectInputStream(fis);
try {
final String name = f.getAbsolutePath();
if (!is.readUTF().equals("VPD")) {
throw new IOException(name);
}
final int width = is.readInt();
final int height = is.readInt();
final VPDocumentNode document;
if (doc == null) {
final DrawArea drawArea = new DrawArea(width, height);
document = new VPDocumentNode(new DrawPanel(f.getName(), drawArea, keyframer));
drawArea.setDocument(document);
} else {
document = doc;
}
if (width < 1) {
throw new IOException(name);
}
if (height < 1) {
throw new IOException(name);
}
final int latestHistoryStep = is.readInt();
if (latestHistoryStep < 0) {
throw new IOException(name);
}
int historyStepIndex = 1;
HistoryStep> step = null;
while (is.readBoolean()) {
final String classname = is.readUTF();
@SuppressWarnings("unchecked")
final Class> stepClass = (Class>) Class.forName(classname);
final ReadOnlyHistoryStepDataBean data = (ReadOnlyHistoryStepDataBean) is.readObject();
if (step == null) {
step = stepClass.getConstructor(History.class, data.getClass()).newInstance(history, data);
} else {
try {
step = stepClass.getConstructor(History.class, HistoryStep.class, data.getClass()).newInstance(history, step, data);
} catch (final NoSuchMethodException m) {
final Constructor>[] declaredConstructors = stepClass.getDeclaredConstructors();
for (final Constructor> constructor : declaredConstructors) {
log.system(constructor);
}
is.close();
throw m;
}
}
if (historyStepIndex < latestHistoryStep) {
document.setLastExecutedHistoryStep(step);
}
historyStepIndex++;
}
return new AsyncResult(document);
} finally {
is.close();
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy