blob: 4dc19e46c0f560cad6b3e7b778c19b8b6226d2d7 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
/*******************************************************************************
* Copyright (c) 2004, 2008 John Krasnay and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* John Krasnay - initial API and implementation
* David Carver unit tests fixes
* Igor Jacy Lino Campista - Java 5 warnings fixed (bug 311325)
*******************************************************************************/
package org.eclipse.wst.xml.vex.core.internal.dom;
import java.util.ArrayList;
import java.util.List;
/**
* Wraps text to a given width.
*/
public class TextWrapper {
private List<String> parts = new ArrayList<String>();
private boolean lastIsWhite = true;
/**
* Class constructor.
*/
public TextWrapper() {
}
/**
* Adds text to the list of things to be wrapped.
*
* @param s
* Text to be added.
*/
public void add(String s) {
int i = 0;
int j = 0;
boolean thisIsWhite = true;
while (j < s.length()) {
// skip non-whitespace
while (j < s.length() && !Character.isWhitespace(s.charAt(j))) {
j++;
thisIsWhite = false;
}
// skip whitespace
while (j < s.length() && Character.isWhitespace(s.charAt(j))) {
j++;
thisIsWhite = true;
}
if (lastIsWhite || parts.isEmpty()) {
this.parts.add(s.substring(i, j));
}
else if (!parts.isEmpty()) {
this.parts.add(((String) this.parts
.remove(this.parts.size() - 1))
+ s.substring(i, j));
}
i = j;
lastIsWhite = thisIsWhite;
}
}
/**
* Adds text to the list of things to be wrapped. The given text will be
* treated as a single unit and will not be split across lines.
*
* @param s
* Text to be added.
*/
public void addNoSplit(String s) {
this.parts.add(s);
}
/**
* Clears any added text.
*/
public void clear() {
this.parts.clear();
}
/**
* Wraps the text into the given width. The text is only broken at spaces,
* meaning the returned lines will not necessarily fit within width.
*
* @param width
*/
public String[] wrap(int width) {
List<String> lines = new ArrayList<String>();
StringBuffer line = new StringBuffer();
for (String s : this.parts) {
if (line.length() > 0 && line.length() + s.length() > width) {
// part won't fit on the current line
lines.add(line.toString());
line.setLength(0);
if (s.length() > width) {
lines.add(s);
} else {
line.append(s);
}
} else {
line.append(s);
}
}
if (line.length() > 0) {
lines.add(line.toString());
}
return (String[]) lines.toArray(new String[lines.size()]);
}
}
|