View Javadoc

1   /*
2    * Copyright (c) 2001 - 2005 ivata limited.
3    * All rights reserved.
4    * -----------------------------------------------------------------------------
5    * ivata masks may be redistributed under the GNU General Public
6    * License as published by the Free Software Foundation;
7    * version 2 of the License.
8    *
9    * These programs are free software; you can redistribute them and/or
10   * modify them under the terms of the GNU General Public License
11   * as published by the Free Software Foundation; version 2 of the License.
12   *
13   * These programs are distributed in the hope that they will be useful,
14   * but WITHOUT ANY WARRANTY; without even the implied warranty of
15   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16   *
17   * See the GNU General Public License in the file LICENSE.txt for more
18   * details.
19   *
20   * If you would like a copy of the GNU General Public License write to
21   *
22   * Free Software Foundation, Inc.
23   * 59 Temple Place - Suite 330
24   * Boston, MA 02111-1307, USA.
25   *
26   *
27   * To arrange commercial support and licensing, contact ivata at
28   *                  http://www.ivata.com/contact.jsp
29   * -----------------------------------------------------------------------------
30   * $Log: LinkFormat.java,v $
31   * Revision 1.3  2005/04/11 14:45:39  colinmacleod
32   * Changed HTMLFormat from an abstract class
33   * into an interface.
34   *
35   * Revision 1.2  2005/04/09 18:04:18  colinmacleod
36   * Changed copyright text to GPL v2 explicitly.
37   *
38   * Revision 1.1  2005/01/06 22:41:01  colinmacleod
39   * Moved up a version number.
40   * Changed copyright notices to 2005.
41   * Updated the documentation:
42   *   - started working on multiproject:site docu.
43   *   - changed the logo.
44   * Added checkstyle and fixed LOADS of style issues.
45   * Added separate thirdparty subproject.
46   * Added struts (in web), util and webgui (in webtheme) from ivata op.
47   *
48   * Revision 1.3  2004/03/21 21:16:37  colinmacleod
49   * Shortened name to ivata op.
50   *
51   * Revision 1.2  2004/02/01 22:07:32  colinmacleod
52   * Added full names to author tags
53   *
54   * Revision 1.1.1.1  2004/01/27 20:59:48  colinmacleod
55   * Moved ivata op to SourceForge.
56   *
57   * Revision 1.2  2003/10/15 14:13:39  colin
58   * Fixes for XDoclet.
59   *
60   * Revision 1.1  2003/02/24 19:33:33  colin
61   * Moved to new subproject.
62   *
63   * Revision 1.3  2003/02/04 17:43:46  colin
64   * copyright notice
65   *
66   * Revision 1.2  2002/06/21 14:50:09  colin
67   * fixed header documentation
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         // find all occurrences of the protocol
111         while ((index = hTMLText.indexOf(protocol, indexLast)) != -1) {
112             // append the string between this and the last protocol
113             if (index != 0) {
114                 returnBuffer.append(hTMLText.substring(indexLast, index));
115             }
116             // store the start: we will use it to substring the URL out
117             int indexStart = index;
118             index += protocol.length();
119             // find the first space after the end of the URL
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             // see if the string is Outlook format, with the URL in <>
128             // afterwards,
129             if ((newIndex = hTMLText.indexOf("<"
130                     + sURL
131                     + ">", indexLast)) != -1) {
132                 int newIndexLast = newIndex + sURL.length() + 2;
133                 // check there is only whitespace between the two links
134                 while (--newIndex >= indexLast) {
135                     if (!Character.isWhitespace(hTMLText.charAt(newIndex))) {
136                         break;
137                     }
138                 }
139                 // if we checked all the chars and they were all whitespace,
140                 // ignore this outlook link
141                 if (newIndex < indexLast) {
142                     indexLast = newIndexLast;
143                 }
144             }
145             // surround with the IIKEEP: tag so that it does not get converted
146             // later
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