| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| Model |
|
| 1.4545454545454546;1.455 |
| 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 | /** | |
| 25 | * A simple pojo to hold data related to a Maven POM file. | |
| 26 | * | |
| 27 | * @author jeremy | |
| 28 | */ | |
| 29 | 184 | public class Model { |
| 30 | ||
| 31 | /** | |
| 32 | * The name of the project. | |
| 33 | */ | |
| 34 | private String name; | |
| 35 | ||
| 36 | /** | |
| 37 | * Get the value of name. | |
| 38 | * | |
| 39 | * @return the value of name | |
| 40 | */ | |
| 41 | public String getName() { | |
| 42 | 32 | return name; |
| 43 | } | |
| 44 | ||
| 45 | /** | |
| 46 | * Set the value of name. | |
| 47 | * | |
| 48 | * @param name new value of name | |
| 49 | */ | |
| 50 | public void setName(String name) { | |
| 51 | 40 | this.name = name; |
| 52 | 40 | } |
| 53 | ||
| 54 | /** | |
| 55 | * The organization name. | |
| 56 | */ | |
| 57 | private String organization; | |
| 58 | ||
| 59 | /** | |
| 60 | * Get the value of organization. | |
| 61 | * | |
| 62 | * @return the value of organization | |
| 63 | */ | |
| 64 | public String getOrganization() { | |
| 65 | 16 | return organization; |
| 66 | } | |
| 67 | ||
| 68 | /** | |
| 69 | * Set the value of organization. | |
| 70 | * | |
| 71 | * @param organization new value of organization | |
| 72 | */ | |
| 73 | public void setOrganization(String organization) { | |
| 74 | 16 | this.organization = organization; |
| 75 | 16 | } |
| 76 | ||
| 77 | /** | |
| 78 | * The description. | |
| 79 | */ | |
| 80 | private String description; | |
| 81 | ||
| 82 | /** | |
| 83 | * Get the value of description. | |
| 84 | * | |
| 85 | * @return the value of description | |
| 86 | */ | |
| 87 | public String getDescription() { | |
| 88 | 16 | return description; |
| 89 | } | |
| 90 | ||
| 91 | /** | |
| 92 | * Set the value of description. | |
| 93 | * | |
| 94 | * @param description new value of description | |
| 95 | */ | |
| 96 | public void setDescription(String description) { | |
| 97 | 24 | this.description = description; |
| 98 | 24 | } |
| 99 | ||
| 100 | /** | |
| 101 | * The group id. | |
| 102 | */ | |
| 103 | private String groupId; | |
| 104 | ||
| 105 | /** | |
| 106 | * Get the value of groupId. | |
| 107 | * | |
| 108 | * @return the value of groupId | |
| 109 | */ | |
| 110 | public String getGroupId() { | |
| 111 | 16 | return groupId; |
| 112 | } | |
| 113 | ||
| 114 | /** | |
| 115 | * Set the value of groupId. | |
| 116 | * | |
| 117 | * @param groupId new value of groupId | |
| 118 | */ | |
| 119 | public void setGroupId(String groupId) { | |
| 120 | 32 | this.groupId = groupId; |
| 121 | 32 | } |
| 122 | ||
| 123 | /** | |
| 124 | * The artifact id. | |
| 125 | */ | |
| 126 | private String artifactId; | |
| 127 | ||
| 128 | /** | |
| 129 | * Get the value of artifactId. | |
| 130 | * | |
| 131 | * @return the value of artifactId | |
| 132 | */ | |
| 133 | public String getArtifactId() { | |
| 134 | 16 | return artifactId; |
| 135 | } | |
| 136 | ||
| 137 | /** | |
| 138 | * Set the value of artifactId. | |
| 139 | * | |
| 140 | * @param artifactId new value of artifactId | |
| 141 | */ | |
| 142 | public void setArtifactId(String artifactId) { | |
| 143 | 32 | this.artifactId = artifactId; |
| 144 | 32 | } |
| 145 | ||
| 146 | /** | |
| 147 | * The version number. | |
| 148 | */ | |
| 149 | private String version; | |
| 150 | ||
| 151 | /** | |
| 152 | * Get the value of version. | |
| 153 | * | |
| 154 | * @return the value of version | |
| 155 | */ | |
| 156 | public String getVersion() { | |
| 157 | 16 | return version; |
| 158 | } | |
| 159 | ||
| 160 | /** | |
| 161 | * Set the value of version. | |
| 162 | * | |
| 163 | * @param version new value of version | |
| 164 | */ | |
| 165 | public void setVersion(String version) { | |
| 166 | 24 | this.version = version; |
| 167 | 24 | } |
| 168 | ||
| 169 | /** | |
| 170 | * The parent group id. | |
| 171 | */ | |
| 172 | private String parentGroupId; | |
| 173 | ||
| 174 | /** | |
| 175 | * Get the value of parentGroupId. | |
| 176 | * | |
| 177 | * @return the value of parentGroupId | |
| 178 | */ | |
| 179 | public String getParentGroupId() { | |
| 180 | 16 | return parentGroupId; |
| 181 | } | |
| 182 | ||
| 183 | /** | |
| 184 | * Set the value of parentGroupId. | |
| 185 | * | |
| 186 | * @param parentGroupId new value of parentGroupId | |
| 187 | */ | |
| 188 | public void setParentGroupId(String parentGroupId) { | |
| 189 | 24 | this.parentGroupId = parentGroupId; |
| 190 | 24 | } |
| 191 | ||
| 192 | /** | |
| 193 | * The parent artifact id. | |
| 194 | */ | |
| 195 | private String parentArtifactId; | |
| 196 | ||
| 197 | /** | |
| 198 | * Get the value of parentArtifactId. | |
| 199 | * | |
| 200 | * @return the value of parentArtifactId | |
| 201 | */ | |
| 202 | public String getParentArtifactId() { | |
| 203 | 16 | return parentArtifactId; |
| 204 | } | |
| 205 | ||
| 206 | /** | |
| 207 | * Set the value of parentArtifactId. | |
| 208 | * | |
| 209 | * @param parentArtifactId new value of parentArtifactId | |
| 210 | */ | |
| 211 | public void setParentArtifactId(String parentArtifactId) { | |
| 212 | 24 | this.parentArtifactId = parentArtifactId; |
| 213 | 24 | } |
| 214 | ||
| 215 | /** | |
| 216 | * The parent version number. | |
| 217 | */ | |
| 218 | private String parentVersion; | |
| 219 | ||
| 220 | /** | |
| 221 | * Get the value of parentVersion. | |
| 222 | * | |
| 223 | * @return the value of parentVersion | |
| 224 | */ | |
| 225 | public String getParentVersion() { | |
| 226 | 16 | return parentVersion; |
| 227 | } | |
| 228 | ||
| 229 | /** | |
| 230 | * Set the value of parentVersion. | |
| 231 | * | |
| 232 | * @param parentVersion new value of parentVersion | |
| 233 | */ | |
| 234 | public void setParentVersion(String parentVersion) { | |
| 235 | 24 | this.parentVersion = parentVersion; |
| 236 | 24 | } |
| 237 | ||
| 238 | /** | |
| 239 | * The list of licenses. | |
| 240 | */ | |
| 241 | 184 | private List<License> licenses = new ArrayList<License>(); |
| 242 | ||
| 243 | /** | |
| 244 | * Returns the list of licenses. | |
| 245 | * | |
| 246 | * @return the list of licenses | |
| 247 | */ | |
| 248 | public List<License> getLicenses() { | |
| 249 | 40 | return licenses; |
| 250 | } | |
| 251 | ||
| 252 | /** | |
| 253 | * Adds a new license to the list of licenses. | |
| 254 | * | |
| 255 | * @param license the license to add | |
| 256 | */ | |
| 257 | public void addLicense(License license) { | |
| 258 | 16 | licenses.add(license); |
| 259 | 16 | } |
| 260 | ||
| 261 | /** | |
| 262 | * Process the Maven properties file and interpolate all properties. | |
| 263 | * | |
| 264 | * @param properties new value of properties | |
| 265 | */ | |
| 266 | public void processProperties(Properties properties) { | |
| 267 | 16 | this.groupId = interpolateString(this.groupId, properties); |
| 268 | 16 | this.artifactId = interpolateString(this.artifactId, properties); |
| 269 | 16 | this.version = interpolateString(this.version, properties); |
| 270 | 16 | this.description = interpolateString(this.description, properties); |
| 271 | 16 | for (License l : this.getLicenses()) { |
| 272 | 0 | l.setName(interpolateString(l.getName(), properties)); |
| 273 | 0 | l.setUrl(interpolateString(l.getUrl(), properties)); |
| 274 | 0 | } |
| 275 | 16 | this.name = interpolateString(this.name, properties); |
| 276 | 16 | this.organization = interpolateString(this.organization, properties); |
| 277 | 16 | this.parentGroupId = interpolateString(this.parentGroupId, properties); |
| 278 | 16 | this.parentArtifactId = interpolateString(this.parentArtifactId, properties); |
| 279 | 16 | this.parentVersion = interpolateString(this.parentVersion, properties); |
| 280 | ||
| 281 | 16 | } |
| 282 | ||
| 283 | /** | |
| 284 | * <p> | |
| 285 | * A utility function that will interpolate strings based on values given in the properties file. It will also interpolate the | |
| 286 | * strings contained within the properties file so that properties can reference other properties.</p> | |
| 287 | * <p> | |
| 288 | * <b>Note:</b> if there is no property found the reference will be removed. In other words, if the interpolated string will | |
| 289 | * be replaced with an empty string. | |
| 290 | * </p> | |
| 291 | * <p> | |
| 292 | * Example:</p> | |
| 293 | * <code> | |
| 294 | * Properties p = new Properties(); | |
| 295 | * p.setProperty("key", "value"); | |
| 296 | * String s = interpolateString("'${key}' and '${nothing}'", p); | |
| 297 | * System.out.println(s); | |
| 298 | * </code> | |
| 299 | * <p> | |
| 300 | * Will result in:</p> | |
| 301 | * <code> | |
| 302 | * 'value' and '' | |
| 303 | * </code> | |
| 304 | * | |
| 305 | * @param text the string that contains references to properties. | |
| 306 | * @param properties a collection of properties that may be referenced within the text. | |
| 307 | * @return the interpolated text. | |
| 308 | */ | |
| 309 | public static String interpolateString(String text, Properties properties) { | |
| 310 | 248 | final Properties props = properties; |
| 311 | 248 | if (text == null) { |
| 312 | 88 | return text; |
| 313 | } | |
| 314 | 160 | if (props == null) { |
| 315 | 48 | return text; |
| 316 | } | |
| 317 | ||
| 318 | 112 | final int pos = text.indexOf("${"); |
| 319 | 112 | if (pos < 0) { |
| 320 | 64 | return text; |
| 321 | } | |
| 322 | 48 | final int end = text.indexOf("}"); |
| 323 | 48 | if (end < pos) { |
| 324 | 0 | return text; |
| 325 | } | |
| 326 | ||
| 327 | 48 | final String propName = text.substring(pos + 2, end); |
| 328 | 48 | String propValue = interpolateString(props.getProperty(propName), props); |
| 329 | 48 | if (propValue == null) { |
| 330 | 0 | propValue = ""; |
| 331 | } | |
| 332 | 48 | final StringBuilder sb = new StringBuilder(propValue.length() + text.length()); |
| 333 | 48 | sb.append(text.subSequence(0, pos)); |
| 334 | 48 | sb.append(propValue); |
| 335 | 48 | sb.append(text.substring(end + 1)); |
| 336 | 48 | return interpolateString(sb.toString(), props); //yes yes, this should be a loop... |
| 337 | } | |
| 338 | ||
| 339 | } |