List of usage examples for org.apache.commons.io FilenameUtils normalize
public static String normalize(String filename)
From source file:edu.umn.msi.tropix.storage.core.monitor.StorageMonitorClosureImpl.java
private File findMatchingDirectory(File file) { File rootDirectory = null;// www. j a va 2s .c om for (final File directory : monitorConfig.getDirectories()) { if (FilenameUtils.normalize(file.getAbsolutePath()) .startsWith(FilenameUtils.normalize(directory.getAbsolutePath()))) { rootDirectory = directory; break; } } return rootDirectory; }
From source file:com.sangupta.jerry.util.FileUtils.java
/** * Resolve a file path into its corresponding {@link File} object. This method * also makes sure that '~' can be used to refer to the user's home directory - the * standard way on Linux and OS X.//from w ww. ja v a 2s .co m * * @param filePath * @return <code>null</code> if filePath is empty, {@link File} instance otherwise. * */ public static File resolveToFile(String filePath) { if (AssertUtils.isEmpty(filePath)) { return null; } if ("..".equals(filePath)) { return new File(".").getAbsoluteFile().getParentFile().getParentFile(); } if (filePath.startsWith("../") || filePath.startsWith("..\\")) { // replace initial path with current path filePath = new File(".").getAbsoluteFile().getParentFile().getParentFile().getAbsolutePath() + File.separator + filePath.substring(2); } if (filePath.startsWith("./") || filePath.startsWith(".\\")) { // replace initial path with current path filePath = new File(".").getAbsoluteFile().getParentFile().getAbsolutePath() + File.separator + filePath.substring(2); } // normalize filePath = FilenameUtils.normalize(filePath); // now check String prefix = FilenameUtils.getPrefix(filePath); if (AssertUtils.isEmpty(prefix)) { return new File(filePath); } // check for user home if (filePath.charAt(0) == '~') { if (filePath.length() == 1) { return getUsersHomeDirectory(); } if (filePath.length() == 2 && filePath.equals("~/")) { return getUsersHomeDirectory(); } return new File(getUsersHomeDirectory(), filePath.substring(2)); } return new File(filePath); }
From source file:com.ariht.maven.plugins.config.io.DirectoryReader.java
/** * Directories and/or specific files you do not wish to include in config generation * are converted from String to File instances. *//*w w w .java2 s.co m*/ private List<File> convertStringsToFiles(final List<String> filesToIgnore) { if (filesToIgnore == null || filesToIgnore.isEmpty()) { return EMPTY_FILE_LIST; } final List<File> filesIgnored = new ArrayList<File>(filesToIgnore.size()); for (String fileToIgnore : new LinkedHashSet<String>(filesToIgnore)) { if (StringUtils.isNotBlank(fileToIgnore)) { fileToIgnore = FilenameUtils.separatorsToUnix(FilenameUtils.normalize(fileToIgnore.trim())); final File file = new File(fileToIgnore); if (file.exists()) { log.debug("Adding ignore for file: " + file.getAbsolutePath()); filesIgnored.add(file); } } } return filesIgnored; }
From source file:jenkins.plugins.shiningpanda.PythonInstallation.java
/** * Check if the installation is in workspace * //from ww w. j a v a 2 s.c o m * @param ws * The workspace file path * @return Return true if in workspace */ public boolean isInWorkspace(FilePath ws) { return FilenameUtils.normalize(getHome()).startsWith(FilenameUtils.normalize(ws.getRemote())); }
From source file:com.uwsoft.editor.proxy.ProjectManager.java
public void createEmptyProject(String projectPath, int width, int height, int pixelPerWorldUnit) throws IOException { /*// w w w . java2 s .co m if (workspacePath.endsWith(File.separator)) { workspacePath = workspacePath.substring(0, workspacePath.length() - 1); } String projPath = workspacePath + File.separator + projectName; */ String projectName = new File(projectPath).getName(); String projPath = FilenameUtils.normalize(projectPath); FileUtils.forceMkdir(new File(projPath)); FileUtils.forceMkdir(new File(projPath + File.separator + "export")); FileUtils.forceMkdir(new File(projPath + File.separator + "assets")); FileUtils.forceMkdir(new File(projPath + File.separator + "scenes")); FileUtils.forceMkdir(new File(projPath + File.separator + "assets/orig")); FileUtils.forceMkdir(new File(projPath + File.separator + "assets/orig/images")); FileUtils.forceMkdir(new File(projPath + File.separator + "assets/orig/particles")); FileUtils.forceMkdir(new File(projPath + File.separator + "assets/orig/animations")); FileUtils.forceMkdir(new File(projPath + File.separator + "assets/orig/pack")); // create project file ProjectVO projVo = new ProjectVO(); projVo.projectName = projectName; projVo.projectVersion = ProjectVersionMigrator.dataFormatVersion; // create project info file ProjectInfoVO projInfoVo = new ProjectInfoVO(); projInfoVo.originalResolution.name = "orig"; projInfoVo.originalResolution.width = width; projInfoVo.originalResolution.height = height; projInfoVo.pixelToWorld = pixelPerWorldUnit; //TODO: add project orig resolution setting currentProjectVO = projVo; currentProjectInfoVO = projInfoVo; currentProjectPath = projPath; SceneDataManager sceneDataManager = facade.retrieveProxy(SceneDataManager.NAME); sceneDataManager.createNewScene("MainScene"); FileUtils.writeStringToFile(new File(projPath + "/project.pit"), projVo.constructJsonString(), "utf-8"); FileUtils.writeStringToFile(new File(projPath + "/project.dt"), projInfoVo.constructJsonString(), "utf-8"); }
From source file:energy.usef.environment.tool.config.ToolConfig.java
public static String getUsefEnvironmentFolder() { String usefEnvironmentFolder = null; String currentDirectory = FileUtil.getCurrentFolder(); if (currentDirectory.endsWith("usef-environment-tool")) { usefEnvironmentFolder = currentDirectory + File.separator + ".." + File.separator + USEF_ENVIRONMENT_FOLDER; } else {// w ww . ja va2s . c o m if (currentDirectory.endsWith("target")) { usefEnvironmentFolder = currentDirectory + File.separator + ".." + File.separator + ".." + File.separator + USEF_ENVIRONMENT_FOLDER; } } usefEnvironmentFolder = FilenameUtils.normalize(usefEnvironmentFolder); if (!FileUtil.isFolderExists(usefEnvironmentFolder)) { String error = "Trying to locate USEF environment folder and could not find the folder: " + usefEnvironmentFolder; LOGGER.error(error); throw new RuntimeException(error); } return usefEnvironmentFolder; }
From source file:avantssar.aslanpp.testing.Tester.java
public static void doTest(TranslationReport rep, ITestTask task, File sourceBaseDir, File targetBaseDir, ASLanPPConnectorImpl translator, TesterCommandLineOptions options, PrintStream err) { String relativePath = FilenameUtils.normalize(task.getASLanPP().getAbsolutePath()) .substring(FilenameUtils.normalizeNoEndSeparator(sourceBaseDir.getAbsolutePath()).length() + 1); File aslanPPinput = new File( FilenameUtils.concat(FilenameUtils.normalize(targetBaseDir.getAbsolutePath()), relativePath)); try {/*from w w w . j ava 2s .co m*/ FileUtils.copyFile(task.getASLanPP(), aslanPPinput, true); File aslanPPcheck1 = new File( FilenameUtils.removeExtension(aslanPPinput.getAbsolutePath()) + ".check1.aslan++"); File aslanPPcheck1det = new File( FilenameUtils.removeExtension(aslanPPinput.getAbsolutePath()) + ".check1.detailed.aslan++"); File aslanPPcheck1err = new File( FilenameUtils.removeExtension(aslanPPinput.getAbsolutePath()) + ".check1.errors.aslan++"); File aslanPPcheck1warn = new File( FilenameUtils.removeExtension(aslanPPinput.getAbsolutePath()) + ".check1.warnings.aslan++"); File aslanPPcheck2 = null; File aslanPPcheck2det = null; File aslanPPcheck2err = null; File aslanPPcheck2warn = null; File aslanFile = new File(FilenameUtils.removeExtension(aslanPPinput.getAbsolutePath()) + ".aslan"); File errorsFile = new File( FilenameUtils.removeExtension(aslanPPinput.getAbsolutePath()) + ".errors.txt"); File warningsFile = new File( FilenameUtils.removeExtension(aslanPPinput.getAbsolutePath()) + ".warnings.txt"); // Load the file and write it back to another file. // Then again load it back and write it to another file. // The files should have the same content. String fileName = aslanPPinput.getAbsolutePath(); String aslanppSpec = FileUtils.readFileToString(task.getASLanPP()); TranslatorOptions optPP = new TranslatorOptions(); optPP.setPrettyPrint(true); TranslatorOptions optPPdet = new TranslatorOptions(); optPPdet.setPreprocess(true); // EntityManager.getInstance().purge(); TranslatorOutput firstLoad = translator.translate(optPP, fileName, aslanppSpec); FileUtils.writeStringToFile(aslanPPcheck1, firstLoad.getSpecification()); FileUtils.writeLines(aslanPPcheck1err, firstLoad.getErrors()); aslanPPcheck1err = deleteIfZero(aslanPPcheck1err); FileUtils.writeLines(aslanPPcheck1warn, firstLoad.getWarnings()); aslanPPcheck1warn = deleteIfZero(aslanPPcheck1warn); // EntityManager.getInstance().purge(); TranslatorOutput firstLoadDet = translator.translate(optPPdet, fileName, aslanppSpec); // TODO should use translate_main, since result will be empty FileUtils.writeStringToFile(aslanPPcheck1det, firstLoadDet.getSpecification()); if (firstLoad.getSpecification() != null) { aslanPPcheck2 = new File( FilenameUtils.removeExtension(aslanPPinput.getAbsolutePath()) + ".check2.aslan++"); aslanPPcheck2det = new File( FilenameUtils.removeExtension(aslanPPinput.getAbsolutePath()) + ".check2.detailed.aslan++"); aslanPPcheck2err = new File( FilenameUtils.removeExtension(aslanPPinput.getAbsolutePath()) + ".check2.errors.aslan++"); aslanPPcheck2warn = new File( FilenameUtils.removeExtension(aslanPPinput.getAbsolutePath()) + ".check2.warnings.aslan++"); // EntityManager.getInstance().purge(); TranslatorOutput secondLoad = translator.translate(optPP, null, firstLoad.getSpecification()); FileUtils.writeStringToFile(aslanPPcheck2, secondLoad.getSpecification()); FileUtils.writeLines(aslanPPcheck2err, secondLoad.getErrors()); aslanPPcheck2err = deleteIfZero(aslanPPcheck2err); FileUtils.writeLines(aslanPPcheck2warn, secondLoad.getWarnings()); aslanPPcheck2warn = deleteIfZero(aslanPPcheck2warn); // EntityManager.getInstance().purge(); TranslatorOutput secondLoadDet = translator.translate(optPPdet, null, firstLoad.getSpecification()); FileUtils.writeStringToFile(aslanPPcheck2det, secondLoadDet.getSpecification()); } // EntityManager.getInstance().purge(); TranslatorOutput result = translator.translate(options, fileName, aslanppSpec); FileUtils.writeStringToFile(aslanFile, result.getSpecification()); FileUtils.writeLines(errorsFile, result.getErrors()); FileUtils.writeLines(warningsFile, result.getWarnings()); File expASLan = null; if (task.getExpectedASLan() != null) { expASLan = new File( FilenameUtils.removeExtension(aslanPPinput.getAbsolutePath()) + ".expected.aslan"); FileUtils.copyFile(task.getExpectedASLan(), expASLan, true); } TranslationInstance ti = rep.newInstance(aslanPPinput, deleteIfZero(aslanPPcheck1), deleteIfZero(aslanPPcheck1det), deleteIfZero(aslanPPcheck2), deleteIfZero(aslanPPcheck2det), result.getErrors().size(), deleteIfZero(errorsFile), result.getWarnings().size(), deleteIfZero(warningsFile), deleteIfZero(expASLan), deleteIfZero(aslanFile), task.getExpectedVerdict()); if (ti.hasTranslation()) { if (bm != null && bm.getBackendRunners().size() > 0) { for (final IBackendRunner runner : bm.getBackendRunners()) { try { List<String> pars = task.getBackendParameters(runner.getName()); pool.execute(new BackendTask(runner.spawn(), pars, aslanFile, ti)); } catch (BackendRunnerInstantiationException bex) { Debug.logger.error("Failed to spawn backend " + runner.getName() + ".", bex); } } } } } catch (Exception e) { System.out.println("Exception while testing file '" + task.getASLanPP().getAbsolutePath() + "': " + e.getMessage()); Debug.logger.error("Failed to test file '" + aslanPPinput + "'.", e); } }
From source file:com.isomorphic.maven.util.ArchiveUtils.java
/** * Derives a new file path, in whole or in part, from an existing path. The following use cases are supported explicitly: * <ul> /*w w w .jav a2 s. c om*/ * <li> * Rename a file (example: smartgwt-lgpl.jar) * <p/> * <code> * ArchiveUtils.rewritePath("smartgwtee-4.1d/lib/smartgwt.jar", "smartgwt-lgpl.jar"); * </code> * </li> * <li> * Move to another directory (example: target/smartgwt.jar) * <p/> * <code> * ArchiveUtils.rewritePath("smartgwtee-4.1d/lib/smartgwt.jar", "target"); * </code> * </li> * <li>Move and rename (example: target/smartgwt-lgpl.jar) * <p/> * <code> * ArchiveUtils.rewritePath("smartgwtee-4.1d/lib/smartgwt.jar", "target/smartgwt-lgpl.jar"); * </code> * </li> * <li>Move to new root directory, preserving some part of the existing path</li> * <ul> * <li> * example: doc/api/com/isomorphic/servlet/IDACall.html * <p/> * <code> * ArchiveUtils.rewritePath("smartgwtee-4.1d/doc/javadoc/com/isomorphic/servlet/IDACall.html","doc/api/#javadoc"); * </code> * </li> * <li> * example: doc/api/com/isomorphic/servlet/network/FileAssembly.html * <p/> * <code> * ArchiveUtils.rewritePath("smartgwtee-4.1d/doc/javadoc/com/isomorphic/servlet/CompressionFilter.html", "doc/api/#javadoc/network"); * </code> * </li> * </ul> * </ul> * * @param oldValue the existing path * @param newValue the value to use for the new path, including optional tokens * @return */ public static String rewritePath(String oldValue, String newValue) { String path = newValue; String filename; if ("".equals(FilenameUtils.getExtension(newValue))) { filename = FilenameUtils.getName(oldValue); } else { path = FilenameUtils.getPath(newValue); filename = FilenameUtils.getName(newValue); } Pattern p = Pattern.compile("(.*?)#(.*?)(?:$|(/.*))"); Matcher m = p.matcher(path); if (m.find()) { String prefix = m.group(1); String dir = m.group(2); String suffix = m.group(3); int index = oldValue.indexOf(dir); int length = dir.length(); String remainder = FilenameUtils.getPath(oldValue.substring(index + length)); if (prefix != null && !prefix.equals("")) { path = prefix + remainder; } else { path = remainder; } if (suffix != null && !suffix.equals("")) { path += suffix; } } return FilenameUtils.normalize(path + "/" + filename); }
From source file:net.sourceforge.jaulp.lang.PropertiesUtils.java
/** * Load properties.//w w w . ja v a2 s. c o m * * @param packagePath * the package path without the file name * @param fileName * the file name * @return the properties * @throws IOException * Signals that an I/O exception has occurred. */ public static Properties loadProperties(String packagePath, String fileName) throws IOException { StringBuilder sb = new StringBuilder(); packagePath = FilenameUtils.normalize(packagePath); String slash = "/"; if (packagePath.startsWith(slash)) { // remove first slash... if (packagePath.endsWith(slash)) { sb.append(packagePath.substring(1, packagePath.length())); } else { // append slash at the end... sb.append(packagePath.substring(1, packagePath.length())).append(slash); } } else { if (packagePath.endsWith(slash)) { // remove first char... sb.append(packagePath); } else { // remove first char... sb.append(packagePath).append(slash); } } packagePath = sb.toString().trim(); sb = new StringBuilder(); if (fileName.startsWith(slash)) { sb.append(fileName.substring(1, fileName.length())); } fileName = sb.toString().trim(); return loadProperties(packagePath + fileName); }
From source file:jenkins.plugins.publish_over.BPBuildInfo.java
private String removePrefix(final String relativePathToFile, final String expandedPrefix) { if (expandedPrefix == null) return relativePathToFile; String toRemove = Util .fixEmptyAndTrim(FilenameUtils.separatorsToUnix(FilenameUtils.normalize(expandedPrefix + "/"))); if (toRemove != null) { if (toRemove.charAt(0) == '/') toRemove = toRemove.substring(1); if (!relativePathToFile.startsWith(toRemove)) { throw new BapPublisherException( Messages.exception_removePrefix_noMatch(relativePathToFile, toRemove)); }// w w w .j av a2 s . c o m return relativePathToFile.substring(toRemove.length()); } return relativePathToFile; }