Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Three small tweaks. #80

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 21 additions & 20 deletions src/main/java/us/springett/parsers/cpe/Cpe.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@
* @author Jeremy Long
*/
public class Cpe implements ICpe, Serializable {
private static final Pattern VERSION_SPLIT_PATTERN = Pattern.compile("(\\.|:-)");
private static final Pattern DIGITS_AND_LETTERS_PATTERN = Pattern.compile("^[\\d]+?[A-z]+");
private static final Pattern VERSION_SPLIT_INNER_PATTERN = Pattern.compile("^([\\d]+?)(.*)$");

/**
* The serial version UID.
Expand Down Expand Up @@ -719,86 +722,85 @@ public String toString() {
* @return the sort order
*/
@Override
public int compareTo(Object o) {
if (o instanceof ICpe) {
ICpe otherObject = (ICpe) o;
public int compareTo(ICpe o) {
if (o != null) {

final int before = -1;
final int equal = 0;
final int after = 1;

if (this == otherObject) {
if (this == o) {
return equal;
}
int r = getPart().getAbbreviation().compareTo(otherObject.getPart().getAbbreviation());
int r = getPart().getAbbreviation().compareTo(o.getPart().getAbbreviation());
if (r < 0) {
return before;
} else if (r > 0) {
return after;
}
r = getVendor().compareTo(otherObject.getVendor());
r = getVendor().compareTo(o.getVendor());
if (r < 0) {
return before;
} else if (r > 0) {
return after;
}
r = getProduct().compareTo(otherObject.getProduct());
r = getProduct().compareTo(o.getProduct());
if (r < 0) {
return before;
} else if (r > 0) {
return after;
}
r = compareVersions(getVersion(), otherObject.getVersion());
r = compareVersions(getVersion(), o.getVersion());
if (r < 0) {
return before;
} else if (r > 0) {
return after;
}
r = getUpdate().compareTo(otherObject.getUpdate());
r = getUpdate().compareTo(o.getUpdate());
if (r < 0) {
return before;
} else if (r > 0) {
return after;
}
r = getEdition().compareTo(otherObject.getEdition());
r = getEdition().compareTo(o.getEdition());
if (r < 0) {
return before;
} else if (r > 0) {
return after;
}
r = getLanguage().compareTo(otherObject.getLanguage());
r = getLanguage().compareTo(o.getLanguage());
if (r < 0) {
return before;
} else if (r > 0) {
return after;
}
r = getSwEdition().compareTo(otherObject.getSwEdition());
r = getSwEdition().compareTo(o.getSwEdition());
if (r < 0) {
return before;
} else if (r > 0) {
return after;
}
r = getTargetSw().compareTo(otherObject.getTargetSw());
r = getTargetSw().compareTo(o.getTargetSw());
if (r < 0) {
return before;
} else if (r > 0) {
return after;
}
r = getTargetHw().compareTo(otherObject.getTargetHw());
r = getTargetHw().compareTo(o.getTargetHw());
if (r < 0) {
return before;
} else if (r > 0) {
return after;
}
r = getOther().compareTo(otherObject.getOther());
r = getOther().compareTo(o.getOther());
if (r < 0) {
return before;
} else if (r > 0) {
return after;
}
return equal;
}
throw new RuntimeException("Unable to compare " + o.getClass().getCanonicalName());
return -1;
}

/**
Expand Down Expand Up @@ -858,13 +860,12 @@ protected static int compareVersions(String left, String right) {
*/
private static List<String> splitVersion(String s) {
//TODO improve performance by removing regex.
final Pattern pattern = Pattern.compile("^([\\d]+?)(.*)$");
final String[] splitString = s.split("(\\.|:-)");
final String[] splitString = VERSION_SPLIT_PATTERN.split(s);

final List<String> res = new ArrayList<>();
for (String token : splitString) {
if (token.matches("^[\\d]+?[A-z]+")) {
final Matcher matcher = pattern.matcher(token);
if (DIGITS_AND_LETTERS_PATTERN.matcher(token).matches()) {
final Matcher matcher = VERSION_SPLIT_INNER_PATTERN.matcher(token);
matcher.find();
final String g1 = matcher.group(1);
final String g2 = matcher.group(2);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/us/springett/parsers/cpe/ICpe.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
*
* @author Jeremy Long
*/
public interface ICpe extends Comparable {
public interface ICpe extends Comparable<ICpe> {

/**
* <p>
Expand Down
119 changes: 56 additions & 63 deletions src/main/java/us/springett/parsers/cpe/util/Convert.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import us.springett.parsers.cpe.exceptions.CpeEncodingException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.regex.Pattern;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -135,39 +136,35 @@ public static String wellFormedToCpeUri(String wellFormed) throws CpeEncodingExc
if (LogicalValue.NA.getAbbreviation().equals(wellFormed)) {
return wellFormed;
}
try {
byte[] bytes = wellFormed.getBytes("UTF-8");
StringBuilder sb = new StringBuilder(wellFormed.length() + 10);
for (int x = 0; x < bytes.length; x++) {
byte c = bytes[x];
if ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
byte[] bytes = wellFormed.getBytes(StandardCharsets.UTF_8);
StringBuilder sb = new StringBuilder(wellFormed.length() + 10);
for (int x = 0; x < bytes.length; x++) {
byte c = bytes[x];
if ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
sb.append((char) c);
} else if (c == '\\') {
x += 1;
if (x >= bytes.length) {
throw new CpeEncodingException("Invalid Well Formed string - ends with an unquoted backslash");
}
c = bytes[x];
if (c == '_' || c == '.' || c == '-') {
sb.append((char) c);
} else if (c == '\\') {
x += 1;
if (x >= bytes.length) {
throw new CpeEncodingException("Invalid Well Formed string - ends with an unquoted backslash");
}
c = bytes[x];
if (c == '_' || c == '.' || c == '-') {
sb.append((char) c);
} else {
//consider .append(Integer.toHexString(c))
sb.append('%')
.append(HEX_CHARS[(c & 0xF0) >>> 4])
.append(HEX_CHARS[c & 0x0F]);
}
} else if (c == '*') {
sb.append("%02");
} else if (c == '?') {
sb.append("%01");
} else {
throw new CpeEncodingException("Invalid Well Formed string - unexpected characters: " + wellFormed);
//consider .append(Integer.toHexString(c))
sb.append('%')
.append(HEX_CHARS[(c & 0xF0) >>> 4])
.append(HEX_CHARS[c & 0x0F]);
}
} else if (c == '*') {
sb.append("%02");
} else if (c == '?') {
sb.append("%01");
} else {
throw new CpeEncodingException("Invalid Well Formed string - unexpected characters: " + wellFormed);
}
return sb.toString();
} catch (UnsupportedEncodingException ex) {
throw new CpeEncodingException("UTF-8 encoding is not supported on this JVM?", ex);
}
return sb.toString();
}

/**
Expand Down Expand Up @@ -200,46 +197,42 @@ public static String cpeUriToWellFormed(String value, boolean lenient) throws Cp
} else if (LogicalValue.NA.getAbbreviation().equals(value)) {
return LogicalValue.NA.getAbbreviation();
}
try {
byte[] bytes = value.toLowerCase().getBytes("UTF-8");
StringBuilder sb = new StringBuilder(value.length());
for (int x = 0; x < bytes.length; x++) {
char c = (char) bytes[x];
if ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'z')) {
sb.append(c);
} else if (c == '_' || c == '.' || c == '-') {
byte[] bytes = value.toLowerCase().getBytes(StandardCharsets.UTF_8);
StringBuilder sb = new StringBuilder(value.length());
for (int x = 0; x < bytes.length; x++) {
char c = (char) bytes[x];
if ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'z')) {
sb.append(c);
} else if (c == '_' || c == '.' || c == '-') {
sb.append('\\').append(c);
} else if (c == '%') {
if ((2 + x) >= bytes.length) {
throw new CpeEncodingException("Invalid CPE URI component - ends with a single percent");
}
char decoded = (char) (Character.digit(bytes[++x], 16) * 16
+ Character.digit(bytes[++x], 16));
switch (decoded) {
case 1:
sb.append('?');
break;
case 2:
sb.append('*');
break;
default:
sb.append('\\').append(decoded);
break;
}
} else {
if (lenient) {
//TODO check to ensure that the value is in the ascii range per spec?
LOG.debug("Invalid CPE URI part, '{}'; escaping '{}' as a well formatted string", value, c);
sb.append('\\').append(c);
} else if (c == '%') {
if ((2 + x) >= bytes.length) {
throw new CpeEncodingException("Invalid CPE URI component - ends with a single percent");
}
char decoded = (char) (Character.digit(bytes[++x], 16) * 16
+ Character.digit(bytes[++x], 16));
switch (decoded) {
case 1:
sb.append('?');
break;
case 2:
sb.append('*');
break;
default:
sb.append('\\').append(decoded);
break;
}
} else {
if (lenient) {
//TODO check to ensure that the value is in the ascii range per spec?
LOG.debug("Invalid CPE URI part, '%s'; escaping '%s' as a well formatted string", value, c);
sb.append('\\').append(c);
} else {
throw new CpeEncodingException("Invalid CPE URI component - unexpected characters");
}
throw new CpeEncodingException("Invalid CPE URI component - unexpected characters");
}
}
return sb.toString();
} catch (UnsupportedEncodingException ex) {
throw new CpeEncodingException("UTF-8 encoding is not supported on this JVM?", ex);
}
return sb.toString();
}

/**
Expand Down