You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
147 lines
4.1 KiB
147 lines
4.1 KiB
package application.parsing;
|
|
|
|
import java.io.File;
|
|
import java.io.FileInputStream;
|
|
import java.io.FileOutputStream;
|
|
import java.util.HashMap;
|
|
import java.util.zip.ZipEntry;
|
|
import java.util.zip.ZipOutputStream;
|
|
|
|
import javax.xml.parsers.SAXParser;
|
|
import javax.xml.parsers.SAXParserFactory;
|
|
|
|
import application.helpers.wrappers.Occupation;
|
|
import application.helpers.wrappers.WrappedException;
|
|
import application.helpers.wrappers.WriteSelection;
|
|
import application.res.Text;
|
|
|
|
/**
|
|
* This class coordinates the work with the parser (Reading and Writing).
|
|
* @author Bianca
|
|
*
|
|
*/
|
|
public class ParsingWrapper
|
|
{
|
|
/** The {@link ReadHandler} who will give an overview of the health data given**/
|
|
private ReadHandler read;
|
|
/** factory needed for the parser**/
|
|
private SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
|
|
/** the parser**/
|
|
private SAXParser saxParser;
|
|
/** The health data file**/
|
|
private File srcFile;
|
|
|
|
/**
|
|
* Constructor tries to get access to the health data file
|
|
*
|
|
* @param file file object of the data
|
|
* @throws Exception in case there was an error while getting access
|
|
*/
|
|
public ParsingWrapper(File file) throws Exception
|
|
{
|
|
srcFile=file;
|
|
saxParser = saxParserFactory.newSAXParser();
|
|
read = new ReadHandler();
|
|
saxParser.parse(srcFile, read);
|
|
if(read.getExportDate()==null) //wrong file / error reading
|
|
{
|
|
throw new Exception();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Gives access to the read data
|
|
* @return
|
|
*/
|
|
public ReadHandler getRead()
|
|
{
|
|
return read;
|
|
}
|
|
|
|
/**
|
|
* Writes the chosen data to the temp-directory
|
|
* @param selection The users selection on what data to use
|
|
* @param job The given job of the user
|
|
* @return the created files. Keys are the readable filenames
|
|
* @throws WrappedException If there was an error with the writing to files
|
|
*/
|
|
public HashMap<String,File> writeSelectedDataToFiles(WriteSelection selection, Occupation job) throws WrappedException
|
|
{
|
|
try
|
|
{
|
|
WriteHandler write = new WriteHandler(selection, job);
|
|
saxParser.parse(srcFile, write);
|
|
if(write.getErrorMessages()!=null)
|
|
{
|
|
throw new WrappedException(null, write.getErrorMessages());
|
|
}
|
|
return write.getAllFiles();
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
throw new WrappedException(e, Text.E_WRITE_TO_TEMP_FILES);
|
|
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Wraps the files from the temp directory into a zip file and places it in the given directory.
|
|
* @param files The files to wrap up
|
|
* @param targetDir The directory on where to save them
|
|
* @return The created zip file or {@code null} if writing / creating didn't work
|
|
*/
|
|
public File writeToZipFile(HashMap<String,File> files, File targetDir)
|
|
{
|
|
File target = new File(targetDir, "Export.zip");
|
|
int c=0;
|
|
//file with the same name could already exist
|
|
while(target.exists())
|
|
{
|
|
c++;
|
|
target = new File(targetDir, "Export("+c+").zip");
|
|
}
|
|
boolean created=false;
|
|
try
|
|
{
|
|
created=target.createNewFile();
|
|
}
|
|
catch(Exception e)
|
|
{
|
|
//file couldn't be created due to exception
|
|
}
|
|
if(!created)
|
|
{ //file couldn't be created, so writing impossible
|
|
return null;
|
|
}
|
|
try(FileOutputStream fos = new FileOutputStream(target);
|
|
ZipOutputStream zipOut = new ZipOutputStream(fos))
|
|
{
|
|
for (String key : files.keySet())
|
|
{
|
|
File srcFile=files.get(key);
|
|
if(srcFile!=null)
|
|
{
|
|
try(FileInputStream fis = new FileInputStream(srcFile))
|
|
{
|
|
String filename= srcFile.getName();
|
|
filename=filename.substring(filename.indexOf("__")+2);
|
|
ZipEntry zipEntry = new ZipEntry(filename);
|
|
zipOut.putNextEntry(zipEntry);
|
|
byte[] bytes = new byte[1024];
|
|
int length;
|
|
while((length = fis.read(bytes)) >= 0)
|
|
{
|
|
zipOut.write(bytes, 0, length);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
catch(Exception e)
|
|
{
|
|
//Problem with the file streams, Nothing a user could change so:
|
|
return null;
|
|
}
|
|
return target;
|
|
}
|
|
}
|