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: StripTagFormat.java,v $
31   * Revision 1.3  2005/04/11 14:45:40  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.2  2003/02/04 17:43:46  colin
64   * copyright notice
65   *
66   * Revision 1.1  2002/06/28 13:26:12  colin
67   * created to strip out style tags and tested on library noticeboard
68   * -----------------------------------------------------------------------------
69   */
70  package com.ivata.mask.web.format;
71  /***
72   * <p>
73   * This class defines a standard way of stripping unwanted tags out of HTML. It
74   * looks for tags and end tags and strips out everything in between. Your tag
75   * must be 'closed', XML-style.
76   * </p>
77   *
78   * @since ivata masks 0.4 (2002-06-19)
79   * @author Colin MacLeod
80   * <a href='mailto:colin.macleod@ivata.com'>colin.macleod@ivata.com</a>
81   * @version $Revision: 1.3 $
82   */
83  public class StripTagFormat implements HTMLFormat {
84      /***
85       * Select the tag name of the tag you want to strip out of the string.
86       */
87      private String tagName = null;
88      /***
89       * <p>
90       * Takes the text provided and strips all of the tags out, as set by
91       * <code>setTagName</code>. If no tag name has been set, this method
92       * returns the HTML text unaltered.
93       * </p>
94       *
95       * @param hTMLTextParam
96       *            Refer to {@link com.ivata.mask.web.format.HTMLFormat#format}.
97       * @return Refer to {@link com.ivata.mask.web.format.HTMLFormat#format}.
98       */
99      public final String format(final String hTMLTextParam) {
100         String hTMLText = hTMLTextParam;
101         // first prerequisites: is there a tag name
102         if (tagName == null) {
103             return hTMLText;
104         }
105         // OK, so we got a tag name, see if we can find the opening tag in the
106         // text
107         int openTagStartPos, openTagEndPos, closeTagPos;
108         // make these strings here to save string concatenation on each run of
109         // the loop
110         // make the tag name all lower case
111         tagName = tagName.toLowerCase();
112         // make a new string to search which is a lower case equivalent of the
113         // original HTML string
114         String searchText = hTMLText.toLowerCase();
115         String openTagStart = "<" + tagName;
116         String closeTag = "</" + tagName + ">";
117         // keep going as long as we find new occurrences of the tag
118         while ((openTagStartPos = searchText.indexOf(openTagStart)) != -1) {
119             // now find the end of the open tag
120             openTagEndPos = searchText.indexOf('>', openTagStartPos);
121             // if that didn't find anything return the string unaltered
122             if (openTagEndPos == -1) {
123                 break;
124             }
125             // see if this tag is closed
126             if (searchText.charAt(openTagEndPos - 1) == '/') {
127                 // great! all we have to do is remove the tag
128                 hTMLText = hTMLText.substring(0, openTagStartPos)
129                         + hTMLText.substring(openTagEndPos + 1);
130                 searchText = searchText.substring(0, openTagStartPos)
131                         + searchText.substring(openTagEndPos + 1);
132             } else {
133                 // otherwise we need to find the closing tag
134                 closeTagPos = searchText.indexOf(closeTag);
135                 // if we don't find the close tag, just remove the open tag
136                 if (closeTagPos == -1) {
137                     hTMLText = hTMLText.substring(0, openTagStartPos)
138                             + hTMLText.substring(openTagEndPos + 1);
139                     searchText = searchText.substring(0, openTagStartPos)
140                             + searchText.substring(openTagEndPos + 1);
141                 } else {
142                     // otherwise, remove everything in between as well
143                     hTMLText = hTMLText.substring(0, openTagStartPos)
144                             + hTMLText.substring(closeTagPos
145                                     + closeTag.length());
146                     searchText = searchText.substring(0, openTagStartPos)
147                             + searchText.substring(closeTagPos
148                                     + closeTag.length());
149                 }
150             }
151         }
152         return hTMLText;
153     }
154     /***
155      * Select the tag name of the tag you want to strip out of the string.
156      *
157      * @return the current value of tagName.
158      */
159     public final String getTagName() {
160         return tagName;
161     }
162     /***
163      * Select the tag name of the tag you want to strip out of the string.
164      *
165      * @param tagNameParam
166      *            the new value of tagName.
167      */
168     public final void setTagName(final String tagNameParam) {
169         this.tagName = tagNameParam;
170     }
171 }
172