001: /*
002:
003: This software is OSI Certified Open Source Software.
004: OSI Certified is a certification mark of the Open Source Initiative.
005:
006: The license (Mozilla version 1.0) can be read at the MMBase site.
007: See http://www.MMBase.org/license
008:
009: */
010: package org.mmbase.util;
011:
012: import org.mmbase.util.logging.Logger;
013: import org.mmbase.util.logging.Logging;
014:
015: /**
016: * Class to strip characters from the beginning and end of strings.
017: *
018: * <PRE>
019: * Example1: Strip.Char("..dfld..",'.',Strip.TRAILING) yields "..dlfd."
020: * Example2: Strip.Chars("..dfld..",".",Strip.TRAILING) yields "..dlfd"
021: * Example3: Strip.Chars(". .. dfld. , .","., ",Strip.BOTH) yields "dfld"
022: * </PRE>
023: *
024: * @author Rico Jansen
025: * @version $Id: Strip.java,v 1.7 2004/09/30 14:07:13 pierre Exp $
026: */
027: public class Strip {
028:
029: // logger
030: private static Logger log = Logging.getLoggerInstance(Strip.class
031: .getName());
032:
033: /**
034: * Strip nothing, a rather ineffecient form of a copy
035: */
036: public static final int NOTHING = 0;
037:
038: /**
039: * Strip leading, only characters at begin of string are checked
040: */
041: public static final int LEADING = 1;
042:
043: /**
044: * Strip trailing, only characters at end of string are checked
045: */
046: public static final int TRAILING = 2;
047:
048: /**
049: * Strip both, characters at begin and end of string are checked
050: */
051: public static final int BOTH = 3;
052:
053: /**
054: * Strip double quotes from beginning, end or both, only once.
055: * @param str the string to strip
056: * @param where one of {@link #NOTHING}, {@link #LEADING}, {@link #TRAILING}
057: * or {@link #BOTH}
058: * @return the stripped String
059: */
060: public static String DoubleQuote(String str, int where) {
061: return Char(str, '"', where);
062: }
063:
064: /**
065: * Strip single quotes from beginning, end or both, only once.
066: * @param str the string to strip
067: * @param where one of {@link #NOTHING}, {@link #LEADING}, {@link #TRAILING}
068: * or {@link #BOTH}
069: * @return the stripped String
070: */
071: public static String SingleQuote(String str, int where) {
072: return Char(str, '\'', where);
073: }
074:
075: /**
076: * Strip multiple whitespace characters from beginning, end or both, that
077: * means keep on stripping util a non-whitespace character is found.
078: * @param str the string to strip
079: * @param where one of {@link #NOTHING}, {@link #LEADING}, {@link #TRAILING}
080: * or {@link #BOTH}
081: * @return the stripped String
082: */
083: public static String Whitespace(String str, int where) {
084: return Chars(str, " \t\n\r", where);
085: }
086:
087: /**
088: * Strip all of the specified character from beginning, end or both.
089: * @param str the string to strip
090: * @param chr the character to strip from the string
091: * @param where one of {@link #NOTHING}, {@link #LEADING}, {@link #TRAILING}
092: * or {@link #BOTH}
093: * @return the stripped String
094: */
095: public static String Char(String str, char chr, int where) {
096: if (str != null && str.length() > 0) {
097: int lead = 0;
098: int trail = str.length() - 1;
099:
100: switch (where) {
101: case LEADING:
102: if (str.charAt(lead) == chr)
103: lead++;
104: break;
105: case TRAILING:
106: if (str.charAt(trail) == chr)
107: trail--;
108: break;
109: case BOTH:
110: if (str.charAt(lead) == chr)
111: lead++;
112: if (str.charAt(trail) == chr)
113: trail--;
114: break;
115: default:
116: break;
117: }
118: str = str.substring(lead, trail + 1);
119: }
120: return str;
121: }
122:
123: /**
124: * Strip multiple characters contained in the set given as second parameter
125: * until a non-set character.
126: * @param str the string to strip
127: * @param chars a string containing all characters to strip from the string
128: * @param where one of {@link #NOTHING}, {@link #LEADING}, {@link #TRAILING}
129: * or {@link #BOTH}
130: * @return the stripped String
131: */
132: public static String Chars(String str, String chars, int where) {
133:
134: if (str != null && str.length() > 0) {
135: int lead = 0;
136: int trail = str.length() - 1;
137:
138: if (trail < 1) {
139: where = LEADING;
140: } else {
141: switch (where) {
142: case LEADING:
143: while (chars.indexOf(str.charAt(lead)) != -1
144: && (lead < str.length() - 1))
145: lead++;
146: break;
147: case TRAILING:
148: while (chars.lastIndexOf(str.charAt(trail)) != -1
149: && trail > 0)
150: trail--;
151: break;
152: case BOTH:
153: while (chars.indexOf(str.charAt(lead)) != -1
154: && lead < (str.length() - 1))
155: lead++;
156: while (chars.lastIndexOf(str.charAt(trail)) != -1
157: && trail >= lead)
158: trail--;
159: break;
160: default:
161: break;
162: }
163: }
164: if (lead <= trail) {
165: str = str.substring(lead, trail + 1);
166: } else {
167: str = "";
168: }
169: }
170: return str;
171: }
172:
173: /**
174: * Test the class
175: */
176: public static void main(String args[]) {
177: log.info("Double "
178: + Strip.DoubleQuote("\"double\"", Strip.BOTH));
179: log.info("Single " + Strip.SingleQuote("'single'", Strip.BOTH));
180: log.info("White |"
181: + Strip.Whitespace(" white \n", Strip.BOTH)
182: + "|");
183: }
184: }
|