View Javadoc
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      * Process the Maven properties file and interpolate all properties.
266      *
267      * @param properties new value of properties
268      */
269     public void processProperties(Properties properties) {
270         this.groupId = interpolateString(this.groupId, properties);
271         this.artifactId = interpolateString(this.artifactId, properties);
272         this.version = interpolateString(this.version, properties);
273         this.description = interpolateString(this.description, properties);
274         for (License l : this.getLicenses()) {
275             l.setName(interpolateString(l.getName(), properties));
276             l.setUrl(interpolateString(l.getUrl(), properties));
277         }
278         this.name = interpolateString(this.name, properties);
279         this.organization = interpolateString(this.organization, properties);
280         this.parentGroupId = interpolateString(this.parentGroupId, properties);
281         this.parentArtifactId = interpolateString(this.parentArtifactId, properties);
282         this.parentVersion = interpolateString(this.parentVersion, properties);
283 
284     }
285 
286     /**
287      * <p>
288      * A utility function that will interpolate strings based on values given in the properties file. It will also interpolate the
289      * strings contained within the properties file so that properties can reference other properties.</p>
290      * <p>
291      * <b>Note:</b> if there is no property found the reference will be removed. In other words, if the interpolated string will
292      * be replaced with an empty string.
293      * </p>
294      * <p>
295      * Example:</p>
296      * <code>
297      * Properties p = new Properties();
298      * p.setProperty("key", "value");
299      * String s = interpolateString("'${key}' and '${nothing}'", p);
300      * System.out.println(s);
301      * </code>
302      * <p>
303      * Will result in:</p>
304      * <code>
305      * 'value' and ''
306      * </code>
307      *
308      * @param text the string that contains references to properties.
309      * @param properties a collection of properties that may be referenced within the text.
310      * @return the interpolated text.
311      */
312     public static String interpolateString(String text, Properties properties) {
313         if (null == text || null == properties) {
314             return text;
315         }
316         final StrSubstitutor substitutor = new StrSubstitutor(new PropertyLookup(properties));
317         return substitutor.replace(text);
318     }
319 
320     /**
321      * Utility class that can provide values from a Properties object to a StrSubstitutor.
322      */
323     private static class PropertyLookup extends StrLookup {
324 
325         /**
326          * Reference to the properties to lookup.
327          */
328         private final Properties props;
329 
330         /**
331          * Constructs a new property lookup.
332          *
333          * @param props the properties to wrap.
334          */
335         PropertyLookup(Properties props) {
336             this.props = props;
337         }
338 
339         /**
340          * Looks up the given property.
341          *
342          * @param key the key to the property
343          * @return the value of the property specified by the key
344          */
345         @Override
346         public String lookup(String key) {
347             return props.getProperty(key);
348         }
349     }
350 }