}
};
// Replace all replacement variables with prototype values
ReplacementEngine engine = new ReplacementEngine();
engine.addPattern(NetworkPlaceInstall.VARIABLE_PATTERN, r, null);
path = engine.replace(path);
//
URI newUri = null;
// Is it a UNC path
if(path.startsWith("\\\\")) {
try {
newUri = new URI("smb:" + path.replace('\\', '/'));
}
catch(MalformedURIException murie) {
murie.printStackTrace();
}
}
// Is this a supported RI?
if(newUri == null) {
int idx = path.indexOf(':');
if(idx > 1) { // index of 1 would mean a windows absolute path
// Is it an non windows absolute file URI?
String rpath = path.substring(idx + 1);
String scheme = path.substring(0, idx);
if(scheme.equals("file")) {
if(rpath.startsWith("//") && !rpath.startsWith("///") &&
( rpath.length() < 4 || rpath.charAt(3) != ':' ) ) {
path = scheme + ":/" + rpath;
}
}
newUri = new URI(path);
}
}
// Is it a local file? (wont work for replacements)
boolean switchSlash = false;
if(newUri == null) {
if(path.contains("\\")) {
switchSlash = true;
}
try {
String scheme;
if(path.toLowerCase().endsWith(".jar")) {
scheme = "jar";
} else if(path.toLowerCase().endsWith(".zip")) {
scheme = "zip";
} else {
scheme = "file";
}
newUri = new URI(scheme + ":///" + DAVUtilities.stripLeadingSlash(path.replace('\\', '/')));
} catch (MalformedURIException e) {
e.printStackTrace();
}
}
// Convert the network place if required
if(newUri != null) {
engine = new ReplacementEngine();
r = new Replacer() {
public String getReplacement(Pattern pattern, Matcher matcher, String replacementPattern) {
String match = matcher.group();
String key = match.substring(11, match.length() - 1);
try {
int idx = key.indexOf("_");
if (idx == -1) {
throw new Exception("String replacement pattern is in incorrect format for " + key
+ ". Must be <TYPE>:<key>");
}
String type = key.substring(0, idx);
key = key.substring(idx + 1);
return "${" + type + ":" + key + "}";
}
catch(Exception e) {
NetworkPlaceInstall.log.error("Failed to replace.", e);
}
return "prototype";
}
};
engine.addPattern(NetworkPlaceInstall.PROTOTYPE_PATTERN, r, null);
String newScheme = newUri.getScheme();
String newHost = newUri.getHost();
if(!Util.isNullOrTrimmedBlank(newHost)) {
newHost = engine.replace(newHost);
}
int newPort = Math.max(newUri.getPort(), 0);
String newPath = newUri.getPath();
if(!Util.isNullOrTrimmedBlank(newPath)) {
newPath = Util.urlDecode(engine.replace(newPath));
}
if(newPath.startsWith("/") && newPath.length() > 2 && newPath.charAt(2) == ':') {
newPath = newPath.substring(1);
}
if(switchSlash) {
newPath = newPath.replace('/', '\\');
}
String newFragment = newUri.getFragment();
if(!Util.isNullOrTrimmedBlank(newFragment)) {
newFragment = engine.replace(newFragment);
}
String newUserinfo = newUri.getUserinfo();
int idx = newUserinfo == null ? -1 : newUserinfo.indexOf(':');
String newUsername = newUserinfo;
String newPassword = "";
if(idx != -1) {
newPassword = newUsername.substring(idx + 1);
newUsername = newUsername.substring(0, idx);
}
if(!Util.isNullOrTrimmedBlank(newUsername)) {
newUsername = Util.urlDecode(engine.replace(newUsername));
}
if(!Util.isNullOrTrimmedBlank(newPassword)) {
newPassword = Util.urlDecode(engine.replace(newPassword));
}
networkPlace.setScheme(Util.emptyWhenNull(newScheme));
networkPlace.setHost(Util.emptyWhenNull(newHost));
networkPlace.setPort(newPort);