protected void process()

in language/ide/indexing/src/org/netbeans/modules/jackpot30/indexing/batch/OptionProcessorImpl.java [90:233]


    protected void process(Env env, Map<Option, String[]> optionValues) throws CommandException {
        List<Project> projects = new LinkedList<Project>();
        Map<String, List<ClassPath>> classPaths = new HashMap<String, List<ClassPath>>();

        if (optionValues.containsKey(APPLY_TRANSFORMATIONS_PROJECT)) {
            String[] projectNames = optionValues.get(APPLY_TRANSFORMATIONS_PROJECT);

            if (projectNames.length == 0) {
                env.getErrorStream().println("At least one parameter needed for " + APPLY_TRANSFORMATIONS_PROJECT_OPTION);
                throw new CommandException(1);
            }

            FileObject currentDirectory = FileUtil.toFileObject(env.getCurrentDirectory());

            OUTER: for (String p : projectNames) {
                FileObject projectFile = currentDirectory.getFileObject(p);

                if (projectFile == null) {
                    projectFile = FileUtil.toFileObject(new File(p));
                }

                if (projectFile == null) {
                    env.getErrorStream().println("Ignoring file " + p + " - cannot be found.");
                    continue;
                }

                if (!projectFile.isFolder()) {
                    env.getErrorStream().println("Ignoring file " + p + " - not a folder.");
                    continue;
                }

                Project project = null;
                String  error   = null;

                try {
                    project = ProjectManager.getDefault().findProject(projectFile);
                } catch (IOException ex) {
                    error = ex.getLocalizedMessage();
                } catch (IllegalArgumentException ex) {
                    error = ex.getLocalizedMessage();
                }

                if (project == null) {
                    if (error == null) {
                        env.getErrorStream().println("Ignoring file " + p + " - not a project.");
                    } else {
                        env.getErrorStream().println("Ignoring file " + p + " - not a project (" + error + ").");
                    }

                    continue;
                }

                for (SourceGroup sg : ProjectUtils.getSources(project).getSourceGroups("java")) {
                    FileObject root = sg.getRootFolder();

                    for (String type : Arrays.asList(ClassPath.BOOT, ClassPath.COMPILE, ClassPath.SOURCE)) {
                        if (!handleClassPath(root, type, env, p, classPaths)) {
                            continue OUTER;
                        }
                    }
                }

                projects.add(project);
            }
        } else {
            projects.addAll(Arrays.asList(OpenProjects.getDefault().getOpenProjects()));
        }

        if (optionValues.containsKey(LIST)) {
            env.getOutputStream().println("Supported Hints:");

            Set<ClassPath> cps = new HashSet<ClassPath>();

            for (List<ClassPath> c : classPaths.values()) {
                cps.addAll(c);
            }

            Set<String> displayNames = Utilities.sortOutHints(Utilities.listAllHints(cps), new TreeMap<String, Collection<HintDescription>>()).keySet();

            for (String displayName : displayNames) {
                env.getOutputStream().println(displayName);
            }
        }

        if (optionValues.containsKey(APPLY_TRANSFORMATIONS)) {
            String hintsArg = optionValues.get(APPLY_TRANSFORMATIONS)[0];
            List<HintDescription> hintDescriptions = new LinkedList<HintDescription>();
            Set<ClassPath> cps = new HashSet<ClassPath>();

            for (List<ClassPath> c : classPaths.values()) {
                cps.addAll(c);
            }

            Map<String, Collection<HintDescription>> sorted = Utilities.sortOutHints(Utilities.listAllHints(cps), new TreeMap<String, Collection<HintDescription>>());

            for (String hint : hintsArg.split(":")) {
                Collection<HintDescription> descs = sorted.get(hint);

                if (descs == null) {
                    env.getErrorStream().println("Unknown hint: " + hint);
                    continue;
                }

                hintDescriptions.addAll(descs);
            }

            Collection<Folder> roots = new ArrayList<Folder>();

            for (FileObject f : BatchUtilities.getSourceGroups(projects)) {
                roots.add(new Folder(f));
            }

            BatchResult candidates = BatchSearch.findOccurrences(hintDescriptions, Scopes.specifiedFoldersScope(roots.toArray(new Folder[0])));
            List<MessageImpl> problems = new LinkedList<MessageImpl>(candidates.problems);
            Collection<? extends ModificationResult> res = BatchUtilities.applyFixes(candidates, new ProgressHandleWrapper(100), null, problems);
            Set<FileObject> modified = new HashSet<FileObject>();

            for (ModificationResult mr : res) {
                try {
                    mr.commit();
                } catch (IOException ex) {
                    ex.printStackTrace(env.getErrorStream());
                    problems.add(new MessageImpl(MessageKind.ERROR, "Cannot apply changes: " + ex.getLocalizedMessage()));
                }
                modified.addAll(mr.getModifiedFileObjects());
            }

            try {
                org.netbeans.modules.jackpot30.indexing.batch.BatchUtilities.removeUnusedImports(modified);
            } catch (IOException ex) {
                ex.printStackTrace(env.getErrorStream());
                problems.add(new MessageImpl(MessageKind.ERROR, "Cannot remove unused imports: " + ex.getLocalizedMessage()));
            }

            if (!problems.isEmpty()) {
                env.getErrorStream().println("Problem encountered while applying the transformations:");

                for (MessageImpl problem : problems) {
                    env.getErrorStream().println(problem.text);
                }
            }
        }

    }