Created
May 13, 2014 14:39
-
-
Save smanikandan14/a176433b5497fdeab452 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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