Coverage Report - org.owasp.dependencycheck.xml.pom.Model
 
Classes in this File Line Coverage Branch Coverage Complexity
Model
94%
48/51
83%
5/6
1.167
Model$PropertyLookup
100%
4/4
N/A
1.167
 
 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  23
 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  4
         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  5
         this.name = name;
 55  5
     }
 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  2
         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  2
         this.organization = organization;
 78  2
     }
 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  2
         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  3
         this.description = description;
 101  3
     }
 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  2
         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  4
         this.groupId = groupId;
 124  4
     }
 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  2
         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  4
         this.artifactId = artifactId;
 147  4
     }
 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  2
         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  3
         this.version = version;
 170  3
     }
 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  2
         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  3
         this.parentGroupId = parentGroupId;
 193  3
     }
 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  2
         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  3
         this.parentArtifactId = parentArtifactId;
 216  3
     }
 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  2
         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  3
         this.parentVersion = parentVersion;
 239  3
     }
 240  
 
 241  
     /**
 242  
      * The list of licenses.
 243  
      */
 244  23
     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  5
         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  2
         licenses.add(license);
 262  2
     }
 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  2
         this.groupId = interpolateString(this.groupId, properties);
 271  2
         this.artifactId = interpolateString(this.artifactId, properties);
 272  2
         this.version = interpolateString(this.version, properties);
 273  2
         this.description = interpolateString(this.description, properties);
 274  2
         for (License l : this.getLicenses()) {
 275  0
             l.setName(interpolateString(l.getName(), properties));
 276  0
             l.setUrl(interpolateString(l.getUrl(), properties));
 277  0
         }
 278  2
         this.name = interpolateString(this.name, properties);
 279  2
         this.organization = interpolateString(this.organization, properties);
 280  2
         this.parentGroupId = interpolateString(this.parentGroupId, properties);
 281  2
         this.parentArtifactId = interpolateString(this.parentArtifactId, properties);
 282  2
         this.parentVersion = interpolateString(this.parentVersion, properties);
 283  
 
 284  2
     }
 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  19
         if (null == text || null == properties) {
 314  17
             return text;
 315  
         }
 316  2
         final StrSubstitutor substitutor = new StrSubstitutor(new PropertyLookup(properties));
 317  2
         return substitutor.replace(text);
 318  
     }
 319  
 
 320  
     /**
 321  
      * Utility class that can provide values from a Properties object to a StrSubstitutor.
 322  
      */
 323  23
     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  2
         PropertyLookup(Properties props) {
 336  2
             this.props = props;
 337  2
         }
 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  6
             return props.getProperty(key);
 348  
         }
 349  
     }
 350  
 }