protected void doExecute()

in vault-cli/src/main/java/org/apache/jackrabbit/vault/cli/CmdFormatCli.java [49:115]


    protected void doExecute(VaultFsApp app, CommandLine cl) throws Exception {
        boolean checkOnly = cl.hasOption(optCheckOnly);
        boolean verbose = cl.hasOption(OPT_VERBOSE);
        List<String> givenPatterns = (List<String>) cl.getValues(optPatterns);
        List<String> localPaths = new LinkedList<String>(cl.getValues(argPaths));

        List<Pattern> parsedPatterns = new ArrayList<>(givenPatterns.size());
        boolean hasArgSeparator = false;
        for (String pattern : givenPatterns) {
            if ("--".equals(pattern)) {
                // little hack to separate the patterns from the files
                hasArgSeparator = true;
            } else {
                if (hasArgSeparator) {
                    localPaths.add(0, pattern);
                } else {
                    parsedPatterns.add(Pattern.compile(pattern));
                }
            }
        }
        if (parsedPatterns.isEmpty()) {
            parsedPatterns.add(DEFAULT_PATTERN);
        }

        List<File> localFiles = app.getPlatformFiles(localPaths, true);
        if (localFiles.isEmpty()) {
            localFiles.add(app.getPlatformFile(".", true));
        }

        List<String> formattedFiles = new LinkedList<>();
        DocViewFormat format = new DocViewFormat();
        for (File file: localFiles) {
            if (file.isDirectory()) {
                if (verbose) {
                    System.out.printf(Locale.ENGLISH, "traversing: %s%n", file);
                    for (Pattern p: parsedPatterns) {
                        System.out.printf(Locale.ENGLISH, "scanning for files matching: %s%n", p);
                    }
                }
                formattedFiles.addAll(format.format(file, parsedPatterns, checkOnly));
            } else {
                if (verbose) {
                    System.out.printf(Locale.ENGLISH, "processing: %s%n", file);
                }
                if (format.format(file, checkOnly)) {
                    formattedFiles.add(file.getPath());
                }
            }
        }
        if (formattedFiles.isEmpty()) {
            System.out.println("All files already properly formatted.");
            return;
        }

        final Path cwd = Paths.get(new File("").getAbsolutePath());
        if (checkOnly) {
            System.out.println("The following files are not properly formatted:\n");
            for (String path: formattedFiles) {
                System.out.println(cwd.relativize(Paths.get(path)));
            }
        } else {
            System.out.println("reformatted files:\n");
            for (String path: formattedFiles) {
                System.out.println(cwd.relativize(Paths.get(path)));
            }
        }
    }