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.IOException;
021: import java.io.Reader;
022: import org.apache.tools.ant.ProjectComponent;
023:
024: /**
025: * Class to tokenize the input as areas separated
026: * by white space, or by a specified list of
027: * delim characters. Behaves like java.util.StringTokenizer.
028: * If the stream starts with delim characters, the first
029: * token will be an empty string (unless the treat delims
030: * as tokens flag is set).
031: * @since Ant 1.7
032: */
033: public class StringTokenizer extends ProjectComponent implements
034: Tokenizer {
035: private String intraString = "";
036: private int pushed = -2;
037: private char[] delims = null;
038: private boolean delimsAreTokens = false;
039: private boolean suppressDelims = false;
040: private boolean includeDelims = false;
041:
042: /**
043: * attribute delims - the delimiter characters
044: * @param delims a string containing the delimiter characters
045: */
046: public void setDelims(String delims) {
047: this .delims = StringUtils.resolveBackSlash(delims)
048: .toCharArray();
049: }
050:
051: /**
052: * attribute delimsaretokens - treat delimiters as
053: * separate tokens.
054: * @param delimsAreTokens true if delimiters are to be separate
055: */
056:
057: public void setDelimsAreTokens(boolean delimsAreTokens) {
058: this .delimsAreTokens = delimsAreTokens;
059: }
060:
061: /**
062: * attribute suppressdelims - suppress delimiters.
063: * default - false
064: * @param suppressDelims if true do not report delimiters
065: */
066: public void setSuppressDelims(boolean suppressDelims) {
067: this .suppressDelims = suppressDelims;
068: }
069:
070: /**
071: * attribute includedelims - treat delimiters as part
072: * of the token.
073: * default - false
074: * @param includeDelims if true add delimiters to the token
075: */
076: public void setIncludeDelims(boolean includeDelims) {
077: this .includeDelims = includeDelims;
078: }
079:
080: /**
081: * find and return the next token
082: *
083: * @param in the input stream
084: * @return the token
085: * @exception IOException if an error occurs reading
086: */
087: public String getToken(Reader in) throws IOException {
088: int ch = -1;
089: if (pushed != -2) {
090: ch = pushed;
091: pushed = -2;
092: } else {
093: ch = in.read();
094: }
095: if (ch == -1) {
096: return null;
097: }
098: boolean inToken = true;
099: intraString = "";
100: StringBuffer word = new StringBuffer();
101: StringBuffer padding = new StringBuffer();
102: while (ch != -1) {
103: char c = (char) ch;
104: boolean isDelim = isDelim(c);
105: if (inToken) {
106: if (isDelim) {
107: if (delimsAreTokens) {
108: if (word.length() == 0) {
109: word.append(c);
110: } else {
111: pushed = ch;
112: }
113: break;
114: }
115: padding.append(c);
116: inToken = false;
117: } else {
118: word.append(c);
119: }
120: } else {
121: if (isDelim) {
122: padding.append(c);
123: } else {
124: pushed = ch;
125: break;
126: }
127: }
128: ch = in.read();
129: }
130: intraString = padding.toString();
131: if (includeDelims) {
132: word.append(intraString);
133: }
134: return word.toString();
135: }
136:
137: /**
138: * @return the intratoken string
139: */
140: public String getPostToken() {
141: return suppressDelims || includeDelims ? "" : intraString;
142: }
143:
144: private boolean isDelim(char ch) {
145: if (delims == null) {
146: return Character.isWhitespace(ch);
147: }
148: for (int i = 0; i < delims.length; ++i) {
149: if (delims[i] == ch) {
150: return true;
151: }
152: }
153: return false;
154: }
155: }
|