Debian Codegraph @

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

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