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.xml.suppression; 19 20 import org.apache.commons.lang3.builder.EqualsBuilder; 21 import org.apache.commons.lang3.builder.HashCodeBuilder; 22 23 import java.util.regex.Pattern; 24 import javax.annotation.concurrent.ThreadSafe; 25 26 /** 27 * A simple PropertyType used to represent a string value that could be used as 28 * a regular expression or could be case insensitive. The equals method has been 29 * over-ridden so that the object will correctly compare to strings. 30 * 31 * @author Jeremy Long 32 */ 33 @ThreadSafe 34 public class PropertyType { 35 36 //<editor-fold defaultstate="collapsed" desc="properties"> 37 /** 38 * The value. 39 */ 40 private String value; 41 /** 42 * Whether or not the expression is a regex. 43 */ 44 private boolean regex = false; 45 /** 46 * Indicates case sensitivity. 47 */ 48 private boolean caseSensitive = false; 49 50 /** 51 * Gets the value of the value property. 52 * 53 * @return the value of the value property 54 */ 55 public String getValue() { 56 return value; 57 } 58 59 /** 60 * Sets the value of the value property. 61 * 62 * @param value the value of the value property 63 */ 64 public void setValue(String value) { 65 this.value = value; 66 } 67 68 /** 69 * Returns whether or not the value is a regex. 70 * 71 * @return true if the value is a regex, otherwise false 72 */ 73 public boolean isRegex() { 74 return regex; 75 } 76 77 /** 78 * Sets whether the value property is a regex. 79 * 80 * @param value true if the value is a regex, otherwise false 81 */ 82 public void setRegex(boolean value) { 83 this.regex = value; 84 } 85 86 /** 87 * Gets the value of the caseSensitive property. 88 * 89 * @return true if the value is case sensitive 90 */ 91 public boolean isCaseSensitive() { 92 return caseSensitive; 93 } 94 95 /** 96 * Sets the value of the caseSensitive property. 97 * 98 * @param value whether the value is case sensitive 99 */ 100 public void setCaseSensitive(boolean value) { 101 this.caseSensitive = value; 102 } 103 //</editor-fold> 104 105 /** 106 * Uses the object's properties to determine if the supplied string matches 107 * the value of this property. 108 * 109 * @param text the String to validate 110 * @return whether the text supplied is matched by the value of the property 111 */ 112 public boolean matches(String text) { 113 if (text == null) { 114 return false; 115 } 116 if (this.regex) { 117 final Pattern rx; 118 if (this.caseSensitive) { 119 rx = Pattern.compile(this.value); 120 } else { 121 rx = Pattern.compile(this.value, Pattern.CASE_INSENSITIVE); 122 } 123 return rx.matcher(text).matches(); 124 } else { 125 if (this.caseSensitive) { 126 return value.equals(text); 127 } else { 128 return value.equalsIgnoreCase(text); 129 } 130 } 131 } 132 133 //<editor-fold defaultstate="collapsed" desc="standard implementations of hashCode, equals, and toString"> 134 135 /** 136 * Default implementation of hashCode. 137 * 138 * @return the hash code 139 */ 140 @Override 141 public int hashCode() { 142 return new HashCodeBuilder(3, 59) 143 .append(value) 144 .append(regex) 145 .append(caseSensitive) 146 .toHashCode(); 147 } 148 149 /** 150 * Default implementation of equals. 151 * 152 * @param obj the object to compare 153 * @return whether the objects are equivalent 154 */ 155 @Override 156 public boolean equals(Object obj) { 157 if (obj == null || !(obj instanceof PropertyType)) { 158 return false; 159 } 160 if (this == obj) { 161 return true; 162 } 163 final PropertyType rhs = (PropertyType) obj; 164 return new EqualsBuilder() 165 .append(value, rhs.value) 166 .append(regex, rhs.regex) 167 .append(caseSensitive, rhs.caseSensitive) 168 .isEquals(); 169 } 170 171 /** 172 * Default implementation of toString(). 173 * 174 * @return the string representation of the object 175 */ 176 @Override 177 public String toString() { 178 return "PropertyType{" + "value=" + value + ", regex=" + regex + ", caseSensitive=" + caseSensitive + '}'; 179 } 180 //</editor-fold> 181 }