Example usage for org.apache.commons.io FilenameUtils normalizeNoEndSeparator

List of usage examples for org.apache.commons.io FilenameUtils normalizeNoEndSeparator

Introduction

In this page you can find the example usage for org.apache.commons.io FilenameUtils normalizeNoEndSeparator.

Prototype

public static String normalizeNoEndSeparator(String filename, boolean unixSeparator) 

Source Link

Document

Normalizes a path, removing double and single dot path steps, and removing any final directory separator.

Usage

From source file:org.pentaho.platform.repository.RepositoryFilenameUtils.java

/**
 * Normalizes a path, removing double and single dot path steps, and removing any final directory separator.
 * <p/>//from w  ww.java 2s . c  om
 * This method normalizes a path to a standard format.
 * <p/>
 * A trailing slash will be removed. A double slash will be merged to a single slash (but UNC names are handled).
 * A single dot path segment will be removed. A double dot will cause that path segment and the one before to be
 * removed. If the double dot has no parent path segment to work with, <code>null</code> is returned.
 * <p/>
 * The output will be the same on both Unix and Windows except for the separator character.
 * 
 * <pre>
 * /foo//               -->   /foo
 * /foo/./              -->   /foo
 * /foo/../bar          -->   /bar
 * /foo/../bar/         -->   /bar
 * /foo/../bar/../baz   -->   /baz
 * //foo//./bar         -->   /foo/bar
 * /../                 -->   null
 * ../foo               -->   null
 * foo/bar/..           -->   foo
 * foo/../../bar        -->   null
 * foo/../bar           -->   bar
 * </pre>
 * 
 * @param filename
 *          the filename to normalize, null returns null
 * @return the normalized filename, or null if invalid
 */
public static String normalizeNoEndSeparator(final String filename) {
    return FilenameUtils.normalizeNoEndSeparator(filename, true);
}

From source file:org.sonar.plugins.openedge.foundation.OpenEdgeProjectHelper.java

public static String getPathRelativeToSourceDirs(File file, List<String> propath) {
    for (String entry : propath) {
        String s = getRelativePath(FilenameUtils.normalizeNoEndSeparator(file.getAbsolutePath(), true), entry);
        if (s.length() != 0)
            return s;
    }/*from w w w .j a v a2  s.c om*/
    return "";
}

From source file:org.sonar.plugins.openedge.foundation.OpenEdgeSettings.java

