source: josm/trunk/src/org/openstreetmap/josm/data/preferences/sources/SourceEntry.java@ 13543

Last change on this file since 13543 was 12825, checked in by Don-vip, 7 years ago

see #15229 - see #15182 - make FileWatcher generic so it has no more dependence on MapCSS

  • Property svn:eol-style set to native
File size: 6.8 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.preferences.sources;
3
4import java.io.File;
5import java.util.Objects;
6import java.util.regex.Matcher;
7import java.util.regex.Pattern;
8
9import org.openstreetmap.josm.tools.Logging;
10import org.openstreetmap.josm.tools.Utils;
11
12/**
13 * A source entry primarily used to save the user's selection of mappaint styles,
14 * but also for preset sources or validator rules.
15 * @since 12649 (moved from gui.preferences package)
16 * @since 3796
17 */
18public class SourceEntry {
19
20 /**
21 * The type of source entry.
22 * @since 12825
23 **/
24 public final SourceType type;
25
26 /**
27 * A URL can be anything that CachedFile understands, i.e.
28 * a local file, http://, or a file from the current jar
29 */
30 public String url;
31
32 /**
33 * Indicates, that {@link #url} is a zip file and the resource is
34 * inside the zip file.
35 */
36 public boolean isZip;
37
38 /**
39 * If {@link #isZip} is true, denotes the path inside the zip file.
40 */
41 public String zipEntryPath;
42
43 /**
44 * Name is used as a namespace for color preferences and (currently) only
45 * one file with a name can be loaded at a time. Additional styles must
46 * either have the same name as the main style or no name at all.
47 * If no name is provided, it will be set to the default value "standard".
48 * The name can also be given in the xml file as attribute for the rules tag.
49 * (This overrides the name given in the preferences, otherwise both
50 * methods are equivalent.)
51 */
52 public String name;
53
54 /**
55 * A title that can be used as menu entry.
56 */
57 public String title;
58
59 /**
60 * active is a boolean flag that can be used to turn the source on or off at runtime.
61 */
62 public boolean active;
63
64 /**
65 * Constructs a new {@code SourceEntry}.
66 * @param type type of source entry
67 * @param url URL that {@link org.openstreetmap.josm.io.CachedFile} understands
68 * @param isZip if url is a zip file and the resource is inside the zip file
69 * @param zipEntryPath If {@code isZip} is {@code true}, denotes the path inside the zip file
70 * @param name Source name
71 * @param title title that can be used as menu entry
72 * @param active boolean flag that can be used to turn the source on or off at runtime
73 * @see #url
74 * @see #isZip
75 * @see #zipEntryPath
76 * @see #name
77 * @see #title
78 * @see #active
79 * @since 12825
80 */
81 public SourceEntry(SourceType type, String url, boolean isZip, String zipEntryPath, String name, String title, boolean active) {
82 this.type = type;
83 this.url = url;
84 this.isZip = isZip;
85 this.zipEntryPath = "".equals(zipEntryPath) ? null : zipEntryPath;
86 this.name = "".equals(name) ? null : name;
87 this.title = "".equals(title) ? null : title;
88 this.active = active;
89 }
90
91 /**
92 * Constructs a new {@code SourceEntry}.
93 * @param type type of source entry
94 * @param url URL that {@link org.openstreetmap.josm.io.CachedFile} understands
95 * @param name Source name
96 * @param title title that can be used as menu entry
97 * @param active boolean flag that can be used to turn the source on or off at runtime
98 * @see #url
99 * @see #name
100 * @see #title
101 * @see #active
102 * @since 12825
103 */
104 public SourceEntry(SourceType type, String url, String name, String title, boolean active) {
105 this(type, url, false, null, name, title, active);
106 }
107
108 /**
109 * Constructs a new {@code SourceEntry}.
110 * @param e existing source entry to copy
111 */
112 public SourceEntry(SourceEntry e) {
113 this.type = e.type;
114 this.url = e.url;
115 this.isZip = e.isZip;
116 this.zipEntryPath = e.zipEntryPath;
117 this.name = e.name;
118 this.title = e.title;
119 this.active = e.active;
120 }
121
122 @Override
123 public boolean equals(Object obj) {
124 if (this == obj) return true;
125 if (obj == null || getClass() != obj.getClass()) return false;
126 SourceEntry that = (SourceEntry) obj;
127 return isZip == that.isZip &&
128 active == that.active &&
129 Objects.equals(url, that.url) &&
130 Objects.equals(zipEntryPath, that.zipEntryPath) &&
131 Objects.equals(name, that.name) &&
132 Objects.equals(title, that.title);
133 }
134
135 @Override
136 public int hashCode() {
137 return Objects.hash(url, isZip, zipEntryPath, name, title, active);
138 }
139
140 @Override
141 public String toString() {
142 return title != null ? title : url;
143 }
144
145 /**
146 * String to show in menus and error messages.
147 * @return Usually the shortdescription, but can be the file name
148 * if no shortdescription is available.
149 */
150 public String getDisplayString() {
151 if (title != null)
152 return title;
153 else
154 return getFileNamePart();
155 }
156
157 /**
158 * Extracts file part from url, e.g.:
159 * <code>http://www.test.com/file.xml?format=text --&gt; file.xml</code>
160 * @return The filename part of the URL
161 */
162 public String getFileNamePart() {
163 Pattern p = Pattern.compile("([^/\\\\]*?)([?].*)?$");
164 Matcher m = p.matcher(url);
165 if (m.find()) {
166 return m.group(1);
167 } else {
168 Logging.warn("Unexpected URL format: "+url);
169 return url;
170 }
171 }
172
173 /**
174 * the name / identifier that should be used to save custom color values
175 * and similar stuff to the preference file
176 * @return the identifier; never null. Usually the result is "standard"
177 */
178 public String getPrefName() {
179 return name == null ? "standard" : name;
180 }
181
182 /**
183 * Determines if this source denotes a file on a local filesystem.
184 * @return {@code true} if the source is a local file
185 */
186 public boolean isLocal() {
187 return Utils.isLocalUrl(url);
188 }
189
190 /**
191 * Return the source directory, only for local files.
192 * @return The source directory, or {@code null} if this file isn't local, or does not have a parent
193 * @since 7276
194 */
195 public File getLocalSourceDir() {
196 if (!isLocal())
197 return null;
198 return new File(url).getParentFile();
199 }
200
201 /**
202 * Returns the parent directory of the resource inside the zip file.
203 *
204 * @return the parent directory of the resource inside the zip file,
205 * "." if zipEntryPath is a top level file; null, if zipEntryPath is null
206 */
207 public String getZipEntryDirName() {
208 if (zipEntryPath == null) return null;
209 File file = new File(zipEntryPath);
210 File dir = file.getParentFile();
211 if (dir == null) return ".";
212 String path = dir.getPath();
213 if (!"/".equals(File.separator)) {
214 path = path.replace(File.separator, "/");
215 }
216 return path;
217 }
218}
Note: See TracBrowser for help on using the repository browser.