View Javadoc

1   /***
2    * Licensed under the Artistic License; you may not use this file
3    * except in compliance with the License.
4    * You may obtain a copy of the License at
5    *
6    *      http://displaytag.sourceforge.net/license.html
7    *
8    * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
9    * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
10   * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
11   */
12  package org.displaytag.decorator;
13  
14  import org.apache.commons.lang.ArrayUtils;
15  import org.apache.commons.lang.StringUtils;
16  import org.displaytag.util.TagConstants;
17  
18  
19  /***
20   * This takes the string that is passed in, and "auto-links" it, it turns email addresses into hyperlinks, and also
21   * turns things that looks like URLs into hyperlinks as well.
22   * @author Fabrizio Giustina
23   * @version $Revision: 1.3 $ ($Author: fgiust $)
24   */
25  public class AutolinkColumnDecorator implements ColumnDecorator
26  {
27  
28      /***
29       * Instance used for the "autolink" tag attribute. Will be removed in future along with the attribute.
30       */
31      public static final ColumnDecorator INSTANCE = new AutolinkColumnDecorator();
32  
33      /***
34       * "://".
35       */
36      private static final String URL_DELIM = "://"; //$NON-NLS-1$
37  
38      /***
39       * Urls.
40       */
41      private static final String[] URLS_PREFIXES = //
42      new String[]{"http", "https", "ftp"}; //$NON-NLS-1$ //$NON-NLS-1$ //$NON-NLS-1$
43  
44      /***
45       * @see org.displaytag.decorator.ColumnDecorator#decorate(java.lang.Object)
46       */
47      public String decorate(Object columnValue)
48      {
49          if (columnValue == null)
50          {
51              return null;
52          }
53          String work = columnValue.toString();
54  
55          int urlBegin;
56          StringBuffer buffer = new StringBuffer();
57  
58          // First check for email addresses.
59          while ((urlBegin = work.indexOf('@')) != -1)
60          {
61              int start = 0;
62              int end = work.length() - 1;
63  
64              // scan backwards...
65              for (int j = urlBegin; j >= 0; j--)
66              {
67                  if (Character.isWhitespace(work.charAt(j)))
68                  {
69                      start = j + 1;
70                      break;
71                  }
72              }
73  
74              // scan forwards...
75              for (int j = urlBegin; j <= end; j++)
76              {
77                  if (Character.isWhitespace(work.charAt(j)))
78                  {
79                      end = j - 1;
80                      break;
81                  }
82              }
83  
84              String email = work.substring(start, end + 1);
85  
86              buffer.append(work.substring(0, start)).append("<a href=\"mailto:") //$NON-NLS-1$
87                  .append(email + "\">") //$NON-NLS-1$
88                  .append(email).append("</a>"); //$NON-NLS-1$
89  
90              if (end == work.length())
91              {
92                  work = TagConstants.EMPTY_STRING;
93              }
94              else
95              {
96                  work = work.substring(end + 1);
97              }
98          }
99  
100         work = buffer.toString() + work;
101         buffer = new StringBuffer();
102 
103         // Now check for urls...
104         while ((urlBegin = work.indexOf(URL_DELIM)) != -1)
105         {
106 
107             // scan backwards...
108             int fullUrlBegin = urlBegin;
109             StringBuffer prefixBuffer = new StringBuffer(10);
110             for (int j = fullUrlBegin - 1; j >= 0; j--)
111             {
112                 if (Character.isWhitespace(work.charAt(j)))
113                 {
114                     fullUrlBegin = j + 1;
115                     break;
116                 }
117                 fullUrlBegin = j;
118                 prefixBuffer.append(work.charAt(j));
119             }
120 
121             if (!ArrayUtils.contains(URLS_PREFIXES, StringUtils.reverse(prefixBuffer.toString())))
122             {
123 
124                 buffer.append(work.substring(0, urlBegin + 3));
125                 work = work.substring(urlBegin + 3);
126                 continue;
127             }
128 
129             int urlEnd = work.length();
130 
131             // scan forwards...
132             for (int j = urlBegin; j < urlEnd; j++)
133             {
134                 if (Character.isWhitespace(work.charAt(j)))
135                 {
136                     urlEnd = j;
137                     break;
138                 }
139             }
140 
141             String url = work.substring(fullUrlBegin, urlEnd);
142 
143             buffer.append(work.substring(0, fullUrlBegin)).append("<a href=\"")//$NON-NLS-1$
144                 .append(url).append("\">")//$NON-NLS-1$
145                 .append(url).append("</a>"); //$NON-NLS-1$
146 
147             if (urlEnd >= work.length())
148             {
149                 work = TagConstants.EMPTY_STRING;
150             }
151             else
152             {
153                 work = work.substring(urlEnd);
154             }
155         }
156 
157         buffer.append(work);
158         return buffer.toString();
159     }
160 
161 }