| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| PathTokenizer |
|
| 8.0;8 |
| 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.owasp.dependencycheck.org.apache.tools.ant; | |
| 19 | ||
| 20 | import java.io.File; | |
| 21 | import java.util.NoSuchElementException; | |
| 22 | import java.util.StringTokenizer; | |
| 23 | import org.owasp.dependencycheck.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 tokenizer; | |
| 38 | ||
| 39 | /** | |
| 40 | * A String which stores any path components which have been read ahead | |
| 41 | * due to DOS filesystem compensation. | |
| 42 | */ | |
| 43 | 0 | private String 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 | 0 | 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 | 0 | public PathTokenizer(String path) { |
| 64 | 0 | if (onNetWare) { |
| 65 | // For NetWare, use the boolean=true mode, so we can use delimiter | |
| 66 | // information to make a better decision later. | |
| 67 | 0 | tokenizer = new StringTokenizer(path, ":;", true); |
| 68 | } else { | |
| 69 | // on Windows and Unix, we can ignore delimiters and still have | |
| 70 | // enough information to tokenize correctly. | |
| 71 | 0 | tokenizer = new StringTokenizer(path, ":;", false); |
| 72 | } | |
| 73 | 0 | dosStyleFilesystem = File.pathSeparatorChar == ';'; |
| 74 | 0 | } |
| 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 | 0 | if (lookahead != null) { |
| 86 | 0 | return true; |
| 87 | } | |
| 88 | ||
| 89 | 0 | 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 nextToken() throws NoSuchElementException { | |
| 101 | 0 | String token = null; |
| 102 | 0 | if (lookahead != null) { |
| 103 | 0 | token = lookahead; |
| 104 | 0 | lookahead = null; |
| 105 | } else { | |
| 106 | 0 | token = tokenizer.nextToken().trim(); |
| 107 | } | |
| 108 | ||
| 109 | 0 | if (!onNetWare) { |
| 110 | 0 | 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 | 0 | String nextToken = tokenizer.nextToken().trim(); |
| 116 | 0 | 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 | 0 | token += ":" + nextToken; |
| 121 | } else { | |
| 122 | // store the token just read for next time | |
| 123 | 0 | lookahead = nextToken; |
| 124 | } | |
| 125 | 0 | } |
| 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 | 0 | if (token.equals(File.pathSeparator) || token.equals(":")) { |
| 130 | // ignore ";" and get the next token | |
| 131 | 0 | token = tokenizer.nextToken().trim(); |
| 132 | } | |
| 133 | ||
| 134 | 0 | if (tokenizer.hasMoreTokens()) { |
| 135 | // this path could be a drive spec, so look at the next token | |
| 136 | 0 | String nextToken = tokenizer.nextToken().trim(); |
| 137 | ||
| 138 | // make sure we aren't going to get the path separator next | |
| 139 | 0 | if (!nextToken.equals(File.pathSeparator)) { |
| 140 | 0 | if (nextToken.equals(":")) { |
| 141 | 0 | if (!token.startsWith("/") && !token.startsWith("\\") |
| 142 | && !token.startsWith(".") | |
| 143 | && !token.startsWith("..")) { | |
| 144 | // it indeed is a drive spec, get the next bit | |
| 145 | 0 | String oneMore = tokenizer.nextToken().trim(); |
| 146 | 0 | if (!oneMore.equals(File.pathSeparator)) { |
| 147 | 0 | token += ":" + oneMore; |
| 148 | } else { | |
| 149 | 0 | token += ":"; |
| 150 | 0 | lookahead = oneMore; |
| 151 | } | |
| 152 | 0 | } |
| 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 | 0 | lookahead = nextToken; |
| 158 | } | |
| 159 | } | |
| 160 | } | |
| 161 | } | |
| 162 | 0 | return token; |
| 163 | } | |
| 164 | } | |
| 165 |