void processMetadata() throws Exception {
    VelocityContext velocityContext = metadataResourceEngine.getVelocityContext();
    velocityContext.put(KEY_DATE_FORMAT, new SimpleDateFormat("yyyy-MM-dd"));
    velocityContext.put(KEY_DATE, new Date());

    HashMap<String, String> metadataPaths = cliHandler.fetchGlobalMetadataFiles();
    for (String key : metadataPaths.keySet()) {
      metadataResourceEngine.readResource(key, metadataPaths.get(key));
    }

    Map<String, String> sourcePaths = cliHandler.fetchSourceItemFiles();
    for (String key : sourcePaths.keySet()) {
      metadataResourceEngine.readRelatedResource(key, sourcePaths.get(key));
    }
    velocityContext.put(KEY_XPATH, new XPathHandler());
    velocityContext.put(KEY_SOURCES, sourcePaths);

    velocityContext.put(KEY_SYSTEM, System.getProperties());
    velocityContext.put(KEY_ARGS, Arrays.asList(cliHandler.fetchArguments()));

    Map<String, String> templatePaths = cliHandler.fetchTemplateFiles();
    String outputItemPath = cliHandler.fetchTargetItemFile();
    velocityContext.put(KEY_TARGET, outputItemPath);
    for (String templateKey : templatePaths.keySet()) {
      metadataResourceEngine.writeRelatedResource(templatePaths.get(templateKey), outputItemPath);
    }
  }
예제 #2
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);
      }
    }
  }