Skip to content

Commit

Permalink
Merge pull request #3 from itplr-kosit/cli-interface
Browse files Browse the repository at this point in the history
First version of CLI
  • Loading branch information
phax authored Mar 6, 2020
2 parents 202ac41 + 213670c commit 3735652
Show file tree
Hide file tree
Showing 2 changed files with 172 additions and 3 deletions.
46 changes: 43 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<?xml version="1.0" encoding="UTF-8" ?>
<!--
Copyright (C) 2019-2020 Philip Helger
Expand Down Expand Up @@ -93,6 +93,11 @@
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>info.picocli</groupId>
<artifactId>picocli</artifactId>
<version>4.1.4</version>
</dependency>
</dependencies>
</dependencyManagement>

Expand Down Expand Up @@ -125,7 +130,11 @@
<groupId>com.helger</groupId>
<artifactId>ph-ubl22</artifactId>
</dependency>

<dependency>
<groupId>info.picocli</groupId>
<artifactId>picocli</artifactId>
<version>4.1.4</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand All @@ -134,7 +143,7 @@
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<scope>test</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.helger</groupId>
Expand All @@ -151,6 +160,7 @@
<extensions>true</extensions>
<configuration>
<instructions>
<Main-Class>com.helger.en16931.cii2ubl.cli.CIIToUBLConverter</Main-Class>
<Automatic-Module-Name>com.helger.en16931-cii2ubl</Automatic-Module-Name>
<Export-Package>com.helger.en16931-cii2ubl.*</Export-Package>
<Import-Package>!javax.annotation.*,*</Import-Package>
Expand All @@ -171,6 +181,36 @@
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.1</version>
<configuration>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<shadedArtifactAttached>true</shadedArtifactAttached>
<shadedClassifierName>full</shadedClassifierName> <!-- Any name that makes sense -->
</configuration>
</execution>
</executions>

</plugin>
</plugins>
</build>
</project>
129 changes: 129 additions & 0 deletions src/main/java/com/helger/en16931/cii2ubl/cli/CIIToUBLConverter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package com.helger.en16931.cii2ubl.cli;

import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.stream.Collectors;

import com.helger.commons.error.list.ErrorList;
import com.helger.commons.io.file.FilenameHelper;
import com.helger.en16931.cii2ubl.CIIToUBL21Converter;
import com.helger.en16931.cii2ubl.EUBLCreationMode;
import com.helger.ubl21.UBL21Writer;
import com.helger.ubl21.UBL21WriterBuilder;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import oasis.names.specification.ubl.schema.xsd.invoice_21.InvoiceType;
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;

/**
* Main
*/
@Command(description = "CII to UBL Converter.", name = "CIItoUBLConverter", mixinStandardHelpOptions = true, separator = " ")
public class CIIToUBLConverter implements Callable<Integer> {

private static final Logger log = LoggerFactory.getLogger(CIIToUBLConverter.class);

@Option(names = "--ubl", defaultValue = "2.1", description = "Version of the target UBL Format (default: ${DEFAULT-VALUE})")
private String UBLVersion;

@Option(names = "--mode", defaultValue = "INVOICE", description = "Allowedvalues:${COMPLETION-CANDIDATES}")
private EUBLCreationMode mode;

@Option(names = { "-t",
"--target" }, paramLabel = "out", defaultValue = ".", description = "The target directory for result output (default: ${DEFAULT-VALUE})")
private String outputDir;

@Parameters(arity = "1..*", description = "One or more Files")
private List<File> files;

// doing the business
public Integer call() throws Exception {
try {
init();
} catch (IOException e) {
log.error("Could not initialize converter", e.getLocalizedMessage());
}
// TODO switch between versions
final CIIToUBL21Converter converter = new CIIToUBL21Converter();
converter.setUBLCreationMode(mode);
final ErrorList errorList = new ErrorList();

Serializable invoice = null;
InvoiceType ublInvoiceType = null;
File destFile = null;
UBL21WriterBuilder<InvoiceType> aWriter = null;

for (File f : this.files) {
log.debug("Converting file={}", f.toString());
invoice = converter.convertCIItoUBL(f, errorList);
ublInvoiceType = (InvoiceType) invoice;
destFile = new File(this.outputDir, FilenameHelper.getBaseName(f) + "-ubl.xml");
aWriter = UBL21Writer.invoice().setFormattedOutput(true);
aWriter.write(ublInvoiceType, destFile);
}

return 0;
}

private void init() throws IOException {
this.outputDir = normalizeOutputDirectory(this.outputDir);
this.files = normalizeInputFiles(this.files);
}

private String normalizeOutputDirectory(String dir) {

log.debug("CLI option output directory={}", dir);
String result = "";
result = Paths.get(dir).toAbsolutePath().normalize().toString();
log.debug("Normalized output directory={}", result);
return result;
}

private List<File> normalizeInputFiles(List<File> files) throws IOException {
List<File> normalizedFiles = new ArrayList<File>();

List<File> dirFiles = new ArrayList<File>();
for (File file : files) {
if (file.isDirectory()) {
log.debug("Is a diractory={}", file.toString());
// collecting readable and normalized absolute path files
dirFiles = Files.walk(file.toPath()).filter(p -> Files.isReadable(p) && !Files.isDirectory(p))
.map(p -> normalizeFile(p)).peek(f -> log.debug("Add file={}", f.toString()))
.collect(Collectors.toList());
normalizedFiles.addAll(dirFiles);
} else if (file.canRead()) {
log.debug("Is a file={}", file.toString());
normalizedFiles.add(normalizeFile(file.toPath()));
}
}
return normalizedFiles;

}

private static File normalizeFile(Path path) {

return new File(path.toAbsolutePath().normalize().toString());

}

public static void main(final String[] args) {

log.info("Starting CII to UBL Converter");
final CommandLine cmd = new CommandLine(new CIIToUBLConverter());
cmd.setCaseInsensitiveEnumValuesAllowed(true);
final int exitCode = cmd.execute(args);
System.exit(exitCode);
}
}

0 comments on commit 3735652

Please sign in to comment.