Example usage for java.nio.file Files isDirectory

List of usage examples for java.nio.file Files isDirectory

Introduction

In this page you can find the example usage for java.nio.file Files isDirectory.

Prototype

public static boolean isDirectory(Path path, LinkOption... options) 

Source Link

Document

Tests whether a file is a directory.

Usage

From source file:com.phoenixnap.oss.ramlapisync.plugin.SpringMvcRamlVerifierMojo.java

public void execute() throws MojoExecutionException, MojoFailureException {
    long startTime = System.currentTimeMillis();
    if (project.getPackaging().equals("pom")) {
        this.getLog().info("Skipping [pom] project: " + project.getName());

    } else if (project.getPackaging().equals("maven-plugin")) {
        this.getLog().info("Skipping [maven-plugin] project: " + project.getName());

    } else if (!Files.isDirectory(Paths.get(project.getBuild().getSourceDirectory()),
            LinkOption.NOFOLLOW_LINKS)) {
        this.getLog().info("Skipping project with missing src folder: " + project.getName());

    } else {//w w  w  .ja va 2 s  .c  o  m
        try {
            verifyRaml();
        } catch (IOException e) {
            ClassLoaderUtils.restoreOriginalClassLoader();
            throw new MojoExecutionException(e, "Unexpected exception while executing Raml Sync Plugin.",
                    e.toString());
        }
    }
    this.getLog().info("Raml Generation Complete in:" + (System.currentTimeMillis() - startTime) + "ms");
}

From source file:de.teamgrit.grit.checking.compile.HaskellCompileChecker.java

/**
 * This Method generates the command required to start the compiler. It
 * generates a list of strings that can be passed to a process builder.
 * /*from ww  w  .  ja  v  a 2 s . co  m*/
 * @param pathToProgramFile
 *            Where to look for the main file that will be compiled.
 * @param compilerName
 *            Which compiler to call
 * @param compilerFlags
 *            User supplied flags to be passed
 * @return List of string with the command for the process builder.
 * @throws BadCompilerSpecifiedException
 *             When no compiler is given.
 * @throws FileNotFoundException
 *             When the file to be compiled does not exist
 * @throws CompilerOutputFolderExistsException
 *             Due to slightly uncompatible CompileCheckerInterface this
 *             exception is in the declaration but it is never thrown.
 *             JavaCompileChecker uses this exception.
 */
private List<String> createCompilerInvocation(Path pathToProgramFile, String compilerName,
        List<String> compilerFlags)
        throws BadCompilerSpecifiedException, FileNotFoundException, CompilerOutputFolderExistsException {

    List<String> compilerInvocation = new LinkedList<>();
    // We need a compiler name. Without it we cannot compile anything and
    // abort.
    if (("".equals(compilerName)) || (compilerName == null)) {
        throw new BadCompilerSpecifiedException("No compiler specified.");
    } else {
        compilerInvocation.add(compilerName);
    }

    // If compiler flags are passed, append them after the compiler name.
    // If we didn't get any we append nothing.
    if ((compilerFlags != null) && (!(compilerFlags.isEmpty()))) {
        compilerInvocation.addAll(compilerFlags);
    }

    // now we tell ghc to stop after compilation because we just want to
    // see if there are syntax errors in the code
    compilerInvocation.add("-c");

    // Check for the existence of the program file we are trying to
    // compile.
    if ((pathToProgramFile == null) || (pathToProgramFile.compareTo(Paths.get("")) == 0)) {
        throw new FileNotFoundException("No file to compile specified");
    } else {
        if (Files.isDirectory(pathToProgramFile, LinkOption.NOFOLLOW_LINKS)) {
            // we are supposed to compile a folder. Hence we'll scan for
            // lhs files and pass them to the compiler.
            RegexDirectoryWalker dirWalker = new RegexDirectoryWalker(".+\\.([Ll])?[Hh][Ss]");
            try {
                Files.walkFileTree(pathToProgramFile, dirWalker);
            } catch (IOException e) {
                LOGGER.severe("Could not walk submission " + pathToProgramFile.toString()
                        + " while building compiler invocation: " + e.getMessage());
            }
            for (Path matchedFile : dirWalker.getFoundFiles()) {
                compilerInvocation.add(matchedFile.toFile().getAbsolutePath());
            }

        } else if (Files.exists(pathToProgramFile, LinkOption.NOFOLLOW_LINKS)) {
            // if the file exists, just pass the file name, since the
            // compiler will
            // be confined to the directory the file is in a few lines
            // down.
            compilerInvocation.add(pathToProgramFile.toString());
        } else {
            throw new FileNotFoundException("Program file that should be compiled does not exist."
                    + "Filename : \"" + pathToProgramFile.toString() + "\"");
        }
    }
    return compilerInvocation;
}

