KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > PathTokenizer


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;
19
20 import java.io.File JavaDoc;
21 import java.util.NoSuchElementException JavaDoc;
22 import java.util.StringTokenizer JavaDoc;
23 import org.apache.tools.ant.taskdefs.condition.Os;
24
25 /**
26  * A Path tokenizer takes a path and returns the components that make up
27  * that path.
28  *
29  * The path can use path separators of either ':' or ';' and file separators
30  * of either '/' or '\'.
31  *
32  */

33 public class PathTokenizer {
34     /**
35      * A tokenizer to break the string up based on the ':' or ';' separators.
36      */

37     private StringTokenizer JavaDoc tokenizer;
38
39     /**
40      * A String which stores any path components which have been read ahead
41      * due to DOS filesystem compensation.
42      */

43     private String JavaDoc lookahead = null;
44
45     /**
46      * A boolean that determines if we are running on Novell NetWare, which
47      * exhibits slightly different path name characteristics (multi-character
48      * volume / drive names)
49      */

50     private boolean onNetWare = Os.isFamily("netware");
51
52     /**
53      * Flag to indicate whether or not we are running on a platform with a
54      * DOS style filesystem
55      */

56     private boolean dosStyleFilesystem;
57
58     /**
59      * Constructs a path tokenizer for the specified path.
60      *
61      * @param path The path to tokenize. Must not be <code>null</code>.
62      */

63     public PathTokenizer(String JavaDoc path) {
64         if (onNetWare) {
65             // For NetWare, use the boolean=true mode, so we can use delimiter
66
// information to make a better decision later.
67
tokenizer = new StringTokenizer JavaDoc(path, ":;", true);
68         } else {
69             // on Windows and Unix, we can ignore delimiters and still have
70
// enough information to tokenize correctly.
71
tokenizer = new StringTokenizer JavaDoc(path, ":;", false);
72         }
73         dosStyleFilesystem = File.pathSeparatorChar == ';';
74     }
75
76     /**
77      * Tests if there are more path elements available from this tokenizer's
78      * path. If this method returns <code>true</code>, then a subsequent call
79      * to nextToken will successfully return a token.
80      *
81      * @return <code>true</code> if and only if there is at least one token
82      * in the string after the current position; <code>false</code> otherwise.
83      */

84     public boolean hasMoreTokens() {
85         if (lookahead != null) {
86             return true;
87         }
88
89         return tokenizer.hasMoreTokens();
90     }
91
92     /**
93      * Returns the next path element from this tokenizer.
94      *
95      * @return the next path element from this tokenizer.
96      *
97      * @exception NoSuchElementException if there are no more elements in this
98      * tokenizer's path.
99      */

100     public String JavaDoc nextToken() throws NoSuchElementException JavaDoc {
101         String JavaDoc token = null;
102         if (lookahead != null) {
103             token = lookahead;
104             lookahead = null;
105         } else {
106             token = tokenizer.nextToken().trim();
107         }
108
109         if (!onNetWare) {
110             if (token.length() == 1 && Character.isLetter(token.charAt(0))
111                                     && dosStyleFilesystem
112                                     && tokenizer.hasMoreTokens()) {
113                 // we are on a dos style system so this path could be a drive
114
// spec. We look at the next token
115
String JavaDoc nextToken = tokenizer.nextToken().trim();
116                 if (nextToken.startsWith("\\") || nextToken.startsWith("/")) {
117                     // we know we are on a DOS style platform and the next path
118
// starts with a slash or backslash, so we know this is a
119
// drive spec
120
token += ":" + nextToken;
121                 } else {
122                     // store the token just read for next time
123
lookahead = nextToken;
124                 }
125             }
126         } else {
127             // we are on NetWare, tokenizing is handled a little differently,
128
// due to the fact that NetWare has multiple-character volume names.
129
if (token.equals(File.pathSeparator) || token.equals(":")) {
130                 // ignore ";" and get the next token
131
token = tokenizer.nextToken().trim();
132             }
133
134             if (tokenizer.hasMoreTokens()) {
135                 // this path could be a drive spec, so look at the next token
136
String JavaDoc nextToken = tokenizer.nextToken().trim();
137
138                 // make sure we aren't going to get the path separator next
139
if (!nextToken.equals(File.pathSeparator)) {
140                     if (nextToken.equals(":")) {
141                         if (!token.startsWith("/") && !token.startsWith("\\")
142                             && !token.startsWith(".")
143                             && !token.startsWith("..")) {
144                             // it indeed is a drive spec, get the next bit
145
String JavaDoc oneMore = tokenizer.nextToken().trim();
146                             if (!oneMore.equals(File.pathSeparator)) {
147                                 token += ":" + oneMore;
148                             } else {
149                                 token += ":";
150                                 lookahead = oneMore;
151                             }
152                         }
153                         // implicit else: ignore the ':' since we have either a
154
// UNIX or a relative path
155
} else {
156                         // store the token just read for next time
157
lookahead = nextToken;
158                     }
159                 }
160             }
161         }
162         return token;
163     }
164 }
165
166
Popular Tags