List of usage examples for org.apache.commons.io FilenameUtils separatorsToUnix
public static String separatorsToUnix(String path)
From source file:com.ariht.maven.plugins.config.io.FileInfo.java
public void lookForExternalFiles(final List<String> externalBasePaths) { if (externalBasePaths == null || externalBasePaths.isEmpty()) { return;//from ww w . jav a 2 s . c o m } externalFiles = new LinkedList<File>(); for (final String basePath : externalBasePaths) { final String fullCanonicalFilename = FilenameUtils .separatorsToUnix(basePath + relativeSubDirectory + "/" + this.getName()); log.debug("Searching for: [" + fullCanonicalFilename + "]"); final File externalFile = new File(fullCanonicalFilename); if (externalFile.exists()) { log.debug("Including external file: [" + fullCanonicalFilename + "]"); externalFiles.add(externalFile); } } }
From source file:jodtemplate.pptx.ImageService.java
private Relationship getImageRelationship(final Image image, final Slide slide) { final Path imageFullPath = Paths.get(image.getFullPath()); final Path slideFullPath = Paths.get(FilenameUtils .getFullPath(slide.getPresentation().getFullPath() + slide.getRelationship().getTarget())); final Path relativeImagePath = slideFullPath.relativize(imageFullPath); final String normRelativeImagePath = FilenameUtils.separatorsToUnix(relativeImagePath.toString()); Relationship imageRel = slide.getRelationshipByTarget(normRelativeImagePath); if (imageRel == null) { imageRel = new Relationship(); imageRel.setId(slide.getNextId()); imageRel.setTarget(normRelativeImagePath); imageRel.setType(Relationship.IMAGE_TYPE); slide.addOtherRelationship(imageRel); }//from w w w .j a v a 2 s . co m return imageRel; }
From source file:info.schnatterer.songbirdDbTools.Utils.ResourceUtils.java
/** * Get the relative path from one file to another, specifying the directory separator. If one of the provided * resources does not exist, it is assumed to be a file unless it ends with '/' or '\'. * /*from w w w.j a v a2 s .co m*/ * @param targetPath * targetPath is calculated to this file * @param basePath * basePath is calculated from this file * @param pathSeparator * directory separator. The platform default is not assumed so that we can test Unix behavior when * running on Windows (for example) * @return <code>targetPath</code> relativized to <code>basePath</code> * * @author http://stackoverflow.com/questions/204784/how-to-construct-a-relative * -path-in-java-from-two-absolute-paths-or-urls */ public static String getRelativePath(final String basePath, final String targetPath, final String pathSeparator) { // Normalize the paths String normalizedTargetPath = FilenameUtils.normalizeNoEndSeparator(targetPath); String normalizedBasePath = FilenameUtils.normalizeNoEndSeparator(basePath); // Undo the changes to the separators made by normalization if (pathSeparator.equals("/")) { normalizedTargetPath = FilenameUtils.separatorsToUnix(normalizedTargetPath); normalizedBasePath = FilenameUtils.separatorsToUnix(normalizedBasePath); } else if (pathSeparator.equals("\\")) { normalizedTargetPath = FilenameUtils.separatorsToWindows(normalizedTargetPath); normalizedBasePath = FilenameUtils.separatorsToWindows(normalizedBasePath); } else { throw new IllegalArgumentException("Unrecognised dir separator '" + pathSeparator + "'"); } String[] base = normalizedBasePath.split(Pattern.quote(pathSeparator)); String[] target = normalizedTargetPath.split(Pattern.quote(pathSeparator)); // First get all the common elements. Store them as a string, // and also count how many of them there are. StringBuffer common = new StringBuffer(); int commonIndex = 0; while (commonIndex < target.length && commonIndex < base.length && target[commonIndex].equals(base[commonIndex])) { common.append(target[commonIndex] + pathSeparator); commonIndex++; } if (commonIndex == 0) { // No single common path element. This most // likely indicates differing drive letters, like C: and D:. // These paths cannot be relativized. throw new PathResolutionException("No common path element found for '" + normalizedTargetPath + "' and '" + normalizedBasePath + "'"); } // The number of directories we have to backtrack depends on whether the // base is a file or a dir // For example, the relative path from // // /foo/bar/baz/gg/ff to /foo/bar/baz // // ".." if ff is a file // "../.." if ff is a directory // // The following is a heuristic to figure out if the base refers to a // file or dir. It's not perfect, because // the resource referred to by this path may not actually exist, but // it's the best I can do boolean baseIsFile = true; File baseResource = new File(normalizedBasePath); if (baseResource.exists()) { baseIsFile = baseResource.isFile(); } else if (basePath.endsWith(pathSeparator)) { baseIsFile = false; } StringBuffer relative = new StringBuffer(); if (base.length != commonIndex) { int numDirsUp; if (baseIsFile) { numDirsUp = base.length - commonIndex - 1; } else { numDirsUp = base.length - commonIndex; } for (int i = 0; i < numDirsUp; i++) { relative.append(".." + pathSeparator); } } relative.append(normalizedTargetPath.substring(common.length())); return relative.toString(); }
From source file:com.centeractive.ws.builder.utils.ResourceUtils.java
private static String constructResourcePath(String packagePath, String resourceName) { String resourcePath = String.format("/%s/%s", packagePath, resourceName); String resourcePathUnixSeparators = FilenameUtils.separatorsToUnix(resourcePath); String resourcePathNoLeadingSeparators = removeLeadingUnixSeparators(resourcePathUnixSeparators); String normalizedResourcePath = FilenameUtils.normalizeNoEndSeparator(resourcePathNoLeadingSeparators, true);/*from w w w.j ava2 s . c o m*/ return normalizedResourcePath; }
From source file:com.xiaomi.linden.cluster.ShardClient.java
public ShardClient(final ZkClient zkClient, final String zk, final String path, final LindenService.ServiceIface localClient, final int localPort, final int shardId) { this.zk = zk; this.path = path; this.localClient = localClient; this.shardId = shardId; this.localHostPort = String.format("%s:%s", CommonUtils.getLocalHost(), localPort); // if the path does not exist, create it. // the path may be created later, so the listener will not work // and the isAvailable will always be false. if (!zkClient.exists(path)) { zkClient.createPersistent(path, true); }/*from w w w. ja v a 2s . c o m*/ List<String> children = zkClient.getChildren(path); final Map<String, Map.Entry<String, LindenService.ServiceIface>> lindenClients = new ConcurrentHashMap<>(); zkClient.subscribeChildChanges(path, new LindenZKListener(path, children) { @Override public void onChildChange(String parent, List<String> children, List<String> newAdded, List<String> deleted) { for (String node : newAdded) { String fullPath = FilenameUtils.separatorsToUnix(FilenameUtils.concat(path, node)); byte[] bytes = zkClient.readData(fullPath); ServiceInstance instance = JSONObject.parseObject(new String(bytes), ServiceInstance.class); String hostPort = String.format("%s:%s", instance.getServiceEndpoint().getHost(), instance.getServiceEndpoint().getPort()); if (localHostPort.equals(hostPort)) { haslocalClient = true; lindenClients.put(node, new AbstractMap.SimpleEntry<>(hostPort, localClient)); LOGGER.info("Linden local node {} {} joined shard {}.", node, hostPort, shardId); } else { LindenService.ServiceIface client = Thrift.newIface(hostPort, LindenService.ServiceIface.class); lindenClients.put(node, new AbstractMap.SimpleEntry<>(hostPort, client)); LOGGER.info("Linden node {} {} joined shard {}.", node, hostPort, shardId); } } for (String node : deleted) { if (lindenClients.containsKey(node)) { String hostPort = lindenClients.get(node).getKey(); lindenClients.remove(node); LOGGER.info("Linden node {} {} left shard {}.", node, hostPort, shardId); } } // ensure the new node overrides the old node. List<String> sortedNodes = new ArrayList<>(); for (String node : lindenClients.keySet()) { sortedNodes.add(node); } Collections.sort(sortedNodes, Collections.reverseOrder()); Set<String> uniqueClients = new HashSet<>(); for (String node : sortedNodes) { String hostPort = lindenClients.get(node).getKey(); if (uniqueClients.contains(hostPort)) { lindenClients.remove(node); LOGGER.warn("Linden node {} {} is duplicated in shard {}, removed!", node, hostPort, shardId); } else { uniqueClients.add(hostPort); } } LOGGER.info("{} Linden node in shard {}.", lindenClients.size(), shardId); List<Map.Entry<String, LindenService.ServiceIface>> tempClients = new ArrayList<>(); for (String node : lindenClients.keySet()) { tempClients.add(lindenClients.get(node)); String hostPort = lindenClients.get(node).getKey(); LOGGER.info("Linden node {} {} on service in shard {}.", node, hostPort, shardId); } clients = tempClients; } }); }
From source file:com.geewhiz.pacify.utils.FileUtils.java
/** * /*from w ww.ja v a2 s. c o m*/ * @param relativePath * @param regEx * file separator should be unix file separator * @return */ public static Boolean matches(Path relativePath, String regEx) { // we need to convert it, otherwise we have to mess around with // backslash quoting and regex String unixFileformat = FilenameUtils.separatorsToUnix(relativePath.toString()); return Pattern.matches(regEx, unixFileformat); }
From source file:hudson.plugins.project_inheritance.util.PathMapping.java
/** * Get the relative path from one file to another, specifying the directory separator. * If one of the provided resources does not exist, it is assumed to be a file unless it ends with '/' or * '\'.//from w w w . j a va 2 s.c o m * * @param targetPath targetPath is calculated to this file * @param basePath basePath is calculated from this file * @param pathSeparator directory separator. The platform default is not assumed so that we can test Unix behaviour when running on Windows (for example) * @return */ public static String getRelativePath(String targetPath, String basePath, String pathSeparator) { // Normalize the paths String normalizedTargetPath = FilenameUtils.normalizeNoEndSeparator(targetPath); String normalizedBasePath = FilenameUtils.normalizeNoEndSeparator(basePath); // Undo the changes to the separators made by normalization if (pathSeparator.equals("/")) { normalizedTargetPath = FilenameUtils.separatorsToUnix(normalizedTargetPath); normalizedBasePath = FilenameUtils.separatorsToUnix(normalizedBasePath); } else if (pathSeparator.equals("\\")) { normalizedTargetPath = FilenameUtils.separatorsToWindows(normalizedTargetPath); normalizedBasePath = FilenameUtils.separatorsToWindows(normalizedBasePath); } else { throw new IllegalArgumentException("Unrecognised dir separator '" + pathSeparator + "'"); } String[] base = normalizedBasePath.split(Pattern.quote(pathSeparator)); String[] target = normalizedTargetPath.split(Pattern.quote(pathSeparator)); // First get all the common elements. Store them as a string, // and also count how many of them there are. StringBuilder common = new StringBuilder(); int commonIndex = 0; while (commonIndex < target.length && commonIndex < base.length && target[commonIndex].equals(base[commonIndex])) { common.append(target[commonIndex] + pathSeparator); commonIndex++; } if (commonIndex == 0) { // No single common path element. This most // likely indicates differing drive letters, like C: and D:. // These paths cannot be relativized. throw new PathResolutionException("No common path element found for '" + normalizedTargetPath + "' and '" + normalizedBasePath + "'"); } // The number of directories we have to backtrack depends on whether the base is a file or a dir // For example, the relative path from // // /foo/bar/baz/gg/ff to /foo/bar/baz // // ".." if ff is a file // "../.." if ff is a directory // // The following is a heuristic to figure out if the base refers to a file or dir. It's not perfect, because // the resource referred to by this path may not actually exist, but it's the best I can do boolean baseIsFile = true; File baseResource = new File(normalizedBasePath); if (baseResource.exists()) { baseIsFile = baseResource.isFile(); } else if (basePath.endsWith(pathSeparator)) { baseIsFile = false; } StringBuilder relative = new StringBuilder(); if (base.length != commonIndex) { int numDirsUp = baseIsFile ? base.length - commonIndex - 1 : base.length - commonIndex; for (int i = 0; i < numDirsUp; i++) { relative.append(".." + pathSeparator); } } relative.append(normalizedTargetPath.substring(common.length())); return relative.toString(); }
From source file:com.thoughtworks.go.domain.ArtifactPlan.java
public String getSrc() { return FilenameUtils.separatorsToUnix(src); }
From source file:com.thoughtworks.go.buildsession.GenerateTestReportCommandExecutor.java
private ArrayList<File> findMatchedSourceFiles(BuildSession buildSession, File workingDirectory, String[] sources) {/*from w w w . j a v a 2 s. co m*/ ArrayList<File> allFiles = new ArrayList<>(); for (String src : sources) { File source = new File(FileUtil.applyBaseDirIfRelativeAndNormalize(workingDirectory, new File(src))); WildcardScanner wildcardScanner = new WildcardScanner(workingDirectory, src); File[] files = wildcardScanner.getFiles(); if (files.length > 0) { final List<File> fileList = files == null ? new ArrayList<>() : Arrays.asList(files); allFiles.addAll(fileList); } else { final String message = MessageFormat.format( "The Directory {0} specified as a test artifact was not found." + " Please check your configuration", FilenameUtils.separatorsToUnix(source.getPath())); buildSession.printlnWithPrefix(message); LOG.error(message); } } return allFiles; }
From source file:com.thoughtworks.go.domain.ArtifactPlan.java
public String getDest() { return FilenameUtils.separatorsToUnix(dest); }