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]
# 1

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);

}

TimSparqa at 2007-7-11 15:23:38 > top of Java-index,Java Essentials,Java Programming...
# 2
If you go for the method above, you also need to handle1) No hyperlink needed2) more than one hyperlink needed3) https URL
rym82a at 2007-7-11 15:23:38 > top of Java-index,Java Essentials,Java Programming...
# 3

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?

skrancha at 2007-7-11 15:23:38 > top of Java-index,Java Essentials,Java Programming...
# 4
All very true. Probably other boundary conditions too. Like URL starting the string and/or ending the string and all sort of things like that.
TimSparqa at 2007-7-11 15:23:38 > top of Java-index,Java Essentials,Java Programming...
# 5

test:

http://www.google.com.au/search?q=java+construct+URL+add+label&hl=en&client=firefox-a&rls=org.mozilla:en-US:official&hs=R7a&start=20&sa=Nq=java+construct+URL+add+label&hl=en&client=firefox-a&rls=org.mozilla:en-US:official&hs=R7a&start=20&sa=Nq=java+construct+URL+add+label&hl=en&client=firefox-a&rls=org.mozilla:en-US:official&hs=R7a&start=20&sa=Nq=java+construct+URL+add+label&hl=en&client=firefox-a&rls=org.mozilla:en-US:official&hs=R7a&start=20&sa=Nq=java+construct+URL+add+label&hl=en&client=firefox-a&rls=org.mozilla:en-US:official&hs=R7a&start=20&sa=N

skrancha at 2007-7-11 15:23:38 > top of Java-index,Java Essentials,Java Programming...
# 6

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>...

skrancha at 2007-7-11 15:23:38 > top of Java-index,Java Essentials,Java Programming...