1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70 package com.ivata.mask.web.format;
71 /***
72 * <p>
73 * This class converts URLs in the text into HTML anchor links.
74 * </p>
75 *
76 * @since ivata masks 0.4 (2002-06-19)
77 * @author Colin MacLeod
78 * <a href='mailto:colin.macleod@ivata.com'>colin.macleod@ivata.com</a>
79 * @version $Revision: 1.3 $
80 */
81 public class LinkFormat implements HTMLFormat {
82 /***
83 * <p>
84 * Set by <code>HTMLFormatter</code> if HTML entities are converted (by
85 * <code>HTMLEntityFormat</code>) after this format is applied. This
86 * ensures that our tags are marked to keep, so they do not get converted.
87 * </p>
88 */
89 private boolean convertHTMLEntities = false;
90 /***
91 * <p>
92 * Helper method. The string <code>hTMLText</code> is searched for URLs
93 * which begin with the protocol text provided, and all are converted to
94 * anchor links.
95 * </p>
96 *
97 * @param hTMLText
98 * the text to convert.
99 * @param protocol
100 * the protocol to look for.This can be "http://", for example.
101 * @return a string with all of the URLs of the given protocol converted to
102 * HTML anchor links.
103 */
104 private String convertOneURLType(final String hTMLText,
105 final String protocol) {
106 int index = 0;
107 int indexLast = 0;
108 StringBuffer returnBuffer = new StringBuffer();
109 String sURL;
110
111 while ((index = hTMLText.indexOf(protocol, indexLast)) != -1) {
112
113 if (index != 0) {
114 returnBuffer.append(hTMLText.substring(indexLast, index));
115 }
116
117 int indexStart = index;
118 index += protocol.length();
119
120 int length = hTMLText.length();
121 while ((index < length) && isURLCharacter(hTMLText.charAt(index))) {
122 ++index;
123 }
124 sURL = hTMLText.substring(indexStart, index);
125 indexLast = index;
126 int newIndex;
127
128
129 if ((newIndex = hTMLText.indexOf("<"
130 + sURL
131 + ">", indexLast)) != -1) {
132 int newIndexLast = newIndex + sURL.length() + 2;
133
134 while (--newIndex >= indexLast) {
135 if (!Character.isWhitespace(hTMLText.charAt(newIndex))) {
136 break;
137 }
138 }
139
140
141 if (newIndex < indexLast) {
142 indexLast = newIndexLast;
143 }
144 }
145
146
147 if (convertHTMLEntities) {
148 returnBuffer.append("<IIKEEP:>");
149 }
150 returnBuffer.append("<a href='");
151 returnBuffer.append(sURL);
152 returnBuffer.append("' target='_blank'>");
153 returnBuffer.append(sURL);
154 returnBuffer.append("</a>");
155 if (convertHTMLEntities) {
156 returnBuffer.append("</IIKEEP:>");
157 }
158 }
159 returnBuffer.append(hTMLText.substring(indexLast));
160 return returnBuffer.toString();
161 }
162 /***
163 * <p>
164 * Convert all URLs in the text provided into HTML 'anchor' links.
165 * Currently, this method knows and converts URLs beginning with: <br/>
166 * <ul>
167 * <li>http://</li>
168 * <li>https://</li>
169 * <li>ftp://</li>
170 * </ul>
171 * </p>
172 *
173 * @param hTMLTextParam
174 * HTML text to convert URLs in.
175 * @return formatted text, with all of the URLs converted to HTML anchor
176 * tags.
177 */
178 public final String format(final String hTMLTextParam) {
179 String hTMLText = hTMLTextParam;
180 hTMLText = convertOneURLType(hTMLText, "http://");
181 hTMLText = convertOneURLType(hTMLText, "https://");
182 hTMLText = convertOneURLType(hTMLText, "ftp://");
183 return hTMLText;
184 }
185 /***
186 * <p>
187 * Check to see if a character us allowed as part of a URL string.
188 * </p>
189 *
190 * @param checkChar
191 * character to check
192 * @return <code>true</code> if the character may be in a URL, otherwise
193 * <code>false</code>.
194 */
195 private boolean isURLCharacter(final char checkChar) {
196 return (((checkChar >= 'a') && (checkChar <= 'z'))
197 || ((checkChar >= 'A') && (checkChar <= 'Z'))
198 || ((checkChar >= '0') && (checkChar <= '9'))
199 || (checkChar == '%') || (checkChar == '/')
200 || (checkChar == '-') || (checkChar == '_')
201 || (checkChar == ':') || (checkChar == '?')
202 || (checkChar == '.') || (checkChar == ',')
203 || (checkChar == '&') || (checkChar == '#')
204 || (checkChar == '@') || (checkChar == '='));
205 }
206 /***
207 * Allows <code>HTMLFormat</code> to tell this format it should convert
208 * HTML character entities in the display part of the link.
209 *
210 * @param convertHTMLEntitiesParam
211 * if <code>true</code> then character entities will be
212 * converted.
213 */
214 final void setConvertHTMLEntities(final boolean convertHTMLEntitiesParam) {
215 convertHTMLEntities = convertHTMLEntitiesParam;
216 }
217 }
218