Beispiel #1
0
  /**
   * Extracts a list of directives from a list of strings.
   *
   * @param lines List of strings.
   * @return A list of directives.
   * @throws AraraException Something wrong happened, to be caught in the higher levels.
   */
  public static List<Directive> extractDirectives(List<String> lines) throws AraraException {

    String regex = (String) ConfigurationController.getInstance().get("execution.file.pattern");
    regex = regex.concat((String) ConfigurationController.getInstance().get("application.pattern"));
    Pattern pattern = Pattern.compile(regex);
    List<Pair<Integer, String>> pairs = new ArrayList<Pair<Integer, String>>();
    Matcher matcher;
    for (int i = 0; i < lines.size(); i++) {
      matcher = pattern.matcher(lines.get(i));
      if (matcher.find()) {
        String line = lines.get(i).substring(matcher.end(), lines.get(i).length());
        Pair<Integer, String> pair = new Pair<Integer, String>(i + 1, line);
        pairs.add(pair);

        logger.info(
            messages.getMessage(Messages.LOG_INFO_POTENTIAL_PATTERN_FOUND, (i + 1), line.trim()));
      }
    }

    if (pairs.isEmpty()) {
      throw new AraraException(messages.getMessage(Messages.ERROR_VALIDATE_NO_DIRECTIVES_FOUND));
    }

    List<DirectiveAssembler> assemblers = new ArrayList<DirectiveAssembler>();
    DirectiveAssembler assembler = new DirectiveAssembler();
    regex = (String) ConfigurationController.getInstance().get("directives.linebreak.pattern");
    pattern = Pattern.compile(regex);
    for (Pair<Integer, String> pair : pairs) {
      matcher = pattern.matcher(pair.getSecondElement());
      if (matcher.find()) {
        if (!assembler.isAppendAllowed()) {
          throw new AraraException(
              messages.getMessage(
                  Messages.ERROR_VALIDATE_ORPHAN_LINEBREAK, pair.getFirstElement()));
        } else {
          assembler.addLineNumber(pair.getFirstElement());
          assembler.appendLine(matcher.group(1));
        }
      } else {
        if (assembler.isAppendAllowed()) {
          assemblers.add(assembler);
        }
        assembler = new DirectiveAssembler();
        assembler.addLineNumber(pair.getFirstElement());
        assembler.appendLine(pair.getSecondElement());
      }
    }
    if (assembler.isAppendAllowed()) {
      assemblers.add(assembler);
    }

    List<Directive> directives = new ArrayList<Directive>();
    for (DirectiveAssembler current : assemblers) {
      directives.add(generateDirective(current));
    }
    return directives;
  }
Beispiel #2
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(), "(", ")", ", ")));
    }
  }