3

I know this questions get ask a lot, and Kelly Chan did provide an answer that is work for me, however, there still minor problem that I hope the community can help me out.

For example if a user type this:

Please visit www.google.com

Then I want to convert it into this

Please visit <a href="http://www.google.com">www.google.com</a>

NOTE: that the original text only contain www.google.com, but I somehow detect that it need to have http:// in front of it. so the link become <a href="http://www.google.com">www.google.com</a>. If the link is http://www.google.com, then I just need to wrap it around <a href>.

EDIT: Kelly Chan has revised her answer and it work. Below is the solution.

    Pattern patt = Pattern.compile("(?i)\\b((?:https?://|www\\d{0,3}[.]|[a-z0-9.\\-]+[.][a-z]{2,4}/)(?:[^\\s()<>]+|\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\))+(?:\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\)|[^\\s`!()\\[\\]{};:\'\".,<>???“”‘’]))");
    Matcher matcher = patt.matcher(this.mytext);    
    if(matcher.find()){
        if (matcher.group(1).startsWith("http://")){
            return matcher.replaceAll("<a href=\"$1\">$1</a>");
        }else{
            return matcher.replaceAll("<a href=\"http://$1\">$1</a>");
        }   
    }else{
        return this.mytext
    }
Thang Pham
  • 38,125
  • 75
  • 201
  • 285
  • 1
    A search including "Linkify" and "URL" will prove fruitful. (This question gets asked a lot - i.e. There are lots of answers out there already.) – ridgerunner Mar 22 '11 at 04:51
  • @ridgerunner: I am sorry, however I did try what you said and I cant seem to find an answer that fix my problem. `Kelly Chan` provides a nice solution of detect and convert the text link to html link. However, for link like `www.google.com`, I need to make it into `http://www/google.com`, and `Kelly` code does not do so. Can you think of a way to help me on that. I did search around and cant seem to find a solution fit my need. – Thang Pham Mar 22 '11 at 16:56

1 Answers1

4

You can encapsulate the mytext into an object (say MyTextTO ) .Then you implement a method (say getLinkifiedMyText()) to return the linkified format of mytext on the MyTextTO. Your MBean should has an ArrayList<MyTextTO>to store a list of the MyTextTO which will be displayed in your JSF using <h:dataTable> . After you bind the value of the <h:outputText> to the getLinkifiedMyText(), the linkified text can be displayed.

I refer to this link to implement the getLinkifiedMyText() :

public class MyTextTO{
        private String mytext;

       /**Getters , setters and constructor**/

        public String getLinkifiedMyText(){

            try {
                    Pattern patt = Pattern.compile("(?i)\\b((?:https?://|www\\d{0,3}[.]|[a-z0-9.\\-]+[.][a-z]{2,4}/)(?:[^\\s()<>]+|\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\))+(?:\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\)|[^\\s`!()\\[\\]{};:\'\".,<>???“”‘’]))");
                    Matcher matcher = patt.matcher(this.mytext);    

                    if (matcher.group(1).startsWith("http://")){
                                return matcher.replaceAll("<a href=\"$1\">$1</a>");
                    }else{
                            return matcher.replaceAll("<a href=\"http://$1\">$1</a>");
                    }   
            } catch (Exception e) {
               return this.mytext;
            }
        }
}



<h:dataTable  value="#{bean.dataList}" var="row">
    <h:column>  
        <h:outputText value="#{row.linkifiedMyText}" escape="false" />
    </h:column>
</h:dataTable>
Community
  • 1
  • 1
Ken Chan
  • 84,777
  • 26
  • 143
  • 172
  • This work great. Thank you. However I have a quick question. Now that I have this `www.google.com`, but it does not link to me google page. it link me to `localhost:8080/myprojectname/www.google.com`. This happen because I dont have `http://` in front of `www.google.com`, is it possible if you can fix your regex to also detect if `http://` is there, if not then prepend it before wrap around `` – Thang Pham Mar 22 '11 at 15:51
  • I got an Exception `java.lang.IllegalStateException: No match found` – Thang Pham Mar 22 '11 at 17:44
  • Look to me like it should be `group(3)` instead of `group(1)`, but I try `group(3)` and no luck. – Thang Pham Mar 22 '11 at 17:51
  • I figure it out. You need to wrap your `if else` inside `if(matcher.find())` – Thang Pham Mar 22 '11 at 18:05