1 | /*
|
---|
2 | * Copyright 2003-2004 The Apache Software Foundation
|
---|
3 | *
|
---|
4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
5 | * you may not use this file except in compliance with the License.
|
---|
6 | * You may obtain a copy of the License at
|
---|
7 | *
|
---|
8 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
9 | *
|
---|
10 | * Unless required by applicable law or agreed to in writing, software
|
---|
11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
13 | * See the License for the specific language governing permissions and
|
---|
14 | * limitations under the License.
|
---|
15 | *
|
---|
16 | */
|
---|
17 | package org.apache.tools.ant.util;
|
---|
18 |
|
---|
19 | import java.io.Reader;
|
---|
20 | import java.io.IOException;
|
---|
21 |
|
---|
22 | import org.apache.tools.ant.ProjectComponent;
|
---|
23 |
|
---|
24 | /**
|
---|
25 | * class to tokenize the input as lines seperated
|
---|
26 | * by \r (mac style), \r\n (dos/windows style) or \n (unix style)
|
---|
27 | * @since Ant 1.6
|
---|
28 | */
|
---|
29 | public class LineTokenizer extends ProjectComponent
|
---|
30 | implements Tokenizer {
|
---|
31 | private String lineEnd = "";
|
---|
32 | private int pushed = -2;
|
---|
33 | private boolean includeDelims = false;
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * attribute includedelims - whether to include
|
---|
37 | * the line ending with the line, or to return
|
---|
38 | * it in the posttoken
|
---|
39 | * default false
|
---|
40 | * @param includeDelims if true include /r and /n in the line
|
---|
41 | */
|
---|
42 |
|
---|
43 | public void setIncludeDelims(boolean includeDelims) {
|
---|
44 | this.includeDelims = includeDelims;
|
---|
45 | }
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * get the next line from the input
|
---|
49 | *
|
---|
50 | * @param in the input reader
|
---|
51 | * @return the line excluding /r or /n, unless includedelims is set
|
---|
52 | * @exception IOException if an error occurs reading
|
---|
53 | */
|
---|
54 | public String getToken(Reader in) throws IOException {
|
---|
55 | int ch = -1;
|
---|
56 | if (pushed != -2) {
|
---|
57 | ch = pushed;
|
---|
58 | pushed = -2;
|
---|
59 | } else {
|
---|
60 | ch = in.read();
|
---|
61 | }
|
---|
62 | if (ch == -1) {
|
---|
63 | return null;
|
---|
64 | }
|
---|
65 |
|
---|
66 | lineEnd = "";
|
---|
67 | StringBuffer line = new StringBuffer();
|
---|
68 |
|
---|
69 | int state = 0;
|
---|
70 | while (ch != -1) {
|
---|
71 | if (state == 0) {
|
---|
72 | if (ch == '\r') {
|
---|
73 | state = 1;
|
---|
74 | } else if (ch == '\n') {
|
---|
75 | lineEnd = "\n";
|
---|
76 | break;
|
---|
77 | } else {
|
---|
78 | line.append((char) ch);
|
---|
79 | }
|
---|
80 | } else {
|
---|
81 | state = 0;
|
---|
82 | if (ch == '\n') {
|
---|
83 | lineEnd = "\r\n";
|
---|
84 | } else {
|
---|
85 | pushed = ch;
|
---|
86 | lineEnd = "\r";
|
---|
87 | }
|
---|
88 | break;
|
---|
89 | }
|
---|
90 | ch = in.read();
|
---|
91 | }
|
---|
92 | if (ch == -1 && state == 1) {
|
---|
93 | lineEnd = "\r";
|
---|
94 | }
|
---|
95 |
|
---|
96 | if (includeDelims) {
|
---|
97 | line.append(lineEnd);
|
---|
98 | }
|
---|
99 | return line.toString();
|
---|
100 | }
|
---|
101 |
|
---|
102 | /**
|
---|
103 | * @return the line ending character(s) for the current line
|
---|
104 | */
|
---|
105 | public String getPostToken() {
|
---|
106 | if (includeDelims) {
|
---|
107 | return "";
|
---|
108 | }
|
---|
109 | return lineEnd;
|
---|
110 | }
|
---|
111 |
|
---|
112 | }
|
---|
113 |
|
---|