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: SearchReplaceFormat.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.2 2003/02/28 07:27:32 colin
61 * fixed - no longer uses deliminators (StringTokenizer)
62 *
63 * Revision 1.1 2003/02/24 19:33:33 colin
64 * Moved to new subproject.
65 *
66 * Revision 1.1 2003/02/20 12:48:41 colin
67 * first version
68 * -----------------------------------------------------------------------------
69 */
70 package com.ivata.mask.web.format;
71 /***
72 * <p>
73 * Replace all occurrences of one string with another.
74 * </p>
75 *
76 * @since ivata masks 0.4 (2003-02-09)
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 SearchReplaceFormat implements HTMLFormat {
82 /***
83 * <p>
84 * Represents the string which will be searched for.
85 * </p>
86 */
87 private String search;
88 /***
89 * <p>
90 * Represents the string which will replace the searched for string.
91 * </p>
92 */
93 private String replace;
94 /***
95 * <p>
96 * Format the string given in <code>hTMLText</code> replacing all
97 * occurrences of the string set by <code>setSearch</code> with the string
98 * set by <code>setReplace</code>.
99 * </p>
100 *
101 * @param hTMLText
102 * the text to search and replace.
103 * @return Refer to {@link HTMLFormat#format}.
104 */
105 public final String format(final String hTMLText) {
106 if (search == null) {
107 throw new NullPointerException(
108 "ERROR in SearchReplaceFormat: search string is null.");
109 }
110 // if the replace string is null, interpret that as empty
111 if (replace == null) {
112 replace = "";
113 }
114 StringBuffer buffer = new StringBuffer();
115 int index, indexBefore = 0, len = search.length();
116 while ((index = hTMLText.indexOf(search, indexBefore)) != -1) {
117 buffer.append(hTMLText.substring(indexBefore, index));
118 buffer.append(replace);
119 indexBefore = index + len;
120 }
121 buffer.append(hTMLText.substring(indexBefore));
122 return buffer.toString();
123 }
124 /***
125 * <p>
126 * Represents the string which will be searched for.
127 * </p>
128 *
129 * @return the current value of search.
130 */
131 public final String getSearch() {
132 return search;
133 }
134 /***
135 * <p>
136 * Represents the string which will be searched for.
137 * </p>
138 *
139 * @param searchParam
140 * the new value of search.
141 */
142 public final void setSearch(final String searchParam) {
143 this.search = searchParam;
144 }
145 /***
146 * <p>
147 * Represents the string which will replace the searched for string.
148 * </p>
149 *
150 * @return the current value of replace.
151 */
152 public final String getReplace() {
153 return replace;
154 }
155 /***
156 * <p>
157 * Represents the string which will replace the searched for string.
158 * </p>
159 *
160 * @param replaceParam
161 * the new value of replace.
162 */
163 public final void setReplace(final String replaceParam) {
164 this.replace = replaceParam;
165 }
166 }
167