Convert String URL to render as html link
HI there,
I have a bean property 'theDescription' which represents some text submitted by users that I am retrieving from a database. The problem I have is that some submissions contain a URL address within the text...at times very long URL's. It's simple enough to extract the URL using the snippet below:
publicstaticvoid main(String args[]){
String theDescription ="some comments http://someurl.com some more comments";
String theURL ="";
Pattern pattern = Pattern.compile("http(.+?)\\s");
Matcher matcher = pattern.matcher(theDescription);
while (matcher.find()) theURL = matcher.group();
System.out.println(theURL);
}
What i need to do is some how display the selected text as a link when my JSF page is rendered.
So ideally it would show as:
some commentsLink some more comments
thanks in advance.
[1166 byte] By [
skrancha] at [2007-11-26 23:48:14]

You are probably right using regular expressions, but this works for me.
public static void main(String[] args) {
String text = "this is some text http://www.google.com this is some more text";
String start = text.substring(0, text.indexOf("http://"));
String url = text.substring(text.indexOf("http://"), text.indexOf(" ", text.indexOf("http://")));
String end = text.substring(text.indexOf("http://") + url.length(), text.length());
System.out.println(start + "<a href=" + url + ">Link</a>" + end);
}
thanks for the post but this is what i'm getting in the source code of my page:
<td>this is some text & 'lt;'a href=http://www.google.com'>'Link&'lt;/a>'; this is some more text</td>
instead of:
<td>this is some text Link this is some more text</td>
do i need use a servlet to set the content type to html?
Thanks for all the posts, I've decided on a work around which involves mixing JSTL and JSF when rendering the long url's/links see above post for example causing the table to stretch (i was after an alternative to the css options currently available which involve trimming the url or placing it some kind of scroll box). Solution is below:
The values in the for each loop will need to be substituted with the number of URL's found in the backing bean including logic to allow for variations in protocol https etc....
...
<h:outputText value="Related Links"/>
<h:panelGrid columns="2">
<c:forEach var="i" begin="1" end="10" step="1">
<h:outputLink value="#{MyBean.theURL}">
<f:verbatim>Related Link <c:out value="${i}"/></f:verbatim>
</h:outputLink>
</c:forEach>
</h:panelGrid>...