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