/src/main/java/org/kyasuda/docwaza/util/PlatformUtils.java

https://github.com/ayasuda2003/docwaza · Java · 161 lines · 107 code · 23 blank · 31 comment · 16 complexity · cf5f8a5200e18c5781eee75263e2e1f2 MD5 · raw file

  1. //
  2. // JODConverter - Java OpenDocument Converter
  3. // Copyright 2009 Art of Solving Ltd
  4. // Copyright 2004-2009 Mirko Nasato
  5. //
  6. // JODConverter is free software: you can redistribute it and/or
  7. // modify it under the terms of the GNU Lesser General Public License
  8. // as published by the Free Software Foundation, either version 3 of
  9. // the License, or (at your option) any later version.
  10. //
  11. // JODConverter is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. // Lesser General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU Lesser General
  17. // Public License along with JODConverter. If not, see
  18. // <http://www.gnu.org/licenses/>.
  19. //
  20. // Contributors:
  21. // Yasuda Keisuke (kyasuda), Chia Chang (chiayear2003)
  22. package org.kyasuda.docwaza.util;
  23. import java.io.File;
  24. import java.util.Arrays;
  25. import java.util.List;
  26. import java.util.logging.Logger;
  27. public class PlatformUtils {
  28. private static final Logger logger = Logger.getLogger(PlatformUtils.class.getName());
  29. private static final String OS_NAME = System.getProperty("os.name").toLowerCase();
  30. private static final String WINDOWS = "windows";
  31. private static final String MAC = "mac";
  32. private static final String LINUX = "linux";
  33. private static final String[] LINUX_OO_HOME_PATHS = {
  34. "/usr/lib/libreoffice", "/usr/lib/openoffice",
  35. "/usr/lib/openoffice.org", "/usr/lib/openoffice.org3","/opt/openoffice4",
  36. "/opt/openoffice.org3", "/opt/libreoffice", "/usr/lib/ooo" };
  37. private static final String[] MAC_OO_HOME_PATHS = {
  38. "/Applications/LibreOffice.app/Contents",
  39. "/Applications/OpenOffice.app/Contents" };
  40. private static final String[] WINDOWS_OO_HOME_PATHS = {
  41. System.getenv("ProgramFiles") + File.separator + "LibreOffice 3.9",
  42. System.getenv("ProgramFiles") + File.separator + "LibreOffice 3.8",
  43. System.getenv("ProgramFiles") + File.separator + "LibreOffice 3.7",
  44. System.getenv("ProgramFiles") + File.separator + "LibreOffice 3.6",
  45. System.getenv("ProgramFiles") + File.separator + "LibreOffice 3.5",
  46. System.getenv("ProgramFiles") + File.separator + "LibreOffice 3.4",
  47. System.getenv("ProgramFiles") + File.separator + "LibreOffice 3",
  48. System.getenv("ProgramFiles(x86)") + File.separator
  49. + "LibreOffice 3.9",
  50. System.getenv("ProgramFiles(x86)") + File.separator
  51. + "LibreOffice 3.8",
  52. System.getenv("ProgramFiles(x86)") + File.separator
  53. + "LibreOffice 3.7",
  54. System.getenv("ProgramFiles(x86)") + File.separator
  55. + "LibreOffice 3.6",
  56. System.getenv("ProgramFiles(x86)") + File.separator
  57. + "LibreOffice 3.5",
  58. System.getenv("ProgramFiles(x86)") + File.separator
  59. + "LibreOffice 3.4",
  60. System.getenv("ProgramFiles(x86)") + File.separator
  61. + "LibreOffice 3",
  62. System.getenv("ProgramFiles") + File.separator + "OpenOffice.org 3",
  63. System.getenv("ProgramFiles(x86)") + File.separator
  64. + "OpenOffice.org 3", };
  65. private static final String[] LINUX_OO_PROFILE_PATHS = {
  66. System.getProperty("user.home") + File.separator
  67. + ".openoffice.org/4",
  68. System.getProperty("user.home") + File.separator + ".libreoffice/4" };
  69. private static final String[] MAC_OO_PROFILE_PATHS = {
  70. System.getProperty("user.home") + File.separator
  71. + "Library/Application Support/OpenOffice.org/4",
  72. System.getProperty("user.home") + File.separator
  73. + "Library/Application Support/LibreOffice.org/4" };
  74. private static final String[] WINDOWS_OO_PROFILE_PATHS = {
  75. System.getenv("APPDATA") + File.separator + "LibreOffice.org/3",
  76. System.getenv("APPDATA") + File.separator + "OpenOffice.org/3" };
  77. private static String officeProfileDir = null;
  78. private static String officeHome = null;
  79. private PlatformUtils() {
  80. throw new AssertionError("utility class must not be instantiated");
  81. }
  82. public static boolean isLinux() {
  83. return OS_NAME.startsWith(LINUX);
  84. }
  85. public static boolean isMac() {
  86. return OS_NAME.startsWith(MAC);
  87. }
  88. public static boolean isWindows() {
  89. return OS_NAME.startsWith(WINDOWS);
  90. }
  91. /**
  92. * Search for OpenOffice or LibreOffice on default paths.
  93. *
  94. * @return path to Office home or an empty String if not found.
  95. */
  96. public static String findOfficeHome() {
  97. if (officeHome == null) {
  98. String[] homeList = new String[0];
  99. if (isLinux()) {
  100. homeList = LINUX_OO_HOME_PATHS;
  101. } else if (isMac()) {
  102. homeList = MAC_OO_HOME_PATHS;
  103. } else if (isWindows()) {
  104. homeList = WINDOWS_OO_HOME_PATHS;
  105. }
  106. officeHome = searchExistingfile(Arrays.asList(homeList));
  107. }
  108. return officeHome;
  109. }
  110. /**
  111. * Search for OpenOffice or LibreOffice user profile on default paths.
  112. *
  113. * @return path to Office profile or an empty String if not found.
  114. */
  115. public static String findOfficeProfileDir() {
  116. if (officeProfileDir == null) {
  117. String[] profileDirList = new String[0];
  118. if (isLinux()) {
  119. profileDirList = LINUX_OO_PROFILE_PATHS;
  120. } else if (isMac()) {
  121. profileDirList = MAC_OO_PROFILE_PATHS;
  122. } else if (isWindows()) {
  123. profileDirList = WINDOWS_OO_PROFILE_PATHS;
  124. }
  125. officeProfileDir = searchExistingfile(Arrays.asList(profileDirList));
  126. }
  127. return officeProfileDir;
  128. }
  129. protected static String searchExistingfile(List<String> pathList) {
  130. for (String path : pathList) {
  131. if (new File(path).exists()) {
  132. logger.info("Jod will be using " + path);
  133. return path;
  134. }
  135. }
  136. return "";
  137. }
  138. }