public OpenEdgeSettings(Settings settings, FileSystem fileSystem) {
    this.settings = settings;

    // Looking for source directories
    String sonarSources = settings.getString("sonar.sources");
    if (sonarSources == null) {
        sourceDirs.add(FilenameUtils.normalizeNoEndSeparator(fileSystem.baseDir().getAbsolutePath(), true));
    } else {/*from   ww w. j  a  v a2 s.  com*/
        for (String str : Splitter.on(',').trimResults().split(sonarSources)) {
            String dir = FilenameUtils
                    .normalizeNoEndSeparator(new File(fileSystem.baseDir(), str).getAbsolutePath(), true);
            sourceDirs.add(dir);
        }
    }

    // And for .pct directory
    String binariesSetting = settings.getString(OpenEdgePlugin.BINARIES);
    if (binariesSetting == null) {
        LOG.warn("Property {} not defined, using default value", OpenEdgePlugin.BINARIES);
        binariesSetting = "build";
    }
    File binaries = new File(fileSystem.baseDir(), binariesSetting);
    this.pctDir = new File(binaries, ".pct");
    this.dbgDir = new File(binaries, ".dbg");
    // Getting ClassInformation objects from rcode in binaries dir
    /* XXX if (binaries.exists() && binaries.isDirectory()) {
      readRCodeFromDirectory(binaries, genClasses);
    }
    LOG.info("{} classes read from {} directory", genClasses.keySet().size(), binariesSetting); */

    // PROPATH definition
    String propathProp = settings.getString(OpenEdgePlugin.PROPATH);
    LOG.info("Using PROPATH : {}", propathProp);
    if (propathProp != null) {
        for (String str : Splitter.on(',').trimResults().split(propathProp)) {
            File entry = fileSystem.resolvePath(str);
            LOG.debug("Adding {} to PROPATH", entry.getAbsolutePath());
            propath.add(entry);
        }
    }
    String dlcInstallDir = settings.getString(OpenEdgePlugin.DLC);
    boolean dlcInPropath = settings.getBoolean(OpenEdgePlugin.PROPATH_DLC);
    if (dlcInPropath && !Strings.isNullOrEmpty(dlcInstallDir)) {
        File dlc = new File(dlcInstallDir);
        LOG.info("Adding DLC directory '{}' to PROPATH", dlc.getAbsolutePath());
        propath.add(new File(dlc, "gui"));
        propath.add(new File(dlc, "tty"));
        propath.add(new File(dlc, "src"));
        propath.add(dlc);
    }
    // Getting ClassInformation objects from rcode in propath
    /* XXX for (File entry : propath) {
      if (entry.isDirectory()) {
        readRCodeFromDirectory(entry, ppClasses);
      } else if (entry.isFile() && "pl".equalsIgnoreCase(Files.getFileExtension(entry.getName()))) {
        readRCodeFromPL(entry, ppClasses);
      }
    }
    LOG.info("{} classes read from PROPATH entries", ppClasses.keySet().size(), binariesSetting);*/

    // File definition for temporary .schema file
    File dbFile;
    try {
        dbFile = File.createTempFile("proparse", ".schema");
    } catch (IOException caught) {
        LOG.error("Unable to create proparse.schema file", caught);
        throw new RuntimeException(caught);
    }

    // Database definitions
    String dbs = settings.getString(OpenEdgePlugin.DATABASES);
    LOG.info("Using schema : {}", dbs);
    if (dbs != null) {
        try (BufferedWriter writer = Files.newWriter(dbFile, Charsets.UTF_8)) {
            for (String str : Splitter.on(',').trimResults().split(dbs)) {
                String dbName = "";
                int colonPos = str.lastIndexOf(':');
                if (colonPos == -1) {
                    dbName = FilenameUtils.getBaseName(str);
                } else {
                    dbName = str.substring(colonPos + 1);
                    str = str.substring(0, colonPos);
                }
                LOG.debug("Parsing {} with alias {}", fileSystem.resolvePath(str), dbName);
                DatabaseDescription desc = DumpFileUtils.getDatabaseDescription(fileSystem.resolvePath(str));
                // XXX dbDesc.put(dbName, mapToDatabaseKeyword(desc, dbName).getTables());
                writer.write(":: " + dbName);
                writer.newLine();
                for (Table tbl : desc.getTables()) {
                    writer.write(": " + tbl.getName() + " ");
                    writer.newLine();
                    for (Field fld : tbl.getFields()) {
                        writer.write(fld.getName() + " " + fld.getDataType().toUpperCase() + " "
                                + (fld.getExtent() == null ? "0" : fld.getExtent()));
                        writer.newLine();
                    }
                }
            }
        } catch (IOException caught) {
            LOG.error("Unable to write proparse.schema file", caught);
        }
    }

    Schema sch = null;
    try {
        sch = new Schema(dbFile.getAbsolutePath(), true);
        if (!sch.getDbSet().isEmpty()) {
            sch.createAlias("dictdb", sch.getDbSet().first().getName());
        }
        if (settings.getString(OpenEdgePlugin.ALIASES) != null) {
            for (String str : Splitter.on(';').trimResults()
                    .split(settings.getString(OpenEdgePlugin.ALIASES))) {
                List<String> lst = Splitter.on(',').trimResults().splitToList(str);
                for (String alias : lst.subList(1, lst.size())) {
                    LOG.debug("Adding {} aliases to database {}", new Object[] { alias, lst.get(0) });
                    sch.createAlias(alias, lst.get(1));
                }
            }
        }
    } catch (IOException caught) {
        LOG.error("Unable to read proparse.schema file", caught);
    }
    dbFile.delete();

    // CPD annotations
    for (String str : Strings.nullToEmpty(settings.getString(OpenEdgePlugin.CPD_ANNOTATIONS)).split(",")) {
        LOG.debug("CPD annotation : '{}'", str);
        cpdAnnotations.add(str);
    }

    IProgressSettings settings1 = new ProgressSettings(true, "", "WIN32", getPropathAsString(), "11.5",
            "MS-WIN95");
    IProparseSettings settings2 = new ProparseSettings();
    proparseSession = new RefactorSession(settings1, settings2, sch, fileSystem.encoding());
}

