PageRenderTime 62ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/modules/apps/sync/sync-engine/src/main/java/com/liferay/sync/engine/util/MSOfficeFileUtil.java

http://github.com/liferay/liferay-portal
Java | 165 lines | 114 code | 35 blank | 16 comment | 22 complexity | b866d3f81504e612bf5d234569b664b6 MD5 | raw file
Possible License(s): LGPL-2.0
  1. /**
  2. * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
  3. *
  4. * This library is free software; you can redistribute it and/or modify it under
  5. * the terms of the GNU Lesser General Public License as published by the Free
  6. * Software Foundation; either version 2.1 of the License, or (at your option)
  7. * any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  11. * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
  12. * details.
  13. */
  14. package com.liferay.sync.engine.util;
  15. import java.nio.file.Files;
  16. import java.nio.file.Path;
  17. import java.util.Arrays;
  18. import java.util.Date;
  19. import java.util.HashSet;
  20. import java.util.Set;
  21. import java.util.regex.Matcher;
  22. import java.util.regex.Pattern;
  23. import org.apache.commons.io.FilenameUtils;
  24. import org.apache.poi.hpsf.HPSFPropertiesOnlyDocument;
  25. import org.apache.poi.hpsf.SummaryInformation;
  26. import org.apache.poi.poifs.filesystem.NPOIFSFileSystem;
  27. /**
  28. * @author Shinn Lok
  29. */
  30. public class MSOfficeFileUtil {
  31. public static Date getLastSavedDate(Path filePath) {
  32. NPOIFSFileSystem npoifsFileSystem = null;
  33. try {
  34. npoifsFileSystem = new NPOIFSFileSystem(filePath.toFile());
  35. HPSFPropertiesOnlyDocument hpsfPropertiesOnlyDocument =
  36. new HPSFPropertiesOnlyDocument(npoifsFileSystem);
  37. SummaryInformation summaryInformation =
  38. hpsfPropertiesOnlyDocument.getSummaryInformation();
  39. return summaryInformation.getLastSaveDateTime();
  40. }
  41. catch (Exception e) {
  42. return null;
  43. }
  44. finally {
  45. if (npoifsFileSystem != null) {
  46. try {
  47. npoifsFileSystem.close();
  48. }
  49. catch (Exception e) {
  50. return null;
  51. }
  52. }
  53. }
  54. }
  55. public static boolean isLegacyExcelFile(Path filePath) {
  56. String extension = FilenameUtils.getExtension(filePath.toString());
  57. if (extension == null) {
  58. return false;
  59. }
  60. extension = extension.toLowerCase();
  61. if (extension.equals("xls") && !Files.isDirectory(filePath)) {
  62. return true;
  63. }
  64. return false;
  65. }
  66. public static boolean isTempCreatedFile(Path filePath) {
  67. String fileName = String.valueOf(filePath.getFileName());
  68. if ((fileName.startsWith("~$") ||
  69. ((fileName.startsWith("~") || fileName.startsWith("ppt") ||
  70. fileName.startsWith("pub")) &&
  71. fileName.endsWith(".tmp"))) &&
  72. !Files.isDirectory(filePath)) {
  73. return true;
  74. }
  75. Matcher matcher = _tempCreatedFilePattern.matcher(
  76. String.valueOf(filePath.getFileName()));
  77. if (matcher.matches() && !Files.isDirectory(filePath)) {
  78. return true;
  79. }
  80. return false;
  81. }
  82. public static boolean isTempRenamedFile(
  83. Path sourceFilePath, Path targetFilePath) {
  84. String extension = FilenameUtils.getExtension(
  85. sourceFilePath.toString());
  86. if (extension.equals("pub")) {
  87. String fileName = String.valueOf(targetFilePath.getFileName());
  88. if (fileName.startsWith("pub") && fileName.endsWith(".tmp")) {
  89. return true;
  90. }
  91. }
  92. else if (hasExtension(extension, _excelExtensions) ||
  93. hasExtension(extension, _powerpointExtensions)) {
  94. Matcher matcher = _tempRenamedFilePattern.matcher(
  95. String.valueOf(targetFilePath.getFileName()));
  96. if (matcher.matches()) {
  97. return true;
  98. }
  99. }
  100. else if (hasExtension(extension, _wordExtensions)) {
  101. String fileName = String.valueOf(targetFilePath.getFileName());
  102. if (fileName.startsWith("~WR") && fileName.endsWith(".tmp")) {
  103. return true;
  104. }
  105. }
  106. return false;
  107. }
  108. protected static boolean hasExtension(
  109. String extension, Set<String> extensions) {
  110. if ((extension != null) &&
  111. extensions.contains(extension.toLowerCase())) {
  112. return true;
  113. }
  114. return false;
  115. }
  116. private static final Set<String> _excelExtensions = new HashSet<>(
  117. Arrays.asList(
  118. "csv", "xla", "xlam", "xls", "xlsb", "xlsm", "xlsx", "xlt", "xltm",
  119. "xltx"));
  120. private static final Set<String> _powerpointExtensions = new HashSet<>(
  121. Arrays.asList(
  122. "pot", "potm", "potx", "ppa", "ppam", "pps", "ppsm", "ppsx", "ppt",
  123. "pptm", "pptx"));
  124. private static final Pattern _tempCreatedFilePattern = Pattern.compile(
  125. "[0-9A-F]{6,8}\\.tmp");
  126. private static final Pattern _tempRenamedFilePattern = Pattern.compile(
  127. "[0-9A-F]{6,8}(\\.tmp)?");
  128. private static final Set<String> _wordExtensions = new HashSet<>(
  129. Arrays.asList("doc", "docb", "docm", "docx", "dot", "dotm", "dotx"));
  130. }