Beispiel #1
0
  private void runVelocityTemplates() {
    String velocityDirPath = commandLineArgs.getVelocityTemplateDirPath();
    File velocityDir;
    if (velocityDirPath != null) {
      velocityDir = new File(velocityDirPath);
    } else {
      velocityDir = new File(CommandLineArgs.DEFAULT_VELOCITY_TEMPLATE_DIRPATH);
    }

    String[] templateNames =
        velocityDir.list(
            new FilenameFilter() {
              @Override
              public boolean accept(File dir, String name) {
                return name.toLowerCase().endsWith(CommandLineArgs.VELOCITY_TEMPLATE_EXTENSION);
              }
            });

    Logger logger = commandLineContext.getLogger();
    if (templateNames == null) {
      String msgPattern = "Velocity template directory '%s' does not exist or inaccessible";
      logger.severe(String.format(msgPattern, velocityDir));
      return;
    }
    if (templateNames.length == 0) {
      String msgPattern = "Velocity template directory '%s' does not contain any templates (*.vm)";
      logger.warning(String.format(msgPattern, velocityDir));
      return;
    }

    // It can happen that we have no target file when the operator implements the Output interface
    if (!commandLineContext.isFile(commandLineArgs.getTargetFilePath())) {
      String msgPattern =
          "Target file '%s' does not exist, but is required to process velocity templates";
      logger.warning(String.format(msgPattern, commandLineArgs.getTargetFilePath()));
      return;
    }

    for (String templateName : templateNames) {
      try {
        metadataResourceEngine.writeRelatedResource(
            velocityDir + "/" + templateName, commandLineArgs.getTargetFilePath());
      } catch (IOException e) {
        String msgPattern = "Can't write related resource using template file '%s': %s";
        logSevereProblem(String.format(msgPattern, templateName, e.getMessage()), e);
      }
    }
  }