PageRenderTime 25ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/plugins/tasks/tasks-core/src/com/intellij/tasks/generic/SelectorBasedResponseHandler.java

http://github.com/JetBrains/intellij-community
Java | 269 lines | 222 code | 29 blank | 18 comment | 48 complexity | 90cb625756db92a707dd221bc56a8f8d MD5 | raw file
Possible License(s): BSD-3-Clause, Apache-2.0, MPL-2.0-no-copyleft-exception, MIT, EPL-1.0, AGPL-1.0
  1. /*
  2. * Copyright 2000-2017 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
  3. */
  4. package com.intellij.tasks.generic;
  5. import com.intellij.openapi.diagnostic.Logger;
  6. import com.intellij.openapi.fileTypes.FileType;
  7. import com.intellij.openapi.project.Project;
  8. import com.intellij.openapi.util.text.StringUtil;
  9. import com.intellij.tasks.Task;
  10. import com.intellij.tasks.impl.TaskUtil;
  11. import com.intellij.ui.components.JBScrollPane;
  12. import com.intellij.util.containers.ContainerUtil;
  13. import com.intellij.util.xmlb.annotations.XCollection;
  14. import org.jetbrains.annotations.NonNls;
  15. import org.jetbrains.annotations.NotNull;
  16. import org.jetbrains.annotations.Nullable;
  17. import javax.swing.*;
  18. import java.util.ArrayList;
  19. import java.util.Date;
  20. import java.util.LinkedHashMap;
  21. import java.util.List;
  22. /**
  23. * @author Mikhail Golubev
  24. */
  25. public abstract class SelectorBasedResponseHandler extends ResponseHandler {
  26. private static final Logger LOG = Logger.getInstance(SelectorBasedResponseHandler.class);
  27. // Supported selector names
  28. @NonNls protected static final String TASKS = "tasks";
  29. @NonNls protected static final String SUMMARY = "summary";
  30. @NonNls protected static final String DESCRIPTION = "description";
  31. @NonNls protected static final String ISSUE_URL = "issueUrl";
  32. @NonNls protected static final String CLOSED = "closed";
  33. @NonNls protected static final String UPDATED = "updated";
  34. @NonNls protected static final String CREATED = "created";
  35. @NonNls protected static final String SINGLE_TASK_ID = "singleTask-id";
  36. @NonNls protected static final String SINGLE_TASK_SUMMARY = "singleTask-summary";
  37. @NonNls protected static final String SINGLE_TASK_DESCRIPTION = "singleTask-description";
  38. @NonNls protected static final String SINGLE_TASK_ISSUE_URL = "singleTask-issueUrl";
  39. @NonNls protected static final String SINGLE_TASK_CLOSED = "singleTask-closed";
  40. @NonNls protected static final String SINGLE_TASK_UPDATED = "singleTask-updated";
  41. @NonNls protected static final String SINGLE_TASK_CREATED = "singleTask-created";
  42. @NonNls protected static final String ID = "id";
  43. protected LinkedHashMap<String, Selector> mySelectors = new LinkedHashMap<>();
  44. /**
  45. * Serialization constructor
  46. */
  47. @SuppressWarnings("UnusedDeclaration")
  48. protected SelectorBasedResponseHandler() {
  49. // empty
  50. }
  51. protected SelectorBasedResponseHandler(GenericRepository repository) {
  52. super(repository);
  53. // standard selectors
  54. setSelectors(ContainerUtil.newArrayList(
  55. // matched against list of tasks at whole downloaded from "taskListUrl"
  56. new Selector(TASKS),
  57. // matched against single tasks extracted from the list downloaded from "taskListUrl"
  58. new Selector(ID),
  59. new Selector(SUMMARY),
  60. new Selector(DESCRIPTION),
  61. new Selector(UPDATED),
  62. new Selector(CREATED),
  63. new Selector(CLOSED),
  64. new Selector(ISSUE_URL),
  65. // matched against single task downloaded from "singleTaskUrl"
  66. new Selector(SINGLE_TASK_ID),
  67. new Selector(SINGLE_TASK_SUMMARY),
  68. new Selector(SINGLE_TASK_DESCRIPTION),
  69. new Selector(SINGLE_TASK_UPDATED),
  70. new Selector(SINGLE_TASK_CREATED),
  71. new Selector(SINGLE_TASK_CLOSED),
  72. new Selector(SINGLE_TASK_ISSUE_URL)
  73. ));
  74. }
  75. @XCollection(propertyElementName = "selectors")
  76. @NotNull
  77. public List<Selector> getSelectors() {
  78. return new ArrayList<>(mySelectors.values());
  79. }
  80. public void setSelectors(@NotNull List<Selector> selectors) {
  81. mySelectors.clear();
  82. for (Selector selector : selectors) {
  83. mySelectors.put(selector.getName(), selector);
  84. }
  85. }
  86. /**
  87. * Only predefined selectors should be accessed.
  88. */
  89. @NotNull
  90. protected Selector getSelector(@NotNull String name) {
  91. return mySelectors.get(name);
  92. }
  93. @NotNull
  94. protected String getSelectorPath(@NotNull String name) {
  95. Selector s = getSelector(name);
  96. return s.getPath();
  97. }
  98. @NotNull
  99. @Override
  100. public JComponent getConfigurationComponent(@NotNull Project project) {
  101. FileType fileType = getResponseType().getSelectorFileType();
  102. HighlightedSelectorsTable table = new HighlightedSelectorsTable(fileType, project, getSelectors());
  103. return new JBScrollPane(table);
  104. }
  105. @Override
  106. public SelectorBasedResponseHandler clone() {
  107. SelectorBasedResponseHandler clone = (SelectorBasedResponseHandler)super.clone();
  108. clone.mySelectors = new LinkedHashMap<>(mySelectors.size());
  109. for (Selector selector : mySelectors.values()) {
  110. clone.mySelectors.put(selector.getName(), selector.clone());
  111. }
  112. return clone;
  113. }
  114. @Override
  115. public boolean isConfigured() {
  116. Selector idSelector = getSelector(ID);
  117. if (StringUtil.isEmpty(idSelector.getPath())) return false;
  118. Selector summarySelector = getSelector(SUMMARY);
  119. if (StringUtil.isEmpty(summarySelector.getPath()) && !myRepository.getDownloadTasksInSeparateRequests()) return false;
  120. return true;
  121. }
  122. @Override
  123. public boolean equals(Object o) {
  124. if (this == o) return true;
  125. if (!(o instanceof SelectorBasedResponseHandler)) return false;
  126. SelectorBasedResponseHandler handler = (SelectorBasedResponseHandler)o;
  127. if (!mySelectors.equals(handler.mySelectors)) return false;
  128. return true;
  129. }
  130. @Override
  131. public int hashCode() {
  132. return mySelectors.hashCode();
  133. }
  134. @Override
  135. public final Task @NotNull [] parseIssues(@NotNull String response, int max) throws Exception {
  136. if (StringUtil.isEmpty(getSelectorPath(TASKS)) ||
  137. StringUtil.isEmpty(getSelectorPath(ID)) ||
  138. (StringUtil.isEmpty(getSelectorPath(SUMMARY)) && !myRepository.getDownloadTasksInSeparateRequests())) {
  139. throw new Exception("Selectors 'tasks', 'id' and 'summary' are mandatory");
  140. }
  141. List<Object> tasks = selectTasksList(response, max);
  142. LOG.debug(String.format("Total %d tasks extracted from response", tasks.size()));
  143. List<Task> result = new ArrayList<>(tasks.size());
  144. for (Object context : tasks) {
  145. String id = selectString(getSelector(ID), context);
  146. GenericTask task;
  147. if (myRepository.getDownloadTasksInSeparateRequests()) {
  148. task = new GenericTask(id, "", myRepository);
  149. }
  150. else {
  151. String summary = selectString(getSelector(SUMMARY), context);
  152. assert id != null && summary != null;
  153. task = new GenericTask(id, summary, myRepository);
  154. String description = selectString(getSelector(DESCRIPTION), context);
  155. if (description != null) {
  156. task.setDescription(description);
  157. }
  158. String issueUrl = selectString(getSelector(ISSUE_URL), context);
  159. if (issueUrl != null) {
  160. task.setIssueUrl(issueUrl);
  161. }
  162. Boolean closed = selectBoolean(getSelector(CLOSED), context);
  163. if (closed != null) {
  164. task.setClosed(closed);
  165. }
  166. Date updated = selectDate(getSelector(UPDATED), context);
  167. if (updated != null) {
  168. task.setUpdated(updated);
  169. }
  170. Date created = selectDate(getSelector(CREATED), context);
  171. if (created != null) {
  172. task.setCreated(created);
  173. }
  174. }
  175. result.add(task);
  176. }
  177. return result.toArray(Task.EMPTY_ARRAY);
  178. }
  179. @Nullable
  180. private Date selectDate(@NotNull Selector selector, @NotNull Object context) throws Exception {
  181. String s = selectString(selector, context);
  182. if (s == null) {
  183. return null;
  184. }
  185. return TaskUtil.parseDate(s);
  186. }
  187. @Nullable
  188. protected Boolean selectBoolean(@NotNull Selector selector, @NotNull Object context) throws Exception {
  189. String s = selectString(selector, context);
  190. if (s == null) {
  191. return null;
  192. }
  193. s = StringUtil.toLowerCase(s.trim());
  194. if (s.equals("true")) {
  195. return true;
  196. }
  197. else if (s.equals("false")) {
  198. return false;
  199. }
  200. throw new Exception(
  201. String.format("Expression '%s' should match boolean value. Got '%s' instead", selector.getName(), s));
  202. }
  203. @NotNull
  204. protected abstract List<Object> selectTasksList(@NotNull String response, int max) throws Exception;
  205. @Nullable
  206. protected abstract String selectString(@NotNull Selector selector, @NotNull Object context) throws Exception;
  207. @Nullable
  208. @Override
  209. public final Task parseIssue(@NotNull String response) throws Exception {
  210. if (StringUtil.isEmpty(getSelectorPath(SINGLE_TASK_ID)) ||
  211. StringUtil.isEmpty(getSelectorPath(SINGLE_TASK_SUMMARY))) {
  212. throw new Exception("Selectors 'singleTask-id' and 'singleTask-summary' are mandatory");
  213. }
  214. String id = selectString(getSelector(SINGLE_TASK_ID), response);
  215. String summary = selectString(getSelector(SINGLE_TASK_SUMMARY), response);
  216. assert id != null && summary != null;
  217. GenericTask task = new GenericTask(id, summary, myRepository);
  218. String description = selectString(getSelector(SINGLE_TASK_DESCRIPTION), response);
  219. if (description != null) {
  220. task.setDescription(description);
  221. }
  222. String issueUrl = selectString(getSelector(SINGLE_TASK_ISSUE_URL), response);
  223. if (issueUrl != null) {
  224. task.setIssueUrl(issueUrl);
  225. }
  226. Boolean closed = selectBoolean(getSelector(SINGLE_TASK_CLOSED), response);
  227. if (closed != null) {
  228. task.setClosed(closed);
  229. }
  230. Date updated = selectDate(getSelector(SINGLE_TASK_UPDATED), response);
  231. if (updated != null) {
  232. task.setUpdated(updated);
  233. }
  234. Date created = selectDate(getSelector(SINGLE_TASK_CREATED), response);
  235. if (created != null) {
  236. task.setCreated(created);
  237. }
  238. return task;
  239. }
  240. }