Skip to content

Instantly share code, notes, and snippets.

@smanikandan14
Created May 13, 2014 14:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save smanikandan14/a176433b5497fdeab452 to your computer and use it in GitHub Desktop.
Save smanikandan14/a176433b5497fdeab452 to your computer and use it in GitHub Desktop.
private boolean replaceLocalFilePath(String relativeUrl, String localfilePath) {
boolean urlMatchFound = false;
String PATTERN = relativeUrl;
Pattern addressPattern = Pattern.compile(PATTERN, Pattern.CASE_INSENSITIVE);
Matcher matcher = addressPattern.matcher(downloadedData.toString());
int count = 0;
// Find the number of occurrences of relative url is found in html.
while (matcher.find()) {
count++;
}
//Loop over downloaded html to find the occurrences of relativeUrl.
//Get the start and end position of relativeUrl and go reverse and
//find the starting position of the url. Use url starting position to replace
//with local file path.
for( int i = 0; i < count; i++) {
matcher = addressPattern.matcher(downloadedData.toString());
matcher.find();
int start = matcher.start();
int end = matcher.end();
int urlStartPosition = downloadedData.toString().lastIndexOf("http", start);
if (urlStartPosition == 0) {
urlStartPosition = downloadedData.toString().lastIndexOf("https", start);
}
if ( urlStartPosition >= 0) {
urlMatchFound = true;
downloadedData.delete(urlStartPosition, end);
downloadedData.insert(urlStartPosition, localfilePath);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment