ConfigManager.java 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. package com.zd.ueditor;
  2. import org.json.JSONArray;
  3. import org.json.JSONObject;
  4. import java.io.*;
  5. import java.util.HashMap;
  6. import java.util.Map;
  7. public class ConfigManager {
  8. private String rootPath;
  9. private String originalPath;
  10. private String contextPath;
  11. private String realPath;
  12. private static String configFileName = "config.json";
  13. private String parentPath;
  14. private JSONObject jsonConfig;
  15. private static String SCRAWL_FILE_NAME = "scrawl";
  16. private static String REMOTE_FILE_NAME = "remote";
  17. private ConfigManager(String rootPath, String contextPath, String uri,String realPath) throws FileNotFoundException, IOException {
  18. this.parentPath = null;
  19. this.jsonConfig = null;
  20. rootPath = rootPath.replace("\\", "/");
  21. this.rootPath = rootPath;
  22. this.realPath = realPath;
  23. this.contextPath = contextPath;
  24. if (contextPath.length() > 0) {
  25. this.originalPath = this.rootPath + uri.substring(contextPath.length());
  26. } else {
  27. this.originalPath = this.rootPath + uri;
  28. }
  29. this.initEnv();
  30. }
  31. public static ConfigManager getInstance(String rootPath, String contextPath, String uri,String realPath) {
  32. try {
  33. return new ConfigManager(rootPath, contextPath, uri,realPath);
  34. } catch (Exception e) {
  35. return null;
  36. }
  37. }
  38. public boolean valid() {
  39. return this.jsonConfig != null;
  40. }
  41. public JSONObject getAllConfig() {
  42. return this.jsonConfig;
  43. }
  44. public Map<String, Object> getConfig(int type) {
  45. Map<String, Object> conf = new HashMap<String, Object>();
  46. String savePath = null;
  47. switch (type) {
  48. case 4: {
  49. conf.put("isBase64", "false");
  50. conf.put("maxSize", this.jsonConfig.getLong("fileMaxSize"));
  51. conf.put("allowFiles", this.getArray("fileAllowFiles"));
  52. conf.put("fieldName", this.jsonConfig.getString("fileFieldName"));
  53. savePath = this.jsonConfig.getString("filePathFormat");
  54. break;
  55. }
  56. case 1: {
  57. conf.put("isBase64", "false");
  58. conf.put("maxSize", this.jsonConfig.getLong("imageMaxSize"));
  59. conf.put("allowFiles", this.getArray("imageAllowFiles"));
  60. conf.put("fieldName", this.jsonConfig.getString("imageFieldName"));
  61. savePath = this.jsonConfig.getString("imagePathFormat");
  62. break;
  63. }
  64. case 3: {
  65. conf.put("maxSize", this.jsonConfig.getLong("videoMaxSize"));
  66. conf.put("allowFiles", this.getArray("videoAllowFiles"));
  67. conf.put("fieldName", this.jsonConfig.getString("videoFieldName"));
  68. savePath = this.jsonConfig.getString("videoPathFormat");
  69. break;
  70. }
  71. case 2: {
  72. conf.put("filename", "scrawl");
  73. conf.put("maxSize", this.jsonConfig.getLong("scrawlMaxSize"));
  74. conf.put("fieldName", this.jsonConfig.getString("scrawlFieldName"));
  75. conf.put("isBase64", "true");
  76. savePath = this.jsonConfig.getString("scrawlPathFormat");
  77. break;
  78. }
  79. case 5: {
  80. conf.put("filename", "remote");
  81. conf.put("filter", this.getArray("catcherLocalDomain"));
  82. conf.put("maxSize", this.jsonConfig.getLong("catcherMaxSize"));
  83. conf.put("allowFiles", this.getArray("catcherAllowFiles"));
  84. conf.put("fieldName", String.valueOf(this.jsonConfig.getString("catcherFieldName")) + "[]");
  85. savePath = this.jsonConfig.getString("catcherPathFormat");
  86. break;
  87. }
  88. case 7: {
  89. conf.put("allowFiles", this.getArray("imageManagerAllowFiles"));
  90. conf.put("dir", this.jsonConfig.getString("imageManagerListPath"));
  91. conf.put("count", this.jsonConfig.getInt("imageManagerListSize"));
  92. break;
  93. }
  94. case 6: {
  95. conf.put("allowFiles", this.getArray("fileManagerAllowFiles"));
  96. conf.put("dir", this.jsonConfig.getString("fileManagerListPath"));
  97. conf.put("count", this.jsonConfig.getInt("fileManagerListSize"));
  98. break;
  99. }
  100. }
  101. conf.put("savePath", savePath);
  102. conf.put("rootPath", this.realPath);
  103. return conf;
  104. }
  105. private void initEnv() throws FileNotFoundException, IOException {
  106. File file = new File(this.originalPath);
  107. if (!file.isAbsolute()) {
  108. file = new File(file.getAbsolutePath());
  109. }
  110. this.parentPath = file.getParent();
  111. String configContent = this.readFile(this.getConfigPath());
  112. try {
  113. JSONObject jsonConfig = new JSONObject(configContent);
  114. this.jsonConfig = jsonConfig;
  115. } catch (Exception e) {
  116. this.jsonConfig = null;
  117. }
  118. }
  119. private String getConfigPath() {
  120. return String.valueOf(this.parentPath) + File.separator + "config.json";
  121. }
  122. private String[] getArray(String key) {
  123. JSONArray jsonArray = this.jsonConfig.getJSONArray(key);
  124. String[] result = new String[jsonArray.length()];
  125. for (int i = 0, len = jsonArray.length(); i < len; ++i) {
  126. result[i] = jsonArray.getString(i);
  127. }
  128. return result;
  129. }
  130. private String readFile(String path) throws IOException {
  131. StringBuilder builder = new StringBuilder();
  132. try {
  133. InputStreamReader reader = new InputStreamReader(new FileInputStream(path), "UTF-8");
  134. BufferedReader bfReader = new BufferedReader(reader);
  135. String tmpContent = null;
  136. while ((tmpContent = bfReader.readLine()) != null) {
  137. builder.append(tmpContent);
  138. }
  139. bfReader.close();
  140. } catch (UnsupportedEncodingException ex) {
  141. }
  142. return this.filter(builder.toString());
  143. }
  144. private String filter(String input) {
  145. return input.replaceAll("/\\*[\\s\\S]*?\\*/", "");
  146. }
  147. }