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.

147 lines
4.1 KiB

3 years ago
  1. package application.parsing;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.FileOutputStream;
  5. import java.util.HashMap;
  6. import java.util.zip.ZipEntry;
  7. import java.util.zip.ZipOutputStream;
  8. import javax.xml.parsers.SAXParser;
  9. import javax.xml.parsers.SAXParserFactory;
  10. import application.helpers.wrappers.Occupation;
  11. import application.helpers.wrappers.WrappedException;
  12. import application.helpers.wrappers.WriteSelection;
  13. import application.res.Text;
  14. /**
  15. * This class coordinates the work with the parser (Reading and Writing).
  16. * @author Bianca
  17. *
  18. */
  19. public class ParsingWrapper
  20. {
  21. /** The {@link ReadHandler} who will give an overview of the health data given**/
  22. private ReadHandler read;
  23. /** factory needed for the parser**/
  24. private SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
  25. /** the parser**/
  26. private SAXParser saxParser;
  27. /** The health data file**/
  28. private File srcFile;
  29. /**
  30. * Constructor tries to get access to the health data file
  31. *
  32. * @param file file object of the data
  33. * @throws Exception in case there was an error while getting access
  34. */
  35. public ParsingWrapper(File file) throws Exception
  36. {
  37. srcFile=file;
  38. saxParser = saxParserFactory.newSAXParser();
  39. read = new ReadHandler();
  40. saxParser.parse(srcFile, read);
  41. if(read.getExportDate()==null) //wrong file / error reading
  42. {
  43. throw new Exception();
  44. }
  45. }
  46. /**
  47. * Gives access to the read data
  48. * @return
  49. */
  50. public ReadHandler getRead()
  51. {
  52. return read;
  53. }
  54. /**
  55. * Writes the chosen data to the temp-directory
  56. * @param selection The users selection on what data to use
  57. * @param job The given job of the user
  58. * @return the created files. Keys are the readable filenames
  59. * @throws WrappedException If there was an error with the writing to files
  60. */
  61. public HashMap<String,File> writeSelectedDataToFiles(WriteSelection selection, Occupation job) throws WrappedException
  62. {
  63. try
  64. {
  65. WriteHandler write = new WriteHandler(selection, job);
  66. saxParser.parse(srcFile, write);
  67. if(write.getErrorMessages()!=null)
  68. {
  69. throw new WrappedException(null, write.getErrorMessages());
  70. }
  71. return write.getAllFiles();
  72. }
  73. catch (Exception e)
  74. {
  75. throw new WrappedException(e, Text.E_WRITE_TO_TEMP_FILES);
  76. }
  77. }
  78. /**
  79. * Wraps the files from the temp directory into a zip file and places it in the given directory.
  80. * @param files The files to wrap up
  81. * @param targetDir The directory on where to save them
  82. * @return The created zip file or {@code null} if writing / creating didn't work
  83. */
  84. public File writeToZipFile(HashMap<String,File> files, File targetDir)
  85. {
  86. File target = new File(targetDir, "Export.zip");
  87. int c=0;
  88. //file with the same name could already exist
  89. while(target.exists())
  90. {
  91. c++;
  92. target = new File(targetDir, "Export("+c+").zip");
  93. }
  94. boolean created=false;
  95. try
  96. {
  97. created=target.createNewFile();
  98. }
  99. catch(Exception e)
  100. {
  101. //file couldn't be created due to exception
  102. }
  103. if(!created)
  104. { //file couldn't be created, so writing impossible
  105. return null;
  106. }
  107. try(FileOutputStream fos = new FileOutputStream(target);
  108. ZipOutputStream zipOut = new ZipOutputStream(fos))
  109. {
  110. for (String key : files.keySet())
  111. {
  112. File srcFile=files.get(key);
  113. if(srcFile!=null)
  114. {
  115. try(FileInputStream fis = new FileInputStream(srcFile))
  116. {
  117. String filename= srcFile.getName();
  118. filename=filename.substring(filename.indexOf("__")+2);
  119. ZipEntry zipEntry = new ZipEntry(filename);
  120. zipOut.putNextEntry(zipEntry);
  121. byte[] bytes = new byte[1024];
  122. int length;
  123. while((length = fis.read(bytes)) >= 0)
  124. {
  125. zipOut.write(bytes, 0, length);
  126. }
  127. }
  128. }
  129. }
  130. }
  131. catch(Exception e)
  132. {
  133. //Problem with the file streams, Nothing a user could change so:
  134. return null;
  135. }
  136. return target;
  137. }
  138. }