Monday, January 28, 2008

Replace relative links to absolute base links in HTML - Java

this is a function that can add a base url to all urls in an html page, this code was translated from this vb function displayed here :http://www.motobit.com/tips/detpg_replace-relative-links/

special thanks to the original coder...

the function (java) was tested and run successfully!

public static String ReplaceLinksToBase(String HTML, String baseURL)
// replaces all links in an html to base url
// By Ramik, 28/1/2008
{
HTML = ReplaceTagToBase(HTML, "a", "href", baseURL);
// replace <img src=...> links
HTML = ReplaceTagToBase(HTML, "img", "src", baseURL);
HTML = ReplaceTagToBase(HTML, "link", "href", baseURL);
return HTML;
}


public static String ReplaceTagToBase (String HTML, String TagName, String ValueName, String BaseURL)
// replace a specific tag url to base Url
//By Ramik, 28/01/2008
{
int pos1, pos2;
String p1, p2, html;
html= HTML;
//Find the tag In HTML.
String temp = "<"+TagName ;
pos1 = HTML.indexOf(temp);
while (pos1>0)
{
//The tag was found. Find closing > of the tag.
pos2 = HTML.indexOf(">",pos1+TagName.length());
if (pos2>-1)
{
//separate tag text from HTML
String tag, tag1;
tag = HTML.substring(pos1, pos2);
//rewrite realative URL links In the one tag.
tag1 = ReplaceParamToBase(tag, ValueName, BaseURL);
//The tag was changed - relative links found.
if (!tag1.equals(tag)) //there is some change In the tag.
{
//get parts before And after the tag
p1 = HTML.substring(0,pos1);
p2 = HTML.substring(pos2,HTML.length());
//Compute new POs2 position
pos2 = pos2 + tag1.length() - tag.length() ;
//replace old tag with relative links with the new version
html = p1 + tag1 + p2 ;
}
}
if (pos2>0)
pos1 = HTML.indexOf("<"+TagName,pos2+1);
else
pos1 = 0 ;
}
return html;
}

public static String ReplaceParamToBase( String Tag, String ValueName,String BaseURL)
{
String p1, p2 , c1, URL, tag=Tag;
int pos1, pos2, lenVal ;
lenVal = ValueName.length() ;
//find position of the tag value
pos1 = Tag.indexOf(ValueName+"=");
if (pos1>-1)
{
//get a first char after =
c1 = Tag.substring(pos1+lenVal+1,pos1+lenVal+2);
if (c1.equals("\""))
{
pos1 = pos1+lenVal+1 ;
pos2 = Tag.indexOf("\"",pos1+1);
}
else if (c1.equals("'"))
{
pos1 = pos1+lenVal+1;
pos2 = Tag.indexOf("'",pos1+1);
}
else//the value ends with Space
{
pos1 = pos1+lenVal;
pos2 = Tag.indexOf(" ",pos1);
if (pos2==-1)
pos2 = Tag.length();
}
p1 = Tag.substring(0,pos1+1);
p2 = Tag.substring(pos2,Tag.length());
URL = Tag.substring(pos1+1,pos2);
//is the value relative URL?
if (URL.indexOf("://")==-1 && URL.indexOf("mailto:")==-1)
{
//make the new absolute URL
if (BaseURL.charAt(BaseURL.length()-1)!='/' && BaseURL.charAt(BaseURL.length()-1)!='\\' )
{
BaseURL=BaseURL+'/';
}
URL = BaseURL+URL ;
tag = p1+URL+p2;
}

}
return tag;
}


public static String replaceAllTags (String HTML, String TagName, String ValueName, String BaseURL)
//Replace all tags in html to base url
// by ramik 30/1/2008
{
String html = HTML , allHtml="";
int pos1=0, pos2 = HTML.indexOf(">");
if (pos2>-1)
pos2++;
while (pos2>-1)
{
html = HTML.substring(pos1,pos2);
allHtml = allHtml + ReplaceTagToBase(html,TagName, ValueName, BaseURL);
pos1=pos2;
pos2=HTML.indexOf(">",pos2);
if (pos2>-1)
pos2++;
}
allHtml = allHtml + HTML.substring(pos1);
return allHtml;
}

Discuss this Post

1 comment:

panschk said...

Thanks for the code. I could use it in a project I'm doing right now.