Ejemplo n.º 1
0
  /**
   * Retrieves IG rules from provided input files.
   *
   * @param files A collection of input files from which rules will be retrieved.
   * @param mappings {@link Map} of pairs (extension - processor). Extension is a lowercase file
   *     extension (e.g. "dtd"), processor is the {@link Processor} responsible for this file type.
   *     If <code>mappings</code> contain the key "*", will the associated processor handle all file
   *     types.
   * @return List of IG rules. Empty, if there are no input files or an error occurs.
   */
  private static List<Element> getRulesFromInput(
      final Collection<File> files, final Map<String, Processor<Element>> mappings)
      throws InterruptedException {
    if (BaseUtils.isEmpty(files)) {
      return new ArrayList<Element>(0);
    }

    final List<Element> ret = new ArrayList<Element>();

    for (final File f : files) {
      if (Thread.interrupted()) {
        throw new InterruptedException();
      }
      try {
        final Processor<Element> p =
            getProcessorForExtension(FileUtils.getExtension(f.getAbsolutePath()), mappings);
        if (p != null) {
          final List<Element> rules = p.process(new FileInputStream(f));
          for (final Element rule : rules) {
            rule.getMetadata().put(IGGUtils.FILE_ORIGIN, f.getAbsoluteFile());
          }
          ret.addAll(rules);
        } else {
          // TODO rio This is a hack to not print errors that XQuery files do not
          // have defined mapping.
          if (!FileUtils.getExtension(f.getAbsolutePath()).equals("xq")) {
            LOG.error(
                "File extension does not have a corresponding mapping: " + f.getAbsolutePath());
          }
        }
      } catch (final FileNotFoundException e) {
        throw new RuntimeException("File not found: " + f.getAbsolutePath(), e);
      }
    }

    return ret;
  }
  /**
   * Processes files with XQuery queries by supplying them to the specified processor. Result is a
   * list of respective syntax trees.
   */
  private List<ModuleNode> processXQueries(
      final Collection<File> files, final Processor<ModuleNode> xqueryProcessor)
      throws InterruptedException {
    if (BaseUtils.isEmpty(files) || xqueryProcessor == null) {
      return new ArrayList<ModuleNode>(0);
    }

    final List<ModuleNode> ret = new ArrayList<ModuleNode>();

    for (final File f : files) {
      if (Thread.interrupted()) {
        throw new InterruptedException();
      }
      try {
        if (FileUtils.getExtension(f.getAbsolutePath()).equals(xqueryProcessor.getExtension())) {
          /* TODO rio Interface of input processor requires List as a return type
           * but our XQuery processor returns only one syntax tree per file. So
           * it is at index 0. An empty list indicates parsing error.
           */
          final List<ModuleNode> syntaxTree = xqueryProcessor.process(new FileInputStream(f));
          if (syntaxTree.size() > 0) {
            assert (syntaxTree.size() == 1);
            ret.add(syntaxTree.get(0));
          } else {
            LOG.error(
                "Error in XQuery file "
                    + f.getAbsolutePath()
                    + ". Try to strip it of empty lines and comments."); // TODO rio Fix weird
                                                                         // parsing errors.
          }
        }
      } catch (final FileNotFoundException e) {
        throw new RuntimeException("File not found: " + f.getAbsolutePath(), e);
      }
    }

    return ret;
  }