/**
 * Helper class for accessing settings from module's properties panel.
 *
 * @author reseto
 */
public final class XSDImportSettings {

  private XSDImportSettings() {}

  private static final Properties PROPERTIES =
      RunningProject.getActiveProjectProps(XSDImportPropertiesPanel.PANEL_NAME);

  /**
   * Get the selected parser from XSD Import project properties. Parser is selected from all
   * available parsers using lookup for <code>XSDParser</code> interface.
   *
   * @return Selected parser.
   */
  public static XSDParser getParser() {
    return ModuleSelectionHelper.lookupImpl(
        XSDParser.class, PROPERTIES.getProperty(XSDImportPropertiesPanel.PARSER_PROP));
  }

  /**
   * Check if more verbose information should be logged.
   *
   * @return True if verbose setting enabled, else false. False on error.
   */
  public static boolean isVerbose() {
    return Boolean.parseBoolean(
        PROPERTIES.getProperty(
            XSDImportPropertiesPanel.VERBOSE_INFO_PROP,
            XSDImportPropertiesPanel.VERBOSE_INFO_DEFAULT));
  }

  /**
   * Get current log level for module XSD Importer. On error, or by default this method returns root
   * log level.
   *
   * @return Current log level for XSD Importer.
   */
  public static Level getLogLevel() {
    return Level.toLevel(
        PROPERTIES.getProperty(
            XSDImportPropertiesPanel.LOG_LEVEL_PROP, XSDImportPropertiesPanel.LOG_LEVEL_DEFAULT),
        Logger.getRootLogger().getLevel());
  }

  /**
   * Check if import process should halt on any error. If disabled, the file is just skipped and
   * error is logged. By default this setting is enabled.
   *
   * @return True if "Stop on error" checkbox is checked, else false. True if exception occurs.
   */
  public static boolean isStopOnError() {
    return Boolean.parseBoolean(
        PROPERTIES.getProperty(
            XSDImportPropertiesPanel.STOP_ON_ERROR_PROP,
            XSDImportPropertiesPanel.STOP_ON_ERROR_DEFAULT));
  }
}
  @Override
  public void start(final Input input, final IGGeneratorCallback callback)
      throws InterruptedException {
    // find processor mappings for all folders
    final Map<FolderType, Map<String, Processor<Element>>> registeredProcessors =
        getRegisteredProcessors();

    final List<Element> documentRules = new ArrayList<Element>();
    final List<Element> schemaQueryRules = new ArrayList<Element>();

    // run processors on input, gather IG rules
    documentRules.addAll(
        getRulesFromInput(input.getDocuments(), registeredProcessors.get(FolderType.DOCUMENT)));
    verifySimpleGrammar(documentRules);
    schemaQueryRules.addAll(
        getRulesFromInput(input.getSchemas(), registeredProcessors.get(FolderType.SCHEMA)));
    schemaQueryRules.addAll(
        getRulesFromInput(input.getQueries(), registeredProcessors.get(FolderType.QUERY)));

    // if there are no schema/query rules, or the next module can handle simple
    // grammar, just output all of it without expansion
    if (BaseUtils.isEmpty(schemaQueryRules)
        || RunningProject.getNextModuleCaps()
            .getCapabilities()
            .contains("can.handle.complex.regexps")) {
      documentRules.addAll(schemaQueryRules);

      // show the rules
      RuleDisplayerHelper.showRulesAsync("IG", new CloneHelper().cloneGrammar(documentRules), true);

      callback.finished(documentRules);
      return;
    }

    // otherwise, we have to expand
    // show the rules before expansion
    final List<Element> before = new ArrayList<Element>();
    before.addAll(documentRules);
    before.addAll(schemaQueryRules);
    RuleDisplayerHelper.showRulesAsync("Raw", new CloneHelper().cloneGrammar(before), true);

    // lookup expander
    final Expander expander = Lookup.getDefault().lookup(Expander.class);
    final List<Element> expanded = expander.expand(schemaQueryRules);

    final List<Element> ret = new ArrayList<Element>();
    ret.addAll(documentRules);
    ret.addAll(expanded);

    // show the rules after expansion
    RuleDisplayerHelper.showRulesAsync("Expanded", new CloneHelper().cloneGrammar(ret), true);

    // return expanded
    callback.finished(ret);
  }