Skip to content

Commit

Permalink
Added the --relative-to=URI argument to the resolve command to suppor…
Browse files Browse the repository at this point in the history
…t relative generated links during profile resolution. Resolves #84. (#88)
  • Loading branch information
david-waltermire authored Dec 11, 2024
1 parent f06acec commit 9f449aa
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import gov.nist.secauto.metaschema.core.metapath.item.node.IDocumentNodeItem;
import gov.nist.secauto.metaschema.core.metapath.item.node.INodeItem;
import gov.nist.secauto.metaschema.core.util.ObjectUtils;
import gov.nist.secauto.metaschema.core.util.UriUtils;
import gov.nist.secauto.metaschema.databind.IBindingContext;
import gov.nist.secauto.metaschema.databind.io.DeserializationFeature;
import gov.nist.secauto.metaschema.databind.io.Format;
Expand All @@ -33,6 +34,7 @@
import java.io.IOException;
import java.io.PrintStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Path;
import java.util.Collection;
import java.util.List;
Expand All @@ -48,12 +50,19 @@ public abstract class AbstractResolveCommand
private static final List<ExtraArgument> EXTRA_ARGUMENTS = ObjectUtils.notNull(List.of(
ExtraArgument.newInstance("URI to resolve", true),
ExtraArgument.newInstance("destination file", false)));
private static final Option RELATIVE_TO = Option.builder()
.longOpt("relative-to")
.desc("Generate URI references relative to this resource")
.hasArg()
.build();

@NonNull
private static final List<Option> OPTIONS = ObjectUtils.notNull(
List.of(
MetaschemaCommands.AS_FORMAT_OPTION,
MetaschemaCommands.TO_OPTION,
MetaschemaCommands.OVERWRITE_OPTION));
MetaschemaCommands.OVERWRITE_OPTION,
RELATIVE_TO));

@Override
public String getDescription() {
Expand Down Expand Up @@ -153,10 +162,25 @@ protected void executeCommand(
destination = MetaschemaCommands.handleDestination(ObjectUtils.requireNonNull(extraArgs.get(1)), cmdLine);
}

URI relativeTo;
if (cmdLine.hasOption(RELATIVE_TO)) {
relativeTo = getCurrentWorkingDirectory().toUri().resolve(cmdLine.getOptionValue(RELATIVE_TO));
} else {
relativeTo = document.getDocumentUri();
}

// this is a profile
DynamicContext dynamicContext = new DynamicContext(document.getStaticContext());
dynamicContext.setDocumentLoader(loader);
ProfileResolver resolver = new ProfileResolver(dynamicContext);
ProfileResolver resolver = new ProfileResolver(
dynamicContext,
(uri, src) -> {
try {
return UriUtils.relativize(relativeTo, src.resolve(uri), true);
} catch (URISyntaxException ex) {
throw new IllegalArgumentException(ex);
}
});

IDocumentNodeItem resolvedProfile;
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public String getName() {

@Override
public String getDescription() {
return "Generate a diagram for the provided Metaschema module";
return "List allowed values constraints for the provided Metaschema module";
}

@SuppressWarnings("null")
Expand Down

0 comments on commit 9f449aa

Please sign in to comment.