Package gov.nist.secauto.cpe.common
Class Utilities
java.lang.Object
gov.nist.secauto.cpe.common.Utilities
public class Utilities
extends java.lang.Object
A collection of utility functions for use with the gov.nist.secauto.cpe.matching and
gov.nist.secauto.cpe.naming packages.
- Author:
- Joshua Kraunelis, David Waltermire
- See Also:
- CPE Specifications
-
Constructor Summary
Constructors Constructor Description Utilities()
-
Method Summary
Modifier and Type Method Description static boolean
containsWildcards(java.lang.String str)
Searches string for special characters * and ?.static int
countEscapeCharacters(java.lang.String str, int start, int end)
Counts the number of escape characters in the string beginning and ending at the given indices.static int
getUnescapedColonIndex(java.lang.String str)
Searches a string for the first unescaped colon and returns the index of that colon.static int
indexOf(java.lang.String str1, java.lang.String str2, int offset)
Searches a string for the first occurrence of another string, starting at a given offset.static boolean
isAlphanum(java.lang.String str)
Determines if the string contains only alphanumeric characters or the underscore character.static boolean
isEvenNumber(int num)
Checks if given number is even or not.static java.lang.String
reverse(java.lang.String str)
Returns a copy of the given string in reverse order.static java.lang.String
strcat(java.lang.String... strings)
Concatenates an arbitrary number of strings, in the given order.static int
strchr(java.lang.String str, int chr, int offset)
Searches a string for a character starting at a given offset.static int
strlen(java.lang.String str)
Returns the number of characters in the given string.static java.lang.String
substr(java.lang.String str, int begin, int end)
Extracts the characters between b and e, from the string s.static java.lang.String
toLowercase(java.lang.String str)
Converts all alphabetic characters in a String to lowercase.static void
validateFS(java.lang.String str)
This function is not part of the reference implementation pseudo code found in the CPE 2.3 specification.static void
validateURI(java.lang.String str)
This function is not part of the reference implementation pseudo code found in the CPE 2.3 specification.
-
Constructor Details
-
Utilities
public Utilities()
-
-
Method Details
-
strcat
public static java.lang.String strcat(java.lang.String... strings)Concatenates an arbitrary number of strings, in the given order.- Parameters:
strings
- strings to be concatenated- Returns:
- a single string representing all the arguments, concatenated
-
substr
public static java.lang.String substr(java.lang.String str, int begin, int end)Extracts the characters between b and e, from the string s.- Parameters:
str
- the string which the substring should be extracted frombegin
- beginning index, inclusiveend
- ending index, exclusive- Returns:
- the characters between index begin and index end
-
strlen
public static int strlen(java.lang.String str)Returns the number of characters in the given string.- Parameters:
str
- the string- Returns:
- the number of characters in the string
-
strchr
public static int strchr(java.lang.String str, int chr, int offset)Searches a string for a character starting at a given offset. Returns the offset of the character if found, -1 if not found.- Parameters:
str
- string to be searchedchr
- character to search foroffset
- offset to start at- Returns:
- the integer offset of the character, or -1
-
toLowercase
public static java.lang.String toLowercase(java.lang.String str)Converts all alphabetic characters in a String to lowercase.- Parameters:
str
- string to convert to lowercase- Returns:
- lowercase version of str
-
indexOf
public static int indexOf(java.lang.String str1, java.lang.String str2, int offset)Searches a string for the first occurrence of another string, starting at a given offset.- Parameters:
str1
- String to search.str2
- String to search for.offset
- Integer offset or -1 if not found.- Returns:
- the position of the first occurrence of str2, or -1 if the string was not found
-
containsWildcards
public static boolean containsWildcards(java.lang.String str)Searches string for special characters * and ?.- Parameters:
str
- String to be searched- Returns:
true
if string contains a wildcard, orfalse
otherwise
-
isEvenNumber
public static boolean isEvenNumber(int num)Checks if given number is even or not.- Parameters:
num
- number to check- Returns:
true
if number is even,false
if not
-
countEscapeCharacters
public static int countEscapeCharacters(java.lang.String str, int start, int end)Counts the number of escape characters in the string beginning and ending at the given indices.- Parameters:
str
- string to searchstart
- beginning index, inclusiveend
- ending index, exclusive- Returns:
- number of escape characters in string
-
getUnescapedColonIndex
public static int getUnescapedColonIndex(java.lang.String str)Searches a string for the first unescaped colon and returns the index of that colon.- Parameters:
str
- string to search- Returns:
- index of first unescaped colon, or 0 if not found
-
isAlphanum
public static boolean isAlphanum(java.lang.String str)Determines if the string contains only alphanumeric characters or the underscore character.- Parameters:
str
- the string in question- Returns:
true
if c is alphanumeric or underscore, orfalse
otherwise
-
reverse
public static java.lang.String reverse(java.lang.String str)Returns a copy of the given string in reverse order.- Parameters:
str
- the string to be reversed- Returns:
- a reversed copy of s
-
validateURI
public static void validateURI(java.lang.String str) throws java.text.ParseExceptionThis function is not part of the reference implementation pseudo code found in the CPE 2.3 specification. It enforces two rules in the specification: 1) a CPE URI must start with the characters "cpe:/". 2) A URI may not contain more than 7 components. If either rule is violated, a ParseException is thrown.- Parameters:
str
- the potential CPE formatted string to validate- Throws:
java.text.ParseException
- if one of the rules for a CPE URI is violated
-
validateFS
public static void validateFS(java.lang.String str) throws java.text.ParseExceptionThis function is not part of the reference implementation pseudo code found in the CPE 2.3 specification. It enforces three rules found in the specification: 1) A CPE formatted string must start with the characters "cpe:2.3:". 2) A formatted string must contain 11 components. 3) A formatted string must not contain empty components. If any rule is violated, a ParseException is thrown.- Parameters:
str
- the potential CPE formatted string to validate- Throws:
java.text.ParseException
- if one of the rules for a CPE formatted string is violated
-