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.4KB

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