blob: 1122bd490e3cfe1f3598645e187576392d16fb86 (
plain)
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
|
/*
* Copyright 2003-2004 Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.apache.tools.ant.util;
import java.io.Reader;
import java.io.IOException;
import org.apache.tools.ant.ProjectComponent;
/**
* class to tokenize the input as lines seperated
* by \r (mac style), \r\n (dos/windows style) or \n (unix style)
* @author Peter Reilly
* @since Ant 1.6
*/
public class LineTokenizer extends ProjectComponent
implements Tokenizer {
private String lineEnd = "";
private int pushed = -2;
private boolean includeDelims = false;
/**
* attribute includedelims - whether to include
* the line ending with the line, or to return
* it in the posttoken
* default false
* @param includeDelims if true include /r and /n in the line
*/
public void setIncludeDelims(boolean includeDelims) {
this.includeDelims = includeDelims;
}
/**
* get the next line from the input
*
* @param in the input reader
* @return the line excluding /r or /n, unless includedelims is set
* @exception IOException if an error occurs reading
*/
public String getToken(Reader in) throws IOException {
int ch = -1;
if (pushed != -2) {
ch = pushed;
pushed = -2;
} else {
ch = in.read();
}
if (ch == -1) {
return null;
}
lineEnd = "";
StringBuffer line = new StringBuffer();
int state = 0;
while (ch != -1) {
if (state == 0) {
if (ch == '\r') {
state = 1;
} else if (ch == '\n') {
lineEnd = "\n";
break;
} else {
line.append((char) ch);
}
} else {
state = 0;
if (ch == '\n') {
lineEnd = "\r\n";
} else {
pushed = ch;
lineEnd = "\r";
}
break;
}
ch = in.read();
}
if (ch == -1 && state == 1) {
lineEnd = "\r";
}
if (includeDelims) {
line.append(lineEnd);
}
return line.toString();
}
/**
* @return the line ending character(s) for the current line
*/
public String getPostToken() {
if (includeDelims) {
return "";
}
return lineEnd;
}
}
|