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

Last change on this file since 8195 was 8195, checked in by simon04, 9 years ago

see #9907 - Make "Open Location" capable of downloading notes

If more than one download task is suitable, ask the user which tasks to perform.

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