From source file:org.yamj.core.service.staging.StagingService.java

@Transactional(propagation = Propagation.REQUIRED)
public Library storeLibrary(ImportDTO libraryDTO) {
    Library library = stagingDao.getLibrary(libraryDTO.getClient(), libraryDTO.getPlayerPath());
    if (library == null) {
        library = new Library();
        library.setClient(libraryDTO.getClient());
        library.setPlayerPath(libraryDTO.getPlayerPath());
    }//from  ww  w .  j a va  2 s .  c o  m
    library.setBaseDirectory(FilenameUtils.normalizeNoEndSeparator(libraryDTO.getBaseDirectory(), true));
    library.setLastScanned(new Date(System.currentTimeMillis()));
    stagingDao.storeEntity(library);
    return library;
}

From source file:org.yamj.core.service.staging.StagingService.java

@Transactional(propagation = Propagation.REQUIRED)
public void storeStageDirectory(StageDirectoryDTO stageDirectoryDTO, Library library) {
    // normalize the directory path by using URI
    String normalized = FilenameUtils.normalizeNoEndSeparator(stageDirectoryDTO.getPath(), true);

    StageDirectory stageDirectory = stagingDao.getStageDirectory(normalized, library);
    if (stageDirectory == null) {
        stageDirectory = new StageDirectory();
        stageDirectory.setDirectoryPath(normalized);
        stageDirectory.setLibrary(library);
        stageDirectory.setStatus(StatusType.NEW);
        stageDirectory.setDirectoryDate(new Date(stageDirectoryDTO.getDate()));

        // getById parent stage directory
        int lastIndex = normalized.lastIndexOf('/');
        if (lastIndex > 0) {
            String parentPath = normalized.substring(0, lastIndex);
            StageDirectory parent = stagingDao.getStageDirectory(parentPath, library);
            if (parent != null) {
                stageDirectory.setParentDirectory(parent);
            }// ww w  . ja va2s.  c  om
        }

        stagingDao.saveEntity(stageDirectory);
    } else {
        Date newDate = new Date(stageDirectoryDTO.getDate());
        if (newDate.compareTo(stageDirectory.getDirectoryDate()) != 0) {
            stageDirectory.setDirectoryDate(new Date(stageDirectoryDTO.getDate()));
            stageDirectory.setStatus(StatusType.UPDATED);
            stagingDao.updateEntity(stageDirectory);
        }
    }

    for (StageFileDTO stageFileDTO : stageDirectoryDTO.getStageFiles()) {
        String baseName = FilenameUtils.getBaseName(stageFileDTO.getFileName());
        String extension = FilenameUtils.getExtension(stageFileDTO.getFileName());
        if (StringUtils.isBlank(baseName) || StringUtils.isBlank(extension)) {
            // no valid baseName or extension
            continue;
        }

        StageFile stageFile = stagingDao.getStageFile(baseName, extension, stageDirectory);
        if (stageFile == null) {
            // create new stage file entry
            stageFile = new StageFile();
            stageFile.setBaseName(baseName);
            stageFile.setExtension(extension);
            stageFile.setFileDate(new Date(stageFileDTO.getFileDate()));
            stageFile.setFileSize(stageFileDTO.getFileSize());
            stageFile.setStageDirectory(stageDirectory);
            stageFile.setFileType(filenameScanner.determineFileType(extension));
            stageFile
                    .setFullPath(FilenameUtils.concat(stageDirectoryDTO.getPath(), stageFileDTO.getFileName()));
            stageFile.setStatus(StatusType.NEW);
            stagingDao.saveEntity(stageFile);
        } else {
            Date newDate = new Date(stageFileDTO.getFileDate());
            if ((newDate.compareTo(stageFile.getFileDate()) != 0)
                    || (stageFile.getFileSize() != stageFileDTO.getFileSize())) {
                stageFile.setFileDate(new Date(stageFileDTO.getFileDate()));
                stageFile.setFileSize(stageFileDTO.getFileSize());
                if (!StatusType.DUPLICATE.equals(stageFile.getStatus())) {
                    // mark as updated if no duplicate
                    stageFile.setStatus(StatusType.UPDATED);
                }
                stagingDao.updateEntity(stageFile);
            }
        }
    }
}