1 /*
2 * This file is part of dependency-check-core.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 * Copyright (c) 2015 Jeremy Long. All Rights Reserved.
17 */
18 package org.owasp.dependencycheck.xml.pom;
19
20 import java.util.ArrayList;
21 import java.util.List;
22 import java.util.Properties;
23
24 import org.apache.commons.lang3.text.StrLookup;
25 import org.apache.commons.lang3.text.StrSubstitutor;
26
27 /**
28 * A simple pojo to hold data related to a Maven POM file.
29 *
30 * @author jeremy
31 */
32 public class Model {
33
34 /**
35 * The name of the project.
36 */
37 private String name;
38
39 /**
40 * Get the value of name.
41 *
42 * @return the value of name
43 */
44 public String getName() {
45 return name;
46 }
47
48 /**
49 * Set the value of name.
50 *
51 * @param name new value of name
52 */
53 public void setName(String name) {
54 this.name = name;
55 }
56
57 /**
58 * The organization name.
59 */
60 private String organization;
61
62 /**
63 * Get the value of organization.
64 *
65 * @return the value of organization
66 */
67 public String getOrganization() {
68 return organization;
69 }
70
71 /**
72 * Set the value of organization.
73 *
74 * @param organization new value of organization
75 */
76 public void setOrganization(String organization) {
77 this.organization = organization;
78 }
79
80 /**
81 * The description.
82 */
83 private String description;
84
85 /**
86 * Get the value of description.
87 *
88 * @return the value of description
89 */
90 public String getDescription() {
91 return description;
92 }
93
94 /**
95 * Set the value of description.
96 *
97 * @param description new value of description
98 */
99 public void setDescription(String description) {
100 this.description = description;
101 }
102
103 /**
104 * The group id.
105 */
106 private String groupId;
107
108 /**
109 * Get the value of groupId.
110 *
111 * @return the value of groupId
112 */
113 public String getGroupId() {
114 return groupId;
115 }
116
117 /**
118 * Set the value of groupId.
119 *
120 * @param groupId new value of groupId
121 */
122 public void setGroupId(String groupId) {
123 this.groupId = groupId;
124 }
125
126 /**
127 * The artifact id.
128 */
129 private String artifactId;
130
131 /**
132 * Get the value of artifactId.
133 *
134 * @return the value of artifactId
135 */
136 public String getArtifactId() {
137 return artifactId;
138 }
139
140 /**
141 * Set the value of artifactId.
142 *
143 * @param artifactId new value of artifactId
144 */
145 public void setArtifactId(String artifactId) {
146 this.artifactId = artifactId;
147 }
148
149 /**
150 * The version number.
151 */
152 private String version;
153
154 /**
155 * Get the value of version.
156 *
157 * @return the value of version
158 */
159 public String getVersion() {
160 return version;
161 }
162
163 /**
164 * Set the value of version.
165 *
166 * @param version new value of version
167 */
168 public void setVersion(String version) {
169 this.version = version;
170 }
171
172 /**
173 * The parent group id.
174 */
175 private String parentGroupId;
176
177 /**
178 * Get the value of parentGroupId.
179 *
180 * @return the value of parentGroupId
181 */
182 public String getParentGroupId() {
183 return parentGroupId;
184 }
185
186 /**
187 * Set the value of parentGroupId.
188 *
189 * @param parentGroupId new value of parentGroupId
190 */
191 public void setParentGroupId(String parentGroupId) {
192 this.parentGroupId = parentGroupId;
193 }
194
195 /**
196 * The parent artifact id.
197 */
198 private String parentArtifactId;
199
200 /**
201 * Get the value of parentArtifactId.
202 *
203 * @return the value of parentArtifactId
204 */
205 public String getParentArtifactId() {
206 return parentArtifactId;
207 }
208
209 /**
210 * Set the value of parentArtifactId.
211 *
212 * @param parentArtifactId new value of parentArtifactId
213 */
214 public void setParentArtifactId(String parentArtifactId) {
215 this.parentArtifactId = parentArtifactId;
216 }
217
218 /**
219 * The parent version number.
220 */
221 private String parentVersion;
222
223 /**
224 * Get the value of parentVersion.
225 *
226 * @return the value of parentVersion
227 */
228 public String getParentVersion() {
229 return parentVersion;
230 }
231
232 /**
233 * Set the value of parentVersion.
234 *
235 * @param parentVersion new value of parentVersion
236 */
237 public void setParentVersion(String parentVersion) {
238 this.parentVersion = parentVersion;
239 }
240
241 /**
242 * The list of licenses.
243 */
244 private final List<License> licenses = new ArrayList<License>();
245
246 /**
247 * Returns the list of licenses.
248 *
249 * @return the list of licenses
250 */
251 public List<License> getLicenses() {
252 return licenses;
253 }
254
255 /**
256 * Adds a new license to the list of licenses.
257 *
258 * @param license the license to add
259 */
260 public void addLicense(License license) {
261 licenses.add(license);
262 }
263
264 /**
265 * The project URL.
266 */
267 private String projectURL;
268
269 /**
270 * Get the value of projectURL.
271 *
272 * @return the value of projectURL
273 */
274 public String getProjectURL() {
275 return projectURL;
276 }
277
278 /**
279 * Set the value of projectURL.
280 *
281 * @param projectURL new value of projectURL
282 */
283 public void setProjectURL(String projectURL) {
284 this.projectURL = projectURL;
285 }
286
287 /**
288 * Process the Maven properties file and interpolate all properties.
289 *
290 * @param properties new value of properties
291 */
292 public void processProperties(Properties properties) {
293 this.groupId = interpolateString(this.groupId, properties);
294 this.artifactId = interpolateString(this.artifactId, properties);
295 this.version = interpolateString(this.version, properties);
296 this.description = interpolateString(this.description, properties);
297 for (License l : this.getLicenses()) {
298 l.setName(interpolateString(l.getName(), properties));
299 l.setUrl(interpolateString(l.getUrl(), properties));
300 }
301 this.name = interpolateString(this.name, properties);
302 this.projectURL = interpolateString(this.projectURL, properties);
303 this.organization = interpolateString(this.organization, properties);
304 this.parentGroupId = interpolateString(this.parentGroupId, properties);
305 this.parentArtifactId = interpolateString(this.parentArtifactId, properties);
306 this.parentVersion = interpolateString(this.parentVersion, properties);
307 }
308
309 /**
310 * <p>
311 * A utility function that will interpolate strings based on values given in
312 * the properties file. It will also interpolate the strings contained
313 * within the properties file so that properties can reference other
314 * properties.</p>
315 * <p>
316 * <b>Note:</b> if there is no property found the reference will be removed.
317 * In other words, if the interpolated string will be replaced with an empty
318 * string.
319 * </p>
320 * <p>
321 * Example:</p>
322 * <code>
323 * Properties p = new Properties();
324 * p.setProperty("key", "value");
325 * String s = interpolateString("'${key}' and '${nothing}'", p);
326 * System.out.println(s);
327 * </code>
328 * <p>
329 * Will result in:</p>
330 * <code>
331 * 'value' and ''
332 * </code>
333 *
334 * @param text the string that contains references to properties.
335 * @param properties a collection of properties that may be referenced
336 * within the text.
337 * @return the interpolated text.
338 */
339 public static String interpolateString(String text, Properties properties) {
340 if (null == text || null == properties) {
341 return text;
342 }
343 final StrSubstitutor substitutor = new StrSubstitutor(new PropertyLookup(properties));
344 return substitutor.replace(text);
345 }
346
347 /**
348 * Utility class that can provide values from a Properties object to a
349 * StrSubstitutor.
350 */
351 private static class PropertyLookup extends StrLookup<String> {
352
353 /**
354 * Reference to the properties to lookup.
355 */
356 private final Properties props;
357
358 /**
359 * Constructs a new property lookup.
360 *
361 * @param props the properties to wrap.
362 */
363 PropertyLookup(Properties props) {
364 this.props = props;
365 }
366
367 /**
368 * Looks up the given property.
369 *
370 * @param key the key to the property
371 * @return the value of the property specified by the key
372 */
373 @Override
374 public String lookup(String key) {
375 return props.getProperty(key);
376 }
377 }
378 }