-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Partial data-extract implementation
- Loading branch information
Showing
36 changed files
with
2,632 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
...in/java/com/fortify/cli/common/data_extract/cli/cmd/AbstractDataExtractCreateCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/******************************************************************************* | ||
* Copyright 2021, 2023 Open Text. | ||
* | ||
* The only warranties for products and services of Open Text | ||
* and its affiliates and licensors ("Open Text") are as may | ||
* be set forth in the express warranty statements accompanying | ||
* such products and services. Nothing herein should be construed | ||
* as constituting an additional warranty. Open Text shall not be | ||
* liable for technical or editorial errors or omissions contained | ||
* herein. The information contained herein is subject to change | ||
* without notice. | ||
*******************************************************************************/ | ||
package com.fortify.cli.common.data_extract.cli.cmd; | ||
|
||
import java.util.List; | ||
|
||
import com.fortify.cli.common.cli.cmd.AbstractRunnableCommand; | ||
import com.fortify.cli.common.cli.mixin.CommonOptionMixins.OptionParametersMixin; | ||
import com.fortify.cli.common.cli.mixin.CommonOptionMixins.OptionParametersMixin.OptionParameterHelper; | ||
import com.fortify.cli.common.data_extract.helper.DataExtractTemplateDescriptor; | ||
import com.fortify.cli.common.data_extract.helper.DataExtractTemplateHelper; | ||
import com.fortify.cli.common.data_extract.helper.DataExtractTemplateRunner; | ||
import com.fortify.cli.common.progress.cli.mixin.ProgressWriterFactoryMixin; | ||
import com.fortify.cli.common.progress.helper.IProgressWriterI18n; | ||
import com.fortify.cli.common.util.DisableTest; | ||
import com.fortify.cli.common.util.DisableTest.TestType; | ||
|
||
import picocli.CommandLine.Mixin; | ||
import picocli.CommandLine.Option; | ||
|
||
public abstract class AbstractDataExtractCreateCommand extends AbstractRunnableCommand implements Runnable { | ||
@Option(names={"-t", "--template"}, required=true, descriptionKey="fcli.data-extract.create.template") private String template; | ||
@Option(names="--template-help", required=false, arity = "0", descriptionKey="fcli.data-extract.create.template-help") private boolean isTemplateHelpRequested; | ||
@DisableTest({TestType.MULTI_OPT_SPLIT, TestType.MULTI_OPT_PLURAL_NAME, TestType.OPT_LONG_NAME}) | ||
@Option(names="--<template-parameter>", paramLabel="<value>", descriptionKey="fcli.data-extract.create.template-parameter") | ||
private List<String> dummyForSynopsis; | ||
@Mixin private OptionParametersMixin templateParameters; | ||
@Mixin private ProgressWriterFactoryMixin progressWriterFactory; | ||
|
||
@Override | ||
public final void run() { | ||
initMixins(); | ||
Runnable delayedConsoleWriter = null; | ||
try ( var progressWriter = progressWriterFactory.create() ) { | ||
progressWriter.writeProgress("Loading template %s", template); | ||
var templateDescriptor = DataExtractTemplateHelper.load(getType(), template); | ||
try ( var templateExecutor = DataExtractTemplateRunner.builder() | ||
.template(templateDescriptor) | ||
.inputParameters(templateParameters.getOptions()) | ||
.progressWriter(progressWriter).build() ) | ||
{ | ||
delayedConsoleWriter = run(templateDescriptor, templateExecutor, progressWriter); | ||
} | ||
} | ||
delayedConsoleWriter.run(); | ||
} | ||
|
||
private Runnable run(DataExtractTemplateDescriptor templateDescriptor, DataExtractTemplateRunner templateExecutor, IProgressWriterI18n progressWriter) { | ||
configure(templateExecutor); | ||
OptionParameterHelper helper = templateParameters.getHelper(templateExecutor::configureOptionParameterHelper); | ||
if ( isTemplateHelpRequested ) { | ||
String templateHelp = getTemplateHelp(templateDescriptor, helper); | ||
return ()->{System.out.println(templateHelp);}; | ||
} else { | ||
helper.validate(); | ||
progressWriter.writeProgress("Executing template %s", template); | ||
return templateExecutor.execute(); | ||
} | ||
} | ||
|
||
private final String getTemplateHelp(DataExtractTemplateDescriptor template, OptionParameterHelper helper) { | ||
return String.format( | ||
"\nTemplate: %s\n"+ | ||
"\n%s\n"+ | ||
"\nTemplate options:\n"+ | ||
"%s", | ||
template.getName(), template.getDescription(), helper.getSupportedOptionsTable()); | ||
} | ||
|
||
protected abstract String getType(); | ||
protected abstract void configure(DataExtractTemplateRunner templateExecutor); | ||
} |
37 changes: 37 additions & 0 deletions
37
.../com/fortify/cli/common/data_extract/cli/cmd/AbstractDataExtractListTemplatesCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/******************************************************************************* | ||
* Copyright 2021, 2023 Open Text. | ||
* | ||
* The only warranties for products and services of Open Text | ||
* and its affiliates and licensors ("Open Text") are as may | ||
* be set forth in the express warranty statements accompanying | ||
* such products and services. Nothing herein should be construed | ||
* as constituting an additional warranty. Open Text shall not be | ||
* liable for technical or editorial errors or omissions contained | ||
* herein. The information contained herein is subject to change | ||
* without notice. | ||
*******************************************************************************/ | ||
package com.fortify.cli.common.data_extract.cli.cmd; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.node.ObjectNode; | ||
import com.fortify.cli.common.data_extract.helper.DataExtractTemplateHelper; | ||
import com.fortify.cli.common.json.JsonHelper; | ||
import com.fortify.cli.common.output.cli.cmd.AbstractOutputCommand; | ||
import com.fortify.cli.common.output.cli.cmd.IJsonNodeSupplier; | ||
|
||
public abstract class AbstractDataExtractListTemplatesCommand extends AbstractOutputCommand implements IJsonNodeSupplier { | ||
@Override | ||
public final JsonNode getJsonNode() { | ||
return DataExtractTemplateHelper.list(getType()) | ||
.map(JsonHelper.getObjectMapper()::valueToTree) | ||
.map(ObjectNode.class::cast) | ||
.collect(JsonHelper.arrayNodeCollector()); | ||
} | ||
@Override | ||
public final boolean isSingular() { | ||
return false; | ||
} | ||
protected abstract String getType(); | ||
|
||
|
||
} |
83 changes: 83 additions & 0 deletions
83
...on/src/main/java/com/fortify/cli/common/data_extract/helper/DataExtractSpelFunctions.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/******************************************************************************* | ||
* Copyright 2021, 2023 Open Text. | ||
* | ||
* The only warranties for products and services of Open Text | ||
* and its affiliates and licensors ("Open Text") are as may | ||
* be set forth in the express warranty statements accompanying | ||
* such products and services. Nothing herein should be construed | ||
* as constituting an additional warranty. Open Text shall not be | ||
* liable for technical or editorial errors or omissions contained | ||
* herein. The information contained herein is subject to change | ||
* without notice. | ||
*******************************************************************************/ | ||
package com.fortify.cli.common.data_extract.helper; | ||
|
||
import java.util.List; | ||
|
||
import org.jsoup.Jsoup; | ||
import org.jsoup.nodes.Document; | ||
import org.jsoup.safety.Safelist; | ||
|
||
import com.formkiq.graalvm.annotations.Reflectable; | ||
import com.fortify.cli.common.util.StringUtils; | ||
|
||
import lombok.NoArgsConstructor; | ||
|
||
@Reflectable @NoArgsConstructor | ||
public class DataExtractSpelFunctions { | ||
public static final String join(String separator, List<Object> elts) { | ||
switch (separator) { | ||
case "\\n": separator="\n"; break; | ||
case "\\t": separator="\t"; break; | ||
} | ||
return elts==null ? "" : String.join(separator, elts.stream().map(Object::toString).toList()); | ||
} | ||
|
||
/** | ||
* Convenience method to throw an exception if an expression evaluates to false | ||
* @param throwError true if error should be thrown, false otherwise | ||
* @param msg Message for exception to be thrown | ||
* @return true if throwError is false | ||
* @throws IllegalStateException with the given message if throwError is true | ||
*/ | ||
public static final boolean check(boolean throwError, String msg) { | ||
if ( throwError ) { | ||
throw new IllegalStateException(msg); | ||
} else { | ||
return true; | ||
} | ||
} | ||
|
||
/** | ||
* Abbreviate the given text to the given maximum width | ||
* @param text to abbreviate | ||
* @param maxWidth Maximum width | ||
* @return Abbreviated text | ||
*/ | ||
public static final String abbreviate(String text, int maxWidth) { | ||
return StringUtils.abbreviate(text, maxWidth); | ||
} | ||
|
||
/** | ||
* @param html to be converted to plain text | ||
* @return Formatted plain-text string for the given HTML contents | ||
*/ | ||
public static final String htmlToText(String html) { | ||
if( html==null ) { return null; } | ||
Document document = Jsoup.parse(html); | ||
document.outputSettings(new Document.OutputSettings().prettyPrint(false));//makes html() preserve linebreaks and spacing | ||
document.select("br").append("\\n"); | ||
document.select("p").prepend("\\n\\n"); | ||
String s = document.html().replaceAll("\\\\n", "\n"); | ||
return Jsoup.clean(s, "", Safelist.none(), new Document.OutputSettings().prettyPrint(false)); | ||
} | ||
|
||
/** | ||
* @param html to be converted to plain text | ||
* @return Single line of plain text for the given HTML contents | ||
*/ | ||
public static final String htmlToSingleLineText(String html) { | ||
if( html==null ) { return null; } | ||
return Jsoup.clean(html, "", Safelist.none()); | ||
} | ||
} |
Oops, something went wrong.