Java Code Examples for java.nio.file.Path#iterator()

The following examples show how to use java.nio.file.Path#iterator() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PathUtils.java    From PeerWasp with MIT License 6 votes vote down vote up
public static Path getCommonPath(Path path1, Path path2){
	Path commonPath = Paths.get("");
   	if(path1 == null || path2 == null){
   		return commonPath;
   	}
	Iterator<Path> iterPath1 = path1.iterator();
	Iterator<Path> iterPath2 = path2.iterator();

	while(iterPath1.hasNext() && iterPath2.hasNext()){
		Path next1 = iterPath1.next();

		if(next1.equals(iterPath2.next())){
			commonPath = commonPath.resolve(next1);
		} else {
			break;
		}
	}

	return commonPath;
}
 
Example 2
Source File: DefaultClassUsageFileReader.java    From buck with Apache License 2.0 6 votes vote down vote up
/**
 * Convert a path rooted in another cell to an absolute path in the filesystem
 *
 * @param cellRootedPath a path beginning with '/cell_name/' followed by a relative path in that
 *     cell
 * @param cellPathResolver the CellPathResolver capable of mapping cell_name to absolute root path
 * @return an absolute path: 'path/to/cell/root/' + 'relative/path/in/cell'
 */
private static Path getAbsolutePathForCellRootedPath(
    Path cellRootedPath, CellPathResolver cellPathResolver) {
  Preconditions.checkArgument(cellRootedPath.isAbsolute(), "Path must begin with /<cell_name>");
  Iterator<Path> pathIterator = cellRootedPath.iterator();
  Path cellNamePath = pathIterator.next();
  Path relativeToCellRoot = pathIterator.next();
  while (pathIterator.hasNext()) {
    relativeToCellRoot = relativeToCellRoot.resolve(pathIterator.next());
  }
  String cellName = cellNamePath.toString();
  Optional<String> canonicalCellName =
      cellName.equals(DefaultClassUsageFileWriter.ROOT_CELL_IDENTIFIER)
          ? Optional.empty()
          : Optional.of(cellName);
  return cellPathResolver.getCellPathOrThrow(canonicalCellName).resolve(relativeToCellRoot);
}
 
Example 3
Source File: Escaper.java    From buck with Apache License 2.0 6 votes vote down vote up
/**
 * Escapes forward slashes in a Path as a String that is safe to consume with other tools (such as
 * gcc). On Unix systems, this is equivalent to {@link java.nio.file.Path Path.toString()}.
 *
 * @param path the Path to escape
 * @return the escaped Path
 */
public static String escapePathForCIncludeString(Path path) {
  if (File.separatorChar != '\\') {
    return path.toString();
  }
  StringBuilder result = new StringBuilder();
  if (path.startsWith(File.separator)) {
    result.append("\\\\");
  }
  for (Iterator<Path> iterator = path.iterator(); iterator.hasNext(); ) {
    result.append(iterator.next());
    if (iterator.hasNext()) {
      result.append("\\\\");
    }
  }
  if (path.getNameCount() > 0 && path.endsWith(File.separator)) {
    result.append("\\\\");
  }
  return result.toString();
}
 
Example 4
Source File: JarAnalyzer.java    From steady with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the fully-qualified Java class identifier for a given JAR entry.
 * This is done by removing the file extension and by interpreting folders as packages.
 * If anything goes wrong, null is returned.
 *
 * @param _jar_entry_name a {@link java.lang.String} object.
 * @return a {@link java.lang.String} object.
 */
public static String getFqClassname(String _jar_entry_name) {
	String cn = null;
	if(_jar_entry_name.endsWith(".class")) {			
		// 18.11.2014: Ignore "package-info.class" files, which can contain annotations and documentation
		// 05.12.2017: Ignore "module-info.class" files, which can contain annotations and documentation
		if(!_jar_entry_name.endsWith("package-info.class") && !_jar_entry_name.endsWith("module-info.class")) {
			final StringBuffer fqn = new StringBuffer();

			final Path p = Paths.get(_jar_entry_name);
			final Iterator<Path> i = p.iterator();

			while(i.hasNext()) {
				String element = i.next().toString();
				if(element.endsWith(".class"))
					element = element.substring(0, element.length()-6); // ".class"
				if(JarAnalyzer.isJavaIdentifier(element)) {
					if(fqn.length()!=0)
						fqn.append('.');
					fqn.append(element);
				}
				else {
					JarAnalyzer.log.warn("JAR entry [" + _jar_entry_name + "] cannot be transformed to a fully-qualified Java class identifier, because [" + element + "] is not a valid identifier");
					return null;
				}
			}
			cn = fqn.toString();
		}
	}
	return cn;
}
 
Example 5
Source File: AddBuildConfigToComponentDecorator.java    From dekorate with Apache License 2.0 5 votes vote down vote up
/**
 * Get the context path of the current module.
 * This is the relative path from project root, excluding the module directory.
 *
 * @param modulePath The relative path from the project root.
 * @return the name of module directory.
 */
private static String toContextPath(Path modulePath) {
  StringBuilder sb = new StringBuilder();
  Iterator<Path> iterator = modulePath.iterator();
  while (iterator.hasNext()) {
    String current = iterator.next().toString();
    if (iterator.hasNext()) {
      sb.append(current);
      sb.append(File.separator);
    }
  }
  return sb.toString();
}
 
Example 6
Source File: AddBuildConfigToComponentDecorator.java    From dekorate with Apache License 2.0 5 votes vote down vote up
/**
 * Get the directory name of the current module.
 *
 * @param modulePath The relative path from the project root.
 * @return the name of module directory.
 */
private static String toModuleDirName(Path modulePath) {
  Iterator<Path> iterator = modulePath.iterator();
  while (iterator.hasNext()) {
    String current = iterator.next().toString();
    if (!iterator.hasNext()) {
      return current;
    }
  }
  return null;
}
 
Example 7
Source File: Downloader.java    From nju-lib-downloader with GNU General Public License v3.0 5 votes vote down vote up
private static String parsePathWithWhiteSpace(Path path) {
    StringBuffer output = new StringBuffer();
    output.append(path.getRoot());
    Iterator<Path> pathIterator = path.iterator();
    while (pathIterator.hasNext()) {
        output.append(System.getProperty("file.separator"));
        output.append(parsePathWithWhiteSpace(pathIterator.next().toString()));
    }
    return output.toString();
}
 
Example 8
Source File: TestPath.java    From jsr203-hadoop with Apache License 2.0 5 votes vote down vote up
@Test
public void iterator() throws IOException {
  Path rootPath = Paths.get(clusterUri);

  Path p = rootPath.resolve("tmp/testNormalize/test");
  Iterator<Path> it = p.iterator();
  assertNotNull(it);
  assertEquals("tmp", it.next().toString());
  assertEquals("testNormalize", it.next().toString());
  assertEquals("test", it.next().toString());
}
 
Example 9
Source File: ExternalModuleSpecService.java    From wildfly-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
private int compareBySameLevel(Path path1, Path path2) {
    int i = 0;
    int comparison = 0;
    for (Iterator<Path> iterPath1 = path1.iterator(); iterPath1.hasNext(); i++) {
        Path levelPath1 = iterPath1.next();
        Path levelPath2 = path2.getName(i);
        comparison = levelPath1.compareTo(levelPath2);
        if (comparison!=0) {
            break;
        }
    }
    return comparison;
}
 
Example 10
Source File: ToolPath.java    From protools with Apache License 2.0 4 votes vote down vote up
public static Iterator<Path> dirIterator(Path dir) {
    return dir.iterator();
}