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

Last change on this file since 11063 was 10305, checked in by Don-vip, 8 years ago

sonar - various code cleanup fixes

  • Property svn:eol-style set to native
File size: 4.4 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 * @param <T> The downloaded data type
12 * @since 2322
13 */
14public abstract class AbstractDownloadTask<T> implements DownloadTask {
15 private final List<Object> errorMessages;
16 private boolean canceled;
17 private boolean failed;
18 protected T downloadedData;
19
20 /**
21 * Constructs a new {@code AbstractDownloadTask}.
22 */
23 public AbstractDownloadTask() {
24 errorMessages = new ArrayList<>();
25 }
26
27 /**
28 * Determines if the download task has been canceled.
29 * @return {@code true} if the download task has been canceled
30 */
31 public boolean isCanceled() {
32 return canceled;
33 }
34
35 /**
36 * Marks this download task as canceled.
37 * @param canceled {@code true} to mark this download task as canceled
38 */
39 public void setCanceled(boolean canceled) {
40 this.canceled = canceled;
41 }
42
43 /**
44 * Determines if the download task has failed.
45 * @return {@code true} if the download task has failed
46 */
47 public boolean isFailed() {
48 return failed;
49 }
50
51 /**
52 * Marks this download task as failed.
53 * @param failed {@code true} to mark this download task as failed
54 */
55 public void setFailed(boolean failed) {
56 this.failed = failed;
57 }
58
59 protected final void rememberErrorMessage(String message) {
60 errorMessages.add(message);
61 }
62
63 protected final void rememberException(Exception exception) {
64 errorMessages.add(exception);
65 }
66
67 protected final void rememberDownloadedData(T data) {
68 this.downloadedData = data;
69 }
70
71 /**
72 * Replies the downloaded data.
73 * @return The downloaded data.
74 */
75 public final T getDownloadedData() {
76 return downloadedData;
77 }
78
79 @Override
80 public List<Object> getErrorObjects() {
81 return errorMessages;
82 }
83
84 @Override
85 public String acceptsDocumentationSummary() {
86 StringBuilder buff = new StringBuilder(128)
87 .append("<tr><td>")
88 .append(getTitle())
89 .append(":</td><td>");
90 String[] patterns = getPatterns();
91 if (patterns.length > 0) {
92 buff.append("<ul>");
93 for (String pattern: patterns) {
94 buff.append("<li>")
95 .append(XmlWriter.encode(pattern))
96 .append("</li>");
97 }
98 buff.append("</ul>");
99 }
100 buff.append("</td></tr>");
101 return buff.toString();
102 }
103
104 /**
105 * Determines if the given URL is accepted by {@link #getPatterns}.
106 * Can be overridden for more complex checking logic.
107 * @param url URL to donwload
108 * @return {@code true} if this URL is accepted
109 */
110 public boolean acceptsUrl(String url) {
111 if (url == null)
112 return false;
113 for (String p: getPatterns()) {
114 if (url.matches(p)) {
115 return true;
116 }
117 }
118 return false;
119 }
120
121 /**
122 * Check / decide if the task is safe for remotecontrol.
123 *
124 * Keep in mind that a potential attacker has full control over the content
125 * of the file that will be downloaded.
126 * If it is possible to run arbitrary code or write to the local file
127 * system, then the task is (obviously) not save for remote execution.
128 *
129 * The default value is false = unsafe. Override in a subclass to
130 * allow running the task via remotecontol.
131 *
132 * @return true if it is safe to download and open any file of the
133 * corresponding format, false otherwise
134 */
135 public boolean isSafeForRemotecontrolRequests() {
136 return false;
137 }
138
139 @Override
140 public boolean acceptsUrl(String url, boolean isRemotecontrol) {
141 if (isRemotecontrol && !isSafeForRemotecontrolRequests())
142 return false;
143 return acceptsUrl(url);
144 }
145
146 // Default name to keep old plugins compatible
147 @Override
148 public String getTitle() {
149 return getClass().getName();
150 }
151
152 @Override
153 public String toString() {
154 return this.getTitle();
155 }
156
157 // Default pattern to keep old plugins compatible
158 @Override
159 public String[] getPatterns() {
160 return new String[]{};
161 }
162}
Note: See TracBrowser for help on using the repository browser.