01: package de.regnis.q.sequence.line.simplifier;
02:
03: /**
04: * @author Marc Strapetz
05: */
06: public class QSequenceLineWhiteSpaceReducingSimplifier implements
07: QSequenceLineSimplifier {
08:
09: // Static =================================================================
10:
11: public static String reduceWhiteSpaces(String text) {
12: final StringBuffer buffer = new StringBuffer();
13: boolean lastWasWhiteSpace = false;
14: for (int index = 0; index < text.length(); index++) {
15: final char ch = text.charAt(index);
16: if (ch != '\n' && ch != '\r' && Character.isWhitespace(ch)) {
17: lastWasWhiteSpace = true;
18: } else {
19: if (lastWasWhiteSpace) {
20: buffer.append(' ');
21: lastWasWhiteSpace = false;
22: }
23: buffer.append(ch);
24: }
25: }
26:
27: if (lastWasWhiteSpace) {
28: buffer.append(' ');
29: }
30:
31: return buffer.toString();
32: }
33:
34: // Implemented ============================================================
35:
36: public byte[] simplify(byte[] bytes) {
37: return reduceWhiteSpaces(new String(bytes)).getBytes();
38: }
39: }
|