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.

Helper.java 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package com.rthoni.intellij.codefromds.business;
  2. import com.intellij.database.psi.DbDataSource;
  3. import com.intellij.database.psi.DbPsiFacade;
  4. import com.intellij.openapi.project.Project;
  5. import com.intellij.openapi.project.ProjectManager;
  6. import org.json.JSONArray;
  7. import org.json.JSONObject;
  8. import java.io.IOException;
  9. import java.nio.charset.StandardCharsets;
  10. import java.nio.file.Files;
  11. import java.nio.file.Path;
  12. import java.nio.file.Paths;
  13. import java.util.Arrays;
  14. import java.util.Collection;
  15. import java.util.List;
  16. import java.util.Optional;
  17. import java.util.stream.Collectors;
  18. /**
  19. * Created by robin on 11/15/16.
  20. */
  21. public abstract class Helper {
  22. public static List<DbDataSource> getDataSources()
  23. {
  24. ProjectManager pm = ProjectManager.getInstance();
  25. Project[] projects = pm.getOpenProjects();
  26. return Arrays.stream(projects).map(project -> DbPsiFacade.getInstance(project).getDataSources())
  27. .flatMap(Collection::stream).collect(Collectors.toList());
  28. }
  29. public static DbDataSource findDataSource(String name)
  30. {
  31. Optional<DbDataSource> opt = getDataSources().stream().filter(d -> d.getName().equals(name)).findFirst();
  32. if (opt.isPresent()) {
  33. return opt.get();
  34. }
  35. return null;
  36. }
  37. public static JSONObject findTableInJson(JSONArray tables, String name)
  38. {
  39. for (int i = 0; i < tables.length(); ++i) {
  40. if (tables.getJSONObject(i).getString("table").equals(name)) {
  41. return tables.getJSONObject(i);
  42. }
  43. }
  44. return null;
  45. }
  46. public static JSONObject findColumnInJson(JSONArray tables, String name)
  47. {
  48. for (int i = 0; i < tables.length(); ++i) {
  49. if (tables.getJSONObject(i).getString("column").equals(name)) {
  50. return tables.getJSONObject(i);
  51. }
  52. }
  53. return null;
  54. }
  55. public static String readFile(String path) throws IOException {
  56. String data = Files.readAllLines(Paths.get(path), StandardCharsets.UTF_8)
  57. .stream().reduce("", (s1, s2) -> s1 + s2 + "\n");
  58. if (data.startsWith("\uFEFF")) {
  59. return data.substring(1);
  60. }
  61. return data;
  62. }
  63. public static String getJsonString(JSONObject obj, String key, String defaultValue)
  64. {
  65. try {
  66. return obj.getString(key);
  67. }
  68. catch (Exception ignored)
  69. {
  70. return defaultValue;
  71. }
  72. }
  73. public static String getJsonString(JSONObject obj, String key)
  74. {
  75. return getJsonString(obj, key, "");
  76. }
  77. public static String getRelativePath(String base, String absolute)
  78. {
  79. if (absolute == null || absolute.isEmpty()) {
  80. return "";
  81. }
  82. Path basePath = Paths.get(base);
  83. Path absolutePath = Paths.get(absolute);
  84. return basePath.relativize(absolutePath).toString();
  85. }
  86. public static String getRelativePath(Project project, String absolute)
  87. {
  88. return getRelativePath(project.getBasePath(), absolute);
  89. }
  90. public static String getAbsolutePath(String base, String relative)
  91. {
  92. return Paths.get(base, relative).toString();
  93. }
  94. public static String getAbsolutePath(Project project, String relative)
  95. {
  96. return getAbsolutePath(project.getBasePath(), relative);
  97. }
  98. public static String pluralize(String str)
  99. {
  100. if (str.endsWith("y")) {
  101. return str.substring(1, str.length() - 1) + "ies";
  102. }
  103. return str + "s";
  104. }
  105. }