* Copy the classpath to the command line with access rules included.
* @param cmd the given command line
* @param classpath the given classpath entry
*/
private void createClasspathArgument(Commandline cmd, Path classpath) {
Argument arg = cmd.createArgument();
final String[] pathElements = classpath.list();
// empty path return empty string
if (pathElements.length == 0) {
arg.setValue(Util.EMPTY_STRING);
return;
}
// no access rules, can set the path directly
if (accessRules == null) {
arg.setPath(classpath);
return;
}
int rulesLength = accessRules.size();
String[] rules = (String[]) accessRules.toArray(new String[rulesLength]);
int nextRule = 0;
final StringBuffer result = new StringBuffer();
//access rules are expected in the same order as the classpath, but there could
//be elements in the classpath not in the access rules or access rules not in the classpath
for (int i = 0, max = pathElements.length; i < max; i++) {
if (i > 0)
result.append(File.pathSeparatorChar);
String pathElement = pathElements[i];
result.append(pathElement);
//the rules list is [path, rule, path, rule, ...]
for (int j = nextRule; j < rulesLength; j += 2) {
String rule = rules[j];
if (pathElement.endsWith(rule)) {
result.append(rules[j + 1]);
nextRule = j + 2;
break;
}
// if the path doesn't match, it could be due to a trailing file separatorChar in the rule
if (rule.endsWith(File.separator)) {
// rule ends with the File.separator, but pathElement might not
// otherwise it would match on the first endsWith
int ruleLength = rule.length();
if (pathElement.regionMatches(false, pathElement.length() - ruleLength + 1, rule, 0, ruleLength - 1)) {
result.append(rules[j + 1]);
nextRule = j + 2;
break;
}
} else if (pathElement.endsWith(File.separator)) {
// rule doesn't end with the File.separator, but pathElement might
int ruleLength = rule.length();
if (pathElement.regionMatches(false, pathElement.length() - ruleLength - 1, rule, 0, ruleLength)) {
result.append(rules[j + 1]);
nextRule = j + 2;
break;
}
}
}
}
arg.setValue(result.toString());
}