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;
019:
020: import java.io.File;
021: import java.util.NoSuchElementException;
022: import java.util.StringTokenizer;
023: import org.apache.tools.ant.taskdefs.condition.Os;
024:
025: /**
026: * A Path tokenizer takes a path and returns the components that make up
027: * that path.
028: *
029: * The path can use path separators of either ':' or ';' and file separators
030: * of either '/' or '\'.
031: *
032: */
033: public class PathTokenizer {
034: /**
035: * A tokenizer to break the string up based on the ':' or ';' separators.
036: */
037: private StringTokenizer tokenizer;
038:
039: /**
040: * A String which stores any path components which have been read ahead
041: * due to DOS filesystem compensation.
042: */
043: private String lookahead = null;
044:
045: /**
046: * A boolean that determines if we are running on Novell NetWare, which
047: * exhibits slightly different path name characteristics (multi-character
048: * volume / drive names)
049: */
050: private boolean onNetWare = Os.isFamily("netware");
051:
052: /**
053: * Flag to indicate whether or not we are running on a platform with a
054: * DOS style filesystem
055: */
056: private boolean dosStyleFilesystem;
057:
058: /**
059: * Constructs a path tokenizer for the specified path.
060: *
061: * @param path The path to tokenize. Must not be <code>null</code>.
062: */
063: public PathTokenizer(String path) {
064: if (onNetWare) {
065: // For NetWare, use the boolean=true mode, so we can use delimiter
066: // information to make a better decision later.
067: tokenizer = new StringTokenizer(path, ":;", true);
068: } else {
069: // on Windows and Unix, we can ignore delimiters and still have
070: // enough information to tokenize correctly.
071: tokenizer = new StringTokenizer(path, ":;", false);
072: }
073: dosStyleFilesystem = File.pathSeparatorChar == ';';
074: }
075:
076: /**
077: * Tests if there are more path elements available from this tokenizer's
078: * path. If this method returns <code>true</code>, then a subsequent call
079: * to nextToken will successfully return a token.
080: *
081: * @return <code>true</code> if and only if there is at least one token
082: * in the string after the current position; <code>false</code> otherwise.
083: */
084: public boolean hasMoreTokens() {
085: if (lookahead != null) {
086: return true;
087: }
088:
089: return tokenizer.hasMoreTokens();
090: }
091:
092: /**
093: * Returns the next path element from this tokenizer.
094: *
095: * @return the next path element from this tokenizer.
096: *
097: * @exception NoSuchElementException if there are no more elements in this
098: * tokenizer's path.
099: */
100: public String nextToken() throws NoSuchElementException {
101: String 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
111: && Character.isLetter(token.charAt(0))
112: && dosStyleFilesystem && 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 nextToken = tokenizer.nextToken().trim();
116: if (nextToken.startsWith("\\")
117: || nextToken.startsWith("/")) {
118: // we know we are on a DOS style platform and the next path
119: // starts with a slash or backslash, so we know this is a
120: // drive spec
121: token += ":" + nextToken;
122: } else {
123: // store the token just read for next time
124: lookahead = nextToken;
125: }
126: }
127: } else {
128: // we are on NetWare, tokenizing is handled a little differently,
129: // due to the fact that NetWare has multiple-character volume names.
130: if (token.equals(File.pathSeparator) || token.equals(":")) {
131: // ignore ";" and get the next token
132: token = tokenizer.nextToken().trim();
133: }
134:
135: if (tokenizer.hasMoreTokens()) {
136: // this path could be a drive spec, so look at the next token
137: String nextToken = tokenizer.nextToken().trim();
138:
139: // make sure we aren't going to get the path separator next
140: if (!nextToken.equals(File.pathSeparator)) {
141: if (nextToken.equals(":")) {
142: if (!token.startsWith("/")
143: && !token.startsWith("\\")
144: && !token.startsWith(".")
145: && !token.startsWith("..")) {
146: // it indeed is a drive spec, get the next bit
147: String oneMore = tokenizer.nextToken()
148: .trim();
149: if (!oneMore.equals(File.pathSeparator)) {
150: token += ":" + oneMore;
151: } else {
152: token += ":";
153: lookahead = oneMore;
154: }
155: }
156: // implicit else: ignore the ':' since we have either a
157: // UNIX or a relative path
158: } else {
159: // store the token just read for next time
160: lookahead = nextToken;
161: }
162: }
163: }
164: }
165: return token;
166: }
167: }
|