01: /*
02:
03: This software is OSI Certified Open Source Software.
04: OSI Certified is a certification mark of the Open Source Initiative.
05:
06: The license (Mozilla version 1.0) can be read at the MMBase site.
07: See http://www.MMBase.org/license
08:
09: */
10: package org.mmbase.util.transformers;
11:
12: import java.io.Reader;
13: import java.io.Writer;
14:
15: import org.mmbase.util.logging.*;
16:
17: /**
18: * Replace 1 or white space by 1 space, and all spaces on the begin and end.
19: *
20: * @author Michiel Meeuwissen
21: * @since MMBase-1.7.4
22: */
23:
24: public class SpaceRemover extends ReaderTransformer implements
25: CharTransformer {
26:
27: private static final Logger log = Logging
28: .getLoggerInstance(SpaceRemover.class);
29:
30: public Writer transform(Reader r, Writer w) {
31:
32: try {
33: log.debug("Starting to remove all space.");
34:
35: int c = r.read();
36:
37: // remove all leading space;
38: while (c != -1 && Character.isWhitespace((char) c)) {
39: c = r.read();
40: }
41: // consider the rest;
42: boolean writing = true;
43: while (c != -1) {
44: if (Character.isWhitespace((char) c)) {
45: writing = false;
46: } else {
47: if (!writing) {
48: w.write(' ');
49: writing = true;
50: }
51: w.write(c);
52: }
53: c = r.read();
54: }
55: log.debug("Finished");
56: } catch (java.io.IOException e) {
57: log.error(e.toString());
58: }
59: return w;
60: }
61:
62: public String toString() {
63: return "SPACEREMOVER";
64: }
65: }
|