From source file:de.teamgrit.grit.checking.compile.JavaCompileChecker.java

/**
 * This Method generates the command required to start the compiler. It
 * generates a list of strings that can be passed to a process builder.
 *
 * @param pathToSourceFolder// w  w w. j  a va2s  .c o m
 *            Where to look for the source files that will be compiled.
 * @param outputFolder
 *            Where .class files will be placed
 * @param compilerName
 *            Which compiler to call
 * @param compilerFlags
 *            User supplied flags to be passed
 * @throws BadCompilerSpecifiedException
 *             When no compiler is given.
 * @throws FileNotFoundException
 *             When the file to be compiled does not exist
 * @throws CompilerOutputFolderExistsException
 *             The output folder may not exist. This is thrown when it does
 *             exist.
 * @return List of string with the command for the process builder.
 */
private List<String> createCompilerInvocation(Path pathToSourceFolder, Path outputFolder, String compilerName,
        List<String> compilerFlags)
        throws BadCompilerSpecifiedException, FileNotFoundException, CompilerOutputFolderExistsException {

    List<String> compilerInvocation = new LinkedList<>();
    // Check if a compiler has been supplied. Without one we abort
    // compiling. Else we start building the compiler command.
    if (("".equals(compilerName)) || (compilerName == null)) {
        throw new BadCompilerSpecifiedException("No compiler specified.");
    } else {
        compilerInvocation.add(compilerName);
    }

    // If compiler flags are passed, append them after the compiler name.
    // If we didn't get any we append nothing. Appending empty strings or
    // nulls to the compiler invocation must be avoided, since it can cause
    // javac to crash. Hence, we ignore such entries.
    if ((compilerFlags != null) && (!(compilerFlags.isEmpty()))) {
        for (String flag : compilerFlags) {
            // If javac gets passed a "" it dies due to a bug, so avoid
            // this
            if ((flag != null) && !"".equals(flag)) {
                compilerInvocation.add(flag);
            }
        }
    }

    // Add JUnit and the current directory to the classpath.s
    compilerInvocation.add("-cp");
    String cp = ".:" + m_junitLocation.toAbsolutePath().toString();
    // Add all additional .jar files contained in javalib directory to the classpath
    if (!m_libLocation.toFile().exists()) {
        m_libLocation.toFile().mkdir();
    } else {
        for (File f : FileUtils.listFiles(m_libLocation.toFile(), new String[] { "jar" }, false)) {
            cp = cp + ":" + f.getAbsolutePath();
        }
    }
    compilerInvocation.add(cp);

    //make sure java uses utf8 for encoding
    compilerInvocation.add("-encoding");
    compilerInvocation.add("UTF-8");

    // Check for the existence of the program file we are trying to
    // compile.
    if ((pathToSourceFolder == null) || !(pathToSourceFolder.toFile().exists())) {
        throw new FileNotFoundException("No file to compile specified");
    } else {
        if (Files.isDirectory(pathToSourceFolder, LinkOption.NOFOLLOW_LINKS)) {
            // we are supposed to compile a folder. Hence we'll scan for
            // java files and pass them to the compiler.
            List<Path> foundFiles = exploreDirectory(pathToSourceFolder);
            for (Path matchedFile : foundFiles) {
                compilerInvocation.add(matchedFile.toFile().getAbsolutePath());
            }

            compilerInvocation.add("-d");
            compilerInvocation.add(outputFolder.toAbsolutePath().toString());
        } else {
            throw new FileNotFoundException("Program file that should be compiled does not exist."
                    + "Filename : \"" + pathToSourceFolder.toString() + "\"");
        }
    }

    return compilerInvocation;
}

From source file:edu.ehu.galan.lite.model.Document.java

