Example usage for java.nio.file Path getName

List of usage examples for java.nio.file Path getName

Introduction

In this page you can find the example usage for java.nio.file Path getName.

Prototype

Path getName(int index);

Source Link

Document

Returns a name element of this path as a Path object.

Usage

From source file:org.xwiki.filter.xff.internal.input.ClassesReader.java

private void routeProperty(Path path, InputStream inputStream) throws FilterException {
    String filename = path.getFileName().toString();
    String filePropertyName = path.getName(path.getNameCount() - 2).toString();
    for (Property property : this.xClass.getProperties()) {
        String propertyName = property.getName();
        if (filePropertyName.equals(propertyName)) {
            for (Attribute attribute : property.getAttributes()) {
                String attributeName = attribute.getName();
                if (filename.startsWith(attributeName + '.')) {
                    try {
                        attribute.setValue(IOUtils.toString(inputStream));
                    } catch (IOException e) {
                        String message = String.format("Unable to read a string from '%s'.", path.toString());
                        throw new FilterException(message, e);
                    }/*from   ww  w .  ja v a 2  s.  c  o m*/
                }
            }
        }
    }
}

From source file:org.sleuthkit.autopsy.casemodule.SingleUserCaseConverter.java

/**
 * Fix up any paths in the database that refer to items that have moved.
 * Candidates include events.db, input images, reports, file paths, etc.
 *
 * @param icd the Import Case Data for the current case
 *
 * @throws Exception/*from   w ww .  j ava 2 s .  co  m*/
 * @throws SQLExceptionException
 */
private static void fixPaths(ImportCaseData icd) throws SQLException, Exception {
    /// Fix paths in reports, tsk_files_path, and tsk_image_names tables

    String input = icd.getImageInputFolder().toString();
    String output = icd.getImageOutputFolder().toString();

    Connection postgresqlConnection = getPostgreSQLConnection(icd);

    if (postgresqlConnection != null) {
        String hostName = NetworkUtils.getLocalHostName();

        // add hostname to reports
        Statement updateStatement = postgresqlConnection.createStatement();
        updateStatement.executeUpdate("UPDATE reports SET path=CONCAT('" + hostName
                + "/', path) WHERE path IS NOT NULL AND path != ''"); //NON-NLS

        // add hostname to tsk_files_path
        updateStatement = postgresqlConnection.createStatement();
        updateStatement.executeUpdate("UPDATE tsk_files_path SET path=CONCAT('" + hostName
                + "\\', path) WHERE path IS NOT NULL AND path != ''"); //NON-NLS

        String caseName = icd.getRawFolderName().toLowerCase();

        if (icd.getCopySourceImages()) {
            // update path for images
            Statement inputStatement = postgresqlConnection.createStatement();
            ResultSet inputResultSet = inputStatement.executeQuery("SELECT * FROM tsk_image_names"); //NON-NLS

            while (inputResultSet.next()) {
                Path oldPath = Paths.get(inputResultSet.getString(2));

                for (int x = 0; x < oldPath.getNameCount(); ++x) {
                    if (oldPath.getName(x).toString().toLowerCase().equals(caseName)) {
                        Path newPath = Paths.get(output,
                                oldPath.subpath(x + 1, oldPath.getNameCount()).toString());
                        updateStatement = postgresqlConnection.createStatement();
                        updateStatement.executeUpdate("UPDATE tsk_image_names SET name='" + newPath.toString()
                                + "' WHERE obj_id = " + inputResultSet.getInt(1)); //NON-NLS
                        break;
                    }
                }
            }
        }
        postgresqlConnection.close();
    } else {
        throw new Exception(NbBundle.getMessage(SingleUserCaseConverter.class,
                "SingleUserCaseConverter.CanNotOpenDatabase")); //NON-NLS
    }
}

From source file:com.basistech.yca.FlatteningConfigFileManager.java

private String[] parsePid(Path path) {
    String filename = path.getName(path.getNameCount() - 1).toString();
    String pid = filename.substring(0, filename.lastIndexOf('.'));
    int n = pid.indexOf('-');
    if (n > 0) {
        String factoryPid = pid.substring(n + 1);
        pid = pid.substring(0, n);/*from w w w. java 2s  . c om*/
        return new String[] { pid, factoryPid };
    } else {
        return new String[] { pid, null };
    }
}

From source file:fr.lille1.car.burihabwa.rest.utils.FTPAdapterImpl.java

public String getFile(final String path) {
    Path completePath = Paths.get(path);
    int nameCount = completePath.getNameCount();
    return completePath.getName(nameCount - 1).toString();
}

From source file:com.surevine.gateway.scm.IncomingProcessorImpl.java

private Path findFileEndsWith(final Collection<Path> paths, final String endsWith) {
    for (final Path entry : paths) {
        final String lastPath = entry.getName(entry.getNameCount() - 1).toString();
        if (StringUtils.endsWith(lastPath, endsWith)) {
            return entry;
        }//  w w w  .j  ava  2s  .c om
    }
    return null;
}

From source file:com.fizzed.stork.deploy.Archive.java

