001: /*
002: * Copyright (c) 2001 - 2005 ivata limited.
003: * All rights reserved.
004: * -----------------------------------------------------------------------------
005: * ivata masks may be redistributed under the GNU General Public
006: * License as published by the Free Software Foundation;
007: * version 2 of the License.
008: *
009: * These programs are free software; you can redistribute them and/or
010: * modify them under the terms of the GNU General Public License
011: * as published by the Free Software Foundation; version 2 of the License.
012: *
013: * These programs are distributed in the hope that they will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
016: *
017: * See the GNU General Public License in the file LICENSE.txt for more
018: * details.
019: *
020: * If you would like a copy of the GNU General Public License write to
021: *
022: * Free Software Foundation, Inc.
023: * 59 Temple Place - Suite 330
024: * Boston, MA 02111-1307, USA.
025: *
026: *
027: * To arrange commercial support and licensing, contact ivata at
028: * http://www.ivata.com/contact.jsp
029: * -----------------------------------------------------------------------------
030: * $Log: SearchReplaceFormat.java,v $
031: * Revision 1.5 2005/10/03 10:17:25 colinmacleod
032: * Fixed some style and javadoc issues.
033: *
034: * Revision 1.4 2005/10/02 14:06:33 colinmacleod
035: * Added/improved log4j logging.
036: *
037: * Revision 1.3 2005/04/11 14:45:40 colinmacleod
038: * Changed HTMLFormat from an abstract class
039: * into an interface.
040: *
041: * Revision 1.2 2005/04/09 18:04:18 colinmacleod
042: * Changed copyright text to GPL v2 explicitly.
043: *
044: * Revision 1.1 2005/01/06 22:41:01 colinmacleod
045: * Moved up a version number.
046: * Changed copyright notices to 2005.
047: * Updated the documentation:
048: * - started working on multiproject:site docu.
049: * - changed the logo.
050: * Added checkstyle and fixed LOADS of style issues.
051: * Added separate thirdparty subproject.
052: * Added struts (in web), util and webgui (in webtheme) from ivata op.
053: *
054: * Revision 1.3 2004/03/21 21:16:37 colinmacleod
055: * Shortened name to ivata op.
056: *
057: * Revision 1.2 2004/02/01 22:07:32 colinmacleod
058: * Added full names to author tags
059: *
060: * Revision 1.1.1.1 2004/01/27 20:59:48 colinmacleod
061: * Moved ivata op to SourceForge.
062: *
063: * Revision 1.2 2003/10/15 14:13:39 colin
064: * Fixes for XDoclet.
065: *
066: * Revision 1.2 2003/02/28 07:27:32 colin
067: * fixed - no longer uses deliminators (StringTokenizer)
068: *
069: * Revision 1.1 2003/02/24 19:33:33 colin
070: * Moved to new subproject.
071: *
072: * Revision 1.1 2003/02/20 12:48:41 colin
073: * first version
074: * -----------------------------------------------------------------------------
075: */
076: package com.ivata.mask.web.format;
077:
078: import org.apache.log4j.Logger;
079:
080: /**
081: * <p>
082: * Replace all occurrences of one string with another.
083: * </p>
084: *
085: * @since ivata masks 0.4 (2003-02-09)
086: * @author Colin MacLeod
087: * <a href='mailto:colin.macleod@ivata.com'>colin.macleod@ivata.com</a>
088: * @version $Revision: 1.5 $
089: */
090: public class SearchReplaceFormat implements HTMLFormat {
091: /**
092: * Logger for this class.
093: */
094: private static final Logger logger = Logger
095: .getLogger(SearchReplaceFormat.class);
096:
097: /**
098: * <p>
099: * Represents the string which will be searched for.
100: * </p>
101: */
102: private String search;
103: /**
104: * <p>
105: * Represents the string which will replace the searched for string.
106: * </p>
107: */
108: private String replace;
109:
110: /**
111: * <p>
112: * Format the string given in <code>hTMLText</code> replacing all
113: * occurrences of the string set by <code>setSearch</code> with the string
114: * set by <code>setReplace</code>.
115: * </p>
116: *
117: * @param hTMLText
118: * the text to search and replace.
119: * @return <copyDoc>Refer to {@link HTMLFormat#format}.</copyDoc>
120: */
121: public final String format(final String hTMLText) {
122: if (logger.isDebugEnabled()) {
123: logger.debug("format(String hTMLText = " + hTMLText
124: + ") - start");
125: }
126:
127: if (search == null) {
128: throw new NullPointerException(
129: "ERROR in SearchReplaceFormat: search string is null.");
130: }
131: // if the replace string is null, interpret that as empty
132: if (replace == null) {
133: replace = "";
134: }
135: StringBuffer buffer = new StringBuffer();
136: int index, indexBefore = 0, len = search.length();
137: while ((index = hTMLText.indexOf(search, indexBefore)) != -1) {
138: buffer.append(hTMLText.substring(indexBefore, index));
139: buffer.append(replace);
140: indexBefore = index + len;
141: }
142: buffer.append(hTMLText.substring(indexBefore));
143: String returnString = buffer.toString();
144: if (logger.isDebugEnabled()) {
145: logger.debug("format(String) - end - return value = "
146: + returnString);
147: }
148: return returnString;
149: }
150:
151: /**
152: * <p>
153: * Represents the string which will be searched for.
154: * </p>
155: *
156: * @return the current value of search.
157: */
158: public final String getSearch() {
159: if (logger.isDebugEnabled()) {
160: logger.debug("getSearch() - start");
161: }
162:
163: if (logger.isDebugEnabled()) {
164: logger
165: .debug("getSearch() - end - return value = "
166: + search);
167: }
168: return search;
169: }
170:
171: /**
172: * <p>
173: * Represents the string which will be searched for.
174: * </p>
175: *
176: * @param searchParam
177: * the new value of search.
178: */
179: public final void setSearch(final String searchParam) {
180: if (logger.isDebugEnabled()) {
181: logger.debug("setSearch(String searchParam = "
182: + searchParam + ") - start");
183: }
184:
185: this .search = searchParam;
186:
187: if (logger.isDebugEnabled()) {
188: logger.debug("setSearch(String) - end");
189: }
190: }
191:
192: /**
193: * <p>
194: * Represents the string which will replace the searched for string.
195: * </p>
196: *
197: * @return the current value of replace.
198: */
199: public final String getReplace() {
200: if (logger.isDebugEnabled()) {
201: logger.debug("getReplace() - start");
202: }
203:
204: if (logger.isDebugEnabled()) {
205: logger.debug("getReplace() - end - return value = "
206: + replace);
207: }
208: return replace;
209: }
210:
211: /**
212: * <p>
213: * Represents the string which will replace the searched for string.
214: * </p>
215: *
216: * @param replaceParam
217: * the new value of replace.
218: */
219: public final void setReplace(final String replaceParam) {
220: if (logger.isDebugEnabled()) {
221: logger.debug("setReplace(String replaceParam = "
222: + replaceParam + ") - start");
223: }
224:
225: this .replace = replaceParam;
226:
227: if (logger.isDebugEnabled()) {
228: logger.debug("setReplace(String) - end");
229: }
230: }
231: }
|