Example #1
0
  /**
   * Generates a directive from a directive assembler.
   *
   * @param assembler The directive assembler.
   * @return The corresponding directive.
   * @throws AraraException Something wrong happened, to be caught in the higher levels.
   */
  public static Directive generateDirective(DirectiveAssembler assembler) throws AraraException {
    String regex = (String) ConfigurationController.getInstance().get("directives.pattern");
    Pattern pattern = Pattern.compile(regex);
    Matcher matcher = pattern.matcher(assembler.getText());
    if (matcher.find()) {
      Directive directive = new Directive();
      directive.setIdentifier(matcher.group(1));
      directive.setParameters(getParameters(matcher.group(3), assembler.getLineNumbers()));
      Conditional conditional = new Conditional();
      conditional.setType(getType(matcher.group(5)));
      conditional.setCondition(getCondition(matcher.group(6)));
      directive.setConditional(conditional);
      directive.setLineNumbers(assembler.getLineNumbers());

      logger.info(messages.getMessage(Messages.LOG_INFO_POTENTIAL_DIRECTIVE_FOUND, directive));

      return directive;
    } else {
      throw new AraraException(
          messages.getMessage(
              Messages.ERROR_VALIDATE_INVALID_DIRECTIVE_FORMAT,
              CommonUtils.getCollectionElements(assembler.getLineNumbers(), "(", ")", ", ")));
    }
  }
Example #2
0
  /**
   * Validates the list of directives, returning a new list.
   *
   * @param directives The list of directives.
   * @return A new list of directives.
   * @throws AraraException Something wrong happened, to be caught in the higher levels.
   */
  public static List<Directive> validate(List<Directive> directives) throws AraraException {

    ArrayList<Directive> result = new ArrayList<Directive>();
    for (Directive directive : directives) {
      Map<String, Object> parameters = directive.getParameters();

      if (parameters.containsKey("file")) {
        throw new AraraException(
            messages.getMessage(
                Messages.ERROR_VALIDATE_FILE_IS_RESERVED,
                CommonUtils.getCollectionElements(directive.getLineNumbers(), "(", ")", ", ")));
      }

      if (parameters.containsKey("reference")) {
        throw new AraraException(
            messages.getMessage(
                Messages.ERROR_VALIDATE_REFERENCE_IS_RESERVED,
                CommonUtils.getCollectionElements(directive.getLineNumbers(), "(", ")", ", ")));
      }

      if (parameters.containsKey("files")) {

        Object holder = parameters.get("files");
        if (holder instanceof List) {
          @SuppressWarnings("unchecked")
          List<Object> files = (List<Object>) holder;
          parameters.remove("files");
          if (files.isEmpty()) {
            throw new AraraException(
                messages.getMessage(
                    Messages.ERROR_VALIDATE_EMPTY_FILES_LIST,
                    CommonUtils.getCollectionElements(directive.getLineNumbers(), "(", ")", ", ")));
          }
          for (Object file : files) {
            Map<String, Object> map = new HashMap<String, Object>();
            for (String key : parameters.keySet()) {
              map.put(key, parameters.get(key));
            }
            File representation = CommonUtils.getCanonicalFile(String.valueOf(file));

            map.put("reference", representation);
            map.put("file", representation.getName());

            Directive addition = new Directive();
            Conditional conditional = new Conditional();
            conditional.setCondition(directive.getConditional().getCondition());
            conditional.setType(directive.getConditional().getType());
            addition.setIdentifier(directive.getIdentifier());
            addition.setConditional(conditional);
            addition.setParameters(map);
            addition.setLineNumbers(directive.getLineNumbers());
            result.add(addition);
          }
        } else {
          throw new AraraException(
              messages.getMessage(
                  Messages.ERROR_VALIDATE_FILES_IS_NOT_A_LIST,
                  CommonUtils.getCollectionElements(directive.getLineNumbers(), "(", ")", ", ")));
        }
      } else {
        File representation =
            (File) ConfigurationController.getInstance().get("execution.reference");
        parameters.put("file", representation.getName());
        parameters.put("reference", representation);
        directive.setParameters(parameters);
        result.add(directive);
      }
    }

    logger.info(messages.getMessage(Messages.LOG_INFO_VALIDATED_DIRECTIVES));
    logger.info(
        DisplayUtils.displayOutputSeparator(
            messages.getMessage(Messages.LOG_INFO_DIRECTIVES_BLOCK)));
    for (Directive directive : result) {
      logger.info(directive.toString());
    }

    logger.info(DisplayUtils.displaySeparator());

    return result;
  }