public Path unpack(Path unpackDir) throws IOException {
    Files.createDirectories(unpackDir);

    log.info("Unpacking {} to {}", file, unpackDir);

    // we need to know the top-level dir(s) created by unpack
    final Set<Path> firstLevelPaths = new LinkedHashSet<>();
    final AtomicInteger count = new AtomicInteger();

    try (ArchiveInputStream ais = newArchiveInputStream(file)) {
        ArchiveEntry entry;//from  w  ww  .  j  ava  2s .  c o  m
        while ((entry = ais.getNextEntry()) != null) {
            try {
                Path entryFile = Paths.get(entry.getName());
                Path resolvedFile = unpackDir.resolve(entryFile);

                firstLevelPaths.add(entryFile.getName(0));

                log.debug("{}", resolvedFile);

                if (entry.isDirectory()) {
                    Files.createDirectories(resolvedFile);
                } else {
                    unpackEntry(ais, resolvedFile);
                    count.incrementAndGet();
                }
            } catch (IOException | IllegalStateException | IllegalArgumentException e) {
                log.error("", e);
                throw new RuntimeException(e);
            }
        }
    }

    if (firstLevelPaths.size() != 1) {
        throw new IOException("Only archives with a single top-level directory are supported!");
    }

    Path assemblyDir = unpackDir.resolve(firstLevelPaths.iterator().next());

    log.info("Unpacked {} files to {}", count, assemblyDir);

    return assemblyDir;
}

From source file:org.apache.gobblin.config.store.zip.ZipFileConfigStore.java

/**
 * Retrieves all the children of the given {@link ConfigKeyPath} using {@link Files#walk} to list files
 *//*from   w  ww  .jav a  2  s.c o  m*/
@Override
public Collection<ConfigKeyPath> getChildren(ConfigKeyPath configKey, String version)
        throws VersionDoesNotExistException {
    Preconditions.checkNotNull(configKey, "configKey cannot be null!");
    Preconditions.checkArgument(version.equals(getCurrentVersion()));

    List<ConfigKeyPath> children = new ArrayList<>();
    Path datasetDir = getDatasetDirForKey(configKey);

    try {

        if (!Files.exists(this.fs.getPath(datasetDir.toString()))) {
            return children;
        }

        Stream<Path> files = Files.walk(datasetDir, 1);

        for (Iterator<Path> it = files.iterator(); it.hasNext();) {
            Path path = it.next();

            if (Files.isDirectory(path) && !path.equals(datasetDir)) {
                children.add(configKey
                        .createChild(StringUtils.removeEnd(path.getName(path.getNameCount() - 1).toString(),
                                SingleLinkedListConfigKeyPath.PATH_DELIMETER)));
            }

        }
        return children;
    } catch (IOException e) {
        throw new RuntimeException(
                String.format("Error while getting children for configKey: \"%s\"", configKey), e);
    }
}

From source file:com.surevine.gateway.scm.IncomingProcessorImpl.java

public Path getTmpExtractionPath(final Path archivePath) {
    String tmpDir = PropertyUtil.getTempDir();
    final String filename = archivePath.getName(archivePath.getNameCount() - 1).toString();
    tmpDir += "/" + filename.substring(0, filename.indexOf('.'));
    return new File(tmpDir).toPath();
}

From source file:org.openhab.tools.analysis.checkstyle.ServiceComponentManifestCheck.java

private int getIndex(Path path, String dirName) {
    int result = -1;
    for (int i = 0; i < path.getNameCount(); i++) {
        if (path.getName(i).toString().equals(dirName)) {
            result = i;//  w  ww.ja  va 2  s .  c o  m
            break;
        }
    }
    return result;
}

From source file:de.ncoder.studipsync.Syncer.java

public boolean areFilesInSync(List<Download> downloads, List<Path> localFiles) throws IOException {
    if (!checkLevel.includes(CheckLevel.Files)) {
        return true;
    }/*  w w w . j  a v a 2s . c  o m*/
    for (Download download : downloads) {
        //Find matching candidates
        List<Path> localCandidates = new LinkedList<>();
        for (Path local : localFiles) {
            // Check candidate name
            if (local.getName(local.getNameCount() - 1).toString().equals(download.getFileName())) {
                if (!localCandidates.isEmpty()) {
                    //Already found a candidate
                    log.debug(marker,
                            "Local files " + localCandidates + " and " + local + " match " + download + "!");
                }
                localCandidates.add(local);

                //Check LastModifiedTime
                if (!checkLevel.includes(CheckLevel.ModTime)) {
                    continue;
                }
                Date localLastMod = new Date(Files.getLastModifiedTime(local).toMillis());
                if (!localLastMod.after(download.getLastModified())) {
                    //Candidate *potentially* outdated
                    log.warn(marker, "Local file " + local + "(" + localLastMod + ") older than online Version "
                            + download + "(" + download.getLastModified() + ")!");
                    return false;
                }
            }
        }

        //Require at least one candidate
        if (localCandidates.isEmpty()) {
            //No candidates found
            log.warn(marker, "No local file matching " + download + " (~" + download.getFileName() + ")!");
            return false;
        }
    }
    return true;
}