Skip to main content
summaryrefslogtreecommitdiffstats
blob: a7b6f66bf947cd44e1b6bc7fe5c4092e04c3e4e4 (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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*******************************************************************************
 * Copyright (c) 2004 IBM Corporation 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:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.wst.html.core.internal.format;



final class SpaceConverter {

	private String source = null;
	private int length = 0;
	private int startOffset = 0;
	private int endOffset = 0;
	private int spaceCount = 0;
	private int wordCount = 0;
	private StringBuffer buffer = null;
	private int lastOffset = 0;
	private boolean keepBlankLines = false;

	/**
	 */
	public SpaceConverter(String source) {
		super();

		if (source == null) {
			this.source = new String();
		}
		else {
			this.source = source;
			this.length = source.length();
		}
	}

	/**
	 */
	public SpaceConverter(String source, boolean keepBlankLines) {
		super();

		if (source == null) {
			this.source = new String();
		}
		else {
			this.source = source;
			this.length = source.length();
		}
		this.keepBlankLines = keepBlankLines;
	}

	/**
	 */
	public void compressSpaces() {
		if (this.spaceCount == 0)
			return;
		if (this.spaceCount == 1 && this.source.charAt(this.startOffset) == ' ')
			return;

		if (this.buffer == null)
			this.buffer = new StringBuffer();
		if (this.startOffset > this.lastOffset) {
			this.buffer.append(this.source.substring(this.lastOffset, this.startOffset));
		}

		this.buffer.append(' ');

		this.lastOffset = this.startOffset + this.spaceCount;
	}

	/**
	 */
	public String getSource() {
		if (this.buffer == null)
			this.buffer = new StringBuffer();
		if (this.length > this.lastOffset) {
			this.buffer.append(this.source.substring(this.lastOffset, this.length));
		}
		return this.buffer.toString();
	}

	/**
	 */
	public int getSpaceCount() {
		return this.spaceCount;
	}

	/**
	 */
	public boolean hasSpaces() {
		return (this.spaceCount > 0);
	}

	/**
	 */
	public boolean isModified() {
		return (this.buffer != null);
	}

	/**
	 * Add number of the old blank lines to new space string
	 */
	private static String mergeBlankLines(String newSpaces, String oldSpaces) {
		if (newSpaces == null || newSpaces.length() == 0)
			return newSpaces;
		if (oldSpaces == null)
			return newSpaces;

		// count old new lines
		int newLineCount = 0;
		int oldLength = oldSpaces.length();
		for (int i = 0; i < oldLength; i++) {
			char c = oldSpaces.charAt(i);
			if (c == '\r') {
				newLineCount++;
				if (i + 1 < oldLength) {
					c = oldSpaces.charAt(i + 1);
					if (c == '\n')
						i++;
				}
			}
			else {
				if (c == '\n')
					newLineCount++;
			}
		}
		if (newLineCount < 2)
			return newSpaces; // no blank line

		// here assuming newSpaces starts with a new line if any
		String delim = null;
		char d = newSpaces.charAt(0);
		if (d == '\r') {
			if (newSpaces.length() > 1 && newSpaces.charAt(1) == '\n')
				delim = "\r\n";//$NON-NLS-1$
			else
				delim = "\r";//$NON-NLS-1$
		}
		else {
			if (d == '\n')
				delim = "\n";//$NON-NLS-1$
			else
				return newSpaces; // no new line
		}

		newLineCount--;
		StringBuffer buffer = new StringBuffer(newSpaces.length() + newLineCount * 2);
		while (newLineCount > 0) {
			buffer.append(delim);
			newLineCount--;
		}
		buffer.append(newSpaces);
		return buffer.toString();
	}

	/**
	 */
	public int nextWord() {
		if (this.endOffset == this.length) {
			this.startOffset = this.endOffset;
			this.spaceCount = 0;
			this.wordCount = 0;
			return 0;
		}

		this.startOffset = this.endOffset;
		int i = this.startOffset;
		for (; i < this.length; i++) {
			if (!Character.isWhitespace(this.source.charAt(i)))
				break;
		}
		this.spaceCount = i - this.startOffset;

		int wordOffset = i;
		for (; i < this.length; i++) {
			if (Character.isWhitespace(this.source.charAt(i)))
				break;
		}
		this.wordCount = i - wordOffset;
		this.endOffset = i;

		return this.wordCount;
	}

	/**
	 */
	public void replaceSpaces(String spaces) {
		int spaceLength = (spaces != null ? spaces.length() : 0);
		String oldSpaces = null;
		if (spaceLength == this.spaceCount) {
			if (spaceLength == 0)
				return;
			if (this.startOffset == 0) {
				if (this.source.startsWith(spaces))
					return;
			}
			else if (this.endOffset == this.length) {
				if (this.source.endsWith(spaces))
					return;
			}
			else {
				int textOffset = this.startOffset + this.spaceCount;
				oldSpaces = this.source.substring(this.startOffset, textOffset);
				if (oldSpaces.equals(spaces))
					return;
			}
		}
		if (this.keepBlankLines && this.spaceCount > 0) {
			if (oldSpaces == null) {
				int textOffset = this.startOffset + this.spaceCount;
				oldSpaces = this.source.substring(this.startOffset, textOffset);
			}
			if (oldSpaces != null) {
				spaces = mergeBlankLines(spaces, oldSpaces);
			}
		}

		if (this.buffer == null)
			this.buffer = new StringBuffer();
		if (this.startOffset > this.lastOffset) {
			this.buffer.append(this.source.substring(this.lastOffset, this.startOffset));
		}

		if (spaceLength > 0)
			this.buffer.append(spaces);

		this.lastOffset = this.startOffset + this.spaceCount;
	}
}

Back to the top