KickJava   Java API By Example, From Geeks To Geeks.

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


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.IOException JavaDoc;
21 import java.io.Reader JavaDoc;
22 import org.apache.tools.ant.ProjectComponent;
23
24 /**
25  * Class to tokenize the input as areas separated
26  * by white space, or by a specified list of
27  * delim characters. Behaves like java.util.StringTokenizer.
28  * If the stream starts with delim characters, the first
29  * token will be an empty string (unless the treat delims
30  * as tokens flag is set).
31  * @since Ant 1.7
32  */

33 public class StringTokenizer extends ProjectComponent implements Tokenizer {
34     private String JavaDoc intraString = "";
35     private int pushed = -2;
36     private char[] delims = null;
37     private boolean delimsAreTokens = false;
38     private boolean suppressDelims = false;
39     private boolean includeDelims = false;
40
41     /**
42      * attribute delims - the delimiter characters
43      * @param delims a string containing the delimiter characters
44      */

45     public void setDelims(String JavaDoc delims) {
46         this.delims = StringUtils.resolveBackSlash(delims).toCharArray();
47     }
48
49     /**
50      * attribute delimsaretokens - treat delimiters as
51      * separate tokens.
52      * @param delimsAreTokens true if delimiters are to be separate
53      */

54
55     public void setDelimsAreTokens(boolean delimsAreTokens) {
56         this.delimsAreTokens = delimsAreTokens;
57     }
58     /**
59      * attribute suppressdelims - suppress delimiters.
60      * default - false
61      * @param suppressDelims if true do not report delimiters
62      */

63     public void setSuppressDelims(boolean suppressDelims) {
64         this.suppressDelims = suppressDelims;
65     }
66
67     /**
68      * attribute includedelims - treat delimiters as part
69      * of the token.
70      * default - false
71      * @param includeDelims if true add delimiters to the token
72      */

73     public void setIncludeDelims(boolean includeDelims) {
74         this.includeDelims = includeDelims;
75     }
76
77     /**
78      * find and return the next token
79      *
80      * @param in the input stream
81      * @return the token
82      * @exception IOException if an error occurs reading
83      */

84     public String JavaDoc getToken(Reader JavaDoc in) throws IOException JavaDoc {
85         int ch = -1;
86         if (pushed != -2) {
87             ch = pushed;
88             pushed = -2;
89         } else {
90             ch = in.read();
91         }
92         if (ch == -1) {
93             return null;
94         }
95         boolean inToken = true;
96         intraString = "";
97         StringBuffer JavaDoc word = new StringBuffer JavaDoc();
98         StringBuffer JavaDoc padding = new StringBuffer JavaDoc();
99         while (ch != -1) {
100             char c = (char) ch;
101             boolean isDelim = isDelim(c);
102             if (inToken) {
103                 if (isDelim) {
104                     if (delimsAreTokens) {
105                         if (word.length() == 0) {
106                             word.append(c);
107                         } else {
108                             pushed = ch;
109                         }
110                         break;
111                     }
112                     padding.append(c);
113                     inToken = false;
114                 } else {
115                     word.append(c);
116                 }
117             } else {
118                 if (isDelim) {
119                     padding.append(c);
120                 } else {
121                     pushed = ch;
122                     break;
123                 }
124             }
125             ch = in.read();
126         }
127         intraString = padding.toString();
128         if (includeDelims) {
129             word.append(intraString);
130         }
131         return word.toString();
132     }
133
134     /**
135      * @return the intratoken string
136      */

137     public String JavaDoc getPostToken() {
138         return suppressDelims || includeDelims ? "" : intraString;
139     }
140
141     private boolean isDelim(char ch) {
142         if (delims == null) {
143             return Character.isWhitespace(ch);
144         }
145         for (int i = 0; i < delims.length; ++i) {
146             if (delims[i] == ch) {
147                 return true;
148             }
149         }
150         return false;
151     }
152 }
153
Popular Tags