| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| DependencyVersionUtil |
|
| 7.0;7 |
| 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) 2013 Jeremy Long. All Rights Reserved. | |
| 17 | */ | |
| 18 | package org.owasp.dependencycheck.utils; | |
| 19 | ||
| 20 | import java.util.ArrayList; | |
| 21 | import java.util.regex.Matcher; | |
| 22 | import java.util.regex.Pattern; | |
| 23 | ||
| 24 | /** | |
| 25 | * <p> | |
| 26 | * A utility class to extract version numbers from file names (or other strings containing version numbers.</p> | |
| 27 | * | |
| 28 | * @author Jeremy Long <jeremy.long@owasp.org> | |
| 29 | */ | |
| 30 | public final class DependencyVersionUtil { | |
| 31 | ||
| 32 | /** | |
| 33 | * Regular expression to extract version numbers from file names. | |
| 34 | */ | |
| 35 | 1 | private static final Pattern RX_VERSION = Pattern.compile("\\d+(\\.\\d{1,6})+(\\.?([_-](release|beta|alpha)|[a-zA-Z_-]{1,3}\\d{1,8}))?"); |
| 36 | /** | |
| 37 | * Regular expression to extract a single version number without periods. This is a last ditch effort just to check | |
| 38 | * in case we are missing a version number using the previous regex. | |
| 39 | */ | |
| 40 | 1 | private static final Pattern RX_SINGLE_VERSION = Pattern.compile("\\d+(\\.?([_-](release|beta|alpha)|[a-zA-Z_-]{1,3}\\d{1,8}))?"); |
| 41 | ||
| 42 | /** | |
| 43 | * Private constructor for utility class. | |
| 44 | */ | |
| 45 | 0 | private DependencyVersionUtil() { |
| 46 | 0 | } |
| 47 | ||
| 48 | /** | |
| 49 | * <p> | |
| 50 | * A utility class to extract version numbers from file names (or other strings containing version numbers.<br/> | |
| 51 | * Example:<br/> | |
| 52 | * Give the file name: library-name-1.4.1r2-release.jar<br/> | |
| 53 | * This function would return: 1.4.1.r2</p> | |
| 54 | * | |
| 55 | * @param text the text being analyzed | |
| 56 | * @return a DependencyVersion containing the version | |
| 57 | */ | |
| 58 | public static DependencyVersion parseVersion(String text) { | |
| 59 | 12839 | if (text == null) { |
| 60 | 40 | return null; |
| 61 | } | |
| 62 | //'-' is a special case used within the CVE entries, just include it as the version. | |
| 63 | 12799 | if ("-".equals(text)) { |
| 64 | 19 | final DependencyVersion dv = new DependencyVersion(); |
| 65 | 19 | final ArrayList<String> list = new ArrayList<String>(); |
| 66 | 19 | list.add(text); |
| 67 | 19 | dv.setVersionParts(list); |
| 68 | 19 | return dv; |
| 69 | } | |
| 70 | 12780 | String version = null; |
| 71 | 12780 | Matcher matcher = RX_VERSION.matcher(text); |
| 72 | 12780 | if (matcher.find()) { |
| 73 | 12664 | version = matcher.group(); |
| 74 | } | |
| 75 | //throw away the results if there are two things that look like version numbers | |
| 76 | 12780 | if (matcher.find()) { |
| 77 | 2 | return null; |
| 78 | } | |
| 79 | 12778 | if (version == null) { |
| 80 | 116 | matcher = RX_SINGLE_VERSION.matcher(text); |
| 81 | 116 | if (matcher.find()) { |
| 82 | 106 | version = matcher.group(); |
| 83 | } else { | |
| 84 | 10 | return null; |
| 85 | } | |
| 86 | //throw away the results if there are two things that look like version numbers | |
| 87 | 106 | if (matcher.find()) { |
| 88 | 1 | return null; |
| 89 | } | |
| 90 | } | |
| 91 | 12767 | return new DependencyVersion(version); |
| 92 | } | |
| 93 | } |