1
Fork 0
mirror of https://github.com/Steffo99/unimore-oop-2020-cleaver.git synced 2024-11-23 00:24:18 +00:00

something

This commit is contained in:
Steffo 2019-12-15 20:48:43 +01:00
parent 3cf9025752
commit cd5137b289
10 changed files with 217 additions and 87 deletions

5
sample_file.chp Normal file
View file

@ -0,0 +1,5 @@
<Cleaver>
<Compress/>
<Crypt/>
<Split mode="by-parts">10</Split>
</Cleaver>

View file

@ -0,0 +1,4 @@
package eu.steffo.cleaver.errors;
public class ChpFileError extends Exception {
}

View file

@ -0,0 +1,4 @@
package eu.steffo.cleaver.errors;
public class ProgrammingError extends Exception {
}

View file

@ -1,11 +1,26 @@
package eu.steffo.cleaver.gui.panels; package eu.steffo.cleaver.gui.panels;
import eu.steffo.cleaver.gui.rows.CreateJobButtonRow;
import eu.steffo.cleaver.gui.rows.option.CompressRow;
import eu.steffo.cleaver.gui.rows.option.CryptRow;
import eu.steffo.cleaver.gui.rows.option.SplitRow;
import eu.steffo.cleaver.logic.ChopJob; import eu.steffo.cleaver.logic.ChopJob;
import eu.steffo.cleaver.logic.Job; import eu.steffo.cleaver.logic.Job;
import eu.steffo.cleaver.logic.compress.CompressConfig;
import eu.steffo.cleaver.logic.crypt.CryptConfig;
import eu.steffo.cleaver.logic.split.SplitConfig;
import javax.swing.*;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
public class ChopPanel extends CreateJobPanel { public class ChopPanel extends CreateJobPanel {
protected final SplitRow splitOptionPanel;
protected final CryptRow cryptOptionPanel;
protected final CompressRow compressOptionPanel;
protected final CreateJobButtonRow createJobButtonPanel;
@Override @Override
protected String getPanelText() { protected String getPanelText() {
@ -19,5 +34,55 @@ public class ChopPanel extends CreateJobPanel {
public ChopPanel(ActionListener onCreateJobClick) { public ChopPanel(ActionListener onCreateJobClick) {
super(onCreateJobClick); super(onCreateJobClick);
this.add(Box.createVerticalStrut(8));
splitOptionPanel = new SplitRow();
this.add(splitOptionPanel);
this.add(Box.createVerticalStrut(8));
cryptOptionPanel = new CryptRow();
this.add(cryptOptionPanel);
this.add(Box.createVerticalStrut(8));
compressOptionPanel = new CompressRow();
this.add(compressOptionPanel);
this.add(Box.createVerticalStrut(8));
createJobButtonPanel = new CreateJobButtonRow(onCreateJobClick);
this.add(createJobButtonPanel);
this.add(Box.createVerticalStrut(8));
}
public void createAndAddJobs(ArrayList<Job> jobs) {
if(fileSelectPanel.getSelectedFiles().length == 0) {
JOptionPane.showMessageDialog(null, "No files selected.", "Error", JOptionPane.ERROR_MESSAGE);
}
for(File file : fileSelectPanel.getSelectedFiles()) {
SplitConfig sc;
try {
sc = splitOptionPanel.getSplitConfig();
} catch (NumberFormatException exc) {
JOptionPane.showMessageDialog(null, "Invalid value in the Split fields.", "Error", JOptionPane.ERROR_MESSAGE);
return;
}
CryptConfig cc = cryptOptionPanel.getCryptConfig();
CompressConfig zc = compressOptionPanel.getCompressConfig();
try {
Job job = getJobClass().getConstructor(File.class, SplitConfig.class, CryptConfig.class, CompressConfig.class).newInstance(file, sc, cc, zc);
jobs.add(job);
} catch (InstantiationException | NoSuchMethodException | InvocationTargetException | IllegalAccessException ex) {
JOptionPane.showMessageDialog(null, ex.toString(), "Error", JOptionPane.ERROR_MESSAGE);
}
}
fileSelectPanel.clearSelectedFiles();
} }
} }

View file

@ -1,27 +1,15 @@
package eu.steffo.cleaver.gui.panels; package eu.steffo.cleaver.gui.panels;
import eu.steffo.cleaver.gui.rows.CreateJobButtonRow;
import eu.steffo.cleaver.gui.rows.FileSelectRow; import eu.steffo.cleaver.gui.rows.FileSelectRow;
import eu.steffo.cleaver.gui.rows.TitleRow; import eu.steffo.cleaver.gui.rows.TitleRow;
import eu.steffo.cleaver.gui.rows.option.*;
import eu.steffo.cleaver.logic.Job; import eu.steffo.cleaver.logic.Job;
import eu.steffo.cleaver.logic.compress.CompressConfig;
import eu.steffo.cleaver.logic.crypt.CryptConfig;
import eu.steffo.cleaver.logic.split.SplitConfig;
import javax.swing.*; import javax.swing.*;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
public abstract class CreateJobPanel extends JPanel { public abstract class CreateJobPanel extends JPanel {
protected final TitleRow titlePanel; protected final TitleRow titlePanel;
protected final FileSelectRow fileSelectPanel; protected final FileSelectRow fileSelectPanel;
protected final SplitRow splitOptionPanel;
protected final CryptRow cryptOptionPanel;
protected final CompressRow compressOptionPanel;
protected final CreateJobButtonRow createJobButtonPanel;
protected abstract String getPanelText(); protected abstract String getPanelText();
@ -42,55 +30,5 @@ public abstract class CreateJobPanel extends JPanel {
fileSelectPanel = new FileSelectRow(); fileSelectPanel = new FileSelectRow();
this.add(fileSelectPanel); this.add(fileSelectPanel);
this.add(Box.createVerticalStrut(8));
splitOptionPanel = new SplitRow();
this.add(splitOptionPanel);
this.add(Box.createVerticalStrut(8));
cryptOptionPanel = new CryptRow();
this.add(cryptOptionPanel);
this.add(Box.createVerticalStrut(8));
compressOptionPanel = new CompressRow();
this.add(compressOptionPanel);
this.add(Box.createVerticalStrut(8));
createJobButtonPanel = new CreateJobButtonRow(onCreateJobClick);
this.add(createJobButtonPanel);
this.add(Box.createVerticalStrut(8));
}
public void createAndAddJobs(ArrayList<Job> jobs) {
if(fileSelectPanel.getSelectedFiles().length == 0) {
JOptionPane.showMessageDialog(null, "No files selected.", "Error", JOptionPane.ERROR_MESSAGE);
}
for(File file : fileSelectPanel.getSelectedFiles()) {
SplitConfig sc;
try {
sc = splitOptionPanel.getSplitConfig();
} catch (NumberFormatException exc) {
JOptionPane.showMessageDialog(null, "Invalid value in the Split fields.", "Error", JOptionPane.ERROR_MESSAGE);
return;
}
CryptConfig cc = cryptOptionPanel.getCryptConfig();
CompressConfig zc = compressOptionPanel.getCompressConfig();
try {
Job job = getJobClass().getConstructor(File.class, SplitConfig.class, CryptConfig.class, CompressConfig.class).newInstance(file, sc, cc, zc);
jobs.add(job);
} catch (InstantiationException | NoSuchMethodException | InvocationTargetException | IllegalAccessException ex) {
JOptionPane.showMessageDialog(null, ex.toString(), "Error", JOptionPane.ERROR_MESSAGE);
}
}
fileSelectPanel.clearSelectedFiles();
} }
} }

View file

@ -1,11 +1,19 @@
package eu.steffo.cleaver.gui.panels; package eu.steffo.cleaver.gui.panels;
import eu.steffo.cleaver.gui.rows.CreateJobButtonRow;
import eu.steffo.cleaver.gui.rows.option.CryptRow;
import eu.steffo.cleaver.logic.Job; import eu.steffo.cleaver.logic.Job;
import eu.steffo.cleaver.logic.StitchJob; import eu.steffo.cleaver.logic.StitchJob;
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
import java.io.File;
import java.util.ArrayList;
public class StitchPanel extends CreateJobPanel { public class StitchPanel extends CreateJobPanel {
protected final CreateJobButtonRow createJobButtonPanel;
protected final CryptRow cryptOptionPanel;
@Override @Override
protected String getPanelText() { protected String getPanelText() {
@ -19,8 +27,30 @@ public class StitchPanel extends CreateJobPanel {
public StitchPanel(ActionListener onCreateJobClick) { public StitchPanel(ActionListener onCreateJobClick) {
super(onCreateJobClick); super(onCreateJobClick);
this.splitOptionPanel.setEditable(false);
this.cryptOptionPanel.setEditable(false); this.add(Box.createVerticalStrut(8));
this.compressOptionPanel.setEditable(false); this.add(Box.createVerticalStrut(24));
this.add(Box.createVerticalStrut(8));
cryptOptionPanel = new CryptRow();
this.add(cryptOptionPanel);
this.add(Box.createVerticalStrut(8));
this.add(Box.createVerticalStrut(24));
this.add(Box.createVerticalStrut(8));
createJobButtonPanel = new CreateJobButtonRow(onCreateJobClick);
this.add(createJobButtonPanel);
this.add(Box.createVerticalStrut(8));
fileSelectPanel.setFileFilter(new FileNameExtensionFilter("Cleaver Metadata (*.chp)", "chp"));
}
public void createAndAddJobs(ArrayList<Job> jobs) {
File[] files = fileSelectPanel.getSelectedFiles();
for(File file : files) {
job
}
} }
} }

View file

@ -4,6 +4,7 @@ import eu.steffo.cleaver.gui.rows.Row;
import java.awt.*; import java.awt.*;
import javax.swing.*; import javax.swing.*;
import javax.swing.filechooser.FileFilter;
import java.io.File; import java.io.File;
public class FileSelectRow extends Row { public class FileSelectRow extends Row {
@ -56,4 +57,8 @@ public class FileSelectRow extends Row {
fileChooser.setSelectedFiles(new File[0]); fileChooser.setSelectedFiles(new File[0]);
update(); update();
} }
public void setFileFilter(FileFilter filter) {
fileChooser.setFileFilter(filter);
}
} }

View file

@ -11,9 +11,15 @@ import java.io.OutputStream;
import java.util.zip.DeflaterOutputStream; import java.util.zip.DeflaterOutputStream;
public class ChopJob extends Job { public class ChopJob extends Job {
protected final SplitConfig splitConfig;
protected final CryptConfig cryptConfig;
protected final CompressConfig compressConfig;
public ChopJob(File file, SplitConfig splitConfig, CryptConfig cryptConfig, CompressConfig compressConfig) { public ChopJob(File file, SplitConfig splitConfig, CryptConfig cryptConfig, CompressConfig compressConfig) {
super(file, splitConfig, cryptConfig, compressConfig); super(file);
this.splitConfig = splitConfig;
this.cryptConfig = cryptConfig;
this.compressConfig = compressConfig;
} }
@Override @Override
@ -37,4 +43,16 @@ public class ChopJob extends Job {
// TODO: end with inputStream.transferTo(outputStream); // TODO: end with inputStream.transferTo(outputStream);
} }
public SplitConfig getSplitConfig() {
return splitConfig;
}
public CryptConfig getCryptConfig() {
return cryptConfig;
}
public CompressConfig getCompressConfig() {
return compressConfig;
}
} }

View file

@ -10,16 +10,10 @@ import eu.steffo.cleaver.logic.split.SplitConfig;
public abstract class Job extends Thread { public abstract class Job extends Thread {
protected final File file; protected final File file;
protected final SplitConfig splitConfig;
protected final CryptConfig cryptConfig;
protected final CompressConfig compressConfig;
protected Progress progress; protected Progress progress;
public Job(File file, SplitConfig splitConfig, CryptConfig cryptConfig, CompressConfig compressConfig) { public Job(File file) {
this.file = file; this.file = file;
this.splitConfig = splitConfig;
this.cryptConfig = cryptConfig;
this.compressConfig = compressConfig;
this.progress = new NotStartedProgress(); this.progress = new NotStartedProgress();
} }
@ -29,18 +23,6 @@ public abstract class Job extends Thread {
return file; return file;
} }
public SplitConfig getSplitConfig() {
return splitConfig;
}
public CryptConfig getCryptConfig() {
return cryptConfig;
}
public CompressConfig getCompressConfig() {
return compressConfig;
}
public Progress getProgress() { public Progress getProgress() {
return progress; return progress;
} }

View file

@ -1,15 +1,82 @@
package eu.steffo.cleaver.logic; package eu.steffo.cleaver.logic;
import eu.steffo.cleaver.errors.ChpFileError;
import eu.steffo.cleaver.errors.ProgrammingError;
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;
import eu.steffo.cleaver.logic.split.SplitByPartsConfig;
import eu.steffo.cleaver.logic.split.SplitBySizeConfig;
import eu.steffo.cleaver.logic.split.SplitConfig; import eu.steffo.cleaver.logic.split.SplitConfig;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.File; import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class StitchJob extends Job { public class StitchJob extends Job {
protected final SplitConfig splitConfig;
protected final CryptConfig cryptConfig;
protected final CompressConfig compressConfig;
public StitchJob(File file, SplitConfig splitConfig, CryptConfig cryptConfig, CompressConfig compressConfig) { public StitchJob(File file, String cryptKey) throws ChpFileError, ProgrammingError {
super(file, splitConfig, cryptConfig, compressConfig); super(file);
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = null;
try {
builder = factory.newDocumentBuilder();
} catch (ParserConfigurationException e) {
throw new ProgrammingError();
}
Document doc = null;
try {
doc = builder.parse(file);
} catch (SAXException | IOException e) {
throw new ProgrammingError();
}
Element root = doc.getDocumentElement();
NodeList splits = root.getElementsByTagName("Split");
NodeList crypts = root.getElementsByTagName("Crypt");
NodeList compresses = root.getElementsByTagName("Compress");
Node splitNode = splits.item(0);
Node cryptNode = crypts.item(0);
Node compressNode = compresses.item(0);
if(splitNode == null) {
splitConfig = null;
}
else {
Element split = (Element)splitNode;
String splitMode = split.getAttribute("mode");
if(splitMode.equals("by-parts")) {
splitConfig = new SplitByPartsConfig(Integer.parseInt(split.getTextContent()));
}
else if(splitMode.equals("by-size")) {
splitConfig = new SplitBySizeConfig(Integer.parseInt(split.getTextContent()));
}
else {
throw new ChpFileError();
}
}
if(cryptNode == null) {
cryptConfig = null;
}
else {
cryptConfig = new CryptConfig(cryptKey);
}
if(compressNode == null) {
compressConfig = null;
}
else {
compressConfig = new CompressConfig();
}
} }
@Override @Override
@ -21,4 +88,16 @@ public class StitchJob extends Job {
public void run() { public void run() {
} }
public SplitConfig getSplitConfig() {
return splitConfig;
}
public CryptConfig getCryptConfig() {
return cryptConfig;
}
public CompressConfig getCompressConfig() {
return compressConfig;
}
} }