/**
 * Copyright (c) 2015, German Neuroinformatics Node (G-Node)
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted under the terms of the BSD License. See
 * LICENSE file in the root of the Project.
 */

package org.g_node.converter;

import com.hp.hpl.jena.rdf.model.Model;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.jena.riot.RiotException;
import org.apache.log4j.Logger;
import org.g_node.micro.commons.CliToolController;
import org.g_node.micro.rdf.RdfFileServiceJena;
import org.g_node.srv.CliOptionService;
import org.g_node.srv.CtrlCheckService;

/**
 * Controller class for the RDF to RDF converter.
 *
 * @author Michael Sonntag (sonntag@bio.lmu.de)
 */
public class ConvCliToolController implements CliToolController {
    /**
     * Access to the main LOGGER.
     */
    private static final Logger LOGGER = Logger.getLogger(ConvCliToolController.class.getName());
    /**
     * Method returning the commandline options of the RDF to RDF converter.
     * @return Available commandline options.
     */
    public final Options options() {
        final Options options = new Options();

        final Option opHelp = CliOptionService.getHelpOpt("");
        final Option opIn = CliOptionService.getInFileOpt(
                "Input RDF file that's supposed to be converted into a different RDF format.");
        final Option opOut = CliOptionService.getOutFileOpt("");
        final Option opFormat = CliOptionService.getOutFormatOpt("", RdfFileServiceJena.RDF_FORMAT_MAP.keySet());

        options.addOption(opHelp);
        options.addOption(opIn);
        options.addOption(opOut);
        options.addOption(opFormat);

        return options;
    }

    /**
     * Method converting data from an input RDF file to an RDF file of a different supported RDF format.
     * @param cmd User provided {@link CommandLine} input.
     */
    public final void run(final CommandLine cmd) {

        final Map<String, String> rdfFormatExtensions = RdfFileServiceJena.RDF_FORMAT_EXTENSION;
        final Set<String> rdfFormatKeys = RdfFileServiceJena.RDF_FORMAT_MAP.keySet();

        final String inputFile = cmd.getOptionValue("i");
        if (!CtrlCheckService.isExistingFile(inputFile)) {
            return;
        }

        final Set<String> checkExtension = rdfFormatExtensions.values()
                .stream()
                .map(c->c.toUpperCase(Locale.ENGLISH))
                .collect(Collectors.toSet());
        if (!CtrlCheckService.isSupportedInFileType(inputFile, checkExtension)) {
            return;
        }

        if (!RdfFileServiceJena.isValidRdfFile(inputFile)) {
            return;
        }

        final String outputFormat = cmd.getOptionValue("f", "TTL").toUpperCase(Locale.ENGLISH);
        if (!CtrlCheckService.isSupportedOutputFormat(outputFormat, rdfFormatKeys)) {
            return;
        }

        final int i = inputFile.lastIndexOf('.');
        final String defaultOutputFile = String.join("", inputFile.substring(0, i), "_out");
        String outputFile = cmd.getOptionValue("o", defaultOutputFile);

        if (!outputFile.toLowerCase().endsWith(rdfFormatExtensions.get(outputFormat))) {
            outputFile = String.join("", outputFile, ".", rdfFormatExtensions.get(outputFormat));
        }

        Model convData;

        try {
            ConvCliToolController.LOGGER.info("Reading input file...");
            convData = RdfFileServiceJena.openModelFromFile(inputFile);

        } catch (RiotException e) {
            ConvCliToolController.LOGGER.error(e.getMessage());
            // TODO find out how to print stacktrace to log4j logfile
            e.printStackTrace();
            return;
        }

        RdfFileServiceJena.saveModelToFile(outputFile, convData, outputFormat);
    }

}

@@ -313,48 +313,8 @@
 r;%0A%0A
-import com.hp.hpl.jena.rdf.model.Model;%0A
 impo
@@ -334,16 +334,16 @@
 Locale;%0A
+
 import j
@@ -538,83 +538,8 @@
 ns;%0A
-import org.apache.jena.riot.RiotException;%0Aimport org.apache.log4j.Logger;%0A
 impo
@@ -823,16 +823,16 @@
 de)%0A */%0A
+
 public c
@@ -901,156 +901,8 @@
 /**%0A
-     * Access to the main LOGGER.%0A     */%0A    private static final Logger LOGGER = Logger.getLogger(ConvCliToolController.class.getName());%0A    /**%0A
     
@@ -3289,461 +3289,46 @@
     
-Model convData;%0A%0A        try %7B%0A            ConvCliToolController.LOGGER.info(%22Reading input file...%22);%0A            convData = RdfFileServiceJena.openModelFromFile(inputFile);%0A%0A        %7D catch (RiotException e) %7B%0A            ConvCliToolController.LOGGER.error(e.getMessage());%0A            // TODO find out how to print stacktrace to log4j logfile%0A            e.printStackTrace();%0A            return;%0A        %7D%0A%0A        RdfFileServiceJena.saveModelTo
+ConverterJena.runConverter(input
 File
-(
+, 
 outp
@@ -3338,18 +3338,8 @@
 ile,
- convData,
  out

Use ConverterJena