You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

PathTokenizer.java 8.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. * Copyright (c) 2000,2002-2003 The Apache Software Foundation. All rights
  5. * reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * 3. The end-user documentation included with the redistribution, if
  20. * any, must include the following acknowlegement:
  21. * "This product includes software developed by the
  22. * Apache Software Foundation (http://www.apache.org/)."
  23. * Alternately, this acknowlegement may appear in the software itself,
  24. * if and wherever such third-party acknowlegements normally appear.
  25. *
  26. * 4. The names "Ant" and "Apache Software
  27. * Foundation" must not be used to endorse or promote products derived
  28. * from this software without prior written permission. For written
  29. * permission, please contact apache@apache.org.
  30. *
  31. * 5. Products derived from this software may not be called "Apache"
  32. * nor may "Apache" appear in their names without prior written
  33. * permission of the Apache Group.
  34. *
  35. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  36. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  37. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  38. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  39. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  42. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  43. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  44. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  45. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  46. * SUCH DAMAGE.
  47. * ====================================================================
  48. *
  49. * This software consists of voluntary contributions made by many
  50. * individuals on behalf of the Apache Software Foundation. For more
  51. * information on the Apache Software Foundation, please see
  52. * <http://www.apache.org/>.
  53. */
  54. package org.apache.tools.ant;
  55. import java.io.File;
  56. import java.util.NoSuchElementException;
  57. import java.util.StringTokenizer;
  58. import org.apache.tools.ant.taskdefs.condition.Os;
  59. /**
  60. * A Path tokenizer takes a path and returns the components that make up
  61. * that path.
  62. *
  63. * The path can use path separators of either ':' or ';' and file separators
  64. * of either '/' or '\'.
  65. *
  66. * @author Conor MacNeill
  67. * @author <a href="mailto:jtulley@novell.com">Jeff Tulley</a>
  68. */
  69. public class PathTokenizer {
  70. /**
  71. * A tokenizer to break the string up based on the ':' or ';' separators.
  72. */
  73. private StringTokenizer tokenizer;
  74. /**
  75. * A String which stores any path components which have been read ahead
  76. * due to DOS filesystem compensation.
  77. */
  78. private String lookahead = null;
  79. /**
  80. * A boolean that determines if we are running on Novell NetWare, which
  81. * exhibits slightly different path name characteristics (multi-character
  82. * volume / drive names)
  83. */
  84. private boolean onNetWare = Os.isFamily("netware");
  85. /**
  86. * Flag to indicate whether or not we are running on a platform with a
  87. * DOS style filesystem
  88. */
  89. private boolean dosStyleFilesystem;
  90. /**
  91. * Constructs a path tokenizer for the specified path.
  92. *
  93. * @param path The path to tokenize. Must not be <code>null</code>.
  94. */
  95. public PathTokenizer(String path) {
  96. if (onNetWare) {
  97. // For NetWare, use the boolean=true mode, so we can use delimiter
  98. // information to make a better decision later.
  99. tokenizer = new StringTokenizer(path, ":;", true);
  100. } else {
  101. // on Windows and Unix, we can ignore delimiters and still have
  102. // enough information to tokenize correctly.
  103. tokenizer = new StringTokenizer(path, ":;", false);
  104. }
  105. dosStyleFilesystem = File.pathSeparatorChar == ';';
  106. }
  107. /**
  108. * Tests if there are more path elements available from this tokenizer's
  109. * path. If this method returns <code>true</code>, then a subsequent call
  110. * to nextToken will successfully return a token.
  111. *
  112. * @return <code>true</code> if and only if there is at least one token
  113. * in the string after the current position; <code>false</code> otherwise.
  114. */
  115. public boolean hasMoreTokens() {
  116. if (lookahead != null) {
  117. return true;
  118. }
  119. return tokenizer.hasMoreTokens();
  120. }
  121. /**
  122. * Returns the next path element from this tokenizer.
  123. *
  124. * @return the next path element from this tokenizer.
  125. *
  126. * @exception NoSuchElementException if there are no more elements in this
  127. * tokenizer's path.
  128. */
  129. public String nextToken() throws NoSuchElementException {
  130. String token = null;
  131. if (lookahead != null) {
  132. token = lookahead;
  133. lookahead = null;
  134. } else {
  135. token = tokenizer.nextToken().trim();
  136. }
  137. if (!onNetWare) {
  138. if (token.length() == 1 && Character.isLetter(token.charAt(0))
  139. && dosStyleFilesystem
  140. && tokenizer.hasMoreTokens()) {
  141. // we are on a dos style system so this path could be a drive
  142. // spec. We look at the next token
  143. String nextToken = tokenizer.nextToken().trim();
  144. if (nextToken.startsWith("\\") || nextToken.startsWith("/")) {
  145. // we know we are on a DOS style platform and the next path
  146. // starts with a slash or backslash, so we know this is a
  147. // drive spec
  148. token += ":" + nextToken;
  149. } else {
  150. // store the token just read for next time
  151. lookahead = nextToken;
  152. }
  153. }
  154. } else {
  155. // we are on NetWare, tokenizing is handled a little differently,
  156. // due to the fact that NetWare has multiple-character volume names.
  157. if (token.equals(File.pathSeparator) || token.equals(":")) {
  158. // ignore ";" and get the next token
  159. token = tokenizer.nextToken().trim();
  160. }
  161. if (tokenizer.hasMoreTokens()) {
  162. // this path could be a drive spec, so look at the next token
  163. String nextToken = tokenizer.nextToken().trim();
  164. // make sure we aren't going to get the path separator next
  165. if (!nextToken.equals(File.pathSeparator)) {
  166. if (nextToken.equals(":")) {
  167. if (!token.startsWith("/") && !token.startsWith("\\")
  168. && !token.startsWith(".")
  169. && !token.startsWith("..")) {
  170. // it indeed is a drive spec, get the next bit
  171. String oneMore = tokenizer.nextToken().trim();
  172. if (!oneMore.equals(File.pathSeparator)) {
  173. token += ":" + oneMore;
  174. } else {
  175. token += ":";
  176. lookahead = oneMore;
  177. }
  178. }
  179. // implicit else: ignore the ':' since we have either a
  180. // UNIX or a relative path
  181. } else {
  182. // store the token just read for next time
  183. lookahead = nextToken;
  184. }
  185. }
  186. }
  187. }
  188. return token;
  189. }
  190. }