1 12 package org.displaytag.decorator; 13 14 import javax.servlet.jsp.PageContext ; 15 16 import org.apache.commons.lang.ArrayUtils; 17 import org.apache.commons.lang.StringUtils; 18 import org.displaytag.properties.MediaTypeEnum; 19 import org.displaytag.util.TagConstants; 20 21 22 28 public class AutolinkColumnDecorator implements DisplaytagColumnDecorator 29 { 30 31 34 public static final DisplaytagColumnDecorator INSTANCE = new AutolinkColumnDecorator(); 35 36 39 private static final String URL_DELIM = "://"; 41 44 private static final String [] URLS_PREFIXES = new String []{"http", "https", "ftp"}; 47 50 public Object decorate(Object columnValue, PageContext pageContext, MediaTypeEnum media) 51 { 52 53 if (columnValue == null) 54 { 55 return null; 56 } 57 String work = columnValue.toString(); 58 59 int urlBegin; 60 StringBuffer buffer = new StringBuffer (); 61 62 while ((urlBegin = work.indexOf('@')) != -1) 64 { 65 int start = 0; 66 int end = work.length() - 1; 67 68 for (int j = urlBegin; j >= 0; j--) 70 { 71 if (Character.isWhitespace(work.charAt(j))) 72 { 73 start = j + 1; 74 break; 75 } 76 } 77 78 for (int j = urlBegin; j <= end; j++) 80 { 81 if (Character.isWhitespace(work.charAt(j))) 82 { 83 end = j - 1; 84 break; 85 } 86 } 87 88 String email = work.substring(start, end + 1); 89 90 buffer.append(work.substring(0, start)).append("<a HREF=\"mailto:") .append(email + "\">") .append(email).append("</a>"); 94 if (end == work.length()) 95 { 96 work = TagConstants.EMPTY_STRING; 97 } 98 else 99 { 100 work = work.substring(end + 1); 101 } 102 } 103 104 work = buffer.toString() + work; 105 buffer = new StringBuffer (); 106 107 while ((urlBegin = work.indexOf(URL_DELIM)) != -1) 109 { 110 111 int fullUrlBegin = urlBegin; 113 StringBuffer prefixBuffer = new StringBuffer (10); 114 for (int j = fullUrlBegin - 1; j >= 0; j--) 115 { 116 if (Character.isWhitespace(work.charAt(j))) 117 { 118 fullUrlBegin = j + 1; 119 break; 120 } 121 fullUrlBegin = j; 122 prefixBuffer.append(work.charAt(j)); 123 } 124 125 if (!ArrayUtils.contains(URLS_PREFIXES, StringUtils.reverse(prefixBuffer.toString()))) 126 { 127 128 buffer.append(work.substring(0, urlBegin + 3)); 129 work = work.substring(urlBegin + 3); 130 continue; 131 } 132 133 int urlEnd = work.length(); 134 135 for (int j = urlBegin; j < urlEnd; j++) 137 { 138 if (Character.isWhitespace(work.charAt(j))) 139 { 140 urlEnd = j; 141 break; 142 } 143 } 144 145 String url = work.substring(fullUrlBegin, urlEnd); 146 147 buffer.append(work.substring(0, fullUrlBegin)).append("<a HREF=\"") .append(url).append("\">") .append(url).append("</a>"); 151 if (urlEnd >= work.length()) 152 { 153 work = TagConstants.EMPTY_STRING; 154 } 155 else 156 { 157 work = work.substring(urlEnd); 158 } 159 } 160 161 buffer.append(work); 162 return buffer.toString(); 163 } 164 165 } 166 | Popular Tags |