001: package net.firstpartners.nounit.utility;
002:
003: /**
004: * Title: NoUnit - Identify Classes that are not being unit Tested
005: *
006: * Copyright (C) 2001 Paul Browne , FirstPartners.net
007: *
008: *
009: * This program is free software; you can redistribute it and/or
010: * modify it under the terms of the GNU General Public License
011: * as published by the Free Software Foundation; either version 2
012: * of the License, or (at your option) any later version.
013: *
014: * This program is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
017: * GNU General Public License for more details.
018: *
019: * You should have received a copy of the GNU General Public License
020: * along with this program; if not, write to the Free Software
021: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
022: *
023: * @author Paul Browne
024: * @version 0.7
025: */
026:
027: /**
028: * Performs common , but tricky tasks.
029: * e.g. Stripping of spaces from Files
030: */
031: public class TextUtil {
032:
033: /**
034: * Replace the first occurrence of <code>oldSubstring</code> in
035: * <code>string</code>, if there is one, with <code>newSubstring</code>.
036: *
037: * @param string - Replace a substring of this String
038: * @param oldSubstring - The substring of <code>string</code> to be replaced
039: * @param newSubstring - The string to put into <code> string</code>
040: * @return A new String which is a copy of <code>string</code> with the first
041: * occurrence of <code>oldSubstring</code> in <code>string</code>, if
042: * there is one, with <code>newSubstring</code>.
043: * Returns null if <code>string</code> is null.
044: * Returns <code>string</code> if either substring is null or
045: * <code>oldSubstring</code> is empty
046: */
047: public static String replace(String string, String oldSubstring,
048: String newSubstring) {
049: String result = string;
050:
051: if ((string != null) && (string.length() > 0)
052: && (oldSubstring != null)
053: && (oldSubstring.length() > 0)
054: && (newSubstring != null)) {
055: int pos = string.indexOf(oldSubstring);
056: result = string.substring(0, pos) + newSubstring
057: + string.substring(pos + oldSubstring.length());
058: }
059:
060: return result;
061: }
062:
063: /**
064: * Replaces all occurrences of <code>oldSubstring</code> in
065: * <code>string</code>, if there are any, with <code>newSubstring</code>.
066: *
067: * @param string - Replace substrings of this String
068: * @param oldSubstring - The substring of <code>string</code> to be replaced
069: * @param newSubstring - The string to put into <code> string</code>
070: * @return A new String which is a copy of <code>string</code> with all
071: * occurrences of <code>oldSubstring</code> in <code>string</code>,
072: * if there are any, with <code>newSubstring</code>.
073: * Returns null if <code>string</code> is null.
074: * Returns <code>string</code> if either substring is null or
075: * <code>oldSubstring</code> is empty
076: */
077: public static String replaceAll(String string, String oldSubstring,
078: String newSubstring) {
079: //Local Variables
080: String result = string;
081:
082: if ((result != null) && (result.length() > 0)
083: && (result.indexOf(oldSubstring) > -1)
084: && (oldSubstring.length() > 0)
085: && (!oldSubstring.equals(newSubstring))) {
086:
087: while (result.indexOf(oldSubstring) > -1) {
088: result = replace(result, oldSubstring, newSubstring);
089: }
090: }
091:
092: return result;
093: }
094:
095: /**
096: * Finds (start and end) markers in piece of text
097: * extracts text (note including markers) in between
098: * @param fullText to search in
099: * @param startIndex ignore text before this point
100: * @param startMarker start marker for the piece of text to extract
101: * @param endMarker end marker
102: * @return foundText , empty String if nothing found
103: */
104: public static String find(String fullText, int startIndex,
105: String startMarker, String endMarker) {
106:
107: //Local Variables
108: int startPlace = 0;
109: int endPlace = 0;
110: String foundText = "";
111:
112: //Find the first instance of text
113: startPlace = fullText.indexOf(startMarker, startIndex)
114: + startMarker.length();
115:
116: //Find the first instance of end marker after this
117: if (startPlace > startIndex) {
118: startIndex = startPlace;
119: }
120: endPlace = fullText.indexOf(endMarker, startIndex);
121:
122: //Copy and return
123: try {
124: if ((startPlace > -1) || (endPlace > -1)) {
125: foundText = fullText.substring(startPlace, endPlace);
126: }
127: } catch (java.lang.StringIndexOutOfBoundsException sioobe) {
128: // do nothing - will return default of empty string
129: }
130:
131: //Ensure that there are no dodgy strings..
132: if (startPlace < startIndex) {
133: foundText = "";
134: }
135:
136: return foundText;
137: }
138:
139: /**
140: * Remove all instances of input string from Output
141: * @param inputString
142: * @param removeString
143: * @return updateString
144: */
145: public static String removeAll(String inputString,
146: String removeString) {
147:
148: //Internal Variables
149: StringBuffer updateString = new StringBuffer();
150: String tmpString;
151:
152: for (int a = 0; a < inputString.length(); a++) {
153:
154: tmpString = inputString.substring(a, a + 1);
155: if (!tmpString.equals(removeString)) {
156: updateString.append(tmpString);
157: }
158:
159: }
160:
161: return updateString.toString();
162:
163: }
164:
165: /**
166: * Strip out any trailing characters
167: * @param inString to be operated on
168: * @param toRemove string to remove at end if found
169: * @return inString with end removed
170: */
171: public static String removeTrailing(String inString, String toRemove) {
172: while (inString.endsWith(toRemove)) {
173: inString = inString.substring(0, inString.length()
174: - toRemove.length());
175: }
176:
177: return inString;
178:
179: }
180:
181: }
|