Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.selesse.jxlint;
  
 
 import java.io.File;
 import java.util.List;

The Dispatcher guides the application's logic flow. It looks at the options provided in com.selesse.jxlint.model.ProgramOptions and decides what objects, functions, etc. to call based on the options. Its logical route is documented in dispatch(com.selesse.jxlint.model.ProgramOptions,com.selesse.jxlint.settings.ProgramSettings).
 
 public class Dispatcher {
    
The order for the dispatcher is as such:
  1. First, look for the mutually exclusive options ("help", "version", "list", "show"). These are first-come, first-serve. If enabled, branch out to those options.
  2. Second, check to see if warnings are errors and keep note of it.
  3. Thirdly, check to see if the source directory exists. Exit if it doesn't.
  4. Fourthly, check to see if "check" was called. Branch out if it is.
  5. Then, by default, we only do enabled rules. We check to see if "Wall" or "nowarn" are set. If they are, adjust accordingly.
  6. Finally, we check to see if "enable" or "disable" are set and modify our list of rules accordingly.
  7. Dispatch to com.selesse.jxlint.actions.LintHandler.
 
     public static void dispatch(ProgramOptions programOptionsProgramSettings programSettings) {
         Profiler.setEnabled(programOptions.hasOption(.));
 
         LintRules lintRules = LintRulesImpl.getInstance();
 
         // If/else train of mutually exclusive options
         if (programOptions.hasOption(.)) {
             doHelp(programSettings);
         }
         else if (programOptions.hasOption(.)) {
             doVersion(programSettings);
         }
         else if (programOptions.hasOption(.)) {
             LintRuleInformationDisplayer.listRules();
         }
         else if (programOptions.hasOption(.)) {
             JettyWebRunner jettyWebRunner = new JettyWebRunner(programSettings);
             jettyWebRunner.start();
             return;
         }
         else if (programOptions.hasOption(.)) {
             LintRuleInformationDisplayer.showRules(programOptions);
         }
         else if (programOptions.hasOption(.)) {
             LintRuleInformationDisplayer.printMarkdownRuleReport(programSettings);
         }
 
         boolean warningsAreErrors = false;
         if (programOptions.hasOption(.)) {
             warningsAreErrors = true;
         }
 
         // we've parsed all mutually exclusive options, now let's make sure we have a proper source directory
         if (programOptions.getSourceDirectory() != null) {
             String sourceDirectoryString = programOptions.getSourceDirectory();
             if (isInvalidSourceDirectory(sourceDirectoryString)) {
                 File sourceDirectory = new File(sourceDirectoryString);
                 String outputBuffer = "Invalid source directory \"" + sourceDirectoryString + "\" : ";
                 if (!sourceDirectory.exists()) {
                     outputBuffer += "Directory does not exist.";
                 }
                 else if (!sourceDirectory.isDirectory()) {
                     outputBuffer += "\"" + sourceDirectoryString + "\" is not a directory.";
                 }
                 else if (!sourceDirectory.canRead()) {
                     outputBuffer += "Cannot read directory.";
                 }
 
                 ProgramExitter.exitProgramWithMessage(outputBuffer.);
             }
            lintRules.setSourceDirectory(new File(sourceDirectoryString));
        }
        else {
            ProgramExitter.exitProgramWithMessage("Error: could not find directory to validate.",
                    .);
        }
        if (programOptions.hasOption(.)) {
            String checkRules = programOptions.getOption(.);
            List<StringcheckRulesList = null;
            try {
                checkRulesList = ProgramOptions.getRuleListFromOptionString(checkRules);
            }
            catch (NonExistentLintRuleException e) {
                e.printStackTrace();
            }
            handleLint(lintRules.getOnlyRules(checkRulesList), warningsAreErrorsprogramOptionsprogramSettings);
            return;
        }
        // By default, we only check enabled rules.
        // We adjust this according to the options below.
        List<LintRulelintRuleList = Lists.newArrayList(lintRules.getAllEnabledRules());
        if (programOptions.hasOption(.)) {
            lintRuleList = Lists.newArrayList(lintRules.getAllRules());
        }
        else if (programOptions.hasOption(.)) {
            lintRuleList = Lists.newArrayList(lintRules.getAllRulesWithSeverity(.));
            lintRuleList.addAll(lintRules.getAllRulesWithSeverity(.));
        }
        if (programOptions.hasOption(.)) {
            String enabledCategories = programOptions.getOption(.);
            try {
                final List<StringenabledCategoriesList =
                        ProgramOptions.getCategoryListFromOptionString(enabledCategories);
                lintRuleList = Lists.newArrayList(Iterables.filter(lintRuleListnew Predicate<LintRule>() {
                    @Override
                    public boolean apply(@Nullable LintRule input) {
                        return input != null && enabledCategoriesList.contains(input.getCategory().toString());
                    }
                }));
            }
            catch (IllegalArgumentException e) {
                ProgramExitter.exitProgramWithMessage(e.getMessage(), .);
            }
        }
        // Options that are "standalone"
        if (programOptions.hasOption(.)) {
            String disabledRules = programOptions.getOption(.);
            try {
                List<StringdisabledRulesList = ProgramOptions.getRuleListFromOptionString(disabledRules);
                lintRuleList.removeAll(lintRules.getOnlyRules(disabledRulesList));
            }
            catch (NonExistentLintRuleException e) {
                ProgramExitter.exitProgramWithMessage(e.getMessage(), .);
            }
        }
        if (programOptions.hasOption(.)) {
            String enabledRules = programOptions.getOption(.);
            try {
                List<StringenabledRulesList = ProgramOptions.getRuleListFromOptionString(enabledRules);
                lintRuleList.addAll(lintRules.getOnlyRules(enabledRulesList));
            }
            catch (NonExistentLintRuleException e) {
                ProgramExitter.exitProgramWithMessage(e.getMessage(), .);
            }
        }
        handleLint(lintRuleListwarningsAreErrorsprogramOptionsprogramSettings);
    }
    private static void handleLint(List<LintRulelintRulesboolean warningsAreErrorsProgramOptions options,
                                   ProgramSettings settings) {
        LintHandler lintHandler = new LintHandler(lintRuleswarningsAreErrorsoptionssettings);
        lintHandler.lintAndReportAndExit(LintRulesImpl.willExitAfterReporting());
    }
    private static void doHelp(ProgramSettings programSettings) {
        ProgramExitter.exitProgramWithMessage(CommandLineOptions.getHelpMessage(programSettings), .);
    }
    private static void doVersion(ProgramSettings programSettings) {
        ProgramExitter.exitProgramWithMessage(programSettings.getProgramName() + ": version " +
                programSettings.getProgramVersion(), .);
    }
    private static boolean isInvalidSourceDirectory(String sourceDirectoryString) {
        File sourceDirectory = new File(sourceDirectoryString);
        return !sourceDirectory.exists() || !sourceDirectory.isDirectory() || !sourceDirectory.canRead();
    }
New to GrepCode? Check out our FAQ X