Debian Codegraph @

bundles/org.eclipse.equinox.p2.updatesite/src/org/eclipse/equinox/internal/p2/updatesite/SiteFeature.java

raw
1 /*******************************************************************************
2 * Copyright (c) 2000, 2017 IBM Corporation and others.
3 *
4 * This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License 2.0
6 * which accompanies this distribution, and is available at
7 * https://www.eclipse.org/legal/epl-2.0/
8 *
9 * SPDX-License-Identifier: EPL-2.0
10 *
11 * Contributors:
12 * IBM Corporation - initial API and implementation
13 * James D Miles (IBM Corp.) - bug 191783, NullPointerException in FeatureDownloader
14 *******************************************************************************/
15 package org.eclipse.equinox.internal.p2.updatesite;
16
17 import java.io.File;
18 import java.net.MalformedURLException;
19 import java.net.URL;
20 import java.util.*;
21
22 /**
23 * A reference to a feature in an update site.xml file.
24 *
25 * Based on org.eclipse.update.core.model.FeatureReferenceModel.
26 */
27 public class SiteFeature {
28
29 private String arch;
30 // performance
31 private URL base;
32 private List<String> categoryNames;
33 private String featureId;
34 private String featureVersion;
35 private String label;
36 private String nl;
37
38 private String os;
39 private String patch;
40 private static final boolean resolved = false;
41 private SiteModel site;
42 private String type;
43 private URL url;
44 private String urlString;
45 private String ws;
46
47 /*
48 * Compares two URL for equality
49 */
50 public static boolean sameURL(URL url1, URL url2) {
51 if (url1 == url2)
52 return true;
53 if (url1 == null ^ url2 == null)
54 return false;
55
56 // check if URL are file: URL as we may
57 // have 2 URL pointing to the same featureReference
58 // but with different representation
59 // (i.e. file:/C;/ and file:C:/)
60 final boolean isFile1 = "file".equalsIgnoreCase(url1.getProtocol());//$NON-NLS-1$
61 final boolean isFile2 = "file".equalsIgnoreCase(url2.getProtocol());//$NON-NLS-1$
62 if (isFile1 && isFile2) {
63 File file1 = new File(url1.getFile());
64 File file2 = new File(url2.getFile());
65 return file1.equals(file2);
66 }
67 // URL1 xor URL2 is a file, return false. (They either both need to be files, or
68 // neither)
69 if (isFile1 ^ isFile2)
70 return false;
71 return getExternalForm(url1).equals(getExternalForm(url2));
72 }
73
74 /**
75 * Gets the external form of this URL. In particular, it trims any white space,
76 * removes a trailing slash and creates a lower case string.
77 */
78 private static String getExternalForm(URL url) {
79 String externalForm = url.toExternalForm();
80 if (externalForm == null)
81 return ""; //$NON-NLS-1$
82 externalForm = externalForm.trim();
83 if (externalForm.endsWith("/")) { //$NON-NLS-1$
84 // Remove the trailing slash
85 externalForm = externalForm.substring(0, externalForm.length() - 1);
86 }
87 return externalForm.toLowerCase();
88
89 }
90
91 /**
92 * Creates an uninitialized feature reference model object.
93 */
94 public SiteFeature() {
95 super();
96 }
97
98 /**
99 * Adds the name of a category this feature belongs to. Throws a runtime
100 * exception if this object is marked read-only.
101 *
102 * @param categoryName category name
103 */
104 public void addCategoryName(String categoryName) {
105 if (this.categoryNames == null)
106 this.categoryNames = new ArrayList<>();
107 if (!this.categoryNames.contains(categoryName))
108 this.categoryNames.add(categoryName);
109 }
110
111 private void delayedResolve() {
112
113 // PERF: delay resolution
114 if (resolved)
115 return;
116
117 // resolve local elements
118 try {
119 url = new URL(base, urlString);
120 } catch (MalformedURLException e) {
121 // UpdateCore.warn("", e); //$NON-NLS-1$
122 }
123 }
124
125 /**
126 * Compares 2 feature reference models for equality
127 *
128 * @param object feature reference model to compare with
129 * @return <code>true</code> if the two models are equal, <code>false</code>
130 * otherwise
131 */
132 @Override
133 public boolean equals(Object object) {
134 if (object == null)
135 return false;
136 if (!(object instanceof SiteFeature))
137 return false;
138 SiteFeature that = (SiteFeature) object;
139 if (this.featureId == null) {
140 if (that.featureId != null)
141 return false;
142 } else if (!this.featureId.equals(that.featureId))
143 return false;
144 if (this.featureVersion == null) {
145 if (that.featureVersion != null)
146 return false;
147 } else if (!this.featureVersion.equals(that.featureVersion))
148 return false;
149 if (this.label == null) {
150 if (that.label != null)
151 return false;
152 } else if (!this.label.equals(that.label))
153 return false;
154 return sameURL(this.getURL(), that.getURL());
155 }
156
157 @Override
158 public int hashCode() {
159 final int prime = 31;
160 int result = 1;
161 result = prime * result + (featureId == null ? 0 : featureId.hashCode());
162 result = prime * result + (featureVersion == null ? 0 : featureVersion.hashCode());
163 if (this.getURL() == null)
164 return result;
165
166 if ("file".equalsIgnoreCase(getURL().getProtocol())) {//$NON-NLS-1$
167 // If the URL is a file, then create the HashCode from the file
168 File f = new File(getURL().getFile());
169 if (f != null)
170 result = prime * result + f.hashCode();
171 } else
172 // Otherwise create it from the External form of the URL (in lower case)
173 result = prime * result + getExternalForm(this.getURL()).hashCode();
174 return result;
175 }
176
177 /**
178 * Returns the names of categories the referenced feature belongs to.
179 *
180 * @return an array of names, or an empty array.
181 */
182 public String[] getCategoryNames() {
183 if (categoryNames == null)
184 return new String[0];
185
186 return categoryNames.toArray(new String[0]);
187 }
188
189 /**
190 * Returns the feature identifier as a string
191 *
192 * @return feature identifier
193 */
194 public String getFeatureIdentifier() {
195 return featureId;
196 }
197
198 /**
199 * Returns the feature version as a string
200 *
201 * @return feature version
202 */
203 public String getFeatureVersion() {
204 return featureVersion;
205 }
206
207 /**
208 * Retrieve the displayable label for the feature reference. If the model object
209 * has been resolved, the label is localized.
210 *
211 * @return displayable label, or <code>null</code>.
212 */
213 public String getLabel() {
214 return label;
215 }
216
217 /**
218 * Retrieve the non-localized displayable label for the feature reference.
219 *
220 * @return non-localized displayable label, or <code>null</code>.
221 */
222 public String getLabelNonLocalized() {
223 return label;
224 }
225
226 /**
227 * Get optional locale specification as a comma-separated string.
228 *
229 * @return the locale specification string, or <code>null</code>.
230 */
231 public String getNL() {
232 return nl;
233 }
234
235 /**
236 * Get optional operating system specification as a comma-separated string.
237 *
238 * @return the operating system specification string, or <code>null</code>.
239 */
240 public String getOS() {
241 return os;
242 }
243
244 /**
245 * Get optional system architecture specification as a comma-separated string.
246 *
247 * @return the system architecture specification string, or <code>null</code>.
248 */
249 public String getOSArch() {
250 return arch;
251 }
252
253 /**
254 * Returns the patch mode.
255 */
256 public String getPatch() {
257 return patch;
258 }
259
260 /**
261 * Returns the site model for the reference.
262 *
263 * @return site model
264 * @since 2.0
265 */
266 public SiteModel getSiteModel() {
267 return site;
268 }
269
270 /**
271 * Returns the referenced feature type.
272 *
273 * @return feature type, or <code>null</code> representing the default feature
274 * type for the site
275 */
276 public String getType() {
277 return type;
278 }
279
280 /**
281 * Returns the resolved URL for the feature reference.
282 *
283 * @return url string
284 */
285 public URL getURL() {
286 delayedResolve();
287 return url;
288 }
289
290 /**
291 * Returns the unresolved URL string for the reference.
292 *
293 * @return url string
294 */
295 public String getURLString() {
296 return urlString;
297 }
298
299 /**
300 * Get optional windowing system specification as a comma-separated string.
301 *
302 * @return the windowing system specification string, or <code>null</code>.
303 */
304 public String getWS() {
305 return ws;
306 }
307
308 /**
309 * Resolve the model object. Any URL strings in the model are resolved relative
310 * to the base URL argument. Any translatable strings in the model that are
311 * specified as translation keys are localized using the supplied resource
312 * bundle.
313 *
314 * @param resolveBase URL
315 * @param bundleURL resource bundle URL
316 * @exception MalformedURLException
317 */
318 public void resolve(URL resolveBase, URL bundleURL) throws MalformedURLException {
319 this.base = resolveBase;
320 }
321
322 /**
323 * Sets the system architecture specification. Throws a runtime exception if
324 * this object is marked read-only.
325 *
326 * @param arch system architecture specification as a comma-separated list
327 */
328 public void setArch(String arch) {
329 this.arch = arch;
330 }
331
332 /**
333 * Sets the names of categories this feature belongs to. Throws a runtime
334 * exception if this object is marked read-only.
335 *
336 * @param categoryNames an array of category names
337 */
338 public void setCategoryNames(String[] categoryNames) {
339 if (categoryNames == null)
340 this.categoryNames = null;
341 else
342 this.categoryNames = new ArrayList<>(Arrays.asList(categoryNames));
343 }
344
345 /**
346 * Sets the feature identifier. Throws a runtime exception if this object is
347 * marked read-only.
348 *
349 * @param featureId feature identifier
350 */
351 public void setFeatureIdentifier(String featureId) {
352 this.featureId = featureId;
353 }
354
355 /**
356 * Sets the feature version. Throws a runtime exception if this object is marked
357 * read-only.
358 *
359 * @param featureVersion feature version
360 */
361 public void setFeatureVersion(String featureVersion) {
362 this.featureVersion = featureVersion;
363 }
364
365 /**
366 * Sets the label.
367 *
368 * @param label The label to set
369 */
370 public void setLabel(String label) {
371 this.label = label;
372 }
373
374 /**
375 * Sets the locale specification. Throws a runtime exception if this object is
376 * marked read-only.
377 *
378 * @param nl locale specification as a comma-separated list
379 */
380 public void setNL(String nl) {
381 this.nl = nl;
382 }
383
384 /**
385 * Sets the operating system specification. Throws a runtime exception if this
386 * object is marked read-only.
387 *
388 * @param os operating system specification as a comma-separated list
389 */
390 public void setOS(String os) {
391 this.os = os;
392 }
393
394 /**
395 * Sets the patch mode.
396 */
397 public void setPatch(String patch) {
398 this.patch = patch;
399 }
400
401 /**
402 * Sets the site for the referenced. Throws a runtime exception if this object
403 * is marked read-only.
404 *
405 * @param site site for the reference
406 */
407 public void setSiteModel(SiteModel site) {
408 this.site = site;
409 }
410
411 /**
412 * Sets the referenced feature type. Throws a runtime exception if this object
413 * is marked read-only.
414 *
415 * @param type referenced feature type
416 */
417 public void setType(String type) {
418 this.type = type;
419 }
420
421 /**
422 * Sets the unresolved URL for the feature reference. Throws a runtime exception
423 * if this object is marked read-only.
424 *
425 * @param urlString unresolved URL string
426 */
427 public void setURLString(String urlString) {
428 this.urlString = urlString;
429 this.url = null;
430 }
431
432 /**
433 * Sets the windowing system specification. Throws a runtime exception if this
434 * object is marked read-only.
435 *
436 * @param ws windowing system specification as a comma-separated list
437 */
438 public void setWS(String ws) {
439 this.ws = ws;
440 }
441
442 /**
443 * @see Object#toString()
444 */
445 @Override
446 public String toString() {
447 StringBuilder buffer = new StringBuilder();
448 if (featureId != null)
449 buffer.append(featureId).append(' ');
450 if (featureVersion != null)
451 buffer.append(featureVersion).append(' ');
452 if (url != null) {
453 buffer.append(" at "); //$NON-NLS-1$
454 buffer.append(url.toExternalForm());
455 }
456 return buffer.toString();
457 }
458
459 }