/**
 * Save the results of the document in a Json text file, if you want to use current directory
 * use "./" or the absolute path, the method will check whether the Dir exists or not and will
 * try to create according to that/*w  w  w  .  j a  va  2  s.c om*/
 *
 * @param pFolder - where you want to save the results
 * @param pDoc
 */
public static void saveJsonToDir(String pFolder, Document pDoc) {
    Doc msg = new Doc();
    msg.setName(pDoc.name);
    msg.setKnowledgeSource(pDoc.getSource().toString());
    List<MsgTop> list = new ArrayList<>();
    for (Topic string : pDoc.topicList) {
        MsgTop top = new MsgTop();
        top.setSourceTitle(string.getSourceTitle());
        top.setDefinition(string.getSourceDef());
        top.setId(string.getId());
        top.setTopic(string.getTopic());
        top.setLabels(string.getLabelList());
        top.setDomainRelatedness(string.getDomainRelatedness());

        List<Translation> transList = new ArrayList<>();
        HashMap<String, String> map = string.getTranslations();
        Iterator<Map.Entry<String, String>> it = map.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry<String, String> pairs = it.next();
            Translation trans = new Translation();
            trans.setLang(pairs.getKey());
            trans.setText(pairs.getValue());
            transList.add(trans);
        }
        List<Link> linksIn = new ArrayList<>();
        HashMap<Integer, Double> map3 = string.getLinksIn();
        Iterator<Map.Entry<Integer, Double>> it2 = map3.entrySet().iterator();
        while (it2.hasNext()) {
            Map.Entry<Integer, Double> pairs = it2.next();
            Link link = new Link();
            link.setId(pairs.getKey());
            link.setRelatedness(pairs.getValue());
            linksIn.add(link);
        }
        List<Link> linksOut = new ArrayList<>();
        HashMap<Integer, Double> map4 = string.getLinksOut();
        Iterator<Map.Entry<Integer, Double>> it3 = map4.entrySet().iterator();
        while (it3.hasNext()) {
            Map.Entry<Integer, Double> pairs = it3.next();
            Link link = new Link();
            link.setId(pairs.getKey());
            link.setRelatedness(pairs.getValue());
            linksOut.add(link);
        }
        top.setLinksIn(linksIn);
        top.setLinksOut(linksOut);
        top.setTranslations(transList);
        List<Parent> parentList = new ArrayList<>();
        HashMap<Integer, String> map2 = string.getParentCategories();
        Iterator<Map.Entry<Integer, String>> itr = map2.entrySet().iterator();
        while (itr.hasNext()) {
            Map.Entry<Integer, String> pairs = itr.next();
            Parent trans = new Parent();
            trans.setId(pairs.getKey());
            trans.setSourceTitle(pairs.getValue());
            parentList.add(trans);
        }
        top.setParentCategories(parentList);
        list.add(top);
    }
    msg.setTopics(list);
    List<DomainRel> listRel = new ArrayList<>();
    for (Map.Entry<Integer, String> pair : pDoc.domainTopics.entrySet()) {
        DomainRel rel = new DomainRel();
        rel.setId(pair.getKey());
        rel.setTitle(pair.getValue());
        listRel.add(rel);
    }
    msg.setDomainTopics(listRel);
    if (pFolder.equals("")) {
        pFolder = System.getProperty("user.dir");

    }
    if (Files.isDirectory(Paths.get(pFolder), LinkOption.NOFOLLOW_LINKS)) {
        FileWriter outFile = null;
        try {
            FileUtils.deleteQuietly(new File(pFolder + "/" + pDoc.getName() + ".json"));
            outFile = new FileWriter(pFolder + "/" + pDoc.getName() + ".json");
            boolean first = true;
            try (PrintWriter out = new PrintWriter(outFile)) {
                Gson son = new GsonBuilder().setPrettyPrinting().create();
                out.print(son.toJson(msg));
            }
        } catch (IOException ex) {
            logger.warn("couldn't save the document results in json format", ex);
        } finally {
            try {
                if (outFile != null) {
                    outFile.close();
                }
            } catch (IOException ex) {
                logger.warn("Error while closing the file", ex);
            }
        }
        logger.info(pDoc.path + "  Saved... ");
    } else if (Files.exists(Paths.get(pFolder), LinkOption.NOFOLLOW_LINKS)) {
        logger.error("The folder exists but it isn't a directory...maybe a file?");
    } else {
        logger.warn("The directory doesn't exist... will be created");
        FileWriter outFile = null;
        try {
            FileUtils.forceMkdir(new File(pFolder));
            FileUtils.deleteQuietly(new File(pFolder + "/" + pDoc.getName() + ".json"));
            outFile = new FileWriter(pFolder + "/" + pDoc.getName() + ".json");
            boolean first = true;
            try (PrintWriter out = new PrintWriter(outFile)) {
                Gson son = new GsonBuilder().setPrettyPrinting().create();
                out.print(son.toJson(pDoc));
            }
        } catch (IOException ex) {
            logger.warn("couldn't save the document results in json format", ex);
        } finally {
            try {
                if (outFile != null) {
                    outFile.close();
                }
            } catch (IOException ex) {
                logger.warn("Error while closing the file", ex);
            }
        }
        logger.info(pDoc.path + "  Saved... ");

    }

}

