Repository for the HealthTool which enables Apple users to analyse their health data from the Apple health app and prepares the data for contributing it for future studies on wearable data.
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

3 years ago
  1. package application.parsing;
  2. import java.io.BufferedWriter;
  3. import java.io.File;
  4. import java.io.FileWriter;
  5. import java.io.IOException;
  6. import java.nio.file.Files;
  7. import application.helpers.Utils;
  8. import application.helpers.wrappers.WrappedException;
  9. import application.res.Text;
  10. /**
  11. * This class wraps a file together with its writer and readable name together,
  12. * so the writer can be written on repeatedly without having to be opened each
  13. * and every time.
  14. *
  15. * @author Bianca
  16. *
  17. */
  18. public class FileWrapper
  19. {
  20. /**
  21. * The file to be wrapped
  22. */
  23. private File file;
  24. /**
  25. * The filename in a readable format so it can be displayed to the user.
  26. */
  27. private String readableFileName;
  28. /**
  29. * A writer for the file
  30. */
  31. private BufferedWriter bout;
  32. /**
  33. * Creates a file in temp directory for this filename and creates the writer.
  34. *
  35. * @param filename Exact filename ending
  36. * @param readableFilename First part of the readable name
  37. * @param readableSubFilename Second part of the readable name
  38. * @throws WrappedException If there was an error creating the file or the
  39. * writer
  40. */
  41. private FileWrapper(String filename, String readableFilename, String readableSubFilename) throws WrappedException
  42. {
  43. try
  44. {
  45. file = Files.createTempFile("health_data", "__" + filename).toFile();
  46. readableFileName = createReadableFileName(readableFilename, readableSubFilename);
  47. file.deleteOnExit();
  48. bout = new BufferedWriter(new FileWriter(file, true));
  49. }
  50. catch (IOException e)
  51. {
  52. throw new WrappedException(null, Text.E_CREATE_TEMP_FILES);
  53. }
  54. }
  55. /**
  56. * Creates a file in temp directory for this filename and creates the writer.
  57. *
  58. * @param filename Exact filename ending
  59. * @param readableFilename Readable name
  60. * @throws WrappedException If there was an error creating the file or the
  61. * writer
  62. */
  63. public FileWrapper(String filename, String readableFilename) throws WrappedException
  64. {
  65. this(filename, readableFilename, null);
  66. }
  67. /**
  68. * Creates a file in temp directory for this filename and creates the writer.
  69. * The header will be written with the given header coloumns.
  70. *
  71. * @param filename Exact filename ending
  72. * @param readableFilename First part of the readable name
  73. * @param readableSubFilename Second part of the readable name
  74. * @param headerColoums The header coloumn names
  75. * @throws WrappedException If there was an error creating the file or the
  76. * writer
  77. */
  78. public FileWrapper(String filename, String readableFilename, String readableSubFilename, String[] headerColoums)
  79. throws WrappedException
  80. {
  81. this(filename, readableFilename, readableSubFilename);
  82. try
  83. {
  84. writeHeaderColumn(headerColoums, true);
  85. bout.newLine();
  86. }
  87. catch (IOException e)
  88. {
  89. throw new WrappedException(null, Text.E_CREATE_TEMP_FILES);
  90. }
  91. }
  92. /**
  93. * Combines the two parts of the readable filename into on.
  94. *
  95. * @param partOne The first (main) part of the filename.
  96. * @param partTwo The second (additional) part of the filename.
  97. * @return The described value.
  98. */
  99. public static String createReadableFileName(String partOne, String partTwo)
  100. {
  101. if (partTwo == null)
  102. {
  103. return String.format(Text.F_FILE_DESC, partOne, "");
  104. }
  105. return String.format(Text.F_FILE_DESC, partOne, partTwo);
  106. }
  107. /**
  108. * Creates a readable filename.
  109. *
  110. * @param name The intended filename.
  111. * @return The described value.
  112. */
  113. public static String createReadableFileName(String name)
  114. {
  115. return createReadableFileName(name, null);
  116. }
  117. /**
  118. * Writes the header column in the files (names of attributes and sub attributes
  119. * (in lists))
  120. *
  121. * @param attributes names of the attributes
  122. * @param top attributes can be nested. If this is {@code True}, then
  123. * this is the top level attribute. If this is {@code False}
  124. * than it's a sub attribute.
  125. * @throws IOException if there was an error writing
  126. */
  127. private void writeHeaderColumn(String[] attributes, boolean top) throws IOException
  128. {
  129. for (int i = 0; i < attributes.length; i++)
  130. {
  131. // write attributeName
  132. bout.write(attributes[i]);
  133. // in case of sub attributes write sested elements
  134. if (attributes[i].equalsIgnoreCase(Text.TAG_NAME_META_DATA_ENTRY))
  135. {
  136. bout.write("[[");
  137. writeHeaderColumn(Utils.META_DATA_ATTR, false);
  138. bout.write("]*]");
  139. }
  140. if (attributes[i].equalsIgnoreCase(Text.TAG_NAME_WORKOUT_EVENT))
  141. {
  142. bout.write("[[");
  143. writeHeaderColumn(Utils.WORKOUT_EVENT_ATTR, false);
  144. bout.write("]*]");
  145. }
  146. if (attributes[i].equalsIgnoreCase(Text.TAG_NAME_WORKOUT_ROUTE))
  147. {
  148. bout.write("[[");
  149. writeHeaderColumn(Utils.WORKOUT_ROUTE_ATTR, false);
  150. bout.write("]*]");
  151. }
  152. if (attributes[i].equalsIgnoreCase(Text.TAG_NAME_HR_LIST))
  153. {
  154. bout.write("[[");
  155. writeHeaderColumn(Utils.HR_LIST_ATTR, false);
  156. bout.write("]*]");
  157. }
  158. if (attributes[i].equalsIgnoreCase(Text.TAG_NAME_IB_PER_MINUTES))
  159. {
  160. bout.write("[[");
  161. writeHeaderColumn(Utils.IB_PER_MINUTES_ATTR, false);
  162. bout.write("]*]");
  163. }
  164. //Elements are separated with ';' on top level and with ',' on all other levels
  165. if (i < attributes.length - 1)
  166. {
  167. if (top)
  168. {
  169. bout.write(";");
  170. }
  171. else
  172. {
  173. bout.write(",");
  174. }
  175. }
  176. }
  177. }
  178. /**
  179. * Returns the writer for the file.
  180. * @return the described value.
  181. */
  182. public BufferedWriter getBufferedWriter()
  183. {
  184. return bout;
  185. }
  186. /**
  187. * Returns the file as an object.
  188. * @return The described value.
  189. */
  190. public File getFile()
  191. {
  192. return file;
  193. }
  194. /**
  195. * Returns the readable filename.
  196. * @return The described value.
  197. */
  198. public String getReadableFilename()
  199. {
  200. return readableFileName;
  201. }
  202. }