source: josm/trunk/src/org/openstreetmap/josm/actions/downloadtasks/AbstractDownloadTask.java@ 8379

Last change on this file since 8379 was 8379, checked in by Don-vip, 9 years ago

Consecutively calls to StringBuffer/StringBuilder .append should reuse the target object

  • Property svn:eol-style set to native
File size: 3.2 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.actions.downloadtasks;
3
4import java.util.ArrayList;
5import java.util.List;
6
7import org.openstreetmap.josm.io.XmlWriter;
8
9/**
10 * Common abstract implementation of other download tasks
11 * @since 2322
12 */
13public abstract class AbstractDownloadTask implements DownloadTask {
14 private List<Object> errorMessages;
15 private boolean canceled = false;
16 private boolean failed = false;
17
18 public AbstractDownloadTask() {
19 errorMessages = new ArrayList<>();
20 }
21
22 public boolean isCanceled() {
23 return canceled;
24 }
25
26 public void setCanceled(boolean canceled) {
27 this.canceled = canceled;
28 }
29
30 public boolean isFailed() {
31 return failed;
32 }
33
34 public void setFailed(boolean failed) {
35 this.failed = failed;
36 }
37
38 protected void rememberErrorMessage(String message) {
39 errorMessages.add(message);
40 }
41
42 protected void rememberException(Exception exception) {
43 errorMessages.add(exception);
44 }
45
46 @Override
47 public List<Object> getErrorObjects() {
48 return errorMessages;
49 }
50
51 @Override
52 public String acceptsDocumentationSummary() {
53 StringBuilder buff = new StringBuilder("<tr><td>");
54 buff.append(getTitle())
55 .append(":</td><td>");
56 String[] patterns = getPatterns();
57 if (patterns.length>0) {
58 buff.append("<ul>");
59 for (String pattern: patterns) {
60 buff.append("<li>")
61 .append(XmlWriter.encode(pattern))
62 .append("</li>");
63 }
64 buff.append("</ul>");
65 }
66 buff.append("</td></tr>");
67 return buff.toString();
68 }
69
70 // Can be overridden for more complex checking logic
71 public boolean acceptsUrl(String url) {
72 if (url==null) return false;
73 for (String p: getPatterns()) {
74 if (url.matches(p)) {
75 return true;
76 }
77 }
78 return false;
79 }
80
81 /**
82 * Check / decide if the task is safe for remotecontrol.
83 *
84 * Keep in mind that a potential attacker has full control over the content
85 * of the file that will be downloaded.
86 * If it is possible to run arbitrary code or write to the local file
87 * system, then the task is (obviously) not save for remote execution.
88 *
89 * The default value is false = unsafe. Override in a subclass to
90 * allow running the task via remotecontol.
91 *
92 * @return true if it is safe to download and open any file of the
93 * corresponding format, false otherwise
94 */
95 public boolean isSafeForRemotecontrolRequests() {
96 return false;
97 }
98
99 @Override
100 public boolean acceptsUrl(String url, boolean isRemotecontrol) {
101 if (isRemotecontrol && !isSafeForRemotecontrolRequests()) return false;
102 return acceptsUrl(url);
103 }
104
105 // Default name to keep old plugins compatible
106 @Override
107 public String getTitle() {
108 return getClass().getName();
109 }
110
111 @Override
112 public String toString() {
113 return this.getTitle();
114 }
115
116 // Default pattern to keep old plugins compatible
117 @Override
118 public String[] getPatterns() {
119 return new String[]{};
120 }
121
122}
Note: See TracBrowser for help on using the repository browser.