From source file:com.htmlhifive.visualeditor.persister.LocalFileContentsPersister.java

/**
 * ?????????????./*from  ww  w.  j a  v  a2s .c o  m*/
 * 
 * @param key ?
 * @return ????
 */
@Override
public UrlTreeMetaData<InputStream> generateMetaDataFromReal(String key, UrlTreeContext ctx)
        throws BadContentException {
    if (!this.canLoad(key, ctx)) {
        throw new IllegalArgumentException(
                "Cannot Load it. check before can it load with canLoad(key): " + key);
    }
    Path p = this.generateFileObj(key);
    File f = p.toFile();
    long lastModified = f.lastModified();

    logger.trace("[?]????lastModified: " + p.toString() + ":"
            + lastModified);

    UrlTreeMetaData<InputStream> md = new UrlTreeMetaData<>();
    md.setDirectory(Files.isDirectory(p, LinkOption.NOFOLLOW_LINKS));
    md.setFilename(key);
    md.setOwnerId(ctx.getUserName());
    md.setGroupId(ctx.getPrimaryGroup());
    md.setCreatedTime(lastModified);
    md.setUpdatedTime(lastModified);
    md.setPermission(ctx.getDefaultPermission());
    String contentType = new MimetypesFileTypeMap().getContentType(p.getFileName().toString());
    md.setContentType(contentType);

    return md;
}

From source file:at.tfr.securefs.ui.CopyFilesServiceBean.java

private Path validateFromPath(String fromPathName) throws IOException {
    Path from = Paths.get(fromPathName);
    if (!Files.isDirectory(from, LinkOption.NOFOLLOW_LINKS)) {
        throw new IOException("Path " + from + " is no directory");
    }//from  w ww.j  a  v a  2 s  . com
    if (!Files.isReadable(from)) {
        throw new IOException("Path " + from + " is not readable");
    }
    if (!Files.isExecutable(from)) {
        throw new IOException("Path " + from + " is not executable");
    }
    return from;
}

From source file:at.tfr.securefs.ui.CopyFilesServiceBean.java

private Path validateToPath(String toPathName, ProcessFilesData pfd) throws IOException {
    Path to = Paths.get(toPathName);
    if (Files.exists(to, LinkOption.NOFOLLOW_LINKS)) {
        if (Files.isSameFile(to, Paths.get(pfd.getFromRootPath()))) {
            throw new IOException("Path " + to + " may not be same as FromPath: " + pfd.getFromRootPath());
        }/* w w  w.j a  v  a2s . c o  m*/
        if (!Files.isDirectory(to, LinkOption.NOFOLLOW_LINKS)) {
            throw new IOException("Path " + to + " is no directory");
        }
        if (!Files.isWritable(to)) {
            throw new IOException("Path " + to + " is not writable");
        }
        if (!Files.isExecutable(to)) {
            throw new IOException("Path " + to + " is not executable");
        }
        if (!pfd.isAllowOverwriteExisting()) {
            if (Files.newDirectoryStream(to).iterator().hasNext()) {
                throw new IOException("Path " + to + " is not empty, delete content copy.");
            }
        }
    }
    return to;
}