source: josm/trunk/src/org/openstreetmap/josm/gui/preferences/SourceEntry.java@ 8377

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

performance - remove useless boxing of boolean constants

  • 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.gui.preferences;
3
4import java.io.File;
5import java.util.Objects;
6import java.util.regex.Matcher;
7import java.util.regex.Pattern;
8
9import org.openstreetmap.josm.Main;
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 3796
16 */
17public 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 (obj == null || getClass() != obj.getClass())
112 return false;
113 final SourceEntry other = (SourceEntry) obj;
114 return Objects.equals(other.url, url) &&
115 other.isZip == isZip &&
116 Objects.equals(other.zipEntryPath, zipEntryPath) &&
117 Objects.equals(other.name, name) &&
118 Objects.equals(other.title, title) &&
119 other.active == active;
120 }
121
122 @Override
123 public int hashCode() {
124 int hash = 5;
125 hash = 89 * hash + (this.url != null ? this.url.hashCode() : 0);
126 hash = 89 * hash + (this.isZip ? 1 : 0);
127 hash = 89 * hash + (this.zipEntryPath != null ? this.zipEntryPath.hashCode() : 0);
128 hash = 89 * hash + (this.name != null ? this.name.hashCode() : 0);
129 hash = 89 * hash + (this.title != null ? this.title.hashCode() : 0);
130 hash = 89 * hash + (this.active ? 1 : 0);
131 return hash;
132 }
133
134 @Override
135 public String toString() {
136 return title != null ? title : url;
137 }
138
139 /**
140 * String to show in menus and error messages.
141 * @return Usually the shortdescription, but can be the file name
142 * if no shortdescription is available.
143 */
144 public String getDisplayString() {
145 if (title != null)
146 return title;
147 else
148 return getFileNamePart();
149 }
150
151 /**
152 * Extracts file part from url, e.g.:
153 * <code>http://www.test.com/file.xml?format=text --&gt; file.xml</code>
154 * @return The filename part of the URL
155 */
156 public String getFileNamePart() {
157 Pattern p = Pattern.compile("([^/\\\\]*?)([?].*)?$");
158 Matcher m = p.matcher(url);
159 if (m.find()) {
160 return m.group(1);
161 } else {
162 Main.warn("Unexpected URL format: "+url);
163 return url;
164 }
165 }
166
167 /**
168 * the name / identifier that should be used to save custom color values
169 * and similar stuff to the preference file
170 * @return the identifier; never null. Usually the result is "standard"
171 */
172 public String getPrefName() {
173 return name == null ? "standard" : name;
174 }
175
176 /**
177 * Determines if this source denotes a file on a local filesystem.
178 * @return {@code true} if the source is a local file
179 */
180 public boolean isLocal() {
181 return Utils.isLocalUrl(url);
182 }
183
184 /**
185 * Return the source directory, only for local files.
186 * @return The source directory, or {@code null} if this file isn't local, or does not have a parent
187 * @since 7276
188 */
189 public File getLocalSourceDir() {
190 if (!isLocal())
191 return null;
192 return new File(url).getParentFile();
193 }
194
195 /**
196 * Returns the parent directory of the resource inside the zip file.
197 *
198 * @return the parent directory of the resource inside the zip file,
199 * "." if zipEntryPath is a top level file; null, if zipEntryPath is null
200 */
201 public String getZipEntryDirName() {
202 if (zipEntryPath == null) return null;
203 File file = new File(zipEntryPath);
204 File dir = file.getParentFile();
205 if (dir == null) return ".";
206 String path = dir.getPath();
207 if (!"/".equals(File.separator)) {
208 path = path.replace(File.separator, "/");
209 }
210 return path;
211 }
212}
Note: See TracBrowser for help on using the repository browser.