001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.tools.ant.util;
019:
020: import java.io.Reader;
021: import java.io.IOException;
022:
023: import org.apache.tools.ant.ProjectComponent;
024:
025: /**
026: * class to tokenize the input as lines seperated
027: * by \r (mac style), \r\n (dos/windows style) or \n (unix style)
028: * @since Ant 1.6
029: */
030: public class LineTokenizer extends ProjectComponent implements
031: Tokenizer {
032: private String lineEnd = "";
033: private int pushed = -2;
034: private boolean includeDelims = false;
035:
036: /**
037: * attribute includedelims - whether to include
038: * the line ending with the line, or to return
039: * it in the posttoken
040: * default false
041: * @param includeDelims if true include /r and /n in the line
042: */
043:
044: public void setIncludeDelims(boolean includeDelims) {
045: this .includeDelims = includeDelims;
046: }
047:
048: /**
049: * get the next line from the input
050: *
051: * @param in the input reader
052: * @return the line excluding /r or /n, unless includedelims is set
053: * @exception IOException if an error occurs reading
054: */
055: public String getToken(Reader in) throws IOException {
056: int ch = -1;
057: if (pushed != -2) {
058: ch = pushed;
059: pushed = -2;
060: } else {
061: ch = in.read();
062: }
063: if (ch == -1) {
064: return null;
065: }
066:
067: lineEnd = "";
068: StringBuffer line = new StringBuffer();
069:
070: int state = 0;
071: while (ch != -1) {
072: if (state == 0) {
073: if (ch == '\r') {
074: state = 1;
075: } else if (ch == '\n') {
076: lineEnd = "\n";
077: break;
078: } else {
079: line.append((char) ch);
080: }
081: } else {
082: state = 0;
083: if (ch == '\n') {
084: lineEnd = "\r\n";
085: } else {
086: pushed = ch;
087: lineEnd = "\r";
088: }
089: break;
090: }
091: ch = in.read();
092: }
093: if (ch == -1 && state == 1) {
094: lineEnd = "\r";
095: }
096:
097: if (includeDelims) {
098: line.append(lineEnd);
099: }
100: return line.toString();
101: }
102:
103: /**
104: * @return the line ending character(s) for the current line
105: */
106: public String getPostToken() {
107: if (includeDelims) {
108: return "";
109: }
110: return lineEnd;
111: }
112:
113: }
|