KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > util > LineTokenizer


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18 package org.apache.tools.ant.util;
19
20 import java.io.Reader JavaDoc;
21 import java.io.IOException JavaDoc;
22
23 import org.apache.tools.ant.ProjectComponent;
24
25 /**
26  * class to tokenize the input as lines seperated
27  * by \r (mac style), \r\n (dos/windows style) or \n (unix style)
28  * @since Ant 1.6
29  */

30 public class LineTokenizer extends ProjectComponent
31     implements Tokenizer {
32     private String JavaDoc lineEnd = "";
33     private int pushed = -2;
34     private boolean includeDelims = false;
35
36     /**
37      * attribute includedelims - whether to include
38      * the line ending with the line, or to return
39      * it in the posttoken
40      * default false
41      * @param includeDelims if true include /r and /n in the line
42      */

43
44     public void setIncludeDelims(boolean includeDelims) {
45         this.includeDelims = includeDelims;
46     }
47
48     /**
49      * get the next line from the input
50      *
51      * @param in the input reader
52      * @return the line excluding /r or /n, unless includedelims is set
53      * @exception IOException if an error occurs reading
54      */

55     public String JavaDoc getToken(Reader JavaDoc in) throws IOException JavaDoc {
56         int ch = -1;
57         if (pushed != -2) {
58             ch = pushed;
59             pushed = -2;
60         } else {
61             ch = in.read();
62         }
63         if (ch == -1) {
64             return null;
65         }
66
67         lineEnd = "";
68         StringBuffer JavaDoc line = new StringBuffer JavaDoc();
69
70         int state = 0;
71         while (ch != -1) {
72             if (state == 0) {
73                 if (ch == '\r') {
74                     state = 1;
75                 } else if (ch == '\n') {
76                     lineEnd = "\n";
77                     break;
78                 } else {
79                     line.append((char) ch);
80                 }
81             } else {
82                 state = 0;
83                 if (ch == '\n') {
84                     lineEnd = "\r\n";
85                 } else {
86                     pushed = ch;
87                     lineEnd = "\r";
88                 }
89                 break;
90             }
91             ch = in.read();
92         }
93         if (ch == -1 && state == 1) {
94             lineEnd = "\r";
95         }
96
97         if (includeDelims) {
98             line.append(lineEnd);
99         }
100         return line.toString();
101     }
102
103     /**
104      * @return the line ending character(s) for the current line
105      */

106     public String JavaDoc getPostToken() {
107         if (includeDelims) {
108             return "";
109         }
110         return lineEnd;
111     }
112
113 }
114
115
Popular Tags