Debian Codegraph @

bundles/org.eclipse.equinox.p2.metadata/src/org/eclipse/equinox/internal/p2/metadata/License.java

raw
1 /*******************************************************************************
2 * Copyright (c) 2008, 2017 Genuitec, LLC and others.
3 *
4 * This
5 * program and the accompanying materials are made available under the terms of
6 * the Eclipse Public License 2.0 which accompanies this distribution, and is
7 * available at
8 * https://www.eclipse.org/legal/epl-2.0/
9 *
10 * SPDX-License-Identifier: EPL-2.0
11 *
12 * Contributors:
13 * Genuitec, LLC - initial API and implementation
14 * IBM Corporation - ongoing development
15 * EclipseSource - ongoing development
16 ******************************************************************************/
17 package org.eclipse.equinox.internal.p2.metadata;
18
19 import java.math.BigInteger;
20 import java.net.URI;
21 import java.nio.charset.StandardCharsets;
22 import java.security.MessageDigest;
23 import java.security.NoSuchAlgorithmException;
24 import org.eclipse.equinox.p2.metadata.ILicense;
25
26 /**
27 * The <code>License</code> class represents a software license. A license has required body text
28 * which may be the full text or an annotation. An optional URL field can be specified
29 * which links to full text. Licenses can be easily compared using their digests.
30 */
31 public class License implements ILicense {
32 /**
33 * The <code>body</code> contains the descriptive text for the license. This may
34 * be a summary for a full license specified in a URL.
35 */
36 private final String body;
37
38 /**
39 * The <code>location</code> is the URL of the license.
40 */
41 private URI location;
42
43 /**
44 * The <code>digest</code> is the cached message digest of the normalized body
45 */
46 private String digest;
47
48 /**
49 * Creates a new license object which is identified by users using the <code>body</code> field.
50 * The body should contain either the full text of the license or an summary for a license
51 * fully specified in the given location.
52 *
53 * @param location the location of a document containing the full license, or <code>null</code>
54 * @param body the license body, cannot be <code>null</code>
55 * @throws IllegalArgumentException when the <code>body</code> is <code>null</code>
56 */
57 public License(URI location, String body, String uuid) {
58 if (body == null)
59 throw new IllegalArgumentException("body cannot be null"); //$NON-NLS-1$
60 this.body = body;
61 this.location = location;
62 this.digest = uuid;
63 }
64
65 /**
66 * Returns the location of a document containing the full license.
67 *
68 * @return the location of the license document, or <code>null</code>
69 */
70 @Override
71 public URI getLocation() {
72 return location;
73 }
74
75 /**
76 * Returns the license body.
77 * @return the license body, never <code>null</code>
78 */
79 @Override
80 public String getBody() {
81 return body;
82 }
83
84 /**
85 * Returns the message digest of the license body. The digest is calculated on a normalized
86 * version of the license where all whitespace has been reduced to one space.
87 * @return the message digest as a <code>BigInteger</code>, never <code>null</code>
88 */
89 @Override
90 public synchronized String getUUID() {
91 if (digest == null)
92 digest = calculateLicenseDigest().toString(16);
93
94 return digest;
95 }
96
97 @Override
98 public boolean equals(Object obj) {
99 if (obj == this)
100 return true;
101 if (obj == null)
102 return false;
103 if (obj instanceof ILicense) {
104 ILicense other = (ILicense) obj;
105 if (other.getUUID().equals(getUUID()))
106 return true;
107 }
108 return false;
109 }
110
111 @Override
112 public int hashCode() {
113 return getUUID().hashCode();
114 }
115
116 private BigInteger calculateLicenseDigest() {
117 String message = normalize(getBody());
118 try {
119 MessageDigest algorithm = MessageDigest.getInstance("MD5"); //$NON-NLS-1$
120 algorithm.reset();
121 algorithm.update(message.getBytes(StandardCharsets.UTF_8));
122 byte[] digestBytes = algorithm.digest();
123 return new BigInteger(1, digestBytes);
124 } catch (NoSuchAlgorithmException e) {
125 throw new RuntimeException(e);
126 }
127 }
128
129 /**
130 * Replace all sequences of whitespace with a single whitespace character.
131 */
132 private String normalize(String license) {
133 String text = license.trim();
134 StringBuilder result = new StringBuilder();
135 int length = text.length();
136 for (int i = 0; i < length; i++) {
137 char c = text.charAt(i);
138 boolean foundWhitespace = false;
139 while (Character.isWhitespace(c) && i < length) {
140 foundWhitespace = true;
141 c = text.charAt(++i);
142 }
143 if (foundWhitespace)
144 result.append(' ');
145 if (i < length)
146 result.append(c);
147 }
148 return result.toString();
149 }
150 }