Improved Templates i.e.
[phpeclipse.git] / archive / net.sourceforge.phpeclipse.wiki / src / net / sourceforge / phpeclipse / wiki / editor / LineTokenizer.java
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  *  2004 - modified for PHPeclipse plugins
17  */
18 package net.sourceforge.phpeclipse.wiki.editor;
19
20 import java.io.Reader;
21 import java.io.IOException;
22
23 /**
24  * class to tokenize the input as lines seperated
25  * by \r (mac style), \r\n (dos/windows style) or \n (unix style)
26  * @since Ant 1.6
27  */
28 public class LineTokenizer {
29     private String  lineEnd = "";
30     private int     pushed = -2;
31     private boolean includeDelims = false;
32
33     /**
34      * attribute includedelims - whether to include
35      * the line ending with the line, or to return
36      * it in the posttoken
37      * default false
38      * @param includeDelims if true include /r and /n in the line
39      */
40
41     public void setIncludeDelims(boolean includeDelims) {
42         this.includeDelims = includeDelims;
43     }
44
45     /**
46      * get the next line from the input
47      *
48      * @param in the input reader
49      * @return the line excluding /r or /n, unless includedelims is set
50      * @exception IOException if an error occurs reading
51      */
52     public boolean getToken(StringBuffer line, Reader in) throws IOException {
53         int ch = -1;
54         if (pushed != -2) {
55             ch = pushed;
56             pushed = -2;
57         } else {
58             ch = in.read();
59         }
60         if (ch == -1) {
61             return false;
62         }
63
64         lineEnd = "";
65 //        StringBuffer line = new StringBuffer();
66
67         int state = 0;
68         while (ch != -1) {
69             if (state == 0) {
70                 if (ch == '\r') {
71                     state = 1;
72                 } else if (ch == '\n') {
73                     lineEnd = "\n";
74                     break;
75                 } else {
76                     line.append((char) ch);
77                 }
78             } else {
79                 state = 0;
80                 if (ch == '\n') {
81                     lineEnd = "\r\n";
82                 } else {
83                     pushed = ch;
84                     lineEnd = "\r";
85                 }
86                 break;
87             }
88             ch = in.read();
89         }
90         if (ch == -1 && state == 1) {
91             lineEnd = "\r";
92         }
93
94         if (includeDelims) {
95             line.append(lineEnd);
96         }
97         return true;
98     }
99
100     /**
101      * @return the line ending character(s) for the current line
102      */
103     public String getPostToken() {
104         if (includeDelims) {
105             return "";
106         }
107         return lineEnd;
108     }
109
110 }