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

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

fix #9778, fix #9806 - access OSM API and JOSM website in HTTPS by default + other HTTPS links where applicable + update CONTRIBUTION

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