Skip to content

Commit

Permalink
Fixed a schema generation bug causing an existing test to fail due to…
Browse files Browse the repository at this point in the history
… improper handling of JSON keys. Added basic support for arbitrary Metapath command execution. Resolves usnistgov/metaschema-java#241. Builds on metaschema-framework#96.
  • Loading branch information
david-waltermire committed Sep 6, 2024
1 parent 5472808 commit 6995204
Show file tree
Hide file tree
Showing 23 changed files with 568 additions and 215 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
* SPDX-FileCopyrightText: none
* SPDX-License-Identifier: CC0-1.0
*/

package gov.nist.secauto.metaschema.core.metapath.item;

import gov.nist.secauto.metaschema.core.metapath.ICollectionValue;
import gov.nist.secauto.metaschema.core.metapath.ISequence;
import gov.nist.secauto.metaschema.core.metapath.item.atomic.IAnyAtomicItem;
import gov.nist.secauto.metaschema.core.metapath.item.function.IArrayItem;
import gov.nist.secauto.metaschema.core.metapath.item.function.IMapItem;
import gov.nist.secauto.metaschema.core.metapath.item.node.INodeItem;

import java.io.PrintWriter;

import edu.umd.cs.findbugs.annotations.NonNull;

public class DefaultItemWriter implements IItemWriter {

@NonNull
private final PrintWriter writer;
@NonNull
private final Visitor visitor = new Visitor();

public DefaultItemWriter(@NonNull PrintWriter writer) {
this.writer = writer;
}

@Override
public void writeSequence(ISequence<?> sequence) {
boolean wrap = sequence.size() > 1;
if (wrap) {
writer.append('(');
}
for (IItem item : sequence) {
item.accept(visitor);
}

if (wrap) {
writer.append(')');
}
}

@Override
public void writeArray(IArrayItem<?> array) {
writer.append('[');
for (ICollectionValue value : array) {
assert value != null;
writeCollectionValue(value);
}
writer.append(']');
}

@Override
public void writeMap(IMapItem<?> map) {
writer.append("map {");
for (ICollectionValue value : map.values()) {
assert value != null;
writeCollectionValue(value);
}
writer.append('}');
}

@Override
public void writeNode(INodeItem node) {
writer.append(node.getBaseUri().toString());
writer.append('#');
writer.append(node.getMetapath());
}

@Override
public void writeAtomicValue(IAnyAtomicItem node) {
writer.append(node.asString());
}

protected void writeCollectionValue(@NonNull ICollectionValue value) {
if (value instanceof IItem) {
((IItem) value).accept(visitor);
} else if (value instanceof ISequence) {
writeSequence((ISequence<?>) value);
}
}

private final class Visitor implements IItemVisitor {

@Override
public void visit(IArrayItem<?> array) {
writeArray(array);
}

@Override
public void visit(IMapItem<?> map) {
writeMap(map);
}

@Override
public void visit(INodeItem node) {
writeNode(node);
}

@Override
public void visit(IAnyAtomicItem node) {
writeAtomicValue(node);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

import java.util.stream.Stream;

import edu.umd.cs.findbugs.annotations.NonNull;

public interface IItem extends ICollectionValue {

/**
Expand Down Expand Up @@ -49,4 +51,6 @@ default ISequence<?> asSequence() {
default Stream<? extends IItem> flatten() {
return Stream.of(this);
}

void accept(@NonNull IItemVisitor visitor);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* SPDX-FileCopyrightText: none
* SPDX-License-Identifier: CC0-1.0
*/

package gov.nist.secauto.metaschema.core.metapath.item;

import gov.nist.secauto.metaschema.core.metapath.item.atomic.IAnyAtomicItem;
import gov.nist.secauto.metaschema.core.metapath.item.function.IArrayItem;
import gov.nist.secauto.metaschema.core.metapath.item.function.IMapItem;
import gov.nist.secauto.metaschema.core.metapath.item.node.INodeItem;

import edu.umd.cs.findbugs.annotations.NonNull;

public interface IItemVisitor {
void visit(@NonNull IArrayItem<?> array);

void visit(@NonNull IMapItem<?> map);

void visit(@NonNull INodeItem node);

void visit(@NonNull IAnyAtomicItem node);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* SPDX-FileCopyrightText: none
* SPDX-License-Identifier: CC0-1.0
*/

package gov.nist.secauto.metaschema.core.metapath.item;

import gov.nist.secauto.metaschema.core.metapath.ISequence;
import gov.nist.secauto.metaschema.core.metapath.item.atomic.IAnyAtomicItem;
import gov.nist.secauto.metaschema.core.metapath.item.function.IArrayItem;
import gov.nist.secauto.metaschema.core.metapath.item.function.IMapItem;
import gov.nist.secauto.metaschema.core.metapath.item.node.INodeItem;

import edu.umd.cs.findbugs.annotations.NonNull;

public interface IItemWriter {
void writeSequence(@NonNull ISequence<?> sequence);

void writeArray(@NonNull IArrayItem<?> array);

void writeMap(@NonNull IMapItem<?> map);

void writeNode(@NonNull INodeItem node);

void writeAtomicValue(@NonNull IAnyAtomicItem node);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import gov.nist.secauto.metaschema.core.datatype.IDataTypeAdapter;
import gov.nist.secauto.metaschema.core.metapath.IPrintable;
import gov.nist.secauto.metaschema.core.metapath.item.IItemVisitor;
import gov.nist.secauto.metaschema.core.metapath.item.function.IMapItem;
import gov.nist.secauto.metaschema.core.metapath.item.function.IMapKey;
import gov.nist.secauto.metaschema.core.util.ObjectUtils;
Expand Down Expand Up @@ -103,4 +104,9 @@ default IStringItem asStringItem() {
* than, equal to, or greater than the {@code item}.
*/
int compareTo(@NonNull IAnyAtomicItem other);

@Override
default void accept(IItemVisitor visitor) {
visitor.visit(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import gov.nist.secauto.metaschema.core.metapath.impl.AbstractArrayItem;
import gov.nist.secauto.metaschema.core.metapath.impl.ArrayItemN;
import gov.nist.secauto.metaschema.core.metapath.item.IItem;
import gov.nist.secauto.metaschema.core.metapath.item.IItemVisitor;
import gov.nist.secauto.metaschema.core.util.ObjectUtils;

import java.util.ArrayList;
Expand Down Expand Up @@ -570,4 +571,9 @@ static <T extends ICollectionValue> IArrayItem<T> copyOf(@NonNull Collection<? e
? empty()
: new ArrayItemN<>(new ArrayList<>(collection));
}

@Override
default void accept(IItemVisitor visitor) {
visitor.visit(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import gov.nist.secauto.metaschema.core.metapath.impl.AbstractMapItem;
import gov.nist.secauto.metaschema.core.metapath.impl.MapItemN;
import gov.nist.secauto.metaschema.core.metapath.item.IItem;
import gov.nist.secauto.metaschema.core.metapath.item.IItemVisitor;
import gov.nist.secauto.metaschema.core.metapath.item.atomic.IAnyAtomicItem;

import java.util.LinkedHashMap;
Expand Down Expand Up @@ -782,4 +783,9 @@ IMapItem<V> copyOf(Map<? extends IMapKey, ? extends V> map) {
? empty()
: new MapItemN<>(new LinkedHashMap<>(map));
}

@Override
default void accept(IItemVisitor visitor) {
visitor.visit(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,38 +11,11 @@
* A {@link INodeItem} supported by a {@link IFlagInstance}.
*/
abstract class AbstractFlagInstanceNodeItem
extends AbstractInstanceNodeItem<IFlagDefinition, IFlagInstance, IModelNodeItem<?, ?>>
implements IFlagNodeItem {
@NonNull
private final IFlagInstance instance;
@NonNull
private final IModelNodeItem<?, ?> parent;

public AbstractFlagInstanceNodeItem(@NonNull IFlagInstance instance, @NonNull IModelNodeItem<?, ?> parent) {
this.instance = instance;
this.parent = parent;
}

@Override
public IFlagDefinition getDefinition() {
return getInstance().getDefinition();
}

@Override
@NonNull
public IFlagInstance getInstance() {
return instance;
}

@Override
@NonNull
public IModelNodeItem<?, ?> getParentContentNodeItem() {
return getParentNodeItem();
}

@Override
@NonNull
public IModelNodeItem<?, ?> getParentNodeItem() {
return parent;
super(instance, parent);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import gov.nist.secauto.metaschema.core.metapath.format.IPathFormatter;
import gov.nist.secauto.metaschema.core.metapath.format.IPathSegment;
import gov.nist.secauto.metaschema.core.metapath.item.IItem;
import gov.nist.secauto.metaschema.core.metapath.item.IItemVisitor;
import gov.nist.secauto.metaschema.core.model.IResourceLocation;
import gov.nist.secauto.metaschema.core.util.ObjectUtils;

Expand Down Expand Up @@ -285,4 +286,9 @@ default Stream<? extends IFlagNodeItem> flags() {
*/
@NonNull
StaticContext getStaticContext();

@Override
default void accept(IItemVisitor visitor) {
visitor.visit(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
}
],
"complex-fields4": {
"complex-field4-1": {"STRVALUE": "test-string5"},
"complex-field4-2": {"STRVALUE": "test-string6"}
"complex-field4-1": "test-string5",
"complex-field4-2": "test-string6"
}
}
}
11 changes: 11 additions & 0 deletions metaschema-cli/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,17 @@
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
</dependency>

<dependency>
<groupId>io.github.hakky54</groupId>
<artifactId>logcaptor</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public abstract class AbstractValidateContentCommand
private static final Option CONSTRAINTS_OPTION = ObjectUtils.notNull(
Option.builder("c")
.hasArgs()
.argName("URI")
.argName("URL")
.desc("additional constraint definitions")
.build());
@NonNull
Expand Down

This file was deleted.

Loading

0 comments on commit 6995204

Please sign in to comment.