/flexodesktop/model/flexoutils/src/main/java/org/openflexo/toolbox/WindowsCommandRetriever.java

https://github.com/bluepimento/openflexo · Java · 73 lines · 39 code · 5 blank · 29 comment · 5 complexity · 93fb2a3dc6854b18ea8398663dd1463c MD5 · raw file

  1. /*
  2. * (c) Copyright 2010-2011 AgileBirds
  3. *
  4. * This file is part of OpenFlexo.
  5. *
  6. * OpenFlexo is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * OpenFlexo 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
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with OpenFlexo. If not, see <http://www.gnu.org/licenses/>.
  18. *
  19. */
  20. package org.openflexo.toolbox;
  21. import java.io.IOException;
  22. import org.openflexo.toolbox.WinRegistryAccess.ConsoleReader;
  23. /**
  24. * @author gpolet
  25. *
  26. */
  27. public class WindowsCommandRetriever {
  28. /**
  29. *
  30. * @param extension
  31. * the file extension (with or without the preceding '.')
  32. * @return the command to execute for the specified <code>extension</code> or null if there are no associated command
  33. */
  34. public static String commandForExtension(String extension) {
  35. String regKey = "HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\FileExts\\" + extension;
  36. String fileType = WinRegistryAccess.getRegistryValue(regKey, "ProgID", WinRegistryAccess.REG_SZ_TOKEN);
  37. if (fileType == null) {
  38. StringBuilder sb = new StringBuilder("cmd /C assoc ");
  39. sb.append(extension.startsWith(".") ? extension : "." + extension);
  40. ConsoleReader reader;
  41. try {
  42. Process process = Runtime.getRuntime().exec(sb.toString());
  43. reader = new ConsoleReader(process.getInputStream());
  44. reader.start();
  45. process.waitFor();
  46. reader.join();
  47. } catch (IOException e) {
  48. e.printStackTrace();
  49. return null;
  50. } catch (InterruptedException e) {
  51. e.printStackTrace();
  52. return null;
  53. }
  54. String result = reader.getResult();
  55. if (result.indexOf("=") > -1) {
  56. fileType = result.substring(result.indexOf("=") + 1).trim();
  57. }
  58. }
  59. if (fileType == null) {
  60. return null;
  61. }
  62. return getCommandForFileType(fileType);
  63. }
  64. public static String getCommandForFileType(String fileType) {
  65. String path = "HKEY_CLASSES_ROOT\\" + fileType + "\\shell\\open\\command";
  66. return WinRegistryAccess.getRegistryValue(path, null, WinRegistryAccess.REG_SZ_TOKEN);
  67. }
  68. }