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.
217 lines
5.7 KiB
217 lines
5.7 KiB
package application.parsing;
|
|
|
|
import java.io.BufferedWriter;
|
|
import java.io.File;
|
|
import java.io.FileWriter;
|
|
import java.io.IOException;
|
|
import java.nio.file.Files;
|
|
|
|
import application.helpers.Utils;
|
|
import application.helpers.wrappers.WrappedException;
|
|
import application.res.Text;
|
|
|
|
/**
|
|
* This class wraps a file together with its writer and readable name together,
|
|
* so the writer can be written on repeatedly without having to be opened each
|
|
* and every time.
|
|
*
|
|
* @author Bianca
|
|
*
|
|
*/
|
|
|
|
public class FileWrapper
|
|
{
|
|
/**
|
|
* The file to be wrapped
|
|
*/
|
|
private File file;
|
|
/**
|
|
* The filename in a readable format so it can be displayed to the user.
|
|
*/
|
|
private String readableFileName;
|
|
/**
|
|
* A writer for the file
|
|
*/
|
|
private BufferedWriter bout;
|
|
|
|
/**
|
|
* Creates a file in temp directory for this filename and creates the writer.
|
|
*
|
|
* @param filename Exact filename ending
|
|
* @param readableFilename First part of the readable name
|
|
* @param readableSubFilename Second part of the readable name
|
|
* @throws WrappedException If there was an error creating the file or the
|
|
* writer
|
|
*/
|
|
private FileWrapper(String filename, String readableFilename, String readableSubFilename) throws WrappedException
|
|
{
|
|
try
|
|
{
|
|
file = Files.createTempFile("health_data", "__" + filename).toFile();
|
|
readableFileName = createReadableFileName(readableFilename, readableSubFilename);
|
|
file.deleteOnExit();
|
|
bout = new BufferedWriter(new FileWriter(file, true));
|
|
}
|
|
catch (IOException e)
|
|
{
|
|
throw new WrappedException(null, Text.E_CREATE_TEMP_FILES);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Creates a file in temp directory for this filename and creates the writer.
|
|
*
|
|
* @param filename Exact filename ending
|
|
* @param readableFilename Readable name
|
|
* @throws WrappedException If there was an error creating the file or the
|
|
* writer
|
|
*/
|
|
public FileWrapper(String filename, String readableFilename) throws WrappedException
|
|
{
|
|
this(filename, readableFilename, null);
|
|
}
|
|
|
|
/**
|
|
* Creates a file in temp directory for this filename and creates the writer.
|
|
* The header will be written with the given header coloumns.
|
|
*
|
|
* @param filename Exact filename ending
|
|
* @param readableFilename First part of the readable name
|
|
* @param readableSubFilename Second part of the readable name
|
|
* @param headerColoums The header coloumn names
|
|
* @throws WrappedException If there was an error creating the file or the
|
|
* writer
|
|
*/
|
|
public FileWrapper(String filename, String readableFilename, String readableSubFilename, String[] headerColoums)
|
|
throws WrappedException
|
|
{
|
|
this(filename, readableFilename, readableSubFilename);
|
|
try
|
|
{
|
|
writeHeaderColumn(headerColoums, true);
|
|
bout.newLine();
|
|
}
|
|
catch (IOException e)
|
|
{
|
|
throw new WrappedException(null, Text.E_CREATE_TEMP_FILES);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Combines the two parts of the readable filename into on.
|
|
*
|
|
* @param partOne The first (main) part of the filename.
|
|
* @param partTwo The second (additional) part of the filename.
|
|
* @return The described value.
|
|
*/
|
|
public static String createReadableFileName(String partOne, String partTwo)
|
|
{
|
|
if (partTwo == null)
|
|
{
|
|
return String.format(Text.F_FILE_DESC, partOne, "");
|
|
}
|
|
return String.format(Text.F_FILE_DESC, partOne, partTwo);
|
|
}
|
|
|
|
/**
|
|
* Creates a readable filename.
|
|
*
|
|
* @param name The intended filename.
|
|
* @return The described value.
|
|
*/
|
|
public static String createReadableFileName(String name)
|
|
{
|
|
return createReadableFileName(name, null);
|
|
}
|
|
|
|
/**
|
|
* Writes the header column in the files (names of attributes and sub attributes
|
|
* (in lists))
|
|
*
|
|
* @param attributes names of the attributes
|
|
* @param top attributes can be nested. If this is {@code True}, then
|
|
* this is the top level attribute. If this is {@code False}
|
|
* than it's a sub attribute.
|
|
* @throws IOException if there was an error writing
|
|
*/
|
|
private void writeHeaderColumn(String[] attributes, boolean top) throws IOException
|
|
{
|
|
for (int i = 0; i < attributes.length; i++)
|
|
{
|
|
// write attributeName
|
|
bout.write(attributes[i]);
|
|
// in case of sub attributes write sested elements
|
|
if (attributes[i].equalsIgnoreCase(Text.TAG_NAME_META_DATA_ENTRY))
|
|
{
|
|
bout.write("[[");
|
|
writeHeaderColumn(Utils.META_DATA_ATTR, false);
|
|
bout.write("]*]");
|
|
}
|
|
if (attributes[i].equalsIgnoreCase(Text.TAG_NAME_WORKOUT_EVENT))
|
|
{
|
|
bout.write("[[");
|
|
writeHeaderColumn(Utils.WORKOUT_EVENT_ATTR, false);
|
|
bout.write("]*]");
|
|
}
|
|
if (attributes[i].equalsIgnoreCase(Text.TAG_NAME_WORKOUT_ROUTE))
|
|
{
|
|
bout.write("[[");
|
|
writeHeaderColumn(Utils.WORKOUT_ROUTE_ATTR, false);
|
|
bout.write("]*]");
|
|
}
|
|
if (attributes[i].equalsIgnoreCase(Text.TAG_NAME_HR_LIST))
|
|
{
|
|
bout.write("[[");
|
|
writeHeaderColumn(Utils.HR_LIST_ATTR, false);
|
|
bout.write("]*]");
|
|
}
|
|
if (attributes[i].equalsIgnoreCase(Text.TAG_NAME_IB_PER_MINUTES))
|
|
{
|
|
bout.write("[[");
|
|
writeHeaderColumn(Utils.IB_PER_MINUTES_ATTR, false);
|
|
bout.write("]*]");
|
|
}
|
|
//Elements are separated with ';' on top level and with ',' on all other levels
|
|
if (i < attributes.length - 1)
|
|
{
|
|
if (top)
|
|
{
|
|
bout.write(";");
|
|
}
|
|
else
|
|
{
|
|
bout.write(",");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* Returns the writer for the file.
|
|
* @return the described value.
|
|
*/
|
|
public BufferedWriter getBufferedWriter()
|
|
{
|
|
return bout;
|
|
}
|
|
|
|
/**
|
|
* Returns the file as an object.
|
|
* @return The described value.
|
|
*/
|
|
public File getFile()
|
|
{
|
|
return file;
|
|
}
|
|
|
|
/**
|
|
* Returns the readable filename.
|
|
* @return The described value.
|
|
*/
|
|
public String getReadableFilename()
|
|
{
|
|
return readableFileName;
|
|
}
|
|
|
|
}
|