1
Fork 0
mirror of https://github.com/Steffo99/unimore-oop-2020-cleaver.git synced 2024-11-26 01:54:20 +00:00

Improve Jobs API

This commit is contained in:
Steffo 2019-12-16 01:46:33 +01:00
parent cc20a5289f
commit bc2591b7a2
4 changed files with 40 additions and 19 deletions

View file

@ -5,10 +5,6 @@ import eu.steffo.cleaver.logic.crypt.CryptConfig;
import eu.steffo.cleaver.logic.split.SplitConfig; import eu.steffo.cleaver.logic.split.SplitConfig;
import java.io.File; import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.OutputStream;
import java.util.zip.DeflaterOutputStream;
public class ChopJob extends Job { public class ChopJob extends Job {
@ -19,6 +15,13 @@ public class ChopJob extends Job {
this.compressConfig = compressConfig; this.compressConfig = compressConfig;
} }
public ChopJob(File file, Runnable swingCallLaterOnProgressChanges, SplitConfig splitConfig, CryptConfig cryptConfig, CompressConfig compressConfig) {
super(file, swingCallLaterOnProgressChanges);
this.splitConfig = splitConfig;
this.cryptConfig = cryptConfig;
this.compressConfig = compressConfig;
}
@Override @Override
public String getType() { public String getType() {
return "Chop"; return "Chop";
@ -26,7 +29,6 @@ public class ChopJob extends Job {
@Override @Override
public void run() { public void run() {
System.out.println("CHOP");
} }
public SplitConfig getSplitConfig() { public SplitConfig getSplitConfig() {

View file

@ -1,6 +1,7 @@
package eu.steffo.cleaver.logic; package eu.steffo.cleaver.logic;
import java.io.File; import java.io.File;
import javax.swing.SwingUtilities;
import eu.steffo.cleaver.logic.compress.CompressConfig; import eu.steffo.cleaver.logic.compress.CompressConfig;
import eu.steffo.cleaver.logic.crypt.CryptConfig; import eu.steffo.cleaver.logic.crypt.CryptConfig;
@ -10,17 +11,22 @@ import eu.steffo.cleaver.logic.split.SplitConfig;
public abstract class Job extends Thread { public abstract class Job extends Thread {
protected File file; protected File file;
protected SplitConfig splitConfig;
protected CryptConfig cryptConfig; private Progress progress;
protected CompressConfig compressConfig; protected Runnable swingCallLaterOnProgressChanges = null;
protected Progress progress;
protected SplitConfig splitConfig = null;
protected CryptConfig cryptConfig = null;
protected CompressConfig compressConfig = null;
public Job(File file) { public Job(File file) {
this.file = file; this.file = file;
this.progress = new NotStartedProgress(); this.progress = new NotStartedProgress();
splitConfig = null; }
cryptConfig = null;
compressConfig = null; public Job(File file, Runnable swingCallLaterOnProgressChanges) {
this(file);
this.swingCallLaterOnProgressChanges = swingCallLaterOnProgressChanges;
} }
public abstract String getType(); public abstract String getType();
@ -33,6 +39,11 @@ public abstract class Job extends Thread {
return progress; return progress;
} }
protected void setProgress(Progress progress) {
this.progress = progress;
SwingUtilities.invokeLater(swingCallLaterOnProgressChanges);
}
public SplitConfig getSplitConfig() { public SplitConfig getSplitConfig() {
return splitConfig; return splitConfig;
} }

View file

@ -26,6 +26,10 @@ public class StitchJob extends Job {
public StitchJob(File file, String cryptKey) throws ChpFileError, ProgrammingError { public StitchJob(File file, String cryptKey) throws ChpFileError, ProgrammingError {
super(file); super(file);
parseChp(openChp(file), cryptKey);
}
protected static Document openChp(File file) throws ChpFileError, ProgrammingError {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder; DocumentBuilder builder;
try { try {
@ -39,26 +43,30 @@ public class StitchJob extends Job {
} catch (SAXException e) { } catch (SAXException e) {
throw new ProgrammingError(); throw new ProgrammingError();
} catch (IOException e) { } catch (IOException e) {
throw new ChpFileError(".chp file does not exist anymore!"); throw new ChpFileError("The .chp file does not exist anymore!");
} }
return doc;
}
protected final void parseChp(Document doc, String cryptKey) {
Element root = doc.getDocumentElement(); Element root = doc.getDocumentElement();
NodeList splits = root.getElementsByTagName("Split"); NodeList splits = root.getElementsByTagName("Split");
NodeList crypts = root.getElementsByTagName("Crypt"); NodeList crypts = root.getElementsByTagName("Crypt");
NodeList compresses = root.getElementsByTagName("Compress"); NodeList compresses = root.getElementsByTagName("Compress");
Node splitNode = splits.item(0); Node splitNode = splits.item(0);
Node cryptNode = crypts.item(0); Node cryptNode = crypts.item(0);
Node compressNode = compresses.item(0); Node compressNode = compresses.item(0);
if(splitNode != null) { if(splitNode != null) {
Element split = (Element)splitNode; Element split = (Element)splitNode;
String splitMode = split.getAttribute("mode"); String splitMode = split.getAttribute("mode");
if(splitMode.equals("by-parts")) { if(splitMode.equals("by-parts")) {
splitConfig = new SplitByPartsConfig(Integer.parseInt(split.getTextContent())); splitConfig = new SplitByPartsConfig(Integer.parseInt(split.getTextContent()));
} }
else if(splitMode.equals("by-size")) {
splitConfig = new SplitBySizeConfig(Integer.parseInt(split.getTextContent()));
}
else { else {
throw new ChpFileError(".chp file contains invalid split mode!"); splitConfig = new SplitBySizeConfig(Integer.parseInt(split.getTextContent()));
} }
} }
if(cryptNode != null) { if(cryptNode != null) {
@ -76,6 +84,6 @@ public class StitchJob extends Job {
@Override @Override
public void run() { public void run() {
System.out.println("STITCH");
} }
} }

View file

@ -1,7 +1,7 @@
package eu.steffo.cleaver.logic.progress; package eu.steffo.cleaver.logic.progress;
public class WorkingProgress extends Progress { public class WorkingProgress extends Progress {
protected float progress; public final float progress;
public WorkingProgress() { public WorkingProgress() {
this.progress = 0f; this.progress = 0f;