Skip to content

Commit

Permalink
convert -s to a multi value option
Browse files Browse the repository at this point in the history
The generator option -s is now a multi value option. This can be used to create a combined documentation for multiple directories.
  • Loading branch information
emilitzerjo committed May 11, 2022
1 parent fbda807 commit 78de628
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 44 deletions.
2 changes: 1 addition & 1 deletion README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ Usage: java [-Dlogback.configurationFile=logback.xml] -jar midscribe.jar [option
Include files
Default: []
-s, --source-directory
Source directory with XML files
Source directory with XML files. This option can be used multiple times to define more than one directory.
-t, --template
Template file. File should be ZIP containing "template" directory with Velocity templates or path to directory with templates. Main template is "documentation.vm"
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ public void setExportOutput(File exportOutput) {
super.setExportOutput(exportOutput);
}

@Parameter(names = {P_SOURCE_DIRECTORY, P_SOURCE_DIRECTORY_LONG}, validateWith = URIConverter.class, descriptionKey = "generate.sourceDirectory")
@Parameter(names = {P_SOURCE_DIRECTORY, P_SOURCE_DIRECTORY_LONG}, converter = FileConverter.class, validateWith = FileConverter.class, descriptionKey = "generate.sourceDirectory")
@Override
public void setSourceDirectory(File sourceDirectory) {
public void setSourceDirectory(List<File> sourceDirectory) {
super.setSourceDirectory(sourceDirectory);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,32 @@
import com.beust.jcommander.IStringConverter;
import com.beust.jcommander.ParameterException;

import java.io.File;
import java.net.URI;

/**
* Created by Viliam Repan (lazyman).
*/
public class URIConverter implements IStringConverter<URI>, IParameterValidator {
public class FileConverter implements IStringConverter<File>, IParameterValidator {

private String optionName;

public URIConverter() {
public FileConverter() {
this(null);
}

public URIConverter(String optionName) {
public FileConverter(String optionName) {
this.optionName = optionName;
}

@Override
public URI convert(String value) {
public File convert(String value) {
if (value == null) {
return null;
}

try {
return URI.create(value);
return new File(value);
} catch (IllegalArgumentException ex) {
throw new IllegalArgumentException("Option " + optionName
+ " doesn't contain valid URL ('" + value + "')", ex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*/
public class GenerateOptions {

private File sourceDirectory;
private List<File> sourceDirectory;

private List<String> include;

Expand Down Expand Up @@ -67,11 +67,11 @@ public void setExportOutput(File exportOutput) {
this.exportOutput = exportOutput;
}

public File getSourceDirectory() {
public List<File> getSourceDirectory() {
return sourceDirectory;
}

public void setSourceDirectory(File sourceDirectory) {
public void setSourceDirectory(List<File> sourceDirectory) {
this.sourceDirectory = sourceDirectory;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,38 +64,39 @@ public void init() throws Exception {

ParsingContext parsingContext = prismContext.createParsingContextForCompatibilityMode();

File sources = options.getSourceDirectory();
Iterator<File> files = FileUtils.iterateFiles(sources,
new InMemoryFileFilter(sources, options.getInclude(), options.getExclude()), TrueFileFilter.INSTANCE);

while (files.hasNext()) {
File file = files.next();

LOG.debug("Loading {}", file);

try (InputStream is = new FileInputStream(file)) {
List<PrismObject<? extends Objectable>> objects;
if (options.isExpand()) {
InputStream expanded = expander.expand(is, StandardCharsets.UTF_8);
PrismParser parser = prismContext.parserFor(expanded).language(PrismContext.LANG_XML).context(parsingContext);
objects = parser.parseObjects();
} else {
PrismParser parser = prismContext.parserFor(is).language(PrismContext.LANG_XML).context(parsingContext);
objects = parser.parseObjects();
}
for (File source : options.getSourceDirectory()) {
Iterator<File> files = FileUtils.iterateFiles(source,
new InMemoryFileFilter(source, options.getInclude(), options.getExclude()), TrueFileFilter.INSTANCE);

while (files.hasNext()) {
File file = files.next();

LOG.debug("Loading {}", file);

try (InputStream is = new FileInputStream(file)) {
List<PrismObject<? extends Objectable>> objects;
if (options.isExpand()) {
InputStream expanded = expander.expand(is, StandardCharsets.UTF_8);
PrismParser parser = prismContext.parserFor(expanded).language(PrismContext.LANG_XML).context(parsingContext);
objects = parser.parseObjects();
} else {
PrismParser parser = prismContext.parserFor(is).language(PrismContext.LANG_XML).context(parsingContext);
objects = parser.parseObjects();
}

for (PrismObject<? extends Objectable> object : objects) {
ObjectType obj = (ObjectType) object.asObjectable();
for (PrismObject<? extends Objectable> object : objects) {
ObjectType obj = (ObjectType) object.asObjectable();

List<ObjectType> list = this.objects.get(obj.getClass());
if (list == null) {
list = new ArrayList<>();
this.objects.put(obj.getClass(), list);
List<ObjectType> list = this.objects.get(obj.getClass());
if (list == null) {
list = new ArrayList<>();
this.objects.put(obj.getClass(), list);
}
list.add(obj);
}
list.add(obj);
} catch (Exception ex) {
LOG.warn("Couldn't load file {}, reason: {}", file.getPath(), ex.getMessage());
}
} catch (Exception ex) {
LOG.warn("Couldn't load file {}, reason: {}", file.getPath(), ex.getMessage());
}
}
LOG.debug("Initialization done");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class GeneratorTest extends MidscribeTest {
@Test
public void generateExample() throws Exception {
GenerateOptions opts = prepareOptions("generateExample");
opts.setSourceDirectory(new File("./src/test/resources/objects"));
opts.setSourceDirectory(List.of(new File("./src/test/resources/objects")));
opts.getExclude().addAll(Arrays.asList(new String[]{"users/*.xml", "tasks/misc/*"}));

Generator generator = new Generator(opts);
Expand All @@ -32,7 +32,7 @@ public void generateExample() throws Exception {
@Test
public void generatePdfExample() throws Exception {
GenerateOptions opts = prepareOptions("generatePdfExample");
opts.setSourceDirectory(new File("./src/test/resources/objects"));
opts.setSourceDirectory(List.of(new File("./src/test/resources/objects")));
opts.getExclude().addAll(Arrays.asList(new String[]{"users/*.xml", "tasks/misc/*"}));
opts.setExportFormat(ExportFormat.PDF);

Expand All @@ -43,7 +43,7 @@ public void generatePdfExample() throws Exception {
@Test
public void generateWithCustomZipTemplate() throws Exception {
GenerateOptions opts = prepareOptions("generateWithCustomZipTemplate");
opts.setSourceDirectory(new File("./src/test/resources/objects"));
opts.setSourceDirectory(List.of(new File("./src/test/resources/objects")));
opts.getExclude().addAll(Arrays.asList(new String[]{"users/*.xml", "tasks/misc/*"}));
opts.setTemplate(new File("./src/test/resources/template.zip"));

Expand Down Expand Up @@ -82,7 +82,7 @@ public void generateAdocPdf() throws IOException {
@Test
public void generateHtmlWithCustomLogListener() throws Exception {
GenerateOptions opts = prepareOptions("generateHtmlWithCustomLogListener");
opts.setSourceDirectory(new File("./src/test/resources/objects"));
opts.setSourceDirectory(List.of(new File("./src/test/resources/objects")));
opts.setInclude(Arrays.asList("generateHtmlWithCustomLogListener.xml"));

List<String> messages = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;

/**
* Created by Viliam Repan (lazyman).
Expand All @@ -20,7 +21,7 @@ public abstract class MidscribeTest {

protected GenerateOptions prepareOptions(String name) {
GenerateOptions opts = new GenerateOptions();
opts.setSourceDirectory(new File("./src/test/resources"));
opts.setSourceDirectory(List.of(new File("./src/test/resources")));
opts.setInclude(Arrays.asList("objects/**/*.xml"));
opts.setAdocOutput(new File("./target/" + name + ".adoc"));
opts.setExportOutput(new File("./target/" + name + ".html"));
Expand Down

0 comments on commit 78de628

Please sign in to comment.