From a3cfe96ec72aae4c4c5961bc47f44ff470dc7a6d Mon Sep 17 00:00:00 2001 From: David Waltermire Date: Mon, 23 Oct 2023 17:02:03 -0400 Subject: [PATCH 01/13] allow web and maven central to be selected. --- .github/workflows/release.yml | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index ba26e1aa1..f6b59f9af 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -3,6 +3,18 @@ on: tags: - "v*" workflow_dispatch: + inputs: + release: + description: 'Delpoy release?' + type: boolean + default: false + website: + description: 'Deploy website?' + type: boolean + default: false + pages-branch: + description: 'Pages branch name' + default: nist-pages name: Deploy Tagged Release jobs: deploy-to-nexus: @@ -38,6 +50,7 @@ jobs: # Maven Deploy # ------------------------- - name: Deploy Maven Artifacts + if: github.event_name == 'push' || (github.event_name == 'workflow_dispatch' && contains('true', github.event.inputs.release)) run: | mvn -B -e -Pgpg -Prelease -Preporting deploy # mvn -Pgpg -Prelease nexus-staging:close -DstagingDescription="closing to release" @@ -46,6 +59,7 @@ jobs: MAVEN_CENTRAL_TOKEN: ${{ secrets.SONATYPE_PASSWORD }} MAVEN_GPG_PASSPHRASE: ${{ secrets.GPG_PASSPHRASE }} - name: Create release + if: github.event_name == 'push' || (github.event_name == 'workflow_dispatch' && contains('true', github.event.inputs.release)) uses: softprops/action-gh-release@9d7c94cfd0a1f3ed45544c887983e9fa900f0564 with: draft: true @@ -57,9 +71,14 @@ jobs: # Maven Site # ------------------------- - name: Build Website + if: github.event_name == 'push' || (github.event_name == 'workflow_dispatch' && contains('true', github.event.inputs.website)) run: | mvn -B -e -Prelease -Preporting install site site:stage - - name: Run Website Deploy Script - run: | - touch target/staging/.nojekyll - bash .github/workflows/deploy.sh --push-only -v -m "Deploying website [ci skip]" + - name: Website Deploy + uses: peaceiris/actions-gh-pages@373f7f263a76c20808c831209c920827a82a2847 + if: github.event_name == 'push' || (github.event_name == 'workflow_dispatch' && contains('true', github.event.inputs.website)) + with: + personal_token: ${{ secrets.COMMIT_TOKEN }} + publish_dir: ./target/staging + external_repository: ${{ github.repository }} + publish_branch: ${{ (github.event_name == 'workflow_dispatch' && github.event.inputs.pages-branch) || 'nist-pages' }} From ef34bc3c8cc90eea011c8743394ef01171aec4d0 Mon Sep 17 00:00:00 2001 From: David Waltermire Date: Sun, 21 Apr 2024 02:23:52 -0400 Subject: [PATCH 02/13] normalizing line endings --- core/metaschema | 2 +- .../datatype/AbstractCustomJavaDataType.java | 174 +-- .../AbstractCustomJavaDataTypeAdapter.java | 136 +- .../core/datatype/IDataTypeAdapter.java | 776 +++++------ .../core/datatype/adapter/Base64Adapter.java | 202 +-- .../core/datatype/adapter/BooleanAdapter.java | 312 ++--- .../core/datatype/adapter/DateAdapter.java | 294 ++-- .../datatype/adapter/DateTimeAdapter.java | 262 ++-- .../adapter/DateTimeWithTZAdapter.java | 192 +-- .../datatype/adapter/DateWithTZAdapter.java | 216 +-- .../core/datatype/adapter/DecimalAdapter.java | 230 ++-- .../datatype/adapter/EmailAddressAdapter.java | 128 +- .../datatype/adapter/HostnameAdapter.java | 122 +- .../datatype/adapter/IPv4AddressAdapter.java | 194 +-- .../datatype/adapter/IPv6AddressAdapter.java | 194 +-- .../core/datatype/adapter/IntegerAdapter.java | 158 +-- .../core/datatype/adapter/NcNameAdapter.java | 128 +- .../adapter/NonNegativeIntegerAdapter.java | 130 +- .../adapter/PositiveIntegerAdapter.java | 130 +- .../core/datatype/adapter/StringAdapter.java | 122 +- .../core/datatype/adapter/UriAdapter.java | 166 +-- .../datatype/adapter/UriReferenceAdapter.java | 164 +-- .../datatype/markup/AbstractMarkupString.java | 600 ++++---- .../core/datatype/markup/MarkupLine.java | 220 +-- .../datatype/markup/MarkupLineAdapter.java | 200 +-- .../core/datatype/markup/MarkupMultiline.java | 192 +-- .../markup/MarkupMultilineAdapter.java | 222 +-- .../markup/flexmark/AstCollectingVisitor.java | 176 +-- .../markup/flexmark/FlexmarkFactory.java | 206 +-- .../flexmark/InsertAnchorExtension.java | 696 +++++----- .../markup/flexmark/XmlMarkupParser.java | 494 +++---- .../metaschema/core/datatype/object/Date.java | 108 +- .../core/datatype/object/DateTime.java | 108 +- .../core/model/JsonGroupAsBehavior.java | 104 +- .../core/model/MetaschemaException.java | 176 +-- .../core/model/XmlGroupAsBehavior.java | 76 +- .../core/model/util/XmlEventUtil.java | 1210 ++++++++--------- .../metaschema/core/util/CollectionUtil.java | 522 +++---- .../datatype/markup/MarkupStringTest.java | 788 +++++------ .../markup/flexmark/MarkupParserTest.java | 242 ++-- databind/pom.xml | 364 ++--- .../metaschema/databind/IBindingContext.java | 982 ++++++------- .../databind/codegen/JavaGenerator.java | 204 +-- .../databind/io/BindingException.java | 154 +-- .../databind/io/IParsingContext.java | 110 +- .../databind/io/xml/CommentFilter.java | 80 +- .../databind/io/xml/IXmlParsingContext.java | 168 +-- .../databind/io/xml/IXmlProblemHandler.java | 280 ++-- .../model/annotations/BoundAssembly.java | 290 ++-- .../model/annotations/BoundField.java | 380 +++--- .../databind/model/annotations/BoundFlag.java | 292 ++-- .../databind/model/annotations/Ignore.java | 84 +- .../annotations/JsonFieldValueKeyFlag.java | 100 +- .../databind/model/annotations/JsonKey.java | 96 +- .../model/annotations/MetaschemaAssembly.java | 292 ++-- .../databind/model/annotations/XmlNs.java | 114 +- .../databind/model/annotations/XmlNsForm.java | 66 +- .../databind/model/annotations/XmlSchema.java | 188 +-- .../model/impl/ClassIntrospector.java | 142 +- .../databind/codegen/BasicMetaschemaTest.java | 370 ++--- .../metaschema/codegen/test/parent.json | 16 +- .../metaschema/assembly/example.json | 164 +-- .../resources/metaschema/assembly/example.xml | 78 +- .../metaschema/assembly/metaschema.xml | 138 +- .../metaschema/bad_index-has-key/example.json | 22 +- .../metaschema/fields_with_flags/example.json | 54 +- .../metaschema/fields_with_flags/example.xml | 22 +- .../fields_with_flags/metaschema.xml | 132 +- .../resources/metaschema/simple/example.json | 4 +- .../resources/metaschema/simple/example.xml | 4 +- .../metaschema/simple/metaschema.xml | 46 +- .../metaschema/simple_with_field/example.json | 20 +- .../metaschema/simple_with_field/example.xml | 34 +- .../simple_with_field/metaschema.xml | 146 +- .../metaschema/simple_with_uuid/example.json | 4 +- .../metaschema/simple_with_uuid/example.xml | 4 +- .../simple_with_uuid/metaschema.xml | 46 +- 77 files changed, 8231 insertions(+), 8231 deletions(-) diff --git a/core/metaschema b/core/metaschema index 16919d512..9527c9441 160000 --- a/core/metaschema +++ b/core/metaschema @@ -1 +1 @@ -Subproject commit 16919d512f96110c49fa2edcd3e6ee9a377c0b69 +Subproject commit 9527c9441f02fa8d31bf03b6e16190f6de8554eb diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/AbstractCustomJavaDataType.java b/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/AbstractCustomJavaDataType.java index e5ad1b43a..d42b85329 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/AbstractCustomJavaDataType.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/AbstractCustomJavaDataType.java @@ -1,87 +1,87 @@ -/* - * Portions of this software was developed by employees of the National Institute - * of Standards and Technology (NIST), an agency of the Federal Government and is - * being made available as a public service. Pursuant to title 17 United States - * Code Section 105, works of NIST employees are not subject to copyright - * protection in the United States. This software may be subject to foreign - * copyright. Permission in the United States and in foreign countries, to the - * extent that NIST may hold copyright, to use, copy, modify, create derivative - * works, and distribute this software and its documentation without fee is hereby - * granted on a non-exclusive basis, provided that this notice and disclaimer - * of warranty appears in all copies. - * - * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER - * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY - * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM - * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE - * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT - * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, - * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, - * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, - * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR - * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT - * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. - */ - -package gov.nist.secauto.metaschema.core.datatype; - -import gov.nist.secauto.metaschema.core.util.ObjectUtils; - -import java.util.Objects; - -import edu.umd.cs.findbugs.annotations.NonNull; - -/** - * A common base implementation of a custom Java object providing an underlying - * implementation of a data type. - * - * @param - * the bound object type supported by this data type - * @param - * the inner value of the data type object - */ -public abstract class AbstractCustomJavaDataType, VALUE> - implements ICustomJavaDataType { - @NonNull - private final VALUE value; - - /** - * Construct a new instance of a custom Java object-based data value. - * - * @param value - * the bound object that the data type is based on - */ - protected AbstractCustomJavaDataType(@NonNull VALUE value) { - this.value = ObjectUtils.requireNonNull(value, "value"); - } - - /** - * Get the bound Java object value. - * - * @return the bound object - */ - @NonNull - public VALUE getValue() { - return value; - } - // - // public void setValue(T value) { - // this.value = value; - // } - - @Override - public int hashCode() { - return value.hashCode(); - } - - @Override - public boolean equals(Object obj) { - return Objects.equals(value, obj); - } - - @Override - public String toString() { - return value.toString(); - } -} +/* + * Portions of this software was developed by employees of the National Institute + * of Standards and Technology (NIST), an agency of the Federal Government and is + * being made available as a public service. Pursuant to title 17 United States + * Code Section 105, works of NIST employees are not subject to copyright + * protection in the United States. This software may be subject to foreign + * copyright. Permission in the United States and in foreign countries, to the + * extent that NIST may hold copyright, to use, copy, modify, create derivative + * works, and distribute this software and its documentation without fee is hereby + * granted on a non-exclusive basis, provided that this notice and disclaimer + * of warranty appears in all copies. + * + * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER + * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY + * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM + * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE + * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT + * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, + * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, + * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, + * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR + * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT + * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. + */ + +package gov.nist.secauto.metaschema.core.datatype; + +import gov.nist.secauto.metaschema.core.util.ObjectUtils; + +import java.util.Objects; + +import edu.umd.cs.findbugs.annotations.NonNull; + +/** + * A common base implementation of a custom Java object providing an underlying + * implementation of a data type. + * + * @param + * the bound object type supported by this data type + * @param + * the inner value of the data type object + */ +public abstract class AbstractCustomJavaDataType, VALUE> + implements ICustomJavaDataType { + @NonNull + private final VALUE value; + + /** + * Construct a new instance of a custom Java object-based data value. + * + * @param value + * the bound object that the data type is based on + */ + protected AbstractCustomJavaDataType(@NonNull VALUE value) { + this.value = ObjectUtils.requireNonNull(value, "value"); + } + + /** + * Get the bound Java object value. + * + * @return the bound object + */ + @NonNull + public VALUE getValue() { + return value; + } + // + // public void setValue(T value) { + // this.value = value; + // } + + @Override + public int hashCode() { + return value.hashCode(); + } + + @Override + public boolean equals(Object obj) { + return Objects.equals(value, obj); + } + + @Override + public String toString() { + return value.toString(); + } +} diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/AbstractCustomJavaDataTypeAdapter.java b/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/AbstractCustomJavaDataTypeAdapter.java index 9cad33bc4..8908f6d36 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/AbstractCustomJavaDataTypeAdapter.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/AbstractCustomJavaDataTypeAdapter.java @@ -1,68 +1,68 @@ -/* - * Portions of this software was developed by employees of the National Institute - * of Standards and Technology (NIST), an agency of the Federal Government and is - * being made available as a public service. Pursuant to title 17 United States - * Code Section 105, works of NIST employees are not subject to copyright - * protection in the United States. This software may be subject to foreign - * copyright. Permission in the United States and in foreign countries, to the - * extent that NIST may hold copyright, to use, copy, modify, create derivative - * works, and distribute this software and its documentation without fee is hereby - * granted on a non-exclusive basis, provided that this notice and disclaimer - * of warranty appears in all copies. - * - * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER - * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY - * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM - * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE - * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT - * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, - * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, - * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, - * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR - * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT - * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. - */ - -package gov.nist.secauto.metaschema.core.datatype; - -import gov.nist.secauto.metaschema.core.metapath.item.atomic.IAnyAtomicItem; - -import edu.umd.cs.findbugs.annotations.NonNull; - -/** - * Provides a Java type adapter implementation for data types that are based on - * {@link ICustomJavaDataType}. - * - * @param - * the Java type this adapter supports, which is based on - * {@link ICustomJavaDataType} - * @param - * the Metapath item type associated with the adapter - */ -public abstract class AbstractCustomJavaDataTypeAdapter< - TYPE extends ICustomJavaDataType< - TYPE>, - ITEM_TYPE extends IAnyAtomicItem> - extends AbstractDataTypeAdapter { - - /** - * Construct a new Java type adapter for the class based on - * {@link ICustomJavaDataType}. - * - * @param clazz - * a data type class based on {@link ICustomJavaDataType} - */ - public AbstractCustomJavaDataTypeAdapter(@NonNull Class clazz) { - super(clazz); - } - - @SuppressWarnings("unchecked") - @Override - public TYPE copy(Object obj) { - // Datatype-based types are required to provide a copy method. Delegate to this - // method. - return ((TYPE) obj).copy(); - } - -} +/* + * Portions of this software was developed by employees of the National Institute + * of Standards and Technology (NIST), an agency of the Federal Government and is + * being made available as a public service. Pursuant to title 17 United States + * Code Section 105, works of NIST employees are not subject to copyright + * protection in the United States. This software may be subject to foreign + * copyright. Permission in the United States and in foreign countries, to the + * extent that NIST may hold copyright, to use, copy, modify, create derivative + * works, and distribute this software and its documentation without fee is hereby + * granted on a non-exclusive basis, provided that this notice and disclaimer + * of warranty appears in all copies. + * + * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER + * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY + * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM + * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE + * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT + * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, + * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, + * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, + * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR + * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT + * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. + */ + +package gov.nist.secauto.metaschema.core.datatype; + +import gov.nist.secauto.metaschema.core.metapath.item.atomic.IAnyAtomicItem; + +import edu.umd.cs.findbugs.annotations.NonNull; + +/** + * Provides a Java type adapter implementation for data types that are based on + * {@link ICustomJavaDataType}. + * + * @param + * the Java type this adapter supports, which is based on + * {@link ICustomJavaDataType} + * @param + * the Metapath item type associated with the adapter + */ +public abstract class AbstractCustomJavaDataTypeAdapter< + TYPE extends ICustomJavaDataType< + TYPE>, + ITEM_TYPE extends IAnyAtomicItem> + extends AbstractDataTypeAdapter { + + /** + * Construct a new Java type adapter for the class based on + * {@link ICustomJavaDataType}. + * + * @param clazz + * a data type class based on {@link ICustomJavaDataType} + */ + public AbstractCustomJavaDataTypeAdapter(@NonNull Class clazz) { + super(clazz); + } + + @SuppressWarnings("unchecked") + @Override + public TYPE copy(Object obj) { + // Datatype-based types are required to provide a copy method. Delegate to this + // method. + return ((TYPE) obj).copy(); + } + +} diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/IDataTypeAdapter.java b/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/IDataTypeAdapter.java index 826c26f48..3c967782f 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/IDataTypeAdapter.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/IDataTypeAdapter.java @@ -1,388 +1,388 @@ -/* - * Portions of this software was developed by employees of the National Institute - * of Standards and Technology (NIST), an agency of the Federal Government and is - * being made available as a public service. Pursuant to title 17 United States - * Code Section 105, works of NIST employees are not subject to copyright - * protection in the United States. This software may be subject to foreign - * copyright. Permission in the United States and in foreign countries, to the - * extent that NIST may hold copyright, to use, copy, modify, create derivative - * works, and distribute this software and its documentation without fee is hereby - * granted on a non-exclusive basis, provided that this notice and disclaimer - * of warranty appears in all copies. - * - * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER - * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY - * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM - * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE - * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT - * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, - * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, - * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, - * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR - * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT - * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. - */ - -package gov.nist.secauto.metaschema.core.datatype; - -import com.fasterxml.jackson.core.JsonGenerator; -import com.fasterxml.jackson.core.JsonParser; -import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonFormatTypes; - -import gov.nist.secauto.metaschema.core.metapath.function.InvalidValueForCastFunctionException; -import gov.nist.secauto.metaschema.core.metapath.item.atomic.IAnyAtomicItem; -import gov.nist.secauto.metaschema.core.util.ObjectUtils; - -import org.codehaus.stax2.XMLEventReader2; -import org.codehaus.stax2.XMLStreamWriter2; -import org.codehaus.stax2.evt.XMLEventFactory2; - -import java.io.IOException; -import java.lang.reflect.Field; -import java.util.List; -import java.util.function.Supplier; - -import javax.xml.namespace.QName; -import javax.xml.stream.XMLEventWriter; -import javax.xml.stream.XMLStreamException; -import javax.xml.stream.events.StartElement; -import javax.xml.stream.events.XMLEvent; - -import edu.umd.cs.findbugs.annotations.NonNull; - -public interface IDataTypeAdapter { - - /** - * Get the metaschema type names associated with this adapter. This name must be - * unique with respect to all other metaschema types. - *

- * At least one name must be provided, with the first name being the most - * preferred name. - * - * @return the name - */ - @NonNull - List getNames(); - - /** - * The JSON primative type of the data type. - * - * @return the JSON data type - */ - JsonFormatTypes getJsonRawType(); - - /** - * Get the most preferred name for this data type. - * - * @return the name - */ - @NonNull - default String getPreferredName() { - return ObjectUtils.notNull(getNames().iterator().next()); - } - - /** - * Get the Java class supported by this adapter. - * - * @return the Java class - */ - @NonNull - Class getJavaClass(); - - /** - * Casts the provided value to the type associated with this adapter. - * - * @param value - * a value of the provided type - * @return the typed value - */ - @NonNull - TYPE toValue(@NonNull Object value); - - /** - * Gets the value as a string suitable for writing as text. This is intended for - * data types that have a simple string-based structure in XML and JSON, such as - * for XML attributes or JSON keys. An adapter for a complex data structures - * that consist of XML elements will throw an - * {@link UnsupportedOperationException} when this is called. - * - * @param value - * the data to formatted as a string - * @return a string - * @throws UnsupportedOperationException - * if the data type cannot be represented as a string - */ - @NonNull - String asString(@NonNull Object value); - - /** - * Create a copy of the provided value. - * - * @param obj - * the value to copy - * @return the copy - */ - @NonNull - TYPE copy(@NonNull Object obj); - - /** - * Determines if the data type is an atomic, scalar value. Complex structures - * such as Markup are not considered atomic. - * - * @return {@code true} if the data type is an atomic scalar value, or - * {@code false} otherwise - */ - default boolean isAtomic() { - return true; - } - - /** - * Get the java type of the associated item. - * - * @return the java associated item type - */ - @NonNull - Class getItemClass(); - - /** - * Construct a new item of this type using the provided value. - * - * @param value - * the item's value - * @return a new item - */ - // TODO: markup types are not atomic values. Figure out a better base type - // (i.e., IValuedItem) - @NonNull - IAnyAtomicItem newItem(@NonNull Object value); - - /** - * Cast the provided item to an item of this type, if possible. - * - * @param item - * the atomic item to cast - * @return an atomic item of this type - * @throws InvalidValueForCastFunctionException - * if the provided item type cannot be cast to this item type - */ - @NonNull - IAnyAtomicItem cast(IAnyAtomicItem item); - - /** - * Indicates if the adapter will parse the {@link XMLEvent#START_ELEMENT} before - * parsing the value data. - * - * @return {@code true} if the adapter requires the start element for parsing, - * or {@code false} otherwise - */ - // TODO; implement or remove this - boolean isParsingStartElement(); - - /** - * Determines if adapter can parse the next element. The next element's - * {@link QName} is provided for this purpose. - *

- * This will be called when the parser encounter's an element it does not - * recognize. This gives the adapter a chance to request parsing of the data. - * - * @param nextElementQName - * the next element's namespace-qualified name - * @return {@code true} if the adapter will parse the element, or {@code false} - * otherwise - */ - boolean canHandleQName(@NonNull QName nextElementQName); - - /** - * Parses a provided string. Used to parse XML attributes, simple XML character - * data, and JSON/YAML property values. - * - * @param value - * the string value to parse - * @return the parsed data as the adapter's type - * @throws IllegalArgumentException - * if the data is not valid to the data type - */ - @NonNull - TYPE parse(@NonNull String value); - - /** - * This method is expected to parse content starting at the next event. Parsing - * will continue until the next event represents content that is not handled by - * this adapter. This means the event stream should be positioned after any - * {@link XMLEvent#END_ELEMENT} that corresponds to an - * {@link XMLEvent#START_ELEMENT} parsed by this adapter. - *

- * If {@link #isParsingStartElement()} returns {@code true}, then the first - * event to parse will be the {@link XMLEvent#START_ELEMENT} for the element - * that contains the value data, then the value data. If this is the case, this - * method must also parse the corresponding {@link XMLEvent#END_ELEMENT}. - * Otherwise, the first event to parse will be the value data. - *

- * The value data is expected to be parsed completely, leaving the event stream - * on a peeked event corresponding to content that is not handled by this - * method. - * - * @param eventReader - * the XML parser used to read the parsed value - * @return the parsed value - * @throws IOException - * if a parsing error occurs - */ - @NonNull - TYPE parse(@NonNull XMLEventReader2 eventReader) throws IOException; - - /** - * Parses a JSON property value. - * - * @param parser - * the JSON parser used to read the parsed value - * @return the parsed value - * @throws IOException - * if a parsing error occurs - */ - @NonNull - TYPE parse(@NonNull JsonParser parser) throws IOException; - - /** - * Parses a provided string using {@link #parse(String)}. - *

- * This method may pre-parse the data and then return copies, since the data can - * only be parsed once, but the supplier might be called multiple times. - * - * @param value - * the string value to parse - * @return a supplier that will provide new instances of the parsed data - * @throws IOException - * if an error occurs while parsing - * @see #parse(String) - */ - @NonNull - default Supplier parseAndSupply(@NonNull String value) throws IOException { - TYPE retval = parse(value); - return () -> copy(retval); - } - - /** - * Parses a provided string using - * {@link IDataTypeAdapter#parse(XMLEventReader2)}. - *

- * This method may pre-parse the data and then return copies, since the data can - * only be parsed once, but the supplier might be called multiple times. - * - * @param eventReader - * the XML parser used to read the parsed value - * @return a supplier that will provide new instances of the parsed data - * @throws IOException - * if an error occurs while parsing - * @see #parse(String) - * @see #parse(XMLEventReader2) - */ - @NonNull - default Supplier parseAndSupply(@NonNull XMLEventReader2 eventReader) throws IOException { - TYPE retval = parse(eventReader); - return () -> copy(retval); - } - - /** - * Parses a provided string using {@link #parse(JsonParser)}. - *

- * This method may pre-parse the data and then return copies, since the data can - * only be parsed once, but the supplier might be called multiple times. - * - * @param parser - * the JSON parser used to read the parsed value - * @return a supplier that will provide new instances of the parsed data - * @throws IOException - * if an error occurs while parsing - * @see #parse(String) - * @see #parse(JsonParser) - */ - @NonNull - default Supplier parseAndSupply(@NonNull JsonParser parser) throws IOException { - TYPE retval = parse(parser); - return () -> copy(retval); - } - - /** - * Writes the provided Java class instance data as XML. The parent element - * information is provided as a {@link StartElement} event, which allows - * namespace information to be obtained from the parent element using the - * {@link StartElement#getName()} and {@link StartElement#getNamespaceContext()} - * methods, which can be used when writing the provided instance value. - * - * @param instance - * the {@link Field} instance value to write - * @param parent - * the {@link StartElement} XML event that is the parent of the data to - * write - * @param eventFactory - * the XML event factory used to generate XML writing events - * @param eventWriter - * the XML writer used to output XML as events - * @throws XMLStreamException - * if an unexpected error occurred while processing the XML output - * @throws IOException - * if an unexpected error occurred while writing to the output stream - */ - void writeXmlValue(@NonNull Object instance, @NonNull StartElement parent, @NonNull XMLEventFactory2 eventFactory, - @NonNull XMLEventWriter eventWriter) - throws IOException, XMLStreamException; - - /** - * Writes the provided Java class instance data as XML. The parent element - * information is provided as an XML {@link QName}, which allows namespace - * information to be obtained from the parent element. Additional namespace - * information can be gathered using the - * {@link XMLStreamWriter2#getNamespaceContext()} method, which can be used when - * writing the provided instance value. - * - * @param instance - * the {@link Field} instance value to write - * @param parentName - * the qualified name of the XML data's parent element - * @param writer - * the XML writer used to output the XML data - * @throws XMLStreamException - * if an unexpected error occurred while processing the XML output - */ - void writeXmlValue(@NonNull Object instance, @NonNull QName parentName, @NonNull XMLStreamWriter2 writer) - throws XMLStreamException; - - /** - * Writes the provided Java class instance as a JSON/YAML field value. - * - * @param instance - * the {@link Field} instance value to write - * @param writer - * the JSON/YAML writer used to output the JSON/YAML data - * @throws IOException - * if an unexpected error occurred while writing the JSON/YAML output - */ - void writeJsonValue(@NonNull Object instance, @NonNull JsonGenerator writer) throws IOException; - - /** - * Gets the default value to use as the JSON/YAML field name for a Metaschema - * field value if no JSON value key flag or name is configured. - * - * @return the default field name to use - */ - @NonNull - String getDefaultJsonValueKey(); - - /** - * Determines if the data type's value is allowed to be unwrapped in XML when - * the value is a field value. - * - * @return {@code true} if allowed, or {@code false} otherwise. - */ - boolean isUnrappedValueAllowedInXml(); - - /** - * Determines if the datatype uses mixed text and element content in XML. - * - * @return {@code true} if the datatype uses mixed text and element content in - * XML, or {@code false} otherwise - */ - boolean isXmlMixed(); -} +/* + * Portions of this software was developed by employees of the National Institute + * of Standards and Technology (NIST), an agency of the Federal Government and is + * being made available as a public service. Pursuant to title 17 United States + * Code Section 105, works of NIST employees are not subject to copyright + * protection in the United States. This software may be subject to foreign + * copyright. Permission in the United States and in foreign countries, to the + * extent that NIST may hold copyright, to use, copy, modify, create derivative + * works, and distribute this software and its documentation without fee is hereby + * granted on a non-exclusive basis, provided that this notice and disclaimer + * of warranty appears in all copies. + * + * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER + * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY + * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM + * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE + * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT + * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, + * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, + * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, + * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR + * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT + * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. + */ + +package gov.nist.secauto.metaschema.core.datatype; + +import com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonFormatTypes; + +import gov.nist.secauto.metaschema.core.metapath.function.InvalidValueForCastFunctionException; +import gov.nist.secauto.metaschema.core.metapath.item.atomic.IAnyAtomicItem; +import gov.nist.secauto.metaschema.core.util.ObjectUtils; + +import org.codehaus.stax2.XMLEventReader2; +import org.codehaus.stax2.XMLStreamWriter2; +import org.codehaus.stax2.evt.XMLEventFactory2; + +import java.io.IOException; +import java.lang.reflect.Field; +import java.util.List; +import java.util.function.Supplier; + +import javax.xml.namespace.QName; +import javax.xml.stream.XMLEventWriter; +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.events.StartElement; +import javax.xml.stream.events.XMLEvent; + +import edu.umd.cs.findbugs.annotations.NonNull; + +public interface IDataTypeAdapter { + + /** + * Get the metaschema type names associated with this adapter. This name must be + * unique with respect to all other metaschema types. + *

+ * At least one name must be provided, with the first name being the most + * preferred name. + * + * @return the name + */ + @NonNull + List getNames(); + + /** + * The JSON primative type of the data type. + * + * @return the JSON data type + */ + JsonFormatTypes getJsonRawType(); + + /** + * Get the most preferred name for this data type. + * + * @return the name + */ + @NonNull + default String getPreferredName() { + return ObjectUtils.notNull(getNames().iterator().next()); + } + + /** + * Get the Java class supported by this adapter. + * + * @return the Java class + */ + @NonNull + Class getJavaClass(); + + /** + * Casts the provided value to the type associated with this adapter. + * + * @param value + * a value of the provided type + * @return the typed value + */ + @NonNull + TYPE toValue(@NonNull Object value); + + /** + * Gets the value as a string suitable for writing as text. This is intended for + * data types that have a simple string-based structure in XML and JSON, such as + * for XML attributes or JSON keys. An adapter for a complex data structures + * that consist of XML elements will throw an + * {@link UnsupportedOperationException} when this is called. + * + * @param value + * the data to formatted as a string + * @return a string + * @throws UnsupportedOperationException + * if the data type cannot be represented as a string + */ + @NonNull + String asString(@NonNull Object value); + + /** + * Create a copy of the provided value. + * + * @param obj + * the value to copy + * @return the copy + */ + @NonNull + TYPE copy(@NonNull Object obj); + + /** + * Determines if the data type is an atomic, scalar value. Complex structures + * such as Markup are not considered atomic. + * + * @return {@code true} if the data type is an atomic scalar value, or + * {@code false} otherwise + */ + default boolean isAtomic() { + return true; + } + + /** + * Get the java type of the associated item. + * + * @return the java associated item type + */ + @NonNull + Class getItemClass(); + + /** + * Construct a new item of this type using the provided value. + * + * @param value + * the item's value + * @return a new item + */ + // TODO: markup types are not atomic values. Figure out a better base type + // (i.e., IValuedItem) + @NonNull + IAnyAtomicItem newItem(@NonNull Object value); + + /** + * Cast the provided item to an item of this type, if possible. + * + * @param item + * the atomic item to cast + * @return an atomic item of this type + * @throws InvalidValueForCastFunctionException + * if the provided item type cannot be cast to this item type + */ + @NonNull + IAnyAtomicItem cast(IAnyAtomicItem item); + + /** + * Indicates if the adapter will parse the {@link XMLEvent#START_ELEMENT} before + * parsing the value data. + * + * @return {@code true} if the adapter requires the start element for parsing, + * or {@code false} otherwise + */ + // TODO; implement or remove this + boolean isParsingStartElement(); + + /** + * Determines if adapter can parse the next element. The next element's + * {@link QName} is provided for this purpose. + *

+ * This will be called when the parser encounter's an element it does not + * recognize. This gives the adapter a chance to request parsing of the data. + * + * @param nextElementQName + * the next element's namespace-qualified name + * @return {@code true} if the adapter will parse the element, or {@code false} + * otherwise + */ + boolean canHandleQName(@NonNull QName nextElementQName); + + /** + * Parses a provided string. Used to parse XML attributes, simple XML character + * data, and JSON/YAML property values. + * + * @param value + * the string value to parse + * @return the parsed data as the adapter's type + * @throws IllegalArgumentException + * if the data is not valid to the data type + */ + @NonNull + TYPE parse(@NonNull String value); + + /** + * This method is expected to parse content starting at the next event. Parsing + * will continue until the next event represents content that is not handled by + * this adapter. This means the event stream should be positioned after any + * {@link XMLEvent#END_ELEMENT} that corresponds to an + * {@link XMLEvent#START_ELEMENT} parsed by this adapter. + *

+ * If {@link #isParsingStartElement()} returns {@code true}, then the first + * event to parse will be the {@link XMLEvent#START_ELEMENT} for the element + * that contains the value data, then the value data. If this is the case, this + * method must also parse the corresponding {@link XMLEvent#END_ELEMENT}. + * Otherwise, the first event to parse will be the value data. + *

+ * The value data is expected to be parsed completely, leaving the event stream + * on a peeked event corresponding to content that is not handled by this + * method. + * + * @param eventReader + * the XML parser used to read the parsed value + * @return the parsed value + * @throws IOException + * if a parsing error occurs + */ + @NonNull + TYPE parse(@NonNull XMLEventReader2 eventReader) throws IOException; + + /** + * Parses a JSON property value. + * + * @param parser + * the JSON parser used to read the parsed value + * @return the parsed value + * @throws IOException + * if a parsing error occurs + */ + @NonNull + TYPE parse(@NonNull JsonParser parser) throws IOException; + + /** + * Parses a provided string using {@link #parse(String)}. + *

+ * This method may pre-parse the data and then return copies, since the data can + * only be parsed once, but the supplier might be called multiple times. + * + * @param value + * the string value to parse + * @return a supplier that will provide new instances of the parsed data + * @throws IOException + * if an error occurs while parsing + * @see #parse(String) + */ + @NonNull + default Supplier parseAndSupply(@NonNull String value) throws IOException { + TYPE retval = parse(value); + return () -> copy(retval); + } + + /** + * Parses a provided string using + * {@link IDataTypeAdapter#parse(XMLEventReader2)}. + *

+ * This method may pre-parse the data and then return copies, since the data can + * only be parsed once, but the supplier might be called multiple times. + * + * @param eventReader + * the XML parser used to read the parsed value + * @return a supplier that will provide new instances of the parsed data + * @throws IOException + * if an error occurs while parsing + * @see #parse(String) + * @see #parse(XMLEventReader2) + */ + @NonNull + default Supplier parseAndSupply(@NonNull XMLEventReader2 eventReader) throws IOException { + TYPE retval = parse(eventReader); + return () -> copy(retval); + } + + /** + * Parses a provided string using {@link #parse(JsonParser)}. + *

+ * This method may pre-parse the data and then return copies, since the data can + * only be parsed once, but the supplier might be called multiple times. + * + * @param parser + * the JSON parser used to read the parsed value + * @return a supplier that will provide new instances of the parsed data + * @throws IOException + * if an error occurs while parsing + * @see #parse(String) + * @see #parse(JsonParser) + */ + @NonNull + default Supplier parseAndSupply(@NonNull JsonParser parser) throws IOException { + TYPE retval = parse(parser); + return () -> copy(retval); + } + + /** + * Writes the provided Java class instance data as XML. The parent element + * information is provided as a {@link StartElement} event, which allows + * namespace information to be obtained from the parent element using the + * {@link StartElement#getName()} and {@link StartElement#getNamespaceContext()} + * methods, which can be used when writing the provided instance value. + * + * @param instance + * the {@link Field} instance value to write + * @param parent + * the {@link StartElement} XML event that is the parent of the data to + * write + * @param eventFactory + * the XML event factory used to generate XML writing events + * @param eventWriter + * the XML writer used to output XML as events + * @throws XMLStreamException + * if an unexpected error occurred while processing the XML output + * @throws IOException + * if an unexpected error occurred while writing to the output stream + */ + void writeXmlValue(@NonNull Object instance, @NonNull StartElement parent, @NonNull XMLEventFactory2 eventFactory, + @NonNull XMLEventWriter eventWriter) + throws IOException, XMLStreamException; + + /** + * Writes the provided Java class instance data as XML. The parent element + * information is provided as an XML {@link QName}, which allows namespace + * information to be obtained from the parent element. Additional namespace + * information can be gathered using the + * {@link XMLStreamWriter2#getNamespaceContext()} method, which can be used when + * writing the provided instance value. + * + * @param instance + * the {@link Field} instance value to write + * @param parentName + * the qualified name of the XML data's parent element + * @param writer + * the XML writer used to output the XML data + * @throws XMLStreamException + * if an unexpected error occurred while processing the XML output + */ + void writeXmlValue(@NonNull Object instance, @NonNull QName parentName, @NonNull XMLStreamWriter2 writer) + throws XMLStreamException; + + /** + * Writes the provided Java class instance as a JSON/YAML field value. + * + * @param instance + * the {@link Field} instance value to write + * @param writer + * the JSON/YAML writer used to output the JSON/YAML data + * @throws IOException + * if an unexpected error occurred while writing the JSON/YAML output + */ + void writeJsonValue(@NonNull Object instance, @NonNull JsonGenerator writer) throws IOException; + + /** + * Gets the default value to use as the JSON/YAML field name for a Metaschema + * field value if no JSON value key flag or name is configured. + * + * @return the default field name to use + */ + @NonNull + String getDefaultJsonValueKey(); + + /** + * Determines if the data type's value is allowed to be unwrapped in XML when + * the value is a field value. + * + * @return {@code true} if allowed, or {@code false} otherwise. + */ + boolean isUnrappedValueAllowedInXml(); + + /** + * Determines if the datatype uses mixed text and element content in XML. + * + * @return {@code true} if the datatype uses mixed text and element content in + * XML, or {@code false} otherwise + */ + boolean isXmlMixed(); +} diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/adapter/Base64Adapter.java b/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/adapter/Base64Adapter.java index 4e40ba80d..4f4847cec 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/adapter/Base64Adapter.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/adapter/Base64Adapter.java @@ -1,101 +1,101 @@ -/* - * Portions of this software was developed by employees of the National Institute - * of Standards and Technology (NIST), an agency of the Federal Government and is - * being made available as a public service. Pursuant to title 17 United States - * Code Section 105, works of NIST employees are not subject to copyright - * protection in the United States. This software may be subject to foreign - * copyright. Permission in the United States and in foreign countries, to the - * extent that NIST may hold copyright, to use, copy, modify, create derivative - * works, and distribute this software and its documentation without fee is hereby - * granted on a non-exclusive basis, provided that this notice and disclaimer - * of warranty appears in all copies. - * - * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER - * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY - * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM - * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE - * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT - * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, - * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, - * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, - * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR - * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT - * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. - */ - -package gov.nist.secauto.metaschema.core.datatype.adapter; - -import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonFormatTypes; - -import gov.nist.secauto.metaschema.core.datatype.AbstractDataTypeAdapter; -import gov.nist.secauto.metaschema.core.metapath.item.atomic.IBase64BinaryItem; -import gov.nist.secauto.metaschema.core.util.ObjectUtils; - -import java.nio.ByteBuffer; -import java.util.Base64; -import java.util.List; - -import edu.umd.cs.findbugs.annotations.NonNull; - -public class Base64Adapter - extends AbstractDataTypeAdapter { - @NonNull - private static final List NAMES = ObjectUtils.notNull( - List.of( - "base64", - // for backwards compatibility with original type name - "base64Binary")); - - Base64Adapter() { - super(ByteBuffer.class); - } - - @Override - public List getNames() { - return NAMES; - } - - @Override - public JsonFormatTypes getJsonRawType() { - return JsonFormatTypes.STRING; - } - - @SuppressWarnings("null") - @Override - public ByteBuffer parse(String value) { - Base64.Decoder decoder = Base64.getDecoder(); - byte[] result = decoder.decode(value); - return ByteBuffer.wrap(result); - } - - @Override - public ByteBuffer copy(Object obj) { - ByteBuffer buffer = (ByteBuffer) obj; - final ByteBuffer clone - = buffer.isDirect() ? ByteBuffer.allocateDirect(buffer.capacity()) : ByteBuffer.allocate(buffer.capacity()); - final ByteBuffer readOnlyCopy = buffer.asReadOnlyBuffer(); - readOnlyCopy.flip(); - clone.put(readOnlyCopy); - return clone; - } - - @SuppressWarnings("null") - @Override - public String asString(Object value) { - Base64.Encoder encoder = Base64.getEncoder(); - return encoder.encodeToString(((ByteBuffer) value).array()); - } - - @Override - public Class getItemClass() { - return IBase64BinaryItem.class; - } - - @Override - public IBase64BinaryItem newItem(Object value) { - ByteBuffer item = toValue(value); - return IBase64BinaryItem.valueOf(item); - } - -} +/* + * Portions of this software was developed by employees of the National Institute + * of Standards and Technology (NIST), an agency of the Federal Government and is + * being made available as a public service. Pursuant to title 17 United States + * Code Section 105, works of NIST employees are not subject to copyright + * protection in the United States. This software may be subject to foreign + * copyright. Permission in the United States and in foreign countries, to the + * extent that NIST may hold copyright, to use, copy, modify, create derivative + * works, and distribute this software and its documentation without fee is hereby + * granted on a non-exclusive basis, provided that this notice and disclaimer + * of warranty appears in all copies. + * + * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER + * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY + * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM + * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE + * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT + * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, + * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, + * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, + * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR + * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT + * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. + */ + +package gov.nist.secauto.metaschema.core.datatype.adapter; + +import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonFormatTypes; + +import gov.nist.secauto.metaschema.core.datatype.AbstractDataTypeAdapter; +import gov.nist.secauto.metaschema.core.metapath.item.atomic.IBase64BinaryItem; +import gov.nist.secauto.metaschema.core.util.ObjectUtils; + +import java.nio.ByteBuffer; +import java.util.Base64; +import java.util.List; + +import edu.umd.cs.findbugs.annotations.NonNull; + +public class Base64Adapter + extends AbstractDataTypeAdapter { + @NonNull + private static final List NAMES = ObjectUtils.notNull( + List.of( + "base64", + // for backwards compatibility with original type name + "base64Binary")); + + Base64Adapter() { + super(ByteBuffer.class); + } + + @Override + public List getNames() { + return NAMES; + } + + @Override + public JsonFormatTypes getJsonRawType() { + return JsonFormatTypes.STRING; + } + + @SuppressWarnings("null") + @Override + public ByteBuffer parse(String value) { + Base64.Decoder decoder = Base64.getDecoder(); + byte[] result = decoder.decode(value); + return ByteBuffer.wrap(result); + } + + @Override + public ByteBuffer copy(Object obj) { + ByteBuffer buffer = (ByteBuffer) obj; + final ByteBuffer clone + = buffer.isDirect() ? ByteBuffer.allocateDirect(buffer.capacity()) : ByteBuffer.allocate(buffer.capacity()); + final ByteBuffer readOnlyCopy = buffer.asReadOnlyBuffer(); + readOnlyCopy.flip(); + clone.put(readOnlyCopy); + return clone; + } + + @SuppressWarnings("null") + @Override + public String asString(Object value) { + Base64.Encoder encoder = Base64.getEncoder(); + return encoder.encodeToString(((ByteBuffer) value).array()); + } + + @Override + public Class getItemClass() { + return IBase64BinaryItem.class; + } + + @Override + public IBase64BinaryItem newItem(Object value) { + ByteBuffer item = toValue(value); + return IBase64BinaryItem.valueOf(item); + } + +} diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/adapter/BooleanAdapter.java b/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/adapter/BooleanAdapter.java index 133f8f0dc..859185724 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/adapter/BooleanAdapter.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/adapter/BooleanAdapter.java @@ -1,156 +1,156 @@ -/* - * Portions of this software was developed by employees of the National Institute - * of Standards and Technology (NIST), an agency of the Federal Government and is - * being made available as a public service. Pursuant to title 17 United States - * Code Section 105, works of NIST employees are not subject to copyright - * protection in the United States. This software may be subject to foreign - * copyright. Permission in the United States and in foreign countries, to the - * extent that NIST may hold copyright, to use, copy, modify, create derivative - * works, and distribute this software and its documentation without fee is hereby - * granted on a non-exclusive basis, provided that this notice and disclaimer - * of warranty appears in all copies. - * - * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER - * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY - * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM - * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE - * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT - * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, - * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, - * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, - * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR - * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT - * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. - */ - -package gov.nist.secauto.metaschema.core.datatype.adapter; - -import com.fasterxml.jackson.core.JsonGenerator; -import com.fasterxml.jackson.core.JsonParser; -import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonFormatTypes; - -import gov.nist.secauto.metaschema.core.datatype.AbstractDataTypeAdapter; -import gov.nist.secauto.metaschema.core.metapath.function.InvalidValueForCastFunctionException; -import gov.nist.secauto.metaschema.core.metapath.item.atomic.IAnyAtomicItem; -import gov.nist.secauto.metaschema.core.metapath.item.atomic.IBooleanItem; -import gov.nist.secauto.metaschema.core.metapath.item.atomic.INumericItem; -import gov.nist.secauto.metaschema.core.metapath.item.atomic.IStringItem; -import gov.nist.secauto.metaschema.core.util.ObjectUtils; - -import java.io.IOException; -import java.util.List; - -import edu.umd.cs.findbugs.annotations.NonNull; - -public class BooleanAdapter - extends AbstractDataTypeAdapter { - @NonNull - private static final List NAMES = ObjectUtils.notNull( - List.of("boolean")); - - BooleanAdapter() { - super(Boolean.class); - } - - @Override - public List getNames() { - return NAMES; - } - - @Override - public JsonFormatTypes getJsonRawType() { - return JsonFormatTypes.BOOLEAN; - } - - @SuppressWarnings("null") - @Override - public Boolean parse(String value) { - return Boolean.valueOf(value); - } - - @Override - public Boolean parse(JsonParser parser) throws IOException { - Boolean value = parser.getBooleanValue(); - // skip over value - parser.nextToken(); - return value; - } - - @Override - public void writeJsonValue(Object value, JsonGenerator generator) - throws IOException { - try { - generator.writeBoolean((Boolean) value); - } catch (ClassCastException ex) { - throw new IOException(ex); - } - } - - @Override - public Boolean copy(Object obj) { - // the value is immutable - return (Boolean) obj; - } - - @Override - public Class getItemClass() { - return IBooleanItem.class; - } - - @Override - public IBooleanItem newItem(Object value) { - Boolean item = toValue(value); - return IBooleanItem.valueOf(item); - } - - @Override - protected IBooleanItem castInternal(@NonNull IAnyAtomicItem item) { - IBooleanItem retval; - if (item instanceof INumericItem) { - retval = castToBoolean((INumericItem) item); - } else if (item instanceof IStringItem) { - retval = castToBoolean((IStringItem) item); - } else { - retval = castToBoolean(item.asStringItem()); - } - return retval; - } - - /** - * Cast the provided numeric value to a boolean. Any non-zero value will be - * {@code true}, or {@code false} otherwise. - * - * @param item - * the item to cast - * @return {@code true} if the item value is non-zero, or {@code false} - * otherwise - */ - @NonNull - protected IBooleanItem castToBoolean(@NonNull INumericItem item) { - return IBooleanItem.valueOf(item.toEffectiveBoolean()); - } - - /** - * If the string is a numeric value, treat it as so. Otherwise parse the value - * as a boolean string. - * - * @param item - * the item to cast - * @return the effective boolean value of the string - * @throws InvalidValueForCastFunctionException - * if the provided item cannot be cast to a boolean value by any means - */ - @NonNull - protected IBooleanItem castToBoolean(@NonNull IStringItem item) { - IBooleanItem retval; - try { - INumericItem numeric = INumericItem.cast(item); - retval = castToBoolean(numeric); - } catch (InvalidValueForCastFunctionException ex) { - retval = super.castInternal(item); - } - return retval; - } - -} +/* + * Portions of this software was developed by employees of the National Institute + * of Standards and Technology (NIST), an agency of the Federal Government and is + * being made available as a public service. Pursuant to title 17 United States + * Code Section 105, works of NIST employees are not subject to copyright + * protection in the United States. This software may be subject to foreign + * copyright. Permission in the United States and in foreign countries, to the + * extent that NIST may hold copyright, to use, copy, modify, create derivative + * works, and distribute this software and its documentation without fee is hereby + * granted on a non-exclusive basis, provided that this notice and disclaimer + * of warranty appears in all copies. + * + * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER + * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY + * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM + * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE + * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT + * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, + * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, + * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, + * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR + * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT + * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. + */ + +package gov.nist.secauto.metaschema.core.datatype.adapter; + +import com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonFormatTypes; + +import gov.nist.secauto.metaschema.core.datatype.AbstractDataTypeAdapter; +import gov.nist.secauto.metaschema.core.metapath.function.InvalidValueForCastFunctionException; +import gov.nist.secauto.metaschema.core.metapath.item.atomic.IAnyAtomicItem; +import gov.nist.secauto.metaschema.core.metapath.item.atomic.IBooleanItem; +import gov.nist.secauto.metaschema.core.metapath.item.atomic.INumericItem; +import gov.nist.secauto.metaschema.core.metapath.item.atomic.IStringItem; +import gov.nist.secauto.metaschema.core.util.ObjectUtils; + +import java.io.IOException; +import java.util.List; + +import edu.umd.cs.findbugs.annotations.NonNull; + +public class BooleanAdapter + extends AbstractDataTypeAdapter { + @NonNull + private static final List NAMES = ObjectUtils.notNull( + List.of("boolean")); + + BooleanAdapter() { + super(Boolean.class); + } + + @Override + public List getNames() { + return NAMES; + } + + @Override + public JsonFormatTypes getJsonRawType() { + return JsonFormatTypes.BOOLEAN; + } + + @SuppressWarnings("null") + @Override + public Boolean parse(String value) { + return Boolean.valueOf(value); + } + + @Override + public Boolean parse(JsonParser parser) throws IOException { + Boolean value = parser.getBooleanValue(); + // skip over value + parser.nextToken(); + return value; + } + + @Override + public void writeJsonValue(Object value, JsonGenerator generator) + throws IOException { + try { + generator.writeBoolean((Boolean) value); + } catch (ClassCastException ex) { + throw new IOException(ex); + } + } + + @Override + public Boolean copy(Object obj) { + // the value is immutable + return (Boolean) obj; + } + + @Override + public Class getItemClass() { + return IBooleanItem.class; + } + + @Override + public IBooleanItem newItem(Object value) { + Boolean item = toValue(value); + return IBooleanItem.valueOf(item); + } + + @Override + protected IBooleanItem castInternal(@NonNull IAnyAtomicItem item) { + IBooleanItem retval; + if (item instanceof INumericItem) { + retval = castToBoolean((INumericItem) item); + } else if (item instanceof IStringItem) { + retval = castToBoolean((IStringItem) item); + } else { + retval = castToBoolean(item.asStringItem()); + } + return retval; + } + + /** + * Cast the provided numeric value to a boolean. Any non-zero value will be + * {@code true}, or {@code false} otherwise. + * + * @param item + * the item to cast + * @return {@code true} if the item value is non-zero, or {@code false} + * otherwise + */ + @NonNull + protected IBooleanItem castToBoolean(@NonNull INumericItem item) { + return IBooleanItem.valueOf(item.toEffectiveBoolean()); + } + + /** + * If the string is a numeric value, treat it as so. Otherwise parse the value + * as a boolean string. + * + * @param item + * the item to cast + * @return the effective boolean value of the string + * @throws InvalidValueForCastFunctionException + * if the provided item cannot be cast to a boolean value by any means + */ + @NonNull + protected IBooleanItem castToBoolean(@NonNull IStringItem item) { + IBooleanItem retval; + try { + INumericItem numeric = INumericItem.cast(item); + retval = castToBoolean(numeric); + } catch (InvalidValueForCastFunctionException ex) { + retval = super.castInternal(item); + } + return retval; + } + +} diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/adapter/DateAdapter.java b/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/adapter/DateAdapter.java index 349431e9f..874ae7003 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/adapter/DateAdapter.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/adapter/DateAdapter.java @@ -1,147 +1,147 @@ -/* - * Portions of this software was developed by employees of the National Institute - * of Standards and Technology (NIST), an agency of the Federal Government and is - * being made available as a public service. Pursuant to title 17 United States - * Code Section 105, works of NIST employees are not subject to copyright - * protection in the United States. This software may be subject to foreign - * copyright. Permission in the United States and in foreign countries, to the - * extent that NIST may hold copyright, to use, copy, modify, create derivative - * works, and distribute this software and its documentation without fee is hereby - * granted on a non-exclusive basis, provided that this notice and disclaimer - * of warranty appears in all copies. - * - * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER - * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY - * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM - * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE - * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT - * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, - * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, - * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, - * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR - * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT - * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. - */ - -package gov.nist.secauto.metaschema.core.datatype.adapter; - -import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonFormatTypes; - -import gov.nist.secauto.metaschema.core.datatype.AbstractCustomJavaDataTypeAdapter; -import gov.nist.secauto.metaschema.core.datatype.object.Date; -import gov.nist.secauto.metaschema.core.metapath.function.InvalidValueForCastFunctionException; -import gov.nist.secauto.metaschema.core.metapath.item.atomic.IAnyAtomicItem; -import gov.nist.secauto.metaschema.core.metapath.item.atomic.IDateItem; -import gov.nist.secauto.metaschema.core.metapath.item.atomic.IDateTimeItem; -import gov.nist.secauto.metaschema.core.metapath.item.atomic.IStringItem; -import gov.nist.secauto.metaschema.core.metapath.item.atomic.IUntypedAtomicItem; -import gov.nist.secauto.metaschema.core.util.ObjectUtils; - -import java.time.LocalDate; -import java.time.LocalTime; -import java.time.ZoneOffset; -import java.time.ZonedDateTime; -import java.time.format.DateTimeParseException; -import java.time.temporal.TemporalAccessor; -import java.util.List; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -import edu.umd.cs.findbugs.annotations.NonNull; - -public class DateAdapter - extends AbstractCustomJavaDataTypeAdapter { - @NonNull - private static final List NAMES = ObjectUtils.notNull( - List.of("date")); - private static final Pattern DATE_TIMEZONE = Pattern.compile("^(" - + "^(?:(?:2000|2400|2800|(?:19|2[0-9](?:0[48]|[2468][048]|[13579][26])))-02-29)" - + "|(?:(?:(?:19|2[0-9])[0-9]{2})-02-(?:0[1-9]|1[0-9]|2[0-8]))" - + "|(?:(?:(?:19|2[0-9])[0-9]{2})-(?:0[13578]|10|12)-(?:0[1-9]|[12][0-9]|3[01]))" - + "|(?:(?:(?:19|2[0-9])[0-9]{2})-(?:0[469]|11)-(?:0[1-9]|[12][0-9]|30))" - + ")" - + "(Z|[+-][0-9]{2}:[0-9]{2})?$"); - - DateAdapter() { - super(Date.class); - } - - @Override - public List getNames() { - return NAMES; - } - - @Override - public JsonFormatTypes getJsonRawType() { - return JsonFormatTypes.STRING; - } - - @Override - public Date parse(String value) { - Matcher matcher = DATE_TIMEZONE.matcher(value); - if (!matcher.matches()) { - throw new IllegalArgumentException("Invalid date: " + value); - } - - String parseValue - = String.format("%sT00:00:00%s", matcher.group(1), matcher.group(2) == null ? "" : matcher.group(2)); - try { - TemporalAccessor accessor = DateFormats.DATE_TIME_WITH_TZ.parse(parseValue); - return new Date(ObjectUtils.notNull(ZonedDateTime.from(accessor)), true); // NOPMD - readability - } catch (DateTimeParseException ex) { - try { - TemporalAccessor accessor = DateFormats.DATE_TIME_WITHOUT_TZ.parse(parseValue); - LocalDate date = LocalDate.from(accessor); - return new Date(ObjectUtils.notNull(ZonedDateTime.of(date, LocalTime.MIN, ZoneOffset.UTC)), false); - } catch (DateTimeParseException ex2) { - IllegalArgumentException newEx = new IllegalArgumentException(ex2.getLocalizedMessage(), ex2); - newEx.addSuppressed(ex); - throw newEx; // NOPMD - false positive - } - } - } - - @Override - public String asString(Object obj) { - Date value = (Date) obj; - String retval; - if (value.hasTimeZone()) { - @SuppressWarnings("null") - @NonNull String formatted = DateFormats.DATE_WITH_TZ.format(value.getValue()); - retval = formatted; - } else { - @SuppressWarnings("null") - @NonNull String formatted = DateFormats.DATE_WITHOUT_TZ.format(value.getValue()); - retval = formatted; - } - return retval; - } - - @Override - public Class getItemClass() { - return IDateItem.class; - } - - @Override - public IDateItem newItem(Object value) { - Date item = toValue(value); - return IDateItem.valueOf(item); - } - - @Override - protected @NonNull IDateItem castInternal(@NonNull IAnyAtomicItem item) { - IDateItem retval; - if (item instanceof IDateTimeItem) { - ZonedDateTime value = ((IDateTimeItem) item).asZonedDateTime(); - retval = IDateItem.valueOf(value); - } else if (item instanceof IStringItem || item instanceof IUntypedAtomicItem) { - retval = super.castInternal(item); - } else { - throw new InvalidValueForCastFunctionException( - String.format("unsupported item type '%s'", item.getClass().getName())); - } - return retval; - } - -} +/* + * Portions of this software was developed by employees of the National Institute + * of Standards and Technology (NIST), an agency of the Federal Government and is + * being made available as a public service. Pursuant to title 17 United States + * Code Section 105, works of NIST employees are not subject to copyright + * protection in the United States. This software may be subject to foreign + * copyright. Permission in the United States and in foreign countries, to the + * extent that NIST may hold copyright, to use, copy, modify, create derivative + * works, and distribute this software and its documentation without fee is hereby + * granted on a non-exclusive basis, provided that this notice and disclaimer + * of warranty appears in all copies. + * + * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER + * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY + * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM + * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE + * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT + * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, + * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, + * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, + * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR + * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT + * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. + */ + +package gov.nist.secauto.metaschema.core.datatype.adapter; + +import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonFormatTypes; + +import gov.nist.secauto.metaschema.core.datatype.AbstractCustomJavaDataTypeAdapter; +import gov.nist.secauto.metaschema.core.datatype.object.Date; +import gov.nist.secauto.metaschema.core.metapath.function.InvalidValueForCastFunctionException; +import gov.nist.secauto.metaschema.core.metapath.item.atomic.IAnyAtomicItem; +import gov.nist.secauto.metaschema.core.metapath.item.atomic.IDateItem; +import gov.nist.secauto.metaschema.core.metapath.item.atomic.IDateTimeItem; +import gov.nist.secauto.metaschema.core.metapath.item.atomic.IStringItem; +import gov.nist.secauto.metaschema.core.metapath.item.atomic.IUntypedAtomicItem; +import gov.nist.secauto.metaschema.core.util.ObjectUtils; + +import java.time.LocalDate; +import java.time.LocalTime; +import java.time.ZoneOffset; +import java.time.ZonedDateTime; +import java.time.format.DateTimeParseException; +import java.time.temporal.TemporalAccessor; +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import edu.umd.cs.findbugs.annotations.NonNull; + +public class DateAdapter + extends AbstractCustomJavaDataTypeAdapter { + @NonNull + private static final List NAMES = ObjectUtils.notNull( + List.of("date")); + private static final Pattern DATE_TIMEZONE = Pattern.compile("^(" + + "^(?:(?:2000|2400|2800|(?:19|2[0-9](?:0[48]|[2468][048]|[13579][26])))-02-29)" + + "|(?:(?:(?:19|2[0-9])[0-9]{2})-02-(?:0[1-9]|1[0-9]|2[0-8]))" + + "|(?:(?:(?:19|2[0-9])[0-9]{2})-(?:0[13578]|10|12)-(?:0[1-9]|[12][0-9]|3[01]))" + + "|(?:(?:(?:19|2[0-9])[0-9]{2})-(?:0[469]|11)-(?:0[1-9]|[12][0-9]|30))" + + ")" + + "(Z|[+-][0-9]{2}:[0-9]{2})?$"); + + DateAdapter() { + super(Date.class); + } + + @Override + public List getNames() { + return NAMES; + } + + @Override + public JsonFormatTypes getJsonRawType() { + return JsonFormatTypes.STRING; + } + + @Override + public Date parse(String value) { + Matcher matcher = DATE_TIMEZONE.matcher(value); + if (!matcher.matches()) { + throw new IllegalArgumentException("Invalid date: " + value); + } + + String parseValue + = String.format("%sT00:00:00%s", matcher.group(1), matcher.group(2) == null ? "" : matcher.group(2)); + try { + TemporalAccessor accessor = DateFormats.DATE_TIME_WITH_TZ.parse(parseValue); + return new Date(ObjectUtils.notNull(ZonedDateTime.from(accessor)), true); // NOPMD - readability + } catch (DateTimeParseException ex) { + try { + TemporalAccessor accessor = DateFormats.DATE_TIME_WITHOUT_TZ.parse(parseValue); + LocalDate date = LocalDate.from(accessor); + return new Date(ObjectUtils.notNull(ZonedDateTime.of(date, LocalTime.MIN, ZoneOffset.UTC)), false); + } catch (DateTimeParseException ex2) { + IllegalArgumentException newEx = new IllegalArgumentException(ex2.getLocalizedMessage(), ex2); + newEx.addSuppressed(ex); + throw newEx; // NOPMD - false positive + } + } + } + + @Override + public String asString(Object obj) { + Date value = (Date) obj; + String retval; + if (value.hasTimeZone()) { + @SuppressWarnings("null") + @NonNull String formatted = DateFormats.DATE_WITH_TZ.format(value.getValue()); + retval = formatted; + } else { + @SuppressWarnings("null") + @NonNull String formatted = DateFormats.DATE_WITHOUT_TZ.format(value.getValue()); + retval = formatted; + } + return retval; + } + + @Override + public Class getItemClass() { + return IDateItem.class; + } + + @Override + public IDateItem newItem(Object value) { + Date item = toValue(value); + return IDateItem.valueOf(item); + } + + @Override + protected @NonNull IDateItem castInternal(@NonNull IAnyAtomicItem item) { + IDateItem retval; + if (item instanceof IDateTimeItem) { + ZonedDateTime value = ((IDateTimeItem) item).asZonedDateTime(); + retval = IDateItem.valueOf(value); + } else if (item instanceof IStringItem || item instanceof IUntypedAtomicItem) { + retval = super.castInternal(item); + } else { + throw new InvalidValueForCastFunctionException( + String.format("unsupported item type '%s'", item.getClass().getName())); + } + return retval; + } + +} diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/adapter/DateTimeAdapter.java b/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/adapter/DateTimeAdapter.java index 90ca1d9e9..d53061aed 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/adapter/DateTimeAdapter.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/adapter/DateTimeAdapter.java @@ -1,131 +1,131 @@ -/* - * Portions of this software was developed by employees of the National Institute - * of Standards and Technology (NIST), an agency of the Federal Government and is - * being made available as a public service. Pursuant to title 17 United States - * Code Section 105, works of NIST employees are not subject to copyright - * protection in the United States. This software may be subject to foreign - * copyright. Permission in the United States and in foreign countries, to the - * extent that NIST may hold copyright, to use, copy, modify, create derivative - * works, and distribute this software and its documentation without fee is hereby - * granted on a non-exclusive basis, provided that this notice and disclaimer - * of warranty appears in all copies. - * - * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER - * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY - * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM - * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE - * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT - * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, - * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, - * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, - * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR - * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT - * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. - */ - -package gov.nist.secauto.metaschema.core.datatype.adapter; - -import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonFormatTypes; - -import gov.nist.secauto.metaschema.core.datatype.AbstractCustomJavaDataTypeAdapter; -import gov.nist.secauto.metaschema.core.datatype.object.DateTime; -import gov.nist.secauto.metaschema.core.metapath.function.InvalidValueForCastFunctionException; -import gov.nist.secauto.metaschema.core.metapath.item.atomic.IAnyAtomicItem; -import gov.nist.secauto.metaschema.core.metapath.item.atomic.IDateItem; -import gov.nist.secauto.metaschema.core.metapath.item.atomic.IDateTimeItem; -import gov.nist.secauto.metaschema.core.metapath.item.atomic.IStringItem; -import gov.nist.secauto.metaschema.core.metapath.item.atomic.IUntypedAtomicItem; -import gov.nist.secauto.metaschema.core.util.ObjectUtils; - -import java.time.LocalDateTime; -import java.time.ZoneOffset; -import java.time.ZonedDateTime; -import java.time.format.DateTimeParseException; -import java.util.List; - -import edu.umd.cs.findbugs.annotations.NonNull; - -public class DateTimeAdapter - extends AbstractCustomJavaDataTypeAdapter { - @NonNull - private static final List NAMES = ObjectUtils.notNull( - List.of( - "date-time", - // for backwards compatibility with original type name - "dateTime")); - - DateTimeAdapter() { - super(DateTime.class); - } - - @Override - public List getNames() { - return NAMES; - } - - @Override - public JsonFormatTypes getJsonRawType() { - return JsonFormatTypes.STRING; - } - - @SuppressWarnings("null") - @Override - public DateTime parse(String value) { - try { - return new DateTime(ZonedDateTime.from(DateFormats.DATE_TIME_WITH_TZ.parse(value)), true); // NOPMD - readability - } catch (DateTimeParseException ex) { - try { - LocalDateTime dateTime = LocalDateTime.from(DateFormats.DATE_TIME_WITHOUT_TZ.parse(value)); - return new DateTime(ZonedDateTime.of(dateTime, ZoneOffset.UTC), false); - } catch (DateTimeParseException ex2) { - IllegalArgumentException newEx = new IllegalArgumentException(ex2.getLocalizedMessage(), ex2); - newEx.addSuppressed(ex); - throw newEx; // NOPMD - it's ok - } - } - } - - @Override - public String asString(Object obj) { - DateTime value = (DateTime) obj; - String retval; - if (value.hasTimeZone()) { - @SuppressWarnings("null") - @NonNull String formatted = DateFormats.DATE_TIME_WITH_TZ.format(value.getValue()); - retval = formatted; - } else { - @SuppressWarnings("null") - @NonNull String formatted = DateFormats.DATE_TIME_WITHOUT_TZ.format(value.getValue()); - retval = formatted; - } - return retval; - } - - @Override - public Class getItemClass() { - return IDateTimeItem.class; - } - - @Override - public IDateTimeItem newItem(Object value) { - DateTime item = toValue(value); - return IDateTimeItem.valueOf(item); - } - - @Override - protected IDateTimeItem castInternal(@NonNull IAnyAtomicItem item) { - // TODO: bring up to spec - IDateTimeItem retval; - if (item instanceof IDateItem) { - retval = IDateTimeItem.valueOf(((IDateItem) item).asZonedDateTime()); - } else if (item instanceof IStringItem || item instanceof IUntypedAtomicItem) { - retval = super.castInternal(item); - } else { - throw new InvalidValueForCastFunctionException( - String.format("unsupported item type '%s'", item.getClass().getName())); - } - return retval; - } - -} +/* + * Portions of this software was developed by employees of the National Institute + * of Standards and Technology (NIST), an agency of the Federal Government and is + * being made available as a public service. Pursuant to title 17 United States + * Code Section 105, works of NIST employees are not subject to copyright + * protection in the United States. This software may be subject to foreign + * copyright. Permission in the United States and in foreign countries, to the + * extent that NIST may hold copyright, to use, copy, modify, create derivative + * works, and distribute this software and its documentation without fee is hereby + * granted on a non-exclusive basis, provided that this notice and disclaimer + * of warranty appears in all copies. + * + * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER + * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY + * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM + * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE + * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT + * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, + * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, + * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, + * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR + * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT + * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. + */ + +package gov.nist.secauto.metaschema.core.datatype.adapter; + +import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonFormatTypes; + +import gov.nist.secauto.metaschema.core.datatype.AbstractCustomJavaDataTypeAdapter; +import gov.nist.secauto.metaschema.core.datatype.object.DateTime; +import gov.nist.secauto.metaschema.core.metapath.function.InvalidValueForCastFunctionException; +import gov.nist.secauto.metaschema.core.metapath.item.atomic.IAnyAtomicItem; +import gov.nist.secauto.metaschema.core.metapath.item.atomic.IDateItem; +import gov.nist.secauto.metaschema.core.metapath.item.atomic.IDateTimeItem; +import gov.nist.secauto.metaschema.core.metapath.item.atomic.IStringItem; +import gov.nist.secauto.metaschema.core.metapath.item.atomic.IUntypedAtomicItem; +import gov.nist.secauto.metaschema.core.util.ObjectUtils; + +import java.time.LocalDateTime; +import java.time.ZoneOffset; +import java.time.ZonedDateTime; +import java.time.format.DateTimeParseException; +import java.util.List; + +import edu.umd.cs.findbugs.annotations.NonNull; + +public class DateTimeAdapter + extends AbstractCustomJavaDataTypeAdapter { + @NonNull + private static final List NAMES = ObjectUtils.notNull( + List.of( + "date-time", + // for backwards compatibility with original type name + "dateTime")); + + DateTimeAdapter() { + super(DateTime.class); + } + + @Override + public List getNames() { + return NAMES; + } + + @Override + public JsonFormatTypes getJsonRawType() { + return JsonFormatTypes.STRING; + } + + @SuppressWarnings("null") + @Override + public DateTime parse(String value) { + try { + return new DateTime(ZonedDateTime.from(DateFormats.DATE_TIME_WITH_TZ.parse(value)), true); // NOPMD - readability + } catch (DateTimeParseException ex) { + try { + LocalDateTime dateTime = LocalDateTime.from(DateFormats.DATE_TIME_WITHOUT_TZ.parse(value)); + return new DateTime(ZonedDateTime.of(dateTime, ZoneOffset.UTC), false); + } catch (DateTimeParseException ex2) { + IllegalArgumentException newEx = new IllegalArgumentException(ex2.getLocalizedMessage(), ex2); + newEx.addSuppressed(ex); + throw newEx; // NOPMD - it's ok + } + } + } + + @Override + public String asString(Object obj) { + DateTime value = (DateTime) obj; + String retval; + if (value.hasTimeZone()) { + @SuppressWarnings("null") + @NonNull String formatted = DateFormats.DATE_TIME_WITH_TZ.format(value.getValue()); + retval = formatted; + } else { + @SuppressWarnings("null") + @NonNull String formatted = DateFormats.DATE_TIME_WITHOUT_TZ.format(value.getValue()); + retval = formatted; + } + return retval; + } + + @Override + public Class getItemClass() { + return IDateTimeItem.class; + } + + @Override + public IDateTimeItem newItem(Object value) { + DateTime item = toValue(value); + return IDateTimeItem.valueOf(item); + } + + @Override + protected IDateTimeItem castInternal(@NonNull IAnyAtomicItem item) { + // TODO: bring up to spec + IDateTimeItem retval; + if (item instanceof IDateItem) { + retval = IDateTimeItem.valueOf(((IDateItem) item).asZonedDateTime()); + } else if (item instanceof IStringItem || item instanceof IUntypedAtomicItem) { + retval = super.castInternal(item); + } else { + throw new InvalidValueForCastFunctionException( + String.format("unsupported item type '%s'", item.getClass().getName())); + } + return retval; + } + +} diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/adapter/DateTimeWithTZAdapter.java b/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/adapter/DateTimeWithTZAdapter.java index ea6c44537..bcac872a4 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/adapter/DateTimeWithTZAdapter.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/adapter/DateTimeWithTZAdapter.java @@ -1,96 +1,96 @@ -/* - * Portions of this software was developed by employees of the National Institute - * of Standards and Technology (NIST), an agency of the Federal Government and is - * being made available as a public service. Pursuant to title 17 United States - * Code Section 105, works of NIST employees are not subject to copyright - * protection in the United States. This software may be subject to foreign - * copyright. Permission in the United States and in foreign countries, to the - * extent that NIST may hold copyright, to use, copy, modify, create derivative - * works, and distribute this software and its documentation without fee is hereby - * granted on a non-exclusive basis, provided that this notice and disclaimer - * of warranty appears in all copies. - * - * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER - * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY - * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM - * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE - * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT - * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, - * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, - * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, - * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR - * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT - * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. - */ - -package gov.nist.secauto.metaschema.core.datatype.adapter; - -import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonFormatTypes; - -import gov.nist.secauto.metaschema.core.datatype.AbstractDataTypeAdapter; -import gov.nist.secauto.metaschema.core.metapath.item.atomic.IDateTimeItem; -import gov.nist.secauto.metaschema.core.util.ObjectUtils; - -import java.time.ZonedDateTime; -import java.time.format.DateTimeParseException; -import java.util.List; - -import edu.umd.cs.findbugs.annotations.NonNull; - -public class DateTimeWithTZAdapter - extends AbstractDataTypeAdapter { - @NonNull - private static final List NAMES = ObjectUtils.notNull( - List.of( - "date-time-with-timezone", - // for backwards compatibility with original type name - "dateTime-with-timezone")); - - DateTimeWithTZAdapter() { - super(ZonedDateTime.class); - } - - @Override - public List getNames() { - return NAMES; - } - - @Override - public JsonFormatTypes getJsonRawType() { - return JsonFormatTypes.STRING; - } - - @SuppressWarnings("null") - @Override - public ZonedDateTime parse(String value) { - try { - return ZonedDateTime.from(DateFormats.DATE_TIME_WITH_TZ.parse(value)); - } catch (DateTimeParseException ex) { - throw new IllegalArgumentException(ex.getLocalizedMessage(), ex); - } - } - - @SuppressWarnings("null") - @Override - public String asString(Object value) { - return DateFormats.DATE_TIME_WITH_TZ.format((ZonedDateTime) value); - } - - @SuppressWarnings("null") - @Override - public ZonedDateTime copy(Object obj) { - return ZonedDateTime.from((ZonedDateTime) obj); - } - - @Override - public Class getItemClass() { - return IDateTimeItem.class; - } - - @Override - public IDateTimeItem newItem(Object value) { - ZonedDateTime item = toValue(value); - return IDateTimeItem.valueOf(item); - } -} +/* + * Portions of this software was developed by employees of the National Institute + * of Standards and Technology (NIST), an agency of the Federal Government and is + * being made available as a public service. Pursuant to title 17 United States + * Code Section 105, works of NIST employees are not subject to copyright + * protection in the United States. This software may be subject to foreign + * copyright. Permission in the United States and in foreign countries, to the + * extent that NIST may hold copyright, to use, copy, modify, create derivative + * works, and distribute this software and its documentation without fee is hereby + * granted on a non-exclusive basis, provided that this notice and disclaimer + * of warranty appears in all copies. + * + * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER + * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY + * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM + * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE + * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT + * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, + * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, + * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, + * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR + * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT + * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. + */ + +package gov.nist.secauto.metaschema.core.datatype.adapter; + +import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonFormatTypes; + +import gov.nist.secauto.metaschema.core.datatype.AbstractDataTypeAdapter; +import gov.nist.secauto.metaschema.core.metapath.item.atomic.IDateTimeItem; +import gov.nist.secauto.metaschema.core.util.ObjectUtils; + +import java.time.ZonedDateTime; +import java.time.format.DateTimeParseException; +import java.util.List; + +import edu.umd.cs.findbugs.annotations.NonNull; + +public class DateTimeWithTZAdapter + extends AbstractDataTypeAdapter { + @NonNull + private static final List NAMES = ObjectUtils.notNull( + List.of( + "date-time-with-timezone", + // for backwards compatibility with original type name + "dateTime-with-timezone")); + + DateTimeWithTZAdapter() { + super(ZonedDateTime.class); + } + + @Override + public List getNames() { + return NAMES; + } + + @Override + public JsonFormatTypes getJsonRawType() { + return JsonFormatTypes.STRING; + } + + @SuppressWarnings("null") + @Override + public ZonedDateTime parse(String value) { + try { + return ZonedDateTime.from(DateFormats.DATE_TIME_WITH_TZ.parse(value)); + } catch (DateTimeParseException ex) { + throw new IllegalArgumentException(ex.getLocalizedMessage(), ex); + } + } + + @SuppressWarnings("null") + @Override + public String asString(Object value) { + return DateFormats.DATE_TIME_WITH_TZ.format((ZonedDateTime) value); + } + + @SuppressWarnings("null") + @Override + public ZonedDateTime copy(Object obj) { + return ZonedDateTime.from((ZonedDateTime) obj); + } + + @Override + public Class getItemClass() { + return IDateTimeItem.class; + } + + @Override + public IDateTimeItem newItem(Object value) { + ZonedDateTime item = toValue(value); + return IDateTimeItem.valueOf(item); + } +} diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/adapter/DateWithTZAdapter.java b/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/adapter/DateWithTZAdapter.java index f116e3c25..7b6c04fde 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/adapter/DateWithTZAdapter.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/adapter/DateWithTZAdapter.java @@ -1,108 +1,108 @@ -/* - * Portions of this software was developed by employees of the National Institute - * of Standards and Technology (NIST), an agency of the Federal Government and is - * being made available as a public service. Pursuant to title 17 United States - * Code Section 105, works of NIST employees are not subject to copyright - * protection in the United States. This software may be subject to foreign - * copyright. Permission in the United States and in foreign countries, to the - * extent that NIST may hold copyright, to use, copy, modify, create derivative - * works, and distribute this software and its documentation without fee is hereby - * granted on a non-exclusive basis, provided that this notice and disclaimer - * of warranty appears in all copies. - * - * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER - * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY - * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM - * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE - * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT - * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, - * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, - * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, - * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR - * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT - * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. - */ - -package gov.nist.secauto.metaschema.core.datatype.adapter; - -import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonFormatTypes; - -import gov.nist.secauto.metaschema.core.datatype.AbstractDataTypeAdapter; -import gov.nist.secauto.metaschema.core.metapath.item.atomic.IDateItem; -import gov.nist.secauto.metaschema.core.util.ObjectUtils; - -import java.time.ZonedDateTime; -import java.time.format.DateTimeParseException; -import java.util.List; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -import edu.umd.cs.findbugs.annotations.NonNull; - -public class DateWithTZAdapter - extends AbstractDataTypeAdapter { - @NonNull - private static final List NAMES = ObjectUtils.notNull( - List.of( - "date-with-timezone")); - private static final Pattern DATE_TIMEZONE = Pattern.compile("^(" - + "^(?:(?:2000|2400|2800|(?:19|2[0-9](?:0[48]|[2468][048]|[13579][26])))-02-29)" - + "|(?:(?:(?:19|2[0-9])[0-9]{2})-02-(?:0[1-9]|1[0-9]|2[0-8]))" - + "|(?:(?:(?:19|2[0-9])[0-9]{2})-(?:0[13578]|10|12)-(?:0[1-9]|[12][0-9]|3[01]))" - + "|(?:(?:(?:19|2[0-9])[0-9]{2})-(?:0[469]|11)-(?:0[1-9]|[12][0-9]|30))" - + ")" - + "(Z|[+-][0-9]{2}:[0-9]{2})$"); - - DateWithTZAdapter() { - super(ZonedDateTime.class); - } - - @Override - public List getNames() { - return NAMES; - } - - @Override - public JsonFormatTypes getJsonRawType() { - return JsonFormatTypes.STRING; - } - - @SuppressWarnings("null") - @Override - public ZonedDateTime parse(String value) { - Matcher matcher = DATE_TIMEZONE.matcher(value); - if (!matcher.matches()) { - throw new IllegalArgumentException("Invalid date: " + value); - } - String parseValue = String.format("%sT00:00:00%s", matcher.group(1), matcher.group(2)); - try { - return ZonedDateTime.from(DateFormats.DATE_TIME_WITH_TZ.parse(parseValue)); - } catch (DateTimeParseException ex) { - throw new IllegalArgumentException(ex.getLocalizedMessage(), ex); - } - } - - @SuppressWarnings("null") - @Override - public String asString(Object value) { - return DateFormats.DATE_WITH_TZ.format((ZonedDateTime) value); - } - - @SuppressWarnings("null") - @Override - public ZonedDateTime copy(Object obj) { - return ZonedDateTime.from((ZonedDateTime) obj); - } - - @Override - public Class getItemClass() { - return IDateItem.class; - } - - @Override - public IDateItem newItem(Object value) { - ZonedDateTime item = toValue(value); - return IDateItem.valueOf(item); - } -} +/* + * Portions of this software was developed by employees of the National Institute + * of Standards and Technology (NIST), an agency of the Federal Government and is + * being made available as a public service. Pursuant to title 17 United States + * Code Section 105, works of NIST employees are not subject to copyright + * protection in the United States. This software may be subject to foreign + * copyright. Permission in the United States and in foreign countries, to the + * extent that NIST may hold copyright, to use, copy, modify, create derivative + * works, and distribute this software and its documentation without fee is hereby + * granted on a non-exclusive basis, provided that this notice and disclaimer + * of warranty appears in all copies. + * + * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER + * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY + * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM + * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE + * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT + * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, + * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, + * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, + * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR + * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT + * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. + */ + +package gov.nist.secauto.metaschema.core.datatype.adapter; + +import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonFormatTypes; + +import gov.nist.secauto.metaschema.core.datatype.AbstractDataTypeAdapter; +import gov.nist.secauto.metaschema.core.metapath.item.atomic.IDateItem; +import gov.nist.secauto.metaschema.core.util.ObjectUtils; + +import java.time.ZonedDateTime; +import java.time.format.DateTimeParseException; +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import edu.umd.cs.findbugs.annotations.NonNull; + +public class DateWithTZAdapter + extends AbstractDataTypeAdapter { + @NonNull + private static final List NAMES = ObjectUtils.notNull( + List.of( + "date-with-timezone")); + private static final Pattern DATE_TIMEZONE = Pattern.compile("^(" + + "^(?:(?:2000|2400|2800|(?:19|2[0-9](?:0[48]|[2468][048]|[13579][26])))-02-29)" + + "|(?:(?:(?:19|2[0-9])[0-9]{2})-02-(?:0[1-9]|1[0-9]|2[0-8]))" + + "|(?:(?:(?:19|2[0-9])[0-9]{2})-(?:0[13578]|10|12)-(?:0[1-9]|[12][0-9]|3[01]))" + + "|(?:(?:(?:19|2[0-9])[0-9]{2})-(?:0[469]|11)-(?:0[1-9]|[12][0-9]|30))" + + ")" + + "(Z|[+-][0-9]{2}:[0-9]{2})$"); + + DateWithTZAdapter() { + super(ZonedDateTime.class); + } + + @Override + public List getNames() { + return NAMES; + } + + @Override + public JsonFormatTypes getJsonRawType() { + return JsonFormatTypes.STRING; + } + + @SuppressWarnings("null") + @Override + public ZonedDateTime parse(String value) { + Matcher matcher = DATE_TIMEZONE.matcher(value); + if (!matcher.matches()) { + throw new IllegalArgumentException("Invalid date: " + value); + } + String parseValue = String.format("%sT00:00:00%s", matcher.group(1), matcher.group(2)); + try { + return ZonedDateTime.from(DateFormats.DATE_TIME_WITH_TZ.parse(parseValue)); + } catch (DateTimeParseException ex) { + throw new IllegalArgumentException(ex.getLocalizedMessage(), ex); + } + } + + @SuppressWarnings("null") + @Override + public String asString(Object value) { + return DateFormats.DATE_WITH_TZ.format((ZonedDateTime) value); + } + + @SuppressWarnings("null") + @Override + public ZonedDateTime copy(Object obj) { + return ZonedDateTime.from((ZonedDateTime) obj); + } + + @Override + public Class getItemClass() { + return IDateItem.class; + } + + @Override + public IDateItem newItem(Object value) { + ZonedDateTime item = toValue(value); + return IDateItem.valueOf(item); + } +} diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/adapter/DecimalAdapter.java b/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/adapter/DecimalAdapter.java index 0754a7cfd..a6ce75d4c 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/adapter/DecimalAdapter.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/adapter/DecimalAdapter.java @@ -1,115 +1,115 @@ -/* - * Portions of this software was developed by employees of the National Institute - * of Standards and Technology (NIST), an agency of the Federal Government and is - * being made available as a public service. Pursuant to title 17 United States - * Code Section 105, works of NIST employees are not subject to copyright - * protection in the United States. This software may be subject to foreign - * copyright. Permission in the United States and in foreign countries, to the - * extent that NIST may hold copyright, to use, copy, modify, create derivative - * works, and distribute this software and its documentation without fee is hereby - * granted on a non-exclusive basis, provided that this notice and disclaimer - * of warranty appears in all copies. - * - * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER - * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY - * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM - * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE - * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT - * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, - * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, - * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, - * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR - * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT - * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. - */ - -package gov.nist.secauto.metaschema.core.datatype.adapter; - -import com.fasterxml.jackson.core.JsonGenerator; -import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonFormatTypes; - -import gov.nist.secauto.metaschema.core.datatype.AbstractDataTypeAdapter; -import gov.nist.secauto.metaschema.core.metapath.item.atomic.IAnyAtomicItem; -import gov.nist.secauto.metaschema.core.metapath.item.atomic.IBooleanItem; -import gov.nist.secauto.metaschema.core.metapath.item.atomic.IDecimalItem; -import gov.nist.secauto.metaschema.core.metapath.item.atomic.INumericItem; -import gov.nist.secauto.metaschema.core.util.ObjectUtils; - -import java.io.IOException; -import java.math.BigDecimal; -import java.math.MathContext; -import java.util.List; - -import edu.umd.cs.findbugs.annotations.NonNull; - -public class DecimalAdapter - extends AbstractDataTypeAdapter { - public static final MathContext MATH_CONTEXT = MathContext.DECIMAL64; - @NonNull - private static final BigDecimal DECIMAL_BOOLEAN_TRUE = new BigDecimal("1.0"); - @NonNull - private static final BigDecimal DECIMAL_BOOLEAN_FALSE = new BigDecimal("0.0"); - @NonNull - private static final List NAMES = ObjectUtils.notNull( - List.of("decimal")); - - DecimalAdapter() { - super(BigDecimal.class); - } - - @Override - public List getNames() { - return NAMES; - } - - @Override - public JsonFormatTypes getJsonRawType() { - return JsonFormatTypes.NUMBER; - } - - @Override - public BigDecimal parse(String value) { - return new BigDecimal(value, MATH_CONTEXT); - } - - @Override - public void writeJsonValue(Object value, JsonGenerator generator) throws IOException { - try { - generator.writeNumber((BigDecimal) value); - } catch (ClassCastException ex) { - throw new IOException(ex); - } - } - - @Override - public BigDecimal copy(Object obj) { - // a BigDecimal is immutable - return (BigDecimal) obj; - } - - @Override - public Class getItemClass() { - return IDecimalItem.class; - } - - @Override - public IDecimalItem newItem(Object value) { - BigDecimal item = toValue(value); - return IDecimalItem.valueOf(item); - } - - @Override - protected IDecimalItem castInternal(@NonNull IAnyAtomicItem item) { - IDecimalItem retval; - if (item instanceof INumericItem) { - retval = newItem(((INumericItem) item).asDecimal()); - } else if (item instanceof IBooleanItem) { - boolean value = ((IBooleanItem) item).toBoolean(); - retval = newItem(value ? DECIMAL_BOOLEAN_TRUE : DECIMAL_BOOLEAN_FALSE); - } else { - retval = super.castInternal(item); - } - return retval; - } -} +/* + * Portions of this software was developed by employees of the National Institute + * of Standards and Technology (NIST), an agency of the Federal Government and is + * being made available as a public service. Pursuant to title 17 United States + * Code Section 105, works of NIST employees are not subject to copyright + * protection in the United States. This software may be subject to foreign + * copyright. Permission in the United States and in foreign countries, to the + * extent that NIST may hold copyright, to use, copy, modify, create derivative + * works, and distribute this software and its documentation without fee is hereby + * granted on a non-exclusive basis, provided that this notice and disclaimer + * of warranty appears in all copies. + * + * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER + * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY + * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM + * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE + * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT + * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, + * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, + * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, + * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR + * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT + * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. + */ + +package gov.nist.secauto.metaschema.core.datatype.adapter; + +import com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonFormatTypes; + +import gov.nist.secauto.metaschema.core.datatype.AbstractDataTypeAdapter; +import gov.nist.secauto.metaschema.core.metapath.item.atomic.IAnyAtomicItem; +import gov.nist.secauto.metaschema.core.metapath.item.atomic.IBooleanItem; +import gov.nist.secauto.metaschema.core.metapath.item.atomic.IDecimalItem; +import gov.nist.secauto.metaschema.core.metapath.item.atomic.INumericItem; +import gov.nist.secauto.metaschema.core.util.ObjectUtils; + +import java.io.IOException; +import java.math.BigDecimal; +import java.math.MathContext; +import java.util.List; + +import edu.umd.cs.findbugs.annotations.NonNull; + +public class DecimalAdapter + extends AbstractDataTypeAdapter { + public static final MathContext MATH_CONTEXT = MathContext.DECIMAL64; + @NonNull + private static final BigDecimal DECIMAL_BOOLEAN_TRUE = new BigDecimal("1.0"); + @NonNull + private static final BigDecimal DECIMAL_BOOLEAN_FALSE = new BigDecimal("0.0"); + @NonNull + private static final List NAMES = ObjectUtils.notNull( + List.of("decimal")); + + DecimalAdapter() { + super(BigDecimal.class); + } + + @Override + public List getNames() { + return NAMES; + } + + @Override + public JsonFormatTypes getJsonRawType() { + return JsonFormatTypes.NUMBER; + } + + @Override + public BigDecimal parse(String value) { + return new BigDecimal(value, MATH_CONTEXT); + } + + @Override + public void writeJsonValue(Object value, JsonGenerator generator) throws IOException { + try { + generator.writeNumber((BigDecimal) value); + } catch (ClassCastException ex) { + throw new IOException(ex); + } + } + + @Override + public BigDecimal copy(Object obj) { + // a BigDecimal is immutable + return (BigDecimal) obj; + } + + @Override + public Class getItemClass() { + return IDecimalItem.class; + } + + @Override + public IDecimalItem newItem(Object value) { + BigDecimal item = toValue(value); + return IDecimalItem.valueOf(item); + } + + @Override + protected IDecimalItem castInternal(@NonNull IAnyAtomicItem item) { + IDecimalItem retval; + if (item instanceof INumericItem) { + retval = newItem(((INumericItem) item).asDecimal()); + } else if (item instanceof IBooleanItem) { + boolean value = ((IBooleanItem) item).toBoolean(); + retval = newItem(value ? DECIMAL_BOOLEAN_TRUE : DECIMAL_BOOLEAN_FALSE); + } else { + retval = super.castInternal(item); + } + return retval; + } +} diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/adapter/EmailAddressAdapter.java b/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/adapter/EmailAddressAdapter.java index 13b9b8657..8a7418177 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/adapter/EmailAddressAdapter.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/adapter/EmailAddressAdapter.java @@ -1,64 +1,64 @@ -/* - * Portions of this software was developed by employees of the National Institute - * of Standards and Technology (NIST), an agency of the Federal Government and is - * being made available as a public service. Pursuant to title 17 United States - * Code Section 105, works of NIST employees are not subject to copyright - * protection in the United States. This software may be subject to foreign - * copyright. Permission in the United States and in foreign countries, to the - * extent that NIST may hold copyright, to use, copy, modify, create derivative - * works, and distribute this software and its documentation without fee is hereby - * granted on a non-exclusive basis, provided that this notice and disclaimer - * of warranty appears in all copies. - * - * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER - * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY - * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM - * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE - * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT - * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, - * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, - * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, - * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR - * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT - * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. - */ - -package gov.nist.secauto.metaschema.core.datatype.adapter; - -import gov.nist.secauto.metaschema.core.metapath.item.atomic.IEmailAddressItem; -import gov.nist.secauto.metaschema.core.util.ObjectUtils; - -import java.util.List; - -import edu.umd.cs.findbugs.annotations.NonNull; - -public class EmailAddressAdapter - extends AbstractStringAdapter { - @NonNull - private static final List NAMES = ObjectUtils.notNull( - List.of( - "email-address", - // for backwards compatibility with original type name - "email")); - - EmailAddressAdapter() { - // avoid general construction - } - - @Override - public List getNames() { - return NAMES; - } - - @Override - public @NonNull Class getItemClass() { - return IEmailAddressItem.class; - } - - @Override - public IEmailAddressItem newItem(Object value) { - String item = asString(value); - return IEmailAddressItem.valueOf(item); - } -} +/* + * Portions of this software was developed by employees of the National Institute + * of Standards and Technology (NIST), an agency of the Federal Government and is + * being made available as a public service. Pursuant to title 17 United States + * Code Section 105, works of NIST employees are not subject to copyright + * protection in the United States. This software may be subject to foreign + * copyright. Permission in the United States and in foreign countries, to the + * extent that NIST may hold copyright, to use, copy, modify, create derivative + * works, and distribute this software and its documentation without fee is hereby + * granted on a non-exclusive basis, provided that this notice and disclaimer + * of warranty appears in all copies. + * + * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER + * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY + * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM + * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE + * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT + * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, + * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, + * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, + * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR + * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT + * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. + */ + +package gov.nist.secauto.metaschema.core.datatype.adapter; + +import gov.nist.secauto.metaschema.core.metapath.item.atomic.IEmailAddressItem; +import gov.nist.secauto.metaschema.core.util.ObjectUtils; + +import java.util.List; + +import edu.umd.cs.findbugs.annotations.NonNull; + +public class EmailAddressAdapter + extends AbstractStringAdapter { + @NonNull + private static final List NAMES = ObjectUtils.notNull( + List.of( + "email-address", + // for backwards compatibility with original type name + "email")); + + EmailAddressAdapter() { + // avoid general construction + } + + @Override + public List getNames() { + return NAMES; + } + + @Override + public @NonNull Class getItemClass() { + return IEmailAddressItem.class; + } + + @Override + public IEmailAddressItem newItem(Object value) { + String item = asString(value); + return IEmailAddressItem.valueOf(item); + } +} diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/adapter/HostnameAdapter.java b/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/adapter/HostnameAdapter.java index 30304b8aa..d01b372d0 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/adapter/HostnameAdapter.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/adapter/HostnameAdapter.java @@ -1,61 +1,61 @@ -/* - * Portions of this software was developed by employees of the National Institute - * of Standards and Technology (NIST), an agency of the Federal Government and is - * being made available as a public service. Pursuant to title 17 United States - * Code Section 105, works of NIST employees are not subject to copyright - * protection in the United States. This software may be subject to foreign - * copyright. Permission in the United States and in foreign countries, to the - * extent that NIST may hold copyright, to use, copy, modify, create derivative - * works, and distribute this software and its documentation without fee is hereby - * granted on a non-exclusive basis, provided that this notice and disclaimer - * of warranty appears in all copies. - * - * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER - * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY - * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM - * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE - * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT - * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, - * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, - * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, - * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR - * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT - * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. - */ - -package gov.nist.secauto.metaschema.core.datatype.adapter; - -import gov.nist.secauto.metaschema.core.metapath.item.atomic.IHostnameItem; -import gov.nist.secauto.metaschema.core.util.ObjectUtils; - -import java.util.List; - -import edu.umd.cs.findbugs.annotations.NonNull; - -public class HostnameAdapter - extends AbstractStringAdapter { - @NonNull - private static final List NAMES = ObjectUtils.notNull( - List.of("hostname")); - - HostnameAdapter() { - // avoid general construction - } - - @Override - public List getNames() { - return NAMES; - } - - @Override - public @NonNull Class getItemClass() { - return IHostnameItem.class; - } - - @Override - public IHostnameItem newItem(Object value) { - String item = asString(value); - return IHostnameItem.valueOf(item); - } -} +/* + * Portions of this software was developed by employees of the National Institute + * of Standards and Technology (NIST), an agency of the Federal Government and is + * being made available as a public service. Pursuant to title 17 United States + * Code Section 105, works of NIST employees are not subject to copyright + * protection in the United States. This software may be subject to foreign + * copyright. Permission in the United States and in foreign countries, to the + * extent that NIST may hold copyright, to use, copy, modify, create derivative + * works, and distribute this software and its documentation without fee is hereby + * granted on a non-exclusive basis, provided that this notice and disclaimer + * of warranty appears in all copies. + * + * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER + * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY + * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM + * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE + * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT + * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, + * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, + * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, + * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR + * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT + * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. + */ + +package gov.nist.secauto.metaschema.core.datatype.adapter; + +import gov.nist.secauto.metaschema.core.metapath.item.atomic.IHostnameItem; +import gov.nist.secauto.metaschema.core.util.ObjectUtils; + +import java.util.List; + +import edu.umd.cs.findbugs.annotations.NonNull; + +public class HostnameAdapter + extends AbstractStringAdapter { + @NonNull + private static final List NAMES = ObjectUtils.notNull( + List.of("hostname")); + + HostnameAdapter() { + // avoid general construction + } + + @Override + public List getNames() { + return NAMES; + } + + @Override + public @NonNull Class getItemClass() { + return IHostnameItem.class; + } + + @Override + public IHostnameItem newItem(Object value) { + String item = asString(value); + return IHostnameItem.valueOf(item); + } +} diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/adapter/IPv4AddressAdapter.java b/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/adapter/IPv4AddressAdapter.java index 5c6aeb585..f4e4f43b5 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/adapter/IPv4AddressAdapter.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/adapter/IPv4AddressAdapter.java @@ -1,97 +1,97 @@ -/* - * Portions of this software was developed by employees of the National Institute - * of Standards and Technology (NIST), an agency of the Federal Government and is - * being made available as a public service. Pursuant to title 17 United States - * Code Section 105, works of NIST employees are not subject to copyright - * protection in the United States. This software may be subject to foreign - * copyright. Permission in the United States and in foreign countries, to the - * extent that NIST may hold copyright, to use, copy, modify, create derivative - * works, and distribute this software and its documentation without fee is hereby - * granted on a non-exclusive basis, provided that this notice and disclaimer - * of warranty appears in all copies. - * - * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER - * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY - * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM - * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE - * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT - * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, - * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, - * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, - * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR - * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT - * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. - */ - -package gov.nist.secauto.metaschema.core.datatype.adapter; - -import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonFormatTypes; - -import gov.nist.secauto.metaschema.core.datatype.AbstractDataTypeAdapter; -import gov.nist.secauto.metaschema.core.metapath.item.atomic.IIPv4AddressItem; -import gov.nist.secauto.metaschema.core.util.ObjectUtils; - -import java.util.List; - -import edu.umd.cs.findbugs.annotations.NonNull; -import inet.ipaddr.AddressStringException; -import inet.ipaddr.IPAddressString; -import inet.ipaddr.IPAddressStringParameters; -import inet.ipaddr.IncompatibleAddressException; -import inet.ipaddr.ipv4.IPv4Address; - -public class IPv4AddressAdapter - extends AbstractDataTypeAdapter { - @NonNull - private static final List NAMES = ObjectUtils.notNull( - List.of("ip-v4-address")); - private static final IPAddressStringParameters IP_V_4; - - static { - IP_V_4 = new IPAddressStringParameters.Builder().allowIPv6(false).allowEmpty(false).allowSingleSegment(false) - .allowWildcardedSeparator(false).getIPv4AddressParametersBuilder().allowBinary(false).allowLeadingZeros(false) - .allowPrefixesBeyondAddressSize(false).getParentBuilder().toParams(); - } - - IPv4AddressAdapter() { - super(IPv4Address.class); - } - - @Override - public List getNames() { - return NAMES; - } - - @Override - public JsonFormatTypes getJsonRawType() { - return JsonFormatTypes.STRING; - } - - @SuppressWarnings("null") - @Override - public IPv4Address parse(String value) { - try { - return (IPv4Address) new IPAddressString(value, IP_V_4).toAddress(); - } catch (AddressStringException | IncompatibleAddressException ex) { - throw new IllegalArgumentException(ex.getLocalizedMessage(), ex); - } - } - - @Override - public IPv4Address copy(Object obj) { - // value is immutable - return (IPv4Address) obj; - } - - @Override - public Class getItemClass() { - return IIPv4AddressItem.class; - } - - @Override - public IIPv4AddressItem newItem(Object value) { - IPv4Address item = toValue(value); - return IIPv4AddressItem.valueOf(item); - } -} +/* + * Portions of this software was developed by employees of the National Institute + * of Standards and Technology (NIST), an agency of the Federal Government and is + * being made available as a public service. Pursuant to title 17 United States + * Code Section 105, works of NIST employees are not subject to copyright + * protection in the United States. This software may be subject to foreign + * copyright. Permission in the United States and in foreign countries, to the + * extent that NIST may hold copyright, to use, copy, modify, create derivative + * works, and distribute this software and its documentation without fee is hereby + * granted on a non-exclusive basis, provided that this notice and disclaimer + * of warranty appears in all copies. + * + * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER + * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY + * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM + * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE + * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT + * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, + * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, + * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, + * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR + * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT + * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. + */ + +package gov.nist.secauto.metaschema.core.datatype.adapter; + +import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonFormatTypes; + +import gov.nist.secauto.metaschema.core.datatype.AbstractDataTypeAdapter; +import gov.nist.secauto.metaschema.core.metapath.item.atomic.IIPv4AddressItem; +import gov.nist.secauto.metaschema.core.util.ObjectUtils; + +import java.util.List; + +import edu.umd.cs.findbugs.annotations.NonNull; +import inet.ipaddr.AddressStringException; +import inet.ipaddr.IPAddressString; +import inet.ipaddr.IPAddressStringParameters; +import inet.ipaddr.IncompatibleAddressException; +import inet.ipaddr.ipv4.IPv4Address; + +public class IPv4AddressAdapter + extends AbstractDataTypeAdapter { + @NonNull + private static final List NAMES = ObjectUtils.notNull( + List.of("ip-v4-address")); + private static final IPAddressStringParameters IP_V_4; + + static { + IP_V_4 = new IPAddressStringParameters.Builder().allowIPv6(false).allowEmpty(false).allowSingleSegment(false) + .allowWildcardedSeparator(false).getIPv4AddressParametersBuilder().allowBinary(false).allowLeadingZeros(false) + .allowPrefixesBeyondAddressSize(false).getParentBuilder().toParams(); + } + + IPv4AddressAdapter() { + super(IPv4Address.class); + } + + @Override + public List getNames() { + return NAMES; + } + + @Override + public JsonFormatTypes getJsonRawType() { + return JsonFormatTypes.STRING; + } + + @SuppressWarnings("null") + @Override + public IPv4Address parse(String value) { + try { + return (IPv4Address) new IPAddressString(value, IP_V_4).toAddress(); + } catch (AddressStringException | IncompatibleAddressException ex) { + throw new IllegalArgumentException(ex.getLocalizedMessage(), ex); + } + } + + @Override + public IPv4Address copy(Object obj) { + // value is immutable + return (IPv4Address) obj; + } + + @Override + public Class getItemClass() { + return IIPv4AddressItem.class; + } + + @Override + public IIPv4AddressItem newItem(Object value) { + IPv4Address item = toValue(value); + return IIPv4AddressItem.valueOf(item); + } +} diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/adapter/IPv6AddressAdapter.java b/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/adapter/IPv6AddressAdapter.java index 05ba1bbca..6d5aa7fae 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/adapter/IPv6AddressAdapter.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/adapter/IPv6AddressAdapter.java @@ -1,97 +1,97 @@ -/* - * Portions of this software was developed by employees of the National Institute - * of Standards and Technology (NIST), an agency of the Federal Government and is - * being made available as a public service. Pursuant to title 17 United States - * Code Section 105, works of NIST employees are not subject to copyright - * protection in the United States. This software may be subject to foreign - * copyright. Permission in the United States and in foreign countries, to the - * extent that NIST may hold copyright, to use, copy, modify, create derivative - * works, and distribute this software and its documentation without fee is hereby - * granted on a non-exclusive basis, provided that this notice and disclaimer - * of warranty appears in all copies. - * - * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER - * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY - * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM - * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE - * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT - * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, - * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, - * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, - * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR - * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT - * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. - */ - -package gov.nist.secauto.metaschema.core.datatype.adapter; - -import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonFormatTypes; - -import gov.nist.secauto.metaschema.core.datatype.AbstractDataTypeAdapter; -import gov.nist.secauto.metaschema.core.metapath.item.atomic.IIPv6AddressItem; -import gov.nist.secauto.metaschema.core.util.ObjectUtils; - -import java.util.List; - -import edu.umd.cs.findbugs.annotations.NonNull; -import inet.ipaddr.AddressStringException; -import inet.ipaddr.IPAddressString; -import inet.ipaddr.IPAddressStringParameters; -import inet.ipaddr.IncompatibleAddressException; -import inet.ipaddr.ipv6.IPv6Address; - -public class IPv6AddressAdapter - extends AbstractDataTypeAdapter { - @NonNull - private static final List NAMES = ObjectUtils.notNull( - List.of("ip-v6-address")); - private static final IPAddressStringParameters IP_V_6; - - static { - IP_V_6 = new IPAddressStringParameters.Builder().allowIPv4(false).allowEmpty(false).allowSingleSegment(false) - .allowWildcardedSeparator(false).getIPv6AddressParametersBuilder().allowBinary(false) - .allowPrefixesBeyondAddressSize(false).getParentBuilder().toParams(); - } - - IPv6AddressAdapter() { - super(IPv6Address.class); - } - - @Override - public List getNames() { - return NAMES; - } - - @Override - public JsonFormatTypes getJsonRawType() { - return JsonFormatTypes.STRING; - } - - @SuppressWarnings("null") - @Override - public IPv6Address parse(String value) { - try { - return (IPv6Address) new IPAddressString(value, IP_V_6).toAddress(); - } catch (AddressStringException | IncompatibleAddressException ex) { - throw new IllegalArgumentException(ex.getLocalizedMessage(), ex); - } - } - - @Override - public IPv6Address copy(Object obj) { - // value is immutable - return (IPv6Address) obj; - } - - @Override - public Class getItemClass() { - return IIPv6AddressItem.class; - } - - @Override - public IIPv6AddressItem newItem(Object value) { - IPv6Address item = toValue(value); - return IIPv6AddressItem.valueOf(item); - } -} +/* + * Portions of this software was developed by employees of the National Institute + * of Standards and Technology (NIST), an agency of the Federal Government and is + * being made available as a public service. Pursuant to title 17 United States + * Code Section 105, works of NIST employees are not subject to copyright + * protection in the United States. This software may be subject to foreign + * copyright. Permission in the United States and in foreign countries, to the + * extent that NIST may hold copyright, to use, copy, modify, create derivative + * works, and distribute this software and its documentation without fee is hereby + * granted on a non-exclusive basis, provided that this notice and disclaimer + * of warranty appears in all copies. + * + * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER + * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY + * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM + * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE + * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT + * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, + * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, + * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, + * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR + * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT + * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. + */ + +package gov.nist.secauto.metaschema.core.datatype.adapter; + +import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonFormatTypes; + +import gov.nist.secauto.metaschema.core.datatype.AbstractDataTypeAdapter; +import gov.nist.secauto.metaschema.core.metapath.item.atomic.IIPv6AddressItem; +import gov.nist.secauto.metaschema.core.util.ObjectUtils; + +import java.util.List; + +import edu.umd.cs.findbugs.annotations.NonNull; +import inet.ipaddr.AddressStringException; +import inet.ipaddr.IPAddressString; +import inet.ipaddr.IPAddressStringParameters; +import inet.ipaddr.IncompatibleAddressException; +import inet.ipaddr.ipv6.IPv6Address; + +public class IPv6AddressAdapter + extends AbstractDataTypeAdapter { + @NonNull + private static final List NAMES = ObjectUtils.notNull( + List.of("ip-v6-address")); + private static final IPAddressStringParameters IP_V_6; + + static { + IP_V_6 = new IPAddressStringParameters.Builder().allowIPv4(false).allowEmpty(false).allowSingleSegment(false) + .allowWildcardedSeparator(false).getIPv6AddressParametersBuilder().allowBinary(false) + .allowPrefixesBeyondAddressSize(false).getParentBuilder().toParams(); + } + + IPv6AddressAdapter() { + super(IPv6Address.class); + } + + @Override + public List getNames() { + return NAMES; + } + + @Override + public JsonFormatTypes getJsonRawType() { + return JsonFormatTypes.STRING; + } + + @SuppressWarnings("null") + @Override + public IPv6Address parse(String value) { + try { + return (IPv6Address) new IPAddressString(value, IP_V_6).toAddress(); + } catch (AddressStringException | IncompatibleAddressException ex) { + throw new IllegalArgumentException(ex.getLocalizedMessage(), ex); + } + } + + @Override + public IPv6Address copy(Object obj) { + // value is immutable + return (IPv6Address) obj; + } + + @Override + public Class getItemClass() { + return IIPv6AddressItem.class; + } + + @Override + public IIPv6AddressItem newItem(Object value) { + IPv6Address item = toValue(value); + return IIPv6AddressItem.valueOf(item); + } +} diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/adapter/IntegerAdapter.java b/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/adapter/IntegerAdapter.java index 2e0684092..31ff2292c 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/adapter/IntegerAdapter.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/adapter/IntegerAdapter.java @@ -1,79 +1,79 @@ -/* - * Portions of this software was developed by employees of the National Institute - * of Standards and Technology (NIST), an agency of the Federal Government and is - * being made available as a public service. Pursuant to title 17 United States - * Code Section 105, works of NIST employees are not subject to copyright - * protection in the United States. This software may be subject to foreign - * copyright. Permission in the United States and in foreign countries, to the - * extent that NIST may hold copyright, to use, copy, modify, create derivative - * works, and distribute this software and its documentation without fee is hereby - * granted on a non-exclusive basis, provided that this notice and disclaimer - * of warranty appears in all copies. - * - * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER - * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY - * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM - * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE - * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT - * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, - * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, - * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, - * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR - * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT - * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. - */ - -package gov.nist.secauto.metaschema.core.datatype.adapter; - -import gov.nist.secauto.metaschema.core.metapath.item.atomic.IAnyAtomicItem; -import gov.nist.secauto.metaschema.core.metapath.item.atomic.IBooleanItem; -import gov.nist.secauto.metaschema.core.metapath.item.atomic.IIntegerItem; -import gov.nist.secauto.metaschema.core.metapath.item.atomic.INumericItem; -import gov.nist.secauto.metaschema.core.util.ObjectUtils; - -import java.math.BigInteger; -import java.util.List; - -import edu.umd.cs.findbugs.annotations.NonNull; - -public class IntegerAdapter - extends AbstractIntegerAdapter { - @NonNull - private static final List NAMES = ObjectUtils.notNull( - List.of("integer")); - - IntegerAdapter() { - // avoid general construction - } - - @Override - public List getNames() { - return NAMES; - } - - @Override - public @NonNull Class getItemClass() { - return IIntegerItem.class; - } - - @Override - public IIntegerItem newItem(Object value) { - BigInteger item = toValue(value); - return IIntegerItem.valueOf(item); - } - - @Override - protected IIntegerItem castInternal(@NonNull IAnyAtomicItem item) { - IIntegerItem retval; - if (item instanceof INumericItem) { - retval = newItem(((INumericItem) item).asInteger()); - } else if (item instanceof IBooleanItem) { - boolean value = ((IBooleanItem) item).toBoolean(); - retval = newItem(ObjectUtils.notNull(value ? BigInteger.ONE : BigInteger.ZERO)); - } else { - retval = super.castInternal(item); - } - return retval; - } -} +/* + * Portions of this software was developed by employees of the National Institute + * of Standards and Technology (NIST), an agency of the Federal Government and is + * being made available as a public service. Pursuant to title 17 United States + * Code Section 105, works of NIST employees are not subject to copyright + * protection in the United States. This software may be subject to foreign + * copyright. Permission in the United States and in foreign countries, to the + * extent that NIST may hold copyright, to use, copy, modify, create derivative + * works, and distribute this software and its documentation without fee is hereby + * granted on a non-exclusive basis, provided that this notice and disclaimer + * of warranty appears in all copies. + * + * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER + * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY + * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM + * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE + * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT + * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, + * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, + * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, + * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR + * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT + * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. + */ + +package gov.nist.secauto.metaschema.core.datatype.adapter; + +import gov.nist.secauto.metaschema.core.metapath.item.atomic.IAnyAtomicItem; +import gov.nist.secauto.metaschema.core.metapath.item.atomic.IBooleanItem; +import gov.nist.secauto.metaschema.core.metapath.item.atomic.IIntegerItem; +import gov.nist.secauto.metaschema.core.metapath.item.atomic.INumericItem; +import gov.nist.secauto.metaschema.core.util.ObjectUtils; + +import java.math.BigInteger; +import java.util.List; + +import edu.umd.cs.findbugs.annotations.NonNull; + +public class IntegerAdapter + extends AbstractIntegerAdapter { + @NonNull + private static final List NAMES = ObjectUtils.notNull( + List.of("integer")); + + IntegerAdapter() { + // avoid general construction + } + + @Override + public List getNames() { + return NAMES; + } + + @Override + public @NonNull Class getItemClass() { + return IIntegerItem.class; + } + + @Override + public IIntegerItem newItem(Object value) { + BigInteger item = toValue(value); + return IIntegerItem.valueOf(item); + } + + @Override + protected IIntegerItem castInternal(@NonNull IAnyAtomicItem item) { + IIntegerItem retval; + if (item instanceof INumericItem) { + retval = newItem(((INumericItem) item).asInteger()); + } else if (item instanceof IBooleanItem) { + boolean value = ((IBooleanItem) item).toBoolean(); + retval = newItem(ObjectUtils.notNull(value ? BigInteger.ONE : BigInteger.ZERO)); + } else { + retval = super.castInternal(item); + } + return retval; + } +} diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/adapter/NcNameAdapter.java b/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/adapter/NcNameAdapter.java index 4cb7365a6..033a42bee 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/adapter/NcNameAdapter.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/adapter/NcNameAdapter.java @@ -1,64 +1,64 @@ -/* - * Portions of this software was developed by employees of the National Institute - * of Standards and Technology (NIST), an agency of the Federal Government and is - * being made available as a public service. Pursuant to title 17 United States - * Code Section 105, works of NIST employees are not subject to copyright - * protection in the United States. This software may be subject to foreign - * copyright. Permission in the United States and in foreign countries, to the - * extent that NIST may hold copyright, to use, copy, modify, create derivative - * works, and distribute this software and its documentation without fee is hereby - * granted on a non-exclusive basis, provided that this notice and disclaimer - * of warranty appears in all copies. - * - * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER - * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY - * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM - * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE - * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT - * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, - * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, - * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, - * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR - * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT - * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. - */ - -package gov.nist.secauto.metaschema.core.datatype.adapter; - -import gov.nist.secauto.metaschema.core.metapath.item.atomic.INcNameItem; -import gov.nist.secauto.metaschema.core.util.ObjectUtils; - -import java.util.List; - -import edu.umd.cs.findbugs.annotations.NonNull; - -@Deprecated(forRemoval = true, since = "0.7.0") -public class NcNameAdapter - extends AbstractStringAdapter { - @NonNull - private static final List NAMES = ObjectUtils.notNull( - List.of("ncname", - // for backwards compatibility with original type name - "NCName")); - - NcNameAdapter() { - // avoid general construction - } - - @Override - public List getNames() { - return NAMES; - } - - @Override - public Class getItemClass() { - return INcNameItem.class; - } - - @Override - public INcNameItem newItem(Object value) { - String item = asString(value); - return INcNameItem.valueOf(item); - } -} +/* + * Portions of this software was developed by employees of the National Institute + * of Standards and Technology (NIST), an agency of the Federal Government and is + * being made available as a public service. Pursuant to title 17 United States + * Code Section 105, works of NIST employees are not subject to copyright + * protection in the United States. This software may be subject to foreign + * copyright. Permission in the United States and in foreign countries, to the + * extent that NIST may hold copyright, to use, copy, modify, create derivative + * works, and distribute this software and its documentation without fee is hereby + * granted on a non-exclusive basis, provided that this notice and disclaimer + * of warranty appears in all copies. + * + * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER + * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY + * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM + * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE + * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT + * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, + * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, + * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, + * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR + * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT + * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. + */ + +package gov.nist.secauto.metaschema.core.datatype.adapter; + +import gov.nist.secauto.metaschema.core.metapath.item.atomic.INcNameItem; +import gov.nist.secauto.metaschema.core.util.ObjectUtils; + +import java.util.List; + +import edu.umd.cs.findbugs.annotations.NonNull; + +@Deprecated(forRemoval = true, since = "0.7.0") +public class NcNameAdapter + extends AbstractStringAdapter { + @NonNull + private static final List NAMES = ObjectUtils.notNull( + List.of("ncname", + // for backwards compatibility with original type name + "NCName")); + + NcNameAdapter() { + // avoid general construction + } + + @Override + public List getNames() { + return NAMES; + } + + @Override + public Class getItemClass() { + return INcNameItem.class; + } + + @Override + public INcNameItem newItem(Object value) { + String item = asString(value); + return INcNameItem.valueOf(item); + } +} diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/adapter/NonNegativeIntegerAdapter.java b/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/adapter/NonNegativeIntegerAdapter.java index 72648415a..06e4c6ddb 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/adapter/NonNegativeIntegerAdapter.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/adapter/NonNegativeIntegerAdapter.java @@ -1,65 +1,65 @@ -/* - * Portions of this software was developed by employees of the National Institute - * of Standards and Technology (NIST), an agency of the Federal Government and is - * being made available as a public service. Pursuant to title 17 United States - * Code Section 105, works of NIST employees are not subject to copyright - * protection in the United States. This software may be subject to foreign - * copyright. Permission in the United States and in foreign countries, to the - * extent that NIST may hold copyright, to use, copy, modify, create derivative - * works, and distribute this software and its documentation without fee is hereby - * granted on a non-exclusive basis, provided that this notice and disclaimer - * of warranty appears in all copies. - * - * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER - * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY - * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM - * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE - * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT - * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, - * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, - * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, - * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR - * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT - * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. - */ - -package gov.nist.secauto.metaschema.core.datatype.adapter; - -import gov.nist.secauto.metaschema.core.metapath.item.atomic.INonNegativeIntegerItem; -import gov.nist.secauto.metaschema.core.util.ObjectUtils; - -import java.math.BigInteger; -import java.util.List; - -import edu.umd.cs.findbugs.annotations.NonNull; - -public class NonNegativeIntegerAdapter - extends AbstractIntegerAdapter { - @NonNull - private static final List NAMES = ObjectUtils.notNull( - List.of( - "non-negative-integer", - // for backwards compatibility with original type name - "nonNegativeInteger")); - - NonNegativeIntegerAdapter() { - // avoid general construction - } - - @Override - public List getNames() { - return NAMES; - } - - @Override - public Class getItemClass() { - return INonNegativeIntegerItem.class; - } - - @Override - public INonNegativeIntegerItem newItem(Object value) { - BigInteger item = toValue(value); - return INonNegativeIntegerItem.valueOf(item); - } -} +/* + * Portions of this software was developed by employees of the National Institute + * of Standards and Technology (NIST), an agency of the Federal Government and is + * being made available as a public service. Pursuant to title 17 United States + * Code Section 105, works of NIST employees are not subject to copyright + * protection in the United States. This software may be subject to foreign + * copyright. Permission in the United States and in foreign countries, to the + * extent that NIST may hold copyright, to use, copy, modify, create derivative + * works, and distribute this software and its documentation without fee is hereby + * granted on a non-exclusive basis, provided that this notice and disclaimer + * of warranty appears in all copies. + * + * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER + * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY + * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM + * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE + * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT + * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, + * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, + * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, + * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR + * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT + * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. + */ + +package gov.nist.secauto.metaschema.core.datatype.adapter; + +import gov.nist.secauto.metaschema.core.metapath.item.atomic.INonNegativeIntegerItem; +import gov.nist.secauto.metaschema.core.util.ObjectUtils; + +import java.math.BigInteger; +import java.util.List; + +import edu.umd.cs.findbugs.annotations.NonNull; + +public class NonNegativeIntegerAdapter + extends AbstractIntegerAdapter { + @NonNull + private static final List NAMES = ObjectUtils.notNull( + List.of( + "non-negative-integer", + // for backwards compatibility with original type name + "nonNegativeInteger")); + + NonNegativeIntegerAdapter() { + // avoid general construction + } + + @Override + public List getNames() { + return NAMES; + } + + @Override + public Class getItemClass() { + return INonNegativeIntegerItem.class; + } + + @Override + public INonNegativeIntegerItem newItem(Object value) { + BigInteger item = toValue(value); + return INonNegativeIntegerItem.valueOf(item); + } +} diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/adapter/PositiveIntegerAdapter.java b/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/adapter/PositiveIntegerAdapter.java index 8e9aad093..2cf76f2b0 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/adapter/PositiveIntegerAdapter.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/adapter/PositiveIntegerAdapter.java @@ -1,65 +1,65 @@ -/* - * Portions of this software was developed by employees of the National Institute - * of Standards and Technology (NIST), an agency of the Federal Government and is - * being made available as a public service. Pursuant to title 17 United States - * Code Section 105, works of NIST employees are not subject to copyright - * protection in the United States. This software may be subject to foreign - * copyright. Permission in the United States and in foreign countries, to the - * extent that NIST may hold copyright, to use, copy, modify, create derivative - * works, and distribute this software and its documentation without fee is hereby - * granted on a non-exclusive basis, provided that this notice and disclaimer - * of warranty appears in all copies. - * - * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER - * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY - * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM - * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE - * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT - * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, - * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, - * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, - * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR - * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT - * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. - */ - -package gov.nist.secauto.metaschema.core.datatype.adapter; - -import gov.nist.secauto.metaschema.core.metapath.item.atomic.IPositiveIntegerItem; -import gov.nist.secauto.metaschema.core.util.ObjectUtils; - -import java.math.BigInteger; -import java.util.List; - -import edu.umd.cs.findbugs.annotations.NonNull; - -public class PositiveIntegerAdapter - extends AbstractIntegerAdapter { - @NonNull - private static final List NAMES = ObjectUtils.notNull( - List.of( - "positive-integer", - // for backwards compatibility with original type name - "positiveInteger")); - - PositiveIntegerAdapter() { - // avoid general construction - } - - @Override - public List getNames() { - return NAMES; - } - - @Override - public Class getItemClass() { - return IPositiveIntegerItem.class; - } - - @Override - public IPositiveIntegerItem newItem(Object value) { - BigInteger item = toValue(value); - return IPositiveIntegerItem.valueOf(item); - } -} +/* + * Portions of this software was developed by employees of the National Institute + * of Standards and Technology (NIST), an agency of the Federal Government and is + * being made available as a public service. Pursuant to title 17 United States + * Code Section 105, works of NIST employees are not subject to copyright + * protection in the United States. This software may be subject to foreign + * copyright. Permission in the United States and in foreign countries, to the + * extent that NIST may hold copyright, to use, copy, modify, create derivative + * works, and distribute this software and its documentation without fee is hereby + * granted on a non-exclusive basis, provided that this notice and disclaimer + * of warranty appears in all copies. + * + * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER + * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY + * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM + * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE + * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT + * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, + * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, + * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, + * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR + * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT + * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. + */ + +package gov.nist.secauto.metaschema.core.datatype.adapter; + +import gov.nist.secauto.metaschema.core.metapath.item.atomic.IPositiveIntegerItem; +import gov.nist.secauto.metaschema.core.util.ObjectUtils; + +import java.math.BigInteger; +import java.util.List; + +import edu.umd.cs.findbugs.annotations.NonNull; + +public class PositiveIntegerAdapter + extends AbstractIntegerAdapter { + @NonNull + private static final List NAMES = ObjectUtils.notNull( + List.of( + "positive-integer", + // for backwards compatibility with original type name + "positiveInteger")); + + PositiveIntegerAdapter() { + // avoid general construction + } + + @Override + public List getNames() { + return NAMES; + } + + @Override + public Class getItemClass() { + return IPositiveIntegerItem.class; + } + + @Override + public IPositiveIntegerItem newItem(Object value) { + BigInteger item = toValue(value); + return IPositiveIntegerItem.valueOf(item); + } +} diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/adapter/StringAdapter.java b/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/adapter/StringAdapter.java index 6d58c700b..bf674c9e2 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/adapter/StringAdapter.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/adapter/StringAdapter.java @@ -1,61 +1,61 @@ -/* - * Portions of this software was developed by employees of the National Institute - * of Standards and Technology (NIST), an agency of the Federal Government and is - * being made available as a public service. Pursuant to title 17 United States - * Code Section 105, works of NIST employees are not subject to copyright - * protection in the United States. This software may be subject to foreign - * copyright. Permission in the United States and in foreign countries, to the - * extent that NIST may hold copyright, to use, copy, modify, create derivative - * works, and distribute this software and its documentation without fee is hereby - * granted on a non-exclusive basis, provided that this notice and disclaimer - * of warranty appears in all copies. - * - * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER - * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY - * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM - * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE - * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT - * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, - * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, - * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, - * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR - * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT - * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. - */ - -package gov.nist.secauto.metaschema.core.datatype.adapter; - -import gov.nist.secauto.metaschema.core.metapath.item.atomic.IStringItem; -import gov.nist.secauto.metaschema.core.util.ObjectUtils; - -import java.util.List; - -import edu.umd.cs.findbugs.annotations.NonNull; - -public class StringAdapter - extends AbstractStringAdapter { - @NonNull - private static final List NAMES = ObjectUtils.notNull( - List.of("string")); - - StringAdapter() { - // avoid general construction - } - - @Override - public List getNames() { - return NAMES; - } - - @Override - public Class getItemClass() { - return IStringItem.class; - } - - @Override - public @NonNull IStringItem newItem(@NonNull Object value) { - String item = asString(value); - return IStringItem.valueOf(item); - } -} +/* + * Portions of this software was developed by employees of the National Institute + * of Standards and Technology (NIST), an agency of the Federal Government and is + * being made available as a public service. Pursuant to title 17 United States + * Code Section 105, works of NIST employees are not subject to copyright + * protection in the United States. This software may be subject to foreign + * copyright. Permission in the United States and in foreign countries, to the + * extent that NIST may hold copyright, to use, copy, modify, create derivative + * works, and distribute this software and its documentation without fee is hereby + * granted on a non-exclusive basis, provided that this notice and disclaimer + * of warranty appears in all copies. + * + * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER + * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY + * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM + * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE + * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT + * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, + * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, + * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, + * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR + * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT + * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. + */ + +package gov.nist.secauto.metaschema.core.datatype.adapter; + +import gov.nist.secauto.metaschema.core.metapath.item.atomic.IStringItem; +import gov.nist.secauto.metaschema.core.util.ObjectUtils; + +import java.util.List; + +import edu.umd.cs.findbugs.annotations.NonNull; + +public class StringAdapter + extends AbstractStringAdapter { + @NonNull + private static final List NAMES = ObjectUtils.notNull( + List.of("string")); + + StringAdapter() { + // avoid general construction + } + + @Override + public List getNames() { + return NAMES; + } + + @Override + public Class getItemClass() { + return IStringItem.class; + } + + @Override + public @NonNull IStringItem newItem(@NonNull Object value) { + String item = asString(value); + return IStringItem.valueOf(item); + } +} diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/adapter/UriAdapter.java b/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/adapter/UriAdapter.java index fbfa746ad..b5f402c6c 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/adapter/UriAdapter.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/adapter/UriAdapter.java @@ -1,83 +1,83 @@ -/* - * Portions of this software was developed by employees of the National Institute - * of Standards and Technology (NIST), an agency of the Federal Government and is - * being made available as a public service. Pursuant to title 17 United States - * Code Section 105, works of NIST employees are not subject to copyright - * protection in the United States. This software may be subject to foreign - * copyright. Permission in the United States and in foreign countries, to the - * extent that NIST may hold copyright, to use, copy, modify, create derivative - * works, and distribute this software and its documentation without fee is hereby - * granted on a non-exclusive basis, provided that this notice and disclaimer - * of warranty appears in all copies. - * - * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER - * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY - * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM - * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE - * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT - * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, - * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, - * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, - * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR - * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT - * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. - */ - -package gov.nist.secauto.metaschema.core.datatype.adapter; - -import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonFormatTypes; - -import gov.nist.secauto.metaschema.core.datatype.AbstractDataTypeAdapter; -import gov.nist.secauto.metaschema.core.metapath.item.atomic.IAnyUriItem; -import gov.nist.secauto.metaschema.core.util.ObjectUtils; - -import java.net.URI; -import java.util.List; - -import edu.umd.cs.findbugs.annotations.NonNull; - -public class UriAdapter - extends AbstractDataTypeAdapter { - @NonNull - private static final List NAMES = ObjectUtils.notNull( - List.of("uri")); - - UriAdapter() { - super(URI.class); - } - - @Override - public List getNames() { - return NAMES; - } - - @Override - public JsonFormatTypes getJsonRawType() { - return JsonFormatTypes.STRING; - } - - @SuppressWarnings("null") - @Override - public URI parse(String value) { - return URI.create(value); - } - - @Override - public URI copy(Object obj) { - // a URI is immutable - return (URI) obj; - } - - @Override - public Class getItemClass() { - return IAnyUriItem.class; - } - - @Override - public IAnyUriItem newItem(Object value) { - URI item = toValue(value); - return IAnyUriItem.valueOf(item); - } - -} +/* + * Portions of this software was developed by employees of the National Institute + * of Standards and Technology (NIST), an agency of the Federal Government and is + * being made available as a public service. Pursuant to title 17 United States + * Code Section 105, works of NIST employees are not subject to copyright + * protection in the United States. This software may be subject to foreign + * copyright. Permission in the United States and in foreign countries, to the + * extent that NIST may hold copyright, to use, copy, modify, create derivative + * works, and distribute this software and its documentation without fee is hereby + * granted on a non-exclusive basis, provided that this notice and disclaimer + * of warranty appears in all copies. + * + * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER + * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY + * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM + * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE + * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT + * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, + * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, + * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, + * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR + * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT + * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. + */ + +package gov.nist.secauto.metaschema.core.datatype.adapter; + +import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonFormatTypes; + +import gov.nist.secauto.metaschema.core.datatype.AbstractDataTypeAdapter; +import gov.nist.secauto.metaschema.core.metapath.item.atomic.IAnyUriItem; +import gov.nist.secauto.metaschema.core.util.ObjectUtils; + +import java.net.URI; +import java.util.List; + +import edu.umd.cs.findbugs.annotations.NonNull; + +public class UriAdapter + extends AbstractDataTypeAdapter { + @NonNull + private static final List NAMES = ObjectUtils.notNull( + List.of("uri")); + + UriAdapter() { + super(URI.class); + } + + @Override + public List getNames() { + return NAMES; + } + + @Override + public JsonFormatTypes getJsonRawType() { + return JsonFormatTypes.STRING; + } + + @SuppressWarnings("null") + @Override + public URI parse(String value) { + return URI.create(value); + } + + @Override + public URI copy(Object obj) { + // a URI is immutable + return (URI) obj; + } + + @Override + public Class getItemClass() { + return IAnyUriItem.class; + } + + @Override + public IAnyUriItem newItem(Object value) { + URI item = toValue(value); + return IAnyUriItem.valueOf(item); + } + +} diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/adapter/UriReferenceAdapter.java b/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/adapter/UriReferenceAdapter.java index e17f4d58b..45180c289 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/adapter/UriReferenceAdapter.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/adapter/UriReferenceAdapter.java @@ -1,82 +1,82 @@ -/* - * Portions of this software was developed by employees of the National Institute - * of Standards and Technology (NIST), an agency of the Federal Government and is - * being made available as a public service. Pursuant to title 17 United States - * Code Section 105, works of NIST employees are not subject to copyright - * protection in the United States. This software may be subject to foreign - * copyright. Permission in the United States and in foreign countries, to the - * extent that NIST may hold copyright, to use, copy, modify, create derivative - * works, and distribute this software and its documentation without fee is hereby - * granted on a non-exclusive basis, provided that this notice and disclaimer - * of warranty appears in all copies. - * - * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER - * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY - * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM - * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE - * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT - * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, - * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, - * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, - * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR - * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT - * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. - */ - -package gov.nist.secauto.metaschema.core.datatype.adapter; - -import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonFormatTypes; - -import gov.nist.secauto.metaschema.core.datatype.AbstractDataTypeAdapter; -import gov.nist.secauto.metaschema.core.metapath.item.atomic.IUriReferenceItem; -import gov.nist.secauto.metaschema.core.util.ObjectUtils; - -import java.net.URI; -import java.util.List; - -import edu.umd.cs.findbugs.annotations.NonNull; - -public class UriReferenceAdapter - extends AbstractDataTypeAdapter { - @NonNull - private static final List NAMES = ObjectUtils.notNull( - List.of("uri-reference")); - - UriReferenceAdapter() { - super(URI.class); - } - - @Override - public List getNames() { - return NAMES; - } - - @Override - public JsonFormatTypes getJsonRawType() { - return JsonFormatTypes.STRING; - } - - @SuppressWarnings("null") - @Override - public URI parse(String value) { - return URI.create(value); - } - - @Override - public URI copy(Object obj) { - // a URI is immutable - return (URI) obj; - } - - @Override - public Class getItemClass() { - return IUriReferenceItem.class; - } - - @Override - public IUriReferenceItem newItem(Object value) { - URI item = toValue(value); - return IUriReferenceItem.valueOf(item); - } -} +/* + * Portions of this software was developed by employees of the National Institute + * of Standards and Technology (NIST), an agency of the Federal Government and is + * being made available as a public service. Pursuant to title 17 United States + * Code Section 105, works of NIST employees are not subject to copyright + * protection in the United States. This software may be subject to foreign + * copyright. Permission in the United States and in foreign countries, to the + * extent that NIST may hold copyright, to use, copy, modify, create derivative + * works, and distribute this software and its documentation without fee is hereby + * granted on a non-exclusive basis, provided that this notice and disclaimer + * of warranty appears in all copies. + * + * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER + * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY + * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM + * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE + * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT + * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, + * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, + * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, + * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR + * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT + * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. + */ + +package gov.nist.secauto.metaschema.core.datatype.adapter; + +import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonFormatTypes; + +import gov.nist.secauto.metaschema.core.datatype.AbstractDataTypeAdapter; +import gov.nist.secauto.metaschema.core.metapath.item.atomic.IUriReferenceItem; +import gov.nist.secauto.metaschema.core.util.ObjectUtils; + +import java.net.URI; +import java.util.List; + +import edu.umd.cs.findbugs.annotations.NonNull; + +public class UriReferenceAdapter + extends AbstractDataTypeAdapter { + @NonNull + private static final List NAMES = ObjectUtils.notNull( + List.of("uri-reference")); + + UriReferenceAdapter() { + super(URI.class); + } + + @Override + public List getNames() { + return NAMES; + } + + @Override + public JsonFormatTypes getJsonRawType() { + return JsonFormatTypes.STRING; + } + + @SuppressWarnings("null") + @Override + public URI parse(String value) { + return URI.create(value); + } + + @Override + public URI copy(Object obj) { + // a URI is immutable + return (URI) obj; + } + + @Override + public Class getItemClass() { + return IUriReferenceItem.class; + } + + @Override + public IUriReferenceItem newItem(Object value) { + URI item = toValue(value); + return IUriReferenceItem.valueOf(item); + } +} diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/markup/AbstractMarkupString.java b/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/markup/AbstractMarkupString.java index 667e292af..5b5f7ddda 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/markup/AbstractMarkupString.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/markup/AbstractMarkupString.java @@ -1,300 +1,300 @@ -/* - * Portions of this software was developed by employees of the National Institute - * of Standards and Technology (NIST), an agency of the Federal Government and is - * being made available as a public service. Pursuant to title 17 United States - * Code Section 105, works of NIST employees are not subject to copyright - * protection in the United States. This software may be subject to foreign - * copyright. Permission in the United States and in foreign countries, to the - * extent that NIST may hold copyright, to use, copy, modify, create derivative - * works, and distribute this software and its documentation without fee is hereby - * granted on a non-exclusive basis, provided that this notice and disclaimer - * of warranty appears in all copies. - * - * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER - * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY - * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM - * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE - * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT - * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, - * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, - * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, - * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR - * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT - * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. - */ - -package gov.nist.secauto.metaschema.core.datatype.markup; - -import com.ctc.wstx.api.WstxOutputProperties; -import com.ctc.wstx.stax.WstxOutputFactory; -import com.vladsch.flexmark.formatter.Formatter; -import com.vladsch.flexmark.html2md.converter.FlexmarkHtmlConverter; -import com.vladsch.flexmark.parser.Parser; -import com.vladsch.flexmark.util.ast.Document; -import com.vladsch.flexmark.util.ast.Node; - -import gov.nist.secauto.metaschema.core.datatype.markup.flexmark.AstCollectingVisitor; -import gov.nist.secauto.metaschema.core.datatype.markup.flexmark.FlexmarkFactory; -import gov.nist.secauto.metaschema.core.datatype.markup.flexmark.IMarkupVisitor; -import gov.nist.secauto.metaschema.core.datatype.markup.flexmark.IMarkupWriter; -import gov.nist.secauto.metaschema.core.datatype.markup.flexmark.InsertAnchorExtension.InsertAnchorNode; -import gov.nist.secauto.metaschema.core.datatype.markup.flexmark.InsertVisitor; -import gov.nist.secauto.metaschema.core.datatype.markup.flexmark.MarkupVisitor; -import gov.nist.secauto.metaschema.core.datatype.markup.flexmark.MarkupXmlEventWriter; -import gov.nist.secauto.metaschema.core.datatype.markup.flexmark.MarkupXmlStreamWriter; -import gov.nist.secauto.metaschema.core.util.ObjectUtils; - -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; -import org.codehaus.stax2.XMLOutputFactory2; -import org.codehaus.stax2.XMLStreamWriter2; -import org.codehaus.stax2.evt.XMLEventFactory2; -import org.jsoup.Jsoup; -import org.jsoup.nodes.TextNode; -import org.jsoup.select.NodeTraversor; -import org.jsoup.select.NodeVisitor; - -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.nio.charset.StandardCharsets; -import java.util.List; -import java.util.function.Predicate; -import java.util.regex.Pattern; -import java.util.stream.Stream; -import java.util.stream.StreamSupport; - -import javax.xml.stream.XMLEventWriter; -import javax.xml.stream.XMLOutputFactory; -import javax.xml.stream.XMLStreamException; -import javax.xml.stream.XMLStreamWriter; - -import edu.umd.cs.findbugs.annotations.NonNull; -import edu.umd.cs.findbugs.annotations.Nullable; - -@SuppressWarnings("PMD.CouplingBetweenObjects") -public abstract class AbstractMarkupString> - implements IMarkupString { - private static final Logger LOGGER = LogManager.getLogger(FlexmarkFactory.class); - - private static final Pattern QUOTE_TAG_REPLACEMENT_PATTERN - = Pattern.compile(""); - - // - // @NonNull - // private static final String DEFAULT_HTML_NS = "http://www.w3.org/1999/xhtml"; - // @NonNull - // private static final String DEFAULT_HTML_PREFIX = ""; - - @NonNull - private final Document document; - - /** - * Construct a new markup string based on the provided flexmark AST graph. - * - * @param document - * the AST graph representing Markdown text - */ - protected AbstractMarkupString(@NonNull Document document) { - this.document = document; - } - - @Override - public Document getDocument() { - return document; - } - - @Override - public boolean isEmpty() { - return getDocument().getFirstChild() == null; - } - - /** - * Parse HTML-based text into markdown as a flexmark AST graph. - *

- * This method uses a two-step approach that first translates the HTML into - * markdown, and then parses the markdown into an AST graph. - * - * @param html - * the HTML text to parse - * @param htmlParser - * the HTML parser used to produce markdown - * @param markdownParser - * the markdown parser - * @return the markdown AST graph - */ - @NonNull - protected static Document parseHtml(@NonNull String html, @NonNull FlexmarkHtmlConverter htmlParser, - @NonNull Parser markdownParser) { - org.jsoup.nodes.Document document = Jsoup.parse(html); - - // Fix for usnistgov/liboscal-java#5 - // Caused by not stripping out extra newlines inside HTML tags - NodeTraversor.traverse(new NodeVisitor() { - - @Override - public void head(org.jsoup.nodes.Node node, int depth) { - if (node instanceof TextNode) { - TextNode textNode = (TextNode) node; - - org.jsoup.nodes.Node parent = textNode.parent(); - - if (!isTag(parent, "code") || !isTag(parent.parent(), "pre")) { - node.replaceWith(new TextNode(textNode.text())); - } - } - } - - private boolean isTag(@Nullable org.jsoup.nodes.Node node, @NonNull String tagName) { - return node != null && tagName.equals(node.normalName()); - } - - }, document); - - String markdown = htmlParser.convert(document); - assert markdown != null; - if (LOGGER.isDebugEnabled()) { - LOGGER.debug("html->markdown: {}", markdown); - } - return parseMarkdown(markdown, markdownParser); - } - - /** - * Parse markdown-based text into a flexmark AST graph. - * - * @param markdown - * the markdown text to parse - * @param parser - * the markdown parser - * @return the markdown AST graph - */ - @SuppressWarnings("null") - @NonNull - protected static Document parseMarkdown(@NonNull String markdown, @NonNull Parser parser) { - return parser.parse(markdown); - } - - @Override - public String toXHtml(@NonNull String namespace) throws XMLStreamException, IOException { - - String retval; - - Document document = getDocument(); - if (document.hasChildren()) { - - XMLOutputFactory2 factory = (XMLOutputFactory2) XMLOutputFactory.newInstance(); - assert factory instanceof WstxOutputFactory; - factory.setProperty(WstxOutputProperties.P_OUTPUT_VALIDATE_STRUCTURE, false); - try (ByteArrayOutputStream os = new ByteArrayOutputStream()) { - XMLStreamWriter2 xmlStreamWriter = (XMLStreamWriter2) factory.createXMLStreamWriter(os); - - writeXHtml(namespace, ObjectUtils.notNull(xmlStreamWriter)); - - xmlStreamWriter.flush(); - xmlStreamWriter.close(); - os.flush(); - retval = ObjectUtils.notNull(os.toString(StandardCharsets.UTF_8)); - } - } else { - retval = ""; - } - return retval; - } - - @Override - public String toHtml() { - // String html; - // try { - // html = toXHtml(""); - // } catch(RuntimeException ex) { - // throw ex; - // } catch (Throwable ex) { - // throw new RuntimeException(ex); - // } - // return QUOTE_TAG_REPLACEMENT_PATTERN.matcher(html) - // .replaceAll("""); - String html = getFlexmarkFactory().getHtmlRenderer().render(getDocument()); - return ObjectUtils.notNull(QUOTE_TAG_REPLACEMENT_PATTERN.matcher(html) - .replaceAll(""")); - } - - @Override - public String toMarkdown() { - return toMarkdown(getFlexmarkFactory().getFormatter()); - } - - @Override - public String toMarkdown(Formatter formatter) { - return ObjectUtils.notNull(formatter.render(getDocument())); - } - - @Override - public void writeXHtml(String namespace, XMLStreamWriter2 streamWriter) throws XMLStreamException { - Document document = getDocument(); - if (document.hasChildren()) { - IMarkupWriter writer = new MarkupXmlStreamWriter( - namespace, - getFlexmarkFactory().getListOptions(), - streamWriter); - - IMarkupVisitor visitor = new MarkupVisitor<>(isBlock()); - visitor.visitDocument(document, writer); - } else { - streamWriter.writeCharacters(""); - } - } - - @Override - public void writeXHtml(String namespace, XMLEventFactory2 eventFactory, XMLEventWriter eventWriter) - throws XMLStreamException { - Document document = getDocument(); - if (document.hasChildren()) { - - IMarkupWriter writer = new MarkupXmlEventWriter( - namespace, - getFlexmarkFactory().getListOptions(), - eventWriter, - eventFactory); - - IMarkupVisitor visitor = new MarkupVisitor<>(isBlock()); - visitor.visitDocument(getDocument(), writer); - } else { - eventWriter.add(eventFactory.createSpace("")); - } - - } - - @SuppressWarnings("null") - @Override - public Stream getNodesAsStream() { - return Stream.concat(Stream.of(getDocument()), - StreamSupport.stream(getDocument().getDescendants().spliterator(), false)); - } - - @Override - @NonNull - public List getInserts() { - return getInserts(insert -> true); - } - - /** - * Retrieve all insert statements that are contained within this markup text - * that match the provided filter. - * - * @param filter - * a filter used to identify matching insert statements - * @return the matching insert statements - */ - @Override - @NonNull - public List getInserts(@NonNull Predicate filter) { - InsertVisitor visitor = new InsertVisitor(filter); - visitor.visitChildren(getDocument()); - return visitor.getInserts(); - } - - @Override - public String toString() { - return AstCollectingVisitor.asString(getDocument()); - } -} +/* + * Portions of this software was developed by employees of the National Institute + * of Standards and Technology (NIST), an agency of the Federal Government and is + * being made available as a public service. Pursuant to title 17 United States + * Code Section 105, works of NIST employees are not subject to copyright + * protection in the United States. This software may be subject to foreign + * copyright. Permission in the United States and in foreign countries, to the + * extent that NIST may hold copyright, to use, copy, modify, create derivative + * works, and distribute this software and its documentation without fee is hereby + * granted on a non-exclusive basis, provided that this notice and disclaimer + * of warranty appears in all copies. + * + * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER + * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY + * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM + * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE + * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT + * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, + * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, + * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, + * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR + * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT + * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. + */ + +package gov.nist.secauto.metaschema.core.datatype.markup; + +import com.ctc.wstx.api.WstxOutputProperties; +import com.ctc.wstx.stax.WstxOutputFactory; +import com.vladsch.flexmark.formatter.Formatter; +import com.vladsch.flexmark.html2md.converter.FlexmarkHtmlConverter; +import com.vladsch.flexmark.parser.Parser; +import com.vladsch.flexmark.util.ast.Document; +import com.vladsch.flexmark.util.ast.Node; + +import gov.nist.secauto.metaschema.core.datatype.markup.flexmark.AstCollectingVisitor; +import gov.nist.secauto.metaschema.core.datatype.markup.flexmark.FlexmarkFactory; +import gov.nist.secauto.metaschema.core.datatype.markup.flexmark.IMarkupVisitor; +import gov.nist.secauto.metaschema.core.datatype.markup.flexmark.IMarkupWriter; +import gov.nist.secauto.metaschema.core.datatype.markup.flexmark.InsertAnchorExtension.InsertAnchorNode; +import gov.nist.secauto.metaschema.core.datatype.markup.flexmark.InsertVisitor; +import gov.nist.secauto.metaschema.core.datatype.markup.flexmark.MarkupVisitor; +import gov.nist.secauto.metaschema.core.datatype.markup.flexmark.MarkupXmlEventWriter; +import gov.nist.secauto.metaschema.core.datatype.markup.flexmark.MarkupXmlStreamWriter; +import gov.nist.secauto.metaschema.core.util.ObjectUtils; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.codehaus.stax2.XMLOutputFactory2; +import org.codehaus.stax2.XMLStreamWriter2; +import org.codehaus.stax2.evt.XMLEventFactory2; +import org.jsoup.Jsoup; +import org.jsoup.nodes.TextNode; +import org.jsoup.select.NodeTraversor; +import org.jsoup.select.NodeVisitor; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.util.List; +import java.util.function.Predicate; +import java.util.regex.Pattern; +import java.util.stream.Stream; +import java.util.stream.StreamSupport; + +import javax.xml.stream.XMLEventWriter; +import javax.xml.stream.XMLOutputFactory; +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.XMLStreamWriter; + +import edu.umd.cs.findbugs.annotations.NonNull; +import edu.umd.cs.findbugs.annotations.Nullable; + +@SuppressWarnings("PMD.CouplingBetweenObjects") +public abstract class AbstractMarkupString> + implements IMarkupString { + private static final Logger LOGGER = LogManager.getLogger(FlexmarkFactory.class); + + private static final Pattern QUOTE_TAG_REPLACEMENT_PATTERN + = Pattern.compile(""); + + // + // @NonNull + // private static final String DEFAULT_HTML_NS = "http://www.w3.org/1999/xhtml"; + // @NonNull + // private static final String DEFAULT_HTML_PREFIX = ""; + + @NonNull + private final Document document; + + /** + * Construct a new markup string based on the provided flexmark AST graph. + * + * @param document + * the AST graph representing Markdown text + */ + protected AbstractMarkupString(@NonNull Document document) { + this.document = document; + } + + @Override + public Document getDocument() { + return document; + } + + @Override + public boolean isEmpty() { + return getDocument().getFirstChild() == null; + } + + /** + * Parse HTML-based text into markdown as a flexmark AST graph. + *

+ * This method uses a two-step approach that first translates the HTML into + * markdown, and then parses the markdown into an AST graph. + * + * @param html + * the HTML text to parse + * @param htmlParser + * the HTML parser used to produce markdown + * @param markdownParser + * the markdown parser + * @return the markdown AST graph + */ + @NonNull + protected static Document parseHtml(@NonNull String html, @NonNull FlexmarkHtmlConverter htmlParser, + @NonNull Parser markdownParser) { + org.jsoup.nodes.Document document = Jsoup.parse(html); + + // Fix for usnistgov/liboscal-java#5 + // Caused by not stripping out extra newlines inside HTML tags + NodeTraversor.traverse(new NodeVisitor() { + + @Override + public void head(org.jsoup.nodes.Node node, int depth) { + if (node instanceof TextNode) { + TextNode textNode = (TextNode) node; + + org.jsoup.nodes.Node parent = textNode.parent(); + + if (!isTag(parent, "code") || !isTag(parent.parent(), "pre")) { + node.replaceWith(new TextNode(textNode.text())); + } + } + } + + private boolean isTag(@Nullable org.jsoup.nodes.Node node, @NonNull String tagName) { + return node != null && tagName.equals(node.normalName()); + } + + }, document); + + String markdown = htmlParser.convert(document); + assert markdown != null; + if (LOGGER.isDebugEnabled()) { + LOGGER.debug("html->markdown: {}", markdown); + } + return parseMarkdown(markdown, markdownParser); + } + + /** + * Parse markdown-based text into a flexmark AST graph. + * + * @param markdown + * the markdown text to parse + * @param parser + * the markdown parser + * @return the markdown AST graph + */ + @SuppressWarnings("null") + @NonNull + protected static Document parseMarkdown(@NonNull String markdown, @NonNull Parser parser) { + return parser.parse(markdown); + } + + @Override + public String toXHtml(@NonNull String namespace) throws XMLStreamException, IOException { + + String retval; + + Document document = getDocument(); + if (document.hasChildren()) { + + XMLOutputFactory2 factory = (XMLOutputFactory2) XMLOutputFactory.newInstance(); + assert factory instanceof WstxOutputFactory; + factory.setProperty(WstxOutputProperties.P_OUTPUT_VALIDATE_STRUCTURE, false); + try (ByteArrayOutputStream os = new ByteArrayOutputStream()) { + XMLStreamWriter2 xmlStreamWriter = (XMLStreamWriter2) factory.createXMLStreamWriter(os); + + writeXHtml(namespace, ObjectUtils.notNull(xmlStreamWriter)); + + xmlStreamWriter.flush(); + xmlStreamWriter.close(); + os.flush(); + retval = ObjectUtils.notNull(os.toString(StandardCharsets.UTF_8)); + } + } else { + retval = ""; + } + return retval; + } + + @Override + public String toHtml() { + // String html; + // try { + // html = toXHtml(""); + // } catch(RuntimeException ex) { + // throw ex; + // } catch (Throwable ex) { + // throw new RuntimeException(ex); + // } + // return QUOTE_TAG_REPLACEMENT_PATTERN.matcher(html) + // .replaceAll("""); + String html = getFlexmarkFactory().getHtmlRenderer().render(getDocument()); + return ObjectUtils.notNull(QUOTE_TAG_REPLACEMENT_PATTERN.matcher(html) + .replaceAll(""")); + } + + @Override + public String toMarkdown() { + return toMarkdown(getFlexmarkFactory().getFormatter()); + } + + @Override + public String toMarkdown(Formatter formatter) { + return ObjectUtils.notNull(formatter.render(getDocument())); + } + + @Override + public void writeXHtml(String namespace, XMLStreamWriter2 streamWriter) throws XMLStreamException { + Document document = getDocument(); + if (document.hasChildren()) { + IMarkupWriter writer = new MarkupXmlStreamWriter( + namespace, + getFlexmarkFactory().getListOptions(), + streamWriter); + + IMarkupVisitor visitor = new MarkupVisitor<>(isBlock()); + visitor.visitDocument(document, writer); + } else { + streamWriter.writeCharacters(""); + } + } + + @Override + public void writeXHtml(String namespace, XMLEventFactory2 eventFactory, XMLEventWriter eventWriter) + throws XMLStreamException { + Document document = getDocument(); + if (document.hasChildren()) { + + IMarkupWriter writer = new MarkupXmlEventWriter( + namespace, + getFlexmarkFactory().getListOptions(), + eventWriter, + eventFactory); + + IMarkupVisitor visitor = new MarkupVisitor<>(isBlock()); + visitor.visitDocument(getDocument(), writer); + } else { + eventWriter.add(eventFactory.createSpace("")); + } + + } + + @SuppressWarnings("null") + @Override + public Stream getNodesAsStream() { + return Stream.concat(Stream.of(getDocument()), + StreamSupport.stream(getDocument().getDescendants().spliterator(), false)); + } + + @Override + @NonNull + public List getInserts() { + return getInserts(insert -> true); + } + + /** + * Retrieve all insert statements that are contained within this markup text + * that match the provided filter. + * + * @param filter + * a filter used to identify matching insert statements + * @return the matching insert statements + */ + @Override + @NonNull + public List getInserts(@NonNull Predicate filter) { + InsertVisitor visitor = new InsertVisitor(filter); + visitor.visitChildren(getDocument()); + return visitor.getInserts(); + } + + @Override + public String toString() { + return AstCollectingVisitor.asString(getDocument()); + } +} diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/markup/MarkupLine.java b/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/markup/MarkupLine.java index 20772f991..72f223e1d 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/markup/MarkupLine.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/markup/MarkupLine.java @@ -1,110 +1,110 @@ -/* - * Portions of this software was developed by employees of the National Institute - * of Standards and Technology (NIST), an agency of the Federal Government and is - * being made available as a public service. Pursuant to title 17 United States - * Code Section 105, works of NIST employees are not subject to copyright - * protection in the United States. This software may be subject to foreign - * copyright. Permission in the United States and in foreign countries, to the - * extent that NIST may hold copyright, to use, copy, modify, create derivative - * works, and distribute this software and its documentation without fee is hereby - * granted on a non-exclusive basis, provided that this notice and disclaimer - * of warranty appears in all copies. - * - * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER - * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY - * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM - * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE - * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT - * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, - * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, - * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, - * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR - * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT - * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. - */ - -package gov.nist.secauto.metaschema.core.datatype.markup; - -import com.vladsch.flexmark.html.HtmlRenderer; -import com.vladsch.flexmark.parser.Parser; -import com.vladsch.flexmark.util.ast.Block; -import com.vladsch.flexmark.util.ast.Document; -import com.vladsch.flexmark.util.ast.Node; -import com.vladsch.flexmark.util.data.DataSet; -import com.vladsch.flexmark.util.data.MutableDataSet; -import com.vladsch.flexmark.util.misc.Extension; - -import gov.nist.secauto.metaschema.core.datatype.markup.flexmark.FlexmarkConfiguration; -import gov.nist.secauto.metaschema.core.datatype.markup.flexmark.FlexmarkFactory; -import gov.nist.secauto.metaschema.core.datatype.markup.flexmark.SuppressPTagExtension; - -import java.util.Collection; -import java.util.LinkedList; -import java.util.List; - -import edu.umd.cs.findbugs.annotations.NonNull; - -public class MarkupLine - extends AbstractMarkupString { - - @NonNull - private static final DataSet FLEXMARK_CONFIG = newParserOptions(); - - @NonNull - private static final FlexmarkFactory FLEXMARK_FACTORY = FlexmarkFactory.newInstance(FLEXMARK_CONFIG); - - @SuppressWarnings("null") - @NonNull - protected static DataSet newParserOptions() { - MutableDataSet options = new MutableDataSet(); - // disable inline HTML - options.set(Parser.HTML_BLOCK_PARSER, false); - // disable list processing - options.set(Parser.LIST_BLOCK_PARSER, false); - options.set(HtmlRenderer.SUPPRESS_HTML_BLOCKS, true); - - Collection currentExtensions = Parser.EXTENSIONS.get(FlexmarkConfiguration.FLEXMARK_CONFIG); - List extensions = new LinkedList<>(currentExtensions); - extensions.add(SuppressPTagExtension.create()); - Parser.EXTENSIONS.set(options, extensions); - - return FlexmarkConfiguration.newFlexmarkConfig(options); - } - - @NonNull - public static MarkupLine fromHtml(@NonNull String html) { - return new MarkupLine( - parseHtml(html, FLEXMARK_FACTORY.getFlexmarkHtmlConverter(), FLEXMARK_FACTORY.getMarkdownParser())); - } - - @NonNull - public static MarkupLine fromMarkdown(@NonNull String markdown) { - return new MarkupLine(parseMarkdown(markdown, FLEXMARK_FACTORY.getMarkdownParser())); - } - - @Override - public FlexmarkFactory getFlexmarkFactory() { - return FLEXMARK_FACTORY; - } - - protected MarkupLine(@NonNull Document astNode) { - super(astNode); - Node child = astNode.getFirstChild(); - if (child instanceof Block && child.getNext() != null) { - throw new IllegalStateException("multiple blocks not allowed"); - } // else empty markdown - } - - @Override - public MarkupLine copy() { - // TODO: find a way to do a deep copy - // this is a shallow copy that uses the same underlying Document object - return new MarkupLine(getDocument()); - } - - @Override - public boolean isBlock() { - return false; - } -} +/* + * Portions of this software was developed by employees of the National Institute + * of Standards and Technology (NIST), an agency of the Federal Government and is + * being made available as a public service. Pursuant to title 17 United States + * Code Section 105, works of NIST employees are not subject to copyright + * protection in the United States. This software may be subject to foreign + * copyright. Permission in the United States and in foreign countries, to the + * extent that NIST may hold copyright, to use, copy, modify, create derivative + * works, and distribute this software and its documentation without fee is hereby + * granted on a non-exclusive basis, provided that this notice and disclaimer + * of warranty appears in all copies. + * + * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER + * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY + * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM + * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE + * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT + * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, + * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, + * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, + * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR + * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT + * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. + */ + +package gov.nist.secauto.metaschema.core.datatype.markup; + +import com.vladsch.flexmark.html.HtmlRenderer; +import com.vladsch.flexmark.parser.Parser; +import com.vladsch.flexmark.util.ast.Block; +import com.vladsch.flexmark.util.ast.Document; +import com.vladsch.flexmark.util.ast.Node; +import com.vladsch.flexmark.util.data.DataSet; +import com.vladsch.flexmark.util.data.MutableDataSet; +import com.vladsch.flexmark.util.misc.Extension; + +import gov.nist.secauto.metaschema.core.datatype.markup.flexmark.FlexmarkConfiguration; +import gov.nist.secauto.metaschema.core.datatype.markup.flexmark.FlexmarkFactory; +import gov.nist.secauto.metaschema.core.datatype.markup.flexmark.SuppressPTagExtension; + +import java.util.Collection; +import java.util.LinkedList; +import java.util.List; + +import edu.umd.cs.findbugs.annotations.NonNull; + +public class MarkupLine + extends AbstractMarkupString { + + @NonNull + private static final DataSet FLEXMARK_CONFIG = newParserOptions(); + + @NonNull + private static final FlexmarkFactory FLEXMARK_FACTORY = FlexmarkFactory.newInstance(FLEXMARK_CONFIG); + + @SuppressWarnings("null") + @NonNull + protected static DataSet newParserOptions() { + MutableDataSet options = new MutableDataSet(); + // disable inline HTML + options.set(Parser.HTML_BLOCK_PARSER, false); + // disable list processing + options.set(Parser.LIST_BLOCK_PARSER, false); + options.set(HtmlRenderer.SUPPRESS_HTML_BLOCKS, true); + + Collection currentExtensions = Parser.EXTENSIONS.get(FlexmarkConfiguration.FLEXMARK_CONFIG); + List extensions = new LinkedList<>(currentExtensions); + extensions.add(SuppressPTagExtension.create()); + Parser.EXTENSIONS.set(options, extensions); + + return FlexmarkConfiguration.newFlexmarkConfig(options); + } + + @NonNull + public static MarkupLine fromHtml(@NonNull String html) { + return new MarkupLine( + parseHtml(html, FLEXMARK_FACTORY.getFlexmarkHtmlConverter(), FLEXMARK_FACTORY.getMarkdownParser())); + } + + @NonNull + public static MarkupLine fromMarkdown(@NonNull String markdown) { + return new MarkupLine(parseMarkdown(markdown, FLEXMARK_FACTORY.getMarkdownParser())); + } + + @Override + public FlexmarkFactory getFlexmarkFactory() { + return FLEXMARK_FACTORY; + } + + protected MarkupLine(@NonNull Document astNode) { + super(astNode); + Node child = astNode.getFirstChild(); + if (child instanceof Block && child.getNext() != null) { + throw new IllegalStateException("multiple blocks not allowed"); + } // else empty markdown + } + + @Override + public MarkupLine copy() { + // TODO: find a way to do a deep copy + // this is a shallow copy that uses the same underlying Document object + return new MarkupLine(getDocument()); + } + + @Override + public boolean isBlock() { + return false; + } +} diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/markup/MarkupLineAdapter.java b/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/markup/MarkupLineAdapter.java index b1dbdd58b..d0a9ad585 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/markup/MarkupLineAdapter.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/markup/MarkupLineAdapter.java @@ -1,100 +1,100 @@ -/* - * Portions of this software was developed by employees of the National Institute - * of Standards and Technology (NIST), an agency of the Federal Government and is - * being made available as a public service. Pursuant to title 17 United States - * Code Section 105, works of NIST employees are not subject to copyright - * protection in the United States. This software may be subject to foreign - * copyright. Permission in the United States and in foreign countries, to the - * extent that NIST may hold copyright, to use, copy, modify, create derivative - * works, and distribute this software and its documentation without fee is hereby - * granted on a non-exclusive basis, provided that this notice and disclaimer - * of warranty appears in all copies. - * - * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER - * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY - * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM - * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE - * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT - * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, - * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, - * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, - * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR - * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT - * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. - */ - -package gov.nist.secauto.metaschema.core.datatype.markup; - -import com.fasterxml.jackson.core.JsonParser; - -import gov.nist.secauto.metaschema.core.datatype.markup.flexmark.XmlMarkupParser; -import gov.nist.secauto.metaschema.core.metapath.item.atomic.IMarkupItem; -import gov.nist.secauto.metaschema.core.util.ObjectUtils; - -import org.codehaus.stax2.XMLEventReader2; - -import java.io.IOException; -import java.util.List; - -import javax.xml.stream.XMLStreamException; - -import edu.umd.cs.findbugs.annotations.NonNull; - -public class MarkupLineAdapter - extends AbstractMarkupAdapter { - @NonNull - private static final List NAMES = ObjectUtils.notNull( - List.of("markup-line")); - - MarkupLineAdapter() { - super(MarkupLine.class); - } - - @Override - public List getNames() { - return NAMES; - } - - /** - * Parse a line of Markdown. - */ - @Override - public MarkupLine parse(String value) { - return MarkupLine.fromMarkdown(value); - } - - @SuppressWarnings("null") - @Override - public MarkupLine parse(XMLEventReader2 eventReader) throws IOException { - try { - return XmlMarkupParser.instance().parseMarkupline(eventReader); - } catch (XMLStreamException ex) { - throw new IOException(ex); - } - } - - @Override - public MarkupLine parse(JsonParser parser) throws IOException { - @SuppressWarnings("null") MarkupLine retval = parse(parser.getValueAsString()); - // skip past value - parser.nextToken(); - return retval; - } - - @Override - public String getDefaultJsonValueKey() { - return "RICHTEXT"; - } - - @Override - public Class getItemClass() { - return IMarkupItem.class; - } - - @Override - public IMarkupItem newItem(Object value) { - MarkupLine item = toValue(value); - return IMarkupItem.valueOf(item); - } -} +/* + * Portions of this software was developed by employees of the National Institute + * of Standards and Technology (NIST), an agency of the Federal Government and is + * being made available as a public service. Pursuant to title 17 United States + * Code Section 105, works of NIST employees are not subject to copyright + * protection in the United States. This software may be subject to foreign + * copyright. Permission in the United States and in foreign countries, to the + * extent that NIST may hold copyright, to use, copy, modify, create derivative + * works, and distribute this software and its documentation without fee is hereby + * granted on a non-exclusive basis, provided that this notice and disclaimer + * of warranty appears in all copies. + * + * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER + * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY + * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM + * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE + * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT + * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, + * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, + * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, + * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR + * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT + * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. + */ + +package gov.nist.secauto.metaschema.core.datatype.markup; + +import com.fasterxml.jackson.core.JsonParser; + +import gov.nist.secauto.metaschema.core.datatype.markup.flexmark.XmlMarkupParser; +import gov.nist.secauto.metaschema.core.metapath.item.atomic.IMarkupItem; +import gov.nist.secauto.metaschema.core.util.ObjectUtils; + +import org.codehaus.stax2.XMLEventReader2; + +import java.io.IOException; +import java.util.List; + +import javax.xml.stream.XMLStreamException; + +import edu.umd.cs.findbugs.annotations.NonNull; + +public class MarkupLineAdapter + extends AbstractMarkupAdapter { + @NonNull + private static final List NAMES = ObjectUtils.notNull( + List.of("markup-line")); + + MarkupLineAdapter() { + super(MarkupLine.class); + } + + @Override + public List getNames() { + return NAMES; + } + + /** + * Parse a line of Markdown. + */ + @Override + public MarkupLine parse(String value) { + return MarkupLine.fromMarkdown(value); + } + + @SuppressWarnings("null") + @Override + public MarkupLine parse(XMLEventReader2 eventReader) throws IOException { + try { + return XmlMarkupParser.instance().parseMarkupline(eventReader); + } catch (XMLStreamException ex) { + throw new IOException(ex); + } + } + + @Override + public MarkupLine parse(JsonParser parser) throws IOException { + @SuppressWarnings("null") MarkupLine retval = parse(parser.getValueAsString()); + // skip past value + parser.nextToken(); + return retval; + } + + @Override + public String getDefaultJsonValueKey() { + return "RICHTEXT"; + } + + @Override + public Class getItemClass() { + return IMarkupItem.class; + } + + @Override + public IMarkupItem newItem(Object value) { + MarkupLine item = toValue(value); + return IMarkupItem.valueOf(item); + } +} diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/markup/MarkupMultiline.java b/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/markup/MarkupMultiline.java index 205b0ff35..534621a1c 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/markup/MarkupMultiline.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/markup/MarkupMultiline.java @@ -1,96 +1,96 @@ -/* - * Portions of this software was developed by employees of the National Institute - * of Standards and Technology (NIST), an agency of the Federal Government and is - * being made available as a public service. Pursuant to title 17 United States - * Code Section 105, works of NIST employees are not subject to copyright - * protection in the United States. This software may be subject to foreign - * copyright. Permission in the United States and in foreign countries, to the - * extent that NIST may hold copyright, to use, copy, modify, create derivative - * works, and distribute this software and its documentation without fee is hereby - * granted on a non-exclusive basis, provided that this notice and disclaimer - * of warranty appears in all copies. - * - * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER - * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY - * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM - * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE - * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT - * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, - * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, - * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, - * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR - * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT - * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. - */ - -package gov.nist.secauto.metaschema.core.datatype.markup; - -import com.vladsch.flexmark.util.ast.Document; - -import gov.nist.secauto.metaschema.core.datatype.markup.flexmark.FlexmarkFactory; - -import edu.umd.cs.findbugs.annotations.NonNull; - -public class MarkupMultiline - extends AbstractMarkupString { - - @NonNull - private static final FlexmarkFactory FLEXMARK_FACTORY = FlexmarkFactory.instance(); - - /** - * Convert the provided HTML string into markup. - * - * @param html - * the HTML - * @return the multiline markup instance - */ - @NonNull - public static MarkupMultiline fromHtml(@NonNull String html) { - return new MarkupMultiline( - parseHtml( - html, - FLEXMARK_FACTORY.getFlexmarkHtmlConverter(), - FLEXMARK_FACTORY.getMarkdownParser())); - } - - /** - * Convert the provided markdown string into markup. - * - * @param markdown - * the markup - * @return the multiline markup instance - */ - @NonNull - public static MarkupMultiline fromMarkdown(@NonNull String markdown) { - return new MarkupMultiline( - parseMarkdown(markdown, FLEXMARK_FACTORY.getMarkdownParser())); - } - - /** - * Construct a new multiline markup instance. - * - * @param astNode - * the parsed markup AST - */ - public MarkupMultiline(@NonNull Document astNode) { - super(astNode); - } - - @Override - public FlexmarkFactory getFlexmarkFactory() { - return FLEXMARK_FACTORY; - } - - @Override - public MarkupMultiline copy() { - // TODO: find a way to do a deep copy - // this is a shallow copy that uses the same underlying Document object - return new MarkupMultiline(getDocument()); - } - - @Override - public boolean isBlock() { - return true; - } -} +/* + * Portions of this software was developed by employees of the National Institute + * of Standards and Technology (NIST), an agency of the Federal Government and is + * being made available as a public service. Pursuant to title 17 United States + * Code Section 105, works of NIST employees are not subject to copyright + * protection in the United States. This software may be subject to foreign + * copyright. Permission in the United States and in foreign countries, to the + * extent that NIST may hold copyright, to use, copy, modify, create derivative + * works, and distribute this software and its documentation without fee is hereby + * granted on a non-exclusive basis, provided that this notice and disclaimer + * of warranty appears in all copies. + * + * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER + * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY + * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM + * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE + * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT + * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, + * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, + * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, + * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR + * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT + * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. + */ + +package gov.nist.secauto.metaschema.core.datatype.markup; + +import com.vladsch.flexmark.util.ast.Document; + +import gov.nist.secauto.metaschema.core.datatype.markup.flexmark.FlexmarkFactory; + +import edu.umd.cs.findbugs.annotations.NonNull; + +public class MarkupMultiline + extends AbstractMarkupString { + + @NonNull + private static final FlexmarkFactory FLEXMARK_FACTORY = FlexmarkFactory.instance(); + + /** + * Convert the provided HTML string into markup. + * + * @param html + * the HTML + * @return the multiline markup instance + */ + @NonNull + public static MarkupMultiline fromHtml(@NonNull String html) { + return new MarkupMultiline( + parseHtml( + html, + FLEXMARK_FACTORY.getFlexmarkHtmlConverter(), + FLEXMARK_FACTORY.getMarkdownParser())); + } + + /** + * Convert the provided markdown string into markup. + * + * @param markdown + * the markup + * @return the multiline markup instance + */ + @NonNull + public static MarkupMultiline fromMarkdown(@NonNull String markdown) { + return new MarkupMultiline( + parseMarkdown(markdown, FLEXMARK_FACTORY.getMarkdownParser())); + } + + /** + * Construct a new multiline markup instance. + * + * @param astNode + * the parsed markup AST + */ + public MarkupMultiline(@NonNull Document astNode) { + super(astNode); + } + + @Override + public FlexmarkFactory getFlexmarkFactory() { + return FLEXMARK_FACTORY; + } + + @Override + public MarkupMultiline copy() { + // TODO: find a way to do a deep copy + // this is a shallow copy that uses the same underlying Document object + return new MarkupMultiline(getDocument()); + } + + @Override + public boolean isBlock() { + return true; + } +} diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/markup/MarkupMultilineAdapter.java b/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/markup/MarkupMultilineAdapter.java index 3c907fa90..4bb3e6c4a 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/markup/MarkupMultilineAdapter.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/markup/MarkupMultilineAdapter.java @@ -1,111 +1,111 @@ -/* - * Portions of this software was developed by employees of the National Institute - * of Standards and Technology (NIST), an agency of the Federal Government and is - * being made available as a public service. Pursuant to title 17 United States - * Code Section 105, works of NIST employees are not subject to copyright - * protection in the United States. This software may be subject to foreign - * copyright. Permission in the United States and in foreign countries, to the - * extent that NIST may hold copyright, to use, copy, modify, create derivative - * works, and distribute this software and its documentation without fee is hereby - * granted on a non-exclusive basis, provided that this notice and disclaimer - * of warranty appears in all copies. - * - * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER - * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY - * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM - * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE - * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT - * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, - * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, - * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, - * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR - * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT - * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. - */ - -package gov.nist.secauto.metaschema.core.datatype.markup; - -import com.fasterxml.jackson.core.JsonParser; - -import gov.nist.secauto.metaschema.core.datatype.markup.flexmark.XmlMarkupParser; -import gov.nist.secauto.metaschema.core.metapath.item.atomic.IMarkupItem; -import gov.nist.secauto.metaschema.core.util.ObjectUtils; - -import org.codehaus.stax2.XMLEventReader2; - -import java.io.IOException; -import java.util.List; - -import javax.xml.namespace.QName; -import javax.xml.stream.XMLStreamException; - -import edu.umd.cs.findbugs.annotations.NonNull; - -public class MarkupMultilineAdapter - extends AbstractMarkupAdapter { - @NonNull - private static final List NAMES = ObjectUtils.notNull( - List.of("markup-multiline")); - - MarkupMultilineAdapter() { - super(MarkupMultiline.class); - } - - @Override - public List getNames() { - return NAMES; - } - - @Override - public boolean isUnrappedValueAllowedInXml() { - return true; - } - - /** - * Parse a line of Markdown. - */ - @Override - public MarkupMultiline parse(String value) { - return MarkupMultiline.fromMarkdown(value); - } - - @SuppressWarnings("null") - @Override - public MarkupMultiline parse(XMLEventReader2 eventReader) throws IOException { - try { - return XmlMarkupParser.instance().parseMarkupMultiline(eventReader); - } catch (XMLStreamException ex) { - throw new IOException(ex); - } - } - - @Override - public MarkupMultiline parse(JsonParser parser) throws IOException { - @SuppressWarnings("null") MarkupMultiline retval = parse(parser.getValueAsString()); - // skip past value - parser.nextToken(); - return retval; - } - - @Override - public boolean canHandleQName(QName nextQName) { - return true; - } - - @Override - public String getDefaultJsonValueKey() { - return "PROSE"; - } - - @Override - public Class getItemClass() { - return IMarkupItem.class; - } - - @Override - public IMarkupItem newItem(Object value) { - MarkupMultiline item = toValue(value); - return IMarkupItem.valueOf(item); - } -} +/* + * Portions of this software was developed by employees of the National Institute + * of Standards and Technology (NIST), an agency of the Federal Government and is + * being made available as a public service. Pursuant to title 17 United States + * Code Section 105, works of NIST employees are not subject to copyright + * protection in the United States. This software may be subject to foreign + * copyright. Permission in the United States and in foreign countries, to the + * extent that NIST may hold copyright, to use, copy, modify, create derivative + * works, and distribute this software and its documentation without fee is hereby + * granted on a non-exclusive basis, provided that this notice and disclaimer + * of warranty appears in all copies. + * + * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER + * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY + * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM + * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE + * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT + * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, + * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, + * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, + * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR + * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT + * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. + */ + +package gov.nist.secauto.metaschema.core.datatype.markup; + +import com.fasterxml.jackson.core.JsonParser; + +import gov.nist.secauto.metaschema.core.datatype.markup.flexmark.XmlMarkupParser; +import gov.nist.secauto.metaschema.core.metapath.item.atomic.IMarkupItem; +import gov.nist.secauto.metaschema.core.util.ObjectUtils; + +import org.codehaus.stax2.XMLEventReader2; + +import java.io.IOException; +import java.util.List; + +import javax.xml.namespace.QName; +import javax.xml.stream.XMLStreamException; + +import edu.umd.cs.findbugs.annotations.NonNull; + +public class MarkupMultilineAdapter + extends AbstractMarkupAdapter { + @NonNull + private static final List NAMES = ObjectUtils.notNull( + List.of("markup-multiline")); + + MarkupMultilineAdapter() { + super(MarkupMultiline.class); + } + + @Override + public List getNames() { + return NAMES; + } + + @Override + public boolean isUnrappedValueAllowedInXml() { + return true; + } + + /** + * Parse a line of Markdown. + */ + @Override + public MarkupMultiline parse(String value) { + return MarkupMultiline.fromMarkdown(value); + } + + @SuppressWarnings("null") + @Override + public MarkupMultiline parse(XMLEventReader2 eventReader) throws IOException { + try { + return XmlMarkupParser.instance().parseMarkupMultiline(eventReader); + } catch (XMLStreamException ex) { + throw new IOException(ex); + } + } + + @Override + public MarkupMultiline parse(JsonParser parser) throws IOException { + @SuppressWarnings("null") MarkupMultiline retval = parse(parser.getValueAsString()); + // skip past value + parser.nextToken(); + return retval; + } + + @Override + public boolean canHandleQName(QName nextQName) { + return true; + } + + @Override + public String getDefaultJsonValueKey() { + return "PROSE"; + } + + @Override + public Class getItemClass() { + return IMarkupItem.class; + } + + @Override + public IMarkupItem newItem(Object value) { + MarkupMultiline item = toValue(value); + return IMarkupItem.valueOf(item); + } +} diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/markup/flexmark/AstCollectingVisitor.java b/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/markup/flexmark/AstCollectingVisitor.java index 2d51a98e2..be57b01bc 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/markup/flexmark/AstCollectingVisitor.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/markup/flexmark/AstCollectingVisitor.java @@ -1,88 +1,88 @@ -/* - * Portions of this software was developed by employees of the National Institute - * of Standards and Technology (NIST), an agency of the Federal Government and is - * being made available as a public service. Pursuant to title 17 United States - * Code Section 105, works of NIST employees are not subject to copyright - * protection in the United States. This software may be subject to foreign - * copyright. Permission in the United States and in foreign countries, to the - * extent that NIST may hold copyright, to use, copy, modify, create derivative - * works, and distribute this software and its documentation without fee is hereby - * granted on a non-exclusive basis, provided that this notice and disclaimer - * of warranty appears in all copies. - * - * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER - * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY - * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM - * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE - * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT - * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, - * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, - * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, - * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR - * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT - * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. - */ - -package gov.nist.secauto.metaschema.core.datatype.markup.flexmark; - -import com.vladsch.flexmark.util.ast.Node; -import com.vladsch.flexmark.util.ast.NodeVisitorBase; - -import gov.nist.secauto.metaschema.core.util.ObjectUtils; - -import edu.umd.cs.findbugs.annotations.NonNull; - -public final class AstCollectingVisitor - extends NodeVisitorBase { - public static final String EOL = "\n"; - - @SuppressWarnings("PMD.AvoidStringBufferField") // short lived - private final StringBuilder strBuilder; - private int indent; // 0; - - private AstCollectingVisitor(@NonNull StringBuilder strBuilder) { - this.strBuilder = strBuilder; - indent = 0; - } - - /** - * Generate a string representation of an AST. - * - * @param node - * the branch of the tree to visualize - * @return the string representation of the AST. - */ - @NonNull - public static String asString(@NonNull Node node) { - StringBuilder builder = new StringBuilder(); - AstCollectingVisitor visitor = new AstCollectingVisitor(builder); - visitor.collect(node); - return ObjectUtils.notNull(builder.toString()); - } - - private void appendIndent() { - for (int i = 0; i < indent * 2; i++) { - strBuilder.append(' '); - } - } - - private void collect(@NonNull Node node) { - visit(node); - } - - @Override - protected void visit(Node node) { - assert node != null; - appendIndent(); - node.astString(strBuilder, true); - strBuilder.append(EOL); - indent++; - - try { - super.visitChildren(node); - } finally { - indent--; - } - } -} +/* + * Portions of this software was developed by employees of the National Institute + * of Standards and Technology (NIST), an agency of the Federal Government and is + * being made available as a public service. Pursuant to title 17 United States + * Code Section 105, works of NIST employees are not subject to copyright + * protection in the United States. This software may be subject to foreign + * copyright. Permission in the United States and in foreign countries, to the + * extent that NIST may hold copyright, to use, copy, modify, create derivative + * works, and distribute this software and its documentation without fee is hereby + * granted on a non-exclusive basis, provided that this notice and disclaimer + * of warranty appears in all copies. + * + * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER + * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY + * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM + * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE + * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT + * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, + * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, + * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, + * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR + * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT + * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. + */ + +package gov.nist.secauto.metaschema.core.datatype.markup.flexmark; + +import com.vladsch.flexmark.util.ast.Node; +import com.vladsch.flexmark.util.ast.NodeVisitorBase; + +import gov.nist.secauto.metaschema.core.util.ObjectUtils; + +import edu.umd.cs.findbugs.annotations.NonNull; + +public final class AstCollectingVisitor + extends NodeVisitorBase { + public static final String EOL = "\n"; + + @SuppressWarnings("PMD.AvoidStringBufferField") // short lived + private final StringBuilder strBuilder; + private int indent; // 0; + + private AstCollectingVisitor(@NonNull StringBuilder strBuilder) { + this.strBuilder = strBuilder; + indent = 0; + } + + /** + * Generate a string representation of an AST. + * + * @param node + * the branch of the tree to visualize + * @return the string representation of the AST. + */ + @NonNull + public static String asString(@NonNull Node node) { + StringBuilder builder = new StringBuilder(); + AstCollectingVisitor visitor = new AstCollectingVisitor(builder); + visitor.collect(node); + return ObjectUtils.notNull(builder.toString()); + } + + private void appendIndent() { + for (int i = 0; i < indent * 2; i++) { + strBuilder.append(' '); + } + } + + private void collect(@NonNull Node node) { + visit(node); + } + + @Override + protected void visit(Node node) { + assert node != null; + appendIndent(); + node.astString(strBuilder, true); + strBuilder.append(EOL); + indent++; + + try { + super.visitChildren(node); + } finally { + indent--; + } + } +} diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/markup/flexmark/FlexmarkFactory.java b/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/markup/flexmark/FlexmarkFactory.java index c48378817..01b3ddbc2 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/markup/flexmark/FlexmarkFactory.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/markup/flexmark/FlexmarkFactory.java @@ -1,103 +1,103 @@ -/* - * Portions of this software was developed by employees of the National Institute - * of Standards and Technology (NIST), an agency of the Federal Government and is - * being made available as a public service. Pursuant to title 17 United States - * Code Section 105, works of NIST employees are not subject to copyright - * protection in the United States. This software may be subject to foreign - * copyright. Permission in the United States and in foreign countries, to the - * extent that NIST may hold copyright, to use, copy, modify, create derivative - * works, and distribute this software and its documentation without fee is hereby - * granted on a non-exclusive basis, provided that this notice and disclaimer - * of warranty appears in all copies. - * - * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER - * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY - * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM - * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE - * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT - * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, - * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, - * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, - * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR - * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT - * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. - */ - -package gov.nist.secauto.metaschema.core.datatype.markup.flexmark; - -import com.vladsch.flexmark.formatter.Formatter; -import com.vladsch.flexmark.html.HtmlRenderer; -import com.vladsch.flexmark.html2md.converter.FlexmarkHtmlConverter; -import com.vladsch.flexmark.parser.ListOptions; -import com.vladsch.flexmark.parser.Parser; -import com.vladsch.flexmark.util.data.DataHolder; - -import edu.umd.cs.findbugs.annotations.NonNull; - -@SuppressWarnings("PMD.DataClass") -public final class FlexmarkFactory { - @NonNull - private static final FlexmarkFactory SINGLETON = new FlexmarkFactory(); - @NonNull - private final Parser markdownParser; - @NonNull - private final HtmlRenderer htmlRenderer; - @NonNull - private final Formatter formatter; - @NonNull - private final FlexmarkHtmlConverter htmlConverter; - @NonNull - final ListOptions listOptions; - - @SuppressWarnings("PMD.AvoidSynchronizedAtMethodLevel") - @NonNull - public static synchronized FlexmarkFactory instance() { - return SINGLETON; - } - - @NonNull - public static FlexmarkFactory newInstance(@NonNull DataHolder config) { - return new FlexmarkFactory(config); - } - - private FlexmarkFactory() { - this(FlexmarkConfiguration.FLEXMARK_CONFIG); - } - - @SuppressWarnings("null") - private FlexmarkFactory(@NonNull DataHolder config) { - this.markdownParser = Parser.builder(config) - .customDelimiterProcessor(new FixedEmphasisDelimiterProcessor(Parser.STRONG_WRAPS_EMPHASIS.get(config))) - .build(); - this.htmlRenderer = HtmlRenderer.builder(config).build(); - this.formatter = Formatter.builder(config).build(); - this.htmlConverter = FlexmarkHtmlConverter.builder(config).build(); - this.listOptions = ListOptions.get(config); - } - - @NonNull - public ListOptions getListOptions() { - return listOptions; - } - - @NonNull - public Parser getMarkdownParser() { - return markdownParser; - } - - @NonNull - public HtmlRenderer getHtmlRenderer() { - return htmlRenderer; - } - - @NonNull - public Formatter getFormatter() { - return formatter; - } - - @NonNull - public FlexmarkHtmlConverter getFlexmarkHtmlConverter() { - return htmlConverter; - } -} +/* + * Portions of this software was developed by employees of the National Institute + * of Standards and Technology (NIST), an agency of the Federal Government and is + * being made available as a public service. Pursuant to title 17 United States + * Code Section 105, works of NIST employees are not subject to copyright + * protection in the United States. This software may be subject to foreign + * copyright. Permission in the United States and in foreign countries, to the + * extent that NIST may hold copyright, to use, copy, modify, create derivative + * works, and distribute this software and its documentation without fee is hereby + * granted on a non-exclusive basis, provided that this notice and disclaimer + * of warranty appears in all copies. + * + * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER + * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY + * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM + * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE + * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT + * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, + * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, + * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, + * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR + * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT + * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. + */ + +package gov.nist.secauto.metaschema.core.datatype.markup.flexmark; + +import com.vladsch.flexmark.formatter.Formatter; +import com.vladsch.flexmark.html.HtmlRenderer; +import com.vladsch.flexmark.html2md.converter.FlexmarkHtmlConverter; +import com.vladsch.flexmark.parser.ListOptions; +import com.vladsch.flexmark.parser.Parser; +import com.vladsch.flexmark.util.data.DataHolder; + +import edu.umd.cs.findbugs.annotations.NonNull; + +@SuppressWarnings("PMD.DataClass") +public final class FlexmarkFactory { + @NonNull + private static final FlexmarkFactory SINGLETON = new FlexmarkFactory(); + @NonNull + private final Parser markdownParser; + @NonNull + private final HtmlRenderer htmlRenderer; + @NonNull + private final Formatter formatter; + @NonNull + private final FlexmarkHtmlConverter htmlConverter; + @NonNull + final ListOptions listOptions; + + @SuppressWarnings("PMD.AvoidSynchronizedAtMethodLevel") + @NonNull + public static synchronized FlexmarkFactory instance() { + return SINGLETON; + } + + @NonNull + public static FlexmarkFactory newInstance(@NonNull DataHolder config) { + return new FlexmarkFactory(config); + } + + private FlexmarkFactory() { + this(FlexmarkConfiguration.FLEXMARK_CONFIG); + } + + @SuppressWarnings("null") + private FlexmarkFactory(@NonNull DataHolder config) { + this.markdownParser = Parser.builder(config) + .customDelimiterProcessor(new FixedEmphasisDelimiterProcessor(Parser.STRONG_WRAPS_EMPHASIS.get(config))) + .build(); + this.htmlRenderer = HtmlRenderer.builder(config).build(); + this.formatter = Formatter.builder(config).build(); + this.htmlConverter = FlexmarkHtmlConverter.builder(config).build(); + this.listOptions = ListOptions.get(config); + } + + @NonNull + public ListOptions getListOptions() { + return listOptions; + } + + @NonNull + public Parser getMarkdownParser() { + return markdownParser; + } + + @NonNull + public HtmlRenderer getHtmlRenderer() { + return htmlRenderer; + } + + @NonNull + public Formatter getFormatter() { + return formatter; + } + + @NonNull + public FlexmarkHtmlConverter getFlexmarkHtmlConverter() { + return htmlConverter; + } +} diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/markup/flexmark/InsertAnchorExtension.java b/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/markup/flexmark/InsertAnchorExtension.java index 8300faa5a..36e422b17 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/markup/flexmark/InsertAnchorExtension.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/markup/flexmark/InsertAnchorExtension.java @@ -1,348 +1,348 @@ -/* - * Portions of this software was developed by employees of the National Institute - * of Standards and Technology (NIST), an agency of the Federal Government and is - * being made available as a public service. Pursuant to title 17 United States - * Code Section 105, works of NIST employees are not subject to copyright - * protection in the United States. This software may be subject to foreign - * copyright. Permission in the United States and in foreign countries, to the - * extent that NIST may hold copyright, to use, copy, modify, create derivative - * works, and distribute this software and its documentation without fee is hereby - * granted on a non-exclusive basis, provided that this notice and disclaimer - * of warranty appears in all copies. - * - * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER - * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY - * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM - * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE - * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT - * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, - * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, - * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, - * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR - * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT - * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. - */ - -package gov.nist.secauto.metaschema.core.datatype.markup.flexmark; // NOPMD AST processor - -import com.vladsch.flexmark.formatter.Formatter; -import com.vladsch.flexmark.formatter.MarkdownWriter; -import com.vladsch.flexmark.formatter.NodeFormatter; -import com.vladsch.flexmark.formatter.NodeFormatterContext; -import com.vladsch.flexmark.formatter.NodeFormatterFactory; -import com.vladsch.flexmark.formatter.NodeFormattingHandler; -import com.vladsch.flexmark.html.HtmlRenderer; -import com.vladsch.flexmark.html.HtmlWriter; -import com.vladsch.flexmark.html.renderer.NodeRenderer; -import com.vladsch.flexmark.html.renderer.NodeRendererContext; -import com.vladsch.flexmark.html.renderer.NodeRendererFactory; -import com.vladsch.flexmark.html.renderer.NodeRenderingHandler; -import com.vladsch.flexmark.html2md.converter.FlexmarkHtmlConverter; -import com.vladsch.flexmark.html2md.converter.HtmlMarkdownWriter; -import com.vladsch.flexmark.html2md.converter.HtmlNodeConverterContext; -import com.vladsch.flexmark.html2md.converter.HtmlNodeRenderer; -import com.vladsch.flexmark.html2md.converter.HtmlNodeRendererFactory; -import com.vladsch.flexmark.html2md.converter.HtmlNodeRendererHandler; -import com.vladsch.flexmark.parser.InlineParser; -import com.vladsch.flexmark.parser.InlineParserExtension; -import com.vladsch.flexmark.parser.InlineParserExtensionFactory; -import com.vladsch.flexmark.parser.LightInlineParser; -import com.vladsch.flexmark.parser.Parser; -import com.vladsch.flexmark.util.ast.Node; -import com.vladsch.flexmark.util.data.DataHolder; -import com.vladsch.flexmark.util.data.DataKey; -import com.vladsch.flexmark.util.data.MutableDataHolder; -import com.vladsch.flexmark.util.misc.Extension; -import com.vladsch.flexmark.util.sequence.BasedSequence; -import com.vladsch.flexmark.util.sequence.CharSubSequence; - -import org.jsoup.nodes.Element; - -import java.util.Collections; -import java.util.Set; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -import edu.umd.cs.findbugs.annotations.NonNull; - -public class InsertAnchorExtension - implements Parser.ParserExtension, HtmlRenderer.HtmlRendererExtension, - Formatter.FormatterExtension, FlexmarkHtmlConverter.HtmlConverterExtension { - public static final DataKey ENABLE_INLINE_INSERT_ANCHORS - = new DataKey<>("ENABLE_INLINE_INSERT_ANCHORS", true); - public static final DataKey ENABLE_RENDERING = new DataKey<>("ENABLE_RENDERING", true); - - public static Extension create() { - return new InsertAnchorExtension(); - } - - @Override - public void parserOptions(MutableDataHolder options) { - // do nothing - } - - @Override - public void rendererOptions(MutableDataHolder options) { - // do nothing - } - - @Override - public void extend(HtmlRenderer.Builder rendererBuilder, String rendererType) { - rendererBuilder.nodeRendererFactory(new InsertAnchorNodeRenderer.Factory()); - } - - @Override - public void extend(Parser.Builder parserBuilder) { - if (ENABLE_INLINE_INSERT_ANCHORS.get(parserBuilder)) { - parserBuilder.customInlineParserExtensionFactory(new InsertAnchorInlineParser.Factory()); - } - } - - @Override - public void extend(Formatter.Builder builder) { - builder.nodeFormatterFactory(new InsertAnchorFormatter.Factory()); - } - - @Override - public void extend(FlexmarkHtmlConverter.Builder builder) { - builder.htmlNodeRendererFactory(new InsertAnchorHtmlNodeRenderer.Factory()); - } - - private static class InsertAnchorOptions { - public final boolean enableInlineInsertAnchors; - public final boolean enableRendering; - - public InsertAnchorOptions(DataHolder options) { - enableInlineInsertAnchors = ENABLE_INLINE_INSERT_ANCHORS.get(options); - enableRendering = ENABLE_RENDERING.get(options); - } - } - - private static class InsertAnchorNodeRenderer implements NodeRenderer { - private final InsertAnchorOptions options; - - public InsertAnchorNodeRenderer(DataHolder options) { - this.options = new InsertAnchorOptions(options); - } - - @Override - public Set> getNodeRenderingHandlers() { - return Collections.singleton(new NodeRenderingHandler<>(InsertAnchorNode.class, this::render)); - } - - @SuppressWarnings("unused") - protected void render(InsertAnchorNode node, NodeRendererContext context, HtmlWriter html) { - if (options.enableRendering) { - html.attr("type", node.getType()).attr("id-ref", node.getIdReference()).withAttr().tagVoid("insert"); - } - } - - // @Override - // public Set> getNodeRenderingHandlers() { - // HashSet> set = new - // HashSet>(); - // set.add(new NodeRenderingHandler(Macro.class, new - // CustomNodeRenderer() { - // @Override - // public void render(Macro node, NodeRendererContext context, HtmlWriter html) - // { - // MacroNodeRenderer.this.render(node, context, html); } - // })); - public static class Factory implements NodeRendererFactory { - - @Override - public NodeRenderer apply(DataHolder options) { - return new InsertAnchorNodeRenderer(options); - } - - } - - } - - private static class InsertAnchorInlineParser implements InlineParserExtension { - private static final Pattern PATTERN = Pattern.compile("\\{\\{\\s*insert:\\s*([^\\s]+),\\s*([^\\s]+)\\s*\\}\\}"); - - public InsertAnchorInlineParser(@SuppressWarnings("unused") LightInlineParser inlineParser) { - // do nothing - } - - @Override - public void finalizeDocument(InlineParser inlineParser) { - // do nothing - } - - @Override - public void finalizeBlock(InlineParser inlineParser) { - // do nothing - } - - @Override - public boolean parse(LightInlineParser inlineParser) { - if (inlineParser.peek() == '{') { - BasedSequence input = inlineParser.getInput(); - Matcher matcher = inlineParser.matcher(PATTERN); - if (matcher != null) { - BasedSequence type = input.subSequence(matcher.start(1), matcher.end(1)); - BasedSequence idReference = input.subSequence(matcher.start(2), matcher.end(2)); - assert type != null; - assert idReference != null; - inlineParser.appendNode(new InsertAnchorNode(type, idReference)); - return true; // NOPMD - readability - } - } - return false; - } - - public static class Factory implements InlineParserExtensionFactory { - @Override - public Set> getAfterDependents() { - return Collections.emptySet(); - } - - @Override - public CharSequence getCharacters() { - return "{"; - } - - @Override - public Set> getBeforeDependents() { - return Collections.emptySet(); - } - - @Override - public InlineParserExtension apply(LightInlineParser lightInlineParser) { - return new InsertAnchorInlineParser(lightInlineParser); - } - - @Override - public boolean affectsGlobalScope() { - return false; - } - } - } - - private static class InsertAnchorFormatter implements NodeFormatter { - private final InsertAnchorOptions options; - - public InsertAnchorFormatter(DataHolder options) { - this.options = new InsertAnchorOptions(options); - } - - @Override - public Set> getNodeFormattingHandlers() { - return options.enableInlineInsertAnchors - ? Collections.singleton(new NodeFormattingHandler<>(InsertAnchorNode.class, this::render)) - : Collections.emptySet(); - } - - @SuppressWarnings("unused") - protected void render(InsertAnchorNode node, NodeFormatterContext context, MarkdownWriter markdown) { - if (options.enableRendering) { - markdown.append("{{ insert: "); - markdown.append(node.getType()); - markdown.append(", "); - markdown.append(node.getIdReference()); - markdown.append(" }}"); - } - } - - @Override - public Set> getNodeClasses() { - return Collections.singleton(InsertAnchorNode.class); - } - - public static class Factory implements NodeFormatterFactory { - - @Override - public NodeFormatter create(DataHolder options) { - return new InsertAnchorFormatter(options); - } - - } - } - - private static class InsertAnchorHtmlNodeRenderer implements HtmlNodeRenderer { - private final InsertAnchorOptions options; - - public InsertAnchorHtmlNodeRenderer(DataHolder options) { - this.options = new InsertAnchorOptions(options); - } - - @Override - public Set> getHtmlNodeRendererHandlers() { - return options.enableInlineInsertAnchors - ? Collections.singleton(new HtmlNodeRendererHandler<>("insert", Element.class, this::processInsert)) - : Collections.emptySet(); - } - - private void processInsert( // NOPMD used as lambda - Element node, - @SuppressWarnings("unused") HtmlNodeConverterContext context, - HtmlMarkdownWriter out) { - - String type = node.attr("type"); - String idRef = node.attr("id-ref"); - - out.append("{{ insert: "); - out.append(type); - out.append(", "); - out.append(idRef); - out.append(" }}"); - } - - public static class Factory implements HtmlNodeRendererFactory { - - @Override - public HtmlNodeRenderer apply(DataHolder options) { - return new InsertAnchorHtmlNodeRenderer(options); - } - } - } - - public static class InsertAnchorNode - extends Node { - - @NonNull - private BasedSequence type; - @NonNull - private BasedSequence idReference; - - @SuppressWarnings("null") - public InsertAnchorNode(@NonNull String type, @NonNull String idReference) { - this(CharSubSequence.of(type), CharSubSequence.of(idReference)); - } - - public InsertAnchorNode(@NonNull BasedSequence type, @NonNull BasedSequence idReference) { - this.type = type; - this.idReference = idReference; - } - - @NonNull - public BasedSequence getType() { - return type; - } - - @NonNull - public BasedSequence getIdReference() { - return idReference; - } - - public void setIdReference(@NonNull BasedSequence value) { - this.idReference = value; - } - - @Override - @NonNull - public BasedSequence[] getSegments() { - @NonNull BasedSequence[] retval = { getType(), getIdReference() }; - return retval; - } - - @Override - public void getAstExtra(StringBuilder out) { - segmentSpanChars(out, getType(), "type"); - segmentSpanChars(out, getIdReference(), "id-ref"); - } - } -} +/* + * Portions of this software was developed by employees of the National Institute + * of Standards and Technology (NIST), an agency of the Federal Government and is + * being made available as a public service. Pursuant to title 17 United States + * Code Section 105, works of NIST employees are not subject to copyright + * protection in the United States. This software may be subject to foreign + * copyright. Permission in the United States and in foreign countries, to the + * extent that NIST may hold copyright, to use, copy, modify, create derivative + * works, and distribute this software and its documentation without fee is hereby + * granted on a non-exclusive basis, provided that this notice and disclaimer + * of warranty appears in all copies. + * + * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER + * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY + * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM + * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE + * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT + * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, + * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, + * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, + * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR + * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT + * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. + */ + +package gov.nist.secauto.metaschema.core.datatype.markup.flexmark; // NOPMD AST processor + +import com.vladsch.flexmark.formatter.Formatter; +import com.vladsch.flexmark.formatter.MarkdownWriter; +import com.vladsch.flexmark.formatter.NodeFormatter; +import com.vladsch.flexmark.formatter.NodeFormatterContext; +import com.vladsch.flexmark.formatter.NodeFormatterFactory; +import com.vladsch.flexmark.formatter.NodeFormattingHandler; +import com.vladsch.flexmark.html.HtmlRenderer; +import com.vladsch.flexmark.html.HtmlWriter; +import com.vladsch.flexmark.html.renderer.NodeRenderer; +import com.vladsch.flexmark.html.renderer.NodeRendererContext; +import com.vladsch.flexmark.html.renderer.NodeRendererFactory; +import com.vladsch.flexmark.html.renderer.NodeRenderingHandler; +import com.vladsch.flexmark.html2md.converter.FlexmarkHtmlConverter; +import com.vladsch.flexmark.html2md.converter.HtmlMarkdownWriter; +import com.vladsch.flexmark.html2md.converter.HtmlNodeConverterContext; +import com.vladsch.flexmark.html2md.converter.HtmlNodeRenderer; +import com.vladsch.flexmark.html2md.converter.HtmlNodeRendererFactory; +import com.vladsch.flexmark.html2md.converter.HtmlNodeRendererHandler; +import com.vladsch.flexmark.parser.InlineParser; +import com.vladsch.flexmark.parser.InlineParserExtension; +import com.vladsch.flexmark.parser.InlineParserExtensionFactory; +import com.vladsch.flexmark.parser.LightInlineParser; +import com.vladsch.flexmark.parser.Parser; +import com.vladsch.flexmark.util.ast.Node; +import com.vladsch.flexmark.util.data.DataHolder; +import com.vladsch.flexmark.util.data.DataKey; +import com.vladsch.flexmark.util.data.MutableDataHolder; +import com.vladsch.flexmark.util.misc.Extension; +import com.vladsch.flexmark.util.sequence.BasedSequence; +import com.vladsch.flexmark.util.sequence.CharSubSequence; + +import org.jsoup.nodes.Element; + +import java.util.Collections; +import java.util.Set; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import edu.umd.cs.findbugs.annotations.NonNull; + +public class InsertAnchorExtension + implements Parser.ParserExtension, HtmlRenderer.HtmlRendererExtension, + Formatter.FormatterExtension, FlexmarkHtmlConverter.HtmlConverterExtension { + public static final DataKey ENABLE_INLINE_INSERT_ANCHORS + = new DataKey<>("ENABLE_INLINE_INSERT_ANCHORS", true); + public static final DataKey ENABLE_RENDERING = new DataKey<>("ENABLE_RENDERING", true); + + public static Extension create() { + return new InsertAnchorExtension(); + } + + @Override + public void parserOptions(MutableDataHolder options) { + // do nothing + } + + @Override + public void rendererOptions(MutableDataHolder options) { + // do nothing + } + + @Override + public void extend(HtmlRenderer.Builder rendererBuilder, String rendererType) { + rendererBuilder.nodeRendererFactory(new InsertAnchorNodeRenderer.Factory()); + } + + @Override + public void extend(Parser.Builder parserBuilder) { + if (ENABLE_INLINE_INSERT_ANCHORS.get(parserBuilder)) { + parserBuilder.customInlineParserExtensionFactory(new InsertAnchorInlineParser.Factory()); + } + } + + @Override + public void extend(Formatter.Builder builder) { + builder.nodeFormatterFactory(new InsertAnchorFormatter.Factory()); + } + + @Override + public void extend(FlexmarkHtmlConverter.Builder builder) { + builder.htmlNodeRendererFactory(new InsertAnchorHtmlNodeRenderer.Factory()); + } + + private static class InsertAnchorOptions { + public final boolean enableInlineInsertAnchors; + public final boolean enableRendering; + + public InsertAnchorOptions(DataHolder options) { + enableInlineInsertAnchors = ENABLE_INLINE_INSERT_ANCHORS.get(options); + enableRendering = ENABLE_RENDERING.get(options); + } + } + + private static class InsertAnchorNodeRenderer implements NodeRenderer { + private final InsertAnchorOptions options; + + public InsertAnchorNodeRenderer(DataHolder options) { + this.options = new InsertAnchorOptions(options); + } + + @Override + public Set> getNodeRenderingHandlers() { + return Collections.singleton(new NodeRenderingHandler<>(InsertAnchorNode.class, this::render)); + } + + @SuppressWarnings("unused") + protected void render(InsertAnchorNode node, NodeRendererContext context, HtmlWriter html) { + if (options.enableRendering) { + html.attr("type", node.getType()).attr("id-ref", node.getIdReference()).withAttr().tagVoid("insert"); + } + } + + // @Override + // public Set> getNodeRenderingHandlers() { + // HashSet> set = new + // HashSet>(); + // set.add(new NodeRenderingHandler(Macro.class, new + // CustomNodeRenderer() { + // @Override + // public void render(Macro node, NodeRendererContext context, HtmlWriter html) + // { + // MacroNodeRenderer.this.render(node, context, html); } + // })); + public static class Factory implements NodeRendererFactory { + + @Override + public NodeRenderer apply(DataHolder options) { + return new InsertAnchorNodeRenderer(options); + } + + } + + } + + private static class InsertAnchorInlineParser implements InlineParserExtension { + private static final Pattern PATTERN = Pattern.compile("\\{\\{\\s*insert:\\s*([^\\s]+),\\s*([^\\s]+)\\s*\\}\\}"); + + public InsertAnchorInlineParser(@SuppressWarnings("unused") LightInlineParser inlineParser) { + // do nothing + } + + @Override + public void finalizeDocument(InlineParser inlineParser) { + // do nothing + } + + @Override + public void finalizeBlock(InlineParser inlineParser) { + // do nothing + } + + @Override + public boolean parse(LightInlineParser inlineParser) { + if (inlineParser.peek() == '{') { + BasedSequence input = inlineParser.getInput(); + Matcher matcher = inlineParser.matcher(PATTERN); + if (matcher != null) { + BasedSequence type = input.subSequence(matcher.start(1), matcher.end(1)); + BasedSequence idReference = input.subSequence(matcher.start(2), matcher.end(2)); + assert type != null; + assert idReference != null; + inlineParser.appendNode(new InsertAnchorNode(type, idReference)); + return true; // NOPMD - readability + } + } + return false; + } + + public static class Factory implements InlineParserExtensionFactory { + @Override + public Set> getAfterDependents() { + return Collections.emptySet(); + } + + @Override + public CharSequence getCharacters() { + return "{"; + } + + @Override + public Set> getBeforeDependents() { + return Collections.emptySet(); + } + + @Override + public InlineParserExtension apply(LightInlineParser lightInlineParser) { + return new InsertAnchorInlineParser(lightInlineParser); + } + + @Override + public boolean affectsGlobalScope() { + return false; + } + } + } + + private static class InsertAnchorFormatter implements NodeFormatter { + private final InsertAnchorOptions options; + + public InsertAnchorFormatter(DataHolder options) { + this.options = new InsertAnchorOptions(options); + } + + @Override + public Set> getNodeFormattingHandlers() { + return options.enableInlineInsertAnchors + ? Collections.singleton(new NodeFormattingHandler<>(InsertAnchorNode.class, this::render)) + : Collections.emptySet(); + } + + @SuppressWarnings("unused") + protected void render(InsertAnchorNode node, NodeFormatterContext context, MarkdownWriter markdown) { + if (options.enableRendering) { + markdown.append("{{ insert: "); + markdown.append(node.getType()); + markdown.append(", "); + markdown.append(node.getIdReference()); + markdown.append(" }}"); + } + } + + @Override + public Set> getNodeClasses() { + return Collections.singleton(InsertAnchorNode.class); + } + + public static class Factory implements NodeFormatterFactory { + + @Override + public NodeFormatter create(DataHolder options) { + return new InsertAnchorFormatter(options); + } + + } + } + + private static class InsertAnchorHtmlNodeRenderer implements HtmlNodeRenderer { + private final InsertAnchorOptions options; + + public InsertAnchorHtmlNodeRenderer(DataHolder options) { + this.options = new InsertAnchorOptions(options); + } + + @Override + public Set> getHtmlNodeRendererHandlers() { + return options.enableInlineInsertAnchors + ? Collections.singleton(new HtmlNodeRendererHandler<>("insert", Element.class, this::processInsert)) + : Collections.emptySet(); + } + + private void processInsert( // NOPMD used as lambda + Element node, + @SuppressWarnings("unused") HtmlNodeConverterContext context, + HtmlMarkdownWriter out) { + + String type = node.attr("type"); + String idRef = node.attr("id-ref"); + + out.append("{{ insert: "); + out.append(type); + out.append(", "); + out.append(idRef); + out.append(" }}"); + } + + public static class Factory implements HtmlNodeRendererFactory { + + @Override + public HtmlNodeRenderer apply(DataHolder options) { + return new InsertAnchorHtmlNodeRenderer(options); + } + } + } + + public static class InsertAnchorNode + extends Node { + + @NonNull + private BasedSequence type; + @NonNull + private BasedSequence idReference; + + @SuppressWarnings("null") + public InsertAnchorNode(@NonNull String type, @NonNull String idReference) { + this(CharSubSequence.of(type), CharSubSequence.of(idReference)); + } + + public InsertAnchorNode(@NonNull BasedSequence type, @NonNull BasedSequence idReference) { + this.type = type; + this.idReference = idReference; + } + + @NonNull + public BasedSequence getType() { + return type; + } + + @NonNull + public BasedSequence getIdReference() { + return idReference; + } + + public void setIdReference(@NonNull BasedSequence value) { + this.idReference = value; + } + + @Override + @NonNull + public BasedSequence[] getSegments() { + @NonNull BasedSequence[] retval = { getType(), getIdReference() }; + return retval; + } + + @Override + public void getAstExtra(StringBuilder out) { + segmentSpanChars(out, getType(), "type"); + segmentSpanChars(out, getIdReference(), "id-ref"); + } + } +} diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/markup/flexmark/XmlMarkupParser.java b/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/markup/flexmark/XmlMarkupParser.java index cfe2b8959..1ba89bdc9 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/markup/flexmark/XmlMarkupParser.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/markup/flexmark/XmlMarkupParser.java @@ -1,247 +1,247 @@ -/* - * Portions of this software was developed by employees of the National Institute - * of Standards and Technology (NIST), an agency of the Federal Government and is - * being made available as a public service. Pursuant to title 17 United States - * Code Section 105, works of NIST employees are not subject to copyright - * protection in the United States. This software may be subject to foreign - * copyright. Permission in the United States and in foreign countries, to the - * extent that NIST may hold copyright, to use, copy, modify, create derivative - * works, and distribute this software and its documentation without fee is hereby - * granted on a non-exclusive basis, provided that this notice and disclaimer - * of warranty appears in all copies. - * - * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER - * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY - * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM - * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE - * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT - * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, - * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, - * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, - * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR - * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT - * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. - */ - -package gov.nist.secauto.metaschema.core.datatype.markup.flexmark; - -import com.vladsch.flexmark.util.sequence.Escaping; - -import gov.nist.secauto.metaschema.core.datatype.markup.MarkupLine; -import gov.nist.secauto.metaschema.core.datatype.markup.MarkupMultiline; -import gov.nist.secauto.metaschema.core.model.util.XmlEventUtil; -import gov.nist.secauto.metaschema.core.util.CollectionUtil; -import gov.nist.secauto.metaschema.core.util.ObjectUtils; - -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; -import org.codehaus.stax2.XMLEventReader2; - -import java.util.Set; - -import javax.xml.namespace.QName; -import javax.xml.stream.XMLStreamConstants; -import javax.xml.stream.XMLStreamException; -import javax.xml.stream.events.Attribute; -import javax.xml.stream.events.Characters; -import javax.xml.stream.events.StartElement; -import javax.xml.stream.events.XMLEvent; - -import edu.umd.cs.findbugs.annotations.NonNull; - -public final class XmlMarkupParser { - private static final Logger LOGGER = LogManager.getLogger(XmlMarkupParser.class); - - @NonNull - public static final Set BLOCK_ELEMENTS = ObjectUtils.notNull( - Set.of( - "h1", - "h2", - "h3", - "h4", - "h5", - "h6", - "ul", - "ol", - "pre", - "hr", - "blockquote", - "p", - "table", - "img")); - - @NonNull - private static final XmlMarkupParser SINGLETON = new XmlMarkupParser(); - - @SuppressWarnings("PMD.AvoidSynchronizedAtMethodLevel") - @NonNull - public static synchronized XmlMarkupParser instance() { - return SINGLETON; - } - - private XmlMarkupParser() { - // disable construction - } - - public MarkupLine parseMarkupline(XMLEventReader2 reader) throws XMLStreamException { // NOPMD - acceptable - StringBuilder buffer = new StringBuilder(); - parseContents(reader, null, buffer); - String html = buffer.toString().trim(); - return html.isEmpty() ? null : MarkupLine.fromHtml(html); - } - - public MarkupMultiline parseMarkupMultiline(XMLEventReader2 reader) throws XMLStreamException { - StringBuilder buffer = new StringBuilder(); - parseToString(reader, buffer); - String html = buffer.toString().trim(); - - if (LOGGER.isDebugEnabled()) { - LOGGER.debug("XML->HTML: {}", html); - } - return html.isEmpty() ? null : MarkupMultiline.fromHtml(html); - } - - private void parseToString(XMLEventReader2 reader, StringBuilder buffer) // NOPMD - acceptable - throws XMLStreamException { - // if (LOGGER.isDebugEnabled()) { - // LOGGER.debug("parseToString(enter): {}", - // XmlEventUtil.toString(reader.peek())); - // } - - outer: while (reader.hasNextEvent() && !reader.peek().isEndElement()) { - // skip whitespace before the next block element - XMLEvent nextEvent = XmlEventUtil.skipWhitespace(reader); - - // if (LOGGER.isDebugEnabled()) { - // LOGGER.debug("parseToString: {}", XmlEventUtil.toString(nextEvent)); - // } - - if (nextEvent.isStartElement()) { - StartElement start = nextEvent.asStartElement(); - QName name = start.getName(); - - // Note: the next element is not consumed. The called method is expected to - // consume it - if (BLOCK_ELEMENTS.contains(name.getLocalPart())) { - parseStartElement(reader, start, buffer); - - // the next event should be the event after the start's END_ELEMENT - // assert XmlEventUtil.isNextEventEndElement(reader, name) : - // XmlEventUtil.toString(reader.peek()); - } else { - // throw new IllegalStateException(); - // stop parsing on first unrecognized event - break outer; - } - } - // reader.nextEvent(); - - // skip whitespace before the next block element - XmlEventUtil.skipWhitespace(reader); - } - - // if (LOGGER.isDebugEnabled()) { - // LOGGER.debug("parseToString(exit): {}", reader.peek() != null ? - // XmlEventUtil.toString(reader.peek()) : ""); - // } - } - - private void parseStartElement(XMLEventReader2 reader, StartElement start, StringBuilder buffer) - throws XMLStreamException { - if (LOGGER.isDebugEnabled()) { - LOGGER.debug("parseStartElement(enter): {}", XmlEventUtil.toString(start)); - } - - // consume the start event - reader.nextEvent(); - - QName name = start.getName(); - buffer.append('<') - .append(name.getLocalPart()); - for (Attribute attribute : CollectionUtil.toIterable( - ObjectUtils.notNull(start.getAttributes()))) { - buffer - .append(' ') - .append(attribute.getName().getLocalPart()) - .append("=\"") - .append(attribute.getValue()) - .append('"'); - } - - XMLEvent next = reader.peek(); - if (next != null && next.isEndElement()) { - buffer.append("/>"); - // consume end element event - reader.nextEvent(); - } else { - buffer.append('>'); - - // parse until the start's END_ELEMENT is reached - parseContents(reader, start, buffer); - - buffer - .append("'); - - // the next event should be the start's END_ELEMENT - XmlEventUtil.assertNext(reader, XMLStreamConstants.END_ELEMENT, name); - - // consume the start's END_ELEMENT - reader.nextEvent(); - } - - if (LOGGER.isDebugEnabled()) { - LOGGER.debug("parseStartElement(exit): {}", reader.peek() != null ? XmlEventUtil.toString(reader.peek()) : ""); - } - } - - private void parseContents(XMLEventReader2 reader, StartElement start, StringBuilder buffer) - throws XMLStreamException { - // if (LOGGER.isDebugEnabled()) { - // LOGGER.debug("parseContents(enter): {}", - // XmlEventUtil.toString(reader.peek())); - // } - - XMLEvent event; - while (reader.hasNextEvent() && !(event = reader.peek()).isEndElement()) { - // // skip whitespace before the next list item - // event = XmlEventUtil.skipWhitespace(reader); - - // if (LOGGER.isDebugEnabled()) { - // LOGGER.debug("parseContents(before): {}", XmlEventUtil.toString(event)); - // } - - if (event.isStartElement()) { - StartElement nextStart = event.asStartElement(); - // QName nextName = nextStart.getName(); - parseStartElement(reader, nextStart, buffer); - - // if (LOGGER.isDebugEnabled()) { - // LOGGER.debug("parseContents(after): {}", - // XmlEventUtil.toString(reader.peek())); - // } - - // assert XmlEventUtil.isNextEventEndElement(reader, nextName) : - // XmlEventUtil.toString(reader.peek()); - - // reader.nextEvent(); - } else if (event.isCharacters()) { - Characters characters = event.asCharacters(); - buffer.append(Escaping.escapeHtml(characters.getData(), true)); - reader.nextEvent(); - } - } - - assert start == null - || XmlEventUtil.isEventEndElement(reader.peek(), ObjectUtils.notNull(start.getName())) : XmlEventUtil - .generateExpectedMessage(reader.peek(), XMLStreamConstants.END_ELEMENT, start.getName()); - - // if (LOGGER.isDebugEnabled()) { - // LOGGER.debug("parseContents(exit): {}", reader.peek() != null ? - // XmlEventUtil.toString(reader.peek()) : ""); - // } - } - -} +/* + * Portions of this software was developed by employees of the National Institute + * of Standards and Technology (NIST), an agency of the Federal Government and is + * being made available as a public service. Pursuant to title 17 United States + * Code Section 105, works of NIST employees are not subject to copyright + * protection in the United States. This software may be subject to foreign + * copyright. Permission in the United States and in foreign countries, to the + * extent that NIST may hold copyright, to use, copy, modify, create derivative + * works, and distribute this software and its documentation without fee is hereby + * granted on a non-exclusive basis, provided that this notice and disclaimer + * of warranty appears in all copies. + * + * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER + * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY + * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM + * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE + * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT + * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, + * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, + * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, + * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR + * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT + * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. + */ + +package gov.nist.secauto.metaschema.core.datatype.markup.flexmark; + +import com.vladsch.flexmark.util.sequence.Escaping; + +import gov.nist.secauto.metaschema.core.datatype.markup.MarkupLine; +import gov.nist.secauto.metaschema.core.datatype.markup.MarkupMultiline; +import gov.nist.secauto.metaschema.core.model.util.XmlEventUtil; +import gov.nist.secauto.metaschema.core.util.CollectionUtil; +import gov.nist.secauto.metaschema.core.util.ObjectUtils; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.codehaus.stax2.XMLEventReader2; + +import java.util.Set; + +import javax.xml.namespace.QName; +import javax.xml.stream.XMLStreamConstants; +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.events.Attribute; +import javax.xml.stream.events.Characters; +import javax.xml.stream.events.StartElement; +import javax.xml.stream.events.XMLEvent; + +import edu.umd.cs.findbugs.annotations.NonNull; + +public final class XmlMarkupParser { + private static final Logger LOGGER = LogManager.getLogger(XmlMarkupParser.class); + + @NonNull + public static final Set BLOCK_ELEMENTS = ObjectUtils.notNull( + Set.of( + "h1", + "h2", + "h3", + "h4", + "h5", + "h6", + "ul", + "ol", + "pre", + "hr", + "blockquote", + "p", + "table", + "img")); + + @NonNull + private static final XmlMarkupParser SINGLETON = new XmlMarkupParser(); + + @SuppressWarnings("PMD.AvoidSynchronizedAtMethodLevel") + @NonNull + public static synchronized XmlMarkupParser instance() { + return SINGLETON; + } + + private XmlMarkupParser() { + // disable construction + } + + public MarkupLine parseMarkupline(XMLEventReader2 reader) throws XMLStreamException { // NOPMD - acceptable + StringBuilder buffer = new StringBuilder(); + parseContents(reader, null, buffer); + String html = buffer.toString().trim(); + return html.isEmpty() ? null : MarkupLine.fromHtml(html); + } + + public MarkupMultiline parseMarkupMultiline(XMLEventReader2 reader) throws XMLStreamException { + StringBuilder buffer = new StringBuilder(); + parseToString(reader, buffer); + String html = buffer.toString().trim(); + + if (LOGGER.isDebugEnabled()) { + LOGGER.debug("XML->HTML: {}", html); + } + return html.isEmpty() ? null : MarkupMultiline.fromHtml(html); + } + + private void parseToString(XMLEventReader2 reader, StringBuilder buffer) // NOPMD - acceptable + throws XMLStreamException { + // if (LOGGER.isDebugEnabled()) { + // LOGGER.debug("parseToString(enter): {}", + // XmlEventUtil.toString(reader.peek())); + // } + + outer: while (reader.hasNextEvent() && !reader.peek().isEndElement()) { + // skip whitespace before the next block element + XMLEvent nextEvent = XmlEventUtil.skipWhitespace(reader); + + // if (LOGGER.isDebugEnabled()) { + // LOGGER.debug("parseToString: {}", XmlEventUtil.toString(nextEvent)); + // } + + if (nextEvent.isStartElement()) { + StartElement start = nextEvent.asStartElement(); + QName name = start.getName(); + + // Note: the next element is not consumed. The called method is expected to + // consume it + if (BLOCK_ELEMENTS.contains(name.getLocalPart())) { + parseStartElement(reader, start, buffer); + + // the next event should be the event after the start's END_ELEMENT + // assert XmlEventUtil.isNextEventEndElement(reader, name) : + // XmlEventUtil.toString(reader.peek()); + } else { + // throw new IllegalStateException(); + // stop parsing on first unrecognized event + break outer; + } + } + // reader.nextEvent(); + + // skip whitespace before the next block element + XmlEventUtil.skipWhitespace(reader); + } + + // if (LOGGER.isDebugEnabled()) { + // LOGGER.debug("parseToString(exit): {}", reader.peek() != null ? + // XmlEventUtil.toString(reader.peek()) : ""); + // } + } + + private void parseStartElement(XMLEventReader2 reader, StartElement start, StringBuilder buffer) + throws XMLStreamException { + if (LOGGER.isDebugEnabled()) { + LOGGER.debug("parseStartElement(enter): {}", XmlEventUtil.toString(start)); + } + + // consume the start event + reader.nextEvent(); + + QName name = start.getName(); + buffer.append('<') + .append(name.getLocalPart()); + for (Attribute attribute : CollectionUtil.toIterable( + ObjectUtils.notNull(start.getAttributes()))) { + buffer + .append(' ') + .append(attribute.getName().getLocalPart()) + .append("=\"") + .append(attribute.getValue()) + .append('"'); + } + + XMLEvent next = reader.peek(); + if (next != null && next.isEndElement()) { + buffer.append("/>"); + // consume end element event + reader.nextEvent(); + } else { + buffer.append('>'); + + // parse until the start's END_ELEMENT is reached + parseContents(reader, start, buffer); + + buffer + .append("'); + + // the next event should be the start's END_ELEMENT + XmlEventUtil.assertNext(reader, XMLStreamConstants.END_ELEMENT, name); + + // consume the start's END_ELEMENT + reader.nextEvent(); + } + + if (LOGGER.isDebugEnabled()) { + LOGGER.debug("parseStartElement(exit): {}", reader.peek() != null ? XmlEventUtil.toString(reader.peek()) : ""); + } + } + + private void parseContents(XMLEventReader2 reader, StartElement start, StringBuilder buffer) + throws XMLStreamException { + // if (LOGGER.isDebugEnabled()) { + // LOGGER.debug("parseContents(enter): {}", + // XmlEventUtil.toString(reader.peek())); + // } + + XMLEvent event; + while (reader.hasNextEvent() && !(event = reader.peek()).isEndElement()) { + // // skip whitespace before the next list item + // event = XmlEventUtil.skipWhitespace(reader); + + // if (LOGGER.isDebugEnabled()) { + // LOGGER.debug("parseContents(before): {}", XmlEventUtil.toString(event)); + // } + + if (event.isStartElement()) { + StartElement nextStart = event.asStartElement(); + // QName nextName = nextStart.getName(); + parseStartElement(reader, nextStart, buffer); + + // if (LOGGER.isDebugEnabled()) { + // LOGGER.debug("parseContents(after): {}", + // XmlEventUtil.toString(reader.peek())); + // } + + // assert XmlEventUtil.isNextEventEndElement(reader, nextName) : + // XmlEventUtil.toString(reader.peek()); + + // reader.nextEvent(); + } else if (event.isCharacters()) { + Characters characters = event.asCharacters(); + buffer.append(Escaping.escapeHtml(characters.getData(), true)); + reader.nextEvent(); + } + } + + assert start == null + || XmlEventUtil.isEventEndElement(reader.peek(), ObjectUtils.notNull(start.getName())) : XmlEventUtil + .generateExpectedMessage(reader.peek(), XMLStreamConstants.END_ELEMENT, start.getName()); + + // if (LOGGER.isDebugEnabled()) { + // LOGGER.debug("parseContents(exit): {}", reader.peek() != null ? + // XmlEventUtil.toString(reader.peek()) : ""); + // } + } + +} diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/object/Date.java b/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/object/Date.java index 7728a397b..290b67be4 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/object/Date.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/object/Date.java @@ -1,54 +1,54 @@ -/* - * Portions of this software was developed by employees of the National Institute - * of Standards and Technology (NIST), an agency of the Federal Government and is - * being made available as a public service. Pursuant to title 17 United States - * Code Section 105, works of NIST employees are not subject to copyright - * protection in the United States. This software may be subject to foreign - * copyright. Permission in the United States and in foreign countries, to the - * extent that NIST may hold copyright, to use, copy, modify, create derivative - * works, and distribute this software and its documentation without fee is hereby - * granted on a non-exclusive basis, provided that this notice and disclaimer - * of warranty appears in all copies. - * - * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER - * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY - * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM - * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE - * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT - * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, - * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, - * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, - * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR - * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT - * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. - */ - -package gov.nist.secauto.metaschema.core.datatype.object; - -import java.time.ZonedDateTime; - -import edu.umd.cs.findbugs.annotations.NonNull; - -public class Date // NOPMD - intentional - extends AbstractAmbiguousTemporal { - - /** - * Construct a new date object. This type supports ambiguous dates that were - * provided without a time zone. - * - * @param value - * the date value - * @param hasTimeZone - * {@code true} if the date is intended to have an associated time zone - * or {@code false} otherwise - */ - public Date(@NonNull ZonedDateTime value, boolean hasTimeZone) { - super(value, hasTimeZone); - } - - @Override - public Date copy() { - return new Date(getValue(), hasTimeZone()); - } -} +/* + * Portions of this software was developed by employees of the National Institute + * of Standards and Technology (NIST), an agency of the Federal Government and is + * being made available as a public service. Pursuant to title 17 United States + * Code Section 105, works of NIST employees are not subject to copyright + * protection in the United States. This software may be subject to foreign + * copyright. Permission in the United States and in foreign countries, to the + * extent that NIST may hold copyright, to use, copy, modify, create derivative + * works, and distribute this software and its documentation without fee is hereby + * granted on a non-exclusive basis, provided that this notice and disclaimer + * of warranty appears in all copies. + * + * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER + * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY + * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM + * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE + * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT + * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, + * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, + * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, + * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR + * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT + * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. + */ + +package gov.nist.secauto.metaschema.core.datatype.object; + +import java.time.ZonedDateTime; + +import edu.umd.cs.findbugs.annotations.NonNull; + +public class Date // NOPMD - intentional + extends AbstractAmbiguousTemporal { + + /** + * Construct a new date object. This type supports ambiguous dates that were + * provided without a time zone. + * + * @param value + * the date value + * @param hasTimeZone + * {@code true} if the date is intended to have an associated time zone + * or {@code false} otherwise + */ + public Date(@NonNull ZonedDateTime value, boolean hasTimeZone) { + super(value, hasTimeZone); + } + + @Override + public Date copy() { + return new Date(getValue(), hasTimeZone()); + } +} diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/object/DateTime.java b/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/object/DateTime.java index e3bd4593e..5c48fc262 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/object/DateTime.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/object/DateTime.java @@ -1,54 +1,54 @@ -/* - * Portions of this software was developed by employees of the National Institute - * of Standards and Technology (NIST), an agency of the Federal Government and is - * being made available as a public service. Pursuant to title 17 United States - * Code Section 105, works of NIST employees are not subject to copyright - * protection in the United States. This software may be subject to foreign - * copyright. Permission in the United States and in foreign countries, to the - * extent that NIST may hold copyright, to use, copy, modify, create derivative - * works, and distribute this software and its documentation without fee is hereby - * granted on a non-exclusive basis, provided that this notice and disclaimer - * of warranty appears in all copies. - * - * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER - * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY - * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM - * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE - * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT - * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, - * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, - * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, - * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR - * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT - * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. - */ - -package gov.nist.secauto.metaschema.core.datatype.object; - -import java.time.ZonedDateTime; - -import edu.umd.cs.findbugs.annotations.NonNull; - -public class DateTime - extends AbstractAmbiguousTemporal { - - /** - * Construct a new date/time object. This type supports ambiguous dates/times - * that were provided without a time zone. - * - * @param value - * the date/time value - * @param hasTimeZone - * {@code true} if the date/time is intended to have an associated time - * zone or {@code false} otherwise - */ - public DateTime(@NonNull ZonedDateTime value, boolean hasTimeZone) { - super(value, hasTimeZone); - } - - @Override - public DateTime copy() { - return new DateTime(getValue(), hasTimeZone()); - } -} +/* + * Portions of this software was developed by employees of the National Institute + * of Standards and Technology (NIST), an agency of the Federal Government and is + * being made available as a public service. Pursuant to title 17 United States + * Code Section 105, works of NIST employees are not subject to copyright + * protection in the United States. This software may be subject to foreign + * copyright. Permission in the United States and in foreign countries, to the + * extent that NIST may hold copyright, to use, copy, modify, create derivative + * works, and distribute this software and its documentation without fee is hereby + * granted on a non-exclusive basis, provided that this notice and disclaimer + * of warranty appears in all copies. + * + * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER + * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY + * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM + * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE + * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT + * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, + * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, + * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, + * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR + * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT + * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. + */ + +package gov.nist.secauto.metaschema.core.datatype.object; + +import java.time.ZonedDateTime; + +import edu.umd.cs.findbugs.annotations.NonNull; + +public class DateTime + extends AbstractAmbiguousTemporal { + + /** + * Construct a new date/time object. This type supports ambiguous dates/times + * that were provided without a time zone. + * + * @param value + * the date/time value + * @param hasTimeZone + * {@code true} if the date/time is intended to have an associated time + * zone or {@code false} otherwise + */ + public DateTime(@NonNull ZonedDateTime value, boolean hasTimeZone) { + super(value, hasTimeZone); + } + + @Override + public DateTime copy() { + return new DateTime(getValue(), hasTimeZone()); + } +} diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/JsonGroupAsBehavior.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/JsonGroupAsBehavior.java index 4b25ae850..1e8f86088 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/JsonGroupAsBehavior.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/JsonGroupAsBehavior.java @@ -1,52 +1,52 @@ -/* - * Portions of this software was developed by employees of the National Institute - * of Standards and Technology (NIST), an agency of the Federal Government and is - * being made available as a public service. Pursuant to title 17 United States - * Code Section 105, works of NIST employees are not subject to copyright - * protection in the United States. This software may be subject to foreign - * copyright. Permission in the United States and in foreign countries, to the - * extent that NIST may hold copyright, to use, copy, modify, create derivative - * works, and distribute this software and its documentation without fee is hereby - * granted on a non-exclusive basis, provided that this notice and disclaimer - * of warranty appears in all copies. - * - * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER - * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY - * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM - * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE - * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT - * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, - * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, - * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, - * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR - * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT - * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. - */ - -package gov.nist.secauto.metaschema.core.model; - -public enum JsonGroupAsBehavior { - /** - * In JSON, the group of instances will be represented as a JSON object, with - * each instance's JSON key used as the property and the remaining data - * represented as a child object of that property. - */ - KEYED, - /** - * In JSON, the group of instances will be represented as a single JSON object - * if there is one, or as an array of JSON objects if there is more than one. An - * empty array will be used when no items exist in the group. - */ - SINGLETON_OR_LIST, - /** - * In JSON, the group of instances will be represented as an array of JSON - * objects if there is more than one. An empty array will be used when no items - * exist in the group. - */ - LIST, - /** - * In JSON, the group of instances will be represented as a single JSON object. - */ - NONE -} +/* + * Portions of this software was developed by employees of the National Institute + * of Standards and Technology (NIST), an agency of the Federal Government and is + * being made available as a public service. Pursuant to title 17 United States + * Code Section 105, works of NIST employees are not subject to copyright + * protection in the United States. This software may be subject to foreign + * copyright. Permission in the United States and in foreign countries, to the + * extent that NIST may hold copyright, to use, copy, modify, create derivative + * works, and distribute this software and its documentation without fee is hereby + * granted on a non-exclusive basis, provided that this notice and disclaimer + * of warranty appears in all copies. + * + * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER + * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY + * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM + * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE + * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT + * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, + * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, + * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, + * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR + * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT + * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. + */ + +package gov.nist.secauto.metaschema.core.model; + +public enum JsonGroupAsBehavior { + /** + * In JSON, the group of instances will be represented as a JSON object, with + * each instance's JSON key used as the property and the remaining data + * represented as a child object of that property. + */ + KEYED, + /** + * In JSON, the group of instances will be represented as a single JSON object + * if there is one, or as an array of JSON objects if there is more than one. An + * empty array will be used when no items exist in the group. + */ + SINGLETON_OR_LIST, + /** + * In JSON, the group of instances will be represented as an array of JSON + * objects if there is more than one. An empty array will be used when no items + * exist in the group. + */ + LIST, + /** + * In JSON, the group of instances will be represented as a single JSON object. + */ + NONE +} diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/MetaschemaException.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/MetaschemaException.java index e811bf671..2f84ed780 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/MetaschemaException.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/MetaschemaException.java @@ -1,88 +1,88 @@ -/* - * Portions of this software was developed by employees of the National Institute - * of Standards and Technology (NIST), an agency of the Federal Government and is - * being made available as a public service. Pursuant to title 17 United States - * Code Section 105, works of NIST employees are not subject to copyright - * protection in the United States. This software may be subject to foreign - * copyright. Permission in the United States and in foreign countries, to the - * extent that NIST may hold copyright, to use, copy, modify, create derivative - * works, and distribute this software and its documentation without fee is hereby - * granted on a non-exclusive basis, provided that this notice and disclaimer - * of warranty appears in all copies. - * - * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER - * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY - * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM - * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE - * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT - * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, - * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, - * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, - * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR - * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT - * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. - */ - -package gov.nist.secauto.metaschema.core.model; - -public class MetaschemaException - extends Exception { - - /** - * The serial version UID. - */ - private static final long serialVersionUID = 1L; - - /** - * Create a new Metaschema exception with a provided message. - * - * @param message - * text describing the cause of the exception - */ - public MetaschemaException(String message) { - super(message); - } - - /** - * Create a new Metaschema exception based on the provided cause. - * - * @param cause - * the exception that caused this exception - */ - public MetaschemaException(Throwable cause) { - super(cause); - } - - /** - * Create a new Metaschema exception with a provided message based on the - * provided cause. - * - * @param message - * text describing the cause of the exception - * @param cause - * the exception that caused this exception - */ - public MetaschemaException(String message, Throwable cause) { - super(message, cause); - } - - /** - * Create a new Metaschema exception with a provided message based on the - * provided cause. - * - * - * @param message - * text describing the cause of the exception - * @param cause - * the exception that caused this exception - * @param enableSuppression - * whether or not suppression is enabled or disabled - * @param writableStackTrace - * whether or not the stack trace should be writable - */ - public MetaschemaException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { - super(message, cause, enableSuppression, writableStackTrace); - } - -} +/* + * Portions of this software was developed by employees of the National Institute + * of Standards and Technology (NIST), an agency of the Federal Government and is + * being made available as a public service. Pursuant to title 17 United States + * Code Section 105, works of NIST employees are not subject to copyright + * protection in the United States. This software may be subject to foreign + * copyright. Permission in the United States and in foreign countries, to the + * extent that NIST may hold copyright, to use, copy, modify, create derivative + * works, and distribute this software and its documentation without fee is hereby + * granted on a non-exclusive basis, provided that this notice and disclaimer + * of warranty appears in all copies. + * + * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER + * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY + * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM + * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE + * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT + * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, + * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, + * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, + * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR + * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT + * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. + */ + +package gov.nist.secauto.metaschema.core.model; + +public class MetaschemaException + extends Exception { + + /** + * The serial version UID. + */ + private static final long serialVersionUID = 1L; + + /** + * Create a new Metaschema exception with a provided message. + * + * @param message + * text describing the cause of the exception + */ + public MetaschemaException(String message) { + super(message); + } + + /** + * Create a new Metaschema exception based on the provided cause. + * + * @param cause + * the exception that caused this exception + */ + public MetaschemaException(Throwable cause) { + super(cause); + } + + /** + * Create a new Metaschema exception with a provided message based on the + * provided cause. + * + * @param message + * text describing the cause of the exception + * @param cause + * the exception that caused this exception + */ + public MetaschemaException(String message, Throwable cause) { + super(message, cause); + } + + /** + * Create a new Metaschema exception with a provided message based on the + * provided cause. + * + * + * @param message + * text describing the cause of the exception + * @param cause + * the exception that caused this exception + * @param enableSuppression + * whether or not suppression is enabled or disabled + * @param writableStackTrace + * whether or not the stack trace should be writable + */ + public MetaschemaException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) { + super(message, cause, enableSuppression, writableStackTrace); + } + +} diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/XmlGroupAsBehavior.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/XmlGroupAsBehavior.java index 2a72b15db..3f5634f1c 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/XmlGroupAsBehavior.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/XmlGroupAsBehavior.java @@ -1,38 +1,38 @@ -/* - * Portions of this software was developed by employees of the National Institute - * of Standards and Technology (NIST), an agency of the Federal Government and is - * being made available as a public service. Pursuant to title 17 United States - * Code Section 105, works of NIST employees are not subject to copyright - * protection in the United States. This software may be subject to foreign - * copyright. Permission in the United States and in foreign countries, to the - * extent that NIST may hold copyright, to use, copy, modify, create derivative - * works, and distribute this software and its documentation without fee is hereby - * granted on a non-exclusive basis, provided that this notice and disclaimer - * of warranty appears in all copies. - * - * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER - * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY - * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM - * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE - * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT - * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, - * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, - * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, - * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR - * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT - * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. - */ - -package gov.nist.secauto.metaschema.core.model; - -public enum XmlGroupAsBehavior { - /** - * In XML, child element instances will be wrapped by a grouping element. - */ - GROUPED, - /** - * In XML, child element instances will exist in an unwrapped form. - */ - UNGROUPED -} +/* + * Portions of this software was developed by employees of the National Institute + * of Standards and Technology (NIST), an agency of the Federal Government and is + * being made available as a public service. Pursuant to title 17 United States + * Code Section 105, works of NIST employees are not subject to copyright + * protection in the United States. This software may be subject to foreign + * copyright. Permission in the United States and in foreign countries, to the + * extent that NIST may hold copyright, to use, copy, modify, create derivative + * works, and distribute this software and its documentation without fee is hereby + * granted on a non-exclusive basis, provided that this notice and disclaimer + * of warranty appears in all copies. + * + * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER + * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY + * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM + * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE + * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT + * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, + * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, + * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, + * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR + * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT + * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. + */ + +package gov.nist.secauto.metaschema.core.model; + +public enum XmlGroupAsBehavior { + /** + * In XML, child element instances will be wrapped by a grouping element. + */ + GROUPED, + /** + * In XML, child element instances will exist in an unwrapped form. + */ + UNGROUPED +} diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/util/XmlEventUtil.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/util/XmlEventUtil.java index 14da67e63..4a49bf509 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/util/XmlEventUtil.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/util/XmlEventUtil.java @@ -1,605 +1,605 @@ -/* - * Portions of this software was developed by employees of the National Institute - * of Standards and Technology (NIST), an agency of the Federal Government and is - * being made available as a public service. Pursuant to title 17 United States - * Code Section 105, works of NIST employees are not subject to copyright - * protection in the United States. This software may be subject to foreign - * copyright. Permission in the United States and in foreign countries, to the - * extent that NIST may hold copyright, to use, copy, modify, create derivative - * works, and distribute this software and its documentation without fee is hereby - * granted on a non-exclusive basis, provided that this notice and disclaimer - * of warranty appears in all copies. - * - * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER - * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY - * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM - * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE - * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT - * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, - * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, - * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, - * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR - * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT - * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. - */ - -package gov.nist.secauto.metaschema.core.model.util; - -import gov.nist.secauto.metaschema.core.util.ObjectUtils; - -import org.codehaus.stax2.XMLEventReader2; -import org.codehaus.stax2.XMLStreamReader2; - -import java.io.IOException; -import java.util.HashMap; -import java.util.Map; -import java.util.Set; -import java.util.regex.Pattern; -import java.util.stream.Collectors; -import java.util.stream.IntStream; - -import javax.xml.namespace.QName; -import javax.xml.stream.Location; -import javax.xml.stream.XMLStreamConstants; -import javax.xml.stream.XMLStreamException; -import javax.xml.stream.events.Characters; -import javax.xml.stream.events.EndElement; -import javax.xml.stream.events.StartElement; -import javax.xml.stream.events.XMLEvent; - -import edu.umd.cs.findbugs.annotations.NonNull; -import edu.umd.cs.findbugs.annotations.Nullable; - -public final class XmlEventUtil { // NOPMD this is a set of utility methods - private static final Pattern WHITESPACE_ONLY = Pattern.compile("^\\s+$"); - - private static final Map EVENT_NAME_MAP = new HashMap<>(); // NOPMD - this value is immutable - - static { - EVENT_NAME_MAP.put(XMLStreamConstants.START_ELEMENT, "START_ELEMENT"); - EVENT_NAME_MAP.put(XMLStreamConstants.END_ELEMENT, "END_ELEMENT"); - EVENT_NAME_MAP.put(XMLStreamConstants.PROCESSING_INSTRUCTION, "PROCESSING_INSTRUCTION"); - EVENT_NAME_MAP.put(XMLStreamConstants.CHARACTERS, "CHARACTERS"); - EVENT_NAME_MAP.put(XMLStreamConstants.COMMENT, "COMMENT"); - EVENT_NAME_MAP.put(XMLStreamConstants.SPACE, "SPACE"); - EVENT_NAME_MAP.put(XMLStreamConstants.START_DOCUMENT, "START_DOCUMENT"); - EVENT_NAME_MAP.put(XMLStreamConstants.END_DOCUMENT, "END_DOCUMENT"); - EVENT_NAME_MAP.put(XMLStreamConstants.ENTITY_REFERENCE, "ENTITY_REFERENCE"); - EVENT_NAME_MAP.put(XMLStreamConstants.ATTRIBUTE, "ATTRIBUTE"); - EVENT_NAME_MAP.put(XMLStreamConstants.DTD, "DTD"); - EVENT_NAME_MAP.put(XMLStreamConstants.CDATA, "CDATA"); - EVENT_NAME_MAP.put(XMLStreamConstants.NAMESPACE, "XML_NAMESPACE"); - EVENT_NAME_MAP.put(XMLStreamConstants.NOTATION_DECLARATION, "NOTATION_DECLARATION"); - EVENT_NAME_MAP.put(XMLStreamConstants.ENTITY_DECLARATION, "ENTITY_DECLARATION"); - } - - private XmlEventUtil() { - // disable construction - } - - @SuppressWarnings("null") - @NonNull - private static Object escape(@NonNull String data) { - return data.chars().mapToObj(c -> (char) c).map(c -> escape(c)).collect(Collectors.joining()); - } - - @SuppressWarnings("null") - @NonNull - private static String escape(char ch) { - String retval; - switch (ch) { - case '\n': - retval = "\\n"; - break; - case '\r': - retval = "\\r"; - break; - default: - retval = String.valueOf(ch); - break; - } - return retval; - } - - /** - * Generate a message suitable for logging that describes the provided - * {@link XMLEvent}. - * - * @param xmlEvent - * the event to generate the message for - * @return the message - */ - @NonNull - public static CharSequence toString(XMLEvent xmlEvent) { - CharSequence retval; - if (xmlEvent == null) { - retval = "EOF"; - } else { - @SuppressWarnings("null") - @NonNull StringBuilder builder = new StringBuilder() - .append(toEventName(xmlEvent)); - QName name = toQName(xmlEvent); - if (name != null) { - builder.append(": ").append(name.toString()); - } - if (xmlEvent.isCharacters()) { - String text = xmlEvent.asCharacters().getData(); - if (text != null) { - builder.append(" '").append(escape(text)).append('\''); - } - } - Location location = toLocation(xmlEvent); - if (location != null) { - builder.append(" at ").append(toString(location)); - } - retval = builder; - } - return retval; - } - - /** - * Generates a message for the provided {@link Location}. - * - * @param location - * the location to generate the message for - * @return the message - */ - @SuppressWarnings("null") - @NonNull - public static CharSequence toString(@Nullable Location location) { - return location == null ? "unknown" - : new StringBuilder() - .append(location.getLineNumber()) - .append(':') - .append(location.getColumnNumber()); - } - - /** - * Generates a string containing the current event and location of the stream - * reader. - * - * @param reader - * the stream reader - * @return the generated string - */ - @NonNull - public static CharSequence toString(@NonNull XMLStreamReader2 reader) { // NO_UCD (unused code) - int type = reader.getEventType(); - - @SuppressWarnings("null") - @NonNull StringBuilder builder = new StringBuilder().append(toEventName(type)); - QName name = reader.getName(); - if (name != null) { - builder.append(": ").append(name.toString()); - } - if (XMLStreamConstants.CHARACTERS == type) { - String text = reader.getText(); - if (text != null) { - builder.append(" '").append(escape(text)).append('\''); - } - } - Location location = reader.getLocation(); - if (location != null) { - builder.append(" at ").append(toString(location)); - } - return builder; - } - - /** - * Retrieve the resource location of {@code event}. - * - * @param event - * the event to identify the location for - * @return the location or {@code null} if the location is unknown - */ - @Nullable - public static Location toLocation(@NonNull XMLEvent event) { - Location retval = null; - if (event.isStartElement()) { - StartElement start = event.asStartElement(); - retval = start.getLocation(); - } else if (event.isEndElement()) { - EndElement end = event.asEndElement(); - retval = end.getLocation(); - } else if (event.isCharacters()) { - Characters characters = event.asCharacters(); - retval = characters.getLocation(); - } - return retval; - } - - /** - * Retrieve the name of the node associated with {@code event}. - * - * @param event - * the event to get the {@link QName} for - * @return the name of the node or {@code null} if the event is not a start or - * end element - */ - @Nullable - public static QName toQName(@NonNull XMLEvent event) { - QName retval = null; - if (event.isStartElement()) { - StartElement start = event.asStartElement(); - retval = start.getName(); - } else if (event.isEndElement()) { - EndElement end = event.asEndElement(); - retval = end.getName(); - } - return retval; - } - - /** - * Get the event name of the {@code event}. - * - * @param event - * the event to get the event name for - * @return the event name - */ - @NonNull - public static String toEventName(@NonNull XMLEvent event) { - return toEventName(event.getEventType()); - } - - /** - * Get the event name of the {@code eventType}, which is one of the types - * defined by {@link XMLStreamConstants}. - * - * @param eventType - * the event constant to get the event name for as defined by - * {@link XMLStreamConstants} - * @return the event name - */ - @NonNull - public static String toEventName(int eventType) { - String retval = EVENT_NAME_MAP.get(eventType); - if (retval == null) { - retval = "unknown event '" + Integer.toString(eventType) + "'"; - } - return retval; - } - - /** - * Advance through XMLEvents until the event type identified by - * {@code eventType} is reached or the end of stream is found. - * - * @param reader - * the event reader to advance - * @param eventType - * the event type to stop on as defined by {@link XMLStreamConstants} - * @return the next event of the specified type or {@code null} if the end of - * stream is reached - * @throws XMLStreamException - * if an error occurred while advancing the stream - */ - @Nullable - public static XMLEvent advanceTo(@NonNull XMLEventReader2 reader, int eventType) - throws XMLStreamException { // NO_UCD (unused code) - XMLEvent xmlEvent; - do { - xmlEvent = reader.nextEvent(); - // if (LOGGER.isWarnEnabled()) { - // LOGGER.warn("skipping over: {}", XmlEventUtil.toString(xmlEvent)); - // } - if (xmlEvent.isStartElement()) { - advanceTo(reader, XMLStreamConstants.END_ELEMENT); - // skip this end element - xmlEvent = reader.nextEvent(); - // if (LOGGER.isDebugEnabled()) { - // LOGGER.debug("skipping over: {}", XmlEventUtil.toString(xmlEvent)); - // } - } - } while (reader.hasNext() && (xmlEvent = reader.peek()).getEventType() != eventType); - return xmlEvent; - } - - @SuppressWarnings("PMD.OnlyOneReturn") - public static XMLEvent skipElement(@NonNull XMLEventReader2 reader) throws XMLStreamException { - XMLEvent xmlEvent = reader.peek(); - if (!xmlEvent.isStartElement()) { - return xmlEvent; - } - // if (LOGGER.isInfoEnabled()) { - // LOGGER.atInfo().log(String.format("At location %s", toString(xmlEvent))); - // } - - int depth = 0; - do { - xmlEvent = reader.nextEvent(); - // if (LOGGER.isInfoEnabled()) { - // LOGGER.atInfo().log(String.format("Skipping %s", toString(xmlEvent))); - // } - if (xmlEvent.isStartElement()) { - depth++; - } else if (xmlEvent.isEndElement()) { - depth--; - } - } while (depth > 0 && reader.hasNext()); - return reader.peek(); - } - - /** - * Skip over any processing instructions. - * - * @param reader - * the event reader to advance - * @return the last processing instruction event or the reader's next event if - * no processing instruction was found - * @throws XMLStreamException - * if an error occurred while advancing the stream - */ - @NonNull - public static XMLEvent skipProcessingInstructions(@NonNull XMLEventReader2 reader) throws XMLStreamException { - XMLEvent nextEvent; - while ((nextEvent = reader.peek()).isProcessingInstruction()) { - nextEvent = reader.nextEvent(); - } - return nextEvent; - } - - /** - * Skip over any whitespace. - * - * @param reader - * the event reader to advance - * @return the last character event containing whitespace or the reader's next - * event if no character event was found - * @throws XMLStreamException - * if an error occurred while advancing the stream - */ - @SuppressWarnings("null") - @NonNull - public static XMLEvent skipWhitespace(@NonNull XMLEventReader2 reader) throws XMLStreamException { - @NonNull XMLEvent nextEvent; - while ((nextEvent = reader.peek()).isCharacters()) { - Characters characters = nextEvent.asCharacters(); - String data = characters.getData(); - if (WHITESPACE_ONLY.matcher(data).matches()) { - nextEvent = reader.nextEvent(); - } else { - break; - } - } - return nextEvent; - } - - /** - * Determine if the {@code event} is an end element whose name matches the - * provided {@code expectedQName}. - * - * @param event - * the event - * @param expectedQName - * the expected element name - * @return {@code true} if the next event matches the {@code expectedQName} - */ - public static boolean isEventEndElement(XMLEvent event, @NonNull QName expectedQName) { - return event != null - && event.isEndElement() - && expectedQName.equals(event.asEndElement().getName()); - } - - /** - * Determine if the {@code event} is an end of document event. - * - * @param event - * the event - * @return {@code true} if the next event is an end of document event - */ - public static boolean isEventEndDocument(XMLEvent event) { - return event != null - && event.isEndElement(); - } - - /** - * Determine if the {@code event} is a start element whose name matches the - * provided {@code expectedQName}. - * - * @param event - * the event - * @param expectedQName - * the expected element name - * @return {@code true} if the next event is a start element that matches the - * {@code expectedQName} - * @throws XMLStreamException - * if an error occurred while looking at the next event - */ - public static boolean isEventStartElement(XMLEvent event, @NonNull QName expectedQName) throws XMLStreamException { - return event != null - && event.isStartElement() - && expectedQName.equals(event.asStartElement().getName()); - } - - /** - * Consume the next event from {@code reader} and assert that this event is of - * the type identified by {@code presumedEventType}. - * - * @param reader - * the event reader - * @param presumedEventType - * the expected event type as defined by {@link XMLStreamConstants} - * @return the next event - * @throws XMLStreamException - * if an error occurred while looking at the next event - */ - public static XMLEvent consumeAndAssert(XMLEventReader2 reader, int presumedEventType) - throws XMLStreamException { - return consumeAndAssert(reader, presumedEventType, null); - } - - /** - * Consume the next event from {@code reader} and assert that this event is of - * the type identified by {@code presumedEventType} and has the name identified - * by {@code presumedName}. - * - * @param reader - * the event reader - * @param presumedEventType - * the expected event type as defined by {@link XMLStreamConstants} - * @param presumedName - * the expected name of the node associated with the event - * @return the next event - * @throws XMLStreamException - * if an error occurred while looking at the next event - */ - public static XMLEvent consumeAndAssert(XMLEventReader2 reader, int presumedEventType, QName presumedName) - throws XMLStreamException { - XMLEvent retval = reader.nextEvent(); - - int eventType = retval.getEventType(); - QName name = toQName(retval); - assert eventType == presumedEventType - && (presumedName == null - || presumedName.equals(name)) : generateExpectedMessage( - retval, - presumedEventType, - presumedName); - return retval; - } - - @NonNull - public static StartElement requireStartElement( - @NonNull XMLEventReader2 reader, - @NonNull QName presumedName) throws IOException, XMLStreamException { - XMLEvent retval = reader.nextEvent(); - if (!(retval.isStartElement() && presumedName.equals(retval.asStartElement().getName()))) { - throw new IOException(generateExpectedMessage( - retval, - XMLStreamConstants.START_ELEMENT, - presumedName).toString()); - } - return ObjectUtils.notNull(retval.asStartElement()); - } - - @NonNull - public static EndElement requireEndElement( - @NonNull XMLEventReader2 reader, - @NonNull QName presumedName) throws IOException, XMLStreamException { - XMLEvent retval = reader.nextEvent(); - if (!(retval.isEndElement() && presumedName.equals(retval.asEndElement().getName()))) { - throw new IOException(generateExpectedMessage( - retval, - XMLStreamConstants.END_ELEMENT, - presumedName).toString()); - } - return ObjectUtils.notNull(retval.asEndElement()); - } - - /** - * Assert that the next event from {@code reader} is of the type identified by - * {@code presumedEventType}. - * - * @param reader - * the event reader - * @param presumedEventType - * the expected event type as defined by {@link XMLStreamConstants} - * @return the next event - * @throws XMLStreamException - * if an error occurred while looking at the next event - * @throws AssertionError - * if the next event does not match the presumed event - */ - public static XMLEvent assertNext( - @NonNull XMLEventReader2 reader, - int presumedEventType) - throws XMLStreamException { - return assertNext(reader, presumedEventType, null); - } - - /** - * Assert that the next event from {@code reader} is of the type identified by - * {@code presumedEventType} and has the name identified by - * {@code presumedName}. - * - * @param reader - * the event reader - * @param presumedEventType - * the expected event type as defined by {@link XMLStreamConstants} - * @param presumedName - * the expected name of the node associated with the event - * @return the next event - * @throws XMLStreamException - * if an error occurred while looking at the next event - * @throws AssertionError - * if the next event does not match the presumed event - */ - public static XMLEvent assertNext( - @NonNull XMLEventReader2 reader, - int presumedEventType, - @Nullable QName presumedName) - throws XMLStreamException { - XMLEvent nextEvent = reader.peek(); - - int eventType = nextEvent.getEventType(); - assert eventType == presumedEventType - && (presumedName == null - || presumedName.equals(toQName(nextEvent))) : generateExpectedMessage( - nextEvent, - presumedEventType, - presumedName); - return nextEvent; - } - - public static CharSequence generateLocationMessage(@NonNull XMLEvent event) { - Location location = toLocation(event); - return location == null ? "" : generateLocationMessage(location); - } - - public static CharSequence generateLocationMessage(@NonNull Location location) { - return new StringBuilder(12) - .append(" at ") - .append(XmlEventUtil.toString(location)); - } - - public static CharSequence generateExpectedMessage( - @Nullable XMLEvent event, - int presumedEventType, - @Nullable QName presumedName) { - StringBuilder builder = new StringBuilder(64); - builder - .append("Expected XML ") - .append(toEventName(presumedEventType)); - - if (presumedName != null) { - builder.append(" for QName '") - .append(presumedName.toString()); - } - - if (event == null) { - builder.append("', instead found null event"); - } else { - builder.append("', instead found ") - .append(toString(event)) - .append(generateLocationMessage(event)); - } - return builder; - } - - /** - * Skips events specified by {@code events}. - * - * @param reader - * the event reader - * @param events - * the events to skip - * @return the next non-mataching event returned by - * {@link XMLEventReader2#peek()}, or {@code null} if there was no next - * event - * @throws XMLStreamException - * if an error occurred while reading - */ - public static XMLEvent skipEvents(XMLEventReader2 reader, int... events) throws XMLStreamException { - Set skipEvents = IntStream.of(events).boxed().collect(Collectors.toSet()); - - XMLEvent nextEvent = null; - while (reader.hasNext()) { - nextEvent = reader.peek(); - if (!skipEvents.contains(nextEvent.getEventType())) { - break; - } - reader.nextEvent(); - } - return nextEvent; - } -} +/* + * Portions of this software was developed by employees of the National Institute + * of Standards and Technology (NIST), an agency of the Federal Government and is + * being made available as a public service. Pursuant to title 17 United States + * Code Section 105, works of NIST employees are not subject to copyright + * protection in the United States. This software may be subject to foreign + * copyright. Permission in the United States and in foreign countries, to the + * extent that NIST may hold copyright, to use, copy, modify, create derivative + * works, and distribute this software and its documentation without fee is hereby + * granted on a non-exclusive basis, provided that this notice and disclaimer + * of warranty appears in all copies. + * + * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER + * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY + * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM + * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE + * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT + * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, + * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, + * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, + * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR + * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT + * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. + */ + +package gov.nist.secauto.metaschema.core.model.util; + +import gov.nist.secauto.metaschema.core.util.ObjectUtils; + +import org.codehaus.stax2.XMLEventReader2; +import org.codehaus.stax2.XMLStreamReader2; + +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; +import java.util.Set; +import java.util.regex.Pattern; +import java.util.stream.Collectors; +import java.util.stream.IntStream; + +import javax.xml.namespace.QName; +import javax.xml.stream.Location; +import javax.xml.stream.XMLStreamConstants; +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.events.Characters; +import javax.xml.stream.events.EndElement; +import javax.xml.stream.events.StartElement; +import javax.xml.stream.events.XMLEvent; + +import edu.umd.cs.findbugs.annotations.NonNull; +import edu.umd.cs.findbugs.annotations.Nullable; + +public final class XmlEventUtil { // NOPMD this is a set of utility methods + private static final Pattern WHITESPACE_ONLY = Pattern.compile("^\\s+$"); + + private static final Map EVENT_NAME_MAP = new HashMap<>(); // NOPMD - this value is immutable + + static { + EVENT_NAME_MAP.put(XMLStreamConstants.START_ELEMENT, "START_ELEMENT"); + EVENT_NAME_MAP.put(XMLStreamConstants.END_ELEMENT, "END_ELEMENT"); + EVENT_NAME_MAP.put(XMLStreamConstants.PROCESSING_INSTRUCTION, "PROCESSING_INSTRUCTION"); + EVENT_NAME_MAP.put(XMLStreamConstants.CHARACTERS, "CHARACTERS"); + EVENT_NAME_MAP.put(XMLStreamConstants.COMMENT, "COMMENT"); + EVENT_NAME_MAP.put(XMLStreamConstants.SPACE, "SPACE"); + EVENT_NAME_MAP.put(XMLStreamConstants.START_DOCUMENT, "START_DOCUMENT"); + EVENT_NAME_MAP.put(XMLStreamConstants.END_DOCUMENT, "END_DOCUMENT"); + EVENT_NAME_MAP.put(XMLStreamConstants.ENTITY_REFERENCE, "ENTITY_REFERENCE"); + EVENT_NAME_MAP.put(XMLStreamConstants.ATTRIBUTE, "ATTRIBUTE"); + EVENT_NAME_MAP.put(XMLStreamConstants.DTD, "DTD"); + EVENT_NAME_MAP.put(XMLStreamConstants.CDATA, "CDATA"); + EVENT_NAME_MAP.put(XMLStreamConstants.NAMESPACE, "XML_NAMESPACE"); + EVENT_NAME_MAP.put(XMLStreamConstants.NOTATION_DECLARATION, "NOTATION_DECLARATION"); + EVENT_NAME_MAP.put(XMLStreamConstants.ENTITY_DECLARATION, "ENTITY_DECLARATION"); + } + + private XmlEventUtil() { + // disable construction + } + + @SuppressWarnings("null") + @NonNull + private static Object escape(@NonNull String data) { + return data.chars().mapToObj(c -> (char) c).map(c -> escape(c)).collect(Collectors.joining()); + } + + @SuppressWarnings("null") + @NonNull + private static String escape(char ch) { + String retval; + switch (ch) { + case '\n': + retval = "\\n"; + break; + case '\r': + retval = "\\r"; + break; + default: + retval = String.valueOf(ch); + break; + } + return retval; + } + + /** + * Generate a message suitable for logging that describes the provided + * {@link XMLEvent}. + * + * @param xmlEvent + * the event to generate the message for + * @return the message + */ + @NonNull + public static CharSequence toString(XMLEvent xmlEvent) { + CharSequence retval; + if (xmlEvent == null) { + retval = "EOF"; + } else { + @SuppressWarnings("null") + @NonNull StringBuilder builder = new StringBuilder() + .append(toEventName(xmlEvent)); + QName name = toQName(xmlEvent); + if (name != null) { + builder.append(": ").append(name.toString()); + } + if (xmlEvent.isCharacters()) { + String text = xmlEvent.asCharacters().getData(); + if (text != null) { + builder.append(" '").append(escape(text)).append('\''); + } + } + Location location = toLocation(xmlEvent); + if (location != null) { + builder.append(" at ").append(toString(location)); + } + retval = builder; + } + return retval; + } + + /** + * Generates a message for the provided {@link Location}. + * + * @param location + * the location to generate the message for + * @return the message + */ + @SuppressWarnings("null") + @NonNull + public static CharSequence toString(@Nullable Location location) { + return location == null ? "unknown" + : new StringBuilder() + .append(location.getLineNumber()) + .append(':') + .append(location.getColumnNumber()); + } + + /** + * Generates a string containing the current event and location of the stream + * reader. + * + * @param reader + * the stream reader + * @return the generated string + */ + @NonNull + public static CharSequence toString(@NonNull XMLStreamReader2 reader) { // NO_UCD (unused code) + int type = reader.getEventType(); + + @SuppressWarnings("null") + @NonNull StringBuilder builder = new StringBuilder().append(toEventName(type)); + QName name = reader.getName(); + if (name != null) { + builder.append(": ").append(name.toString()); + } + if (XMLStreamConstants.CHARACTERS == type) { + String text = reader.getText(); + if (text != null) { + builder.append(" '").append(escape(text)).append('\''); + } + } + Location location = reader.getLocation(); + if (location != null) { + builder.append(" at ").append(toString(location)); + } + return builder; + } + + /** + * Retrieve the resource location of {@code event}. + * + * @param event + * the event to identify the location for + * @return the location or {@code null} if the location is unknown + */ + @Nullable + public static Location toLocation(@NonNull XMLEvent event) { + Location retval = null; + if (event.isStartElement()) { + StartElement start = event.asStartElement(); + retval = start.getLocation(); + } else if (event.isEndElement()) { + EndElement end = event.asEndElement(); + retval = end.getLocation(); + } else if (event.isCharacters()) { + Characters characters = event.asCharacters(); + retval = characters.getLocation(); + } + return retval; + } + + /** + * Retrieve the name of the node associated with {@code event}. + * + * @param event + * the event to get the {@link QName} for + * @return the name of the node or {@code null} if the event is not a start or + * end element + */ + @Nullable + public static QName toQName(@NonNull XMLEvent event) { + QName retval = null; + if (event.isStartElement()) { + StartElement start = event.asStartElement(); + retval = start.getName(); + } else if (event.isEndElement()) { + EndElement end = event.asEndElement(); + retval = end.getName(); + } + return retval; + } + + /** + * Get the event name of the {@code event}. + * + * @param event + * the event to get the event name for + * @return the event name + */ + @NonNull + public static String toEventName(@NonNull XMLEvent event) { + return toEventName(event.getEventType()); + } + + /** + * Get the event name of the {@code eventType}, which is one of the types + * defined by {@link XMLStreamConstants}. + * + * @param eventType + * the event constant to get the event name for as defined by + * {@link XMLStreamConstants} + * @return the event name + */ + @NonNull + public static String toEventName(int eventType) { + String retval = EVENT_NAME_MAP.get(eventType); + if (retval == null) { + retval = "unknown event '" + Integer.toString(eventType) + "'"; + } + return retval; + } + + /** + * Advance through XMLEvents until the event type identified by + * {@code eventType} is reached or the end of stream is found. + * + * @param reader + * the event reader to advance + * @param eventType + * the event type to stop on as defined by {@link XMLStreamConstants} + * @return the next event of the specified type or {@code null} if the end of + * stream is reached + * @throws XMLStreamException + * if an error occurred while advancing the stream + */ + @Nullable + public static XMLEvent advanceTo(@NonNull XMLEventReader2 reader, int eventType) + throws XMLStreamException { // NO_UCD (unused code) + XMLEvent xmlEvent; + do { + xmlEvent = reader.nextEvent(); + // if (LOGGER.isWarnEnabled()) { + // LOGGER.warn("skipping over: {}", XmlEventUtil.toString(xmlEvent)); + // } + if (xmlEvent.isStartElement()) { + advanceTo(reader, XMLStreamConstants.END_ELEMENT); + // skip this end element + xmlEvent = reader.nextEvent(); + // if (LOGGER.isDebugEnabled()) { + // LOGGER.debug("skipping over: {}", XmlEventUtil.toString(xmlEvent)); + // } + } + } while (reader.hasNext() && (xmlEvent = reader.peek()).getEventType() != eventType); + return xmlEvent; + } + + @SuppressWarnings("PMD.OnlyOneReturn") + public static XMLEvent skipElement(@NonNull XMLEventReader2 reader) throws XMLStreamException { + XMLEvent xmlEvent = reader.peek(); + if (!xmlEvent.isStartElement()) { + return xmlEvent; + } + // if (LOGGER.isInfoEnabled()) { + // LOGGER.atInfo().log(String.format("At location %s", toString(xmlEvent))); + // } + + int depth = 0; + do { + xmlEvent = reader.nextEvent(); + // if (LOGGER.isInfoEnabled()) { + // LOGGER.atInfo().log(String.format("Skipping %s", toString(xmlEvent))); + // } + if (xmlEvent.isStartElement()) { + depth++; + } else if (xmlEvent.isEndElement()) { + depth--; + } + } while (depth > 0 && reader.hasNext()); + return reader.peek(); + } + + /** + * Skip over any processing instructions. + * + * @param reader + * the event reader to advance + * @return the last processing instruction event or the reader's next event if + * no processing instruction was found + * @throws XMLStreamException + * if an error occurred while advancing the stream + */ + @NonNull + public static XMLEvent skipProcessingInstructions(@NonNull XMLEventReader2 reader) throws XMLStreamException { + XMLEvent nextEvent; + while ((nextEvent = reader.peek()).isProcessingInstruction()) { + nextEvent = reader.nextEvent(); + } + return nextEvent; + } + + /** + * Skip over any whitespace. + * + * @param reader + * the event reader to advance + * @return the last character event containing whitespace or the reader's next + * event if no character event was found + * @throws XMLStreamException + * if an error occurred while advancing the stream + */ + @SuppressWarnings("null") + @NonNull + public static XMLEvent skipWhitespace(@NonNull XMLEventReader2 reader) throws XMLStreamException { + @NonNull XMLEvent nextEvent; + while ((nextEvent = reader.peek()).isCharacters()) { + Characters characters = nextEvent.asCharacters(); + String data = characters.getData(); + if (WHITESPACE_ONLY.matcher(data).matches()) { + nextEvent = reader.nextEvent(); + } else { + break; + } + } + return nextEvent; + } + + /** + * Determine if the {@code event} is an end element whose name matches the + * provided {@code expectedQName}. + * + * @param event + * the event + * @param expectedQName + * the expected element name + * @return {@code true} if the next event matches the {@code expectedQName} + */ + public static boolean isEventEndElement(XMLEvent event, @NonNull QName expectedQName) { + return event != null + && event.isEndElement() + && expectedQName.equals(event.asEndElement().getName()); + } + + /** + * Determine if the {@code event} is an end of document event. + * + * @param event + * the event + * @return {@code true} if the next event is an end of document event + */ + public static boolean isEventEndDocument(XMLEvent event) { + return event != null + && event.isEndElement(); + } + + /** + * Determine if the {@code event} is a start element whose name matches the + * provided {@code expectedQName}. + * + * @param event + * the event + * @param expectedQName + * the expected element name + * @return {@code true} if the next event is a start element that matches the + * {@code expectedQName} + * @throws XMLStreamException + * if an error occurred while looking at the next event + */ + public static boolean isEventStartElement(XMLEvent event, @NonNull QName expectedQName) throws XMLStreamException { + return event != null + && event.isStartElement() + && expectedQName.equals(event.asStartElement().getName()); + } + + /** + * Consume the next event from {@code reader} and assert that this event is of + * the type identified by {@code presumedEventType}. + * + * @param reader + * the event reader + * @param presumedEventType + * the expected event type as defined by {@link XMLStreamConstants} + * @return the next event + * @throws XMLStreamException + * if an error occurred while looking at the next event + */ + public static XMLEvent consumeAndAssert(XMLEventReader2 reader, int presumedEventType) + throws XMLStreamException { + return consumeAndAssert(reader, presumedEventType, null); + } + + /** + * Consume the next event from {@code reader} and assert that this event is of + * the type identified by {@code presumedEventType} and has the name identified + * by {@code presumedName}. + * + * @param reader + * the event reader + * @param presumedEventType + * the expected event type as defined by {@link XMLStreamConstants} + * @param presumedName + * the expected name of the node associated with the event + * @return the next event + * @throws XMLStreamException + * if an error occurred while looking at the next event + */ + public static XMLEvent consumeAndAssert(XMLEventReader2 reader, int presumedEventType, QName presumedName) + throws XMLStreamException { + XMLEvent retval = reader.nextEvent(); + + int eventType = retval.getEventType(); + QName name = toQName(retval); + assert eventType == presumedEventType + && (presumedName == null + || presumedName.equals(name)) : generateExpectedMessage( + retval, + presumedEventType, + presumedName); + return retval; + } + + @NonNull + public static StartElement requireStartElement( + @NonNull XMLEventReader2 reader, + @NonNull QName presumedName) throws IOException, XMLStreamException { + XMLEvent retval = reader.nextEvent(); + if (!(retval.isStartElement() && presumedName.equals(retval.asStartElement().getName()))) { + throw new IOException(generateExpectedMessage( + retval, + XMLStreamConstants.START_ELEMENT, + presumedName).toString()); + } + return ObjectUtils.notNull(retval.asStartElement()); + } + + @NonNull + public static EndElement requireEndElement( + @NonNull XMLEventReader2 reader, + @NonNull QName presumedName) throws IOException, XMLStreamException { + XMLEvent retval = reader.nextEvent(); + if (!(retval.isEndElement() && presumedName.equals(retval.asEndElement().getName()))) { + throw new IOException(generateExpectedMessage( + retval, + XMLStreamConstants.END_ELEMENT, + presumedName).toString()); + } + return ObjectUtils.notNull(retval.asEndElement()); + } + + /** + * Assert that the next event from {@code reader} is of the type identified by + * {@code presumedEventType}. + * + * @param reader + * the event reader + * @param presumedEventType + * the expected event type as defined by {@link XMLStreamConstants} + * @return the next event + * @throws XMLStreamException + * if an error occurred while looking at the next event + * @throws AssertionError + * if the next event does not match the presumed event + */ + public static XMLEvent assertNext( + @NonNull XMLEventReader2 reader, + int presumedEventType) + throws XMLStreamException { + return assertNext(reader, presumedEventType, null); + } + + /** + * Assert that the next event from {@code reader} is of the type identified by + * {@code presumedEventType} and has the name identified by + * {@code presumedName}. + * + * @param reader + * the event reader + * @param presumedEventType + * the expected event type as defined by {@link XMLStreamConstants} + * @param presumedName + * the expected name of the node associated with the event + * @return the next event + * @throws XMLStreamException + * if an error occurred while looking at the next event + * @throws AssertionError + * if the next event does not match the presumed event + */ + public static XMLEvent assertNext( + @NonNull XMLEventReader2 reader, + int presumedEventType, + @Nullable QName presumedName) + throws XMLStreamException { + XMLEvent nextEvent = reader.peek(); + + int eventType = nextEvent.getEventType(); + assert eventType == presumedEventType + && (presumedName == null + || presumedName.equals(toQName(nextEvent))) : generateExpectedMessage( + nextEvent, + presumedEventType, + presumedName); + return nextEvent; + } + + public static CharSequence generateLocationMessage(@NonNull XMLEvent event) { + Location location = toLocation(event); + return location == null ? "" : generateLocationMessage(location); + } + + public static CharSequence generateLocationMessage(@NonNull Location location) { + return new StringBuilder(12) + .append(" at ") + .append(XmlEventUtil.toString(location)); + } + + public static CharSequence generateExpectedMessage( + @Nullable XMLEvent event, + int presumedEventType, + @Nullable QName presumedName) { + StringBuilder builder = new StringBuilder(64); + builder + .append("Expected XML ") + .append(toEventName(presumedEventType)); + + if (presumedName != null) { + builder.append(" for QName '") + .append(presumedName.toString()); + } + + if (event == null) { + builder.append("', instead found null event"); + } else { + builder.append("', instead found ") + .append(toString(event)) + .append(generateLocationMessage(event)); + } + return builder; + } + + /** + * Skips events specified by {@code events}. + * + * @param reader + * the event reader + * @param events + * the events to skip + * @return the next non-mataching event returned by + * {@link XMLEventReader2#peek()}, or {@code null} if there was no next + * event + * @throws XMLStreamException + * if an error occurred while reading + */ + public static XMLEvent skipEvents(XMLEventReader2 reader, int... events) throws XMLStreamException { + Set skipEvents = IntStream.of(events).boxed().collect(Collectors.toSet()); + + XMLEvent nextEvent = null; + while (reader.hasNext()) { + nextEvent = reader.peek(); + if (!skipEvents.contains(nextEvent.getEventType())) { + break; + } + reader.nextEvent(); + } + return nextEvent; + } +} diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/util/CollectionUtil.java b/core/src/main/java/gov/nist/secauto/metaschema/core/util/CollectionUtil.java index 4cf2a30a7..6634b4271 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/util/CollectionUtil.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/util/CollectionUtil.java @@ -1,261 +1,261 @@ -/* - * Portions of this software was developed by employees of the National Institute - * of Standards and Technology (NIST), an agency of the Federal Government and is - * being made available as a public service. Pursuant to title 17 United States - * Code Section 105, works of NIST employees are not subject to copyright - * protection in the United States. This software may be subject to foreign - * copyright. Permission in the United States and in foreign countries, to the - * extent that NIST may hold copyright, to use, copy, modify, create derivative - * works, and distribute this software and its documentation without fee is hereby - * granted on a non-exclusive basis, provided that this notice and disclaimer - * of warranty appears in all copies. - * - * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER - * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY - * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM - * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE - * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT - * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, - * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, - * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, - * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR - * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT - * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. - */ - -package gov.nist.secauto.metaschema.core.util; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.Collections; -import java.util.Iterator; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.Spliterator; -import java.util.Spliterators; -import java.util.stream.Collectors; -import java.util.stream.IntStream; -import java.util.stream.Stream; -import java.util.stream.StreamSupport; - -import edu.umd.cs.findbugs.annotations.NonNull; -import edu.umd.cs.findbugs.annotations.Nullable; - -public final class CollectionUtil { - - private CollectionUtil() { - // disable construction - } - - /** - * Get a {@link Stream} for the provided {@link Iterable}. - * - * @param - * the type to iterate on - * @param iterator - * the iterator - * @return the stream - */ - public static Stream toStream(@NonNull Iterator iterator) { - Iterable iterable = toIterable(iterator); - return StreamSupport.stream(iterable.spliterator(), false); - } - - /** - * Get an {@link Iterable} for the provided {@link Stream}. - * - * @param - * the type to iterate on - * @param stream - * the stream to iterate over - * @return the resulting iterable instance - */ - @NonNull - public static Iterable toIterable(@NonNull Stream stream) { - return toIterable(ObjectUtils.notNull(stream.iterator())); - } - - /** - * Get an {@link Iterable} for the provided {@link Iterator}. - * - * @param - * the type to iterate on - * @param iterator - * the iterator - * @return the resulting iterable instance - */ - @NonNull - public static Iterable toIterable(@NonNull Iterator iterator) { - return () -> iterator; - } - - /** - * Get a reverse {@link Iterable} for the provided {@link List}. - * - * @param - * the type to iterate on - * @param list - * the list of items to iterate over - * @return the resulting iterable instance - */ - @NonNull - public static Iterable toDescendingIterable(@NonNull List list) { - return toIterable(descendingIterator(list)); - } - - /** - * Convert the provided {@link Iterable} to a list of the same generic type. - * - * @param - * the collection item's generic type - * @param iterable - * the Iterable to convert to a list - * @return the list - */ - @NonNull - public static List toList(Iterable iterable) { - return ObjectUtils.notNull(StreamSupport.stream(iterable.spliterator(), false).collect(Collectors.toList())); - } - - /** - * Convert the provided {@link Iterator} to a list of the same generic type. - * - * @param - * the collection item's generic type - * @param iterator - * the Iterator to convert to a list - * @return the list - */ - @NonNull - public static List toList(Iterator iterator) { - return ObjectUtils.notNull( - StreamSupport.stream(Spliterators.spliteratorUnknownSize(iterator, Spliterator.ORDERED), false) - .collect(Collectors.toList())); - } - - /** - * Get a reverse {@link Iterator} for the provided {@link List}. - * - * @param - * the type to iterate on - * @param list - * the list of items to iterate over - * @return the resulting Iterator instance - */ - @NonNull - public static Iterator descendingIterator(@NonNull List list) { - Iterator retval; - if (list instanceof LinkedList) { - retval = ((LinkedList) list).descendingIterator(); - } else if (list instanceof ArrayList) { - retval = IntStream.range(0, list.size()) - .map(i -> list.size() - 1 - i) - .mapToObj(list::get).iterator(); - } else { - throw new UnsupportedOperationException(); - } - return ObjectUtils.notNull(retval); - } - - @NonNull - public static , A> T requireNonEmpty(@NonNull T collection) { - if (collection.isEmpty()) { - throw new IllegalStateException(); - } - return collection; - } - - @NonNull - public static , A> T requireNonEmpty(@NonNull T collection, @NonNull String message) { - if (collection.isEmpty()) { - throw new IllegalStateException(message); - } - return collection; - } - - /** - * A wrapper of the {@link Collections#unmodifiableCollection(Collection)} - * method that ensure a {@link NonNull} result is returned. - * - * @param - * the collection's item type - * @param collection - * the collection - * @return a non-null unmodifiable instance of the provided collection - */ - @SuppressWarnings("null") - @NonNull - public static Collection unmodifiableCollection(@NonNull Collection collection) { - return Collections.unmodifiableCollection(collection); - } - - @SuppressWarnings("null") - @NonNull - public static Set singleton(@NonNull T value) { - return Collections.singleton(value); - } - - @SuppressWarnings("null") - @NonNull - public static Set emptySet() { - return Collections.emptySet(); - } - - @SuppressWarnings("null") - @NonNull - public static Set unmodifiableSet(@NonNull Set set) { - return Collections.unmodifiableSet(set); - } - - @NonNull - public static List listOrEmpty(@Nullable List list) { - return list == null ? emptyList() : list; - } - - @SafeVarargs - @SuppressWarnings("null") - @NonNull - public static List listOrEmpty(@Nullable T... array) { - return array == null || array.length == 0 ? emptyList() : Arrays.asList(array); - } - - @SuppressWarnings("null") - @NonNull - public static List emptyList() { - return Collections.emptyList(); - } - - @SuppressWarnings("null") - @NonNull - public static List unmodifiableList(@NonNull List list) { - return Collections.unmodifiableList(list); - } - - @SuppressWarnings("null") - @NonNull - public static List singletonList(@NonNull T instance) { - return Collections.singletonList(instance); - } - - @SuppressWarnings("null") - @NonNull - public static Map emptyMap() { - return Collections.emptyMap(); - } - - @SuppressWarnings("null") - @NonNull - public static Map singletonMap(@NonNull K key, @NonNull V value) { - return Collections.singletonMap(key, value); - } - - @SuppressWarnings("null") - @NonNull - public static Map unmodifiableMap(@NonNull Map map) { - return Collections.unmodifiableMap(map); - } -} +/* + * Portions of this software was developed by employees of the National Institute + * of Standards and Technology (NIST), an agency of the Federal Government and is + * being made available as a public service. Pursuant to title 17 United States + * Code Section 105, works of NIST employees are not subject to copyright + * protection in the United States. This software may be subject to foreign + * copyright. Permission in the United States and in foreign countries, to the + * extent that NIST may hold copyright, to use, copy, modify, create derivative + * works, and distribute this software and its documentation without fee is hereby + * granted on a non-exclusive basis, provided that this notice and disclaimer + * of warranty appears in all copies. + * + * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER + * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY + * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM + * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE + * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT + * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, + * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, + * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, + * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR + * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT + * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. + */ + +package gov.nist.secauto.metaschema.core.util; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.Spliterator; +import java.util.Spliterators; +import java.util.stream.Collectors; +import java.util.stream.IntStream; +import java.util.stream.Stream; +import java.util.stream.StreamSupport; + +import edu.umd.cs.findbugs.annotations.NonNull; +import edu.umd.cs.findbugs.annotations.Nullable; + +public final class CollectionUtil { + + private CollectionUtil() { + // disable construction + } + + /** + * Get a {@link Stream} for the provided {@link Iterable}. + * + * @param + * the type to iterate on + * @param iterator + * the iterator + * @return the stream + */ + public static Stream toStream(@NonNull Iterator iterator) { + Iterable iterable = toIterable(iterator); + return StreamSupport.stream(iterable.spliterator(), false); + } + + /** + * Get an {@link Iterable} for the provided {@link Stream}. + * + * @param + * the type to iterate on + * @param stream + * the stream to iterate over + * @return the resulting iterable instance + */ + @NonNull + public static Iterable toIterable(@NonNull Stream stream) { + return toIterable(ObjectUtils.notNull(stream.iterator())); + } + + /** + * Get an {@link Iterable} for the provided {@link Iterator}. + * + * @param + * the type to iterate on + * @param iterator + * the iterator + * @return the resulting iterable instance + */ + @NonNull + public static Iterable toIterable(@NonNull Iterator iterator) { + return () -> iterator; + } + + /** + * Get a reverse {@link Iterable} for the provided {@link List}. + * + * @param + * the type to iterate on + * @param list + * the list of items to iterate over + * @return the resulting iterable instance + */ + @NonNull + public static Iterable toDescendingIterable(@NonNull List list) { + return toIterable(descendingIterator(list)); + } + + /** + * Convert the provided {@link Iterable} to a list of the same generic type. + * + * @param + * the collection item's generic type + * @param iterable + * the Iterable to convert to a list + * @return the list + */ + @NonNull + public static List toList(Iterable iterable) { + return ObjectUtils.notNull(StreamSupport.stream(iterable.spliterator(), false).collect(Collectors.toList())); + } + + /** + * Convert the provided {@link Iterator} to a list of the same generic type. + * + * @param + * the collection item's generic type + * @param iterator + * the Iterator to convert to a list + * @return the list + */ + @NonNull + public static List toList(Iterator iterator) { + return ObjectUtils.notNull( + StreamSupport.stream(Spliterators.spliteratorUnknownSize(iterator, Spliterator.ORDERED), false) + .collect(Collectors.toList())); + } + + /** + * Get a reverse {@link Iterator} for the provided {@link List}. + * + * @param + * the type to iterate on + * @param list + * the list of items to iterate over + * @return the resulting Iterator instance + */ + @NonNull + public static Iterator descendingIterator(@NonNull List list) { + Iterator retval; + if (list instanceof LinkedList) { + retval = ((LinkedList) list).descendingIterator(); + } else if (list instanceof ArrayList) { + retval = IntStream.range(0, list.size()) + .map(i -> list.size() - 1 - i) + .mapToObj(list::get).iterator(); + } else { + throw new UnsupportedOperationException(); + } + return ObjectUtils.notNull(retval); + } + + @NonNull + public static , A> T requireNonEmpty(@NonNull T collection) { + if (collection.isEmpty()) { + throw new IllegalStateException(); + } + return collection; + } + + @NonNull + public static , A> T requireNonEmpty(@NonNull T collection, @NonNull String message) { + if (collection.isEmpty()) { + throw new IllegalStateException(message); + } + return collection; + } + + /** + * A wrapper of the {@link Collections#unmodifiableCollection(Collection)} + * method that ensure a {@link NonNull} result is returned. + * + * @param + * the collection's item type + * @param collection + * the collection + * @return a non-null unmodifiable instance of the provided collection + */ + @SuppressWarnings("null") + @NonNull + public static Collection unmodifiableCollection(@NonNull Collection collection) { + return Collections.unmodifiableCollection(collection); + } + + @SuppressWarnings("null") + @NonNull + public static Set singleton(@NonNull T value) { + return Collections.singleton(value); + } + + @SuppressWarnings("null") + @NonNull + public static Set emptySet() { + return Collections.emptySet(); + } + + @SuppressWarnings("null") + @NonNull + public static Set unmodifiableSet(@NonNull Set set) { + return Collections.unmodifiableSet(set); + } + + @NonNull + public static List listOrEmpty(@Nullable List list) { + return list == null ? emptyList() : list; + } + + @SafeVarargs + @SuppressWarnings("null") + @NonNull + public static List listOrEmpty(@Nullable T... array) { + return array == null || array.length == 0 ? emptyList() : Arrays.asList(array); + } + + @SuppressWarnings("null") + @NonNull + public static List emptyList() { + return Collections.emptyList(); + } + + @SuppressWarnings("null") + @NonNull + public static List unmodifiableList(@NonNull List list) { + return Collections.unmodifiableList(list); + } + + @SuppressWarnings("null") + @NonNull + public static List singletonList(@NonNull T instance) { + return Collections.singletonList(instance); + } + + @SuppressWarnings("null") + @NonNull + public static Map emptyMap() { + return Collections.emptyMap(); + } + + @SuppressWarnings("null") + @NonNull + public static Map singletonMap(@NonNull K key, @NonNull V value) { + return Collections.singletonMap(key, value); + } + + @SuppressWarnings("null") + @NonNull + public static Map unmodifiableMap(@NonNull Map map) { + return Collections.unmodifiableMap(map); + } +} diff --git a/core/src/test/java/gov/nist/secauto/metaschema/core/datatype/markup/MarkupStringTest.java b/core/src/test/java/gov/nist/secauto/metaschema/core/datatype/markup/MarkupStringTest.java index 36680e7c8..540c69b31 100644 --- a/core/src/test/java/gov/nist/secauto/metaschema/core/datatype/markup/MarkupStringTest.java +++ b/core/src/test/java/gov/nist/secauto/metaschema/core/datatype/markup/MarkupStringTest.java @@ -1,394 +1,394 @@ -/* - * Portions of this software was developed by employees of the National Institute - * of Standards and Technology (NIST), an agency of the Federal Government and is - * being made available as a public service. Pursuant to title 17 United States - * Code Section 105, works of NIST employees are not subject to copyright - * protection in the United States. This software may be subject to foreign - * copyright. Permission in the United States and in foreign countries, to the - * extent that NIST may hold copyright, to use, copy, modify, create derivative - * works, and distribute this software and its documentation without fee is hereby - * granted on a non-exclusive basis, provided that this notice and disclaimer - * of warranty appears in all copies. - * - * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER - * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY - * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM - * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE - * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT - * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, - * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, - * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, - * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR - * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT - * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. - */ - -package gov.nist.secauto.metaschema.core.datatype.markup; - -import com.ctc.wstx.api.WstxOutputProperties; -import com.ctc.wstx.stax.WstxOutputFactory; -import com.vladsch.flexmark.ast.Emphasis; -import com.vladsch.flexmark.ast.Heading; -import com.vladsch.flexmark.ast.Paragraph; -import com.vladsch.flexmark.ast.StrongEmphasis; -import com.vladsch.flexmark.ast.Text; -import com.vladsch.flexmark.ast.TextBase; -import com.vladsch.flexmark.ext.escaped.character.EscapedCharacter; -import com.vladsch.flexmark.util.ast.Document; -import com.vladsch.flexmark.util.ast.Node; - -import gov.nist.secauto.metaschema.core.datatype.markup.flexmark.AstCollectingVisitor; -import gov.nist.secauto.metaschema.core.datatype.markup.flexmark.InsertAnchorExtension.InsertAnchorNode; -import gov.nist.secauto.metaschema.core.util.CollectionUtil; - -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; -import org.codehaus.stax2.XMLOutputFactory2; -import org.codehaus.stax2.XMLStreamWriter2; -import org.codehaus.stax2.ri.evt.MergedNsContext; -import org.codehaus.stax2.ri.evt.NamespaceEventImpl; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; - -import java.io.IOException; -import java.io.StringWriter; -import java.util.List; - -import javax.xml.namespace.NamespaceContext; -import javax.xml.stream.XMLOutputFactory; -import javax.xml.stream.XMLStreamException; - -import edu.umd.cs.findbugs.annotations.NonNull; - -class MarkupStringTest { - private static final Logger LOGGER = LogManager.getLogger(MarkupStringTest.class); - private static final String MARKUP_HTML_NAMESPACE = "http://www.w3.org/1999/xhtml"; - private static final String MARKUP_HTML_PREFIX = ""; - - @NonNull - XMLStreamWriter2 newXmlStreamWriter(@NonNull StringWriter stringWriter) throws XMLStreamException { - XMLOutputFactory2 factory = (XMLOutputFactory2) XMLOutputFactory.newInstance(); - assert factory instanceof WstxOutputFactory; - factory.setProperty(WstxOutputProperties.P_OUTPUT_VALIDATE_STRUCTURE, false); - XMLStreamWriter2 xmlStreamWriter = (XMLStreamWriter2) factory.createXMLStreamWriter(stringWriter); - NamespaceContext nsContext = MergedNsContext.construct(xmlStreamWriter.getNamespaceContext(), - List.of(NamespaceEventImpl.constructNamespace(null, MARKUP_HTML_PREFIX, MARKUP_HTML_NAMESPACE))); - xmlStreamWriter.setNamespaceContext(nsContext); - return xmlStreamWriter; - } - - @Test - void markupLineFromMarkdownTest() { - String markdown = "Some \\**more* **text** and a param: {{ insert: param, insert }}."; - - MarkupLine ms = MarkupLine.fromMarkdown(markdown); - Document document = ms.getDocument(); - - Assertions.assertNotNull(document); - - LOGGER.atDebug().log("AST: {}", AstCollectingVisitor.asString(document)); - LOGGER.atDebug().log("HTML: {}", ms.toHtml()); - LOGGER.atDebug().log("Markdown: {}", ms.toMarkdown()); - - // Document[0, 49] - List documentChildren = CollectionUtil.toList(document.getChildren()); - { - // Paragraph[0, 49] - // ensure there is a single paragraph - Assertions.assertEquals(1, documentChildren.size()); - Node paragraph = documentChildren.get(0); - Assertions.assertTrue(paragraph instanceof Paragraph); - - List paragraphChildren = CollectionUtil.toList(paragraph.getChildren()); - // TextBase[0, 7] chars:[0, 7, "Some \*"] - { - TextBase textBase = (TextBase) paragraphChildren.get(0); - List textBaseChildren = CollectionUtil.toList(textBase.getChildren()); - // Text[0, 5] chars:[0, 5, "Some "] - { - Text text = (Text) textBaseChildren.get(0); - Assertions.assertEquals("Some ", text.getChars().toString()); - } - // EscapedCharacter[5, 7] textOpen:[5, 6, "\"] text:[6, 7, "*"] - { - EscapedCharacter text = (EscapedCharacter) textBaseChildren.get(1); - Assertions.assertEquals("*", text.getText().toString()); - } - } - // Emphasis[7, 13] textOpen:[7, 8, "*"] text:[8, 12, "more"] textClose:[12, 13, - // "*"] - { - Emphasis emphasis = (Emphasis) paragraphChildren.get(1); - List emphasisChildren = CollectionUtil.toList(emphasis.getChildren()); - // Text[8, 12] chars:[8, 12, "more"] - { - Text text = (Text) emphasisChildren.get(0); - Assertions.assertEquals("more", text.getChars().toString()); - } - } - // Text[13, 14] chars:[13, 14, " "] - { - Text text = (Text) paragraphChildren.get(2); - Assertions.assertEquals(" ", text.getChars().toString()); - } - // StrongEmphasis[14, 22] textOpen:[14, 16, "**"] text:[16, 20, "text"] - // textClose:[20, 22, "**"] - { - StrongEmphasis strongEmphasis = (StrongEmphasis) paragraphChildren.get(3); - List strongEmphasisChildren = CollectionUtil.toList(strongEmphasis.getChildren()); - // Text[16, 20] chars:[16, 20, "text"] - { - Text text = (Text) strongEmphasisChildren.get(0); - Assertions.assertEquals("text", text.getChars().toString()); - } - - } - // Text[22, 36] chars:[22, 36, " and … ram: "] - { - Text text = (Text) paragraphChildren.get(4); - Assertions.assertEquals(" and a param: ", text.getChars().toString()); - } - // InsertAnchorNode[0, 0] name:[39, 45, "insert"] - { - @SuppressWarnings("unused") InsertAnchorNode insert = (InsertAnchorNode) paragraphChildren.get(5); - } - // Text[48, 49] chars:[48, 49, "."] - { - Text text = (Text) paragraphChildren.get(6); - Assertions.assertEquals(".", text.getChars().toString()); - } - } - - Assertions.assertEquals(markdown, ms.toMarkdown()); - - String html - = "Some *more text and a param: ."; - - Assertions.assertEquals(html, ms.toHtml()); - } - - @Test - void markupMultilineFromMarkdownTest() throws XMLStreamException, IOException { - final String markdown = "# Example\n\nSome \"\\**more*\" **text**\n\nA param: {{ insert: param, insert }}."; - final String html = "

Example

\n" - + "

Some *more text

\n" - + "

A param: .

"; - - MarkupMultiline ms = MarkupMultiline.fromMarkdown(markdown); - LOGGER.atDebug().log("AST: {}", AstCollectingVisitor.asString(ms.getDocument())); - LOGGER.atDebug().log("HTML: {}", ms.toXHtml("")); - LOGGER.atDebug().log("Markdown: {}", ms.toMarkdown()); - - Assertions.assertEquals(markdown, ms.toMarkdown()); - Assertions.assertEquals(html, ms.toXHtml("")); - } - - @Test - void markupMultilineFromHtmlTest() throws XMLStreamException, IOException { - final String html = "

Example

\n" - + "

textquote1

\n" - + "\n" - + "\n" - + "\n" - + "\n" - + "\n" - + "\n" - + "\n" - + "\n" - + "
Heading 1
data1
data2
\n" - + "

Some more text \"alt\"/

"; - final String markdown = "# Example\n" - + "\n" - + "[text](link)\"quote1\"\n" - + "\n" - + "| Heading 1 |\n" - + "|-------------------------------------|\n" - + "| \"data1\" {{ insert: param, insert }} |\n" - + "| \"data2\" {{ insert: param, insert }} |\n" - + "\n" - + "Some \"*more*\" **text** ![alt](src)"; - MarkupMultiline ms = MarkupMultiline.fromHtml(html); - LOGGER.atDebug().log("HTML Source: {}", html); - LOGGER.atDebug().log("AST: {}", AstCollectingVisitor.asString(ms.getDocument())); - LOGGER.atDebug().log("HTML: {}", ms.toXHtml("")); - LOGGER.atDebug().log("Markdown: {}", ms.toMarkdown()); - Assertions.assertEquals(markdown, ms.toMarkdown()); - Assertions.assertEquals(html, ms.toXHtml("")); - } - - /* - * {@link HtmlConverterCoreNodeRenderer} has a bug on line 629 in the call to "wrapTextNodes", which - * can add spaces to the HTML string. This relates to an existing issue. - */ - @Test - void markupSpaceHandlingTest() throws XMLStreamException, IOException { - final String html = "

a b c

"; - final String markdown = "a *b* **c**"; - MarkupMultiline ms = MarkupMultiline.fromHtml(html); - LOGGER.atDebug().log("HTML Source: {}", html); - LOGGER.atDebug().log("AST: {}", AstCollectingVisitor.asString(ms.getDocument())); - LOGGER.atDebug().log("HTML: {}", ms.toXHtml("")); - LOGGER.atDebug().log("Markdown: {}", ms.toMarkdown()); - Assertions.assertNotEquals(markdown, ms.toMarkdown()); - Assertions.assertEquals(html, ms.toXHtml("")); - } - - @Test - void preMarkdown() { - String htmlPreOnly = "
Example **some** *code*
"; - final String html = "
Example **some** *code*\n"
-        + "
"; - final String markdown = "```\n" - + "Example **some** *code*\n" - + "```"; - - MarkupMultiline ms = MarkupMultiline.fromHtml(htmlPreOnly); - - LOGGER.atDebug().log("AST: {}", AstCollectingVisitor.asString(ms.getDocument())); - LOGGER.atDebug().log("HTML: {}", ms.toHtml()); - LOGGER.atDebug().log("Markdown: {}", ms.toMarkdown()); - - Assertions.assertEquals(markdown, ms.toMarkdown()); - Assertions.assertEquals(html, ms.toHtml()); - } - - @Test - void preCodeMarkdown() { - final String html = "
Example **some** *code*\n"
-        + "nextline\n"
-        + "
"; - final String markdown = " Example **some** *code*\n" - + " nextline\n"; - - MarkupMultiline ms = MarkupMultiline.fromHtml(html); - - LOGGER.atDebug().log("AST: {}", AstCollectingVisitor.asString(ms.getDocument())); - LOGGER.atDebug().log("HTML: {}", ms.toHtml()); - LOGGER.atDebug().log("Markdown: {}", ms.toMarkdown()); - - Assertions.assertEquals(markdown, ms.toMarkdown()); - Assertions.assertEquals(html, ms.toHtml()); - } - - @Test - void paragraphCodeMarkdown() { - final String html = "

Example**some** *code*

"; - final String markdown = "Example`**some** *code*`"; - MarkupMultiline ms = MarkupMultiline.fromHtml(html); - - LOGGER.atDebug().log("AST: {}", AstCollectingVisitor.asString(ms.getDocument())); - LOGGER.atDebug().log("HTML: {}", ms.toHtml()); - LOGGER.atDebug().log("Markdown: {}", ms.toMarkdown()); - - Assertions.assertEquals(markdown, ms.toMarkdown()); - Assertions.assertEquals(html, ms.toHtml()); - } - - @Test - void testEntityRoundTrip() throws XMLStreamException { - final String markdown = "hijacked was used (e.g., the <CTRL> + <ALT> + <DEL> keys)."; - final String html = "hijacked was used (e.g., the <CTRL> + <ALT> + <DEL> keys)."; - final String xhtml = "

hijacked was used (e.g., the <CTRL> + <ALT> + <DEL> keys).

"; - - MarkupLine ms = MarkupLine.fromMarkdown(markdown); - Document document = ms.getDocument(); - - Assertions.assertNotNull(document); - - LOGGER.atDebug().log("AST: {}", AstCollectingVisitor.asString(ms.getDocument())); - LOGGER.atDebug().log("HTML: {}", ms.toHtml()); - LOGGER.atDebug().log("Markdown: {}", ms.toMarkdown()); - - Assertions.assertEquals(markdown, ms.toMarkdown()); - Assertions.assertEquals(html, ms.toHtml().trim()); - - StringWriter stringWriter = new StringWriter(); - XMLStreamWriter2 xmlStreamWriter = newXmlStreamWriter(stringWriter); - - xmlStreamWriter.writeStartElement(MARKUP_HTML_NAMESPACE, "p"); - - ms.writeXHtml(MARKUP_HTML_NAMESPACE, xmlStreamWriter); - - xmlStreamWriter.writeEndElement(); - - xmlStreamWriter.close(); - - Assertions.assertEquals(xhtml, stringWriter.toString()); - } - - @Test - void testAposRoundTrip() throws XMLStreamException { - String markdown = "a user’s identity"; - - // test from Markdown source - MarkupLine ms = MarkupLine.fromMarkdown(markdown); - Document document = ms.getDocument(); - - Assertions.assertNotNull(document); - - LOGGER.atDebug().log("AST: {}", AstCollectingVisitor.asString(ms.getDocument())); - LOGGER.atDebug().log("HTML: {}", ms.toHtml()); - LOGGER.atDebug().log("Markdown: {}", ms.toMarkdown()); - - Assertions.assertEquals(markdown, ms.toMarkdown()); - - String html = "a user’s identity"; - Assertions.assertEquals(html, ms.toHtml().trim()); - - StringWriter stringWriter = new StringWriter(); - XMLStreamWriter2 xmlStreamWriter = newXmlStreamWriter(stringWriter); - - xmlStreamWriter.writeStartElement(MARKUP_HTML_NAMESPACE, "p"); - - ms.writeXHtml(MARKUP_HTML_NAMESPACE, xmlStreamWriter); - - xmlStreamWriter.writeEndElement(); - - xmlStreamWriter.close(); - - Assertions.assertEquals("

" + html + "

", stringWriter.toString()); - - // test from HTML source - ms = MarkupLine.fromHtml(html); - document = ms.getDocument(); - - Assertions.assertNotNull(document); - LOGGER.atDebug().log("AST: {}", AstCollectingVisitor.asString(ms.getDocument())); - LOGGER.atDebug().log("HTML: {}", ms.toHtml()); - LOGGER.atDebug().log("Markdown: {}", ms.toMarkdown()); - Assertions.assertEquals(markdown, ms.toMarkdown()); - Assertions.assertEquals(html, ms.toHtml().trim()); - } - - @Test - void testHtml() { - String html = "

before <thing[02] text> after

"; - MarkupMultiline ms = MarkupMultiline.fromHtml(html); - - LOGGER.atDebug().log("AST: {}", AstCollectingVisitor.asString(ms.getDocument())); - LOGGER.atDebug().log("HTML: {}", ms.toHtml()); - LOGGER.atDebug().log("Markdown: {}", ms.toMarkdown()); - } - - @Test - void testIntraTagNewline() { - // addresses usnistgov/liboscal-java#5 - String html = "

A custom title\n" + - " with italic\n" + - "

"; - MarkupMultiline ms = MarkupMultiline.fromHtml(html); - - Document doc = ms.getDocument(); - LOGGER.atDebug().log("AST: {}", AstCollectingVisitor.asString(doc)); - LOGGER.atDebug().log("HTML: {}", ms.toHtml()); - LOGGER.atDebug().log("Markdown: {}", ms.toMarkdown()); - - List children = CollectionUtil.toList(doc.getChildren()); - // ensure there is only 1 child and that it is a heading - Assertions.assertAll( - () -> Assertions.assertEquals(1, children.size()), - () -> Assertions.assertEquals(Heading.class, children.get(0).getClass())); - } -} +/* + * Portions of this software was developed by employees of the National Institute + * of Standards and Technology (NIST), an agency of the Federal Government and is + * being made available as a public service. Pursuant to title 17 United States + * Code Section 105, works of NIST employees are not subject to copyright + * protection in the United States. This software may be subject to foreign + * copyright. Permission in the United States and in foreign countries, to the + * extent that NIST may hold copyright, to use, copy, modify, create derivative + * works, and distribute this software and its documentation without fee is hereby + * granted on a non-exclusive basis, provided that this notice and disclaimer + * of warranty appears in all copies. + * + * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER + * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY + * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM + * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE + * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT + * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, + * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, + * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, + * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR + * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT + * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. + */ + +package gov.nist.secauto.metaschema.core.datatype.markup; + +import com.ctc.wstx.api.WstxOutputProperties; +import com.ctc.wstx.stax.WstxOutputFactory; +import com.vladsch.flexmark.ast.Emphasis; +import com.vladsch.flexmark.ast.Heading; +import com.vladsch.flexmark.ast.Paragraph; +import com.vladsch.flexmark.ast.StrongEmphasis; +import com.vladsch.flexmark.ast.Text; +import com.vladsch.flexmark.ast.TextBase; +import com.vladsch.flexmark.ext.escaped.character.EscapedCharacter; +import com.vladsch.flexmark.util.ast.Document; +import com.vladsch.flexmark.util.ast.Node; + +import gov.nist.secauto.metaschema.core.datatype.markup.flexmark.AstCollectingVisitor; +import gov.nist.secauto.metaschema.core.datatype.markup.flexmark.InsertAnchorExtension.InsertAnchorNode; +import gov.nist.secauto.metaschema.core.util.CollectionUtil; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.codehaus.stax2.XMLOutputFactory2; +import org.codehaus.stax2.XMLStreamWriter2; +import org.codehaus.stax2.ri.evt.MergedNsContext; +import org.codehaus.stax2.ri.evt.NamespaceEventImpl; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.io.IOException; +import java.io.StringWriter; +import java.util.List; + +import javax.xml.namespace.NamespaceContext; +import javax.xml.stream.XMLOutputFactory; +import javax.xml.stream.XMLStreamException; + +import edu.umd.cs.findbugs.annotations.NonNull; + +class MarkupStringTest { + private static final Logger LOGGER = LogManager.getLogger(MarkupStringTest.class); + private static final String MARKUP_HTML_NAMESPACE = "http://www.w3.org/1999/xhtml"; + private static final String MARKUP_HTML_PREFIX = ""; + + @NonNull + XMLStreamWriter2 newXmlStreamWriter(@NonNull StringWriter stringWriter) throws XMLStreamException { + XMLOutputFactory2 factory = (XMLOutputFactory2) XMLOutputFactory.newInstance(); + assert factory instanceof WstxOutputFactory; + factory.setProperty(WstxOutputProperties.P_OUTPUT_VALIDATE_STRUCTURE, false); + XMLStreamWriter2 xmlStreamWriter = (XMLStreamWriter2) factory.createXMLStreamWriter(stringWriter); + NamespaceContext nsContext = MergedNsContext.construct(xmlStreamWriter.getNamespaceContext(), + List.of(NamespaceEventImpl.constructNamespace(null, MARKUP_HTML_PREFIX, MARKUP_HTML_NAMESPACE))); + xmlStreamWriter.setNamespaceContext(nsContext); + return xmlStreamWriter; + } + + @Test + void markupLineFromMarkdownTest() { + String markdown = "Some \\**more* **text** and a param: {{ insert: param, insert }}."; + + MarkupLine ms = MarkupLine.fromMarkdown(markdown); + Document document = ms.getDocument(); + + Assertions.assertNotNull(document); + + LOGGER.atDebug().log("AST: {}", AstCollectingVisitor.asString(document)); + LOGGER.atDebug().log("HTML: {}", ms.toHtml()); + LOGGER.atDebug().log("Markdown: {}", ms.toMarkdown()); + + // Document[0, 49] + List documentChildren = CollectionUtil.toList(document.getChildren()); + { + // Paragraph[0, 49] + // ensure there is a single paragraph + Assertions.assertEquals(1, documentChildren.size()); + Node paragraph = documentChildren.get(0); + Assertions.assertTrue(paragraph instanceof Paragraph); + + List paragraphChildren = CollectionUtil.toList(paragraph.getChildren()); + // TextBase[0, 7] chars:[0, 7, "Some \*"] + { + TextBase textBase = (TextBase) paragraphChildren.get(0); + List textBaseChildren = CollectionUtil.toList(textBase.getChildren()); + // Text[0, 5] chars:[0, 5, "Some "] + { + Text text = (Text) textBaseChildren.get(0); + Assertions.assertEquals("Some ", text.getChars().toString()); + } + // EscapedCharacter[5, 7] textOpen:[5, 6, "\"] text:[6, 7, "*"] + { + EscapedCharacter text = (EscapedCharacter) textBaseChildren.get(1); + Assertions.assertEquals("*", text.getText().toString()); + } + } + // Emphasis[7, 13] textOpen:[7, 8, "*"] text:[8, 12, "more"] textClose:[12, 13, + // "*"] + { + Emphasis emphasis = (Emphasis) paragraphChildren.get(1); + List emphasisChildren = CollectionUtil.toList(emphasis.getChildren()); + // Text[8, 12] chars:[8, 12, "more"] + { + Text text = (Text) emphasisChildren.get(0); + Assertions.assertEquals("more", text.getChars().toString()); + } + } + // Text[13, 14] chars:[13, 14, " "] + { + Text text = (Text) paragraphChildren.get(2); + Assertions.assertEquals(" ", text.getChars().toString()); + } + // StrongEmphasis[14, 22] textOpen:[14, 16, "**"] text:[16, 20, "text"] + // textClose:[20, 22, "**"] + { + StrongEmphasis strongEmphasis = (StrongEmphasis) paragraphChildren.get(3); + List strongEmphasisChildren = CollectionUtil.toList(strongEmphasis.getChildren()); + // Text[16, 20] chars:[16, 20, "text"] + { + Text text = (Text) strongEmphasisChildren.get(0); + Assertions.assertEquals("text", text.getChars().toString()); + } + + } + // Text[22, 36] chars:[22, 36, " and … ram: "] + { + Text text = (Text) paragraphChildren.get(4); + Assertions.assertEquals(" and a param: ", text.getChars().toString()); + } + // InsertAnchorNode[0, 0] name:[39, 45, "insert"] + { + @SuppressWarnings("unused") InsertAnchorNode insert = (InsertAnchorNode) paragraphChildren.get(5); + } + // Text[48, 49] chars:[48, 49, "."] + { + Text text = (Text) paragraphChildren.get(6); + Assertions.assertEquals(".", text.getChars().toString()); + } + } + + Assertions.assertEquals(markdown, ms.toMarkdown()); + + String html + = "Some *more text and a param: ."; + + Assertions.assertEquals(html, ms.toHtml()); + } + + @Test + void markupMultilineFromMarkdownTest() throws XMLStreamException, IOException { + final String markdown = "# Example\n\nSome \"\\**more*\" **text**\n\nA param: {{ insert: param, insert }}."; + final String html = "

Example

\n" + + "

Some *more text

\n" + + "

A param: .

"; + + MarkupMultiline ms = MarkupMultiline.fromMarkdown(markdown); + LOGGER.atDebug().log("AST: {}", AstCollectingVisitor.asString(ms.getDocument())); + LOGGER.atDebug().log("HTML: {}", ms.toXHtml("")); + LOGGER.atDebug().log("Markdown: {}", ms.toMarkdown()); + + Assertions.assertEquals(markdown, ms.toMarkdown()); + Assertions.assertEquals(html, ms.toXHtml("")); + } + + @Test + void markupMultilineFromHtmlTest() throws XMLStreamException, IOException { + final String html = "

Example

\n" + + "

textquote1

\n" + + "\n" + + "\n" + + "\n" + + "\n" + + "\n" + + "\n" + + "\n" + + "\n" + + "
Heading 1
data1
data2
\n" + + "

Some more text \"alt\"/

"; + final String markdown = "# Example\n" + + "\n" + + "[text](link)\"quote1\"\n" + + "\n" + + "| Heading 1 |\n" + + "|-------------------------------------|\n" + + "| \"data1\" {{ insert: param, insert }} |\n" + + "| \"data2\" {{ insert: param, insert }} |\n" + + "\n" + + "Some \"*more*\" **text** ![alt](src)"; + MarkupMultiline ms = MarkupMultiline.fromHtml(html); + LOGGER.atDebug().log("HTML Source: {}", html); + LOGGER.atDebug().log("AST: {}", AstCollectingVisitor.asString(ms.getDocument())); + LOGGER.atDebug().log("HTML: {}", ms.toXHtml("")); + LOGGER.atDebug().log("Markdown: {}", ms.toMarkdown()); + Assertions.assertEquals(markdown, ms.toMarkdown()); + Assertions.assertEquals(html, ms.toXHtml("")); + } + + /* + * {@link HtmlConverterCoreNodeRenderer} has a bug on line 629 in the call to "wrapTextNodes", which + * can add spaces to the HTML string. This relates to an existing issue. + */ + @Test + void markupSpaceHandlingTest() throws XMLStreamException, IOException { + final String html = "

a b c

"; + final String markdown = "a *b* **c**"; + MarkupMultiline ms = MarkupMultiline.fromHtml(html); + LOGGER.atDebug().log("HTML Source: {}", html); + LOGGER.atDebug().log("AST: {}", AstCollectingVisitor.asString(ms.getDocument())); + LOGGER.atDebug().log("HTML: {}", ms.toXHtml("")); + LOGGER.atDebug().log("Markdown: {}", ms.toMarkdown()); + Assertions.assertNotEquals(markdown, ms.toMarkdown()); + Assertions.assertEquals(html, ms.toXHtml("")); + } + + @Test + void preMarkdown() { + String htmlPreOnly = "
Example **some** *code*
"; + final String html = "
Example **some** *code*\n"
+        + "
"; + final String markdown = "```\n" + + "Example **some** *code*\n" + + "```"; + + MarkupMultiline ms = MarkupMultiline.fromHtml(htmlPreOnly); + + LOGGER.atDebug().log("AST: {}", AstCollectingVisitor.asString(ms.getDocument())); + LOGGER.atDebug().log("HTML: {}", ms.toHtml()); + LOGGER.atDebug().log("Markdown: {}", ms.toMarkdown()); + + Assertions.assertEquals(markdown, ms.toMarkdown()); + Assertions.assertEquals(html, ms.toHtml()); + } + + @Test + void preCodeMarkdown() { + final String html = "
Example **some** *code*\n"
+        + "nextline\n"
+        + "
"; + final String markdown = " Example **some** *code*\n" + + " nextline\n"; + + MarkupMultiline ms = MarkupMultiline.fromHtml(html); + + LOGGER.atDebug().log("AST: {}", AstCollectingVisitor.asString(ms.getDocument())); + LOGGER.atDebug().log("HTML: {}", ms.toHtml()); + LOGGER.atDebug().log("Markdown: {}", ms.toMarkdown()); + + Assertions.assertEquals(markdown, ms.toMarkdown()); + Assertions.assertEquals(html, ms.toHtml()); + } + + @Test + void paragraphCodeMarkdown() { + final String html = "

Example**some** *code*

"; + final String markdown = "Example`**some** *code*`"; + MarkupMultiline ms = MarkupMultiline.fromHtml(html); + + LOGGER.atDebug().log("AST: {}", AstCollectingVisitor.asString(ms.getDocument())); + LOGGER.atDebug().log("HTML: {}", ms.toHtml()); + LOGGER.atDebug().log("Markdown: {}", ms.toMarkdown()); + + Assertions.assertEquals(markdown, ms.toMarkdown()); + Assertions.assertEquals(html, ms.toHtml()); + } + + @Test + void testEntityRoundTrip() throws XMLStreamException { + final String markdown = "hijacked was used (e.g., the <CTRL> + <ALT> + <DEL> keys)."; + final String html = "hijacked was used (e.g., the <CTRL> + <ALT> + <DEL> keys)."; + final String xhtml = "

hijacked was used (e.g., the <CTRL> + <ALT> + <DEL> keys).

"; + + MarkupLine ms = MarkupLine.fromMarkdown(markdown); + Document document = ms.getDocument(); + + Assertions.assertNotNull(document); + + LOGGER.atDebug().log("AST: {}", AstCollectingVisitor.asString(ms.getDocument())); + LOGGER.atDebug().log("HTML: {}", ms.toHtml()); + LOGGER.atDebug().log("Markdown: {}", ms.toMarkdown()); + + Assertions.assertEquals(markdown, ms.toMarkdown()); + Assertions.assertEquals(html, ms.toHtml().trim()); + + StringWriter stringWriter = new StringWriter(); + XMLStreamWriter2 xmlStreamWriter = newXmlStreamWriter(stringWriter); + + xmlStreamWriter.writeStartElement(MARKUP_HTML_NAMESPACE, "p"); + + ms.writeXHtml(MARKUP_HTML_NAMESPACE, xmlStreamWriter); + + xmlStreamWriter.writeEndElement(); + + xmlStreamWriter.close(); + + Assertions.assertEquals(xhtml, stringWriter.toString()); + } + + @Test + void testAposRoundTrip() throws XMLStreamException { + String markdown = "a user’s identity"; + + // test from Markdown source + MarkupLine ms = MarkupLine.fromMarkdown(markdown); + Document document = ms.getDocument(); + + Assertions.assertNotNull(document); + + LOGGER.atDebug().log("AST: {}", AstCollectingVisitor.asString(ms.getDocument())); + LOGGER.atDebug().log("HTML: {}", ms.toHtml()); + LOGGER.atDebug().log("Markdown: {}", ms.toMarkdown()); + + Assertions.assertEquals(markdown, ms.toMarkdown()); + + String html = "a user’s identity"; + Assertions.assertEquals(html, ms.toHtml().trim()); + + StringWriter stringWriter = new StringWriter(); + XMLStreamWriter2 xmlStreamWriter = newXmlStreamWriter(stringWriter); + + xmlStreamWriter.writeStartElement(MARKUP_HTML_NAMESPACE, "p"); + + ms.writeXHtml(MARKUP_HTML_NAMESPACE, xmlStreamWriter); + + xmlStreamWriter.writeEndElement(); + + xmlStreamWriter.close(); + + Assertions.assertEquals("

" + html + "

", stringWriter.toString()); + + // test from HTML source + ms = MarkupLine.fromHtml(html); + document = ms.getDocument(); + + Assertions.assertNotNull(document); + LOGGER.atDebug().log("AST: {}", AstCollectingVisitor.asString(ms.getDocument())); + LOGGER.atDebug().log("HTML: {}", ms.toHtml()); + LOGGER.atDebug().log("Markdown: {}", ms.toMarkdown()); + Assertions.assertEquals(markdown, ms.toMarkdown()); + Assertions.assertEquals(html, ms.toHtml().trim()); + } + + @Test + void testHtml() { + String html = "

before <thing[02] text> after

"; + MarkupMultiline ms = MarkupMultiline.fromHtml(html); + + LOGGER.atDebug().log("AST: {}", AstCollectingVisitor.asString(ms.getDocument())); + LOGGER.atDebug().log("HTML: {}", ms.toHtml()); + LOGGER.atDebug().log("Markdown: {}", ms.toMarkdown()); + } + + @Test + void testIntraTagNewline() { + // addresses usnistgov/liboscal-java#5 + String html = "

A custom title\n" + + " with italic\n" + + "

"; + MarkupMultiline ms = MarkupMultiline.fromHtml(html); + + Document doc = ms.getDocument(); + LOGGER.atDebug().log("AST: {}", AstCollectingVisitor.asString(doc)); + LOGGER.atDebug().log("HTML: {}", ms.toHtml()); + LOGGER.atDebug().log("Markdown: {}", ms.toMarkdown()); + + List children = CollectionUtil.toList(doc.getChildren()); + // ensure there is only 1 child and that it is a heading + Assertions.assertAll( + () -> Assertions.assertEquals(1, children.size()), + () -> Assertions.assertEquals(Heading.class, children.get(0).getClass())); + } +} diff --git a/core/src/test/java/gov/nist/secauto/metaschema/core/datatype/markup/flexmark/MarkupParserTest.java b/core/src/test/java/gov/nist/secauto/metaschema/core/datatype/markup/flexmark/MarkupParserTest.java index f9cd70861..b37e4dc1c 100644 --- a/core/src/test/java/gov/nist/secauto/metaschema/core/datatype/markup/flexmark/MarkupParserTest.java +++ b/core/src/test/java/gov/nist/secauto/metaschema/core/datatype/markup/flexmark/MarkupParserTest.java @@ -1,121 +1,121 @@ -/* - * Portions of this software was developed by employees of the National Institute - * of Standards and Technology (NIST), an agency of the Federal Government and is - * being made available as a public service. Pursuant to title 17 United States - * Code Section 105, works of NIST employees are not subject to copyright - * protection in the United States. This software may be subject to foreign - * copyright. Permission in the United States and in foreign countries, to the - * extent that NIST may hold copyright, to use, copy, modify, create derivative - * works, and distribute this software and its documentation without fee is hereby - * granted on a non-exclusive basis, provided that this notice and disclaimer - * of warranty appears in all copies. - * - * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER - * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY - * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM - * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE - * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT - * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, - * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, - * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, - * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR - * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT - * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. - */ - -package gov.nist.secauto.metaschema.core.datatype.markup.flexmark; - -import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; - -import com.ctc.wstx.stax.WstxInputFactory; - -import gov.nist.secauto.metaschema.core.datatype.markup.MarkupMultiline; -import gov.nist.secauto.metaschema.core.model.util.XmlEventUtil; - -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; -import org.codehaus.stax2.XMLEventReader2; -import org.codehaus.stax2.XMLInputFactory2; -import org.junit.jupiter.api.Test; - -import java.io.StringReader; - -import javax.xml.stream.XMLInputFactory; -import javax.xml.stream.XMLStreamException; - -class MarkupParserTest { - private static final Logger LOGGER = LogManager.getLogger(MarkupParserTest.class); - - @Test - void test() throws XMLStreamException { - XMLInputFactory2 factory = (XMLInputFactory2) XMLInputFactory.newInstance(); - assert factory instanceof WstxInputFactory; - factory.configureForXmlConformance(); - factory.setProperty(XMLInputFactory.IS_COALESCING, true); - - String html = new StringBuilder() - .append("\n") - .append("

some text

\n") - .append("

text

\n") - .append("

some text .

\n") - .append("

Example

\n") - .append("

text

\n") - .append("
    \n") - .append("
  • a list item
  • \n") - .append("
  • another list item
  • \n") - .append("
\n") - .append(" \n") - .append(" \n") - .append(" \n") - .append("
Heading 1
data1
\n") - .append("

Some more text\"alt\"

\n") - .append("
\n") - .toString(); - - XMLEventReader2 reader = (XMLEventReader2) factory.createXMLEventReader(new StringReader(html)); - - CharSequence startDocument = XmlEventUtil.toString(reader.nextEvent()); - LOGGER.atDebug().log("StartDocument: {}", startDocument); - - CharSequence startElement = XmlEventUtil.toString(reader.nextEvent()); - LOGGER.atDebug().log("StartElement: {}", startElement); - - assertDoesNotThrow(() -> { - MarkupMultiline markupString = XmlMarkupParser.instance().parseMarkupMultiline(reader); - AstCollectingVisitor.asString(markupString.getDocument()); - // System.out.println(html); - // System.out.println(visitor.getAst()); - // System.out.println(markupString.toMarkdown()); - - }); - } - - @Test - void emptyParagraphTest() throws XMLStreamException { - final String html = new StringBuilder() - .append("\n") - .append("

\n") - .append("\n") - .toString(); - - XMLInputFactory2 factory = (XMLInputFactory2) XMLInputFactory.newInstance(); - assert factory instanceof WstxInputFactory; - factory.configureForXmlConformance(); - factory.setProperty(XMLInputFactory.IS_COALESCING, true); - XMLEventReader2 reader = (XMLEventReader2) factory.createXMLEventReader(new StringReader(html)); - - CharSequence startDocument = XmlEventUtil.toString(reader.nextEvent()); - LOGGER.atDebug().log("StartDocument: {}", startDocument); - - CharSequence startElement = XmlEventUtil.toString(reader.nextEvent()); - LOGGER.atDebug().log("StartElement: {}", startElement); - - assertDoesNotThrow(() -> { - MarkupMultiline ms = XmlMarkupParser.instance().parseMarkupMultiline(reader); - LOGGER.atDebug().log("AST: {}", AstCollectingVisitor.asString(ms.getDocument())); - LOGGER.atDebug().log("HTML: {}", ms.toXHtml("")); - LOGGER.atDebug().log("Markdown: {}", ms.toMarkdown()); - }); - } -} +/* + * Portions of this software was developed by employees of the National Institute + * of Standards and Technology (NIST), an agency of the Federal Government and is + * being made available as a public service. Pursuant to title 17 United States + * Code Section 105, works of NIST employees are not subject to copyright + * protection in the United States. This software may be subject to foreign + * copyright. Permission in the United States and in foreign countries, to the + * extent that NIST may hold copyright, to use, copy, modify, create derivative + * works, and distribute this software and its documentation without fee is hereby + * granted on a non-exclusive basis, provided that this notice and disclaimer + * of warranty appears in all copies. + * + * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER + * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY + * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM + * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE + * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT + * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, + * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, + * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, + * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR + * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT + * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. + */ + +package gov.nist.secauto.metaschema.core.datatype.markup.flexmark; + +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; + +import com.ctc.wstx.stax.WstxInputFactory; + +import gov.nist.secauto.metaschema.core.datatype.markup.MarkupMultiline; +import gov.nist.secauto.metaschema.core.model.util.XmlEventUtil; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.codehaus.stax2.XMLEventReader2; +import org.codehaus.stax2.XMLInputFactory2; +import org.junit.jupiter.api.Test; + +import java.io.StringReader; + +import javax.xml.stream.XMLInputFactory; +import javax.xml.stream.XMLStreamException; + +class MarkupParserTest { + private static final Logger LOGGER = LogManager.getLogger(MarkupParserTest.class); + + @Test + void test() throws XMLStreamException { + XMLInputFactory2 factory = (XMLInputFactory2) XMLInputFactory.newInstance(); + assert factory instanceof WstxInputFactory; + factory.configureForXmlConformance(); + factory.setProperty(XMLInputFactory.IS_COALESCING, true); + + String html = new StringBuilder() + .append("\n") + .append("

some text

\n") + .append("

text

\n") + .append("

some text .

\n") + .append("

Example

\n") + .append("

text

\n") + .append("
    \n") + .append("
  • a list item
  • \n") + .append("
  • another list item
  • \n") + .append("
\n") + .append(" \n") + .append(" \n") + .append(" \n") + .append("
Heading 1
data1
\n") + .append("

Some more text\"alt\"

\n") + .append("
\n") + .toString(); + + XMLEventReader2 reader = (XMLEventReader2) factory.createXMLEventReader(new StringReader(html)); + + CharSequence startDocument = XmlEventUtil.toString(reader.nextEvent()); + LOGGER.atDebug().log("StartDocument: {}", startDocument); + + CharSequence startElement = XmlEventUtil.toString(reader.nextEvent()); + LOGGER.atDebug().log("StartElement: {}", startElement); + + assertDoesNotThrow(() -> { + MarkupMultiline markupString = XmlMarkupParser.instance().parseMarkupMultiline(reader); + AstCollectingVisitor.asString(markupString.getDocument()); + // System.out.println(html); + // System.out.println(visitor.getAst()); + // System.out.println(markupString.toMarkdown()); + + }); + } + + @Test + void emptyParagraphTest() throws XMLStreamException { + final String html = new StringBuilder() + .append("\n") + .append("

\n") + .append("\n") + .toString(); + + XMLInputFactory2 factory = (XMLInputFactory2) XMLInputFactory.newInstance(); + assert factory instanceof WstxInputFactory; + factory.configureForXmlConformance(); + factory.setProperty(XMLInputFactory.IS_COALESCING, true); + XMLEventReader2 reader = (XMLEventReader2) factory.createXMLEventReader(new StringReader(html)); + + CharSequence startDocument = XmlEventUtil.toString(reader.nextEvent()); + LOGGER.atDebug().log("StartDocument: {}", startDocument); + + CharSequence startElement = XmlEventUtil.toString(reader.nextEvent()); + LOGGER.atDebug().log("StartElement: {}", startElement); + + assertDoesNotThrow(() -> { + MarkupMultiline ms = XmlMarkupParser.instance().parseMarkupMultiline(reader); + LOGGER.atDebug().log("AST: {}", AstCollectingVisitor.asString(ms.getDocument())); + LOGGER.atDebug().log("HTML: {}", ms.toXHtml("")); + LOGGER.atDebug().log("Markdown: {}", ms.toMarkdown()); + }); + } +} diff --git a/databind/pom.xml b/databind/pom.xml index 521f024a2..5810ed93a 100644 --- a/databind/pom.xml +++ b/databind/pom.xml @@ -1,183 +1,183 @@ - - - 4.0.0 - - gov.nist.secauto.metaschema - metaschema-framework - 1.0.0-M2-SNAPSHOT - - - metaschema-databind - jar - - Metaschema Java Data/Object Binding and Code Generation - A Metaschema binding-based parser and code generator for Java - objects supporting serialization of conformant XML, JSON, and YAML data. - - ${site.url}${project.artifactId}/ - - - nist-pages - ${site.url}${project.artifactId}/ - - - - - - ${project.parent.groupId} - metaschema-core - - - - com.google.auto.service - auto-service-annotations - true - - - - org.apache.commons - commons-lang3 - - - - nl.talsmasoftware - lazy4j - - - - com.squareup - javapoet - - - - org.json - json - - - com.github.erosb - everit-json-schema - - - - com.fasterxml.jackson.dataformat - jackson-dataformat-xml - - - com.fasterxml.jackson.dataformat - jackson-dataformat-yaml - - - - org.apache.xmlbeans - xmlbeans - - - - - org.apache.logging.log4j - log4j-api - - - org.apache.logging.log4j - log4j-core - test - - - - org.jmock - jmock-junit5 - test - - - - - - - - org.apache.maven.plugins - maven-pmd-plugin - - - target/generated-sources/xmlbeans - - - **/gov/nist/secauto/metaschema/databind/model/metaschema/*.java - - - - - org.apache.maven.plugins - maven-checkstyle-plugin - - **/gov/nist/secauto/metaschema/databind/model/metaschema/*.java,**/gov/nist/secauto/metaschema/databind/model/test/*.java - - - - org.jacoco - jacoco-maven-plugin - - - gov/nist/secauto/metaschema/databind/codegen/xmlbeans/**/* - org/apache/xmlbeans/**/* - - - - - - - - gov.nist.secauto - oss-build-support - - - metaschema-codegen-beans - false - generate-sources - - generate-xmlbeans - - - - ${project.basedir}/src/schema/xmlconfig.xml - ${project.basedir}/src/main/xsd - - ${project.build.directory}/generated-sources/xmlbeans - - ${project.basedir}/target/generated-resources/xmlbeans - metaschema.codegen - - - - - - org.codehaus.mojo - build-helper-maven-plugin - - - add-source - generate-sources - - add-source - - - - - ${project.build.directory}/generated-sources/xmlbeans - - - - - - - org.apache.maven.plugins - maven-dependency-plugin - - - com.sun.xml.bind:jaxb-impl - org.apache.logging.log4j:log4j-core - - - - - + + + 4.0.0 + + gov.nist.secauto.metaschema + metaschema-framework + 1.0.0-M2-SNAPSHOT + + + metaschema-databind + jar + + Metaschema Java Data/Object Binding and Code Generation + A Metaschema binding-based parser and code generator for Java + objects supporting serialization of conformant XML, JSON, and YAML data. + + ${site.url}${project.artifactId}/ + + + nist-pages + ${site.url}${project.artifactId}/ + + + + + + ${project.parent.groupId} + metaschema-core + + + + com.google.auto.service + auto-service-annotations + true + + + + org.apache.commons + commons-lang3 + + + + nl.talsmasoftware + lazy4j + + + + com.squareup + javapoet + + + + org.json + json + + + com.github.erosb + everit-json-schema + + + + com.fasterxml.jackson.dataformat + jackson-dataformat-xml + + + com.fasterxml.jackson.dataformat + jackson-dataformat-yaml + + + + org.apache.xmlbeans + xmlbeans + + + + + org.apache.logging.log4j + log4j-api + + + org.apache.logging.log4j + log4j-core + test + + + + org.jmock + jmock-junit5 + test + + + + + + + + org.apache.maven.plugins + maven-pmd-plugin + + + target/generated-sources/xmlbeans + + + **/gov/nist/secauto/metaschema/databind/model/metaschema/*.java + + + + + org.apache.maven.plugins + maven-checkstyle-plugin + + **/gov/nist/secauto/metaschema/databind/model/metaschema/*.java,**/gov/nist/secauto/metaschema/databind/model/test/*.java + + + + org.jacoco + jacoco-maven-plugin + + + gov/nist/secauto/metaschema/databind/codegen/xmlbeans/**/* + org/apache/xmlbeans/**/* + + + + + + + + gov.nist.secauto + oss-build-support + + + metaschema-codegen-beans + false + generate-sources + + generate-xmlbeans + + + + ${project.basedir}/src/schema/xmlconfig.xml + ${project.basedir}/src/main/xsd + + ${project.build.directory}/generated-sources/xmlbeans + + ${project.basedir}/target/generated-resources/xmlbeans + metaschema.codegen + + + + + + org.codehaus.mojo + build-helper-maven-plugin + + + add-source + generate-sources + + add-source + + + + + ${project.build.directory}/generated-sources/xmlbeans + + + + + + + org.apache.maven.plugins + maven-dependency-plugin + + + com.sun.xml.bind:jaxb-impl + org.apache.logging.log4j:log4j-core + + + + + \ No newline at end of file diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/IBindingContext.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/IBindingContext.java index 84c6bfb00..0567ac59b 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/IBindingContext.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/IBindingContext.java @@ -1,491 +1,491 @@ -/* - * Portions of this software was developed by employees of the National Institute - * of Standards and Technology (NIST), an agency of the Federal Government and is - * being made available as a public service. Pursuant to title 17 United States - * Code Section 105, works of NIST employees are not subject to copyright - * protection in the United States. This software may be subject to foreign - * copyright. Permission in the United States and in foreign countries, to the - * extent that NIST may hold copyright, to use, copy, modify, create derivative - * works, and distribute this software and its documentation without fee is hereby - * granted on a non-exclusive basis, provided that this notice and disclaimer - * of warranty appears in all copies. - * - * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER - * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY - * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM - * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE - * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT - * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, - * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, - * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, - * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR - * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT - * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. - */ - -package gov.nist.secauto.metaschema.databind; - -import gov.nist.secauto.metaschema.core.datatype.IDataTypeAdapter; -import gov.nist.secauto.metaschema.core.metapath.DynamicContext; -import gov.nist.secauto.metaschema.core.metapath.StaticContext; -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.model.IModule; -import gov.nist.secauto.metaschema.core.model.constraint.DefaultConstraintValidator; -import gov.nist.secauto.metaschema.core.model.constraint.FindingCollectingConstraintValidationHandler; -import gov.nist.secauto.metaschema.core.model.constraint.IConstraintValidationHandler; -import gov.nist.secauto.metaschema.core.model.constraint.IConstraintValidator; -import gov.nist.secauto.metaschema.core.model.validation.AggregateValidationResult; -import gov.nist.secauto.metaschema.core.model.validation.IValidationResult; -import gov.nist.secauto.metaschema.core.model.validation.JsonSchemaContentValidator; -import gov.nist.secauto.metaschema.core.model.validation.XmlSchemaContentValidator; -import gov.nist.secauto.metaschema.core.util.ObjectUtils; -import gov.nist.secauto.metaschema.databind.io.BindingException; -import gov.nist.secauto.metaschema.databind.io.DeserializationFeature; -import gov.nist.secauto.metaschema.databind.io.Format; -import gov.nist.secauto.metaschema.databind.io.IBoundLoader; -import gov.nist.secauto.metaschema.databind.io.IDeserializer; -import gov.nist.secauto.metaschema.databind.io.ISerializer; -import gov.nist.secauto.metaschema.databind.io.yaml.YamlOperations; -import gov.nist.secauto.metaschema.databind.model.IBoundDefinitionModel; -import gov.nist.secauto.metaschema.databind.model.IBoundDefinitionModelAssembly; -import gov.nist.secauto.metaschema.databind.model.IBoundDefinitionModelComplex; -import gov.nist.secauto.metaschema.databind.model.IBoundModule; -import gov.nist.secauto.metaschema.databind.model.annotations.MetaschemaAssembly; -import gov.nist.secauto.metaschema.databind.model.annotations.MetaschemaField; - -import org.json.JSONObject; -import org.xml.sax.SAXException; - -import java.io.IOException; -import java.math.BigInteger; -import java.net.URI; -import java.nio.file.Path; -import java.time.ZonedDateTime; -import java.util.List; - -import javax.xml.namespace.QName; -import javax.xml.transform.Source; - -import edu.umd.cs.findbugs.annotations.NonNull; -import edu.umd.cs.findbugs.annotations.Nullable; - -/** - * Provides information supporting a binding between a set of Module models and - * corresponding Java classes. - */ -public interface IBindingContext { - - /** - * Get the singleton {@link IBindingContext} instance, which can be used to load - * information that binds a model to a set of Java classes. - * - * @return a new binding context - */ - @NonNull - static IBindingContext instance() { - return DefaultBindingContext.instance(); - } - - /** - * Register a matcher used to identify a bound class by the definition's root - * name. - * - * @param definition - * the definition to match for - * @return the matcher - */ - @NonNull - IBindingMatcher registerBindingMatcher(@NonNull IBoundDefinitionModelAssembly definition); - - /** - * Register a matcher used to identify a bound class by the definition's root - * name. - * - * @param clazz - * the definition class to match for, which must represent a root - * assembly definition - * @return the matcher - */ - @NonNull - IBindingMatcher registerBindingMatcher(@NonNull Class clazz); - - /** - * Register a class binding for a given bound class. - * - * @param definition - * the bound class information to register - * @return the old bound class information or {@code null} if no binding existed - * for the associated class - */ - @Nullable - IBoundDefinitionModelComplex registerClassBinding(@NonNull IBoundDefinitionModelComplex definition); - - /** - * Get the {@link IBoundDefinitionModel} instance associated with the provided - * Java class. - *

- * Typically the class will have a {@link MetaschemaAssembly} or - * {@link MetaschemaField} annotation. - * - * @param clazz - * the class binding to load - * @return the associated class binding instance or {@code null} if the class is - * not bound - */ - @Nullable - IBoundDefinitionModelComplex getBoundDefinitionForClass(@NonNull Class clazz); - - /** - * Determine the bound class for the provided XML {@link QName}. - * - * @param rootQName - * the root XML element's QName - * @return the bound class or {@code null} if not recognized - * @see IBindingContext#registerBindingMatcher(Class) - */ - @Nullable - Class getBoundClassForRootXmlQName(@NonNull QName rootQName); - - /** - * Determine the bound class for the provided JSON/YAML property/item name using - * any registered matchers. - * - * @param rootName - * the JSON/YAML property/item name - * @return the bound class or {@code null} if not recognized - * @see IBindingContext#registerBindingMatcher(Class) - */ - @Nullable - Class getBoundClassForRootJsonName(@NonNull String rootName); - - /** - * Get's the {@link IDataTypeAdapter} associated with the specified Java class, - * which is used to read and write XML, JSON, and YAML data to and from - * instances of that class. Thus, this adapter supports a direct binding between - * the Java class and structured data in one of the supported formats. Adapters - * are used to support bindings for simple data objects (e.g., {@link String}, - * {@link BigInteger}, {@link ZonedDateTime}, etc). - * - * @param - * the class type of the adapter - * @param clazz - * the Java {@link Class} for the bound type - * @return the adapter instance or {@code null} if the provided class is not - * bound - */ - @Nullable - > TYPE getJavaTypeAdapterInstance(@NonNull Class clazz); - - /** - * Load a bound Metaschema module implemented by the provided class. - *

- * Also registers any associated bound classes. - *

- * Implementations are expected to return the same IModule instance for multiple - * calls to this method with the same class argument. - * - * @param clazz - * the class implementing a bound Metaschema module - * @return the loaded module - */ - @NonNull - IBoundModule registerModule(@NonNull Class clazz); - - /** - * Generate, compile, and load a set of generated Module annotated Java classes - * based on the provided Module {@code module}. - * - * @param module - * the Module module to generate classes for - * @param compilePath - * the path to the directory to generate classes in - * @return this instance - * @throws IOException - * if an error occurred while generating or loading the classes - */ - @NonNull - IBindingContext registerModule( - @NonNull IModule module, - @NonNull Path compilePath) throws IOException; - - /** - * Gets a data {@link ISerializer} which can be used to write Java instance data - * for the provided class in the requested format. - *

- * The provided class must be a bound Java class with a - * {@link MetaschemaAssembly} or {@link MetaschemaField} annotation for which a - * {@link IBoundDefinitionModel} exists. - * - * @param - * the Java type this serializer can write data from - * @param format - * the format to serialize into - * @param clazz - * the Java data object to serialize - * @return the serializer instance - * @throws NullPointerException - * if any of the provided arguments, except the configuration, are - * {@code null} - * @throws IllegalArgumentException - * if the provided class is not bound to a Module assembly or field - * @throws UnsupportedOperationException - * if the requested format is not supported by the implementation - * @see #getBoundDefinitionForClass(Class) - */ - @NonNull - ISerializer newSerializer(@NonNull Format format, @NonNull Class clazz); - - /** - * Gets a data {@link IDeserializer} which can be used to read Java instance - * data for the provided class from the requested format. - *

- * The provided class must be a bound Java class with a - * {@link MetaschemaAssembly} or {@link MetaschemaField} annotation for which a - * {@link IBoundDefinitionModel} exists. - * - * @param - * the Java type this deserializer can read data into - * @param format - * the format to serialize into - * @param clazz - * the Java data type to serialize - * @return the deserializer instance - * @throws NullPointerException - * if any of the provided arguments, except the configuration, are - * {@code null} - * @throws IllegalArgumentException - * if the provided class is not bound to a Module assembly or field - * @throws UnsupportedOperationException - * if the requested format is not supported by the implementation - * @see #getBoundDefinitionForClass(Class) - */ - @NonNull - IDeserializer newDeserializer(@NonNull Format format, @NonNull Class clazz); - - /** - * Get a new {@link IBoundLoader} instance. - * - * @return the instance - */ - @NonNull - IBoundLoader newBoundLoader(); - - /** - * Create a deep copy of the provided bound object. - * - * @param - * the bound object type - * @param other - * the object to copy - * @param parentInstance - * the object's parent or {@code null} - * @return a deep copy of the provided object - * @throws BindingException - * if an error occurred copying content between java instances - * @throws NullPointerException - * if the provided object is {@code null} - * @throws IllegalArgumentException - * if the provided class is not bound to a Module assembly or field - */ - @NonNull - CLASS deepCopy(@NonNull CLASS other, Object parentInstance) throws BindingException; - - /** - * Get a new single use constraint validator. - * - * @param handler - * the validation handler to use to process the validation results - * - * @return the validator - */ - default IConstraintValidator newValidator(@NonNull IConstraintValidationHandler handler) { - IBoundLoader loader = newBoundLoader(); - loader.disableFeature(DeserializationFeature.DESERIALIZE_VALIDATE_CONSTRAINTS); - - DynamicContext context = StaticContext.instance().dynamicContext(); - context.setDocumentLoader(loader); - - return new DefaultConstraintValidator(handler); - } - - /** - * Perform constraint validation on the provided bound object represented as an - * {@link INodeItem}. - * - * @param nodeItem - * the node item to validate - * @return the validation result - * @throws IllegalArgumentException - * if the provided class is not bound to a Module assembly or field - */ - default IValidationResult validate(@NonNull INodeItem nodeItem) { - FindingCollectingConstraintValidationHandler handler = new FindingCollectingConstraintValidationHandler(); - IConstraintValidator validator = newValidator(handler); - DynamicContext dynamicContext = StaticContext.instance().dynamicContext(); - dynamicContext.setDocumentLoader(newBoundLoader()); - validator.validate(nodeItem, dynamicContext); - validator.finalizeValidation(dynamicContext); - return handler; - } - - /** - * Load and perform schema and constraint validation on the target. The - * constraint validation will only be performed if the schema validation passes. - * - * @param target - * the target to validate - * @param asFormat - * the schema format to use to validate the target - * @param schemaProvider - * provides callbacks to get the appropriate schemas - * @return the validation result - * @throws IOException - * if an error occurred while reading the target - * @throws SAXException - * if an error occurred when parsing the target as XML - */ - default IValidationResult validate( - @NonNull URI target, - @NonNull Format asFormat, - @NonNull IValidationSchemaProvider schemaProvider) throws IOException, SAXException { - IValidationResult retval; - switch (asFormat) { - case JSON: - retval = new JsonSchemaContentValidator(schemaProvider.getJsonSchema()).validate(target); - break; - case XML: - List schemaSources = schemaProvider.getXmlSchemas(); - retval = new XmlSchemaContentValidator(schemaSources).validate(target); - break; - case YAML: - JSONObject json = YamlOperations.yamlToJson(YamlOperations.parseYaml(target)); - assert json != null; - retval = new JsonSchemaContentValidator(schemaProvider.getJsonSchema()) - .validate(json, ObjectUtils.notNull(target)); - break; - default: - throw new UnsupportedOperationException("Unsupported format: " + asFormat.name()); - } - - if (retval.isPassing()) { - IValidationResult constraintValidationResult = validateWithConstraints(target); - retval = AggregateValidationResult.aggregate(retval, constraintValidationResult); - } - return retval; - } - - /** - * Load and validate the provided {@code target} using the associated Module - * module constraints. - * - * @param target - * the file to load and validate - * @return the validation results - * @throws IOException - * if an error occurred while loading the document - */ - default IValidationResult validateWithConstraints(@NonNull URI target) throws IOException { - IBoundLoader loader = newBoundLoader(); - loader.disableFeature(DeserializationFeature.DESERIALIZE_VALIDATE_CONSTRAINTS); - - DynamicContext dynamicContext = StaticContext.instance().dynamicContext(); - dynamicContext.setDocumentLoader(loader); - IDocumentNodeItem nodeItem = loader.loadAsNodeItem(target); - - return validate(nodeItem); - } - - interface IModuleLoaderStrategy { - /** - * Load the bound Metaschema module represented by the provided class. - *

- * Implementations are allowed to return a cached instance if the module has - * already been loaded. - * - * @param clazz - * the Module class - * @return the module - * @throws IllegalStateException - * if an error occurred while processing the associated module - * information - */ - @NonNull - IBoundModule loadModule(@NonNull Class clazz); - - /** - * Get the {@link IBoundDefinitionModel} instance associated with the provided - * Java class. - *

- * Typically the class will have a {@link MetaschemaAssembly} or - * {@link MetaschemaField} annotation. - * - * @param clazz - * the class binding to load - * @return the associated class binding instance or {@code null} if the class is - * not bound - */ - @Nullable - IBoundDefinitionModelComplex getBoundDefinitionForClass(@NonNull Class clazz); - } - - interface IValidationSchemaProvider { - /** - * Get a JSON schema to use for content validation. - * - * @return the JSON schema - * @throws IOException - * if an error occurred while loading the schema - */ - @NonNull - JSONObject getJsonSchema() throws IOException; - - /** - * Get a XML schema to use for content validation. - * - * @return the XML schema sources - * @throws IOException - * if an error occurred while loading the schema - */ - @NonNull - List getXmlSchemas() throws IOException; - } - - /** - * Implementations of this interface provide a means by which a bound class can - * be found that corresponds to an XML element, JSON property, or YAML item - * name. - */ - interface IBindingMatcher { - @SuppressWarnings("PMD.ShortMethodName") - @NonNull - static IBindingMatcher of(IBoundDefinitionModelAssembly assembly) { - if (!assembly.isRoot()) { - throw new IllegalArgumentException( - String.format("The provided class '%s' is not a root assembly.", assembly.getBoundClass().getName())); - } - return new RootAssemblyBindingMatcher(assembly); - } - - /** - * Determine the bound class for the provided XML {@link QName}. - * - * @param rootQName - * the root XML element's QName - * @return the bound class for the XML qualified name or {@code null} if not - * recognized - */ - Class getBoundClassForXmlQName(QName rootQName); - - /** - * Determine the bound class for the provided JSON/YAML property/item name. - * - * @param rootName - * the JSON/YAML property/item name - * @return the bound class for the JSON property name or {@code null} if not - * recognized - */ - Class getBoundClassForJsonName(String rootName); - } -} +/* + * Portions of this software was developed by employees of the National Institute + * of Standards and Technology (NIST), an agency of the Federal Government and is + * being made available as a public service. Pursuant to title 17 United States + * Code Section 105, works of NIST employees are not subject to copyright + * protection in the United States. This software may be subject to foreign + * copyright. Permission in the United States and in foreign countries, to the + * extent that NIST may hold copyright, to use, copy, modify, create derivative + * works, and distribute this software and its documentation without fee is hereby + * granted on a non-exclusive basis, provided that this notice and disclaimer + * of warranty appears in all copies. + * + * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER + * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY + * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM + * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE + * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT + * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, + * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, + * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, + * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR + * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT + * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. + */ + +package gov.nist.secauto.metaschema.databind; + +import gov.nist.secauto.metaschema.core.datatype.IDataTypeAdapter; +import gov.nist.secauto.metaschema.core.metapath.DynamicContext; +import gov.nist.secauto.metaschema.core.metapath.StaticContext; +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.model.IModule; +import gov.nist.secauto.metaschema.core.model.constraint.DefaultConstraintValidator; +import gov.nist.secauto.metaschema.core.model.constraint.FindingCollectingConstraintValidationHandler; +import gov.nist.secauto.metaschema.core.model.constraint.IConstraintValidationHandler; +import gov.nist.secauto.metaschema.core.model.constraint.IConstraintValidator; +import gov.nist.secauto.metaschema.core.model.validation.AggregateValidationResult; +import gov.nist.secauto.metaschema.core.model.validation.IValidationResult; +import gov.nist.secauto.metaschema.core.model.validation.JsonSchemaContentValidator; +import gov.nist.secauto.metaschema.core.model.validation.XmlSchemaContentValidator; +import gov.nist.secauto.metaschema.core.util.ObjectUtils; +import gov.nist.secauto.metaschema.databind.io.BindingException; +import gov.nist.secauto.metaschema.databind.io.DeserializationFeature; +import gov.nist.secauto.metaschema.databind.io.Format; +import gov.nist.secauto.metaschema.databind.io.IBoundLoader; +import gov.nist.secauto.metaschema.databind.io.IDeserializer; +import gov.nist.secauto.metaschema.databind.io.ISerializer; +import gov.nist.secauto.metaschema.databind.io.yaml.YamlOperations; +import gov.nist.secauto.metaschema.databind.model.IBoundDefinitionModel; +import gov.nist.secauto.metaschema.databind.model.IBoundDefinitionModelAssembly; +import gov.nist.secauto.metaschema.databind.model.IBoundDefinitionModelComplex; +import gov.nist.secauto.metaschema.databind.model.IBoundModule; +import gov.nist.secauto.metaschema.databind.model.annotations.MetaschemaAssembly; +import gov.nist.secauto.metaschema.databind.model.annotations.MetaschemaField; + +import org.json.JSONObject; +import org.xml.sax.SAXException; + +import java.io.IOException; +import java.math.BigInteger; +import java.net.URI; +import java.nio.file.Path; +import java.time.ZonedDateTime; +import java.util.List; + +import javax.xml.namespace.QName; +import javax.xml.transform.Source; + +import edu.umd.cs.findbugs.annotations.NonNull; +import edu.umd.cs.findbugs.annotations.Nullable; + +/** + * Provides information supporting a binding between a set of Module models and + * corresponding Java classes. + */ +public interface IBindingContext { + + /** + * Get the singleton {@link IBindingContext} instance, which can be used to load + * information that binds a model to a set of Java classes. + * + * @return a new binding context + */ + @NonNull + static IBindingContext instance() { + return DefaultBindingContext.instance(); + } + + /** + * Register a matcher used to identify a bound class by the definition's root + * name. + * + * @param definition + * the definition to match for + * @return the matcher + */ + @NonNull + IBindingMatcher registerBindingMatcher(@NonNull IBoundDefinitionModelAssembly definition); + + /** + * Register a matcher used to identify a bound class by the definition's root + * name. + * + * @param clazz + * the definition class to match for, which must represent a root + * assembly definition + * @return the matcher + */ + @NonNull + IBindingMatcher registerBindingMatcher(@NonNull Class clazz); + + /** + * Register a class binding for a given bound class. + * + * @param definition + * the bound class information to register + * @return the old bound class information or {@code null} if no binding existed + * for the associated class + */ + @Nullable + IBoundDefinitionModelComplex registerClassBinding(@NonNull IBoundDefinitionModelComplex definition); + + /** + * Get the {@link IBoundDefinitionModel} instance associated with the provided + * Java class. + *

+ * Typically the class will have a {@link MetaschemaAssembly} or + * {@link MetaschemaField} annotation. + * + * @param clazz + * the class binding to load + * @return the associated class binding instance or {@code null} if the class is + * not bound + */ + @Nullable + IBoundDefinitionModelComplex getBoundDefinitionForClass(@NonNull Class clazz); + + /** + * Determine the bound class for the provided XML {@link QName}. + * + * @param rootQName + * the root XML element's QName + * @return the bound class or {@code null} if not recognized + * @see IBindingContext#registerBindingMatcher(Class) + */ + @Nullable + Class getBoundClassForRootXmlQName(@NonNull QName rootQName); + + /** + * Determine the bound class for the provided JSON/YAML property/item name using + * any registered matchers. + * + * @param rootName + * the JSON/YAML property/item name + * @return the bound class or {@code null} if not recognized + * @see IBindingContext#registerBindingMatcher(Class) + */ + @Nullable + Class getBoundClassForRootJsonName(@NonNull String rootName); + + /** + * Get's the {@link IDataTypeAdapter} associated with the specified Java class, + * which is used to read and write XML, JSON, and YAML data to and from + * instances of that class. Thus, this adapter supports a direct binding between + * the Java class and structured data in one of the supported formats. Adapters + * are used to support bindings for simple data objects (e.g., {@link String}, + * {@link BigInteger}, {@link ZonedDateTime}, etc). + * + * @param + * the class type of the adapter + * @param clazz + * the Java {@link Class} for the bound type + * @return the adapter instance or {@code null} if the provided class is not + * bound + */ + @Nullable + > TYPE getJavaTypeAdapterInstance(@NonNull Class clazz); + + /** + * Load a bound Metaschema module implemented by the provided class. + *

+ * Also registers any associated bound classes. + *

+ * Implementations are expected to return the same IModule instance for multiple + * calls to this method with the same class argument. + * + * @param clazz + * the class implementing a bound Metaschema module + * @return the loaded module + */ + @NonNull + IBoundModule registerModule(@NonNull Class clazz); + + /** + * Generate, compile, and load a set of generated Module annotated Java classes + * based on the provided Module {@code module}. + * + * @param module + * the Module module to generate classes for + * @param compilePath + * the path to the directory to generate classes in + * @return this instance + * @throws IOException + * if an error occurred while generating or loading the classes + */ + @NonNull + IBindingContext registerModule( + @NonNull IModule module, + @NonNull Path compilePath) throws IOException; + + /** + * Gets a data {@link ISerializer} which can be used to write Java instance data + * for the provided class in the requested format. + *

+ * The provided class must be a bound Java class with a + * {@link MetaschemaAssembly} or {@link MetaschemaField} annotation for which a + * {@link IBoundDefinitionModel} exists. + * + * @param + * the Java type this serializer can write data from + * @param format + * the format to serialize into + * @param clazz + * the Java data object to serialize + * @return the serializer instance + * @throws NullPointerException + * if any of the provided arguments, except the configuration, are + * {@code null} + * @throws IllegalArgumentException + * if the provided class is not bound to a Module assembly or field + * @throws UnsupportedOperationException + * if the requested format is not supported by the implementation + * @see #getBoundDefinitionForClass(Class) + */ + @NonNull + ISerializer newSerializer(@NonNull Format format, @NonNull Class clazz); + + /** + * Gets a data {@link IDeserializer} which can be used to read Java instance + * data for the provided class from the requested format. + *

+ * The provided class must be a bound Java class with a + * {@link MetaschemaAssembly} or {@link MetaschemaField} annotation for which a + * {@link IBoundDefinitionModel} exists. + * + * @param + * the Java type this deserializer can read data into + * @param format + * the format to serialize into + * @param clazz + * the Java data type to serialize + * @return the deserializer instance + * @throws NullPointerException + * if any of the provided arguments, except the configuration, are + * {@code null} + * @throws IllegalArgumentException + * if the provided class is not bound to a Module assembly or field + * @throws UnsupportedOperationException + * if the requested format is not supported by the implementation + * @see #getBoundDefinitionForClass(Class) + */ + @NonNull + IDeserializer newDeserializer(@NonNull Format format, @NonNull Class clazz); + + /** + * Get a new {@link IBoundLoader} instance. + * + * @return the instance + */ + @NonNull + IBoundLoader newBoundLoader(); + + /** + * Create a deep copy of the provided bound object. + * + * @param + * the bound object type + * @param other + * the object to copy + * @param parentInstance + * the object's parent or {@code null} + * @return a deep copy of the provided object + * @throws BindingException + * if an error occurred copying content between java instances + * @throws NullPointerException + * if the provided object is {@code null} + * @throws IllegalArgumentException + * if the provided class is not bound to a Module assembly or field + */ + @NonNull + CLASS deepCopy(@NonNull CLASS other, Object parentInstance) throws BindingException; + + /** + * Get a new single use constraint validator. + * + * @param handler + * the validation handler to use to process the validation results + * + * @return the validator + */ + default IConstraintValidator newValidator(@NonNull IConstraintValidationHandler handler) { + IBoundLoader loader = newBoundLoader(); + loader.disableFeature(DeserializationFeature.DESERIALIZE_VALIDATE_CONSTRAINTS); + + DynamicContext context = StaticContext.instance().dynamicContext(); + context.setDocumentLoader(loader); + + return new DefaultConstraintValidator(handler); + } + + /** + * Perform constraint validation on the provided bound object represented as an + * {@link INodeItem}. + * + * @param nodeItem + * the node item to validate + * @return the validation result + * @throws IllegalArgumentException + * if the provided class is not bound to a Module assembly or field + */ + default IValidationResult validate(@NonNull INodeItem nodeItem) { + FindingCollectingConstraintValidationHandler handler = new FindingCollectingConstraintValidationHandler(); + IConstraintValidator validator = newValidator(handler); + DynamicContext dynamicContext = StaticContext.instance().dynamicContext(); + dynamicContext.setDocumentLoader(newBoundLoader()); + validator.validate(nodeItem, dynamicContext); + validator.finalizeValidation(dynamicContext); + return handler; + } + + /** + * Load and perform schema and constraint validation on the target. The + * constraint validation will only be performed if the schema validation passes. + * + * @param target + * the target to validate + * @param asFormat + * the schema format to use to validate the target + * @param schemaProvider + * provides callbacks to get the appropriate schemas + * @return the validation result + * @throws IOException + * if an error occurred while reading the target + * @throws SAXException + * if an error occurred when parsing the target as XML + */ + default IValidationResult validate( + @NonNull URI target, + @NonNull Format asFormat, + @NonNull IValidationSchemaProvider schemaProvider) throws IOException, SAXException { + IValidationResult retval; + switch (asFormat) { + case JSON: + retval = new JsonSchemaContentValidator(schemaProvider.getJsonSchema()).validate(target); + break; + case XML: + List schemaSources = schemaProvider.getXmlSchemas(); + retval = new XmlSchemaContentValidator(schemaSources).validate(target); + break; + case YAML: + JSONObject json = YamlOperations.yamlToJson(YamlOperations.parseYaml(target)); + assert json != null; + retval = new JsonSchemaContentValidator(schemaProvider.getJsonSchema()) + .validate(json, ObjectUtils.notNull(target)); + break; + default: + throw new UnsupportedOperationException("Unsupported format: " + asFormat.name()); + } + + if (retval.isPassing()) { + IValidationResult constraintValidationResult = validateWithConstraints(target); + retval = AggregateValidationResult.aggregate(retval, constraintValidationResult); + } + return retval; + } + + /** + * Load and validate the provided {@code target} using the associated Module + * module constraints. + * + * @param target + * the file to load and validate + * @return the validation results + * @throws IOException + * if an error occurred while loading the document + */ + default IValidationResult validateWithConstraints(@NonNull URI target) throws IOException { + IBoundLoader loader = newBoundLoader(); + loader.disableFeature(DeserializationFeature.DESERIALIZE_VALIDATE_CONSTRAINTS); + + DynamicContext dynamicContext = StaticContext.instance().dynamicContext(); + dynamicContext.setDocumentLoader(loader); + IDocumentNodeItem nodeItem = loader.loadAsNodeItem(target); + + return validate(nodeItem); + } + + interface IModuleLoaderStrategy { + /** + * Load the bound Metaschema module represented by the provided class. + *

+ * Implementations are allowed to return a cached instance if the module has + * already been loaded. + * + * @param clazz + * the Module class + * @return the module + * @throws IllegalStateException + * if an error occurred while processing the associated module + * information + */ + @NonNull + IBoundModule loadModule(@NonNull Class clazz); + + /** + * Get the {@link IBoundDefinitionModel} instance associated with the provided + * Java class. + *

+ * Typically the class will have a {@link MetaschemaAssembly} or + * {@link MetaschemaField} annotation. + * + * @param clazz + * the class binding to load + * @return the associated class binding instance or {@code null} if the class is + * not bound + */ + @Nullable + IBoundDefinitionModelComplex getBoundDefinitionForClass(@NonNull Class clazz); + } + + interface IValidationSchemaProvider { + /** + * Get a JSON schema to use for content validation. + * + * @return the JSON schema + * @throws IOException + * if an error occurred while loading the schema + */ + @NonNull + JSONObject getJsonSchema() throws IOException; + + /** + * Get a XML schema to use for content validation. + * + * @return the XML schema sources + * @throws IOException + * if an error occurred while loading the schema + */ + @NonNull + List getXmlSchemas() throws IOException; + } + + /** + * Implementations of this interface provide a means by which a bound class can + * be found that corresponds to an XML element, JSON property, or YAML item + * name. + */ + interface IBindingMatcher { + @SuppressWarnings("PMD.ShortMethodName") + @NonNull + static IBindingMatcher of(IBoundDefinitionModelAssembly assembly) { + if (!assembly.isRoot()) { + throw new IllegalArgumentException( + String.format("The provided class '%s' is not a root assembly.", assembly.getBoundClass().getName())); + } + return new RootAssemblyBindingMatcher(assembly); + } + + /** + * Determine the bound class for the provided XML {@link QName}. + * + * @param rootQName + * the root XML element's QName + * @return the bound class for the XML qualified name or {@code null} if not + * recognized + */ + Class getBoundClassForXmlQName(QName rootQName); + + /** + * Determine the bound class for the provided JSON/YAML property/item name. + * + * @param rootName + * the JSON/YAML property/item name + * @return the bound class for the JSON property name or {@code null} if not + * recognized + */ + Class getBoundClassForJsonName(String rootName); + } +} diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/codegen/JavaGenerator.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/codegen/JavaGenerator.java index 901acb846..07bb25cd9 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/codegen/JavaGenerator.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/codegen/JavaGenerator.java @@ -1,102 +1,102 @@ -/* - * Portions of this software was developed by employees of the National Institute - * of Standards and Technology (NIST), an agency of the Federal Government and is - * being made available as a public service. Pursuant to title 17 United States - * Code Section 105, works of NIST employees are not subject to copyright - * protection in the United States. This software may be subject to foreign - * copyright. Permission in the United States and in foreign countries, to the - * extent that NIST may hold copyright, to use, copy, modify, create derivative - * works, and distribute this software and its documentation without fee is hereby - * granted on a non-exclusive basis, provided that this notice and disclaimer - * of warranty appears in all copies. - * - * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER - * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY - * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM - * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE - * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT - * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, - * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, - * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, - * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR - * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT - * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. - */ - -package gov.nist.secauto.metaschema.databind.codegen; - -import gov.nist.secauto.metaschema.core.model.IModule; -import gov.nist.secauto.metaschema.core.util.CollectionUtil; -import gov.nist.secauto.metaschema.databind.codegen.config.IBindingConfiguration; - -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; - -import java.io.IOException; -import java.nio.file.Path; -import java.util.Collection; -import java.util.Objects; - -import edu.umd.cs.findbugs.annotations.NonNull; - -/** - * Provides methods for generating Java classes based on a single or a - * collection of Metaschemas. - */ -public final class JavaGenerator { - private static final Logger LOGGER = LogManager.getLogger(JavaGenerator.class); - - private JavaGenerator() { - // disable construction - } - - /** - * Generate Java sources for the provided Metaschema module. - * - * @param module - * the Metaschema module to generate Java sources for - * @param targetDir - * the directory to generate sources in - * @param bindingConfiguration - * the binding customizations to use when generating the Java classes - * @return information about all the produced classes - * @throws IOException - * if an error occurred while generating the class - */ - public static IProduction generate( - @NonNull IModule module, - @NonNull Path targetDir, - @NonNull IBindingConfiguration bindingConfiguration) throws IOException { - return generate(CollectionUtil.singletonList(module), targetDir, bindingConfiguration); - } - - /** - * Generates Java classes for Module fields and flags. - * - * @param modules - * the Metaschema modules to build classes for - * @param targetDirectory - * the directory to generate classes in - * @param bindingConfiguration - * binding customizations that can be used to set namespaces, class - * names, and other aspects of generated classes - * @return information about all the produced classes - * @throws IOException - * if a build error occurred while generating the class - */ - @NonNull - public static IProduction generate( - @NonNull Collection modules, - @NonNull Path targetDirectory, - @NonNull IBindingConfiguration bindingConfiguration) throws IOException { - Objects.requireNonNull(modules, "metaschemas"); - Objects.requireNonNull(targetDirectory, "generationTargetDirectory"); - Objects.requireNonNull(bindingConfiguration, "bindingConfiguration"); - if (LOGGER.isInfoEnabled()) { - LOGGER.info("Generating Java classes in: {}", targetDirectory); - } - - return IProduction.of(modules, bindingConfiguration, targetDirectory); - } -} +/* + * Portions of this software was developed by employees of the National Institute + * of Standards and Technology (NIST), an agency of the Federal Government and is + * being made available as a public service. Pursuant to title 17 United States + * Code Section 105, works of NIST employees are not subject to copyright + * protection in the United States. This software may be subject to foreign + * copyright. Permission in the United States and in foreign countries, to the + * extent that NIST may hold copyright, to use, copy, modify, create derivative + * works, and distribute this software and its documentation without fee is hereby + * granted on a non-exclusive basis, provided that this notice and disclaimer + * of warranty appears in all copies. + * + * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER + * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY + * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM + * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE + * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT + * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, + * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, + * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, + * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR + * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT + * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. + */ + +package gov.nist.secauto.metaschema.databind.codegen; + +import gov.nist.secauto.metaschema.core.model.IModule; +import gov.nist.secauto.metaschema.core.util.CollectionUtil; +import gov.nist.secauto.metaschema.databind.codegen.config.IBindingConfiguration; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import java.io.IOException; +import java.nio.file.Path; +import java.util.Collection; +import java.util.Objects; + +import edu.umd.cs.findbugs.annotations.NonNull; + +/** + * Provides methods for generating Java classes based on a single or a + * collection of Metaschemas. + */ +public final class JavaGenerator { + private static final Logger LOGGER = LogManager.getLogger(JavaGenerator.class); + + private JavaGenerator() { + // disable construction + } + + /** + * Generate Java sources for the provided Metaschema module. + * + * @param module + * the Metaschema module to generate Java sources for + * @param targetDir + * the directory to generate sources in + * @param bindingConfiguration + * the binding customizations to use when generating the Java classes + * @return information about all the produced classes + * @throws IOException + * if an error occurred while generating the class + */ + public static IProduction generate( + @NonNull IModule module, + @NonNull Path targetDir, + @NonNull IBindingConfiguration bindingConfiguration) throws IOException { + return generate(CollectionUtil.singletonList(module), targetDir, bindingConfiguration); + } + + /** + * Generates Java classes for Module fields and flags. + * + * @param modules + * the Metaschema modules to build classes for + * @param targetDirectory + * the directory to generate classes in + * @param bindingConfiguration + * binding customizations that can be used to set namespaces, class + * names, and other aspects of generated classes + * @return information about all the produced classes + * @throws IOException + * if a build error occurred while generating the class + */ + @NonNull + public static IProduction generate( + @NonNull Collection modules, + @NonNull Path targetDirectory, + @NonNull IBindingConfiguration bindingConfiguration) throws IOException { + Objects.requireNonNull(modules, "metaschemas"); + Objects.requireNonNull(targetDirectory, "generationTargetDirectory"); + Objects.requireNonNull(bindingConfiguration, "bindingConfiguration"); + if (LOGGER.isInfoEnabled()) { + LOGGER.info("Generating Java classes in: {}", targetDirectory); + } + + return IProduction.of(modules, bindingConfiguration, targetDirectory); + } +} diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/io/BindingException.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/io/BindingException.java index 2b1b069c8..3901924c2 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/io/BindingException.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/io/BindingException.java @@ -1,77 +1,77 @@ -/* - * Portions of this software was developed by employees of the National Institute - * of Standards and Technology (NIST), an agency of the Federal Government and is - * being made available as a public service. Pursuant to title 17 United States - * Code Section 105, works of NIST employees are not subject to copyright - * protection in the United States. This software may be subject to foreign - * copyright. Permission in the United States and in foreign countries, to the - * extent that NIST may hold copyright, to use, copy, modify, create derivative - * works, and distribute this software and its documentation without fee is hereby - * granted on a non-exclusive basis, provided that this notice and disclaimer - * of warranty appears in all copies. - * - * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER - * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY - * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM - * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE - * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT - * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, - * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, - * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, - * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR - * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT - * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. - */ - -package gov.nist.secauto.metaschema.databind.io; - -/** - * Used to report exceptional conditions related to processing bound objects. - */ -public class BindingException - extends Exception { - - /** - * The serial version UID. - */ - private static final long serialVersionUID = 1L; - - // public BindingException(String message, Throwable cause, boolean - // enableSuppression, boolean writableStackTrace) { - // super(message, cause, enableSuppression, writableStackTrace); - // } - - /** - * Construct a new binding exception with the provided detail message and cause. - * - * @param message - * the detail message - * @param cause - * the cause of the exception - */ - public BindingException(String message, Throwable cause) { - super(message, cause); - } - - /** - * Construct a new binding exception with the provided detail message. - * - * @param message - * the detail message - */ - public BindingException(String message) { - super(message); - } - - /** - * Construct a new binding exception with the provided cause. - * - * @param cause - * the cause of the exception - */ - public BindingException(Throwable cause) { - super(cause); - } - -} +/* + * Portions of this software was developed by employees of the National Institute + * of Standards and Technology (NIST), an agency of the Federal Government and is + * being made available as a public service. Pursuant to title 17 United States + * Code Section 105, works of NIST employees are not subject to copyright + * protection in the United States. This software may be subject to foreign + * copyright. Permission in the United States and in foreign countries, to the + * extent that NIST may hold copyright, to use, copy, modify, create derivative + * works, and distribute this software and its documentation without fee is hereby + * granted on a non-exclusive basis, provided that this notice and disclaimer + * of warranty appears in all copies. + * + * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER + * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY + * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM + * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE + * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT + * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, + * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, + * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, + * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR + * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT + * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. + */ + +package gov.nist.secauto.metaschema.databind.io; + +/** + * Used to report exceptional conditions related to processing bound objects. + */ +public class BindingException + extends Exception { + + /** + * The serial version UID. + */ + private static final long serialVersionUID = 1L; + + // public BindingException(String message, Throwable cause, boolean + // enableSuppression, boolean writableStackTrace) { + // super(message, cause, enableSuppression, writableStackTrace); + // } + + /** + * Construct a new binding exception with the provided detail message and cause. + * + * @param message + * the detail message + * @param cause + * the cause of the exception + */ + public BindingException(String message, Throwable cause) { + super(message, cause); + } + + /** + * Construct a new binding exception with the provided detail message. + * + * @param message + * the detail message + */ + public BindingException(String message) { + super(message); + } + + /** + * Construct a new binding exception with the provided cause. + * + * @param cause + * the cause of the exception + */ + public BindingException(Throwable cause) { + super(cause); + } + +} diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/io/IParsingContext.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/io/IParsingContext.java index 4db25187e..4916b2a37 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/io/IParsingContext.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/io/IParsingContext.java @@ -1,55 +1,55 @@ -/* - * Portions of this software was developed by employees of the National Institute - * of Standards and Technology (NIST), an agency of the Federal Government and is - * being made available as a public service. Pursuant to title 17 United States - * Code Section 105, works of NIST employees are not subject to copyright - * protection in the United States. This software may be subject to foreign - * copyright. Permission in the United States and in foreign countries, to the - * extent that NIST may hold copyright, to use, copy, modify, create derivative - * works, and distribute this software and its documentation without fee is hereby - * granted on a non-exclusive basis, provided that this notice and disclaimer - * of warranty appears in all copies. - * - * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER - * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY - * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM - * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE - * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT - * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, - * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, - * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, - * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR - * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT - * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. - */ - -package gov.nist.secauto.metaschema.databind.io; - -import edu.umd.cs.findbugs.annotations.NonNull; - -/** - * Provides objects used for parsing data associated with a specific format. - * - * @param - * the format specific data reader - * @param - * the format specific problem handler - */ -public interface IParsingContext { - /** - * The parser used for reading data associated with the supported format. - * - * @return the parser - */ - @NonNull - READER getReader(); - - /** - * A handler that provides callbacks used to resolve parsing issues. - * - * @return the configured handler - */ - @NonNull - PROBLEM_HANDLER getProblemHandler(); -} +/* + * Portions of this software was developed by employees of the National Institute + * of Standards and Technology (NIST), an agency of the Federal Government and is + * being made available as a public service. Pursuant to title 17 United States + * Code Section 105, works of NIST employees are not subject to copyright + * protection in the United States. This software may be subject to foreign + * copyright. Permission in the United States and in foreign countries, to the + * extent that NIST may hold copyright, to use, copy, modify, create derivative + * works, and distribute this software and its documentation without fee is hereby + * granted on a non-exclusive basis, provided that this notice and disclaimer + * of warranty appears in all copies. + * + * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER + * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY + * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM + * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE + * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT + * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, + * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, + * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, + * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR + * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT + * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. + */ + +package gov.nist.secauto.metaschema.databind.io; + +import edu.umd.cs.findbugs.annotations.NonNull; + +/** + * Provides objects used for parsing data associated with a specific format. + * + * @param + * the format specific data reader + * @param + * the format specific problem handler + */ +public interface IParsingContext { + /** + * The parser used for reading data associated with the supported format. + * + * @return the parser + */ + @NonNull + READER getReader(); + + /** + * A handler that provides callbacks used to resolve parsing issues. + * + * @return the configured handler + */ + @NonNull + PROBLEM_HANDLER getProblemHandler(); +} diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/io/xml/CommentFilter.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/io/xml/CommentFilter.java index 7633f2541..f30126b7f 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/io/xml/CommentFilter.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/io/xml/CommentFilter.java @@ -1,40 +1,40 @@ -/* - * Portions of this software was developed by employees of the National Institute - * of Standards and Technology (NIST), an agency of the Federal Government and is - * being made available as a public service. Pursuant to title 17 United States - * Code Section 105, works of NIST employees are not subject to copyright - * protection in the United States. This software may be subject to foreign - * copyright. Permission in the United States and in foreign countries, to the - * extent that NIST may hold copyright, to use, copy, modify, create derivative - * works, and distribute this software and its documentation without fee is hereby - * granted on a non-exclusive basis, provided that this notice and disclaimer - * of warranty appears in all copies. - * - * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER - * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY - * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM - * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE - * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT - * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, - * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, - * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, - * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR - * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT - * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. - */ - -package gov.nist.secauto.metaschema.databind.io.xml; - -import javax.xml.stream.EventFilter; -import javax.xml.stream.XMLStreamConstants; -import javax.xml.stream.events.XMLEvent; - -public class CommentFilter implements EventFilter { - - @Override - public boolean accept(XMLEvent event) { - return event.getEventType() != XMLStreamConstants.COMMENT; - } - -} +/* + * Portions of this software was developed by employees of the National Institute + * of Standards and Technology (NIST), an agency of the Federal Government and is + * being made available as a public service. Pursuant to title 17 United States + * Code Section 105, works of NIST employees are not subject to copyright + * protection in the United States. This software may be subject to foreign + * copyright. Permission in the United States and in foreign countries, to the + * extent that NIST may hold copyright, to use, copy, modify, create derivative + * works, and distribute this software and its documentation without fee is hereby + * granted on a non-exclusive basis, provided that this notice and disclaimer + * of warranty appears in all copies. + * + * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER + * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY + * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM + * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE + * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT + * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, + * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, + * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, + * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR + * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT + * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. + */ + +package gov.nist.secauto.metaschema.databind.io.xml; + +import javax.xml.stream.EventFilter; +import javax.xml.stream.XMLStreamConstants; +import javax.xml.stream.events.XMLEvent; + +public class CommentFilter implements EventFilter { + + @Override + public boolean accept(XMLEvent event) { + return event.getEventType() != XMLStreamConstants.COMMENT; + } + +} diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/io/xml/IXmlParsingContext.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/io/xml/IXmlParsingContext.java index 84cd916df..fb40e511a 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/io/xml/IXmlParsingContext.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/io/xml/IXmlParsingContext.java @@ -1,84 +1,84 @@ -/* - * Portions of this software was developed by employees of the National Institute - * of Standards and Technology (NIST), an agency of the Federal Government and is - * being made available as a public service. Pursuant to title 17 United States - * Code Section 105, works of NIST employees are not subject to copyright - * protection in the United States. This software may be subject to foreign - * copyright. Permission in the United States and in foreign countries, to the - * extent that NIST may hold copyright, to use, copy, modify, create derivative - * works, and distribute this software and its documentation without fee is hereby - * granted on a non-exclusive basis, provided that this notice and disclaimer - * of warranty appears in all copies. - * - * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER - * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY - * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM - * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE - * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT - * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, - * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, - * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, - * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR - * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT - * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. - */ - -package gov.nist.secauto.metaschema.databind.io.xml; - -import gov.nist.secauto.metaschema.databind.io.IParsingContext; -import gov.nist.secauto.metaschema.databind.model.IBoundDefinitionModelComplex; -import gov.nist.secauto.metaschema.databind.model.IBoundInstanceModel; - -import org.codehaus.stax2.XMLEventReader2; - -import java.io.IOException; - -import javax.xml.stream.XMLStreamConstants; - -import edu.umd.cs.findbugs.annotations.NonNull; - -public interface IXmlParsingContext extends IParsingContext { - - // boolean readItems( - // @NonNull IBoundInstanceModel instance, - // @NonNull Object parentObject) throws IOException, XMLStreamException; - - /** - * Parses XML into a bound object based on the provided {@code definition}. - *

- * Parses the {@link XMLStreamConstants#START_DOCUMENT}, any processing - * instructions, and the element. - * - * @param - * the returned object type - * @param definition - * the definition describing the element data to read - * @return the parsed object - * @throws IOException - * if an error occurred while parsing the input - */ - CLASS read(@NonNull IBoundDefinitionModelComplex definition) throws IOException; - - /** - * Read the data associated with the {@code instance} and apply it to the - * provided {@code parentObject}. - * - * @param instance - * the instance to parse data for - * @param parentObject - * the Java object that data parsed by this method will be stored in - * @param parseGrouping - * if {@code true} parse the instance's grouping element or - * {@code false} otherwise - * @return {@code true} if the instance was parsed, or {@code false} if the data - * did not contain information for this instance - * @throws IOException - * if an error occurred while parsing the input - * - */ - boolean readItems( - @NonNull IBoundInstanceModel instance, - @NonNull Object parentObject, - boolean parseGrouping) throws IOException; -} +/* + * Portions of this software was developed by employees of the National Institute + * of Standards and Technology (NIST), an agency of the Federal Government and is + * being made available as a public service. Pursuant to title 17 United States + * Code Section 105, works of NIST employees are not subject to copyright + * protection in the United States. This software may be subject to foreign + * copyright. Permission in the United States and in foreign countries, to the + * extent that NIST may hold copyright, to use, copy, modify, create derivative + * works, and distribute this software and its documentation without fee is hereby + * granted on a non-exclusive basis, provided that this notice and disclaimer + * of warranty appears in all copies. + * + * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER + * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY + * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM + * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE + * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT + * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, + * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, + * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, + * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR + * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT + * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. + */ + +package gov.nist.secauto.metaschema.databind.io.xml; + +import gov.nist.secauto.metaschema.databind.io.IParsingContext; +import gov.nist.secauto.metaschema.databind.model.IBoundDefinitionModelComplex; +import gov.nist.secauto.metaschema.databind.model.IBoundInstanceModel; + +import org.codehaus.stax2.XMLEventReader2; + +import java.io.IOException; + +import javax.xml.stream.XMLStreamConstants; + +import edu.umd.cs.findbugs.annotations.NonNull; + +public interface IXmlParsingContext extends IParsingContext { + + // boolean readItems( + // @NonNull IBoundInstanceModel instance, + // @NonNull Object parentObject) throws IOException, XMLStreamException; + + /** + * Parses XML into a bound object based on the provided {@code definition}. + *

+ * Parses the {@link XMLStreamConstants#START_DOCUMENT}, any processing + * instructions, and the element. + * + * @param + * the returned object type + * @param definition + * the definition describing the element data to read + * @return the parsed object + * @throws IOException + * if an error occurred while parsing the input + */ + CLASS read(@NonNull IBoundDefinitionModelComplex definition) throws IOException; + + /** + * Read the data associated with the {@code instance} and apply it to the + * provided {@code parentObject}. + * + * @param instance + * the instance to parse data for + * @param parentObject + * the Java object that data parsed by this method will be stored in + * @param parseGrouping + * if {@code true} parse the instance's grouping element or + * {@code false} otherwise + * @return {@code true} if the instance was parsed, or {@code false} if the data + * did not contain information for this instance + * @throws IOException + * if an error occurred while parsing the input + * + */ + boolean readItems( + @NonNull IBoundInstanceModel instance, + @NonNull Object parentObject, + boolean parseGrouping) throws IOException; +} diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/io/xml/IXmlProblemHandler.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/io/xml/IXmlProblemHandler.java index a6120ce45..4949267fa 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/io/xml/IXmlProblemHandler.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/io/xml/IXmlProblemHandler.java @@ -1,140 +1,140 @@ -/* - * Portions of this software was developed by employees of the National Institute - * of Standards and Technology (NIST), an agency of the Federal Government and is - * being made available as a public service. Pursuant to title 17 United States - * Code Section 105, works of NIST employees are not subject to copyright - * protection in the United States. This software may be subject to foreign - * copyright. Permission in the United States and in foreign countries, to the - * extent that NIST may hold copyright, to use, copy, modify, create derivative - * works, and distribute this software and its documentation without fee is hereby - * granted on a non-exclusive basis, provided that this notice and disclaimer - * of warranty appears in all copies. - * - * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER - * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY - * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM - * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE - * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT - * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, - * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, - * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, - * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR - * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT - * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. - */ - -package gov.nist.secauto.metaschema.databind.io.xml; - -import gov.nist.secauto.metaschema.databind.io.IProblemHandler; -import gov.nist.secauto.metaschema.databind.model.IBoundDefinitionModel; -import gov.nist.secauto.metaschema.databind.model.IBoundDefinitionModelAssembly; -import gov.nist.secauto.metaschema.databind.model.IBoundInstanceFlag; -import gov.nist.secauto.metaschema.databind.model.IBoundInstanceModel; - -import java.io.IOException; -import java.util.Collection; - -import javax.xml.stream.events.Attribute; -import javax.xml.stream.events.StartElement; - -import edu.umd.cs.findbugs.annotations.NonNull; - -public interface IXmlProblemHandler extends IProblemHandler { - /** - * Callback used to handle an attribute that is unknown to the model being - * parsed. - * - * @param parentDefinition - * the bound class currently describing the data being parsed - * @param targetObject - * the Java object for the {@code parentDefinition} - * @param attribute - * the unknown attribute - * @param parsingContext - * the XML parsing context used for parsing - * @return {@code true} if the attribute was handled by this method, or - * {@code false} otherwise - * @throws IOException - * if an error occurred while handling the unrecognized data - */ - default boolean handleUnknownAttribute( - @NonNull IBoundDefinitionModel parentDefinition, - @NonNull Object targetObject, - @NonNull Attribute attribute, - @NonNull IXmlParsingContext parsingContext) throws IOException { - return false; - } - - /** - * Callback used to handle an element that is unknown to the model being parsed. - * - * @param parentDefinition - * the bound assembly class on which the missing instances are found - * @param targetObject - * the Java object for the {@code parentDefinition} - * @param start - * the parsed XML start element - * @param parsingContext - * the XML parsing context used for parsing - * @return {@code true} if the element was handled by this method, or - * {@code false} otherwise - * @throws IOException - * if an error occurred while handling the unrecognized data - */ - default boolean handleUnknownElement( - @NonNull IBoundDefinitionModelAssembly parentDefinition, - @NonNull Object targetObject, - @NonNull StartElement start, - @NonNull IXmlParsingContext parsingContext) throws IOException { - return false; - } - - /** - * A callback used to handle bound flag instances for which no data was found - * when the content was parsed. - *

- * This can be used to supply default or prescribed values based on application - * logic. - * - * @param parentDefinition - * the bound assembly class on which the missing instances are found - * @param targetObject - * the Java object for the {@code parentDefinition} - * @param unhandledInstances - * the set of instances that had no data to parse - * @throws IOException - * if an error occurred while handling the missing instances - */ - default void handleMissingFlagInstances( - @NonNull IBoundDefinitionModel parentDefinition, - @NonNull Object targetObject, - @NonNull Collection unhandledInstances) - throws IOException { - handleMissingInstances(parentDefinition, targetObject, unhandledInstances); - } - - /** - * A callback used to handle bound model instances for which no data was found - * when the content was parsed. - *

- * This can be used to supply default or prescribed values based on application - * logic. - * - * @param parentDefinition - * the bound assembly class on which the missing instances are found - * @param targetObject - * the Java object for the {@code parentDefinition} - * @param unhandledInstances - * the set of instances that had no data to parse - * @throws IOException - * if an error occurred while handling the missing instances - */ - default void handleMissingModelInstances( - @NonNull IBoundDefinitionModelAssembly parentDefinition, - @NonNull Object targetObject, - @NonNull Collection unhandledInstances) - throws IOException { - handleMissingInstances(parentDefinition, targetObject, unhandledInstances); - } -} +/* + * Portions of this software was developed by employees of the National Institute + * of Standards and Technology (NIST), an agency of the Federal Government and is + * being made available as a public service. Pursuant to title 17 United States + * Code Section 105, works of NIST employees are not subject to copyright + * protection in the United States. This software may be subject to foreign + * copyright. Permission in the United States and in foreign countries, to the + * extent that NIST may hold copyright, to use, copy, modify, create derivative + * works, and distribute this software and its documentation without fee is hereby + * granted on a non-exclusive basis, provided that this notice and disclaimer + * of warranty appears in all copies. + * + * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER + * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY + * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM + * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE + * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT + * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, + * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, + * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, + * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR + * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT + * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. + */ + +package gov.nist.secauto.metaschema.databind.io.xml; + +import gov.nist.secauto.metaschema.databind.io.IProblemHandler; +import gov.nist.secauto.metaschema.databind.model.IBoundDefinitionModel; +import gov.nist.secauto.metaschema.databind.model.IBoundDefinitionModelAssembly; +import gov.nist.secauto.metaschema.databind.model.IBoundInstanceFlag; +import gov.nist.secauto.metaschema.databind.model.IBoundInstanceModel; + +import java.io.IOException; +import java.util.Collection; + +import javax.xml.stream.events.Attribute; +import javax.xml.stream.events.StartElement; + +import edu.umd.cs.findbugs.annotations.NonNull; + +public interface IXmlProblemHandler extends IProblemHandler { + /** + * Callback used to handle an attribute that is unknown to the model being + * parsed. + * + * @param parentDefinition + * the bound class currently describing the data being parsed + * @param targetObject + * the Java object for the {@code parentDefinition} + * @param attribute + * the unknown attribute + * @param parsingContext + * the XML parsing context used for parsing + * @return {@code true} if the attribute was handled by this method, or + * {@code false} otherwise + * @throws IOException + * if an error occurred while handling the unrecognized data + */ + default boolean handleUnknownAttribute( + @NonNull IBoundDefinitionModel parentDefinition, + @NonNull Object targetObject, + @NonNull Attribute attribute, + @NonNull IXmlParsingContext parsingContext) throws IOException { + return false; + } + + /** + * Callback used to handle an element that is unknown to the model being parsed. + * + * @param parentDefinition + * the bound assembly class on which the missing instances are found + * @param targetObject + * the Java object for the {@code parentDefinition} + * @param start + * the parsed XML start element + * @param parsingContext + * the XML parsing context used for parsing + * @return {@code true} if the element was handled by this method, or + * {@code false} otherwise + * @throws IOException + * if an error occurred while handling the unrecognized data + */ + default boolean handleUnknownElement( + @NonNull IBoundDefinitionModelAssembly parentDefinition, + @NonNull Object targetObject, + @NonNull StartElement start, + @NonNull IXmlParsingContext parsingContext) throws IOException { + return false; + } + + /** + * A callback used to handle bound flag instances for which no data was found + * when the content was parsed. + *

+ * This can be used to supply default or prescribed values based on application + * logic. + * + * @param parentDefinition + * the bound assembly class on which the missing instances are found + * @param targetObject + * the Java object for the {@code parentDefinition} + * @param unhandledInstances + * the set of instances that had no data to parse + * @throws IOException + * if an error occurred while handling the missing instances + */ + default void handleMissingFlagInstances( + @NonNull IBoundDefinitionModel parentDefinition, + @NonNull Object targetObject, + @NonNull Collection unhandledInstances) + throws IOException { + handleMissingInstances(parentDefinition, targetObject, unhandledInstances); + } + + /** + * A callback used to handle bound model instances for which no data was found + * when the content was parsed. + *

+ * This can be used to supply default or prescribed values based on application + * logic. + * + * @param parentDefinition + * the bound assembly class on which the missing instances are found + * @param targetObject + * the Java object for the {@code parentDefinition} + * @param unhandledInstances + * the set of instances that had no data to parse + * @throws IOException + * if an error occurred while handling the missing instances + */ + default void handleMissingModelInstances( + @NonNull IBoundDefinitionModelAssembly parentDefinition, + @NonNull Object targetObject, + @NonNull Collection unhandledInstances) + throws IOException { + handleMissingInstances(parentDefinition, targetObject, unhandledInstances); + } +} diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/annotations/BoundAssembly.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/annotations/BoundAssembly.java index f31a8570c..3e4cf04c8 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/annotations/BoundAssembly.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/annotations/BoundAssembly.java @@ -1,145 +1,145 @@ -/* - * Portions of this software was developed by employees of the National Institute - * of Standards and Technology (NIST), an agency of the Federal Government and is - * being made available as a public service. Pursuant to title 17 United States - * Code Section 105, works of NIST employees are not subject to copyright - * protection in the United States. This software may be subject to foreign - * copyright. Permission in the United States and in foreign countries, to the - * extent that NIST may hold copyright, to use, copy, modify, create derivative - * works, and distribute this software and its documentation without fee is hereby - * granted on a non-exclusive basis, provided that this notice and disclaimer - * of warranty appears in all copies. - * - * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER - * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY - * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM - * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE - * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT - * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, - * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, - * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, - * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR - * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT - * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. - */ - -package gov.nist.secauto.metaschema.databind.model.annotations; - -import static java.lang.annotation.ElementType.ANNOTATION_TYPE; -import static java.lang.annotation.ElementType.FIELD; -import static java.lang.annotation.ElementType.METHOD; -import static java.lang.annotation.RetentionPolicy.RUNTIME; - -import gov.nist.secauto.metaschema.core.model.IGroupable; - -import java.lang.annotation.Documented; -import java.lang.annotation.Retention; -import java.lang.annotation.Target; - -import edu.umd.cs.findbugs.annotations.NonNull; - -/** - * Identifies that the annotation target is a bound property that references a - * Module assembly. - *

- * For XML serialization, the {@link #useName()} identifies the name of the - * element to use and the {@link #namespace()} identifies the namespace of this - * element. - *

- * For JSON and YAML serializations, the {@link #useName()} identifies the - * property/item name to use. - */ -@Documented -@Retention(RUNTIME) -@Target({ FIELD, METHOD, ANNOTATION_TYPE }) -public @interface BoundAssembly { - /** - * Get the documentary formal name of the assembly. - *

- * If the value is "##none", then the description will be considered - * {@code null}. - * - * @return a markdown string or {@code "##none"} if no formal name is provided - */ - @NonNull - String formalName() default ModelUtil.NO_STRING_VALUE; - - /** - * Get the documentary description of the assembly. - *

- * If the value is "##none", then the description will be considered - * {@code null}. - * - * @return a markdown string or {@code "##none"} if no description is provided - */ - @NonNull - String description() default ModelUtil.NO_STRING_VALUE; - - /** - * The model name to use for singleton values. This name will be used for - * associated XML elements. - *

- * If the value is "##none", then element name is derived from the JavaBean - * property name. - * - * @return the name or {@code "##none"} if no use name is provided - */ - @NonNull - String useName() default ModelUtil.NO_STRING_VALUE; - - /** - * The binary use name of the assembly. - *

- * The value {@link Integer#MIN_VALUE} indicates that there is no use name. - * - * @return the index value - */ - int useIndex() default Integer.MIN_VALUE; - - /** - * The namespace to use for associated XML elements. - *

- * If the value is "##default", then element name is derived from the namespace - * provided in the package-info. - * - * @return the namespace - */ - @NonNull - String namespace() default ModelUtil.DEFAULT_STRING_VALUE; - - /** - * A non-negative number that indicates the minimum occurrence of the model - * instance. - * - * @return a non-negative number - */ - int minOccurs() default IGroupable.DEFAULT_GROUP_AS_MIN_OCCURS; - - /** - * A number that indicates the maximum occurrence of the model instance. - * - * @return a positive number or {@code -1} to indicate "unbounded" - */ - int maxOccurs() default IGroupable.DEFAULT_GROUP_AS_MAX_OCCURS; - - /** - * Get any remarks for this field. - * - * @return a markdown string or {@code "##none"} if no remarks are provided - */ - @NonNull - String remarks() default ModelUtil.NO_STRING_VALUE; - - /** - * Used to provide grouping information. - *

- * This annotation is required when the value of {@link #maxOccurs()} is greater - * than 1. - * - * @return the configured {@link GroupAs} or the default value with a - * {@code null} {@link GroupAs#name()} - */ - @NonNull - GroupAs groupAs() default @GroupAs(name = ModelUtil.NULL_VALUE); -} +/* + * Portions of this software was developed by employees of the National Institute + * of Standards and Technology (NIST), an agency of the Federal Government and is + * being made available as a public service. Pursuant to title 17 United States + * Code Section 105, works of NIST employees are not subject to copyright + * protection in the United States. This software may be subject to foreign + * copyright. Permission in the United States and in foreign countries, to the + * extent that NIST may hold copyright, to use, copy, modify, create derivative + * works, and distribute this software and its documentation without fee is hereby + * granted on a non-exclusive basis, provided that this notice and disclaimer + * of warranty appears in all copies. + * + * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER + * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY + * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM + * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE + * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT + * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, + * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, + * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, + * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR + * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT + * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. + */ + +package gov.nist.secauto.metaschema.databind.model.annotations; + +import static java.lang.annotation.ElementType.ANNOTATION_TYPE; +import static java.lang.annotation.ElementType.FIELD; +import static java.lang.annotation.ElementType.METHOD; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +import gov.nist.secauto.metaschema.core.model.IGroupable; + +import java.lang.annotation.Documented; +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +import edu.umd.cs.findbugs.annotations.NonNull; + +/** + * Identifies that the annotation target is a bound property that references a + * Module assembly. + *

+ * For XML serialization, the {@link #useName()} identifies the name of the + * element to use and the {@link #namespace()} identifies the namespace of this + * element. + *

+ * For JSON and YAML serializations, the {@link #useName()} identifies the + * property/item name to use. + */ +@Documented +@Retention(RUNTIME) +@Target({ FIELD, METHOD, ANNOTATION_TYPE }) +public @interface BoundAssembly { + /** + * Get the documentary formal name of the assembly. + *

+ * If the value is "##none", then the description will be considered + * {@code null}. + * + * @return a markdown string or {@code "##none"} if no formal name is provided + */ + @NonNull + String formalName() default ModelUtil.NO_STRING_VALUE; + + /** + * Get the documentary description of the assembly. + *

+ * If the value is "##none", then the description will be considered + * {@code null}. + * + * @return a markdown string or {@code "##none"} if no description is provided + */ + @NonNull + String description() default ModelUtil.NO_STRING_VALUE; + + /** + * The model name to use for singleton values. This name will be used for + * associated XML elements. + *

+ * If the value is "##none", then element name is derived from the JavaBean + * property name. + * + * @return the name or {@code "##none"} if no use name is provided + */ + @NonNull + String useName() default ModelUtil.NO_STRING_VALUE; + + /** + * The binary use name of the assembly. + *

+ * The value {@link Integer#MIN_VALUE} indicates that there is no use name. + * + * @return the index value + */ + int useIndex() default Integer.MIN_VALUE; + + /** + * The namespace to use for associated XML elements. + *

+ * If the value is "##default", then element name is derived from the namespace + * provided in the package-info. + * + * @return the namespace + */ + @NonNull + String namespace() default ModelUtil.DEFAULT_STRING_VALUE; + + /** + * A non-negative number that indicates the minimum occurrence of the model + * instance. + * + * @return a non-negative number + */ + int minOccurs() default IGroupable.DEFAULT_GROUP_AS_MIN_OCCURS; + + /** + * A number that indicates the maximum occurrence of the model instance. + * + * @return a positive number or {@code -1} to indicate "unbounded" + */ + int maxOccurs() default IGroupable.DEFAULT_GROUP_AS_MAX_OCCURS; + + /** + * Get any remarks for this field. + * + * @return a markdown string or {@code "##none"} if no remarks are provided + */ + @NonNull + String remarks() default ModelUtil.NO_STRING_VALUE; + + /** + * Used to provide grouping information. + *

+ * This annotation is required when the value of {@link #maxOccurs()} is greater + * than 1. + * + * @return the configured {@link GroupAs} or the default value with a + * {@code null} {@link GroupAs#name()} + */ + @NonNull + GroupAs groupAs() default @GroupAs(name = ModelUtil.NULL_VALUE); +} diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/annotations/BoundField.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/annotations/BoundField.java index 7f918300a..21db7dfd4 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/annotations/BoundField.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/annotations/BoundField.java @@ -1,190 +1,190 @@ -/* - * Portions of this software was developed by employees of the National Institute - * of Standards and Technology (NIST), an agency of the Federal Government and is - * being made available as a public service. Pursuant to title 17 United States - * Code Section 105, works of NIST employees are not subject to copyright - * protection in the United States. This software may be subject to foreign - * copyright. Permission in the United States and in foreign countries, to the - * extent that NIST may hold copyright, to use, copy, modify, create derivative - * works, and distribute this software and its documentation without fee is hereby - * granted on a non-exclusive basis, provided that this notice and disclaimer - * of warranty appears in all copies. - * - * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER - * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY - * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM - * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE - * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT - * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, - * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, - * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, - * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR - * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT - * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. - */ - -package gov.nist.secauto.metaschema.databind.model.annotations; - -import static java.lang.annotation.ElementType.FIELD; -import static java.lang.annotation.ElementType.METHOD; -import static java.lang.annotation.RetentionPolicy.RUNTIME; - -import gov.nist.secauto.metaschema.core.datatype.IDataTypeAdapter; -import gov.nist.secauto.metaschema.core.model.IFieldInstance; -import gov.nist.secauto.metaschema.core.model.IGroupable; - -import java.lang.annotation.Documented; -import java.lang.annotation.Retention; -import java.lang.annotation.Target; - -import edu.umd.cs.findbugs.annotations.NonNull; - -/** - * Identifies that the annotation target is a bound property that references a - * Module field. - *

- * For XML serialization, the {@link #useName()} identifies the name of the - * element to use and the {@link #namespace()} identifies the namespace of this - * element. - *

- * For JSON and YAML serializations, the {@link #useName()} identifies the - * property/item name to use. - *

- * The field must be either: - *

    - *
  1. A Module data type or a collection whose item value is Module data type, - * with a non-null {@link #typeAdapter()}. - *
  2. A type or a collection whose item value is a type based on a class with a - * {@link MetaschemaField} annotation, with a property annotated with - * {@link BoundFieldValue}.
  3. - *
- */ -@Documented -@Retention(RUNTIME) -@Target({ FIELD, METHOD }) -public @interface BoundField { - /** - * Get the documentary formal name of the field. - *

- * If the value is "##none", then the description will be considered - * {@code null}. - * - * @return a markdown string or {@code "##none"} if no formal name is provided - */ - @NonNull - String formalName() default ModelUtil.NO_STRING_VALUE; - - /** - * Get the documentary description of the field. - *

- * If the value is "##none", then the description will be considered - * {@code null}. - * - * @return a markdown string or {@code "##none"} if no description is provided - */ - @NonNull - String description() default ModelUtil.NO_STRING_VALUE; - - /** - * The model name to use for JSON/YAML singleton values and associated XML - * elements. - *

- * If the value is "##none", then the use name will be provided by the - * definition or by the field name if the item value class is missing the - * {@link MetaschemaField} annotation. - * - * @return the name - */ - @NonNull - String useName() default ModelUtil.NO_STRING_VALUE; - - /** - * The binary use name of the field. - *

- * The value {@link Integer#MIN_VALUE} indicates that there is no use name. - * - * @return the index value - */ - int useIndex() default Integer.MIN_VALUE; - - /** - * The Metaschema data type adapter for the field's value. - * - * @return the data type adapter - */ - @NonNull - Class> typeAdapter() default NullJavaTypeAdapter.class; - - /** - * The default value of the field represented as a string. - *

- * The value {@link ModelUtil#NULL_VALUE} is used to indicate if no default - * value is provided. - * - * @return the default value - */ - @NonNull - String defaultValue() default ModelUtil.NULL_VALUE; - - /** - * The namespace to use for associated XML elements. - *

- * If the value is "##default", then element name is derived from the namespace - * provided in the package-info. - * - * @return the namespace - */ - String namespace() default ModelUtil.DEFAULT_STRING_VALUE; - - /** - * If the data type allows it, determines if the field's value must be wrapped - * with an XML element. - * - * @return {@code true} if the field must be wrapped, or {@code false} otherwise - */ - boolean inXmlWrapped() default IFieldInstance.DEFAULT_FIELD_IN_XML_WRAPPED; - - /** - * A non-negative number that indicates the minimum occurrence of the model - * instance. - * - * @return a non-negative number - */ - int minOccurs() default IGroupable.DEFAULT_GROUP_AS_MIN_OCCURS; - - /** - * A number that indicates the maximum occurrence of the model instance. - * - * @return a positive number or {@code -1} to indicate "unbounded" - */ - int maxOccurs() default IGroupable.DEFAULT_GROUP_AS_MAX_OCCURS; - - /** - * Get any remarks for this field. - * - * @return a markdown string or {@code "##none"} if no remarks are provided - */ - @NonNull - String remarks() default ModelUtil.NO_STRING_VALUE; - - /** - * Used to provide grouping information. - *

- * This annotation is required when the value of {@link #maxOccurs()} is greater - * than 1. - * - * @return the configured {@link GroupAs} or the default value with a - * {@code null} {@link GroupAs#name()} - */ - @NonNull - GroupAs groupAs() default @GroupAs(name = ModelUtil.NULL_VALUE); - - /** - * Get the value constraints defined for this Metaschema field inline - * definition. - * - * @return the value constraints - */ - ValueConstraints valueConstraints() default @ValueConstraints; -} +/* + * Portions of this software was developed by employees of the National Institute + * of Standards and Technology (NIST), an agency of the Federal Government and is + * being made available as a public service. Pursuant to title 17 United States + * Code Section 105, works of NIST employees are not subject to copyright + * protection in the United States. This software may be subject to foreign + * copyright. Permission in the United States and in foreign countries, to the + * extent that NIST may hold copyright, to use, copy, modify, create derivative + * works, and distribute this software and its documentation without fee is hereby + * granted on a non-exclusive basis, provided that this notice and disclaimer + * of warranty appears in all copies. + * + * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER + * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY + * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM + * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE + * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT + * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, + * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, + * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, + * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR + * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT + * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. + */ + +package gov.nist.secauto.metaschema.databind.model.annotations; + +import static java.lang.annotation.ElementType.FIELD; +import static java.lang.annotation.ElementType.METHOD; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +import gov.nist.secauto.metaschema.core.datatype.IDataTypeAdapter; +import gov.nist.secauto.metaschema.core.model.IFieldInstance; +import gov.nist.secauto.metaschema.core.model.IGroupable; + +import java.lang.annotation.Documented; +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +import edu.umd.cs.findbugs.annotations.NonNull; + +/** + * Identifies that the annotation target is a bound property that references a + * Module field. + *

+ * For XML serialization, the {@link #useName()} identifies the name of the + * element to use and the {@link #namespace()} identifies the namespace of this + * element. + *

+ * For JSON and YAML serializations, the {@link #useName()} identifies the + * property/item name to use. + *

+ * The field must be either: + *

    + *
  1. A Module data type or a collection whose item value is Module data type, + * with a non-null {@link #typeAdapter()}. + *
  2. A type or a collection whose item value is a type based on a class with a + * {@link MetaschemaField} annotation, with a property annotated with + * {@link BoundFieldValue}.
  3. + *
+ */ +@Documented +@Retention(RUNTIME) +@Target({ FIELD, METHOD }) +public @interface BoundField { + /** + * Get the documentary formal name of the field. + *

+ * If the value is "##none", then the description will be considered + * {@code null}. + * + * @return a markdown string or {@code "##none"} if no formal name is provided + */ + @NonNull + String formalName() default ModelUtil.NO_STRING_VALUE; + + /** + * Get the documentary description of the field. + *

+ * If the value is "##none", then the description will be considered + * {@code null}. + * + * @return a markdown string or {@code "##none"} if no description is provided + */ + @NonNull + String description() default ModelUtil.NO_STRING_VALUE; + + /** + * The model name to use for JSON/YAML singleton values and associated XML + * elements. + *

+ * If the value is "##none", then the use name will be provided by the + * definition or by the field name if the item value class is missing the + * {@link MetaschemaField} annotation. + * + * @return the name + */ + @NonNull + String useName() default ModelUtil.NO_STRING_VALUE; + + /** + * The binary use name of the field. + *

+ * The value {@link Integer#MIN_VALUE} indicates that there is no use name. + * + * @return the index value + */ + int useIndex() default Integer.MIN_VALUE; + + /** + * The Metaschema data type adapter for the field's value. + * + * @return the data type adapter + */ + @NonNull + Class> typeAdapter() default NullJavaTypeAdapter.class; + + /** + * The default value of the field represented as a string. + *

+ * The value {@link ModelUtil#NULL_VALUE} is used to indicate if no default + * value is provided. + * + * @return the default value + */ + @NonNull + String defaultValue() default ModelUtil.NULL_VALUE; + + /** + * The namespace to use for associated XML elements. + *

+ * If the value is "##default", then element name is derived from the namespace + * provided in the package-info. + * + * @return the namespace + */ + String namespace() default ModelUtil.DEFAULT_STRING_VALUE; + + /** + * If the data type allows it, determines if the field's value must be wrapped + * with an XML element. + * + * @return {@code true} if the field must be wrapped, or {@code false} otherwise + */ + boolean inXmlWrapped() default IFieldInstance.DEFAULT_FIELD_IN_XML_WRAPPED; + + /** + * A non-negative number that indicates the minimum occurrence of the model + * instance. + * + * @return a non-negative number + */ + int minOccurs() default IGroupable.DEFAULT_GROUP_AS_MIN_OCCURS; + + /** + * A number that indicates the maximum occurrence of the model instance. + * + * @return a positive number or {@code -1} to indicate "unbounded" + */ + int maxOccurs() default IGroupable.DEFAULT_GROUP_AS_MAX_OCCURS; + + /** + * Get any remarks for this field. + * + * @return a markdown string or {@code "##none"} if no remarks are provided + */ + @NonNull + String remarks() default ModelUtil.NO_STRING_VALUE; + + /** + * Used to provide grouping information. + *

+ * This annotation is required when the value of {@link #maxOccurs()} is greater + * than 1. + * + * @return the configured {@link GroupAs} or the default value with a + * {@code null} {@link GroupAs#name()} + */ + @NonNull + GroupAs groupAs() default @GroupAs(name = ModelUtil.NULL_VALUE); + + /** + * Get the value constraints defined for this Metaschema field inline + * definition. + * + * @return the value constraints + */ + ValueConstraints valueConstraints() default @ValueConstraints; +} diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/annotations/BoundFlag.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/annotations/BoundFlag.java index 4a5f2833d..061188c30 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/annotations/BoundFlag.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/annotations/BoundFlag.java @@ -1,146 +1,146 @@ -/* - * Portions of this software was developed by employees of the National Institute - * of Standards and Technology (NIST), an agency of the Federal Government and is - * being made available as a public service. Pursuant to title 17 United States - * Code Section 105, works of NIST employees are not subject to copyright - * protection in the United States. This software may be subject to foreign - * copyright. Permission in the United States and in foreign countries, to the - * extent that NIST may hold copyright, to use, copy, modify, create derivative - * works, and distribute this software and its documentation without fee is hereby - * granted on a non-exclusive basis, provided that this notice and disclaimer - * of warranty appears in all copies. - * - * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER - * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY - * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM - * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE - * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT - * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, - * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, - * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, - * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR - * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT - * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. - */ - -package gov.nist.secauto.metaschema.databind.model.annotations; - -import static java.lang.annotation.ElementType.FIELD; -import static java.lang.annotation.ElementType.METHOD; -import static java.lang.annotation.RetentionPolicy.RUNTIME; - -import gov.nist.secauto.metaschema.core.datatype.IDataTypeAdapter; - -import java.lang.annotation.Documented; -import java.lang.annotation.Retention; -import java.lang.annotation.Target; - -import edu.umd.cs.findbugs.annotations.NonNull; - -/** - * Identifies that the annotation target is a bound property that represents a - * Module flag. - */ -@Documented -@Retention(RUNTIME) -@Target({ FIELD, METHOD }) -public @interface BoundFlag { - /** - * Get the documentary formal name of the flag. - *

- * If the value is "##none", then the description will be considered - * {@code null}. - * - * @return a markdown string or {@code "##none"} if no formal name is provided - */ - @NonNull - String formalName() default ModelUtil.NO_STRING_VALUE; - - /** - * Get the documentary description of the flag. - *

- * If the value is "##none", then the description will be considered - * {@code null}. - * - * @return a markdown string or {@code "##none"} if no description is provided - */ - @NonNull - String description() default ModelUtil.NO_STRING_VALUE; - - /** - * The model name to use for singleton values. This name will be used for - * associated XML attributes and JSON properties. - *

- * If the value is "##none", then element name is derived from the JavaBean - * property name. - * - * @return the name - */ - @NonNull - String name() default ModelUtil.NO_STRING_VALUE; - - /** - * The binary use name of the flag. - *

- * The value {@link Integer#MIN_VALUE} indicates that there is no use name. - * - * @return the index value - */ - int useIndex() default Integer.MIN_VALUE; - - /** - * XML target namespace of the XML Schema element. - *

- * If the value is "##default", then namespace is derived from the namespace - * provided in the package-info. If the value is "##none", the namespace will be - * {@code null}. - * - * @return the namespace - */ - @NonNull - String namespace() default ModelUtil.NO_STRING_VALUE; - - /** - * The default value of the flag represented as a string. - *

- * The value {@link ModelUtil#NULL_VALUE} is used to indicate if no default - * value is provided. - * - * @return the default value - */ - @NonNull - String defaultValue() default ModelUtil.NULL_VALUE; - - /** - * Specifies if the XML Schema attribute is optional or required. If true, then - * the JavaBean property is mapped to a XML Schema attribute that is required. - * Otherwise it is mapped to a XML Schema attribute that is optional. - * - * @return {@code true} if the flag must occur, or {@code false} otherwise - */ - boolean required() default false; - - /** - * The Module data type adapter for the field's value. - * - * @return the data type adapter - */ - @NonNull - Class> typeAdapter() default NullJavaTypeAdapter.class; - - /** - * Get any remarks for this flag. - * - * @return a markdown string or {@code "##none"} if no remarks are provided - */ - @NonNull - String remarks() default ModelUtil.NO_STRING_VALUE; - - /** - * Get the value constraints defined for this Metaschema flag inline definition. - * - * @return the value constraints - */ - ValueConstraints valueConstraints() default @ValueConstraints; -} +/* + * Portions of this software was developed by employees of the National Institute + * of Standards and Technology (NIST), an agency of the Federal Government and is + * being made available as a public service. Pursuant to title 17 United States + * Code Section 105, works of NIST employees are not subject to copyright + * protection in the United States. This software may be subject to foreign + * copyright. Permission in the United States and in foreign countries, to the + * extent that NIST may hold copyright, to use, copy, modify, create derivative + * works, and distribute this software and its documentation without fee is hereby + * granted on a non-exclusive basis, provided that this notice and disclaimer + * of warranty appears in all copies. + * + * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER + * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY + * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM + * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE + * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT + * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, + * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, + * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, + * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR + * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT + * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. + */ + +package gov.nist.secauto.metaschema.databind.model.annotations; + +import static java.lang.annotation.ElementType.FIELD; +import static java.lang.annotation.ElementType.METHOD; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +import gov.nist.secauto.metaschema.core.datatype.IDataTypeAdapter; + +import java.lang.annotation.Documented; +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +import edu.umd.cs.findbugs.annotations.NonNull; + +/** + * Identifies that the annotation target is a bound property that represents a + * Module flag. + */ +@Documented +@Retention(RUNTIME) +@Target({ FIELD, METHOD }) +public @interface BoundFlag { + /** + * Get the documentary formal name of the flag. + *

+ * If the value is "##none", then the description will be considered + * {@code null}. + * + * @return a markdown string or {@code "##none"} if no formal name is provided + */ + @NonNull + String formalName() default ModelUtil.NO_STRING_VALUE; + + /** + * Get the documentary description of the flag. + *

+ * If the value is "##none", then the description will be considered + * {@code null}. + * + * @return a markdown string or {@code "##none"} if no description is provided + */ + @NonNull + String description() default ModelUtil.NO_STRING_VALUE; + + /** + * The model name to use for singleton values. This name will be used for + * associated XML attributes and JSON properties. + *

+ * If the value is "##none", then element name is derived from the JavaBean + * property name. + * + * @return the name + */ + @NonNull + String name() default ModelUtil.NO_STRING_VALUE; + + /** + * The binary use name of the flag. + *

+ * The value {@link Integer#MIN_VALUE} indicates that there is no use name. + * + * @return the index value + */ + int useIndex() default Integer.MIN_VALUE; + + /** + * XML target namespace of the XML Schema element. + *

+ * If the value is "##default", then namespace is derived from the namespace + * provided in the package-info. If the value is "##none", the namespace will be + * {@code null}. + * + * @return the namespace + */ + @NonNull + String namespace() default ModelUtil.NO_STRING_VALUE; + + /** + * The default value of the flag represented as a string. + *

+ * The value {@link ModelUtil#NULL_VALUE} is used to indicate if no default + * value is provided. + * + * @return the default value + */ + @NonNull + String defaultValue() default ModelUtil.NULL_VALUE; + + /** + * Specifies if the XML Schema attribute is optional or required. If true, then + * the JavaBean property is mapped to a XML Schema attribute that is required. + * Otherwise it is mapped to a XML Schema attribute that is optional. + * + * @return {@code true} if the flag must occur, or {@code false} otherwise + */ + boolean required() default false; + + /** + * The Module data type adapter for the field's value. + * + * @return the data type adapter + */ + @NonNull + Class> typeAdapter() default NullJavaTypeAdapter.class; + + /** + * Get any remarks for this flag. + * + * @return a markdown string or {@code "##none"} if no remarks are provided + */ + @NonNull + String remarks() default ModelUtil.NO_STRING_VALUE; + + /** + * Get the value constraints defined for this Metaschema flag inline definition. + * + * @return the value constraints + */ + ValueConstraints valueConstraints() default @ValueConstraints; +} diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/annotations/Ignore.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/annotations/Ignore.java index 9ae50c923..f8cc50121 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/annotations/Ignore.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/annotations/Ignore.java @@ -1,42 +1,42 @@ -/* - * Portions of this software was developed by employees of the National Institute - * of Standards and Technology (NIST), an agency of the Federal Government and is - * being made available as a public service. Pursuant to title 17 United States - * Code Section 105, works of NIST employees are not subject to copyright - * protection in the United States. This software may be subject to foreign - * copyright. Permission in the United States and in foreign countries, to the - * extent that NIST may hold copyright, to use, copy, modify, create derivative - * works, and distribute this software and its documentation without fee is hereby - * granted on a non-exclusive basis, provided that this notice and disclaimer - * of warranty appears in all copies. - * - * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER - * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY - * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM - * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE - * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT - * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, - * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, - * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, - * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR - * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT - * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. - */ - -package gov.nist.secauto.metaschema.databind.model.annotations; - -import static java.lang.annotation.ElementType.FIELD; -import static java.lang.annotation.RetentionPolicy.RUNTIME; - -import java.lang.annotation.Retention; -import java.lang.annotation.Target; - -/** - * Indicates that the property is to be ignored by the Module binding system. - */ -@Retention(RUNTIME) -@Target({ FIELD }) -public @interface Ignore { - // no fields -} +/* + * Portions of this software was developed by employees of the National Institute + * of Standards and Technology (NIST), an agency of the Federal Government and is + * being made available as a public service. Pursuant to title 17 United States + * Code Section 105, works of NIST employees are not subject to copyright + * protection in the United States. This software may be subject to foreign + * copyright. Permission in the United States and in foreign countries, to the + * extent that NIST may hold copyright, to use, copy, modify, create derivative + * works, and distribute this software and its documentation without fee is hereby + * granted on a non-exclusive basis, provided that this notice and disclaimer + * of warranty appears in all copies. + * + * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER + * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY + * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM + * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE + * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT + * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, + * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, + * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, + * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR + * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT + * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. + */ + +package gov.nist.secauto.metaschema.databind.model.annotations; + +import static java.lang.annotation.ElementType.FIELD; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +/** + * Indicates that the property is to be ignored by the Module binding system. + */ +@Retention(RUNTIME) +@Target({ FIELD }) +public @interface Ignore { + // no fields +} diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/annotations/JsonFieldValueKeyFlag.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/annotations/JsonFieldValueKeyFlag.java index 3405c1bde..aee6e7118 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/annotations/JsonFieldValueKeyFlag.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/annotations/JsonFieldValueKeyFlag.java @@ -1,50 +1,50 @@ -/* - * Portions of this software was developed by employees of the National Institute - * of Standards and Technology (NIST), an agency of the Federal Government and is - * being made available as a public service. Pursuant to title 17 United States - * Code Section 105, works of NIST employees are not subject to copyright - * protection in the United States. This software may be subject to foreign - * copyright. Permission in the United States and in foreign countries, to the - * extent that NIST may hold copyright, to use, copy, modify, create derivative - * works, and distribute this software and its documentation without fee is hereby - * granted on a non-exclusive basis, provided that this notice and disclaimer - * of warranty appears in all copies. - * - * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER - * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY - * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM - * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE - * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT - * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, - * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, - * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, - * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR - * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT - * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. - */ - -package gov.nist.secauto.metaschema.databind.model.annotations; - -import static java.lang.annotation.ElementType.FIELD; -import static java.lang.annotation.ElementType.METHOD; -import static java.lang.annotation.RetentionPolicy.RUNTIME; - -import java.lang.annotation.Documented; -import java.lang.annotation.Retention; -import java.lang.annotation.Target; - -/** - * Indicates that the target of this annotation is a flag whose value should be - * the property/item name of the field's value in JSON or YAML. - *

- * Use of this annotation is mutually exclusive with the {@link BoundFieldValue} - * annotation. - */ -// TODO: remove this and move this to MetaschemaField -@Documented -@Retention(RUNTIME) -@Target({ FIELD, METHOD }) -public @interface JsonFieldValueKeyFlag { - // no fields -} +/* + * Portions of this software was developed by employees of the National Institute + * of Standards and Technology (NIST), an agency of the Federal Government and is + * being made available as a public service. Pursuant to title 17 United States + * Code Section 105, works of NIST employees are not subject to copyright + * protection in the United States. This software may be subject to foreign + * copyright. Permission in the United States and in foreign countries, to the + * extent that NIST may hold copyright, to use, copy, modify, create derivative + * works, and distribute this software and its documentation without fee is hereby + * granted on a non-exclusive basis, provided that this notice and disclaimer + * of warranty appears in all copies. + * + * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER + * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY + * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM + * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE + * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT + * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, + * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, + * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, + * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR + * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT + * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. + */ + +package gov.nist.secauto.metaschema.databind.model.annotations; + +import static java.lang.annotation.ElementType.FIELD; +import static java.lang.annotation.ElementType.METHOD; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +import java.lang.annotation.Documented; +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +/** + * Indicates that the target of this annotation is a flag whose value should be + * the property/item name of the field's value in JSON or YAML. + *

+ * Use of this annotation is mutually exclusive with the {@link BoundFieldValue} + * annotation. + */ +// TODO: remove this and move this to MetaschemaField +@Documented +@Retention(RUNTIME) +@Target({ FIELD, METHOD }) +public @interface JsonFieldValueKeyFlag { + // no fields +} diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/annotations/JsonKey.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/annotations/JsonKey.java index 13989fd4d..1549f389c 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/annotations/JsonKey.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/annotations/JsonKey.java @@ -1,48 +1,48 @@ -/* - * Portions of this software was developed by employees of the National Institute - * of Standards and Technology (NIST), an agency of the Federal Government and is - * being made available as a public service. Pursuant to title 17 United States - * Code Section 105, works of NIST employees are not subject to copyright - * protection in the United States. This software may be subject to foreign - * copyright. Permission in the United States and in foreign countries, to the - * extent that NIST may hold copyright, to use, copy, modify, create derivative - * works, and distribute this software and its documentation without fee is hereby - * granted on a non-exclusive basis, provided that this notice and disclaimer - * of warranty appears in all copies. - * - * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER - * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY - * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM - * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE - * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT - * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, - * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, - * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, - * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR - * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT - * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. - */ - -package gov.nist.secauto.metaschema.databind.model.annotations; - -import static java.lang.annotation.ElementType.FIELD; -import static java.lang.annotation.ElementType.METHOD; -import static java.lang.annotation.RetentionPolicy.RUNTIME; - -import java.lang.annotation.Documented; -import java.lang.annotation.Retention; -import java.lang.annotation.Target; - -/** - * Indicates that the target of this annotation is a Java property that is to be - * used as the JSON or YAML property/item name in for a collection of similar - * objects/items. - */ -// TODO: remove this and move this to MetaschemaField/MetaschemaAssembly -@Documented -@Retention(RUNTIME) -@Target({ FIELD, METHOD }) -public @interface JsonKey { - // no fields -} +/* + * Portions of this software was developed by employees of the National Institute + * of Standards and Technology (NIST), an agency of the Federal Government and is + * being made available as a public service. Pursuant to title 17 United States + * Code Section 105, works of NIST employees are not subject to copyright + * protection in the United States. This software may be subject to foreign + * copyright. Permission in the United States and in foreign countries, to the + * extent that NIST may hold copyright, to use, copy, modify, create derivative + * works, and distribute this software and its documentation without fee is hereby + * granted on a non-exclusive basis, provided that this notice and disclaimer + * of warranty appears in all copies. + * + * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER + * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY + * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM + * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE + * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT + * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, + * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, + * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, + * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR + * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT + * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. + */ + +package gov.nist.secauto.metaschema.databind.model.annotations; + +import static java.lang.annotation.ElementType.FIELD; +import static java.lang.annotation.ElementType.METHOD; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +import java.lang.annotation.Documented; +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +/** + * Indicates that the target of this annotation is a Java property that is to be + * used as the JSON or YAML property/item name in for a collection of similar + * objects/items. + */ +// TODO: remove this and move this to MetaschemaField/MetaschemaAssembly +@Documented +@Retention(RUNTIME) +@Target({ FIELD, METHOD }) +public @interface JsonKey { + // no fields +} diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/annotations/MetaschemaAssembly.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/annotations/MetaschemaAssembly.java index c1b14be32..848e9cdad 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/annotations/MetaschemaAssembly.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/annotations/MetaschemaAssembly.java @@ -1,146 +1,146 @@ -/* - * Portions of this software was developed by employees of the National Institute - * of Standards and Technology (NIST), an agency of the Federal Government and is - * being made available as a public service. Pursuant to title 17 United States - * Code Section 105, works of NIST employees are not subject to copyright - * protection in the United States. This software may be subject to foreign - * copyright. Permission in the United States and in foreign countries, to the - * extent that NIST may hold copyright, to use, copy, modify, create derivative - * works, and distribute this software and its documentation without fee is hereby - * granted on a non-exclusive basis, provided that this notice and disclaimer - * of warranty appears in all copies. - * - * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER - * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY - * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM - * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE - * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT - * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, - * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, - * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, - * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR - * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT - * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. - */ - -package gov.nist.secauto.metaschema.databind.model.annotations; - -import static java.lang.annotation.ElementType.TYPE; -import static java.lang.annotation.RetentionPolicy.RUNTIME; - -import gov.nist.secauto.metaschema.databind.model.IBoundModule; - -import java.lang.annotation.Documented; -import java.lang.annotation.Retention; -import java.lang.annotation.Target; - -import edu.umd.cs.findbugs.annotations.NonNull; - -/** - * This annotation indicates that the target class represents a Module assembly. - */ -@Documented -@Retention(RUNTIME) -@Target(TYPE) -public @interface MetaschemaAssembly { - /** - * Get the documentary formal name of the assembly. - *

- * If the value is "##none", then the description will be considered - * {@code null}. - * - * @return a Markdown string or {@code "##none"} if no formal name is provided - */ - @NonNull - String formalName() default ModelUtil.NO_STRING_VALUE; - - /** - * Get the documentary description of the assembly. - *

- * If the value is "##none", then the description will be considered - * {@code null}. - * - * @return a markdown string or {@code "##none"} if no description is provided - */ - @NonNull - String description() default ModelUtil.NO_STRING_VALUE; - - /** - * Get the Metaschema module class that "owns" this assembly, which is the - * concrete implementation of the module containing the assembly. - * - * @return the {@link IBoundModule} class - */ - @NonNull - Class moduleClass(); - - /** - * Name of the assembly. - * - * @return the name - */ - @NonNull - String name(); - - /** - * The binary name of the assembly. - *

- * The value {@link Integer#MIN_VALUE} indicates that there is no index. - * - * @return the index value - */ - int index() default Integer.MIN_VALUE; - - /** - * Name of the root XML element or the JSON/YAML property. - *

- * If the value is "##none", then there is no root name. - * - * @return the name - */ - @NonNull - String rootName() default ModelUtil.NO_STRING_VALUE; - - /** - * The binary root name of the assembly. - *

- * The value {@link Integer#MIN_VALUE} indicates that there is no root index. - * - * @return the index value - */ - int rootIndex() default Integer.MIN_VALUE; - - /** - * XML target namespace of the XML element. - *

- * If the value is "##default", then namespace is derived from the namespace - * provided in the package-info. - * - * @return the namespace - */ - @NonNull - String rootNamespace() default ModelUtil.DEFAULT_STRING_VALUE; - - /** - * Get any remarks for this assembly. - * - * @return a markdown string or {@code "##none"} if no remarks are provided - */ - @NonNull - String remarks() default ModelUtil.NO_STRING_VALUE; - - /** - * Get the value constraints defined for this Metaschema assembly definition. - * - * @return the value constraints - */ - ValueConstraints valueConstraints() default @ValueConstraints; - - /** - * Get the model constraints defined for this Metaschema assembly definition. - * - * @return the value constraints - */ - AssemblyConstraints modelConstraints() default @AssemblyConstraints; -} +/* + * Portions of this software was developed by employees of the National Institute + * of Standards and Technology (NIST), an agency of the Federal Government and is + * being made available as a public service. Pursuant to title 17 United States + * Code Section 105, works of NIST employees are not subject to copyright + * protection in the United States. This software may be subject to foreign + * copyright. Permission in the United States and in foreign countries, to the + * extent that NIST may hold copyright, to use, copy, modify, create derivative + * works, and distribute this software and its documentation without fee is hereby + * granted on a non-exclusive basis, provided that this notice and disclaimer + * of warranty appears in all copies. + * + * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER + * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY + * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM + * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE + * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT + * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, + * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, + * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, + * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR + * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT + * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. + */ + +package gov.nist.secauto.metaschema.databind.model.annotations; + +import static java.lang.annotation.ElementType.TYPE; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +import gov.nist.secauto.metaschema.databind.model.IBoundModule; + +import java.lang.annotation.Documented; +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +import edu.umd.cs.findbugs.annotations.NonNull; + +/** + * This annotation indicates that the target class represents a Module assembly. + */ +@Documented +@Retention(RUNTIME) +@Target(TYPE) +public @interface MetaschemaAssembly { + /** + * Get the documentary formal name of the assembly. + *

+ * If the value is "##none", then the description will be considered + * {@code null}. + * + * @return a Markdown string or {@code "##none"} if no formal name is provided + */ + @NonNull + String formalName() default ModelUtil.NO_STRING_VALUE; + + /** + * Get the documentary description of the assembly. + *

+ * If the value is "##none", then the description will be considered + * {@code null}. + * + * @return a markdown string or {@code "##none"} if no description is provided + */ + @NonNull + String description() default ModelUtil.NO_STRING_VALUE; + + /** + * Get the Metaschema module class that "owns" this assembly, which is the + * concrete implementation of the module containing the assembly. + * + * @return the {@link IBoundModule} class + */ + @NonNull + Class moduleClass(); + + /** + * Name of the assembly. + * + * @return the name + */ + @NonNull + String name(); + + /** + * The binary name of the assembly. + *

+ * The value {@link Integer#MIN_VALUE} indicates that there is no index. + * + * @return the index value + */ + int index() default Integer.MIN_VALUE; + + /** + * Name of the root XML element or the JSON/YAML property. + *

+ * If the value is "##none", then there is no root name. + * + * @return the name + */ + @NonNull + String rootName() default ModelUtil.NO_STRING_VALUE; + + /** + * The binary root name of the assembly. + *

+ * The value {@link Integer#MIN_VALUE} indicates that there is no root index. + * + * @return the index value + */ + int rootIndex() default Integer.MIN_VALUE; + + /** + * XML target namespace of the XML element. + *

+ * If the value is "##default", then namespace is derived from the namespace + * provided in the package-info. + * + * @return the namespace + */ + @NonNull + String rootNamespace() default ModelUtil.DEFAULT_STRING_VALUE; + + /** + * Get any remarks for this assembly. + * + * @return a markdown string or {@code "##none"} if no remarks are provided + */ + @NonNull + String remarks() default ModelUtil.NO_STRING_VALUE; + + /** + * Get the value constraints defined for this Metaschema assembly definition. + * + * @return the value constraints + */ + ValueConstraints valueConstraints() default @ValueConstraints; + + /** + * Get the model constraints defined for this Metaschema assembly definition. + * + * @return the value constraints + */ + AssemblyConstraints modelConstraints() default @AssemblyConstraints; +} diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/annotations/XmlNs.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/annotations/XmlNs.java index adb7675f2..ce72f12db 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/annotations/XmlNs.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/annotations/XmlNs.java @@ -1,57 +1,57 @@ -/* - * Portions of this software was developed by employees of the National Institute - * of Standards and Technology (NIST), an agency of the Federal Government and is - * being made available as a public service. Pursuant to title 17 United States - * Code Section 105, works of NIST employees are not subject to copyright - * protection in the United States. This software may be subject to foreign - * copyright. Permission in the United States and in foreign countries, to the - * extent that NIST may hold copyright, to use, copy, modify, create derivative - * works, and distribute this software and its documentation without fee is hereby - * granted on a non-exclusive basis, provided that this notice and disclaimer - * of warranty appears in all copies. - * - * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER - * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY - * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM - * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE - * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT - * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, - * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, - * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, - * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR - * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT - * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. - */ - -package gov.nist.secauto.metaschema.databind.model.annotations; - -import static java.lang.annotation.ElementType.ANNOTATION_TYPE; -import static java.lang.annotation.RetentionPolicy.RUNTIME; - -import java.lang.annotation.Retention; -import java.lang.annotation.Target; - -/** - * Used to identify the XML namespace to use for a set of annotated Java - * classes. - */ -@Retention(RUNTIME) -@Target(ANNOTATION_TYPE) -public @interface XmlNs { - /** - * Suggests a namespace prefix to use for generated code. - *

- * If the value is "##none", then there is no prefix defined. - * - * @return the associated namespace prefix - */ - String prefix() default "##none"; - - /** - * Defines the Namespace URI for this namespace. - * - * @return the associated namespace - */ - String namespace(); -} +/* + * Portions of this software was developed by employees of the National Institute + * of Standards and Technology (NIST), an agency of the Federal Government and is + * being made available as a public service. Pursuant to title 17 United States + * Code Section 105, works of NIST employees are not subject to copyright + * protection in the United States. This software may be subject to foreign + * copyright. Permission in the United States and in foreign countries, to the + * extent that NIST may hold copyright, to use, copy, modify, create derivative + * works, and distribute this software and its documentation without fee is hereby + * granted on a non-exclusive basis, provided that this notice and disclaimer + * of warranty appears in all copies. + * + * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER + * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY + * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM + * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE + * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT + * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, + * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, + * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, + * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR + * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT + * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. + */ + +package gov.nist.secauto.metaschema.databind.model.annotations; + +import static java.lang.annotation.ElementType.ANNOTATION_TYPE; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +/** + * Used to identify the XML namespace to use for a set of annotated Java + * classes. + */ +@Retention(RUNTIME) +@Target(ANNOTATION_TYPE) +public @interface XmlNs { + /** + * Suggests a namespace prefix to use for generated code. + *

+ * If the value is "##none", then there is no prefix defined. + * + * @return the associated namespace prefix + */ + String prefix() default "##none"; + + /** + * Defines the Namespace URI for this namespace. + * + * @return the associated namespace + */ + String namespace(); +} diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/annotations/XmlNsForm.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/annotations/XmlNsForm.java index 2dd4abc70..6e72bfa4c 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/annotations/XmlNsForm.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/annotations/XmlNsForm.java @@ -1,33 +1,33 @@ -/* - * Portions of this software was developed by employees of the National Institute - * of Standards and Technology (NIST), an agency of the Federal Government and is - * being made available as a public service. Pursuant to title 17 United States - * Code Section 105, works of NIST employees are not subject to copyright - * protection in the United States. This software may be subject to foreign - * copyright. Permission in the United States and in foreign countries, to the - * extent that NIST may hold copyright, to use, copy, modify, create derivative - * works, and distribute this software and its documentation without fee is hereby - * granted on a non-exclusive basis, provided that this notice and disclaimer - * of warranty appears in all copies. - * - * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER - * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY - * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM - * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE - * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT - * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, - * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, - * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, - * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR - * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT - * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. - */ - -package gov.nist.secauto.metaschema.databind.model.annotations; - -public enum XmlNsForm { - UNQUALIFIED, - QUALIFIED, - UNSET; -} +/* + * Portions of this software was developed by employees of the National Institute + * of Standards and Technology (NIST), an agency of the Federal Government and is + * being made available as a public service. Pursuant to title 17 United States + * Code Section 105, works of NIST employees are not subject to copyright + * protection in the United States. This software may be subject to foreign + * copyright. Permission in the United States and in foreign countries, to the + * extent that NIST may hold copyright, to use, copy, modify, create derivative + * works, and distribute this software and its documentation without fee is hereby + * granted on a non-exclusive basis, provided that this notice and disclaimer + * of warranty appears in all copies. + * + * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER + * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY + * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM + * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE + * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT + * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, + * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, + * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, + * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR + * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT + * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. + */ + +package gov.nist.secauto.metaschema.databind.model.annotations; + +public enum XmlNsForm { + UNQUALIFIED, + QUALIFIED, + UNSET; +} diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/annotations/XmlSchema.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/annotations/XmlSchema.java index 5b8234bb9..415c58c9b 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/annotations/XmlSchema.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/annotations/XmlSchema.java @@ -1,94 +1,94 @@ -/* - * Portions of this software was developed by employees of the National Institute - * of Standards and Technology (NIST), an agency of the Federal Government and is - * being made available as a public service. Pursuant to title 17 United States - * Code Section 105, works of NIST employees are not subject to copyright - * protection in the United States. This software may be subject to foreign - * copyright. Permission in the United States and in foreign countries, to the - * extent that NIST may hold copyright, to use, copy, modify, create derivative - * works, and distribute this software and its documentation without fee is hereby - * granted on a non-exclusive basis, provided that this notice and disclaimer - * of warranty appears in all copies. - * - * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER - * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY - * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM - * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE - * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT - * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, - * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, - * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, - * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR - * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT - * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. - */ - -package gov.nist.secauto.metaschema.databind.model.annotations; - -import static java.lang.annotation.ElementType.PACKAGE; -import static java.lang.annotation.RetentionPolicy.RUNTIME; - -import java.lang.annotation.Retention; -import java.lang.annotation.Target; - -/** - * This annotation provides package-level Module information. - */ -@Retention(RUNTIME) -@Target(PACKAGE) -public @interface XmlSchema { - /** - * The default value of a schema location, which indicates that no schema will - * be associated. - *

- * The value "##none" was chosen because ## is not a valid sequence in - * xs:anyURI. - */ - String NO_LOCATION = ModelUtil.NO_STRING_VALUE; - - /** - * Defines the XML namespace URI and prefix to use for this model. If a prefix - * is not provided, the XML prefix will be auto-generated. - * - * @return an array of namespace definitions - */ - XmlNs[] xmlns() default {}; - - /** - * Name of the XML namespace. - *

- * If the value is "##none", then there is no prefix defined. - * - * @return a namespace string in the form of a URI - */ - String namespace() default ModelUtil.NO_STRING_VALUE; - - /** - * The location of the associated XML schema. - * - * @return a location string in the form of a URI - */ - String xmlSchemaLocation() default NO_LOCATION; - - /** - * The location of the associated JSON schema. - * - * @return a location string in the form of a URI - */ - String jsonSchemaLocation() default NO_LOCATION; - - /** - * Get the default XML element form. - * - * @return the XML element form - */ - XmlNsForm xmlElementFormDefault() default XmlNsForm.UNSET; - - /** - * Get the default XML attribute form. - * - * @return the XML attribute form - */ - XmlNsForm xmlAttributeFormDefault() default XmlNsForm.UNSET; -} +/* + * Portions of this software was developed by employees of the National Institute + * of Standards and Technology (NIST), an agency of the Federal Government and is + * being made available as a public service. Pursuant to title 17 United States + * Code Section 105, works of NIST employees are not subject to copyright + * protection in the United States. This software may be subject to foreign + * copyright. Permission in the United States and in foreign countries, to the + * extent that NIST may hold copyright, to use, copy, modify, create derivative + * works, and distribute this software and its documentation without fee is hereby + * granted on a non-exclusive basis, provided that this notice and disclaimer + * of warranty appears in all copies. + * + * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER + * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY + * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM + * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE + * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT + * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, + * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, + * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, + * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR + * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT + * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. + */ + +package gov.nist.secauto.metaschema.databind.model.annotations; + +import static java.lang.annotation.ElementType.PACKAGE; +import static java.lang.annotation.RetentionPolicy.RUNTIME; + +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +/** + * This annotation provides package-level Module information. + */ +@Retention(RUNTIME) +@Target(PACKAGE) +public @interface XmlSchema { + /** + * The default value of a schema location, which indicates that no schema will + * be associated. + *

+ * The value "##none" was chosen because ## is not a valid sequence in + * xs:anyURI. + */ + String NO_LOCATION = ModelUtil.NO_STRING_VALUE; + + /** + * Defines the XML namespace URI and prefix to use for this model. If a prefix + * is not provided, the XML prefix will be auto-generated. + * + * @return an array of namespace definitions + */ + XmlNs[] xmlns() default {}; + + /** + * Name of the XML namespace. + *

+ * If the value is "##none", then there is no prefix defined. + * + * @return a namespace string in the form of a URI + */ + String namespace() default ModelUtil.NO_STRING_VALUE; + + /** + * The location of the associated XML schema. + * + * @return a location string in the form of a URI + */ + String xmlSchemaLocation() default NO_LOCATION; + + /** + * The location of the associated JSON schema. + * + * @return a location string in the form of a URI + */ + String jsonSchemaLocation() default NO_LOCATION; + + /** + * Get the default XML element form. + * + * @return the XML element form + */ + XmlNsForm xmlElementFormDefault() default XmlNsForm.UNSET; + + /** + * Get the default XML attribute form. + * + * @return the XML attribute form + */ + XmlNsForm xmlAttributeFormDefault() default XmlNsForm.UNSET; +} diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/ClassIntrospector.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/ClassIntrospector.java index 5269fdb73..ff7204587 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/ClassIntrospector.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/ClassIntrospector.java @@ -1,71 +1,71 @@ -/* - * Portions of this software was developed by employees of the National Institute - * of Standards and Technology (NIST), an agency of the Federal Government and is - * being made available as a public service. Pursuant to title 17 United States - * Code Section 105, works of NIST employees are not subject to copyright - * protection in the United States. This software may be subject to foreign - * copyright. Permission in the United States and in foreign countries, to the - * extent that NIST may hold copyright, to use, copy, modify, create derivative - * works, and distribute this software and its documentation without fee is hereby - * granted on a non-exclusive basis, provided that this notice and disclaimer - * of warranty appears in all copies. - * - * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER - * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY - * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM - * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE - * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT - * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, - * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, - * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, - * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR - * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT - * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. - */ - -package gov.nist.secauto.metaschema.databind.model.impl; - -import java.lang.reflect.Method; -import java.util.Collections; -import java.util.LinkedList; -import java.util.List; - -public final class ClassIntrospector { - private ClassIntrospector() { - // disable construction - } - - @SuppressWarnings("PMD.EmptyCatchBlock") - public static List getMatchingMethods(Class clazz, String name, Class... parameterTypes) { - List retval = new LinkedList<>(); - Class searchClass = clazz; - do { - try { - Method method = searchClass.getDeclaredMethod(name, parameterTypes); - retval.add(method); - } catch (NoSuchMethodException ex) { - // do nothing, no matching method was found - } - } while ((searchClass = searchClass.getSuperclass()) != null); - - return retval.isEmpty() ? Collections.emptyList() : Collections.unmodifiableList(retval); - } - - @SuppressWarnings("PMD.EmptyCatchBlock") - public static Method getMatchingMethod(Class clazz, String name, Class... parameterTypes) { - Method retval = null; - Class searchClass = clazz; - do { - try { - retval = searchClass.getDeclaredMethod(name, parameterTypes); - // stop on first found method - break; - } catch (NoSuchMethodException ex) { - // do nothing, no matching method was found - } - } while ((searchClass = searchClass.getSuperclass()) != null); - - return retval; - } -} +/* + * Portions of this software was developed by employees of the National Institute + * of Standards and Technology (NIST), an agency of the Federal Government and is + * being made available as a public service. Pursuant to title 17 United States + * Code Section 105, works of NIST employees are not subject to copyright + * protection in the United States. This software may be subject to foreign + * copyright. Permission in the United States and in foreign countries, to the + * extent that NIST may hold copyright, to use, copy, modify, create derivative + * works, and distribute this software and its documentation without fee is hereby + * granted on a non-exclusive basis, provided that this notice and disclaimer + * of warranty appears in all copies. + * + * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER + * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY + * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM + * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE + * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT + * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, + * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, + * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, + * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR + * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT + * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. + */ + +package gov.nist.secauto.metaschema.databind.model.impl; + +import java.lang.reflect.Method; +import java.util.Collections; +import java.util.LinkedList; +import java.util.List; + +public final class ClassIntrospector { + private ClassIntrospector() { + // disable construction + } + + @SuppressWarnings("PMD.EmptyCatchBlock") + public static List getMatchingMethods(Class clazz, String name, Class... parameterTypes) { + List retval = new LinkedList<>(); + Class searchClass = clazz; + do { + try { + Method method = searchClass.getDeclaredMethod(name, parameterTypes); + retval.add(method); + } catch (NoSuchMethodException ex) { + // do nothing, no matching method was found + } + } while ((searchClass = searchClass.getSuperclass()) != null); + + return retval.isEmpty() ? Collections.emptyList() : Collections.unmodifiableList(retval); + } + + @SuppressWarnings("PMD.EmptyCatchBlock") + public static Method getMatchingMethod(Class clazz, String name, Class... parameterTypes) { + Method retval = null; + Class searchClass = clazz; + do { + try { + retval = searchClass.getDeclaredMethod(name, parameterTypes); + // stop on first found method + break; + } catch (NoSuchMethodException ex) { + // do nothing, no matching method was found + } + } while ((searchClass = searchClass.getSuperclass()) != null); + + return retval; + } +} diff --git a/databind/src/test/java/gov/nist/secauto/metaschema/databind/codegen/BasicMetaschemaTest.java b/databind/src/test/java/gov/nist/secauto/metaschema/databind/codegen/BasicMetaschemaTest.java index a3fa4f16d..9254cd6bd 100644 --- a/databind/src/test/java/gov/nist/secauto/metaschema/databind/codegen/BasicMetaschemaTest.java +++ b/databind/src/test/java/gov/nist/secauto/metaschema/databind/codegen/BasicMetaschemaTest.java @@ -1,185 +1,185 @@ -/* - * Portions of this software was developed by employees of the National Institute - * of Standards and Technology (NIST), an agency of the Federal Government and is - * being made available as a public service. Pursuant to title 17 United States - * Code Section 105, works of NIST employees are not subject to copyright - * protection in the United States. This software may be subject to foreign - * copyright. Permission in the United States and in foreign countries, to the - * extent that NIST may hold copyright, to use, copy, modify, create derivative - * works, and distribute this software and its documentation without fee is hereby - * granted on a non-exclusive basis, provided that this notice and disclaimer - * of warranty appears in all copies. - * - * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER - * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY - * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM - * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE - * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT - * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, - * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, - * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, - * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR - * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT - * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. - */ - -package gov.nist.secauto.metaschema.databind.codegen; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.fail; - -import gov.nist.secauto.metaschema.core.model.MetaschemaException; -import gov.nist.secauto.metaschema.core.util.ObjectUtils; -import gov.nist.secauto.metaschema.databind.io.BindingException; - -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.Test; -import org.junit.platform.commons.util.ReflectionUtils; - -import java.io.IOException; -import java.util.Iterator; -import java.util.List; -import java.util.Map; -import java.util.Set; - -class BasicMetaschemaTest - extends AbstractMetaschemaTest { - - @Test - void testSimpleMetaschema() throws MetaschemaException, IOException, ClassNotFoundException, BindingException { - runTests("simple", "gov.nist.csrc.ns.metaschema.testing.simple.TopLevel", ObjectUtils.notNull(generationDir)); - // runTests("simple", "gov.nist.csrc.ns.metaschema.testing.simple.TopLevel", - // generationDir, (obj) -> - // { - // try { - // Assertions.assertEquals("test", reflectMethod(obj, "getId")); - // } catch (NoSuchMethodException | SecurityException e) { - // Assertions.fail(e); - // } - // }); - } - - @Test - void testSimpleUuidMetaschema() - throws MetaschemaException, IOException, ClassNotFoundException, BindingException { - runTests( - "simple_with_uuid", - "gov.nist.csrc.ns.metaschema.testing.simple.with.uuid.TopLevel", - ObjectUtils.notNull(generationDir), - (obj) -> { - try { - assertEquals("5de455cf-2f8d-4da2-9182-323d433e1065", reflectMethod(obj, "getUuid").toString()); - } catch (NoSuchMethodException | SecurityException e) { - fail(e); - } - }); - } - - @Test - void testSimpleWithFieldMetaschema() - throws MetaschemaException, IOException, ClassNotFoundException, BindingException { - runTests( - "simple_with_field", - "gov.nist.csrc.ns.metaschema.testing.simple.with.field.TopLevel", - ObjectUtils.notNull(generationDir)); - } - - private static Object reflectMethod(Object obj, String name) throws NoSuchMethodException { - return ReflectionUtils.invokeMethod(obj.getClass().getMethod(name), obj); - } - - @Test - void testFieldsWithFlagMetaschema() - throws MetaschemaException, IOException, ClassNotFoundException, BindingException { - runTests( - "fields_with_flags", - "gov.nist.csrc.ns.metaschema.testing.fields.with.flags.TopLevel", - ObjectUtils.notNull(generationDir), - (obj) -> { - try { - Assertions.assertEquals("test", reflectMethod(obj, "getId")); - Object field1 = ReflectionUtils.invokeMethod(obj.getClass().getMethod("getComplexField1"), obj); - Assertions.assertNotNull(field1); - Assertions.assertEquals("complex-field1", reflectMethod(field1, "getId")); - Assertions.assertEquals("test-string", reflectMethod(field1, "getValue")); - - @SuppressWarnings("unchecked") List field2s - = (List) ReflectionUtils.invokeMethod(obj.getClass().getMethod("getComplexFields2"), - obj); - Assertions.assertNotNull(field2s); - Assertions.assertEquals(1, field2s.size()); - Object field2 = field2s.get(0); - Assertions.assertEquals("complex-field2-1", reflectMethod(field2, "getId")); - Assertions.assertEquals("test-string2", reflectMethod(field2, "getValue")); - - @SuppressWarnings("unchecked") List field3s - = (List) ReflectionUtils.invokeMethod(obj.getClass().getMethod("getComplexFields3"), - obj); - Assertions.assertEquals(2, field3s.size()); - Assertions.assertAll("ComplexFields4 item", () -> { - Object item = field3s.get(0); - assertEquals("complex-field3-1", reflectMethod(item, "getId2")); - assertEquals("test-string3", reflectMethod(item, "getValue")); - }); - Assertions.assertAll("ComplexFields4 item", () -> { - Object item = field3s.get(1); - assertEquals("complex-field3-2", reflectMethod(item, "getId2")); - assertEquals("test-string4", reflectMethod(item, "getValue")); - }); - - Assertions.assertAll("ComplexFields4", () -> { - @SuppressWarnings("unchecked") Map collection - = (Map) ReflectionUtils.invokeMethod(obj.getClass().getMethod("getComplexFields4"), - obj); - Assertions.assertNotNull(collection); - Assertions.assertEquals(2, collection.size()); - Set> entries = collection.entrySet(); - Iterator> iter = entries.iterator(); - - Assertions.assertAll("ComplexFields4 item", () -> { - Map.Entry entry = iter.next(); - assertEquals("complex-field4-1", entry.getKey()); - assertEquals("complex-field4-1", reflectMethod(entry.getValue(), "getId2")); - assertEquals("test-string5", reflectMethod(entry.getValue(), "getValue")); - }); - - Assertions.assertAll("ComplexFields4 item", () -> { - Map.Entry entry = iter.next(); - assertEquals("complex-field4-2", entry.getKey()); - assertEquals("complex-field4-2", reflectMethod(entry.getValue(), "getId2")); - assertEquals("test-string6", reflectMethod(entry.getValue(), "getValue")); - }); - }); - } catch (NoSuchMethodException | SecurityException e) { - Assertions.fail(e); - } - }); - } - - @Test - void testAssemblyMetaschema() - throws MetaschemaException, IOException, ClassNotFoundException, BindingException { - runTests( - "assembly", - "gov.nist.itl.metaschema.codegen.xml.example.assembly.TopLevel", - ObjectUtils.notNull(generationDir), - (obj) -> { - try { - Assertions.assertEquals("test", reflectMethod(obj, "getId")); - } catch (NoSuchMethodException | SecurityException e) { - Assertions.fail(e); - } - }); - } - - @Test - void testLocalDefinitionsMetaschema() - throws MetaschemaException, IOException, ClassNotFoundException, BindingException { - runTests( - "local-definitions", - "gov.nist.csrc.ns.metaschema.testing.local.definitions.TopLevel", - ObjectUtils.notNull(generationDir)); - } - -} +/* + * Portions of this software was developed by employees of the National Institute + * of Standards and Technology (NIST), an agency of the Federal Government and is + * being made available as a public service. Pursuant to title 17 United States + * Code Section 105, works of NIST employees are not subject to copyright + * protection in the United States. This software may be subject to foreign + * copyright. Permission in the United States and in foreign countries, to the + * extent that NIST may hold copyright, to use, copy, modify, create derivative + * works, and distribute this software and its documentation without fee is hereby + * granted on a non-exclusive basis, provided that this notice and disclaimer + * of warranty appears in all copies. + * + * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER + * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY + * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM + * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE + * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT + * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, + * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, + * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, + * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR + * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT + * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. + */ + +package gov.nist.secauto.metaschema.databind.codegen; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.fail; + +import gov.nist.secauto.metaschema.core.model.MetaschemaException; +import gov.nist.secauto.metaschema.core.util.ObjectUtils; +import gov.nist.secauto.metaschema.databind.io.BindingException; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.junit.platform.commons.util.ReflectionUtils; + +import java.io.IOException; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Set; + +class BasicMetaschemaTest + extends AbstractMetaschemaTest { + + @Test + void testSimpleMetaschema() throws MetaschemaException, IOException, ClassNotFoundException, BindingException { + runTests("simple", "gov.nist.csrc.ns.metaschema.testing.simple.TopLevel", ObjectUtils.notNull(generationDir)); + // runTests("simple", "gov.nist.csrc.ns.metaschema.testing.simple.TopLevel", + // generationDir, (obj) -> + // { + // try { + // Assertions.assertEquals("test", reflectMethod(obj, "getId")); + // } catch (NoSuchMethodException | SecurityException e) { + // Assertions.fail(e); + // } + // }); + } + + @Test + void testSimpleUuidMetaschema() + throws MetaschemaException, IOException, ClassNotFoundException, BindingException { + runTests( + "simple_with_uuid", + "gov.nist.csrc.ns.metaschema.testing.simple.with.uuid.TopLevel", + ObjectUtils.notNull(generationDir), + (obj) -> { + try { + assertEquals("5de455cf-2f8d-4da2-9182-323d433e1065", reflectMethod(obj, "getUuid").toString()); + } catch (NoSuchMethodException | SecurityException e) { + fail(e); + } + }); + } + + @Test + void testSimpleWithFieldMetaschema() + throws MetaschemaException, IOException, ClassNotFoundException, BindingException { + runTests( + "simple_with_field", + "gov.nist.csrc.ns.metaschema.testing.simple.with.field.TopLevel", + ObjectUtils.notNull(generationDir)); + } + + private static Object reflectMethod(Object obj, String name) throws NoSuchMethodException { + return ReflectionUtils.invokeMethod(obj.getClass().getMethod(name), obj); + } + + @Test + void testFieldsWithFlagMetaschema() + throws MetaschemaException, IOException, ClassNotFoundException, BindingException { + runTests( + "fields_with_flags", + "gov.nist.csrc.ns.metaschema.testing.fields.with.flags.TopLevel", + ObjectUtils.notNull(generationDir), + (obj) -> { + try { + Assertions.assertEquals("test", reflectMethod(obj, "getId")); + Object field1 = ReflectionUtils.invokeMethod(obj.getClass().getMethod("getComplexField1"), obj); + Assertions.assertNotNull(field1); + Assertions.assertEquals("complex-field1", reflectMethod(field1, "getId")); + Assertions.assertEquals("test-string", reflectMethod(field1, "getValue")); + + @SuppressWarnings("unchecked") List field2s + = (List) ReflectionUtils.invokeMethod(obj.getClass().getMethod("getComplexFields2"), + obj); + Assertions.assertNotNull(field2s); + Assertions.assertEquals(1, field2s.size()); + Object field2 = field2s.get(0); + Assertions.assertEquals("complex-field2-1", reflectMethod(field2, "getId")); + Assertions.assertEquals("test-string2", reflectMethod(field2, "getValue")); + + @SuppressWarnings("unchecked") List field3s + = (List) ReflectionUtils.invokeMethod(obj.getClass().getMethod("getComplexFields3"), + obj); + Assertions.assertEquals(2, field3s.size()); + Assertions.assertAll("ComplexFields4 item", () -> { + Object item = field3s.get(0); + assertEquals("complex-field3-1", reflectMethod(item, "getId2")); + assertEquals("test-string3", reflectMethod(item, "getValue")); + }); + Assertions.assertAll("ComplexFields4 item", () -> { + Object item = field3s.get(1); + assertEquals("complex-field3-2", reflectMethod(item, "getId2")); + assertEquals("test-string4", reflectMethod(item, "getValue")); + }); + + Assertions.assertAll("ComplexFields4", () -> { + @SuppressWarnings("unchecked") Map collection + = (Map) ReflectionUtils.invokeMethod(obj.getClass().getMethod("getComplexFields4"), + obj); + Assertions.assertNotNull(collection); + Assertions.assertEquals(2, collection.size()); + Set> entries = collection.entrySet(); + Iterator> iter = entries.iterator(); + + Assertions.assertAll("ComplexFields4 item", () -> { + Map.Entry entry = iter.next(); + assertEquals("complex-field4-1", entry.getKey()); + assertEquals("complex-field4-1", reflectMethod(entry.getValue(), "getId2")); + assertEquals("test-string5", reflectMethod(entry.getValue(), "getValue")); + }); + + Assertions.assertAll("ComplexFields4 item", () -> { + Map.Entry entry = iter.next(); + assertEquals("complex-field4-2", entry.getKey()); + assertEquals("complex-field4-2", reflectMethod(entry.getValue(), "getId2")); + assertEquals("test-string6", reflectMethod(entry.getValue(), "getValue")); + }); + }); + } catch (NoSuchMethodException | SecurityException e) { + Assertions.fail(e); + } + }); + } + + @Test + void testAssemblyMetaschema() + throws MetaschemaException, IOException, ClassNotFoundException, BindingException { + runTests( + "assembly", + "gov.nist.itl.metaschema.codegen.xml.example.assembly.TopLevel", + ObjectUtils.notNull(generationDir), + (obj) -> { + try { + Assertions.assertEquals("test", reflectMethod(obj, "getId")); + } catch (NoSuchMethodException | SecurityException e) { + Assertions.fail(e); + } + }); + } + + @Test + void testLocalDefinitionsMetaschema() + throws MetaschemaException, IOException, ClassNotFoundException, BindingException { + runTests( + "local-definitions", + "gov.nist.csrc.ns.metaschema.testing.local.definitions.TopLevel", + ObjectUtils.notNull(generationDir)); + } + +} diff --git a/databind/src/test/resources/gov/nist/secauto/metaschema/codegen/test/parent.json b/databind/src/test/resources/gov/nist/secauto/metaschema/codegen/test/parent.json index 8023c1d8a..ac12f4e77 100644 --- a/databind/src/test/resources/gov/nist/secauto/metaschema/codegen/test/parent.json +++ b/databind/src/test/resources/gov/nist/secauto/metaschema/codegen/test/parent.json @@ -1,9 +1,9 @@ -{ - "parent": { - "props": { - "id3": {"STRVALUE": "test3"}, - "id2": {"STRVALUE": ""}, - "id1": {"STRVALUE": "test1"} - } - } +{ + "parent": { + "props": { + "id3": {"STRVALUE": "test3"}, + "id2": {"STRVALUE": ""}, + "id1": {"STRVALUE": "test1"} + } + } } \ No newline at end of file diff --git a/databind/src/test/resources/metaschema/assembly/example.json b/databind/src/test/resources/metaschema/assembly/example.json index 6a7c32ae5..5838cbde9 100644 --- a/databind/src/test/resources/metaschema/assembly/example.json +++ b/databind/src/test/resources/metaschema/assembly/example.json @@ -1,83 +1,83 @@ -{ - "$schema": "schema.json", - "top-level": { - "id": "test", - "children": [ - { - "id": "child1", - "grandchildren": { - "id": "singleton-ungrouped", - "fields": { - "id": "field1", - "STRVALUE": "value1" - } - }, - "grandchildren2": [ - { - "id": "multiple-grouped-1", - "fields": { - "id": "field2a", - "STRVALUE": "value2a" - } - }, - { - "id": "multiple-grouped-2", - "fields": { - "id": "field2b", - "STRVALUE": "value2b" - } - } - ], - "grandchild": { - "id": "single-1", - "fields": { - "id": "field3", - "STRVALUE": "value3" - } - } - }, - { - "id": "child2", - "grandchildren": [ - { - "id": "multiple-ungrouped-1", - "fields": { - "id": "field4", - "STRVALUE": "value4" - } - }, - { - "id": "multiple-ungrouped-2", - "fields": { - "id": "field5", - "STRVALUE": "value6" - } - } - ], - "grandchildren2": [ - { - "id": "multiple-grouped-3", - "fields": { - "id": "field6", - "STRVALUE": "value6" - } - }, - { - "id": "multiple-grouped-4", - "fields": { - "id": "field7", - "STRVALUE": "value7" - } - } - ], - "grandchild": { - "id": "single-2", - "fields": { - "id": "field8", - "STRVALUE": "value8" - } - } - } - ] - } +{ + "$schema": "schema.json", + "top-level": { + "id": "test", + "children": [ + { + "id": "child1", + "grandchildren": { + "id": "singleton-ungrouped", + "fields": { + "id": "field1", + "STRVALUE": "value1" + } + }, + "grandchildren2": [ + { + "id": "multiple-grouped-1", + "fields": { + "id": "field2a", + "STRVALUE": "value2a" + } + }, + { + "id": "multiple-grouped-2", + "fields": { + "id": "field2b", + "STRVALUE": "value2b" + } + } + ], + "grandchild": { + "id": "single-1", + "fields": { + "id": "field3", + "STRVALUE": "value3" + } + } + }, + { + "id": "child2", + "grandchildren": [ + { + "id": "multiple-ungrouped-1", + "fields": { + "id": "field4", + "STRVALUE": "value4" + } + }, + { + "id": "multiple-ungrouped-2", + "fields": { + "id": "field5", + "STRVALUE": "value6" + } + } + ], + "grandchildren2": [ + { + "id": "multiple-grouped-3", + "fields": { + "id": "field6", + "STRVALUE": "value6" + } + }, + { + "id": "multiple-grouped-4", + "fields": { + "id": "field7", + "STRVALUE": "value7" + } + } + ], + "grandchild": { + "id": "single-2", + "fields": { + "id": "field8", + "STRVALUE": "value8" + } + } + } + ] + } } \ No newline at end of file diff --git a/databind/src/test/resources/metaschema/assembly/example.xml b/databind/src/test/resources/metaschema/assembly/example.xml index cb430be3b..f95c55edf 100644 --- a/databind/src/test/resources/metaschema/assembly/example.xml +++ b/databind/src/test/resources/metaschema/assembly/example.xml @@ -1,39 +1,39 @@ - - - - - value1 - - - - value2a - - - value2b - - - - value3 - - - - - - value4 - - - value5 - - - - value6 - - - value7 - - - - value8 - - - + + + + + value1 + + + + value2a + + + value2b + + + + value3 + + + + + + value4 + + + value5 + + + + value6 + + + value7 + + + + value8 + + + diff --git a/databind/src/test/resources/metaschema/assembly/metaschema.xml b/databind/src/test/resources/metaschema/assembly/metaschema.xml index 612d02633..3d6bc3255 100644 --- a/databind/src/test/resources/metaschema/assembly/metaschema.xml +++ b/databind/src/test/resources/metaschema/assembly/metaschema.xml @@ -1,69 +1,69 @@ - - - Metaschema with assembly - 1.0 - assembly - http://csrc.nist.gov/ns/metaschema/testing/assembly - http://csrc.nist.gov/ns/metaschema/testing/assembly - - - Root - Root assembly - top-level - - - - - - - - - - Child - Child assembly - - - - - - - grandchild-b - - - - grandchild-c - - - - - - Child - Child assembly - grandchild-a - - - - - - - - - - Complex Field - A complex field with a flag - - - - - Identifier - The document identifier - - + + + Metaschema with assembly + 1.0 + assembly + http://csrc.nist.gov/ns/metaschema/testing/assembly + http://csrc.nist.gov/ns/metaschema/testing/assembly + + + Root + Root assembly + top-level + + + + + + + + + + Child + Child assembly + + + + + + + grandchild-b + + + + grandchild-c + + + + + + Child + Child assembly + grandchild-a + + + + + + + + + + Complex Field + A complex field with a flag + + + + + Identifier + The document identifier + + diff --git a/databind/src/test/resources/metaschema/bad_index-has-key/example.json b/databind/src/test/resources/metaschema/bad_index-has-key/example.json index 426313de2..6ec9d9e63 100644 --- a/databind/src/test/resources/metaschema/bad_index-has-key/example.json +++ b/databind/src/test/resources/metaschema/bad_index-has-key/example.json @@ -1,12 +1,12 @@ -{ - "root": { - "wrappers": [ - { - "id": "a" - }, - { - "id": "b" - } - ] - } +{ + "root": { + "wrappers": [ + { + "id": "a" + }, + { + "id": "b" + } + ] + } } \ No newline at end of file diff --git a/databind/src/test/resources/metaschema/fields_with_flags/example.json b/databind/src/test/resources/metaschema/fields_with_flags/example.json index 7240e099a..a770cb807 100644 --- a/databind/src/test/resources/metaschema/fields_with_flags/example.json +++ b/databind/src/test/resources/metaschema/fields_with_flags/example.json @@ -1,28 +1,28 @@ -{ - "$schema": "schema.json", - "top-level": { - "id": "test", - "complex-field1": { - "id": "complex-field1", - "STRVALUE": "test-string" - }, - "complex-fields2": { - "id": "complex-field2-1", - "STRVALUE": "test-string2" - }, - "complex-fields3": [ - { - "id2": "complex-field3-1", - "STRVALUE": "test-string3" - }, - { - "id2": "complex-field3-2", - "STRVALUE": "test-string4" - } - ], - "complex-fields4": { - "complex-field4-1": {"STRVALUE": "test-string5"}, - "complex-field4-2": {"STRVALUE": "test-string6"} - } - } +{ + "$schema": "schema.json", + "top-level": { + "id": "test", + "complex-field1": { + "id": "complex-field1", + "STRVALUE": "test-string" + }, + "complex-fields2": { + "id": "complex-field2-1", + "STRVALUE": "test-string2" + }, + "complex-fields3": [ + { + "id2": "complex-field3-1", + "STRVALUE": "test-string3" + }, + { + "id2": "complex-field3-2", + "STRVALUE": "test-string4" + } + ], + "complex-fields4": { + "complex-field4-1": {"STRVALUE": "test-string5"}, + "complex-field4-2": {"STRVALUE": "test-string6"} + } + } } \ No newline at end of file diff --git a/databind/src/test/resources/metaschema/fields_with_flags/example.xml b/databind/src/test/resources/metaschema/fields_with_flags/example.xml index fc1abb27f..982076db1 100644 --- a/databind/src/test/resources/metaschema/fields_with_flags/example.xml +++ b/databind/src/test/resources/metaschema/fields_with_flags/example.xml @@ -1,11 +1,11 @@ - - - test-string - test-string2 - - test-string3 - test-string4 - - test-string5 - test-string6 - + + + test-string + test-string2 + + test-string3 + test-string4 + + test-string5 + test-string6 + diff --git a/databind/src/test/resources/metaschema/fields_with_flags/metaschema.xml b/databind/src/test/resources/metaschema/fields_with_flags/metaschema.xml index 8cdc3fb05..809fd1c47 100644 --- a/databind/src/test/resources/metaschema/fields_with_flags/metaschema.xml +++ b/databind/src/test/resources/metaschema/fields_with_flags/metaschema.xml @@ -1,66 +1,66 @@ - - - Metaschema with complex field - 1.0 - complex-field - http://csrc.nist.gov/ns/metaschema/testing/fields/with/flags - http://csrc.nist.gov/ns/metaschema/testing/fields/with/flags - - - Root - Root assembly - top-level - - - - - cmplx-fd2 - - - - - - - - - - - - - Complex Field 1 - A complex field with a flag - - - - - Complex Field 1 - A complex field with a flag - - - - - Complex Field 3 - A complex field with a flag - - A flag - A simple flag - - - - - Complex Field 4 - A complex field with a flag - - - A flag - A simple flag - - - - - Identifier - The document identifier - - + + + Metaschema with complex field + 1.0 + complex-field + http://csrc.nist.gov/ns/metaschema/testing/fields/with/flags + http://csrc.nist.gov/ns/metaschema/testing/fields/with/flags + + + Root + Root assembly + top-level + + + + + cmplx-fd2 + + + + + + + + + + + + + Complex Field 1 + A complex field with a flag + + + + + Complex Field 1 + A complex field with a flag + + + + + Complex Field 3 + A complex field with a flag + + A flag + A simple flag + + + + + Complex Field 4 + A complex field with a flag + + + A flag + A simple flag + + + + + Identifier + The document identifier + + diff --git a/databind/src/test/resources/metaschema/simple/example.json b/databind/src/test/resources/metaschema/simple/example.json index 7ff0133a3..b591e6ec3 100644 --- a/databind/src/test/resources/metaschema/simple/example.json +++ b/databind/src/test/resources/metaschema/simple/example.json @@ -1,3 +1,3 @@ -{ - "top-level": {"id": "test"} +{ + "top-level": {"id": "test"} } \ No newline at end of file diff --git a/databind/src/test/resources/metaschema/simple/example.xml b/databind/src/test/resources/metaschema/simple/example.xml index 52fd40ca8..65ac3c890 100644 --- a/databind/src/test/resources/metaschema/simple/example.xml +++ b/databind/src/test/resources/metaschema/simple/example.xml @@ -1,2 +1,2 @@ - - + + diff --git a/databind/src/test/resources/metaschema/simple/metaschema.xml b/databind/src/test/resources/metaschema/simple/metaschema.xml index be2bc23be..efb07af38 100644 --- a/databind/src/test/resources/metaschema/simple/metaschema.xml +++ b/databind/src/test/resources/metaschema/simple/metaschema.xml @@ -1,23 +1,23 @@ - - - Simple Metaschema - 1.0 - simple - http://csrc.nist.gov/ns/metaschema/testing/simple - http://csrc.nist.gov/ns/metaschema/testing/simple - - - Root - Root assembly - top-level - - - - - - Identifier - The document identifier - - + + + Simple Metaschema + 1.0 + simple + http://csrc.nist.gov/ns/metaschema/testing/simple + http://csrc.nist.gov/ns/metaschema/testing/simple + + + Root + Root assembly + top-level + + + + + + Identifier + The document identifier + + diff --git a/databind/src/test/resources/metaschema/simple_with_field/example.json b/databind/src/test/resources/metaschema/simple_with_field/example.json index bdea751c8..be6697dd5 100644 --- a/databind/src/test/resources/metaschema/simple_with_field/example.json +++ b/databind/src/test/resources/metaschema/simple_with_field/example.json @@ -1,11 +1,11 @@ -{ - "top-level": { - "id": "test", - "field": "field-value", - "field2": "field-value2", - "field3": "field-value3", - "field4s": [ "markdown-string1", "markdown-string2" ], - "field5s": [ "string1", "string2" ], - "field6s": [ true, false ] - } +{ + "top-level": { + "id": "test", + "field": "field-value", + "field2": "field-value2", + "field3": "field-value3", + "field4s": [ "markdown-string1", "markdown-string2" ], + "field5s": [ "string1", "string2" ], + "field6s": [ true, false ] + } } \ No newline at end of file diff --git a/databind/src/test/resources/metaschema/simple_with_field/example.xml b/databind/src/test/resources/metaschema/simple_with_field/example.xml index a347fd680..656982b3d 100644 --- a/databind/src/test/resources/metaschema/simple_with_field/example.xml +++ b/databind/src/test/resources/metaschema/simple_with_field/example.xml @@ -1,17 +1,17 @@ - - - field-value - -

bold italicsfield2-markup1

-

field2-markup2

-
- field-value3 - -

markdown-string1link-text

-

markdown-string2

-
- string1 - string2 - true - false -
+ + + field-value + +

bold italicsfield2-markup1

+

field2-markup2

+
+ field-value3 + +

markdown-string1link-text

+

markdown-string2

+
+ string1 + string2 + true + false +
diff --git a/databind/src/test/resources/metaschema/simple_with_field/metaschema.xml b/databind/src/test/resources/metaschema/simple_with_field/metaschema.xml index 43104d4ac..ffe8d36cd 100644 --- a/databind/src/test/resources/metaschema/simple_with_field/metaschema.xml +++ b/databind/src/test/resources/metaschema/simple_with_field/metaschema.xml @@ -1,73 +1,73 @@ - - - Simple Metaschema - 1.0 - simple - http://csrc.nist.gov/ns/metaschema/testing/simple/with/field - http://csrc.nist.gov/ns/metaschema/testing/simple/with/field - - - Root - Root assembly - top-level - - - - - - - - - - - - - - - - - - - - Identifier - The document identifier - - - - A Field - A field with no flags. - text - - - - A Field - A field with no flags. - text2 - - - - A Field - A field with no flags. - text3 - - - - A Field - A field with no flags. - text4 - - - - A Field - A field with no flags. - text5 - - - - A Field - A field with no flags. - - - + + + Simple Metaschema + 1.0 + simple + http://csrc.nist.gov/ns/metaschema/testing/simple/with/field + http://csrc.nist.gov/ns/metaschema/testing/simple/with/field + + + Root + Root assembly + top-level + + + + + + + + + + + + + + + + + + + + Identifier + The document identifier + + + + A Field + A field with no flags. + text + + + + A Field + A field with no flags. + text2 + + + + A Field + A field with no flags. + text3 + + + + A Field + A field with no flags. + text4 + + + + A Field + A field with no flags. + text5 + + + + A Field + A field with no flags. + + + diff --git a/databind/src/test/resources/metaschema/simple_with_uuid/example.json b/databind/src/test/resources/metaschema/simple_with_uuid/example.json index cec099441..05ee8596f 100644 --- a/databind/src/test/resources/metaschema/simple_with_uuid/example.json +++ b/databind/src/test/resources/metaschema/simple_with_uuid/example.json @@ -1,3 +1,3 @@ -{ - "top-level": {"uuid": "5de455cf-2f8d-4da2-9182-323d433e1065"} +{ + "top-level": {"uuid": "5de455cf-2f8d-4da2-9182-323d433e1065"} } \ No newline at end of file diff --git a/databind/src/test/resources/metaschema/simple_with_uuid/example.xml b/databind/src/test/resources/metaschema/simple_with_uuid/example.xml index 7bdb9252a..92beef6c6 100644 --- a/databind/src/test/resources/metaschema/simple_with_uuid/example.xml +++ b/databind/src/test/resources/metaschema/simple_with_uuid/example.xml @@ -1,2 +1,2 @@ - - + + diff --git a/databind/src/test/resources/metaschema/simple_with_uuid/metaschema.xml b/databind/src/test/resources/metaschema/simple_with_uuid/metaschema.xml index d447fa58d..a7d57fdc1 100644 --- a/databind/src/test/resources/metaschema/simple_with_uuid/metaschema.xml +++ b/databind/src/test/resources/metaschema/simple_with_uuid/metaschema.xml @@ -1,23 +1,23 @@ - - - Simple Metaschema - 1.0 - simple - http://csrc.nist.gov/ns/metaschema/testing/simple/with/uuid - http://csrc.nist.gov/ns/metaschema/testing/simple/with/uuid - - - Root - Root assembly - top-level - - - - - - Universally Unique Identifier - A RFC 4122 version 4 Universally Unique Identifier (UUID) for the containing object. - - + + + Simple Metaschema + 1.0 + simple + http://csrc.nist.gov/ns/metaschema/testing/simple/with/uuid + http://csrc.nist.gov/ns/metaschema/testing/simple/with/uuid + + + Root + Root assembly + top-level + + + + + + Universally Unique Identifier + A RFC 4122 version 4 Universally Unique Identifier (UUID) for the containing object. + + From 2896f01f3b4e481063fb6b46e88565e262810135 Mon Sep 17 00:00:00 2001 From: David Waltermire Date: Mon, 22 Apr 2024 22:08:23 -0400 Subject: [PATCH 03/13] Add support for Expanded QNames --- .../cli/processor/CLIProcessor.java | 2 + .../command/DefaultExtraArgument.java | 4 + core/src/main/antlr4/Metapath10.g4 | 9 +- .../core/datatype/markup/MarkupLine.java | 2 +- .../core/metapath/DynamicContext.java | 14 +- .../metaschema/core/metapath/EQNameUtils.java | 101 ++++ .../core/metapath/MetapathExpression.java | 58 ++- .../core/metapath/StaticContext.java | 179 ++++++- .../metapath/antlr/AbstractAstVisitor.java | 22 +- .../cst/AbstractExpressionVisitor.java | 8 +- .../cst/AbstractNamedInstanceExpression.java | 4 +- .../core/metapath/cst/BuildCSTVisitor.java | 442 ++++++++++-------- .../core/metapath/cst/CSTPrinter.java | 8 +- .../metaschema/core/metapath/cst/For.java | 2 +- .../core/metapath/cst/IExpressionVisitor.java | 8 +- .../metaschema/core/metapath/cst/Let.java | 14 +- ...redicate.java => PredicateExpression.java} | 4 +- .../core/metapath/cst/Quantified.java | 14 +- ...ctionCall.java => StaticFunctionCall.java} | 8 +- .../core/metapath/cst/VariableReference.java | 12 +- .../core/metapath/cst/path/Flag.java | 7 +- .../INameTestExpression.java} | 31 +- .../cst/path/INodeTestExpression.java | 8 +- .../core/metapath/cst/path/ModelInstance.java | 7 +- .../cst/{Name.java => path/NameTest.java} | 41 +- .../core/metapath/cst/path/Step.java | 6 +- .../core/metapath/cst/path/Wildcard.java | 103 ++++ .../metapath/format/MetapathFormatter.java | 5 +- .../library/DefaultFunctionLibrary.java | 2 + .../item/node/AbstractNodeItemFactory.java | 10 +- .../CycledAssemblyInstanceNodeItemImpl.java | 6 +- .../item/node/DefaultNodeItemFactory.java | 53 ++- .../item/node/DocumentNodeItemImpl.java | 5 + .../item/node/IDefinitionNodeItem.java | 16 +- .../metapath/item/node/IDocumentNodeItem.java | 4 +- .../item/node/IFeatureFlagContainerItem.java | 12 +- .../item/node/IFeatureModelContainerItem.java | 13 +- .../metapath/item/node/IFlagNodeItem.java | 6 +- .../core/metapath/item/node/INodeItem.java | 16 +- .../item/node/IRootAssemblyNodeItem.java | 7 +- .../item/node/ModuleNodeItemImpl.java | 7 + .../node/RootAssemblyValuedNodeItemImpl.java | 7 + .../core/model/EmptyFlagContainer.java | 4 +- .../metaschema/core/model/IAssembly.java | 2 +- .../core/model/IAssemblyDefinition.java | 3 +- .../core/model/IAssemblyInstance.java | 12 - .../metaschema/core/model/IContainerFlag.java | 4 +- .../core/model/IContainerFlagSupport.java | 4 +- .../core/model/IContainerModel.java | 8 +- .../core/model/IContainerModelAbsolute.java | 8 +- .../core/model/IContainerModelGrouped.java | 8 +- .../core/model/IContainerModelSupport.java | 8 +- .../metaschema/core/model/IDefinition.java | 2 +- .../core/model/IFeatureContainerFlag.java | 4 +- .../core/model/IFeatureContainerModel.java | 8 +- .../model/IFeatureContainerModelAbsolute.java | 8 +- .../model/IFeatureContainerModelGrouped.java | 8 +- .../secauto/metaschema/core/model/IField.java | 2 +- .../metaschema/core/model/IFieldInstance.java | 12 - .../secauto/metaschema/core/model/IFlag.java | 12 +- .../metaschema/core/model/IFlagInstance.java | 19 - .../metaschema/core/model/IGroupable.java | 8 +- .../metaschema/core/model/IModule.java | 14 +- .../metaschema/core/model/INamedInstance.java | 21 +- .../{INamed.java => INamedModelElement.java} | 59 ++- .../constraint/AbstractConstraintBuilder.java | 7 +- .../AbstractConstraintValidationHandler.java | 4 +- .../AbstractTargetedConstraints.java | 8 +- .../AssemblyTargetedConstraints.java | 3 +- .../DefaultConstraintValidator.java | 12 +- .../constraint/FieldTargetedConstraints.java | 3 +- .../constraint/FlagTargetedConstraints.java | 3 +- .../core/model/constraint/IConstraint.java | 16 +- .../model/constraint/IExpectConstraint.java | 9 +- .../constraint/IFeatureValueConstrained.java | 4 +- .../core/model/constraint/IIndex.java | 5 +- .../core/model/constraint/IKeyField.java | 5 +- .../core/model/constraint/ILet.java | 8 +- .../constraint/ITargetedConstraints.java | 3 +- .../model/constraint/IValueConstrained.java | 4 +- .../model/constraint/ValueConstraintSet.java | 6 +- .../constraint/impl/AbstractConstraint.java | 7 +- ...AbstractDefinitionTargetedConstraints.java | 3 +- .../impl/AbstractKeyConstraint.java | 7 +- .../impl/DefaultAllowedValuesConstraint.java | 3 +- .../impl/DefaultCardinalityConstraint.java | 3 +- .../impl/DefaultExpectConstraint.java | 10 +- .../impl/DefaultIndexConstraint.java | 5 +- .../impl/DefaultIndexHasKeyConstraint.java | 3 +- .../constraint/impl/DefaultKeyField.java | 8 +- .../model/constraint/impl/DefaultLet.java | 8 +- .../impl/DefaultMatchesConstraint.java | 3 +- .../impl/DefaultUniqueConstraint.java | 3 +- ...xternalConstraintsModulePostProcessor.java | 12 +- .../core/model/xml/XmlConstraintLoader.java | 16 +- .../model/xml/XmlMetaConstraintLoader.java | 4 +- .../xml/impl/DefaultContainerFlagSupport.java | 8 +- .../DefaultContainerModelAssemblySupport.java | 8 +- .../impl/DefaultContainerModelSupport.java | 32 +- .../DefaultGroupedModelContainerSupport.java | 32 +- .../core/model/xml/impl/ModelFactory.java | 28 +- .../xml/impl/XmlAssemblyModelContainer.java | 4 +- .../xml/impl/XmlChoiceGroupInstance.java | 4 +- .../model/xml/impl/XmlChoiceInstance.java | 4 +- .../xml/impl/XmlFlagContainerSupport.java | 38 +- .../xml/impl/XmlGlobalFieldDefinition.java | 7 +- .../impl/XmlGroupedInlineFieldDefinition.java | 7 +- .../xml/impl/XmlInlineFieldDefinition.java | 7 +- .../metaschema/core/util/ObjectUtils.java | 9 +- core/src/schema/xmlconfig.xml | 2 + .../core/metapath/ExpressionTestBase.java | 4 +- .../metaschema/core/metapath/TestUtils.java | 2 +- .../metapath/cst/ArrowExpressionTest.java | 3 +- .../metapath/cst/BuildCstVisitorTest.java | 104 +++-- .../core/metapath/cst/EQNameUtilsTest.java | 116 +++++ .../core/metapath/cst/FlagTest.java | 7 +- .../core/metapath/cst/PredicateTest.java | 6 +- .../item/node/DefaultNodeItemFactoryTest.java | 32 +- .../item/node/MockNodeItemFactory.java | 34 +- .../DefaultConstraintValidatorTest.java | 28 +- .../model/xml/MetaConstraintLoaderTest.java | 7 +- .../core/testing/AbstractModelBuilder.java | 28 +- .../core/testing/AssemblyBuilder.java | 10 +- .../metaschema/core/testing/FieldBuilder.java | 6 +- .../metaschema/core/testing/FlagBuilder.java | 8 + .../metaschema/databind/IBindingContext.java | 18 +- .../codegen/PackageProductionImpl.java | 2 + .../config/DefaultBindingConfiguration.java | 2 +- .../codegen/impl/AnnotationGenerator.java | 31 +- .../AbstractModelInstanceTypeInfo.java | 16 +- .../typeinfo/FlagInstanceTypeInfoImpl.java | 14 +- .../codegen/typeinfo/TypeInfoUtils.java | 41 +- .../databind/io/AbstractDeserializer.java | 5 +- .../io/json/MetaschemaJsonReader.java | 15 +- .../io/xml/DefaultXmlDeserializer.java | 4 +- .../databind/io/xml/MetaschemaXmlReader.java | 2 +- .../databind/model/IBoundContainerFlag.java | 8 +- .../model/IBoundContainerModelAssembly.java | 11 +- .../IBoundContainerModelChoiceGroup.java | 11 +- .../model/IBoundDefinitionModelAssembly.java | 11 +- .../model/IBoundDefinitionModelComplex.java | 20 - .../IBoundInstanceModelFieldComplex.java | 6 - .../IBoundInstanceModelGroupedNamed.java | 7 +- .../model/IBoundInstanceModelNamed.java | 9 +- .../databind/model/annotations/ModelUtil.java | 26 +- .../annotations/NullJavaTypeAdapter.java | 1 - .../impl/AbstractBoundInstanceField.java | 13 +- .../impl/AssemblyModelContainerSupport.java | 48 +- .../model/impl/ConstraintFactory.java | 9 +- .../{annotations => impl}/DefaultGroupAs.java | 18 +- .../model/impl/DefinitionAssembly.java | 6 +- .../databind/model/impl/DefinitionField.java | 2 + .../model/impl/FlagContainerSupport.java | 24 +- .../impl/IFeatureBoundContainerFlag.java | 13 +- .../IFeatureBoundContainerModelAssembly.java | 19 +- ...FeatureBoundContainerModelChoiceGroup.java | 15 +- .../model/impl/InstanceFlagInline.java | 4 +- .../impl/InstanceModelAssemblyComplex.java | 13 +- .../model/impl/InstanceModelChoiceGroup.java | 25 +- .../model/impl/InstanceModelFieldComplex.java | 2 + .../impl/InstanceModelGroupedAssembly.java | 7 + .../InstanceModelGroupedFieldComplex.java | 7 + .../metaschema/BindingConstraintLoader.java | 30 +- .../IBindingContainerModelAbsolute.java | 12 +- .../IBindingContainerModelGrouped.java | 12 +- .../metaschema/IBindingDefinitionModel.java | 8 +- .../metaschema/binding/FlagReference.java | 2 +- .../AbstractBindingModelContainerSupport.java | 23 +- .../impl/AbstractInstanceModelGrouped.java | 2 +- .../impl/AbstractInstanceModelNamed.java | 4 +- .../impl/AssemblyModelContainerSupport.java | 39 +- .../model/metaschema/impl/BindingModule.java | 2 + .../ChoiceGroupModelContainerSupport.java | 50 +- .../impl/ChoiceModelContainerSupport.java | 38 +- .../impl/ConstraintBindingSupport.java | 11 +- .../impl/DefinitionAssemblyGlobal.java | 7 +- .../impl/DefinitionFieldGlobal.java | 10 +- .../metaschema/impl/DefinitionFlagGlobal.java | 2 +- .../metaschema/impl/FlagContainerSupport.java | 32 +- .../model/metaschema/impl/GroupAsImpl.java | 4 +- .../impl/IFeatureBindingContainerFlag.java | 13 +- .../impl/IFeatureBindingContainerModel.java | 15 +- ...IFeatureBindingContainerModelAssembly.java | 19 +- .../metaschema/impl/InstanceFlagInline.java | 2 +- .../impl/InstanceFlagReference.java | 2 +- .../impl/InstanceModelAssemblyInline.java | 4 +- .../metaschema/impl/InstanceModelChoice.java | 7 +- .../impl/InstanceModelChoiceGroup.java | 4 +- .../impl/InstanceModelFieldInline.java | 9 +- .../InstanceModelGroupedAssemblyInline.java | 6 +- .../impl/InstanceModelGroupedFieldInline.java | 9 +- .../model/metaschema/impl/ModelSupport.java | 6 +- .../databind/io/xml/XmlParserTest.java | 15 +- .../databind/testing/model/ModelTest.java | 25 +- .../databind/testing/model/ModelTestBase.java | 10 +- ...it.jupiter.api.extension.Extension-removed | 2 +- .../AbstractValidateContentCommand.java | 4 +- .../cli/util/LoggingValidationHandler.java | 5 +- .../xml-outline.html | 100 ++-- .../src/it/generate-sources/verify.groovy | 2 +- pom.xml | 3 +- .../schemagen/AbstractGenerationState.java | 2 +- .../json/impl/AbstractJsonProperty.java | 1 + .../AbstractModelDefinitionJsonSchema.java | 5 +- .../impl/AssemblyDefinitionJsonSchema.java | 4 +- .../impl/ChoiceGroupInstanceJsonProperty.java | 2 + .../schemagen/json/impl/MetadataUtils.java | 6 +- .../builder/AbstractCollectionBuilder.java | 6 +- .../xml/datatype/JDom2XmlSchemaLoader.java | 2 + 209 files changed, 2161 insertions(+), 1207 deletions(-) create mode 100644 core/src/main/java/gov/nist/secauto/metaschema/core/metapath/EQNameUtils.java rename core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/{Predicate.java => PredicateExpression.java} (97%) rename core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/{FunctionCall.java => StaticFunctionCall.java} (95%) rename core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/{Wildcard.java => path/INameTestExpression.java} (67%) rename databind/src/test/java/gov/nist/secauto/metaschema/databind/testing/model/package-info.java => core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/path/INodeTestExpression.java (88%) rename core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/{Name.java => path/NameTest.java} (79%) create mode 100644 core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/path/Wildcard.java rename core/src/main/java/gov/nist/secauto/metaschema/core/model/{INamed.java => INamedModelElement.java} (76%) create mode 100644 core/src/test/java/gov/nist/secauto/metaschema/core/metapath/cst/EQNameUtilsTest.java rename databind/src/main/java/gov/nist/secauto/metaschema/databind/model/{annotations => impl}/DefaultGroupAs.java (81%) diff --git a/cli-processor/src/main/java/gov/nist/secauto/metaschema/cli/processor/CLIProcessor.java b/cli-processor/src/main/java/gov/nist/secauto/metaschema/cli/processor/CLIProcessor.java index 44a921fdd..95389d82c 100644 --- a/cli-processor/src/main/java/gov/nist/secauto/metaschema/cli/processor/CLIProcessor.java +++ b/cli-processor/src/main/java/gov/nist/secauto/metaschema/cli/processor/CLIProcessor.java @@ -66,6 +66,7 @@ import edu.umd.cs.findbugs.annotations.NonNull; import edu.umd.cs.findbugs.annotations.Nullable; +import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; public class CLIProcessor { private static final Logger LOGGER = LogManager.getLogger(CLIProcessor.class); @@ -253,6 +254,7 @@ public class CallingContext { @NonNull private final List extraArgs; + @SuppressFBWarnings(value = "CT_CONSTRUCTOR_THROW", justification = "Use of final fields") public CallingContext(@NonNull List args) { Map topLevelCommandMap = getTopLevelCommands().stream() .collect(Collectors.toUnmodifiableMap(ICommand::getName, Function.identity())); diff --git a/cli-processor/src/main/java/gov/nist/secauto/metaschema/cli/processor/command/DefaultExtraArgument.java b/cli-processor/src/main/java/gov/nist/secauto/metaschema/cli/processor/command/DefaultExtraArgument.java index d98b79211..591ed48f1 100644 --- a/cli-processor/src/main/java/gov/nist/secauto/metaschema/cli/processor/command/DefaultExtraArgument.java +++ b/cli-processor/src/main/java/gov/nist/secauto/metaschema/cli/processor/command/DefaultExtraArgument.java @@ -26,15 +26,19 @@ package gov.nist.secauto.metaschema.cli.processor.command; +import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; + public class DefaultExtraArgument implements ExtraArgument { private final String name; private final boolean required; private final int number; + @SuppressFBWarnings(value = "CT_CONSTRUCTOR_THROW", justification = "Use of final fields") public DefaultExtraArgument(String name, boolean required) { this(name, required, 1); } + @SuppressFBWarnings(value = "CT_CONSTRUCTOR_THROW", justification = "Use of final fields") public DefaultExtraArgument(String name, boolean required, int number) { if (number < 1) { throw new IllegalArgumentException("number must be a positive value"); diff --git a/core/src/main/antlr4/Metapath10.g4 b/core/src/main/antlr4/Metapath10.g4 index 77e81a4b0..79e006469 100644 --- a/core/src/main/antlr4/Metapath10.g4 +++ b/core/src/main/antlr4/Metapath10.g4 @@ -57,14 +57,11 @@ relativepathexpr : stepexpr (( SLASH | SS) stepexpr)* ; stepexpr : postfixexpr | axisstep ; axisstep : (reversestep | forwardstep) predicatelist ; // [40] -// forwardstep : forwardaxis nodetest | abbrevforwardstep ; -forwardstep : forwardaxis nametest | abbrevforwardstep ; +forwardstep : forwardaxis nodetest | abbrevforwardstep ; // forwardaxis : KW_CHILD COLONCOLON | KW_DESCENDANT COLONCOLON | KW_ATTRIBUTE COLONCOLON | KW_SELF COLONCOLON | KW_DESCENDANT_OR_SELF COLONCOLON | KW_FOLLOWING_SIBLING COLONCOLON | KW_FOLLOWING COLONCOLON | KW_NAMESPACE COLONCOLON ; forwardaxis : KW_CHILD COLONCOLON | KW_DESCENDANT COLONCOLON | KW_SELF COLONCOLON | KW_DESCENDANT_OR_SELF COLONCOLON ; -// abbrevforwardstep : AT? nodetest ; -abbrevforwardstep : AT? nametest ; -// reversestep : reverseaxis nodetest | abbrevreversestep ; -reversestep : reverseaxis nametest | abbrevreversestep ; +abbrevforwardstep : AT? nodetest ; +reversestep : reverseaxis nodetest | abbrevreversestep ; // reverseaxis : KW_PARENT COLONCOLON | KW_ANCESTOR COLONCOLON | KW_PRECEDING_SIBLING COLONCOLON | KW_PRECEDING COLONCOLON | KW_ANCESTOR_OR_SELF COLONCOLON ; reverseaxis : KW_PARENT COLONCOLON | KW_ANCESTOR COLONCOLON | KW_ANCESTOR_OR_SELF COLONCOLON ; // [45] diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/markup/MarkupLine.java b/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/markup/MarkupLine.java index 72f223e1d..196b5f303 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/markup/MarkupLine.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/markup/MarkupLine.java @@ -45,7 +45,7 @@ import edu.umd.cs.findbugs.annotations.NonNull; -public class MarkupLine +public final class MarkupLine extends AbstractMarkupString { @NonNull diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/DynamicContext.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/DynamicContext.java index 0b13b5479..7f65a7ccb 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/DynamicContext.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/DynamicContext.java @@ -45,6 +45,8 @@ import java.util.Map; import java.util.concurrent.ConcurrentHashMap; +import javax.xml.namespace.QName; + import edu.umd.cs.findbugs.annotations.NonNull; // TODO: add support for in-scope namespaces @@ -54,17 +56,21 @@ */ public class DynamicContext { // NOPMD - intentional data class @NonNull - private final Map> letVariableMap; + private final Map> letVariableMap; @NonNull private final SharedState sharedState; + public DynamicContext() { + this(StaticContext.instance()); + } + /** * Construct a new Metapath dynamic context. * * @param staticContext * the Metapath static context */ - DynamicContext(@NonNull StaticContext staticContext) { + public DynamicContext(@NonNull StaticContext staticContext) { this.letVariableMap = new ConcurrentHashMap<>(); this.sharedState = new SharedState(staticContext); } @@ -181,7 +187,7 @@ public void cacheResult(@NonNull CallingContext callingContext, @NonNull ISequen } @NonNull - public ISequence getVariableValue(@NonNull String name) { + public ISequence getVariableValue(@NonNull QName name) { return ObjectUtils.requireNonNull(letVariableMap.get(name)); } @@ -193,7 +199,7 @@ public ISequence getVariableValue(@NonNull String name) { * @param boundValue * the value to bind to the variable */ - public void bindVariableValue(@NonNull String name, @NonNull ISequence boundValue) { + public void bindVariableValue(@NonNull QName name, @NonNull ISequence boundValue) { letVariableMap.put(name, boundValue); } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/EQNameUtils.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/EQNameUtils.java new file mode 100644 index 000000000..1e83efe8d --- /dev/null +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/EQNameUtils.java @@ -0,0 +1,101 @@ +/* + * Portions of this software was developed by employees of the National Institute + * of Standards and Technology (NIST), an agency of the Federal Government and is + * being made available as a public service. Pursuant to title 17 United States + * Code Section 105, works of NIST employees are not subject to copyright + * protection in the United States. This software may be subject to foreign + * copyright. Permission in the United States and in foreign countries, to the + * extent that NIST may hold copyright, to use, copy, modify, create derivative + * works, and distribute this software and its documentation without fee is hereby + * granted on a non-exclusive basis, provided that this notice and disclaimer + * of warranty appears in all copies. + * + * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER + * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY + * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM + * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE + * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT + * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, + * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, + * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, + * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR + * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT + * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. + */ + +package gov.nist.secauto.metaschema.core.metapath; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import javax.xml.XMLConstants; +import javax.xml.namespace.QName; + +import edu.umd.cs.findbugs.annotations.NonNull; +import edu.umd.cs.findbugs.annotations.Nullable; + +public final class EQNameUtils { + private static final Pattern URI_QUALIFIED_NAME = Pattern.compile("^Q\\{([^{}]*)\\}(.+)$"); + private static final Pattern LEXICAL_NAME = Pattern.compile("^(?:([^:]+):)?(.+)$"); + private static final Pattern NCNAME = Pattern.compile(String.format("^(\\p{L}|_)(\\p{L}|\\p{N}|[.\\-_])*$")); + + private EQNameUtils() { + // disable construction + } + + @NonNull + public static QName parseName( + @NonNull String name, + @Nullable IEQNamePrefixResolver resolver) { + Matcher matcher = URI_QUALIFIED_NAME.matcher(name); + return matcher.matches() + ? newUriQualifiedName(matcher) + : parseLexicalQName(name, resolver); + } + + @NonNull + public static QName parseUriQualifiedName(@NonNull String name) { + Matcher matcher = URI_QUALIFIED_NAME.matcher(name); + if (!matcher.matches()) { + throw new IllegalArgumentException( + String.format("The name '%s' is not a valid BracedURILiteral of the form: Q{URI}local-name", name)); + } + return newUriQualifiedName(matcher); + } + + @NonNull + private static QName newUriQualifiedName(@NonNull Matcher matcher) { + return new QName(matcher.group(1), matcher.group(2)); + } + + @NonNull + public static QName parseLexicalQName( + @NonNull String name, + @Nullable IEQNamePrefixResolver resolver) { + Matcher matcher = LEXICAL_NAME.matcher(name); + if (!matcher.matches()) { + throw new IllegalArgumentException( + String.format("The name '%s' is not a valid lexical QName of the form: prefix:local-name or local-name", + name)); + } + String prefix = matcher.group(1); + + if (prefix == null) { + prefix = XMLConstants.DEFAULT_NS_PREFIX; + } + + String namespace = resolver == null ? XMLConstants.NULL_NS_URI : resolver.resolve(prefix); + return new QName(namespace, matcher.group(2), prefix); + } + + public static boolean isNcName(@NonNull String name) { + return NCNAME.matcher(name).matches(); + } + + @FunctionalInterface + public interface IEQNamePrefixResolver { + @NonNull + String resolve(@NonNull String prefix); + } +} diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/MetapathExpression.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/MetapathExpression.java index 15d1674aa..2d09b2fbe 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/MetapathExpression.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/MetapathExpression.java @@ -94,11 +94,14 @@ public enum ResultType { private static final Logger LOGGER = LogManager.getLogger(MetapathExpression.class); @NonNull - public static final MetapathExpression CONTEXT_NODE = new MetapathExpression(".", ContextItem.instance()); + public static final MetapathExpression CONTEXT_NODE + = new MetapathExpression(".", ContextItem.instance(), StaticContext.instance()); private final String path; @NonNull - private final IExpression node; + private final IExpression expression; + @NonNull + private final StaticContext staticContext; /** * Compiles a Metapath expression string. @@ -111,6 +114,24 @@ public enum ResultType { */ @NonNull public static MetapathExpression compile(@NonNull String path) { + StaticContext context = StaticContext.builder().build(); + + return compile(path, context); + } + + /** + * Compiles a Metapath expression string using the provided static context. + * + * @param path + * the metapath expression + * @param context + * the static evaluation context + * @return the compiled expression object + * @throws MetapathException + * if an error occurred while compiling the Metapath expression + */ + @NonNull + public static MetapathExpression compile(@NonNull String path, @NonNull StaticContext context) { @NonNull MetapathExpression retval; if (".".equals(path)) { retval = CONTEXT_NODE; @@ -140,12 +161,12 @@ public static MetapathExpression compile(@NonNull String path) { } } - IExpression expr = new BuildCSTVisitor().visit(tree); + IExpression expr = new BuildCSTVisitor(context).visit(tree); if (LOGGER.isDebugEnabled()) { LOGGER.atDebug().log(String.format("Metapath CST:%n%s", CSTPrinter.toString(expr))); } - retval = new MetapathExpression(path, expr); + retval = new MetapathExpression(path, expr, context); } catch (MetapathException | ParseCancellationException ex) { String msg = String.format("Unable to compile Metapath '%s'", path); LOGGER.atError().withThrowable(ex).log(msg); @@ -162,10 +183,13 @@ public static MetapathExpression compile(@NonNull String path) { * the Metapath as a string * @param expr * the Metapath as a compiled abstract syntax tree (AST) + * @param staticContext + * the static evaluation context */ - protected MetapathExpression(@NonNull String path, @NonNull IExpression expr) { + protected MetapathExpression(@NonNull String path, @NonNull IExpression expr, @NonNull StaticContext staticContext) { this.path = path; - this.node = expr; + this.expression = expr; + this.staticContext = staticContext; } /** @@ -184,7 +208,22 @@ public String getPath() { */ @NonNull protected IExpression getASTNode() { - return node; + return expression; + } + + @NonNull + protected StaticContext getStaticContext() { + return staticContext; + } + + /** + * Generate a new dynamic context. + * + * @return the generated dynamic context + */ + @NonNull + public DynamicContext dynamicContext() { + return new DynamicContext(getStaticContext()); } @Override @@ -360,10 +399,7 @@ public ISequence evaluate() { @NonNull public ISequence evaluate( @Nullable IItem focus) { - return (ISequence) evaluate( - focus, - StaticContext.builder() - .build().dynamicContext()); + return (ISequence) evaluate(focus, dynamicContext()); } /** diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/StaticContext.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/StaticContext.java index c30b41349..18f07fb07 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/StaticContext.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/StaticContext.java @@ -26,12 +26,15 @@ package gov.nist.secauto.metaschema.core.metapath; +import gov.nist.secauto.metaschema.core.metapath.EQNameUtils.IEQNamePrefixResolver; import gov.nist.secauto.metaschema.core.util.CollectionUtil; import java.net.URI; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; +import javax.xml.XMLConstants; + import edu.umd.cs.findbugs.annotations.NonNull; import edu.umd.cs.findbugs.annotations.Nullable; import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; @@ -66,6 +69,10 @@ public final class StaticContext { private final URI baseUri; @NonNull private final Map knownNamespaces; + @Nullable + private final URI defaultModelNamespace; + @Nullable + private final URI defaultFunctionNamespace; /** * Get the mapping of prefix to namespace URI for all well-known namespaces @@ -102,11 +109,11 @@ public static Builder builder() { return new Builder(); } - private StaticContext( - @Nullable URI baseUri, - @NonNull Map knownNamespaces) { - this.baseUri = baseUri; - this.knownNamespaces = knownNamespaces; + private StaticContext(Builder builder) { + this.baseUri = builder.baseUri; + this.knownNamespaces = CollectionUtil.unmodifiableMap(Map.copyOf(builder.namespaces)); + this.defaultModelNamespace = builder.defaultModelNamespace; + this.defaultFunctionNamespace = builder.defaultFunctionNamespace; } /** @@ -167,22 +174,93 @@ public String lookupNamespaceForPrefix(@NonNull String prefix) { } /** - * Generate a new dynamic context. + * Get the default namespace for assembly, field, or flag references that have + * no namespace prefix. + * + * @return the namespace if defined or {@code null} otherwise + */ + @Nullable + public URI getDefaultModelNamespace() { + return defaultModelNamespace; + } + + /** + * Get the default namespace for function references that have no namespace + * prefix. * - * @return the generated dynamic context + * @return the namespace if defined or {@code null} otherwise */ + @Nullable + public URI getDefaultFunctionNamespace() { + return defaultFunctionNamespace; + } + + @NonNull + public IEQNamePrefixResolver getFunctionPrefixResolver() { + return (prefix) -> { + String ns = lookupNamespaceForPrefix(prefix); + if (ns == null) { + URI uri = getDefaultFunctionNamespace(); + if (uri != null) { + ns = uri.toASCIIString(); + } + } + return ns == null ? XMLConstants.NULL_NS_URI : ns; + }; + } + + @NonNull + public IEQNamePrefixResolver getFlagPrefixResolver() { + return (prefix) -> { + String ns = lookupNamespaceForPrefix(prefix); + return ns == null ? XMLConstants.NULL_NS_URI : ns; + }; + } + + @NonNull + public IEQNamePrefixResolver getModelPrefixResolver() { + return (prefix) -> { + String ns = lookupNamespaceForPrefix(prefix); + if (ns == null) { + URI uri = getDefaultModelNamespace(); + if (uri != null) { + ns = uri.toASCIIString(); + } + } + return ns == null ? XMLConstants.NULL_NS_URI : ns; + }; + } + + @NonNull + public IEQNamePrefixResolver getVariablePrefixResolver() { + return (prefix) -> { + String ns = lookupNamespaceForPrefix(prefix); + return ns == null ? XMLConstants.NULL_NS_URI : ns; + }; + } + @NonNull - public DynamicContext dynamicContext() { - return new DynamicContext(this); + public Builder buildFrom() { + Builder builder = new Builder(); + builder.baseUri = this.baseUri; + builder.namespaces.putAll(this.knownNamespaces); + builder.defaultModelNamespace = this.defaultModelNamespace; + builder.defaultFunctionNamespace = this.defaultFunctionNamespace; + return builder; } /** * A builder used to generate the static context. */ public static final class Builder { + @Nullable private URI baseUri; @NonNull private final Map namespaces = new ConcurrentHashMap<>(); + @Nullable + private URI defaultModelNamespace; + @Nullable + private URI defaultFunctionNamespace = MetapathConstants.NS_METAPATH_FUNCTIONS; private Builder() { namespaces.put( @@ -240,6 +318,85 @@ public Builder namespace(@NonNull String prefix, @NonNull URI uri) { return this; } + /** + * A convenience method for {@link #namespace(String, URI)}. + * + * @param prefix + * the prefix to associate with the namespace, which may be + * @param uri + * the namespace URI + * @return this builder + * @throws IllegalArgumentException + * if the provided URI is invalid + * @see StaticContext#lookupNamespaceForPrefix(String) + * @see StaticContext#lookupNamespaceURIForPrefix(String) + * @see StaticContext#getWellKnownNamespaces() + */ + @NonNull + public Builder namespace(@NonNull String prefix, @NonNull String uri) { + return namespace(prefix, URI.create(uri)); + } + + /** + * Defines the default namespace to use for assembly, field, or flag references + * that have no namespace prefix. + * + * @param uri + * the namespace URI + * @return this builder + * @see StaticContext#getDefaultModelNamespace() + */ + @NonNull + public Builder defaultModelNamespace(@NonNull URI uri) { + this.defaultModelNamespace = uri; + return this; + } + + /** + * A convenience method for {@link #defaultModelNamespace(URI)}. + * + * @param uri + * the namespace URI + * @return this builder + * @throws IllegalArgumentException + * if the provided URI is invalid + * @see StaticContext#getDefaultModelNamespace() + */ + @NonNull + public Builder defaultModelNamespace(@NonNull String uri) { + return defaultModelNamespace(URI.create(uri)); + } + + /** + * Defines the default namespace to use for assembly, field, or flag references + * that have no namespace prefix. + * + * @param uri + * the namespace URI + * @return this builder + * @see StaticContext#getDefaultFunctionNamespace() + */ + @NonNull + public Builder defaultFunctionNamespace(@NonNull URI uri) { + this.defaultFunctionNamespace = uri; + return this; + } + + /** + * A convenience method for {@link #defaultFunctionNamespace(URI)}. + * + * @param uri + * the namespace URI + * @return this builder + * @throws IllegalArgumentException + * if the provided URI is invalid + * @see StaticContext#getDefaultFunctionNamespace() + */ + @NonNull + public Builder defaultFunctionNamespace(@NonNull String uri) { + return defaultFunctionNamespace(URI.create(uri)); + } + /** * Construct a new static context using the information provided to the builder. * @@ -247,9 +404,7 @@ public Builder namespace(@NonNull String prefix, @NonNull URI uri) { */ @NonNull public StaticContext build() { - return new StaticContext( - baseUri, - CollectionUtil.unmodifiableMap(namespaces)); + return new StaticContext(this); } } } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/antlr/AbstractAstVisitor.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/antlr/AbstractAstVisitor.java index 3b3d16983..b1e15f4e1 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/antlr/AbstractAstVisitor.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/antlr/AbstractAstVisitor.java @@ -473,30 +473,20 @@ public R visitReverseaxis(ReverseaxisContext ctx) { @Override public R visitNodetest(NodetestContext ctx) { - // TODO: revisit once kindtest is implemented - assert ctx != null; - return delegateToChild(ctx); + // should never be called, since this is handled by the calling context + throw new IllegalStateException(); } @Override public R visitNametest(NametestContext ctx) { - assert ctx != null; - return delegateToChild(ctx); + // should never be called, since this is handled by the calling context + throw new IllegalStateException(); } - /** - * Handle the provided expression. - * - * @param ctx - * the provided expression context - * @return the result - */ - protected abstract R handleEqname(@NonNull EqnameContext ctx); - @Override public R visitEqname(EqnameContext ctx) { - assert ctx != null; - return handleEqname(ctx); + // should never be called, since this is handled by the calling context + throw new IllegalStateException(); } /** diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/AbstractExpressionVisitor.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/AbstractExpressionVisitor.java index 24e5fe06e..92c0b5e2a 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/AbstractExpressionVisitor.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/AbstractExpressionVisitor.java @@ -37,12 +37,14 @@ import gov.nist.secauto.metaschema.core.metapath.cst.path.Axis; import gov.nist.secauto.metaschema.core.metapath.cst.path.Flag; import gov.nist.secauto.metaschema.core.metapath.cst.path.ModelInstance; +import gov.nist.secauto.metaschema.core.metapath.cst.path.NameTest; import gov.nist.secauto.metaschema.core.metapath.cst.path.RelativeDoubleSlashPath; import gov.nist.secauto.metaschema.core.metapath.cst.path.RelativeSlashPath; import gov.nist.secauto.metaschema.core.metapath.cst.path.RootDoubleSlashPath; import gov.nist.secauto.metaschema.core.metapath.cst.path.RootSlashOnlyPath; import gov.nist.secauto.metaschema.core.metapath.cst.path.RootSlashPath; import gov.nist.secauto.metaschema.core.metapath.cst.path.Step; +import gov.nist.secauto.metaschema.core.metapath.cst.path.Wildcard; import edu.umd.cs.findbugs.annotations.NonNull; import edu.umd.cs.findbugs.annotations.Nullable; @@ -202,7 +204,7 @@ public RESULT visitFlag(Flag expr, CONTEXT context) { } @Override - public RESULT visitFunctionCall(FunctionCall expr, CONTEXT context) { + public RESULT visitFunctionCall(StaticFunctionCall expr, CONTEXT context) { return visitChildren(expr, context); } @@ -242,7 +244,7 @@ public RESULT visitMultiplication(Multiplication expr, CONTEXT context) { } @Override - public RESULT visitName(Name expr, CONTEXT context) { + public RESULT visitName(NameTest expr, CONTEXT context) { return defaultResult(); } @@ -257,7 +259,7 @@ public RESULT visitOr(Or expr, CONTEXT context) { } @Override - public RESULT visitPredicate(Predicate expr, CONTEXT context) { + public RESULT visitPredicate(PredicateExpression expr, CONTEXT context) { return visitChildren(expr, context); } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/AbstractNamedInstanceExpression.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/AbstractNamedInstanceExpression.java index 69d79049f..1c00129d1 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/AbstractNamedInstanceExpression.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/AbstractNamedInstanceExpression.java @@ -27,6 +27,8 @@ package gov.nist.secauto.metaschema.core.metapath.cst; import gov.nist.secauto.metaschema.core.metapath.cst.path.AbstractPathExpression; +import gov.nist.secauto.metaschema.core.metapath.cst.path.NameTest; +import gov.nist.secauto.metaschema.core.metapath.cst.path.Wildcard; import gov.nist.secauto.metaschema.core.metapath.item.node.INodeItem; import java.util.List; @@ -50,7 +52,7 @@ public AbstractNamedInstanceExpression(@NonNull IExpression test) { } /** - * Get the {@link Wildcard} or {@link Name} test. + * Get the {@link Wildcard} or {@link NameTest} test. * * @return the test */ diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/BuildCSTVisitor.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/BuildCSTVisitor.java index 157a3875a..3cd8b46f8 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/BuildCSTVisitor.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/BuildCSTVisitor.java @@ -26,6 +26,8 @@ package gov.nist.secauto.metaschema.core.metapath.cst; +import gov.nist.secauto.metaschema.core.metapath.EQNameUtils; +import gov.nist.secauto.metaschema.core.metapath.StaticContext; import gov.nist.secauto.metaschema.core.metapath.antlr.Metapath10.AbbrevforwardstepContext; import gov.nist.secauto.metaschema.core.metapath.antlr.Metapath10.AbbrevreversestepContext; import gov.nist.secauto.metaschema.core.metapath.antlr.Metapath10.AdditiveexprContext; @@ -48,6 +50,8 @@ import gov.nist.secauto.metaschema.core.metapath.antlr.Metapath10.LetexprContext; import gov.nist.secauto.metaschema.core.metapath.antlr.Metapath10.LiteralContext; import gov.nist.secauto.metaschema.core.metapath.antlr.Metapath10.MultiplicativeexprContext; +import gov.nist.secauto.metaschema.core.metapath.antlr.Metapath10.NametestContext; +import gov.nist.secauto.metaschema.core.metapath.antlr.Metapath10.NodetestContext; import gov.nist.secauto.metaschema.core.metapath.antlr.Metapath10.NumericliteralContext; import gov.nist.secauto.metaschema.core.metapath.antlr.Metapath10.OrexprContext; import gov.nist.secauto.metaschema.core.metapath.antlr.Metapath10.ParenthesizedexprContext; @@ -81,14 +85,19 @@ import gov.nist.secauto.metaschema.core.metapath.cst.math.Subtraction; import gov.nist.secauto.metaschema.core.metapath.cst.path.Axis; import gov.nist.secauto.metaschema.core.metapath.cst.path.Flag; +import gov.nist.secauto.metaschema.core.metapath.cst.path.INameTestExpression; +import gov.nist.secauto.metaschema.core.metapath.cst.path.INodeTestExpression; import gov.nist.secauto.metaschema.core.metapath.cst.path.ModelInstance; +import gov.nist.secauto.metaschema.core.metapath.cst.path.NameTest; import gov.nist.secauto.metaschema.core.metapath.cst.path.RelativeDoubleSlashPath; import gov.nist.secauto.metaschema.core.metapath.cst.path.RelativeSlashPath; import gov.nist.secauto.metaschema.core.metapath.cst.path.RootDoubleSlashPath; import gov.nist.secauto.metaschema.core.metapath.cst.path.RootSlashOnlyPath; import gov.nist.secauto.metaschema.core.metapath.cst.path.RootSlashPath; import gov.nist.secauto.metaschema.core.metapath.cst.path.Step; +import gov.nist.secauto.metaschema.core.metapath.cst.path.Wildcard; import gov.nist.secauto.metaschema.core.metapath.function.ComparisonFunctions; +import gov.nist.secauto.metaschema.core.metapath.item.node.IDefinitionNodeItem; import gov.nist.secauto.metaschema.core.util.CollectionUtil; import gov.nist.secauto.metaschema.core.util.ObjectUtils; @@ -103,9 +112,12 @@ import java.util.List; import java.util.ListIterator; import java.util.Map; +import java.util.function.Predicate; import java.util.stream.Collectors; import java.util.stream.Stream; +import javax.xml.namespace.QName; + import edu.umd.cs.findbugs.annotations.NonNull; /** @@ -119,11 +131,21 @@ }) public class BuildCSTVisitor extends AbstractCSTVisitorBase { + @NonNull + private final StaticContext context; + + public BuildCSTVisitor(@NonNull StaticContext context) { + this.context = context; + } /* ============================================================ * Expressions - https://www.w3.org/TR/xpath-31/#id-expressions * ============================================================ */ + @NonNull + protected StaticContext getContext() { + return context; + } @Override protected IExpression handleExpr(ExprContext ctx) { @@ -170,9 +192,145 @@ protected IExpression handleNumericLiteral(NumericliteralContext ctx) { @Override protected IExpression handleVarref(VarrefContext ctx) { - Name varName = (Name) ctx.varname().accept(this); - assert varName != null; - return new VariableReference(varName); + return new VariableReference( + EQNameUtils.parseName( + ctx.varname().eqname().getText(), + getContext().getVariablePrefixResolver())); + } + + /* ==================================================================== + * For Expressions - https://www.w3.org/TR/xpath-31/#id-for-expressions + * ==================================================================== + */ + + @SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops") + @Override + protected IExpression handleForexpr(ForexprContext ctx) { + SimpleforclauseContext simpleForClause = ctx.simpleforclause(); + + // for SimpleForBinding ("," SimpleForBinding)* + int bindingCount = simpleForClause.getChildCount() / 2; + + @NonNull IExpression retval = ObjectUtils.notNull(ctx.exprsingle().accept(this)); + + // step through in reverse + for (int idx = bindingCount - 1; idx >= 0; idx--) { + SimpleforbindingContext simpleForBinding = simpleForClause.simpleforbinding(idx); + + VarnameContext varName = simpleForBinding.varname(); + ExprsingleContext exprSingle = simpleForBinding.exprsingle(); + + IExpression boundExpression = exprSingle.accept(this); + assert boundExpression != null; + + QName qname = EQNameUtils.parseName( + varName.eqname().getText(), + getContext().getVariablePrefixResolver()); + + Let.VariableDeclaration variable = new Let.VariableDeclaration(qname, boundExpression); + + retval = new For(variable, retval); + } + return retval; + } + + /* ==================================================================== + * Let Expressions - https://www.w3.org/TR/xpath-31/#id-let-expressions + * ==================================================================== + */ + + @Override + protected IExpression handleLet(LetexprContext context) { + @NonNull IExpression retval = ObjectUtils.notNull(context.exprsingle().accept(this)); + + SimpleletclauseContext letClause = context.simpleletclause(); + List clauses = letClause.simpleletbinding(); + + ListIterator reverseListIterator = clauses.listIterator(clauses.size()); + while (reverseListIterator.hasPrevious()) { + SimpleletbindingContext simpleCtx = reverseListIterator.previous(); + + IExpression boundExpression = simpleCtx.exprsingle().accept(this); + assert boundExpression != null; + + QName varName = EQNameUtils.parseName( + simpleCtx.varname().eqname().getText(), + getContext().getVariablePrefixResolver()); + + retval = new Let(varName, boundExpression, retval); // NOPMD intended + } + return retval; + } + + /* ================================================================================== + * Quantified Expressions - https://www.w3.org/TR/xpath-31/#id-quantified-expressions + * ================================================================================== + */ + + @Override + protected IExpression handleQuantifiedexpr(QuantifiedexprContext ctx) { + Quantified.Quantifier quantifier; + int type = ((TerminalNode) ctx.getChild(0)).getSymbol().getType(); + switch (type) { + case Metapath10Lexer.KW_SOME: + quantifier = Quantified.Quantifier.SOME; + break; + case Metapath10Lexer.KW_EVERY: + quantifier = Quantified.Quantifier.EVERY; + break; + default: + throw new UnsupportedOperationException(((TerminalNode) ctx.getChild(0)).getSymbol().getText()); + } + + int numVars = (ctx.getChildCount() - 2) / 5; // children - "satisfies expr" / ", $ varName in expr" + Map vars = new LinkedHashMap<>(); // NOPMD ordering needed + int offset = 0; + for (; offset < numVars; offset++) { + // $ + QName varName = EQNameUtils.parseName( + ctx.varname(offset).eqname().getText(), + getContext().getVariablePrefixResolver()); + + // in + IExpression varExpr = visit(ctx.exprsingle(offset)); + + vars.put(varName, varExpr); + } + + IExpression satisfies = visit(ctx.exprsingle(offset)); + + return new Quantified(quantifier, vars, satisfies); + } + + /* ======================================================================= + * Arrow operator (=>) - https://www.w3.org/TR/xpath-31/#id-arrow-operator + * ======================================================================= + */ + + @Override + protected IExpression handleArrowexpr(ArrowexprContext context) { + // TODO: handle additional syntax for varef and parenthesized + return handleGroupedNAiry(context, 0, 3, (ctx, idx, left) -> { + // the next child is "=>" + assert "=>".equals(ctx.getChild(idx).getText()); + + int offset = (idx - 1) / 3; + + ArrowfunctionspecifierContext fcCtx = ctx.getChild(ArrowfunctionspecifierContext.class, offset); + ArgumentlistContext argumentCtx = ctx.getChild(ArgumentlistContext.class, offset); + + QName name = EQNameUtils.parseName( + fcCtx.eqname().getText(), + getContext().getFunctionPrefixResolver()); + + try (Stream args = Stream.concat( + Stream.of(left), + parseArgumentList(ObjectUtils.notNull(argumentCtx)))) { + assert args != null; + + return new StaticFunctionCall(name, ObjectUtils.notNull(args.collect(Collectors.toUnmodifiableList()))); + } + }); } /* ================================================================================= @@ -229,13 +387,11 @@ protected Stream parseArgumentList(@NonNull ArgumentlistContext con @Override protected IExpression handleFunctioncall(FunctioncallContext ctx) { - EqnameContext nameCtx = ctx.eqname(); - String name = nameCtx.getText(); - - assert name != null; - - return new FunctionCall( - name, + QName qname = EQNameUtils.parseName( + ctx.eqname().getText(), + getContext().getFunctionPrefixResolver()); + return new StaticFunctionCall( + qname, ObjectUtils.notNull(parseArgumentList(ObjectUtils.notNull(ctx.argumentlist())) .collect(Collectors.toUnmodifiableList()))); } @@ -301,7 +457,7 @@ protected IExpression handlePostfixexpr(PostfixexprContext ctx) { List predicates = numChildren > 1 ? parsePredicates(ctx, 1) : CollectionUtil.emptyList(); if (!predicates.isEmpty()) { - retval = new Predicate(retval, predicates); + retval = new PredicateExpression(retval, predicates); } return retval; } @@ -379,28 +535,40 @@ protected IExpression handleRelativepathexpr(RelativepathexprContext context) { @Override protected IExpression handleForwardstep(ForwardstepContext ctx) { - assert ctx.getChildCount() == 2; + AbbrevforwardstepContext abbrev = ctx.abbrevforwardstep(); - Token token = (Token) ctx.forwardaxis().getChild(0).getPayload(); + Step retval; + if (abbrev == null) { + assert ctx.getChildCount() == 2; - Axis axis; - switch (token.getType()) { - case Metapath10Lexer.KW_SELF: - axis = Axis.SELF; - break; - case Metapath10Lexer.KW_CHILD: - axis = Axis.CHILDREN; - break; - case Metapath10Lexer.KW_DESCENDANT: - axis = Axis.DESCENDANT; - break; - case Metapath10Lexer.KW_DESCENDANT_OR_SELF: - axis = Axis.DESCENDANT_OR_SELF; - break; - default: - throw new UnsupportedOperationException(token.getText()); + Token token = (Token) ctx.forwardaxis().getChild(0).getPayload(); + + Axis axis; + switch (token.getType()) { + case Metapath10Lexer.KW_SELF: + axis = Axis.SELF; + break; + case Metapath10Lexer.KW_CHILD: + axis = Axis.CHILDREN; + break; + case Metapath10Lexer.KW_DESCENDANT: + axis = Axis.DESCENDANT; + break; + case Metapath10Lexer.KW_DESCENDANT_OR_SELF: + axis = Axis.DESCENDANT_OR_SELF; + break; + default: + throw new UnsupportedOperationException(token.getText()); + } + retval = new Step(axis, parseNodeTest(ctx.nodetest(), false)); + } else { + retval = new Step( + Axis.CHILDREN, + parseNodeTest( + ctx.nodetest(), + abbrev.AT() != null)); } - return new Step(axis, visit(ctx.nametest())); + return retval; } @Override @@ -423,7 +591,64 @@ protected IExpression handleReversestep(ReversestepContext ctx) { default: throw new UnsupportedOperationException(token.getText()); } - return new Step(axis, visit(ctx.nametest())); + return new Step(axis, parseNodeTest(ctx.nodetest(), false)); + } + + /* ======================================================= + * Node Tests - https://www.w3.org/TR/xpath-31/#node-tests + * ======================================================= + */ + + /* ======================================================= + * Node Tests - https://www.w3.org/TR/xpath-31/#node-tests + * ======================================================= + */ + + protected INodeTestExpression parseNodeTest(NodetestContext ctx, boolean flag) { + // TODO: implement kind test + NametestContext nameTestCtx = ctx.nametest(); + return parseNameTest(nameTestCtx, flag); + } + + protected INameTestExpression parseNameTest(NametestContext ctx, boolean flag) { + ParseTree testType = ObjectUtils.requireNonNull(ctx.getChild(0)); + INameTestExpression retval; + if (testType instanceof EqnameContext) { + QName qname = EQNameUtils.parseName( + ctx.eqname().getText(), + flag ? getContext().getFlagPrefixResolver() : getContext().getModelPrefixResolver()); + retval = new NameTest(qname); + } else { // wildcard + retval = handleWildcard((WildcardContext) testType); + } + return retval; + } + + @Override + protected Wildcard handleWildcard(WildcardContext ctx) { + Predicate> matcher = null; + TerminalNode node; + if ((node = ctx.STAR()) == null) { + if ((node = ctx.CS()) != null) { + // specified prefix, any local-name + String prefix = ctx.NCName().getText(); + String namespace = getContext().lookupNamespaceForPrefix(prefix); + if (namespace == null) { + throw new IllegalStateException(String.format("Prefix '%s' did not map to a namespace.", prefix)); + } + matcher = new Wildcard.MatchAnyLocalName(namespace); + } else if ((node = ctx.SC()) != null) { + // any prefix, specified local-name + matcher = new Wildcard.MatchAnyNamespace(ctx.NCName().getText()); + } else { + // specified braced namespace, any local-name + String bracedUriLiteral = ctx.BracedURILiteral().getText(); + String namespace = bracedUriLiteral.substring(2, bracedUriLiteral.length() - 1); + matcher = new Wildcard.MatchAnyLocalName(namespace); + } + } // star needs no matcher: any prefix, any local-name + + return new Wildcard(matcher); } /* ====================================================================== @@ -439,27 +664,7 @@ protected IExpression handleAxisstep(AxisstepContext ctx) { List predicates = parsePredicates(predicateTree, 0); - return predicates.isEmpty() ? step : new Predicate(step, predicates); - } - - /* ======================================================= - * Node Tests - https://www.w3.org/TR/xpath-31/#node-tests - * ======================================================= - */ - - @Override - protected IExpression handleEqname(EqnameContext ctx) { - ParseTree tree = ctx.getChild(0); - String name = ((TerminalNode) tree).getText(); - - assert name != null; - - return new Name(name); - } - - @Override - protected IExpression handleWildcard(WildcardContext ctx) { - return new Wildcard(); + return predicates.isEmpty() ? step : new PredicateExpression(step, predicates); } /* =========================================================== @@ -473,10 +678,10 @@ protected IExpression handleAbbrevforwardstep(AbbrevforwardstepContext ctx) { IExpression retval; if (numChildren == 1) { - retval = new ModelInstance(visit(ctx.getChild(0))); + retval = new ModelInstance(parseNodeTest(ctx.nodetest(), false)); } else { // this is an AT test - retval = new Flag(visit(ctx.getChild(1))); + retval = new Flag(parseNodeTest(ctx.nodetest(), true)); } return retval; } @@ -740,68 +945,6 @@ protected IExpression handleAndexpr(AndexprContext ctx) { }); } - /* ==================================================================== - * For Expressions - https://www.w3.org/TR/xpath-31/#id-for-expressions - * ==================================================================== - */ - - @SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops") - @Override - protected IExpression handleForexpr(ForexprContext ctx) { - SimpleforclauseContext simpleForClause = ctx.simpleforclause(); - - // for SimpleForBinding ("," SimpleForBinding)* - int bindingCount = simpleForClause.getChildCount() / 2; - - @NonNull IExpression retval = ObjectUtils.notNull(ctx.exprsingle().accept(this)); - - // step through in reverse - for (int idx = bindingCount - 1; idx >= 0; idx--) { - SimpleforbindingContext simpleForBinding = simpleForClause.simpleforbinding(idx); - - VarnameContext varName = simpleForBinding.varname(); - ExprsingleContext exprSingle = simpleForBinding.exprsingle(); - - Name name = (Name) varName.accept(this); - IExpression boundExpression = exprSingle.accept(this); - - assert name != null; - assert boundExpression != null; - - Let.VariableDeclaration variable = new Let.VariableDeclaration(name, boundExpression); - - retval = new For(variable, retval); - } - return retval; - } - - /* ==================================================================== - * Let Expressions - https://www.w3.org/TR/xpath-31/#id-let-expressions - * ==================================================================== - */ - - @Override - protected IExpression handleLet(LetexprContext context) { - @NonNull IExpression retval = ObjectUtils.notNull(context.exprsingle().accept(this)); - - SimpleletclauseContext letClause = context.simpleletclause(); - List clauses = letClause.simpleletbinding(); - - ListIterator reverseListIterator = clauses.listIterator(clauses.size()); - while (reverseListIterator.hasPrevious()) { - SimpleletbindingContext simpleCtx = reverseListIterator.previous(); - - Name varName = (Name) simpleCtx.varname().accept(this); - IExpression boundExpression = simpleCtx.exprsingle().accept(this); - - assert varName != null; - assert boundExpression != null; - - retval = new Let(varName, boundExpression, retval); // NOPMD intended - } - return retval; - } - /* ========================================================================= * Conditional Expressions - https://www.w3.org/TR/xpath-31/#id-conditionals * ========================================================================= @@ -816,43 +959,6 @@ protected IExpression handleIfexpr(IfexprContext ctx) { return new If(testExpr, thenExpr, elseExpr); } - /* ================================================================================== - * Quantified Expressions - https://www.w3.org/TR/xpath-31/#id-quantified-expressions - * ================================================================================== - */ - - @Override - protected IExpression handleQuantifiedexpr(QuantifiedexprContext ctx) { - Quantified.Quantifier quantifier; - int type = ((TerminalNode) ctx.getChild(0)).getSymbol().getType(); - switch (type) { - case Metapath10Lexer.KW_SOME: - quantifier = Quantified.Quantifier.SOME; - break; - case Metapath10Lexer.KW_EVERY: - quantifier = Quantified.Quantifier.EVERY; - break; - default: - throw new UnsupportedOperationException(((TerminalNode) ctx.getChild(0)).getSymbol().getText()); - } - - int numVars = (ctx.getChildCount() - 2) / 5; // children - "satisfies expr" / ", $ varName in expr" - Map vars = new LinkedHashMap<>(); // NOPMD ordering needed - int offset = 0; - for (; offset < numVars; offset++) { - // $ - String varName = ((Name) visit(ctx.varname(offset))).getValue(); - // in - IExpression varExpr = visit(ctx.exprsingle(offset)); - - vars.put(varName, varExpr); - } - - IExpression satisfies = visit(ctx.exprsingle(offset)); - - return new Quantified(quantifier, vars, satisfies); - } - /* ========================================================================= * Simple map operator (!) - https://www.w3.org/TR/xpath-31/#id-map-operator * ========================================================================= @@ -868,34 +974,4 @@ protected IExpression handleSimplemapexpr(SimplemapexprContext context) { return new SimpleMap(left, right); }); } - - /* ======================================================================= - * Arrow operator (=>) - https://www.w3.org/TR/xpath-31/#id-arrow-operator - * ======================================================================= - */ - - @Override - protected IExpression handleArrowexpr(ArrowexprContext context) { - // TODO: handle additional syntax for varef and parenthesized - return handleGroupedNAiry(context, 0, 3, (ctx, idx, left) -> { - // the next child is "=>" - assert "=>".equals(ctx.getChild(idx).getText()); - - int offset = (idx - 1) / 3; - - ArrowfunctionspecifierContext fcCtx = ctx.getChild(ArrowfunctionspecifierContext.class, offset); - ArgumentlistContext argumentCtx = ctx.getChild(ArgumentlistContext.class, offset); - // QName name = toQName( - String name = fcCtx.eqname().getText(); - assert name != null; - - try (Stream args = Stream.concat( - Stream.of(left), - parseArgumentList(ObjectUtils.notNull(argumentCtx)))) { - assert args != null; - - return new FunctionCall(name, ObjectUtils.notNull(args.collect(Collectors.toUnmodifiableList()))); - } - }); - } } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/CSTPrinter.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/CSTPrinter.java index c5be781ad..635edacb7 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/CSTPrinter.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/CSTPrinter.java @@ -37,12 +37,14 @@ import gov.nist.secauto.metaschema.core.metapath.cst.path.Axis; import gov.nist.secauto.metaschema.core.metapath.cst.path.Flag; import gov.nist.secauto.metaschema.core.metapath.cst.path.ModelInstance; +import gov.nist.secauto.metaschema.core.metapath.cst.path.NameTest; import gov.nist.secauto.metaschema.core.metapath.cst.path.RelativeDoubleSlashPath; import gov.nist.secauto.metaschema.core.metapath.cst.path.RelativeSlashPath; import gov.nist.secauto.metaschema.core.metapath.cst.path.RootDoubleSlashPath; import gov.nist.secauto.metaschema.core.metapath.cst.path.RootSlashOnlyPath; import gov.nist.secauto.metaschema.core.metapath.cst.path.RootSlashPath; import gov.nist.secauto.metaschema.core.metapath.cst.path.Step; +import gov.nist.secauto.metaschema.core.metapath.cst.path.Wildcard; import edu.umd.cs.findbugs.annotations.NonNull; import edu.umd.cs.findbugs.annotations.Nullable; @@ -180,7 +182,7 @@ public String visitFlag(Flag expr, State context) { } @Override - public String visitFunctionCall(FunctionCall expr, State context) { + public String visitFunctionCall(StaticFunctionCall expr, State context) { return appendNode(expr, super.visitFunctionCall(expr, context), context); } @@ -220,7 +222,7 @@ public String visitMultiplication(Multiplication expr, State context) { } @Override - public String visitName(Name expr, State context) { + public String visitName(NameTest expr, State context) { return appendNode(expr, super.visitName(expr, context), context); } @@ -240,7 +242,7 @@ public String visitAxis(Axis expr, State context) { } @Override - public String visitPredicate(Predicate expr, State context) { + public String visitPredicate(PredicateExpression expr, State context) { return appendNode(expr, super.visitPredicate(expr, context), context); } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/For.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/For.java index 41d33a16f..186aed9da 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/For.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/For.java @@ -79,7 +79,7 @@ public ISequence accept(DynamicContext dynamicContext, ISequenc List retval = new LinkedList<>(); for (IItem item : variableResult) { - subDynamicContext.bindVariableValue(variable.getName().getValue(), ISequence.of(item)); + subDynamicContext.bindVariableValue(variable.getName(), ISequence.of(item)); retval.addAll(getReturnExpression().accept(subDynamicContext, focus)); } return ISequence.of(retval); diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/IExpressionVisitor.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/IExpressionVisitor.java index 054fd958b..d0642f479 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/IExpressionVisitor.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/IExpressionVisitor.java @@ -37,12 +37,14 @@ import gov.nist.secauto.metaschema.core.metapath.cst.path.Axis; import gov.nist.secauto.metaschema.core.metapath.cst.path.Flag; import gov.nist.secauto.metaschema.core.metapath.cst.path.ModelInstance; +import gov.nist.secauto.metaschema.core.metapath.cst.path.NameTest; import gov.nist.secauto.metaschema.core.metapath.cst.path.RelativeDoubleSlashPath; import gov.nist.secauto.metaschema.core.metapath.cst.path.RelativeSlashPath; import gov.nist.secauto.metaschema.core.metapath.cst.path.RootDoubleSlashPath; import gov.nist.secauto.metaschema.core.metapath.cst.path.RootSlashOnlyPath; import gov.nist.secauto.metaschema.core.metapath.cst.path.RootSlashPath; import gov.nist.secauto.metaschema.core.metapath.cst.path.Step; +import gov.nist.secauto.metaschema.core.metapath.cst.path.Wildcard; import edu.umd.cs.findbugs.annotations.NonNull; @@ -188,7 +190,7 @@ public interface IExpressionVisitor { * the processing context * @return the visitation result or {@code null} if no result was produced */ - RESULT visitFunctionCall(@NonNull FunctionCall expr, @NonNull CONTEXT context); + RESULT visitFunctionCall(@NonNull StaticFunctionCall expr, @NonNull CONTEXT context); /** * Visit the CST node. @@ -276,7 +278,7 @@ public interface IExpressionVisitor { * the processing context * @return the visitation result or {@code null} if no result was produced */ - RESULT visitName(@NonNull Name expr, @NonNull CONTEXT context); + RESULT visitName(@NonNull NameTest expr, @NonNull CONTEXT context); /** * Visit the CST node. @@ -309,7 +311,7 @@ public interface IExpressionVisitor { * the processing context * @return the visitation result or {@code null} if no result was produced */ - RESULT visitPredicate(@NonNull Predicate expr, @NonNull CONTEXT context); + RESULT visitPredicate(@NonNull PredicateExpression expr, @NonNull CONTEXT context); /** * Visit the CST node. diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/Let.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/Let.java index bd7389bb4..0d2f3de44 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/Let.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/Let.java @@ -33,6 +33,8 @@ import java.util.List; +import javax.xml.namespace.QName; + import edu.umd.cs.findbugs.annotations.NonNull; /** @@ -57,7 +59,7 @@ public class Let implements IExpression { * @param returnExpression * the inner expression to evaluate with the variable in-scope */ - public Let(@NonNull Name name, @NonNull IExpression boundExpression, @NonNull IExpression returnExpression) { + public Let(@NonNull QName name, @NonNull IExpression boundExpression, @NonNull IExpression returnExpression) { this.variable = new VariableDeclaration(name, boundExpression); this.returnExpression = returnExpression; } @@ -104,11 +106,11 @@ public ISequence accept(DynamicContext dynamicContext, ISequenc public static class VariableDeclaration { @NonNull - private final Name name; + private final QName name; @NonNull private final IExpression boundExpression; - public VariableDeclaration(@NonNull Name name, @NonNull IExpression boundExpression) { + public VariableDeclaration(@NonNull QName name, @NonNull IExpression boundExpression) { this.name = name; this.boundExpression = boundExpression; } @@ -119,7 +121,7 @@ public VariableDeclaration(@NonNull Name name, @NonNull IExpression boundExpress * @return the variable name */ @NonNull - public Name getName() { + public QName getName() { return name; } @@ -139,9 +141,7 @@ public void bind( @NonNull DynamicContext boundContext) { ISequence result = getBoundExpression().accept(evalContext, focus); - - String name = getName().getValue(); - boundContext.bindVariableValue(name, result); + boundContext.bindVariableValue(getName(), result); } } } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/Predicate.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/PredicateExpression.java similarity index 97% rename from core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/Predicate.java rename to core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/PredicateExpression.java index 7881452e7..b51633ba2 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/Predicate.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/PredicateExpression.java @@ -42,7 +42,7 @@ import edu.umd.cs.findbugs.annotations.NonNull; -public class Predicate implements IExpression { +public class PredicateExpression implements IExpression { @NonNull private final IExpression base; @NonNull @@ -56,7 +56,7 @@ public class Predicate implements IExpression { * @param predicates * the expression(s) to apply as a filter */ - public Predicate(@NonNull IExpression base, @NonNull List predicates) { + public PredicateExpression(@NonNull IExpression base, @NonNull List predicates) { this.base = base; this.predicates = predicates; } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/Quantified.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/Quantified.java index 04093a970..8a414cee0 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/Quantified.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/Quantified.java @@ -44,6 +44,8 @@ import java.util.stream.Collectors; import java.util.stream.Stream; +import javax.xml.namespace.QName; + import edu.umd.cs.findbugs.annotations.NonNull; public class Quantified @@ -56,7 +58,7 @@ public enum Quantifier { @NonNull private final Quantifier quantifier; @NonNull - private final Map inClauses; + private final Map inClauses; @NonNull private final IExpression satisfies; @@ -74,7 +76,7 @@ public enum Quantifier { */ public Quantified( @NonNull Quantifier quantifier, - @NonNull Map inClauses, + @NonNull Map inClauses, @NonNull IExpression satisfies) { this.quantifier = quantifier; this.inClauses = inClauses; @@ -98,7 +100,7 @@ public Quantifier getQuantifier() { * @return the variable names mapped to the associated Metapath expression */ @NonNull - public Map getInClauses() { + public Map getInClauses() { return inClauses; } @@ -122,20 +124,20 @@ public List getChildren() { @SuppressWarnings("PMD.SystemPrintln") @Override public ISequence accept(DynamicContext dynamicContext, ISequence focus) { - Map> clauses = getInClauses().entrySet().stream() + Map> clauses = getInClauses().entrySet().stream() .map(entry -> Map.entry( entry.getKey(), entry.getValue().accept(dynamicContext, focus))) .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new)); - List clauseKeys = new ArrayList<>(clauses.keySet()); + List clauseKeys = new ArrayList<>(clauses.keySet()); List> clauseValues = new ArrayList<>(clauses.values()); boolean retval = true; for (List product : new CartesianProduct<>(clauseValues)) { DynamicContext subDynamicContext = dynamicContext.subContext(); for (int idx = 0; idx < product.size(); idx++) { - String var = clauseKeys.get(idx); + QName var = clauseKeys.get(idx); IItem item = product.get(idx); assert var != null; diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/FunctionCall.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/StaticFunctionCall.java similarity index 95% rename from core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/FunctionCall.java rename to core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/StaticFunctionCall.java index dbdcf6ea9..9660aeaa3 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/FunctionCall.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/StaticFunctionCall.java @@ -38,11 +38,13 @@ import java.util.Objects; import java.util.stream.Collectors; +import javax.xml.namespace.QName; + import edu.umd.cs.findbugs.annotations.NonNull; -public class FunctionCall implements IExpression { +public class StaticFunctionCall implements IExpression { @NonNull - private final String name; + private final QName name; @NonNull private final List arguments; private IFunction function; @@ -55,7 +57,7 @@ public class FunctionCall implements IExpression { * @param arguments * the expressions used to provide arguments to the function call */ - public FunctionCall(@NonNull String name, @NonNull List arguments) { + public StaticFunctionCall(@NonNull QName name, @NonNull List arguments) { this.name = Objects.requireNonNull(name, "name"); this.arguments = Objects.requireNonNull(arguments, "arguments"); } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/VariableReference.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/VariableReference.java index 9cd5beb7e..959861c84 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/VariableReference.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/VariableReference.java @@ -34,6 +34,8 @@ import java.util.List; +import javax.xml.namespace.QName; + import edu.umd.cs.findbugs.annotations.NonNull; /** @@ -43,7 +45,7 @@ */ public class VariableReference implements IExpression { @NonNull - private final Name name; + private final QName name; /** * Construct a new Metapath variable reference CST node. @@ -51,7 +53,7 @@ public class VariableReference implements IExpression { * @param name * the variable name */ - public VariableReference(@NonNull Name name) { + public VariableReference(@NonNull QName name) { this.name = name; } @@ -61,7 +63,7 @@ public VariableReference(@NonNull Name name) { * @return the variable name */ @NonNull - public Name getName() { + public QName getName() { return name; } @@ -73,7 +75,7 @@ public List getChildren() { @SuppressWarnings("null") @Override public String toASTString() { - return String.format("%s[name=%s]", getClass().getName(), getName().getValue()); + return String.format("%s[name=%s]", getClass().getName(), getName()); } @Override @@ -83,7 +85,7 @@ public RESULT accept(IExpressionVisitor visit @Override public ISequence accept(DynamicContext dynamicContext, ISequence focus) { - return dynamicContext.getVariableValue(ObjectUtils.notNull(getName().getValue())); + return dynamicContext.getVariableValue(ObjectUtils.notNull(getName())); } } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/path/Flag.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/path/Flag.java index 60d9e2a46..8772aaa78 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/path/Flag.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/path/Flag.java @@ -31,13 +31,14 @@ import gov.nist.secauto.metaschema.core.metapath.cst.AbstractNamedInstanceExpression; import gov.nist.secauto.metaschema.core.metapath.cst.IExpression; import gov.nist.secauto.metaschema.core.metapath.cst.IExpressionVisitor; -import gov.nist.secauto.metaschema.core.metapath.cst.Name; import gov.nist.secauto.metaschema.core.metapath.item.ItemUtils; import gov.nist.secauto.metaschema.core.metapath.item.node.IFlagNodeItem; import gov.nist.secauto.metaschema.core.metapath.item.node.INodeItem; import java.util.stream.Stream; +import javax.xml.namespace.QName; + import edu.umd.cs.findbugs.annotations.NonNull; public class Flag // NOPMD - intentional name @@ -87,8 +88,8 @@ public ISequence accept( @NonNull protected Stream match(@NonNull INodeItem focusedItem) { Stream retval; - if (getTest() instanceof Name) { - String name = ((Name) getTest()).getValue(); + if (getTest() instanceof NameTest) { + QName name = ((NameTest) getTest()).getName(); IFlagNodeItem item = focusedItem.getFlagByName(name); retval = item == null ? Stream.empty() : Stream.of(item); diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/Wildcard.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/path/INameTestExpression.java similarity index 67% rename from core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/Wildcard.java rename to core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/path/INameTestExpression.java index 087e7cdf5..992a5211e 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/Wildcard.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/path/INameTestExpression.java @@ -24,47 +24,28 @@ * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. */ -package gov.nist.secauto.metaschema.core.metapath.cst; +package gov.nist.secauto.metaschema.core.metapath.cst.path; -import gov.nist.secauto.metaschema.core.metapath.DynamicContext; -import gov.nist.secauto.metaschema.core.metapath.ISequence; -import gov.nist.secauto.metaschema.core.metapath.item.ItemUtils; +import gov.nist.secauto.metaschema.core.metapath.cst.IExpression; import gov.nist.secauto.metaschema.core.metapath.item.node.INodeItem; import java.util.Collections; import java.util.List; -/** - * The CST node for a Metapath - * wildcard name - * test. - */ -public class Wildcard implements IExpression { +public interface INameTestExpression extends INodeTestExpression { @SuppressWarnings("null") @Override - public List getChildren() { + default List getChildren() { return Collections.emptyList(); } @Override - public Class getBaseResultType() { + default Class getBaseResultType() { return INodeItem.class; } @Override - public Class getStaticResultType() { + default Class getStaticResultType() { return getBaseResultType(); } - - @Override - public RESULT accept(IExpressionVisitor visitor, CONTEXT context) { - return visitor.visitWildcard(this, context); - } - - @Override - public ISequence accept( - DynamicContext dynamicContext, ISequence focus) { - return ISequence.of(focus.asStream() - .map(item -> ItemUtils.checkItemIsNodeItemForStep(item))); - } } diff --git a/databind/src/test/java/gov/nist/secauto/metaschema/databind/testing/model/package-info.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/path/INodeTestExpression.java similarity index 88% rename from databind/src/test/java/gov/nist/secauto/metaschema/databind/testing/model/package-info.java rename to core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/path/INodeTestExpression.java index 6cafc85cb..b5bd5bed1 100644 --- a/databind/src/test/java/gov/nist/secauto/metaschema/databind/testing/model/package-info.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/path/INodeTestExpression.java @@ -24,4 +24,10 @@ * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. */ -package gov.nist.secauto.metaschema.databind.testing.model; +package gov.nist.secauto.metaschema.core.metapath.cst.path; + +import gov.nist.secauto.metaschema.core.metapath.cst.IExpression; + +public interface INodeTestExpression extends IExpression { + // no additional methods +} diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/path/ModelInstance.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/path/ModelInstance.java index f63c0a7f6..4ae747583 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/path/ModelInstance.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/path/ModelInstance.java @@ -31,7 +31,6 @@ import gov.nist.secauto.metaschema.core.metapath.cst.AbstractNamedInstanceExpression; import gov.nist.secauto.metaschema.core.metapath.cst.IExpression; import gov.nist.secauto.metaschema.core.metapath.cst.IExpressionVisitor; -import gov.nist.secauto.metaschema.core.metapath.cst.Name; import gov.nist.secauto.metaschema.core.metapath.item.ItemUtils; import gov.nist.secauto.metaschema.core.metapath.item.node.IModelNodeItem; import gov.nist.secauto.metaschema.core.metapath.item.node.INodeItem; @@ -39,6 +38,8 @@ import java.util.List; import java.util.stream.Stream; +import javax.xml.namespace.QName; + import edu.umd.cs.findbugs.annotations.NonNull; @SuppressWarnings("rawtypes") @@ -90,8 +91,8 @@ public RESULT accept(IExpressionVisitor visit protected Stream> match( @NonNull INodeItem focusedItem) { Stream> retval; - if (getTest() instanceof Name) { - String name = ((Name) getTest()).getValue(); + if (getTest() instanceof NameTest) { + QName name = ((NameTest) getTest()).getName(); List> items = focusedItem.getModelItemsByName(name); retval = items.stream(); } else { diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/Name.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/path/NameTest.java similarity index 79% rename from core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/Name.java rename to core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/path/NameTest.java index 2760a5849..8b0d4f5c7 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/Name.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/path/NameTest.java @@ -24,16 +24,16 @@ * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. */ -package gov.nist.secauto.metaschema.core.metapath.cst; +package gov.nist.secauto.metaschema.core.metapath.cst.path; import gov.nist.secauto.metaschema.core.metapath.DynamicContext; import gov.nist.secauto.metaschema.core.metapath.ISequence; +import gov.nist.secauto.metaschema.core.metapath.cst.IExpressionVisitor; import gov.nist.secauto.metaschema.core.metapath.item.ItemUtils; import gov.nist.secauto.metaschema.core.metapath.item.node.IDefinitionNodeItem; import gov.nist.secauto.metaschema.core.metapath.item.node.INodeItem; -import gov.nist.secauto.metaschema.core.util.CollectionUtil; -import java.util.List; +import javax.xml.namespace.QName; import edu.umd.cs.findbugs.annotations.NonNull; @@ -42,20 +42,20 @@ * expanded QName * name test. */ -public class Name // NOPMD - intentional - implements IExpression { +public class NameTest + implements INameTestExpression { @NonNull - private final String value; + private final QName name; /** * Construct a new expanded QName-based literal expression. * - * @param value + * @param name * the literal value */ - public Name(@NonNull String value) { - this.value = value; + public NameTest(@NonNull QName name) { + this.name = name; } /** @@ -64,23 +64,8 @@ public Name(@NonNull String value) { * @return the string value of the name */ @NonNull - public String getValue() { - return value; - } - - @Override - public List getChildren() { - return CollectionUtil.emptyList(); - } - - @Override - public Class getBaseResultType() { - return INodeItem.class; - } - - @Override - public Class getStaticResultType() { - return getBaseResultType(); + public QName getName() { + return name; } @Override @@ -100,12 +85,12 @@ public ISequence accept( @SuppressWarnings("PMD.UnusedPrivateMethod") private boolean match(INodeItem item) { return item instanceof IDefinitionNodeItem - && getValue().equals(((IDefinitionNodeItem) item).getName()); + && getName().equals(((IDefinitionNodeItem) item).getName()); } @SuppressWarnings("null") @Override public String toASTString() { - return String.format("%s[value=%s]", getClass().getName(), getValue()); + return String.format("%s[name=%s]", getClass().getName(), getName()); } } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/path/Step.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/path/Step.java index 41178b2ae..5e398af78 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/path/Step.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/path/Step.java @@ -49,7 +49,7 @@ public class Step implements IExpression { // NOPMD - intentional @NonNull private final Axis axisExpression; @NonNull - private final IExpression stepExpression; + private final INodeTestExpression stepExpression; @NonNull private final Class staticResultType; @@ -62,7 +62,7 @@ public class Step implements IExpression { // NOPMD - intentional * the sub-expression to evaluate before filtering with the predicates */ @SuppressWarnings("null") - public Step(@NonNull Axis axis, @NonNull IExpression step) { + public Step(@NonNull Axis axis, @NonNull INodeTestExpression step) { this.axisExpression = axis; this.stepExpression = step; this.staticResultType = ExpressionUtils.analyzeStaticResultType(IItem.class, List.of(step)); @@ -84,7 +84,7 @@ public Axis getAxis() { * @return the sub-expression */ @NonNull - public IExpression getStep() { + public INodeTestExpression getStep() { return stepExpression; } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/path/Wildcard.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/path/Wildcard.java new file mode 100644 index 000000000..3ac26f514 --- /dev/null +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/path/Wildcard.java @@ -0,0 +1,103 @@ +/* + * Portions of this software was developed by employees of the National Institute + * of Standards and Technology (NIST), an agency of the Federal Government and is + * being made available as a public service. Pursuant to title 17 United States + * Code Section 105, works of NIST employees are not subject to copyright + * protection in the United States. This software may be subject to foreign + * copyright. Permission in the United States and in foreign countries, to the + * extent that NIST may hold copyright, to use, copy, modify, create derivative + * works, and distribute this software and its documentation without fee is hereby + * granted on a non-exclusive basis, provided that this notice and disclaimer + * of warranty appears in all copies. + * + * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER + * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY + * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM + * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE + * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT + * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, + * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, + * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, + * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR + * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT + * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. + */ + +package gov.nist.secauto.metaschema.core.metapath.cst.path; + +import gov.nist.secauto.metaschema.core.metapath.DynamicContext; +import gov.nist.secauto.metaschema.core.metapath.ISequence; +import gov.nist.secauto.metaschema.core.metapath.cst.IExpressionVisitor; +import gov.nist.secauto.metaschema.core.metapath.item.ItemUtils; +import gov.nist.secauto.metaschema.core.metapath.item.node.IDefinitionNodeItem; +import gov.nist.secauto.metaschema.core.metapath.item.node.INodeItem; + +import java.util.function.Predicate; +import java.util.stream.Stream; + +import edu.umd.cs.findbugs.annotations.NonNull; +import edu.umd.cs.findbugs.annotations.Nullable; + +/** + * The CST node for a Metapath + * wildcard name + * test. + */ +public class Wildcard implements INameTestExpression { + @Nullable + private final Predicate> matcher; + + public Wildcard(@Nullable Predicate> matcher) { + this.matcher = matcher; + } + + @Override + public RESULT accept(IExpressionVisitor visitor, CONTEXT context) { + return visitor.visitWildcard(this, context); + } + + @Override + public ISequence accept( + DynamicContext dynamicContext, ISequence focus) { + Stream nodes = focus.asStream() + .map(item -> ItemUtils.checkItemIsNodeItemForStep(item)); + if (matcher != null) { + Predicate> test = matcher; + nodes = nodes.filter(item -> { + assert matcher != null; + return !(item instanceof IDefinitionNodeItem) || + test.test((IDefinitionNodeItem) item); + }); + } + return ISequence.of(nodes); + } + + public static class MatchAnyNamespace implements Predicate> { + @NonNull + private String localName; + + public MatchAnyNamespace(@NonNull String localName) { + this.localName = localName; + } + + @Override + public boolean test(IDefinitionNodeItem item) { + return localName.equals(item.getName().getLocalPart()); + } + } + + public static class MatchAnyLocalName implements Predicate> { + @NonNull + private String namespace; + + public MatchAnyLocalName(@NonNull String namespace) { + this.namespace = namespace; + } + + @Override + public boolean test(IDefinitionNodeItem item) { + return namespace.equals(item.getName().getNamespaceURI()); + } + } +} diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/format/MetapathFormatter.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/format/MetapathFormatter.java index 48af77792..2b1cf765f 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/format/MetapathFormatter.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/format/MetapathFormatter.java @@ -35,6 +35,7 @@ import gov.nist.secauto.metaschema.core.metapath.item.node.IModuleNodeItem; import gov.nist.secauto.metaschema.core.metapath.item.node.INodeItem; import gov.nist.secauto.metaschema.core.metapath.item.node.IRootAssemblyNodeItem; +import gov.nist.secauto.metaschema.core.util.ObjectUtils; import edu.umd.cs.findbugs.annotations.NonNull; @@ -60,7 +61,7 @@ public String formatDocument(IDocumentNodeItem document) { @Override public String formatRootAssembly(IRootAssemblyNodeItem root) { - return root.getName(); + return ObjectUtils.notNull(root.getName().getLocalPart()); } @Override @@ -88,7 +89,7 @@ public String formatFlag(IFlagNodeItem flag) { @SuppressWarnings("null") @NonNull private static String formatModelPathSegment(@NonNull IModelNodeItem item) { - StringBuilder builder = new StringBuilder(item.getName()) + StringBuilder builder = new StringBuilder(item.getName().getLocalPart()) .append('[') .append(item.getPosition()) .append(']'); diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/DefaultFunctionLibrary.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/DefaultFunctionLibrary.java index 21fd664bc..271813910 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/DefaultFunctionLibrary.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/DefaultFunctionLibrary.java @@ -132,6 +132,7 @@ public DefaultFunctionLibrary() { // NOPMD - intentional // https://www.w3.org/TR/xpath-functions-31/#func-month-from-date // https://www.w3.org/TR/xpath-functions-31/#func-month-from-dateTime // https://www.w3.org/TR/xpath-functions-31/#func-months-from-duration + // https://www.w3.org/TR/xpath-functions-31/#func-node-name // https://www.w3.org/TR/xpath-functions-31/#func-normalize-space // https://www.w3.org/TR/xpath-functions-31/#func-normalize-unicode // https://www.w3.org/TR/xpath-functions-31/#func-not @@ -164,6 +165,7 @@ public DefaultFunctionLibrary() { // NOPMD - intentional registerFunction(FnStartsWith.SIGNATURE); // https://www.w3.org/TR/xpath-functions-31/#func-static-base-uri registerFunction(FnStaticBaseUri.SIGNATURE); + // P0: https://www.w3.org/TR/xpath-functions-31/#func-string // P1: https://www.w3.org/TR/xpath-functions-31/#func-string-join // P1: https://www.w3.org/TR/xpath-functions-31/#func-string-length // P1: https://www.w3.org/TR/xpath-functions-31/#func-subsequence diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/item/node/AbstractNodeItemFactory.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/item/node/AbstractNodeItemFactory.java index a77577f63..593a33ad2 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/item/node/AbstractNodeItemFactory.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/item/node/AbstractNodeItemFactory.java @@ -44,6 +44,8 @@ import java.net.URI; import java.util.stream.Stream; +import javax.xml.namespace.QName; + import edu.umd.cs.findbugs.annotations.NonNull; import edu.umd.cs.findbugs.annotations.Nullable; @@ -142,7 +144,7 @@ public IAssemblyNodeItem newAssemblyNodeItem( IAssemblyNodeItem retval = null; if (!instance.getDefinition().isInline()) { // if not inline, need to check for a cycle - IAssemblyNodeItem cycle = getCycledInstance(instance.getEffectiveName(), instance.getDefinition(), parent); + IAssemblyNodeItem cycle = getCycledInstance(instance.getXmlQName(), instance.getDefinition(), parent); if (cycle != null) { // generate a cycle wrapper of the original node item retval = new CycledAssemblyInstanceNodeItemImpl(instance, parent, cycle); @@ -166,18 +168,18 @@ public IAssemblyNodeItem newAssemblyNodeItem( @Nullable private IAssemblyNodeItem getCycledInstance( - @NonNull String effectiveName, + @NonNull QName name, @NonNull IAssemblyDefinition definition, @NonNull IAssemblyNodeItem parent) { IAssemblyNodeItem retval = null; IAssemblyDefinition parentDefinition = parent.getDefinition(); - if (parent.getName().equals(effectiveName) && parentDefinition.equals(definition)) { + if (parent.getName().equals(name) && parentDefinition.equals(definition)) { retval = parent; } else { IAssemblyNodeItem ancestor = parent.getParentContentNodeItem(); if (ancestor != null) { - retval = getCycledInstance(effectiveName, definition, ancestor); + retval = getCycledInstance(name, definition, ancestor); } } return retval; diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/item/node/CycledAssemblyInstanceNodeItemImpl.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/item/node/CycledAssemblyInstanceNodeItemImpl.java index 8b7ef1e64..ab058c0c6 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/item/node/CycledAssemblyInstanceNodeItemImpl.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/item/node/CycledAssemblyInstanceNodeItemImpl.java @@ -32,6 +32,8 @@ import java.util.Collection; import java.util.List; +import javax.xml.namespace.QName; + import edu.umd.cs.findbugs.annotations.NonNull; class CycledAssemblyInstanceNodeItemImpl @@ -70,7 +72,7 @@ public Collection getFlags() { } @Override - public IFlagNodeItem getFlagByName(@NonNull String name) { + public IFlagNodeItem getFlagByName(@NonNull QName name) { return getCycledNodeItem().getFlagByName(name); } @@ -80,7 +82,7 @@ public IFlagNodeItem getFlagByName(@NonNull String name) { } @Override - public List> getModelItemsByName(String name) { + public List> getModelItemsByName(QName name) { return getCycledNodeItem().getModelItemsByName(name); } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/item/node/DefaultNodeItemFactory.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/item/node/DefaultNodeItemFactory.java index 910cbb098..208ccae64 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/item/node/DefaultNodeItemFactory.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/item/node/DefaultNodeItemFactory.java @@ -30,7 +30,6 @@ import gov.nist.secauto.metaschema.core.metapath.item.node.IFeatureModelContainerItem.ModelContainer; import gov.nist.secauto.metaschema.core.model.IAssemblyInstanceGrouped; import gov.nist.secauto.metaschema.core.model.IChoiceGroupInstance; -import gov.nist.secauto.metaschema.core.model.IFlagDefinition; import gov.nist.secauto.metaschema.core.model.IFlagInstance; import gov.nist.secauto.metaschema.core.model.IModelInstance; import gov.nist.secauto.metaschema.core.model.IModule; @@ -45,10 +44,13 @@ import java.util.List; import java.util.Map; import java.util.concurrent.atomic.AtomicInteger; +import java.util.function.Function; import java.util.function.Supplier; import java.util.stream.Collectors; import java.util.stream.Stream; +import javax.xml.namespace.QName; + import edu.umd.cs.findbugs.annotations.NonNull; final class DefaultNodeItemFactory @@ -74,7 +76,7 @@ private DefaultNodeItemFactory() { @NonNull public Supplier newDataModelSupplier(@NonNull IFieldNodeItem item) { return () -> { - Map flags = generateFlags(item); + Map flags = generateFlags(item); return new FlagContainer(flags); }; } @@ -83,8 +85,8 @@ public Supplier newDataModelSupplier(@NonNull IFieldNodeItem item @NonNull public Supplier newDataModelSupplier(@NonNull IAssemblyNodeItem item) { return () -> { - Map flags = generateFlags(item); - Map>> modelItems = generateModelItems(item); + Map flags = generateFlags(item); + Map>> modelItems = generateModelItems(item); return new ModelContainer(flags, modelItems); }; } @@ -92,7 +94,7 @@ public Supplier newDataModelSupplier(@NonNull IAssemblyNodeItem @Override public Supplier newDataModelSupplier(IRootAssemblyNodeItem item) { return () -> { - Map>> modelItems = CollectionUtil.singletonMap( + Map>> modelItems = CollectionUtil.singletonMap( item.getName(), CollectionUtil.singletonList(item)); return new ModelContainer(CollectionUtil.emptyMap(), modelItems); @@ -109,8 +111,8 @@ public Supplier newDataModelSupplier(IRootAssemblyNodeItem item) */ @SuppressWarnings("PMD.UseConcurrentHashMap") // need an ordered Map @NonNull - protected Map generateFlags(@NonNull IModelNodeItem parent) { - Map retval = new LinkedHashMap<>(); + protected Map generateFlags(@NonNull IModelNodeItem parent) { + Map retval = new LinkedHashMap<>(); Object parentValue = parent.getValue(); assert parentValue != null; @@ -118,7 +120,7 @@ protected Map generateFlags(@NonNull IModelNodeItem Object flagValue = instance.getValue(parentValue); if (flagValue != null) { IFlagNodeItem item = newFlagNodeItem(instance, parent, flagValue); - retval.put(instance.getEffectiveName(), item); + retval.put(instance.getXmlQName(), item); } } return retval.isEmpty() ? CollectionUtil.emptyMap() : CollectionUtil.unmodifiableMap(retval); @@ -134,8 +136,8 @@ protected Map generateFlags(@NonNull IModelNodeItem */ @SuppressWarnings("PMD.UseConcurrentHashMap") // need an ordered map @NonNull - protected Map>> generateModelItems(@NonNull IAssemblyNodeItem parent) { - Map>> retval = new LinkedHashMap<>(); + protected Map>> generateModelItems(@NonNull IAssemblyNodeItem parent) { + Map>> retval = new LinkedHashMap<>(); Object parentValue = parent.getValue(); assert parentValue != null; @@ -152,7 +154,7 @@ protected Map generateFlags(@NonNull IModelNodeItem assert itemValue != null; return newModelItem(namedInstance, parent, index.incrementAndGet(), itemValue); }).collect(Collectors.toUnmodifiableList()); - retval.put(namedInstance.getEffectiveName(), items); + retval.put(namedInstance.getXmlQName(), items); } else if (instance instanceof IChoiceGroupInstance) { IChoiceGroupInstance choiceInstance = (IChoiceGroupInstance) instance; @@ -179,7 +181,7 @@ protected Map generateFlags(@NonNull IModelNodeItem assert itemValue != null; return newModelItem(namedInstance, parent, index.incrementAndGet(), itemValue); }).collect(Collectors.toUnmodifiableList()); - retval.put(namedInstance.getEffectiveName(), items); + retval.put(namedInstance.getXmlQName(), items); } } @@ -193,12 +195,13 @@ public Supplier newMetaschemaModelSupplier(@NonNull IModuleNodeI IModule module = item.getModule(); // build flags from Metaschema definitions - Map flags = ObjectUtils.notNull( + Map flags = ObjectUtils.notNull( Collections.unmodifiableMap(module.getExportedFlagDefinitions().stream() + .map(def -> newFlagNodeItem(ObjectUtils.notNull(def), item)) .collect( Collectors.toMap( - IFlagDefinition::getEffectiveName, - def -> newFlagNodeItem(ObjectUtils.notNull(def), item), + IFlagNodeItem::getName, + Function.identity(), (v1, v2) -> v2, LinkedHashMap::new)))); @@ -208,7 +211,7 @@ public Supplier newMetaschemaModelSupplier(@NonNull IModuleNodeI Stream assemblyStream = module.getExportedAssemblyDefinitions().stream() .map(def -> newAssemblyNodeItem(ObjectUtils.notNull(def), item)); - Map>> modelItems + Map>> modelItems = ObjectUtils.notNull(Stream.concat(fieldStream, assemblyStream) .collect( Collectors.collectingAndThen( @@ -221,7 +224,7 @@ public Supplier newMetaschemaModelSupplier(@NonNull IModuleNodeI @Override public Supplier newMetaschemaModelSupplier(@NonNull IFieldNodeItem item) { return () -> { - Map flags = generateMetaschemaFlags(item); + Map flags = generateMetaschemaFlags(item); return new FlagContainer(flags); }; } @@ -230,34 +233,34 @@ public Supplier newMetaschemaModelSupplier(@NonNull IFieldNodeIte public Supplier newMetaschemaModelSupplier( @NonNull IAssemblyNodeItem item) { return () -> { - Map flags = generateMetaschemaFlags(item); - Map>> modelItems = generateMetaschemaModelItems(item); + Map flags = generateMetaschemaFlags(item); + Map>> modelItems = generateMetaschemaModelItems(item); return new ModelContainer(flags, modelItems); }; } @NonNull - protected Map generateMetaschemaFlags( + protected Map generateMetaschemaFlags( @NonNull IModelNodeItem parent) { - Map retval = new LinkedHashMap<>(); // NOPMD - intentional + Map retval = new LinkedHashMap<>(); // NOPMD - intentional for (IFlagInstance instance : parent.getDefinition().getFlagInstances()) { assert instance != null; IFlagNodeItem item = newFlagNodeItem(instance, parent); - retval.put(instance.getEffectiveName(), item); + retval.put(instance.getXmlQName(), item); } return retval.isEmpty() ? CollectionUtil.emptyMap() : CollectionUtil.unmodifiableMap(retval); } @NonNull - protected Map>> generateMetaschemaModelItems( + protected Map>> generateMetaschemaModelItems( @NonNull IAssemblyNodeItem parent) { - Map>> retval = new LinkedHashMap<>(); // NOPMD - intentional + Map>> retval = new LinkedHashMap<>(); // NOPMD - intentional for (INamedModelInstance instance : CollectionUtil.toIterable(getNamedModelInstances(parent.getDefinition()))) { assert instance != null; IModelNodeItem item = newModelItem(instance, parent); - retval.put(instance.getEffectiveName(), Collections.singletonList(item)); + retval.put(instance.getXmlQName(), Collections.singletonList(item)); } return retval.isEmpty() ? CollectionUtil.emptyMap() : CollectionUtil.unmodifiableMap(retval); } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/item/node/DocumentNodeItemImpl.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/item/node/DocumentNodeItemImpl.java index 4fe228440..0b77870fc 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/item/node/DocumentNodeItemImpl.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/item/node/DocumentNodeItemImpl.java @@ -59,6 +59,11 @@ public IRootAssemblyNodeItem getRootAssemblyNodeItem() { return root; } + @Override + public URI getNamespace() { + return ObjectUtils.notNull(URI.create(getRootAssemblyNodeItem().getName().getNamespaceURI())); + } + @Override @NonNull public URI getDocumentUri() { diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/item/node/IDefinitionNodeItem.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/item/node/IDefinitionNodeItem.java index 9d1da57d4..771c30399 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/item/node/IDefinitionNodeItem.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/item/node/IDefinitionNodeItem.java @@ -28,6 +28,11 @@ import gov.nist.secauto.metaschema.core.model.IDefinition; import gov.nist.secauto.metaschema.core.model.INamedInstance; +import gov.nist.secauto.metaschema.core.util.ObjectUtils; + +import java.net.URI; + +import javax.xml.namespace.QName; import edu.umd.cs.findbugs.annotations.NonNull; @@ -38,9 +43,16 @@ public interface IDefinitionNodeItem getRootNodeItemByName(@NonNull String name) { + default List getRootNodeItemByName(@NonNull QName name) { List> result = getModelItemsByName(name); return result.stream().flatMap(item -> { IRootAssemblyNodeItem retval = null; diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/item/node/IFeatureFlagContainerItem.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/item/node/IFeatureFlagContainerItem.java index f347eeda2..324ade525 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/item/node/IFeatureFlagContainerItem.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/item/node/IFeatureFlagContainerItem.java @@ -33,6 +33,8 @@ import java.util.List; import java.util.Map; +import javax.xml.namespace.QName; + import edu.umd.cs.findbugs.annotations.NonNull; import edu.umd.cs.findbugs.annotations.Nullable; @@ -54,7 +56,7 @@ default Collection getFlags() { } @Override - default IFlagNodeItem getFlagByName(@NonNull String name) { + default IFlagNodeItem getFlagByName(@NonNull QName name) { return getModel().getFlagByName(name); } @@ -65,7 +67,7 @@ default IFlagNodeItem getFlagByName(@NonNull String name) { } @Override - default List> getModelItemsByName(String name) { + default List> getModelItemsByName(QName name) { // no model items return CollectionUtil.emptyList(); } @@ -75,14 +77,14 @@ default IFlagNodeItem getFlagByName(@NonNull String name) { */ class FlagContainer { @NonNull - private final Map flags; + private final Map flags; - protected FlagContainer(@NonNull Map flags) { + protected FlagContainer(@NonNull Map flags) { this.flags = flags; } @Nullable - public IFlagNodeItem getFlagByName(@NonNull String name) { + public IFlagNodeItem getFlagByName(@NonNull QName name) { return flags.get(name); } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/item/node/IFeatureModelContainerItem.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/item/node/IFeatureModelContainerItem.java index ded750c66..1a592c121 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/item/node/IFeatureModelContainerItem.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/item/node/IFeatureModelContainerItem.java @@ -32,6 +32,8 @@ import java.util.List; import java.util.Map; +import javax.xml.namespace.QName; + import edu.umd.cs.findbugs.annotations.NonNull; /** @@ -49,7 +51,7 @@ public interface IFeatureModelContainerItem extends IFeatureFlagContainerItem { } @Override - default List> getModelItemsByName(String name) { + default List> getModelItemsByName(QName name) { return getModel().getModelItemsByName(name); } @@ -58,7 +60,7 @@ public interface IFeatureModelContainerItem extends IFeatureFlagContainerItem { */ class ModelContainer extends FlagContainer { - private final Map>> modelItems; + private final Map>> modelItems; /** * Creates a new collection of flags and model items. @@ -69,8 +71,8 @@ class ModelContainer * a mapping of model item name to a list of model items */ protected ModelContainer( - @NonNull Map flags, - @NonNull Map>> modelItems) { + @NonNull Map flags, + @NonNull Map>> modelItems) { super(flags); this.modelItems = modelItems; } @@ -83,7 +85,7 @@ protected ModelContainer( * @return a lisy of matching model items or {@code null} if no match was found */ @NonNull - public List> getModelItemsByName(@NonNull String name) { + public List> getModelItemsByName(@NonNull QName name) { List> result = modelItems.get(name); return result == null ? CollectionUtil.emptyList() : result; } @@ -98,6 +100,5 @@ protected ModelContainer( public Collection>> getModelItems() { return modelItems.values(); } - } } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/item/node/IFlagNodeItem.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/item/node/IFlagNodeItem.java index 066cf1989..efd4a455e 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/item/node/IFlagNodeItem.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/item/node/IFlagNodeItem.java @@ -37,6 +37,8 @@ import java.util.List; import java.util.stream.Stream; +import javax.xml.namespace.QName; + import edu.umd.cs.findbugs.annotations.NonNull; import edu.umd.cs.findbugs.annotations.Nullable; @@ -80,7 +82,7 @@ default Collection getFlags() { * FlagContainer do not have flag items. This call should return {@code null}. */ @Override - default IFlagNodeItem getFlagByName(@NonNull String name) { + default IFlagNodeItem getFlagByName(@NonNull QName name) { // a flag does not have flags return null; } @@ -112,7 +114,7 @@ default IFlagNodeItem getFlagByName(@NonNull String name) { */ @SuppressWarnings("null") @Override - default List> getModelItemsByName(String name) { + default List> getModelItemsByName(QName name) { // a flag does not have model items return Collections.emptyList(); } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/item/node/INodeItem.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/item/node/INodeItem.java index 4ef6e3038..a7811e867 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/item/node/INodeItem.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/item/node/INodeItem.java @@ -36,6 +36,8 @@ import java.util.List; import java.util.stream.Stream; +import javax.xml.namespace.QName; + import edu.umd.cs.findbugs.annotations.NonNull; import edu.umd.cs.findbugs.annotations.Nullable; @@ -118,6 +120,14 @@ static CLASS toValue(@NonNull INodeItem item) { */ URI getBaseUri(); + /** + * Get the default namespace for the node. + * + * @return the URI + */ + @NonNull + URI getNamespace(); + /** * Get the path for this node item as a Metapath. * @@ -228,7 +238,7 @@ default Stream descendantOrSelf() { Collection getFlags(); /** - * Lookup a flag and value data on this node by it's effective name. + * Lookup a flag and value data on this node by it's effective qualified name. * * @param name * the effective name of the flag @@ -236,7 +246,7 @@ default Stream descendantOrSelf() { * was found */ @Nullable - IFlagNodeItem getFlagByName(@NonNull String name); + IFlagNodeItem getFlagByName(@NonNull QName name); /** * Get the flags and value data associated with this node as a stream. @@ -277,7 +287,7 @@ default Stream flags() { * empty list if an instance with that name is not present */ @NonNull - List> getModelItemsByName(@NonNull String name); + List> getModelItemsByName(@NonNull QName name); /** * Get the model items (i.e., fields, assemblies) and value data associated this diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/item/node/IRootAssemblyNodeItem.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/item/node/IRootAssemblyNodeItem.java index d54aa9a27..e13cd1888 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/item/node/IRootAssemblyNodeItem.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/item/node/IRootAssemblyNodeItem.java @@ -28,7 +28,8 @@ import gov.nist.secauto.metaschema.core.metapath.format.IPathFormatter; import gov.nist.secauto.metaschema.core.model.IAssemblyInstance; -import gov.nist.secauto.metaschema.core.util.ObjectUtils; + +import javax.xml.namespace.QName; import edu.umd.cs.findbugs.annotations.NonNull; @@ -44,9 +45,7 @@ public interface IRootAssemblyNodeItem extends IAssemblyNodeItem { * This overrides the default behavior using the root name for the assembly. */ @Override - default String getName() { - return ObjectUtils.requireNonNull(getDefinition().getRootName()); - } + QName getName(); /** * Get the parent document node item for this root. diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/item/node/ModuleNodeItemImpl.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/item/node/ModuleNodeItemImpl.java index b338b5d0b..6c108e9f3 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/item/node/ModuleNodeItemImpl.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/item/node/ModuleNodeItemImpl.java @@ -29,6 +29,8 @@ import gov.nist.secauto.metaschema.core.model.IModule; import gov.nist.secauto.metaschema.core.util.ObjectUtils; +import java.net.URI; + import edu.umd.cs.findbugs.annotations.NonNull; import nl.talsmasoftware.lazy4j.Lazy; @@ -47,6 +49,11 @@ public ModuleNodeItemImpl( this.model = ObjectUtils.notNull(Lazy.lazy(generator.newMetaschemaModelSupplier(this))); } + @Override + public URI getNamespace() { + return getModule().getXmlNamespace(); + } + @Override public IModule getModule() { return module; diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/item/node/RootAssemblyValuedNodeItemImpl.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/item/node/RootAssemblyValuedNodeItemImpl.java index ddc22b3ae..9c8207a9d 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/item/node/RootAssemblyValuedNodeItemImpl.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/item/node/RootAssemblyValuedNodeItemImpl.java @@ -29,6 +29,8 @@ import gov.nist.secauto.metaschema.core.model.IAssemblyDefinition; import gov.nist.secauto.metaschema.core.util.ObjectUtils; +import javax.xml.namespace.QName; + import edu.umd.cs.findbugs.annotations.NonNull; import nl.talsmasoftware.lazy4j.Lazy; @@ -82,4 +84,9 @@ public Object getValue() { public ModelContainer getModel() { return model.get(); } + + @Override + public QName getName() { + return ObjectUtils.requireNonNull(getDefinition().getRootXmlQName()); + } } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/EmptyFlagContainer.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/EmptyFlagContainer.java index f69edb93c..ca8c4d904 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/EmptyFlagContainer.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/EmptyFlagContainer.java @@ -65,6 +65,8 @@ import java.util.Map; +import javax.xml.namespace.QName; + import edu.umd.cs.findbugs.annotations.NonNull; class EmptyFlagContainer implements IContainerFlagSupport { @@ -72,7 +74,7 @@ class EmptyFlagContainer implements IContainerFlagSupp public static final EmptyFlagContainer EMPTY = new EmptyFlagContainer<>(); @Override - public Map getFlagInstanceMap() { + public Map getFlagInstanceMap() { return CollectionUtil.emptyMap(); } } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/IAssembly.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/IAssembly.java index f40f5c2de..774cc1ba8 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/IAssembly.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/IAssembly.java @@ -32,7 +32,7 @@ * A marker interface for an information element that is an assembly model type. * */ -public interface IAssembly extends IModelElement, INamed, IAttributable { +public interface IAssembly extends INamedModelElement, IAttributable { /** * Provides the Metaschema model type of "ASSEMBLY". * diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/IAssemblyDefinition.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/IAssemblyDefinition.java index a6297b133..ec9bfbc96 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/IAssemblyDefinition.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/IAssemblyDefinition.java @@ -34,6 +34,7 @@ public interface IAssemblyDefinition extends IModelDefinition, IContainerModelAssembly, IAssembly, IFeatureModelConstrained { + QName MODEL_QNAME = new QName(IModule.XML_NAMESPACE, "model"); /** * Check if the assembly is a top-level root assembly. @@ -81,7 +82,7 @@ default QName getRootXmlQName() { QName retval = null; String rootName = getRootName(); if (rootName != null) { - retval = new QName(getContainingModule().getXmlNamespace().toASCIIString(), rootName); + retval = getContainingModule().toModelQName(rootName); } return retval; } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/IAssemblyInstance.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/IAssemblyInstance.java index fe149b15c..6bc6ac7fa 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/IAssemblyInstance.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/IAssemblyInstance.java @@ -26,23 +26,11 @@ package gov.nist.secauto.metaschema.core.model; -import gov.nist.secauto.metaschema.core.util.ObjectUtils; - -import javax.xml.namespace.QName; - -import edu.umd.cs.findbugs.annotations.NonNull; - public interface IAssemblyInstance extends IAssembly, INamedModelInstance { @Override IAssemblyDefinition getDefinition(); - @Override - @NonNull - default QName getXmlQName() { - return ObjectUtils.notNull(INamedModelInstance.super.getXmlQName()); - } - @Override default boolean isEffectiveValueWrappedInXml() { // assembly instances are always wrapped diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/IContainerFlag.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/IContainerFlag.java index ff090f36e..599a2f122 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/IContainerFlag.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/IContainerFlag.java @@ -28,6 +28,8 @@ import java.util.Collection; +import javax.xml.namespace.QName; + import edu.umd.cs.findbugs.annotations.NonNull; import edu.umd.cs.findbugs.annotations.Nullable; @@ -52,7 +54,7 @@ default boolean hasChildren() { * matching the specified name */ @Nullable - IFlagInstance getFlagInstanceByName(@NonNull String name); + IFlagInstance getFlagInstanceByName(@NonNull QName name); /** * Retrieves the flag instances for all flags defined on the containing diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/IContainerFlagSupport.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/IContainerFlagSupport.java index dc005314d..24ef98b9e 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/IContainerFlagSupport.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/IContainerFlagSupport.java @@ -28,6 +28,8 @@ import java.util.Map; +import javax.xml.namespace.QName; + import edu.umd.cs.findbugs.annotations.NonNull; public interface IContainerFlagSupport { @@ -50,7 +52,7 @@ static IContainerFlagSupport empty() { * @return the mapping of flag effective name to flag instance */ @NonNull - Map getFlagInstanceMap(); + Map getFlagInstanceMap(); // @Nullable // FI getJsonKeyFlagInstance(); } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/IContainerModel.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/IContainerModel.java index 1c2795e8f..3bd7e8126 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/IContainerModel.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/IContainerModel.java @@ -28,6 +28,8 @@ import java.util.Collection; +import javax.xml.namespace.QName; + import edu.umd.cs.findbugs.annotations.NonNull; import edu.umd.cs.findbugs.annotations.Nullable; @@ -76,7 +78,7 @@ default boolean hasChildren() { * @see INamedModelInstance#getEffectiveName() */ @Nullable - INamedModelInstance getNamedModelInstanceByName(String name); + INamedModelInstance getNamedModelInstanceByName(QName name); /** * Get all field instances within the container. @@ -96,7 +98,7 @@ default boolean hasChildren() { * @see IFieldInstance#getUseName() */ @Nullable - IFieldInstance getFieldInstanceByName(String name); + IFieldInstance getFieldInstanceByName(QName name); /** * Get all assembly instances within the container. @@ -116,5 +118,5 @@ default boolean hasChildren() { * @see INamedModelInstance#getEffectiveName() */ @Nullable - IAssemblyInstance getAssemblyInstanceByName(String name); + IAssemblyInstance getAssemblyInstanceByName(QName name); } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/IContainerModelAbsolute.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/IContainerModelAbsolute.java index 9d06482f1..501aa0458 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/IContainerModelAbsolute.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/IContainerModelAbsolute.java @@ -28,6 +28,8 @@ import java.util.Collection; +import javax.xml.namespace.QName; + public interface IContainerModelAbsolute extends IContainerModel { @Override @@ -37,17 +39,17 @@ public interface IContainerModelAbsolute extends IContainerModel { Collection getNamedModelInstances(); @Override - INamedModelInstanceAbsolute getNamedModelInstanceByName(String name); + INamedModelInstanceAbsolute getNamedModelInstanceByName(QName name); @Override Collection getFieldInstances(); @Override - IFieldInstanceAbsolute getFieldInstanceByName(String name); + IFieldInstanceAbsolute getFieldInstanceByName(QName name); @Override Collection getAssemblyInstances(); @Override - IAssemblyInstanceAbsolute getAssemblyInstanceByName(String name); + IAssemblyInstanceAbsolute getAssemblyInstanceByName(QName name); } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/IContainerModelGrouped.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/IContainerModelGrouped.java index 819dc7874..80033a95b 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/IContainerModelGrouped.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/IContainerModelGrouped.java @@ -28,6 +28,8 @@ import java.util.Collection; +import javax.xml.namespace.QName; + import edu.umd.cs.findbugs.annotations.NonNull; import edu.umd.cs.findbugs.annotations.Nullable; @@ -62,7 +64,7 @@ default Collection getModelInstances() { */ @Override @Nullable - INamedModelInstanceGrouped getNamedModelInstanceByName(String name); + INamedModelInstanceGrouped getNamedModelInstanceByName(QName name); /** * Get all field instances within the container. @@ -84,7 +86,7 @@ default Collection getModelInstances() { */ @Override @Nullable - IFieldInstanceGrouped getFieldInstanceByName(String name); + IFieldInstanceGrouped getFieldInstanceByName(QName name); /** * Get all assembly instances within the container. @@ -106,5 +108,5 @@ default Collection getModelInstances() { */ @Override @Nullable - IAssemblyInstanceGrouped getAssemblyInstanceByName(String name); + IAssemblyInstanceGrouped getAssemblyInstanceByName(QName name); } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/IContainerModelSupport.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/IContainerModelSupport.java index 892d870ce..dd4e0b844 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/IContainerModelSupport.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/IContainerModelSupport.java @@ -31,6 +31,8 @@ import java.util.Collection; import java.util.Map; +import javax.xml.namespace.QName; + import edu.umd.cs.findbugs.annotations.NonNull; /** @@ -88,7 +90,7 @@ AI extends IAssemblyInstance> IContainerModelSupport empty() { * @return the mapping */ @NonNull - Map getNamedModelInstanceMap(); + Map getNamedModelInstanceMap(); /** * Get a mapping of all field instances, mapped from their effective name to the @@ -97,7 +99,7 @@ AI extends IAssemblyInstance> IContainerModelSupport empty() { * @return the mapping */ @NonNull - Map getFieldInstanceMap(); + Map getFieldInstanceMap(); /** * Get a mapping of all assembly instances, mapped from their effective name to @@ -106,5 +108,5 @@ AI extends IAssemblyInstance> IContainerModelSupport empty() { * @return the mapping */ @NonNull - Map getAssemblyInstanceMap(); + Map getAssemblyInstanceMap(); } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/IDefinition.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/IDefinition.java index 50397822d..e6a367f3b 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/IDefinition.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/IDefinition.java @@ -32,7 +32,7 @@ import edu.umd.cs.findbugs.annotations.NonNull; -public interface IDefinition extends IModelElement, INamed, IAttributable, IFeatureValueConstrained { +public interface IDefinition extends INamedModelElement, IAttributable, IFeatureValueConstrained { @NonNull ModuleScopeEnum DEFAULT_DEFINITION_MODEL_SCOPE = ModuleScopeEnum.INHERITED; diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/IFeatureContainerFlag.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/IFeatureContainerFlag.java index 02c7c1367..72ee85aba 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/IFeatureContainerFlag.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/IFeatureContainerFlag.java @@ -30,6 +30,8 @@ import java.util.Collection; +import javax.xml.namespace.QName; + import edu.umd.cs.findbugs.annotations.NonNull; import edu.umd.cs.findbugs.annotations.Nullable; @@ -50,7 +52,7 @@ public interface IFeatureContainerFlag extends IContai @Override @Nullable - default FI getFlagInstanceByName(String name) { + default FI getFlagInstanceByName(QName name) { return getFlagContainer().getFlagInstanceMap().get(name); } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/IFeatureContainerModel.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/IFeatureContainerModel.java index 7bf024b3a..c96965ea5 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/IFeatureContainerModel.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/IFeatureContainerModel.java @@ -28,6 +28,8 @@ import java.util.Collection; +import javax.xml.namespace.QName; + import edu.umd.cs.findbugs.annotations.NonNull; /** @@ -62,7 +64,7 @@ default Collection getModelInstances() { } @Override - default NMI getNamedModelInstanceByName(String name) { + default NMI getNamedModelInstanceByName(QName name) { return getModelContainer().getNamedModelInstanceMap().get(name); } @@ -73,7 +75,7 @@ default Collection getNamedModelInstances() { } @Override - default FI getFieldInstanceByName(String name) { + default FI getFieldInstanceByName(QName name) { return getModelContainer().getFieldInstanceMap().get(name); } @@ -84,7 +86,7 @@ default Collection getFieldInstances() { } @Override - default AI getAssemblyInstanceByName(String name) { + default AI getAssemblyInstanceByName(QName name) { return getModelContainer().getAssemblyInstanceMap().get(name); } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/IFeatureContainerModelAbsolute.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/IFeatureContainerModelAbsolute.java index 0132e5990..4d3fddc78 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/IFeatureContainerModelAbsolute.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/IFeatureContainerModelAbsolute.java @@ -28,6 +28,8 @@ import java.util.Collection; +import javax.xml.namespace.QName; + /** * Common interface for model container support classes. * @@ -51,7 +53,7 @@ default Collection getModelInstances() { } @Override - default NMI getNamedModelInstanceByName(String name) { + default NMI getNamedModelInstanceByName(QName name) { return getModelContainer().getNamedModelInstanceMap().get(name); } @@ -62,7 +64,7 @@ default Collection getNamedModelInstances() { } @Override - default FI getFieldInstanceByName(String name) { + default FI getFieldInstanceByName(QName name) { return getModelContainer().getFieldInstanceMap().get(name); } @@ -73,7 +75,7 @@ default Collection getFieldInstances() { } @Override - default AI getAssemblyInstanceByName(String name) { + default AI getAssemblyInstanceByName(QName name) { return getModelContainer().getAssemblyInstanceMap().get(name); } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/IFeatureContainerModelGrouped.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/IFeatureContainerModelGrouped.java index 06604b21d..d012bdd87 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/IFeatureContainerModelGrouped.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/IFeatureContainerModelGrouped.java @@ -28,6 +28,8 @@ import java.util.Collection; +import javax.xml.namespace.QName; + import edu.umd.cs.findbugs.annotations.NonNull; public interface IFeatureContainerModelGrouped< @@ -55,7 +57,7 @@ default Collection getModelInstances() { } @Override - default NMI getNamedModelInstanceByName(String name) { + default NMI getNamedModelInstanceByName(QName name) { return getModelContainer().getNamedModelInstanceMap().get(name); } @@ -66,7 +68,7 @@ default Collection getNamedModelInstances() { } @Override - default FI getFieldInstanceByName(String name) { + default FI getFieldInstanceByName(QName name) { return getModelContainer().getFieldInstanceMap().get(name); } @@ -77,7 +79,7 @@ default Collection getFieldInstances() { } @Override - default AI getAssemblyInstanceByName(String name) { + default AI getAssemblyInstanceByName(QName name) { return getModelContainer().getAssemblyInstanceMap().get(name); } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/IField.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/IField.java index d694146a4..ade2e6bc6 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/IField.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/IField.java @@ -30,7 +30,7 @@ * A marker interface for an information element that is a field model type. * */ -public interface IField extends IModelElement, INamed, IAttributable { +public interface IField extends INamedModelElement, IAttributable { /** * Provides the Metaschema model type of "FIELD". * diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/IFieldInstance.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/IFieldInstance.java index 98879931c..9a50d38c7 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/IFieldInstance.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/IFieldInstance.java @@ -26,24 +26,12 @@ package gov.nist.secauto.metaschema.core.model; -import javax.xml.namespace.QName; - public interface IFieldInstance extends IField, INamedModelInstance, IValuedInstance { boolean DEFAULT_FIELD_IN_XML_WRAPPED = true; - @Override - default String getXmlNamespace() { - return INamedModelInstance.super.getXmlNamespace(); - } - @Override IFieldDefinition getDefinition(); - @Override - default QName getXmlQName() { - return isEffectiveValueWrappedInXml() ? INamedModelInstance.super.getXmlQName() : null; - } - /** * Determines if the field is configured to have a wrapper in XML. * diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/IFlag.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/IFlag.java index f227035a5..9e08aed4a 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/IFlag.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/IFlag.java @@ -30,7 +30,7 @@ * A marker interface for an information element that is an flag model type. * */ -public interface IFlag extends IModelElement, INamed, IAttributable { +public interface IFlag extends INamedModelElement, IAttributable { /** * Provides the Metaschema model type of "FLAG". * @@ -40,4 +40,14 @@ public interface IFlag extends IModelElement, INamed, IAttributable { default ModelType getModelType() { return ModelType.FLAG; } + + /** + * {@inheritDoc} + *

+ * Flags by default do not have namespaces. + */ + @Override + default String getXmlNamespace() { + return null; + } } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/IFlagInstance.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/IFlagInstance.java index 7e397d9c7..f927a2965 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/IFlagInstance.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/IFlagInstance.java @@ -26,12 +26,6 @@ package gov.nist.secauto.metaschema.core.model; -import gov.nist.secauto.metaschema.core.util.ObjectUtils; - -import javax.xml.namespace.QName; - -import edu.umd.cs.findbugs.annotations.NonNull; - public interface IFlagInstance extends IFlag, IValuedInstance, IInstanceAbsolute { boolean DEFAULT_FLAG_REQUIRED = false; @@ -39,19 +33,6 @@ public interface IFlagInstance extends IFlag, IValuedInstance, IInstanceAbsolute @Override IContainerFlag getParentContainer(); - @Override - default String getXmlNamespace() { - // by default, flags do not have namespaces - return null; - } - - @NonNull - @Override - default QName getXmlQName() { - // flags always have a qname - return ObjectUtils.requireNonNull(IValuedInstance.super.getXmlQName()); - } - @Override IFlagDefinition getDefinition(); diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/IGroupable.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/IGroupable.java index 0780bf6e6..6cbb04920 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/IGroupable.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/IGroupable.java @@ -85,8 +85,7 @@ default String getGroupAsName() { */ @Nullable default String getGroupAsXmlNamespace() { - // no group-as by default - return null; + return getContainingDefinition().getXmlNamespace(); } /** @@ -97,9 +96,11 @@ default String getGroupAsXmlNamespace() { * * @return the XML namespace */ + // REFACTOR: remove this method + // REFACTOR: consider pushing down the call for getGroupAsXmlNamespace @NonNull default String getEffectiveGroupAsNamespace() { - String namespace = getGroupAsXmlNamespace(); + @Nullable String namespace = getGroupAsXmlNamespace(); if (namespace == null) { namespace = ObjectUtils.notNull(getContainingModule().getXmlNamespace().toASCIIString()); } @@ -115,6 +116,7 @@ default String getEffectiveGroupAsNamespace() { * @return the groupAs QName or {@code null} if no name is configured, such as * when {@link #getMaxOccurs()} = 1. */ + // REFACTOR: rename to getXmlGroupAsQName @Nullable default QName getEffectiveXmlGroupAsQName() { return XmlGroupAsBehavior.GROUPED.equals(getXmlGroupAsBehavior()) diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/IModule.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/IModule.java index 4b1de5491..bb44743e3 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/IModule.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/IModule.java @@ -104,7 +104,7 @@ public interface IModule { * @return the qualified name */ default QName getQName() { - return new QName(getXmlNamespace().toString(), getShortName()); + return new QName(getXmlNamespace().toASCIIString(), getShortName()); } /** @@ -331,4 +331,16 @@ default QName getQName() { */ @Nullable IAssemblyDefinition getExportedAssemblyDefinitionByName(String name); + + @NonNull + default QName toFlagQName(@NonNull String nameRef) { + // TODO: handle namespace prefix + return new QName(nameRef); + } + + @NonNull + default QName toModelQName(@NonNull String nameRef) { + // TODO: handle namespace prefix + return new QName(getXmlNamespace().toASCIIString(), nameRef); + } } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/INamedInstance.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/INamedInstance.java index f9e72947e..fac230666 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/INamedInstance.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/INamedInstance.java @@ -26,8 +26,6 @@ package gov.nist.secauto.metaschema.core.model; -import javax.xml.namespace.QName; - import edu.umd.cs.findbugs.annotations.NonNull; /** @@ -36,7 +34,7 @@ * identify the data. * */ -public interface INamedInstance extends INamed, IAttributable, IInstance { +public interface INamedInstance extends INamedModelElement, IAttributable, IInstance { /** * Retrieve the definition of this instance. * @@ -45,21 +43,4 @@ public interface INamedInstance extends INamed, IAttributable, IInstance { @NonNull IDefinition getDefinition(); - /** - * Get the XML qualified name to use in XML. - * - * @return the XML qualified name, or {@code null} if there isn't one - */ - default QName getXmlQName() { - return new QName(getXmlNamespace(), getEffectiveName()); - } - - /** - * Retrieve the XML namespace for this instance. - * - * @return the XML namespace or {@code null} if no namespace is defined - */ - default String getXmlNamespace() { - return getContainingModule().getXmlNamespace().toASCIIString(); - } } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/INamed.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/INamedModelElement.java similarity index 76% rename from core/src/main/java/gov/nist/secauto/metaschema/core/model/INamed.java rename to core/src/main/java/gov/nist/secauto/metaschema/core/model/INamedModelElement.java index 78192f587..cca624f0a 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/INamed.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/INamedModelElement.java @@ -28,6 +28,9 @@ import gov.nist.secauto.metaschema.core.datatype.markup.MarkupLine; +import javax.xml.XMLConstants; +import javax.xml.namespace.QName; + import edu.umd.cs.findbugs.annotations.NonNull; import edu.umd.cs.findbugs.annotations.Nullable; @@ -36,7 +39,7 @@ * Metaschema module's model that have a name and other identifying * characteristics. */ -public interface INamed extends IDescribable { +public interface INamedModelElement extends IDescribable, IModelElement { /** * The resolved formal display name, which allows an instance to override a * definition's name. @@ -61,26 +64,11 @@ default MarkupLine getEffectiveDescription() { return getDescription(); } - // @NonNull - // default QName getXmlQName() { - // String namespace = getXmlNamespace(); - // - // @NonNull - // QName retval; - // if (namespace != null) { - // retval = new QName(namespace, getEffectiveName()); - // } else { - // retval = new QName(getEffectiveName()); - // } - // return retval; - // } - /** * Retrieve the name of the model element. * * @return the name */ - // from INamedModelElement @NonNull String getName(); @@ -115,6 +103,45 @@ default String getEffectiveName() { return useName == null ? getName() : useName; } + /** + * Retrieve the XML namespace for this instance. + *

+ * Multiple calls to this method are expected to produce the same, deterministic + * return value. + * + * @return the XML namespace or {@code null} if no namespace is defined + */ + @Nullable + default String getXmlNamespace() { + return getContainingModule().getXmlNamespace().toASCIIString(); + } + + /** + * Get the unique XML qualified name for this model element. + *

+ * The qualified name is considered to be unique relative to all sibling + * elements. For a flag, this name will be unique among all flag instances on + * the same field or assembly definition. For a field or assembly, this name + * will be unique among all sibling field or assembly instances on the same + * assembly definition. + *

+ * Multiple calls to this method are expected to produce the same, deterministic + * return value. + *

+ * If {@link #getXmlNamespace()} is {@code null}, the the resulting QName will + * have the namespace {@link XMLConstants#NULL_NS_URI}. + *

+ * This implementation may be overridden by implementation that cache the QName + * or provide for a more efficient method for QName creation. + * + * @return the XML qualified name, or {@code null} if there isn't one + */ + // REFACTOR: rename to getQName + @NonNull + default QName getXmlQName() { + return new QName(getXmlNamespace(), getEffectiveName()); + } + /** * Retrieve the index value to use for binary naming. * diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/AbstractConstraintBuilder.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/AbstractConstraintBuilder.java index be4008c86..95c311acc 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/AbstractConstraintBuilder.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/AbstractConstraintBuilder.java @@ -28,7 +28,6 @@ import gov.nist.secauto.metaschema.core.datatype.markup.MarkupLine; import gov.nist.secauto.metaschema.core.datatype.markup.MarkupMultiline; -import gov.nist.secauto.metaschema.core.metapath.MetapathExpression; import gov.nist.secauto.metaschema.core.model.IAttributable; import gov.nist.secauto.metaschema.core.model.constraint.IConstraint.Level; import gov.nist.secauto.metaschema.core.util.CollectionUtil; @@ -52,7 +51,7 @@ public abstract class AbstractConstraintBuilder< @NonNull private Level level = IConstraint.DEFAULT_LEVEL; @NonNull - private MetapathExpression target = IConstraint.DEFAULT_TARGET; + private String target = IConstraint.DEFAULT_TARGET_METAPATH; @NonNull private Map> properties = new LinkedHashMap<>(); // NOPMD not thread safe private MarkupMultiline remarks; @@ -141,7 +140,7 @@ public T level(@NonNull Level level) { * @return this builder */ @NonNull - public T target(@NonNull MetapathExpression target) { + public T target(@NonNull String target) { this.target = target; return getThis(); } @@ -291,7 +290,7 @@ protected Level getLevel() { * @return the target Metapath expression */ @NonNull - protected MetapathExpression getTarget() { + protected String getTarget() { return target; } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/AbstractConstraintValidationHandler.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/AbstractConstraintValidationHandler.java index f10eb99e1..505dccaef 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/AbstractConstraintValidationHandler.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/AbstractConstraintValidationHandler.java @@ -103,7 +103,7 @@ protected String newCardinalityMinimumViolationMessage( "The cardinality '%d' is below the required minimum '%d' for items matching '%s'.", targets.size(), constraint.getMinOccurs(), - constraint.getTarget().getPath()); + constraint.getTarget()); } /** @@ -271,7 +271,7 @@ protected CharSequence newExpectViolationMessage( message = constraint.generateMessage(target, dynamicContext); } else { message = String.format("Expect constraint '%s' did not match the data at path '%s'", - constraint.getTest().getPath(), + constraint.getTest(), toPath(target)); } return message; diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/AbstractTargetedConstraints.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/AbstractTargetedConstraints.java index 5f376a714..852761d12 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/AbstractTargetedConstraints.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/AbstractTargetedConstraints.java @@ -26,8 +26,6 @@ package gov.nist.secauto.metaschema.core.model.constraint; -import gov.nist.secauto.metaschema.core.metapath.MetapathExpression; - import edu.umd.cs.findbugs.annotations.NonNull; /** @@ -40,7 +38,7 @@ public abstract class AbstractTargetedConstraints implements ITargetedConstraints, IFeatureValueConstrained { @NonNull - private final MetapathExpression targetExpression; + private final String targetExpression; @NonNull private final T constraints; @@ -53,14 +51,14 @@ public abstract class AbstractTargetedConstraints * the constraints to apply to matching targets */ protected AbstractTargetedConstraints( - @NonNull MetapathExpression target, + @NonNull String target, @NonNull T constraints) { this.targetExpression = target; this.constraints = constraints; } @Override - public MetapathExpression getTargetExpression() { + public String getTargetExpression() { return targetExpression; } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/AssemblyTargetedConstraints.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/AssemblyTargetedConstraints.java index 59193ee9e..5595e0459 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/AssemblyTargetedConstraints.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/AssemblyTargetedConstraints.java @@ -26,7 +26,6 @@ package gov.nist.secauto.metaschema.core.model.constraint; -import gov.nist.secauto.metaschema.core.metapath.MetapathExpression; import gov.nist.secauto.metaschema.core.model.IAssemblyDefinition; import gov.nist.secauto.metaschema.core.model.constraint.impl.AbstractDefinitionTargetedConstraints; @@ -51,7 +50,7 @@ public class AssemblyTargetedConstraints * the constraints to apply to matching targets */ public AssemblyTargetedConstraints( - @NonNull MetapathExpression target, + @NonNull String target, @NonNull IModelConstrained constraints) { super(target, constraints); } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/DefaultConstraintValidator.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/DefaultConstraintValidator.java index 2f4a0ca98..4a45b9688 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/DefaultConstraintValidator.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/DefaultConstraintValidator.java @@ -58,6 +58,8 @@ import java.util.concurrent.ConcurrentHashMap; import java.util.regex.Pattern; +import javax.xml.namespace.QName; + import edu.umd.cs.findbugs.annotations.NonNull; import edu.umd.cs.findbugs.annotations.Nullable; @@ -533,12 +535,14 @@ private void validateExpect( @NonNull INodeItem node, @NonNull ISequence targets, @NonNull DynamicContext dynamicContext) { + MetapathExpression metapath = MetapathExpression.compile( + constraint.getTest(), + dynamicContext.getStaticContext()); targets.asStream() .map(item -> (INodeItem) item) .forEachOrdered(item -> { assert item != null; if (item.hasValue()) { - MetapathExpression metapath = constraint.getTest(); try { ISequence result = metapath.evaluate(item, dynamicContext); if (!FnBoolean.fnBoolean(result).toBoolean()) { @@ -610,7 +614,7 @@ private static void rethrowConstraintError( String id = constraint.getId(); if (id == null) { builder.append(" targeting the metapath '") - .append(constraint.getTarget().getPath()) + .append(constraint.getTarget()) .append('\''); } else { builder.append(" with id '") @@ -770,7 +774,7 @@ class Visitor @NonNull private DynamicContext handleLetStatements( @NonNull INodeItem focus, - @NonNull Map letExpressions, + @NonNull Map letExpressions, @NonNull DynamicContext dynamicContext) { DynamicContext retval; @@ -781,7 +785,7 @@ private DynamicContext handleLetStatements( final DynamicContext subContext = dynamicContext.subContext(); for (ILet let : lets) { - String name = let.getName(); + QName name = let.getName(); ISequence result = let.getValueExpression().evaluate(focus, subContext); subContext.bindVariableValue(name, result); } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/FieldTargetedConstraints.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/FieldTargetedConstraints.java index 386f11095..201e64b6e 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/FieldTargetedConstraints.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/FieldTargetedConstraints.java @@ -26,7 +26,6 @@ package gov.nist.secauto.metaschema.core.model.constraint; -import gov.nist.secauto.metaschema.core.metapath.MetapathExpression; import gov.nist.secauto.metaschema.core.model.IFieldDefinition; import gov.nist.secauto.metaschema.core.model.constraint.impl.AbstractDefinitionTargetedConstraints; @@ -50,7 +49,7 @@ public class FieldTargetedConstraints * the constraints to apply to matching targets */ public FieldTargetedConstraints( - @NonNull MetapathExpression target, + @NonNull String target, @NonNull IValueConstrained constraints) { super(target, constraints); } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/FlagTargetedConstraints.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/FlagTargetedConstraints.java index d59269063..7bbdc47e5 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/FlagTargetedConstraints.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/FlagTargetedConstraints.java @@ -26,7 +26,6 @@ package gov.nist.secauto.metaschema.core.model.constraint; -import gov.nist.secauto.metaschema.core.metapath.MetapathExpression; import gov.nist.secauto.metaschema.core.model.IFlagDefinition; import gov.nist.secauto.metaschema.core.model.constraint.impl.AbstractDefinitionTargetedConstraints; @@ -50,7 +49,7 @@ public class FlagTargetedConstraints * the constraints to apply to matching targets */ public FlagTargetedConstraints( - @NonNull MetapathExpression target, + @NonNull String target, @NonNull IValueConstrained constraints) { super(target, constraints); } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/IConstraint.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/IConstraint.java index 4c2a94cf3..26daee64e 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/IConstraint.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/IConstraint.java @@ -73,11 +73,7 @@ enum Level { */ @NonNull Level DEFAULT_LEVEL = Level.ERROR; - /** - * The default target Metapath expression to use if no target is provided. - */ - @NonNull - MetapathExpression DEFAULT_TARGET = MetapathExpression.CONTEXT_NODE; + /** * The default target Metapath expression to use if no target is provided. */ @@ -115,7 +111,7 @@ enum Level { * @return a Metapath expression */ @NonNull - MetapathExpression getTarget(); + String getTarget(); /** * Based on the provided {@code contextNodeItem}, find all nodes matching the @@ -129,7 +125,7 @@ enum Level { @NonNull default ISequence> matchTargets( @NonNull IDefinitionNodeItem contextNodeItem) { - return getTarget().evaluate(contextNodeItem); + return MetapathExpression.compile(getTarget()).evaluate(contextNodeItem); } /** @@ -144,9 +140,11 @@ enum Level { * @see #getTarget() */ @NonNull - default ISequence> matchTargets(@NonNull IDefinitionNodeItem item, + default ISequence> matchTargets( + @NonNull IDefinitionNodeItem item, @NonNull DynamicContext dynamicContext) { - return item.hasValue() ? getTarget().evaluate(item, dynamicContext) : ISequence.empty(); + return item.hasValue() ? MetapathExpression.compile(getTarget(), dynamicContext.getStaticContext()) + .evaluate(item, dynamicContext) : ISequence.empty(); } /** diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/IExpectConstraint.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/IExpectConstraint.java index 856380da4..d1c24b0ff 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/IExpectConstraint.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/IExpectConstraint.java @@ -27,7 +27,6 @@ package gov.nist.secauto.metaschema.core.model.constraint; import gov.nist.secauto.metaschema.core.metapath.DynamicContext; -import gov.nist.secauto.metaschema.core.metapath.MetapathExpression; import gov.nist.secauto.metaschema.core.metapath.item.node.INodeItem; import gov.nist.secauto.metaschema.core.model.constraint.impl.DefaultExpectConstraint; import gov.nist.secauto.metaschema.core.util.ObjectUtils; @@ -43,7 +42,7 @@ public interface IExpectConstraint extends IConstraint { @NonNull - MetapathExpression getTest(); + String getTest(); /** * A message to emit when the constraint is violated. Allows embedded Metapath @@ -67,7 +66,7 @@ static Builder builder() { final class Builder extends AbstractConstraintBuilder { - private MetapathExpression test; + private String test; private String message; private Builder() { @@ -75,7 +74,7 @@ private Builder() { } @NonNull - public Builder test(@NonNull MetapathExpression test) { + public Builder test(@NonNull String test) { this.test = test; return this; } @@ -98,7 +97,7 @@ protected void validate() { ObjectUtils.requireNonNull(getTest()); } - protected MetapathExpression getTest() { + protected String getTest() { return test; } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/IFeatureValueConstrained.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/IFeatureValueConstrained.java index 0bb19ce12..48e6070b9 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/IFeatureValueConstrained.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/IFeatureValueConstrained.java @@ -29,6 +29,8 @@ import java.util.List; import java.util.Map; +import javax.xml.namespace.QName; + import edu.umd.cs.findbugs.annotations.NonNull; public interface IFeatureValueConstrained extends IValueConstrained { @@ -47,7 +49,7 @@ default ILet addLetExpression(ILet let) { } @Override - default Map getLetExpressions() { + default Map getLetExpressions() { return getConstraintSupport().getLetExpressions(); } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/IIndex.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/IIndex.java index 28a00325a..ceedeac65 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/IIndex.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/IIndex.java @@ -182,11 +182,12 @@ private static String buildKeyItem( @NonNull INodeItem item, @NonNull IKeyField keyField, @NonNull DynamicContext dynamicContext) { - MetapathExpression keyPath = keyField.getTarget(); + String keyPath = keyField.getTarget(); + MetapathExpression keyMetapath = MetapathExpression.compile(keyPath, dynamicContext.getStaticContext()); INodeItem keyItem; try { - keyItem = keyPath.evaluateAs(item, ResultType.NODE, dynamicContext); + keyItem = keyMetapath.evaluateAs(item, ResultType.NODE, dynamicContext); } catch (InvalidTypeMetapathException ex) { throw new MetapathException("Key path did not result in a single node", ex); } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/IKeyField.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/IKeyField.java index f00dff177..f2819ee3d 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/IKeyField.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/IKeyField.java @@ -27,7 +27,6 @@ package gov.nist.secauto.metaschema.core.model.constraint; import gov.nist.secauto.metaschema.core.datatype.markup.MarkupMultiline; -import gov.nist.secauto.metaschema.core.metapath.MetapathExpression; import gov.nist.secauto.metaschema.core.model.constraint.impl.DefaultKeyField; import java.util.regex.Pattern; @@ -58,7 +57,7 @@ public interface IKeyField { @SuppressWarnings("PMD.ShortMethodName") @NonNull static IKeyField of( - @NonNull MetapathExpression target, + @NonNull String target, @Nullable Pattern pattern, @Nullable MarkupMultiline remarks) { return new DefaultKeyField(target, pattern, remarks); @@ -71,7 +70,7 @@ static IKeyField of( * @return the Metapath expression identifying the key value target */ @NonNull - MetapathExpression getTarget(); + String getTarget(); /** * A pattern to use to retrieve the value. If non-{@code null}, the first diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/ILet.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/ILet.java index 5a9acb02e..fd17914e2 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/ILet.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/ILet.java @@ -30,6 +30,8 @@ import gov.nist.secauto.metaschema.core.metapath.MetapathExpression; import gov.nist.secauto.metaschema.core.model.constraint.impl.DefaultLet; +import javax.xml.namespace.QName; + import edu.umd.cs.findbugs.annotations.NonNull; @SuppressWarnings("PMD.ShortClassName") @@ -49,7 +51,7 @@ public interface ILet { @SuppressWarnings("PMD.ShortMethodName") @NonNull static ILet of( - @NonNull String name, + @NonNull QName name, @NonNull String valueExpression, @NonNull ISource source) { try { @@ -79,7 +81,7 @@ static ILet of( @SuppressWarnings("PMD.ShortMethodName") @NonNull static ILet of( - @NonNull String name, + @NonNull QName name, @NonNull MetapathExpression valueExpression, @NonNull ISource source) { return new DefaultLet(name, valueExpression, source); @@ -91,7 +93,7 @@ static ILet of( * @return the name */ @NonNull - String getName(); + QName getName(); /** * Get the Metapath expression to use to query the value. diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/ITargetedConstraints.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/ITargetedConstraints.java index b6adabed6..55e5cc7f7 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/ITargetedConstraints.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/ITargetedConstraints.java @@ -26,7 +26,6 @@ package gov.nist.secauto.metaschema.core.model.constraint; -import gov.nist.secauto.metaschema.core.metapath.MetapathExpression; import gov.nist.secauto.metaschema.core.model.IAssemblyDefinition; import gov.nist.secauto.metaschema.core.model.IFieldDefinition; import gov.nist.secauto.metaschema.core.model.IFlagDefinition; @@ -39,7 +38,7 @@ */ public interface ITargetedConstraints extends IValueConstrained { @NonNull - MetapathExpression getTargetExpression(); + String getTargetExpression(); void target(@NonNull IFlagDefinition definition); diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/IValueConstrained.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/IValueConstrained.java index e87a00aca..5c3da5fea 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/IValueConstrained.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/IValueConstrained.java @@ -29,6 +29,8 @@ import java.util.List; import java.util.Map; +import javax.xml.namespace.QName; + import edu.umd.cs.findbugs.annotations.NonNull; /** @@ -42,7 +44,7 @@ public interface IValueConstrained extends IConstrained { * @return the constraints or an empty list */ @NonNull - Map getLetExpressions(); + Map getLetExpressions(); /** * Get the collection of allowed value constraints, if any. diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/ValueConstraintSet.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/ValueConstraintSet.java index 2371598cf..cfc7af70c 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/ValueConstraintSet.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/ValueConstraintSet.java @@ -31,12 +31,14 @@ import java.util.List; import java.util.Map; +import javax.xml.namespace.QName; + import edu.umd.cs.findbugs.annotations.NonNull; public class ValueConstraintSet implements IValueConstrained { // NOPMD - intentional @SuppressWarnings("PMD.UseConcurrentHashMap") // need ordering @NonNull - private final Map lets = new LinkedHashMap<>(); + private final Map lets = new LinkedHashMap<>(); @NonNull private final List constraints = new LinkedList<>(); @NonNull @@ -49,7 +51,7 @@ public class ValueConstraintSet implements IValueConstrained { // NOPMD - intent private final List expectConstraints = new LinkedList<>(); @Override - public Map getLetExpressions() { + public Map getLetExpressions() { return lets; } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/impl/AbstractConstraint.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/impl/AbstractConstraint.java index 5f510d60d..38444969c 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/impl/AbstractConstraint.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/impl/AbstractConstraint.java @@ -28,7 +28,6 @@ import gov.nist.secauto.metaschema.core.datatype.markup.MarkupLine; import gov.nist.secauto.metaschema.core.datatype.markup.MarkupMultiline; -import gov.nist.secauto.metaschema.core.metapath.MetapathExpression; import gov.nist.secauto.metaschema.core.model.IAttributable; import gov.nist.secauto.metaschema.core.model.constraint.IConstraint; import gov.nist.secauto.metaschema.core.model.constraint.ISource; @@ -54,7 +53,7 @@ public abstract class AbstractConstraint implements IConstraint { // NOPMD - int @NonNull private final Level level; @NonNull - private final MetapathExpression target; + private final String target; @Nullable private final MarkupMultiline remarks; @NonNull @@ -87,7 +86,7 @@ protected AbstractConstraint( @Nullable MarkupLine description, @NonNull ISource source, @NonNull Level level, - @NonNull MetapathExpression target, + @NonNull String target, @NonNull Map> properties, @Nullable MarkupMultiline remarks) { Objects.requireNonNull(target); @@ -128,7 +127,7 @@ public Level getLevel() { } @Override - public MetapathExpression getTarget() { + public String getTarget() { return target; } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/impl/AbstractDefinitionTargetedConstraints.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/impl/AbstractDefinitionTargetedConstraints.java index 4755aff5e..11dec4e6a 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/impl/AbstractDefinitionTargetedConstraints.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/impl/AbstractDefinitionTargetedConstraints.java @@ -26,7 +26,6 @@ package gov.nist.secauto.metaschema.core.model.constraint.impl; -import gov.nist.secauto.metaschema.core.metapath.MetapathExpression; import gov.nist.secauto.metaschema.core.model.IAssemblyDefinition; import gov.nist.secauto.metaschema.core.model.IDefinition; import gov.nist.secauto.metaschema.core.model.IFieldDefinition; @@ -62,7 +61,7 @@ public abstract class AbstractDefinitionTargetedConstraints< * the constraints to apply to matching targets */ protected AbstractDefinitionTargetedConstraints( - @NonNull MetapathExpression target, + @NonNull String target, @NonNull S constraints) { super(target, constraints); } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/impl/AbstractKeyConstraint.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/impl/AbstractKeyConstraint.java index 750434bcf..823f92250 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/impl/AbstractKeyConstraint.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/impl/AbstractKeyConstraint.java @@ -28,7 +28,6 @@ import gov.nist.secauto.metaschema.core.datatype.markup.MarkupLine; import gov.nist.secauto.metaschema.core.datatype.markup.MarkupMultiline; -import gov.nist.secauto.metaschema.core.metapath.MetapathExpression; import gov.nist.secauto.metaschema.core.model.IAttributable; import gov.nist.secauto.metaschema.core.model.constraint.IKeyConstraint; import gov.nist.secauto.metaschema.core.model.constraint.IKeyField; @@ -40,8 +39,9 @@ import edu.umd.cs.findbugs.annotations.NonNull; import edu.umd.cs.findbugs.annotations.Nullable; +import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; -public abstract class AbstractKeyConstraint +abstract class AbstractKeyConstraint extends AbstractConstraint implements IKeyConstraint { @NonNull @@ -71,13 +71,14 @@ public abstract class AbstractKeyConstraint * @param remarks * optional remarks describing the intent of the constraint */ + @SuppressFBWarnings("CT_CONSTRUCTOR_THROW") protected AbstractKeyConstraint( @Nullable String id, @Nullable String formalName, @Nullable MarkupLine description, @NonNull ISource source, @NonNull Level level, - @NonNull MetapathExpression target, + @NonNull String target, @NonNull Map> properties, @NonNull List keyFields, @Nullable MarkupMultiline remarks) { diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/impl/DefaultAllowedValuesConstraint.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/impl/DefaultAllowedValuesConstraint.java index 700711159..fbde439b3 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/impl/DefaultAllowedValuesConstraint.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/impl/DefaultAllowedValuesConstraint.java @@ -28,7 +28,6 @@ import gov.nist.secauto.metaschema.core.datatype.markup.MarkupLine; import gov.nist.secauto.metaschema.core.datatype.markup.MarkupMultiline; -import gov.nist.secauto.metaschema.core.metapath.MetapathExpression; import gov.nist.secauto.metaschema.core.metapath.item.atomic.IBooleanItem; import gov.nist.secauto.metaschema.core.model.IAttributable; import gov.nist.secauto.metaschema.core.model.constraint.IAllowedValue; @@ -91,7 +90,7 @@ public DefaultAllowedValuesConstraint( // NOPMD necessary @Nullable MarkupLine description, @NonNull ISource source, @NonNull Level level, - @NonNull MetapathExpression target, + @NonNull String target, @NonNull Map> properties, @NonNull Map allowedValues, boolean allowedOther, diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/impl/DefaultCardinalityConstraint.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/impl/DefaultCardinalityConstraint.java index 5344678f8..eb2450e78 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/impl/DefaultCardinalityConstraint.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/impl/DefaultCardinalityConstraint.java @@ -28,7 +28,6 @@ import gov.nist.secauto.metaschema.core.datatype.markup.MarkupLine; import gov.nist.secauto.metaschema.core.datatype.markup.MarkupMultiline; -import gov.nist.secauto.metaschema.core.metapath.MetapathExpression; import gov.nist.secauto.metaschema.core.model.IAttributable; import gov.nist.secauto.metaschema.core.model.constraint.ICardinalityConstraint; import gov.nist.secauto.metaschema.core.model.constraint.ISource; @@ -83,7 +82,7 @@ public DefaultCardinalityConstraint( @Nullable MarkupLine description, @NonNull ISource source, @NonNull Level level, - @NonNull MetapathExpression target, + @NonNull String target, @NonNull Map> properties, @Nullable Integer minOccurs, @Nullable Integer maxOccurs, diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/impl/DefaultExpectConstraint.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/impl/DefaultExpectConstraint.java index 345ae7ad8..ddd27730f 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/impl/DefaultExpectConstraint.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/impl/DefaultExpectConstraint.java @@ -53,7 +53,7 @@ public final class DefaultExpectConstraint private static final Pattern METAPATH_VALUE_TEMPLATE_PATTERN = Pattern.compile("(?> properties, - @NonNull MetapathExpression test, + @NonNull String test, @Nullable String message, - MarkupMultiline remarks) { + @Nullable MarkupMultiline remarks) { super(id, formalName, description, source, level, target, properties, remarks); this.test = Objects.requireNonNull(test); this.message = message; } @Override - public MetapathExpression getTest() { + public String getTest() { return test; } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/impl/DefaultIndexConstraint.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/impl/DefaultIndexConstraint.java index 2dc16ce3c..ae2a4c79a 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/impl/DefaultIndexConstraint.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/impl/DefaultIndexConstraint.java @@ -28,7 +28,6 @@ import gov.nist.secauto.metaschema.core.datatype.markup.MarkupLine; import gov.nist.secauto.metaschema.core.datatype.markup.MarkupMultiline; -import gov.nist.secauto.metaschema.core.metapath.MetapathExpression; import gov.nist.secauto.metaschema.core.model.IAttributable; import gov.nist.secauto.metaschema.core.model.constraint.IIndexConstraint; import gov.nist.secauto.metaschema.core.model.constraint.IKeyField; @@ -40,6 +39,7 @@ import edu.umd.cs.findbugs.annotations.NonNull; import edu.umd.cs.findbugs.annotations.Nullable; +import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; public final class DefaultIndexConstraint extends AbstractKeyConstraint @@ -73,13 +73,14 @@ public final class DefaultIndexConstraint * optional remarks describing the intent of the constraint */ @SuppressWarnings("PMD.ExcessiveParameterList") + @SuppressFBWarnings(value = "CT_CONSTRUCTOR_THROW", justification = "Use of final fields") public DefaultIndexConstraint( @Nullable String id, @Nullable String formalName, @Nullable MarkupLine description, @NonNull ISource source, @NonNull Level level, - @NonNull MetapathExpression target, + @NonNull String target, @NonNull Map> properties, @NonNull String name, @NonNull List keyFields, diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/impl/DefaultIndexHasKeyConstraint.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/impl/DefaultIndexHasKeyConstraint.java index 54a4e95ed..a6e431332 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/impl/DefaultIndexHasKeyConstraint.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/impl/DefaultIndexHasKeyConstraint.java @@ -28,7 +28,6 @@ import gov.nist.secauto.metaschema.core.datatype.markup.MarkupLine; import gov.nist.secauto.metaschema.core.datatype.markup.MarkupMultiline; -import gov.nist.secauto.metaschema.core.metapath.MetapathExpression; import gov.nist.secauto.metaschema.core.model.IAttributable; import gov.nist.secauto.metaschema.core.model.constraint.IIndexHasKeyConstraint; import gov.nist.secauto.metaschema.core.model.constraint.IKeyField; @@ -80,7 +79,7 @@ public DefaultIndexHasKeyConstraint( @Nullable MarkupLine description, @NonNull ISource source, @NonNull Level level, - @NonNull MetapathExpression target, + @NonNull String target, @NonNull Map> properties, @NonNull String indexName, @NonNull List keyFields, diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/impl/DefaultKeyField.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/impl/DefaultKeyField.java index 13119a4bc..fb06db56c 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/impl/DefaultKeyField.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/impl/DefaultKeyField.java @@ -27,7 +27,6 @@ package gov.nist.secauto.metaschema.core.model.constraint.impl; import gov.nist.secauto.metaschema.core.datatype.markup.MarkupMultiline; -import gov.nist.secauto.metaschema.core.metapath.MetapathExpression; import gov.nist.secauto.metaschema.core.model.constraint.IKeyField; import java.util.regex.Pattern; @@ -40,7 +39,7 @@ public class DefaultKeyField implements IKeyField { @Nullable private final Pattern pattern; @NonNull - private final MetapathExpression target; + private final String target; @Nullable private final MarkupMultiline remarks; @@ -56,7 +55,7 @@ public class DefaultKeyField implements IKeyField { * optional remarks describing the intent of the constraint */ public DefaultKeyField( - @NonNull MetapathExpression target, + @NonNull String target, @Nullable Pattern pattern, @Nullable MarkupMultiline remarks) { this.pattern = pattern; @@ -70,7 +69,7 @@ public Pattern getPattern() { } @Override - public MetapathExpression getTarget() { + public String getTarget() { return target; } @@ -78,5 +77,4 @@ public MetapathExpression getTarget() { public MarkupMultiline getRemarks() { return remarks; } - } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/impl/DefaultLet.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/impl/DefaultLet.java index ae5ca2507..cf30465e1 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/impl/DefaultLet.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/impl/DefaultLet.java @@ -30,11 +30,13 @@ import gov.nist.secauto.metaschema.core.model.constraint.ILet; import gov.nist.secauto.metaschema.core.model.constraint.ISource; +import javax.xml.namespace.QName; + import edu.umd.cs.findbugs.annotations.NonNull; public class DefaultLet implements ILet { @NonNull - private final String name; + private final QName name; @NonNull private final MetapathExpression valueExpression; @NonNull @@ -51,7 +53,7 @@ public class DefaultLet implements ILet { * the source of the let statement */ public DefaultLet( - @NonNull String name, + @NonNull QName name, @NonNull MetapathExpression metapath, @NonNull ISource source) { this.name = name; @@ -60,7 +62,7 @@ public DefaultLet( } @Override - public String getName() { + public QName getName() { return name; } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/impl/DefaultMatchesConstraint.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/impl/DefaultMatchesConstraint.java index ff078ef84..8d832b4dc 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/impl/DefaultMatchesConstraint.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/impl/DefaultMatchesConstraint.java @@ -29,7 +29,6 @@ import gov.nist.secauto.metaschema.core.datatype.IDataTypeAdapter; import gov.nist.secauto.metaschema.core.datatype.markup.MarkupLine; import gov.nist.secauto.metaschema.core.datatype.markup.MarkupMultiline; -import gov.nist.secauto.metaschema.core.metapath.MetapathExpression; import gov.nist.secauto.metaschema.core.model.IAttributable; import gov.nist.secauto.metaschema.core.model.constraint.IMatchesConstraint; import gov.nist.secauto.metaschema.core.model.constraint.ISource; @@ -82,7 +81,7 @@ public DefaultMatchesConstraint( @Nullable MarkupLine description, @NonNull ISource source, @NonNull Level level, - @NonNull MetapathExpression target, + @NonNull String target, @NonNull Map> properties, @Nullable Pattern pattern, @Nullable IDataTypeAdapter dataType, diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/impl/DefaultUniqueConstraint.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/impl/DefaultUniqueConstraint.java index 465194d4f..51e1aa44e 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/impl/DefaultUniqueConstraint.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/impl/DefaultUniqueConstraint.java @@ -28,7 +28,6 @@ import gov.nist.secauto.metaschema.core.datatype.markup.MarkupLine; import gov.nist.secauto.metaschema.core.datatype.markup.MarkupMultiline; -import gov.nist.secauto.metaschema.core.metapath.MetapathExpression; import gov.nist.secauto.metaschema.core.model.IAttributable; import gov.nist.secauto.metaschema.core.model.constraint.IKeyField; import gov.nist.secauto.metaschema.core.model.constraint.ISource; @@ -75,7 +74,7 @@ public DefaultUniqueConstraint( @Nullable MarkupLine description, @NonNull ISource source, @NonNull Level level, - @NonNull MetapathExpression target, + @NonNull String target, @NonNull Map> properties, @NonNull List keyFields, @Nullable MarkupMultiline remarks) { diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/ExternalConstraintsModulePostProcessor.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/ExternalConstraintsModulePostProcessor.java index 30bee9958..bb93284b1 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/ExternalConstraintsModulePostProcessor.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/ExternalConstraintsModulePostProcessor.java @@ -26,9 +26,11 @@ package gov.nist.secauto.metaschema.core.model.xml; +import gov.nist.secauto.metaschema.core.metapath.DynamicContext; import gov.nist.secauto.metaschema.core.metapath.ISequence; import gov.nist.secauto.metaschema.core.metapath.MetapathExpression; import gov.nist.secauto.metaschema.core.metapath.MetapathExpression.ResultType; +import gov.nist.secauto.metaschema.core.metapath.StaticContext; import gov.nist.secauto.metaschema.core.metapath.item.IItem; import gov.nist.secauto.metaschema.core.metapath.item.node.IDefinitionNodeItem; import gov.nist.secauto.metaschema.core.metapath.item.node.IModuleNodeItem; @@ -73,11 +75,17 @@ public void processModule(IModule module) { ConstraintComposingVisitor visitor = new ConstraintComposingVisitor(); IModuleNodeItem moduleItem = INodeItemFactory.instance().newModuleNodeItem(module); + StaticContext staticContext = StaticContext.builder() + .defaultModelNamespace(module.getXmlNamespace()) + .build(); + DynamicContext dynamicContext = new DynamicContext(staticContext); + for (IConstraintSet set : getRegisteredConstraintSets()) { for (ITargetedConstraints targeted : set.getTargetedConstraintsForModule(module)) { // apply targeted constraints - MetapathExpression targetExpression = targeted.getTargetExpression(); - ISequence items = targetExpression.evaluateAs(moduleItem, ResultType.SEQUENCE); + String targetExpression = targeted.getTargetExpression(); + MetapathExpression metapath = MetapathExpression.compile(targetExpression, staticContext); + ISequence items = metapath.evaluateAs(moduleItem, ResultType.SEQUENCE, dynamicContext); if (items != null && !items.isEmpty()) { for (IItem item : items) { diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/XmlConstraintLoader.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/XmlConstraintLoader.java index 33b0ff0b1..f2bd9b3ae 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/XmlConstraintLoader.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/XmlConstraintLoader.java @@ -27,7 +27,6 @@ package gov.nist.secauto.metaschema.core.model.xml; import gov.nist.secauto.metaschema.core.metapath.MetapathException; -import gov.nist.secauto.metaschema.core.metapath.MetapathExpression; import gov.nist.secauto.metaschema.core.model.AbstractLoader; import gov.nist.secauto.metaschema.core.model.IConstraintLoader; import gov.nist.secauto.metaschema.core.model.IModule; @@ -220,35 +219,38 @@ private static void handleScopedAssembly( // NOPMD false positive @NonNull XmlObject obj, Pair> state) { Scope.Assembly assembly = (Scope.Assembly) obj; - MetapathExpression expression = ObjectUtils.requireNonNull(assembly.getTarget()); IModelConstrained constraints = new AssemblyConstraintSet(); ConstraintXmlSupport.parse(constraints, assembly, ObjectUtils.notNull(state.getLeft())); - state.getRight().add(new AssemblyTargetedConstraints(expression, constraints)); + state.getRight().add(new AssemblyTargetedConstraints( + ObjectUtils.requireNonNull(assembly.getTarget()), + constraints)); } private static void handleScopedField( // NOPMD false positive @NonNull XmlObject obj, Pair> state) { Scope.Field field = (Scope.Field) obj; - MetapathExpression expression = ObjectUtils.requireNonNull(field.getTarget()); IValueConstrained constraints = new ValueConstraintSet(); ConstraintXmlSupport.parse(constraints, field, ObjectUtils.notNull(state.getLeft())); - state.getRight().add(new FieldTargetedConstraints(expression, constraints)); + state.getRight().add(new FieldTargetedConstraints( + ObjectUtils.requireNonNull(field.getTarget()), + constraints)); } private static void handleScopedFlag( // NOPMD false positive @NonNull XmlObject obj, Pair> state) { Scope.Flag flag = (Scope.Flag) obj; - MetapathExpression expression = ObjectUtils.requireNonNull(flag.getTarget()); IValueConstrained constraints = new ValueConstraintSet(); ConstraintXmlSupport.parse(constraints, flag, ObjectUtils.notNull(state.getLeft())); - state.getRight().add(new FlagTargetedConstraints(expression, constraints)); + state.getRight().add(new FlagTargetedConstraints( + ObjectUtils.requireNonNull(flag.getTarget()), + constraints)); } } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/XmlMetaConstraintLoader.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/XmlMetaConstraintLoader.java index 016c3326e..98fa49bf2 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/XmlMetaConstraintLoader.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/XmlMetaConstraintLoader.java @@ -26,7 +26,6 @@ package gov.nist.secauto.metaschema.core.model.xml; -import gov.nist.secauto.metaschema.core.metapath.MetapathExpression; import gov.nist.secauto.metaschema.core.model.AbstractLoader; import gov.nist.secauto.metaschema.core.model.IAssemblyDefinition; import gov.nist.secauto.metaschema.core.model.IConstraintLoader; @@ -157,7 +156,6 @@ public Context( public List getTargetedConstraints() { return Stream.concat( getMetapaths().stream() - .map(metapath -> MetapathExpression.compile(ObjectUtils.notNull(metapath))) .map(metapath -> new MetaTargetedContraints(ObjectUtils.notNull(metapath), constraints)), childContexts.stream() .flatMap(child -> child.getTargetedConstraints().stream())) @@ -178,7 +176,7 @@ private static class MetaTargetedContraints implements IFeatureModelConstrained { protected MetaTargetedContraints( - @NonNull MetapathExpression target, + @NonNull String target, @NonNull IModelConstrained constraints) { super(target, constraints); } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/DefaultContainerFlagSupport.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/DefaultContainerFlagSupport.java index 9b44bd5bb..a15cbf6c7 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/DefaultContainerFlagSupport.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/DefaultContainerFlagSupport.java @@ -31,12 +31,14 @@ import java.util.Map; +import javax.xml.namespace.QName; + import edu.umd.cs.findbugs.annotations.NonNull; import edu.umd.cs.findbugs.annotations.Nullable; public class DefaultContainerFlagSupport implements IContainerFlagSupport { @NonNull - private final Map instances; + private final Map instances; @Nullable private final FI jsonKey; @@ -50,14 +52,14 @@ public class DefaultContainerFlagSupport implements IC * configured */ public DefaultContainerFlagSupport( - @NonNull Map instances, + @NonNull Map instances, @Nullable FI jsonKey) { this.instances = instances; this.jsonKey = jsonKey; } @Override - public Map getFlagInstanceMap() { + public Map getFlagInstanceMap() { return instances; } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/DefaultContainerModelAssemblySupport.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/DefaultContainerModelAssemblySupport.java index 65c29ffbb..84cff94b3 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/DefaultContainerModelAssemblySupport.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/DefaultContainerModelAssemblySupport.java @@ -42,6 +42,8 @@ import java.util.List; import java.util.Map; +import javax.xml.namespace.QName; + import edu.umd.cs.findbugs.annotations.NonNull; /** @@ -165,9 +167,9 @@ public DefaultContainerModelAssemblySupport( */ protected DefaultContainerModelAssemblySupport( @NonNull List instances, - @NonNull Map namedModelInstances, - @NonNull Map fieldInstances, - @NonNull Map assemblyInstances, + @NonNull Map namedModelInstances, + @NonNull Map fieldInstances, + @NonNull Map assemblyInstances, @NonNull List choiceInstances, @NonNull Map choiceGroupInstances) { super(instances, namedModelInstances, fieldInstances, assemblyInstances); diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/DefaultContainerModelSupport.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/DefaultContainerModelSupport.java index 7482c7ae4..0ae862d9b 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/DefaultContainerModelSupport.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/DefaultContainerModelSupport.java @@ -40,7 +40,10 @@ import java.util.List; import java.util.Map; +import javax.xml.namespace.QName; + import edu.umd.cs.findbugs.annotations.NonNull; +import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; /** * Provides model container support. @@ -76,11 +79,11 @@ public class DefaultContainerModelSupport< @NonNull private final List modelInstances; @NonNull - private final Map namedModelInstances; + private final Map namedModelInstances; @NonNull - private final Map fieldInstances; + private final Map fieldInstances; @NonNull - private final Map assemblyInstances; + private final Map assemblyInstances; /** * Construct an empty, mutable container. @@ -107,7 +110,8 @@ public DefaultContainerModelSupport() { * the Java type for assembly instances */ @SuppressWarnings({ "PMD.UseConcurrentHashMap" }) - public DefaultContainerModelSupport( + @SuppressFBWarnings(value = "CT_CONSTRUCTOR_THROW", justification = "Use of final fields") + protected DefaultContainerModelSupport( @NonNull Collection instances, @NonNull Class namedModelClass, @NonNull Class fieldClass, @@ -127,13 +131,13 @@ public DefaultContainerModelSupport( this.modelInstances = ObjectUtils.notNull(List.copyOf(instances)); - Map namedModelInstances = new LinkedHashMap<>(); - Map fieldInstances = new LinkedHashMap<>(); - Map assemblyInstances = new LinkedHashMap<>(); + Map namedModelInstances = new LinkedHashMap<>(); + Map fieldInstances = new LinkedHashMap<>(); + Map assemblyInstances = new LinkedHashMap<>(); for (MI instance : instances) { if (namedModelClass.isInstance(instance)) { NMI named = namedModelClass.cast(instance); - String key = named.getEffectiveName(); + QName key = named.getXmlQName(); namedModelInstances.put(key, named); if (fieldClass.isInstance(instance)) { @@ -169,9 +173,9 @@ public DefaultContainerModelSupport( */ protected DefaultContainerModelSupport( @NonNull List modelInstances, - @NonNull Map namedModelInstances, - @NonNull Map fieldInstances, - @NonNull Map assemblyInstances) { + @NonNull Map namedModelInstances, + @NonNull Map fieldInstances, + @NonNull Map assemblyInstances) { this.modelInstances = modelInstances; this.namedModelInstances = namedModelInstances; this.fieldInstances = fieldInstances; @@ -184,17 +188,17 @@ public List getModelInstances() { } @Override - public Map getNamedModelInstanceMap() { + public Map getNamedModelInstanceMap() { return namedModelInstances; } @Override - public Map getFieldInstanceMap() { + public Map getFieldInstanceMap() { return fieldInstances; } @Override - public Map getAssemblyInstanceMap() { + public Map getAssemblyInstanceMap() { return assemblyInstances; } } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/DefaultGroupedModelContainerSupport.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/DefaultGroupedModelContainerSupport.java index 67666163d..749d6baad 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/DefaultGroupedModelContainerSupport.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/DefaultGroupedModelContainerSupport.java @@ -36,7 +36,10 @@ import java.util.LinkedHashMap; import java.util.Map; +import javax.xml.namespace.QName; + import edu.umd.cs.findbugs.annotations.NonNull; +import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; /** * Supports grouped model instance operations on assembly model instances. @@ -62,11 +65,11 @@ public class DefaultGroupedModelContainerSupport< implements IContainerModelSupport { @NonNull - private final Map namedModelInstances; + private final Map namedModelInstances; @NonNull - private final Map fieldInstances; + private final Map fieldInstances; @NonNull - private final Map assemblyInstances; + private final Map assemblyInstances; /** * Construct an empty, mutable container. @@ -90,7 +93,8 @@ public DefaultGroupedModelContainerSupport() { * the Java type for assembly instances */ @SuppressWarnings({ "PMD.UseConcurrentHashMap" }) - public DefaultGroupedModelContainerSupport( + @SuppressFBWarnings(value = "CT_CONSTRUCTOR_THROW", justification = "Use of final fields") + private DefaultGroupedModelContainerSupport( @NonNull Collection instances, @NonNull Class fieldClass, @NonNull Class assemblyClass) { @@ -99,11 +103,11 @@ public DefaultGroupedModelContainerSupport( fieldClass.getName(), assemblyClass.getName()); - Map namedModelInstances = new LinkedHashMap<>(); - Map fieldInstances = new LinkedHashMap<>(); - Map assemblyInstances = new LinkedHashMap<>(); + Map namedModelInstances = new LinkedHashMap<>(); + Map fieldInstances = new LinkedHashMap<>(); + Map assemblyInstances = new LinkedHashMap<>(); for (NMI instance : instances) { - String key = instance.getEffectiveName(); + QName key = instance.getXmlQName(); namedModelInstances.put(key, instance); if (fieldClass.isInstance(instance)) { @@ -135,9 +139,9 @@ public DefaultGroupedModelContainerSupport( * a collection of assembly instances */ protected DefaultGroupedModelContainerSupport( - @NonNull Map namedModelInstances, - @NonNull Map fieldInstances, - @NonNull Map assemblyInstances) { + @NonNull Map namedModelInstances, + @NonNull Map fieldInstances, + @NonNull Map assemblyInstances) { this.namedModelInstances = namedModelInstances; this.fieldInstances = fieldInstances; this.assemblyInstances = assemblyInstances; @@ -150,17 +154,17 @@ public Collection getModelInstances() { } @Override - public Map getNamedModelInstanceMap() { + public Map getNamedModelInstanceMap() { return namedModelInstances; } @Override - public Map getFieldInstanceMap() { + public Map getFieldInstanceMap() { return fieldInstances; } @Override - public Map getAssemblyInstanceMap() { + public Map getAssemblyInstanceMap() { return assemblyInstances; } } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/ModelFactory.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/ModelFactory.java index 49b21d82b..9e5b4acca 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/ModelFactory.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/ModelFactory.java @@ -27,7 +27,6 @@ package gov.nist.secauto.metaschema.core.model.xml.impl; import gov.nist.secauto.metaschema.core.datatype.markup.MarkupMultiline; -import gov.nist.secauto.metaschema.core.metapath.MetapathExpression; import gov.nist.secauto.metaschema.core.model.IAttributable; import gov.nist.secauto.metaschema.core.model.constraint.AbstractConstraintBuilder; import gov.nist.secauto.metaschema.core.model.constraint.AbstractKeyConstraintBuilder; @@ -70,6 +69,8 @@ import java.util.Set; import java.util.stream.Collectors; +import javax.xml.namespace.QName; + import edu.umd.cs.findbugs.annotations.NonNull; import edu.umd.cs.findbugs.annotations.Nullable; @@ -80,8 +81,8 @@ private ModelFactory() { } @NonNull - private static MetapathExpression target(@Nullable MetapathExpression target) { - return target == null ? IConstraint.DEFAULT_TARGET : target; + private static String target(@Nullable String target) { + return target == null ? IConstraint.DEFAULT_TARGET_METAPATH : target; } @NonNull @@ -174,13 +175,13 @@ public static IAllowedValuesConstraint newAllowedValuesConstraint( public static IAllowedValuesConstraint newAllowedValuesConstraint( @NonNull AllowedValuesType xmlObject, @NonNull ISource source) { - return newAllowedValuesConstraint(xmlObject, MetapathExpression.CONTEXT_NODE, source); + return newAllowedValuesConstraint(xmlObject, IConstraint.DEFAULT_TARGET_METAPATH, source); } @NonNull private static IAllowedValuesConstraint newAllowedValuesConstraint( @NonNull AllowedValuesType xmlObject, - @NonNull MetapathExpression target, + @NonNull String target, @NonNull ISource source) { IAllowedValuesConstraint.Builder builder = IAllowedValuesConstraint.builder(); @@ -205,7 +206,7 @@ private static IAllowedValuesConstraint newAllowedValuesConstraint( @NonNull private static > T applyToBuilder( @NonNull ConstraintType xmlObject, - @NonNull MetapathExpression target, + @NonNull String target, @NonNull ISource source, @NonNull T builder) { @@ -247,13 +248,13 @@ public static IMatchesConstraint newMatchesConstraint( public static IMatchesConstraint newMatchesConstraint( @NonNull MatchesConstraintType xmlConstraint, @NonNull ISource source) { - return newMatchesConstraint(xmlConstraint, MetapathExpression.CONTEXT_NODE, source); + return newMatchesConstraint(xmlConstraint, IConstraint.DEFAULT_TARGET_METAPATH, source); } @NonNull private static IMatchesConstraint newMatchesConstraint( @NonNull MatchesConstraintType xmlObject, - @NonNull MetapathExpression target, + @NonNull String target, @NonNull ISource source) { IMatchesConstraint.Builder builder = IMatchesConstraint.builder(); @@ -367,14 +368,14 @@ public static IIndexHasKeyConstraint newIndexHasKeyConstraint( public static IIndexHasKeyConstraint newIndexHasKeyConstraint( @NonNull IndexHasKeyConstraintType xmlObject, @NonNull ISource source) { - return newIndexHasKeyConstraint(xmlObject, MetapathExpression.CONTEXT_NODE, source); + return newIndexHasKeyConstraint(xmlObject, IConstraint.DEFAULT_TARGET_METAPATH, source); } @NonNull private static IIndexHasKeyConstraint newIndexHasKeyConstraint( @NonNull IndexHasKeyConstraintType xmlObject, - @NonNull MetapathExpression target, + @NonNull String target, @NonNull ISource source) { IIndexHasKeyConstraint.Builder builder = IIndexHasKeyConstraint.builder(); @@ -419,13 +420,13 @@ public static IExpectConstraint newExpectConstraint( public static IExpectConstraint newExpectConstraint( @NonNull ExpectConstraintType xmlObject, @NonNull ISource source) { - return newExpectConstraint(xmlObject, MetapathExpression.CONTEXT_NODE, source); + return newExpectConstraint(xmlObject, IConstraint.DEFAULT_TARGET_METAPATH, source); } @NonNull private static IExpectConstraint newExpectConstraint( @NonNull ExpectConstraintType xmlConstraint, - @NonNull MetapathExpression target, + @NonNull String target, @NonNull ISource source) { IExpectConstraint.Builder builder = IExpectConstraint.builder(); @@ -491,8 +492,9 @@ public static ICardinalityConstraint newCardinalityConstraint( public static ILet newLet( @NonNull ConstraintLetType xmlObject, @NonNull ISource source) { + // TODO: figure out how to resolve the namespace prefix on var return ILet.of( - ObjectUtils.notNull(xmlObject.getVar()), + new QName(xmlObject.getVar()), ObjectUtils.notNull(xmlObject.getExpression()), source); } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/XmlAssemblyModelContainer.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/XmlAssemblyModelContainer.java index 7fe54c306..5aa6b2cb1 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/XmlAssemblyModelContainer.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/XmlAssemblyModelContainer.java @@ -198,14 +198,14 @@ private static void handleChoiceGroup( // NOPMD false positive } public void append(@NonNull IFieldInstanceAbsolute instance) { - String key = instance.getEffectiveName(); + QName key = instance.getXmlQName(); getFieldInstanceMap().put(key, instance); getNamedModelInstanceMap().put(key, instance); getModelInstances().add(instance); } public void append(@NonNull IAssemblyInstanceAbsolute instance) { - String key = instance.getEffectiveName(); + QName key = instance.getXmlQName(); getAssemblyInstanceMap().put(key, instance); getNamedModelInstanceMap().put(key, instance); getModelInstances().add(instance); diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/XmlChoiceGroupInstance.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/XmlChoiceGroupInstance.java index 008786c89..9a847300f 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/XmlChoiceGroupInstance.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/XmlChoiceGroupInstance.java @@ -241,13 +241,13 @@ public XmlModelContainer( } public void append(@NonNull IFieldInstanceGrouped instance) { - String key = instance.getEffectiveName(); + QName key = instance.getXmlQName(); getFieldInstanceMap().put(key, instance); getNamedModelInstanceMap().put(key, instance); } public void append(@NonNull IAssemblyInstanceGrouped instance) { - String key = instance.getEffectiveName(); + QName key = instance.getXmlQName(); getAssemblyInstanceMap().put(key, instance); getNamedModelInstanceMap().put(key, instance); } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/XmlChoiceInstance.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/XmlChoiceInstance.java index f34c87605..9c2d24ae0 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/XmlChoiceInstance.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/XmlChoiceInstance.java @@ -220,14 +220,14 @@ public XmlModelContainer( } public void append(@NonNull IFieldInstanceAbsolute instance) { - String key = instance.getEffectiveName(); + QName key = instance.getXmlQName(); getFieldInstanceMap().put(key, instance); getNamedModelInstanceMap().put(key, instance); getModelInstances().add(instance); } public void append(@NonNull IAssemblyInstanceAbsolute instance) { - String key = instance.getEffectiveName(); + QName key = instance.getXmlQName(); getAssemblyInstanceMap().put(key, instance); getNamedModelInstanceMap().put(key, instance); getModelInstances().add(instance); diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/XmlFlagContainerSupport.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/XmlFlagContainerSupport.java index 6a7f4e605..6a9ff11ca 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/XmlFlagContainerSupport.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/XmlFlagContainerSupport.java @@ -59,7 +59,7 @@ class XmlFlagContainerSupport extends DefaultContainerFlagSupport { @SuppressWarnings("PMD.UseConcurrentHashMap") @NonNull - private static final XmlObjectParser>> XML_MODEL_PARSER + private static final XmlObjectParser>> XML_MODEL_PARSER = new XmlObjectParser<>(ObjectUtils.notNull( Map.ofEntries( Map.entry(new QName(IModule.XML_NAMESPACE, "flag"), @@ -68,9 +68,9 @@ class XmlFlagContainerSupport XmlFlagContainerSupport::handleDefineFlag)))) { @Override - protected Handler>> identifyHandler(XmlCursor cursor, + protected Handler>> identifyHandler(XmlCursor cursor, XmlObject obj) { - Handler>> retval; + Handler>> retval; if (obj instanceof FlagReferenceType) { retval = XmlFlagContainerSupport::handleFlag; } else if (obj instanceof InlineFlagDefinitionType) { @@ -84,25 +84,25 @@ protected Handler>> identifyHa private static void handleFlag( // NOPMD false positive @NonNull XmlObject obj, - Pair> state) { + Pair> state) { XmlFlagInstance flagInstance = new XmlFlagInstance( (FlagReferenceType) obj, ObjectUtils.notNull(state.getLeft())); - state.getRight().put(flagInstance.getEffectiveName(), flagInstance); + state.getRight().put(flagInstance.getXmlQName(), flagInstance); } private static void handleDefineFlag( // NOPMD false positive @NonNull XmlObject obj, - Pair> state) { + Pair> state) { XmlInlineFlagDefinition flagInstance = new XmlInlineFlagDefinition( (InlineFlagDefinitionType) obj, ObjectUtils.notNull(state.getLeft())); - state.getRight().put(flagInstance.getEffectiveName(), flagInstance); + state.getRight().put(flagInstance.getXmlQName(), flagInstance); } protected XmlFlagContainerSupport( - @NonNull Map flagInstanceMap, - @Nullable String jsonKeyFlagName) { + @NonNull Map flagInstanceMap, + @Nullable QName jsonKeyFlagName) { super(flagInstanceMap, jsonKeyFlagName == null ? null : ObjectUtils.requireNonNull(flagInstanceMap.get(jsonKeyFlagName))); } @@ -122,7 +122,9 @@ public XmlFlagContainerSupport( xmlField.getFlagList().size() > 0 || xmlField.getDefineFlagList().size() > 0 ? parseLocalFlags(xmlField, container) : CollectionUtil.emptyMap(), - xmlField.isSetJsonKey() ? xmlField.getJsonKey().getFlagRef() : null); + xmlField.isSetJsonKey() + ? container.getContainingModule().toFlagQName(xmlField.getJsonKey().getFlagRef()) + : null); } /** @@ -140,7 +142,9 @@ public XmlFlagContainerSupport( xmlField.getFlagList().size() > 0 || xmlField.getDefineFlagList().size() > 0 ? parseLocalFlags(xmlField, container) : CollectionUtil.emptyMap(), - xmlField.isSetJsonKey() ? xmlField.getJsonKey().getFlagRef() : null); + xmlField.isSetJsonKey() + ? container.getContainingModule().toFlagQName(xmlField.getJsonKey().getFlagRef()) + : null); } /** @@ -176,7 +180,9 @@ public XmlFlagContainerSupport( xmlAssembly.getFlagList().size() > 0 || xmlAssembly.getDefineFlagList().size() > 0 ? parseLocalFlags(xmlAssembly, container) : CollectionUtil.emptyMap(), - xmlAssembly.isSetJsonKey() ? xmlAssembly.getJsonKey().getFlagRef() : null); + xmlAssembly.isSetJsonKey() + ? container.getContainingModule().toFlagQName(xmlAssembly.getJsonKey().getFlagRef()) + : null); } /** @@ -194,7 +200,9 @@ public XmlFlagContainerSupport( xmlAssembly.getFlagList().size() > 0 || xmlAssembly.getDefineFlagList().size() > 0 ? parseLocalFlags(xmlAssembly, container) : CollectionUtil.emptyMap(), - xmlAssembly.isSetJsonKey() ? xmlAssembly.getJsonKey().getFlagRef() : null); + xmlAssembly.isSetJsonKey() + ? container.getContainingModule().toFlagQName(xmlAssembly.getJsonKey().getFlagRef()) + : null); } /** @@ -216,11 +224,11 @@ public XmlFlagContainerSupport( } @NonNull - private static Map parseLocalFlags( + private static Map parseLocalFlags( @NonNull XmlObject xmlObject, @NonNull IModelDefinition parent) { // handle flags - Map flagInstances = new LinkedHashMap<>(); // NOPMD - intentional + Map flagInstances = new LinkedHashMap<>(); // NOPMD - intentional XML_MODEL_PARSER.parse(xmlObject, Pair.of(parent, flagInstances)); diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/XmlGlobalFieldDefinition.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/XmlGlobalFieldDefinition.java index 815d77183..2e5df30d5 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/XmlGlobalFieldDefinition.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/XmlGlobalFieldDefinition.java @@ -47,6 +47,8 @@ import java.util.Map; import java.util.Set; +import javax.xml.namespace.QName; + import edu.umd.cs.findbugs.annotations.NonNull; import edu.umd.cs.findbugs.annotations.Nullable; import nl.talsmasoftware.lazy4j.Lazy; @@ -206,7 +208,10 @@ public boolean hasJsonValueKeyFlagInstance() { public IFlagInstance getJsonValueKeyFlagInstance() { IFlagInstance retval = null; if (getXmlObject().isSetJsonValueKeyFlag() && getXmlObject().getJsonValueKeyFlag().isSetFlagRef()) { - retval = getFlagInstanceByName(ObjectUtils.notNull(getXmlObject().getJsonValueKeyFlag().getFlagRef())); + retval = getFlagInstanceByName( + new QName( + getXmlNamespace(), + ObjectUtils.notNull(getXmlObject().getJsonValueKeyFlag().getFlagRef()))); } return retval; } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/XmlGroupedInlineFieldDefinition.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/XmlGroupedInlineFieldDefinition.java index 34b76cae8..99ed78671 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/XmlGroupedInlineFieldDefinition.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/XmlGroupedInlineFieldDefinition.java @@ -48,6 +48,8 @@ import java.util.Map; import java.util.Set; +import javax.xml.namespace.QName; + import edu.umd.cs.findbugs.annotations.NonNull; import nl.talsmasoftware.lazy4j.Lazy; @@ -181,7 +183,10 @@ public boolean hasJsonValueKeyFlagInstance() { public IFlagInstance getJsonValueKeyFlagInstance() { IFlagInstance retval = null; if (getXmlObject().isSetJsonValueKeyFlag() && getXmlObject().getJsonValueKeyFlag().isSetFlagRef()) { - retval = getFlagInstanceByName(ObjectUtils.notNull(getXmlObject().getJsonValueKeyFlag().getFlagRef())); + retval = getFlagInstanceByName( + new QName( + getXmlNamespace(), + ObjectUtils.notNull(getXmlObject().getJsonValueKeyFlag().getFlagRef()))); } return retval; } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/XmlInlineFieldDefinition.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/XmlInlineFieldDefinition.java index f62d9710d..2cf746f64 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/XmlInlineFieldDefinition.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/XmlInlineFieldDefinition.java @@ -50,6 +50,8 @@ import java.util.Map; import java.util.Set; +import javax.xml.namespace.QName; + import edu.umd.cs.findbugs.annotations.NonNull; import edu.umd.cs.findbugs.annotations.Nullable; import nl.talsmasoftware.lazy4j.Lazy; @@ -229,7 +231,10 @@ public boolean hasJsonValueKeyFlagInstance() { public IFlagInstance getJsonValueKeyFlagInstance() { IFlagInstance retval = null; if (getXmlObject().isSetJsonValueKeyFlag() && getXmlObject().getJsonValueKeyFlag().isSetFlagRef()) { - retval = getFlagInstanceByName(ObjectUtils.notNull(getXmlObject().getJsonValueKeyFlag().getFlagRef())); + retval = getFlagInstanceByName( + new QName( + getXmlNamespace(), + ObjectUtils.notNull(getXmlObject().getJsonValueKeyFlag().getFlagRef()))); } return retval; } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/util/ObjectUtils.java b/core/src/main/java/gov/nist/secauto/metaschema/core/util/ObjectUtils.java index 59681b807..37043a66a 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/util/ObjectUtils.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/util/ObjectUtils.java @@ -31,6 +31,7 @@ import edu.umd.cs.findbugs.annotations.NonNull; import edu.umd.cs.findbugs.annotations.Nullable; +import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; public final class ObjectUtils { private ObjectUtils() { @@ -50,7 +51,7 @@ private ObjectUtils() { * @return the object */ @NonNull - public static T notNull(T obj) { + public static T notNull(@Nullable T obj) { assert obj != null; return obj; } @@ -67,7 +68,8 @@ public static T notNull(T obj) { * if {@code obj} is {@code null} */ @NonNull - public static T requireNonNull(T obj) { + @SuppressFBWarnings("NP_PARAMETER_MUST_BE_NONNULL_BUT_MARKED_AS_NULLABLE") + public static T requireNonNull(@Nullable T obj) { if (obj == null) { throw new NullPointerException(); // NOPMD } @@ -89,7 +91,8 @@ public static T requireNonNull(T obj) { * if {@code obj} is {@code null} */ @NonNull - public static T requireNonNull(T obj, String message) { + @SuppressFBWarnings("NP_PARAMETER_MUST_BE_NONNULL_BUT_MARKED_AS_NULLABLE") + public static T requireNonNull(@Nullable T obj, @NonNull String message) { if (obj == null) { throw new NullPointerException(message); // NOPMD } diff --git a/core/src/schema/xmlconfig.xml b/core/src/schema/xmlconfig.xml index 554903bfa..73d4d6534 100644 --- a/core/src/schema/xmlconfig.xml +++ b/core/src/schema/xmlconfig.xml @@ -42,10 +42,12 @@ javaname="gov.nist.secauto.metaschema.core.datatype.IDataTypeAdapter"> gov.nist.secauto.metaschema.core.model.xml.xmlbeans.handler.DatatypesHandler + gov.nist.secauto.metaschema.core.model.xml.xmlbeans.handler.RegexTypeHandler diff --git a/core/src/test/java/gov/nist/secauto/metaschema/core/metapath/ExpressionTestBase.java b/core/src/test/java/gov/nist/secauto/metaschema/core/metapath/ExpressionTestBase.java index 745ec8ca0..3c8946660 100644 --- a/core/src/test/java/gov/nist/secauto/metaschema/core/metapath/ExpressionTestBase.java +++ b/core/src/test/java/gov/nist/secauto/metaschema/core/metapath/ExpressionTestBase.java @@ -60,9 +60,9 @@ protected Mockery getContext() { protected static DynamicContext newDynamicContext() { URI baseUri = ObjectUtils.notNull(new File("").getAbsoluteFile().toURI()); - return StaticContext.builder() + return new DynamicContext(StaticContext.builder() .baseUri(baseUri) - .build().dynamicContext(); + .build()); } @NonNull diff --git a/core/src/test/java/gov/nist/secauto/metaschema/core/metapath/TestUtils.java b/core/src/test/java/gov/nist/secauto/metaschema/core/metapath/TestUtils.java index 65da46bd5..7b7a4c897 100644 --- a/core/src/test/java/gov/nist/secauto/metaschema/core/metapath/TestUtils.java +++ b/core/src/test/java/gov/nist/secauto/metaschema/core/metapath/TestUtils.java @@ -105,7 +105,7 @@ public static ISequence executeFunction( @Nullable ISequence focus, List> arguments) { - DynamicContext context = dynamicContext == null ? StaticContext.instance().dynamicContext() : dynamicContext; + DynamicContext context = dynamicContext == null ? new DynamicContext() : dynamicContext; ISequence focusSeqence = function.isFocusDepenent() ? ObjectUtils.requireNonNull(focus, "Function call requires a focus") : ISequence.empty(); diff --git a/core/src/test/java/gov/nist/secauto/metaschema/core/metapath/cst/ArrowExpressionTest.java b/core/src/test/java/gov/nist/secauto/metaschema/core/metapath/cst/ArrowExpressionTest.java index ba0fde5c8..67b7689bb 100644 --- a/core/src/test/java/gov/nist/secauto/metaschema/core/metapath/cst/ArrowExpressionTest.java +++ b/core/src/test/java/gov/nist/secauto/metaschema/core/metapath/cst/ArrowExpressionTest.java @@ -28,7 +28,6 @@ import static gov.nist.secauto.metaschema.core.metapath.TestUtils.bool; import static gov.nist.secauto.metaschema.core.metapath.TestUtils.integer; -import static gov.nist.secauto.metaschema.core.metapath.TestUtils.string; import static org.junit.jupiter.api.Assertions.assertEquals; import gov.nist.secauto.metaschema.core.metapath.ExpressionTestBase; @@ -47,7 +46,7 @@ class ArrowExpressionTest extends ExpressionTestBase { private static Stream provideValues() { // NOPMD - false positive return Stream.of( - Arguments.of(ISequence.of(string("true")), "true() => string()"), + // Arguments.of(ISequence.of(string("true")), "true() => string()"), Arguments.of(ISequence.of(bool(false)), "() => exists()"), Arguments.of(ISequence.of(integer(3)), "(1, 2) => sum()")); } diff --git a/core/src/test/java/gov/nist/secauto/metaschema/core/metapath/cst/BuildCstVisitorTest.java b/core/src/test/java/gov/nist/secauto/metaschema/core/metapath/cst/BuildCstVisitorTest.java index 90d253a10..494fb5f0a 100644 --- a/core/src/test/java/gov/nist/secauto/metaschema/core/metapath/cst/BuildCstVisitorTest.java +++ b/core/src/test/java/gov/nist/secauto/metaschema/core/metapath/cst/BuildCstVisitorTest.java @@ -79,10 +79,21 @@ import java.util.List; import java.util.stream.Stream; +import javax.xml.namespace.QName; + import edu.umd.cs.findbugs.annotations.NonNull; @SuppressWarnings("PMD.TooManyStaticImports") class BuildCstVisitorTest { + private static final URI NS_URI = URI.create("http://example.com/ns"); + private static final String NS = NS_URI.toASCIIString(); + + private static final QName ROOT = new QName(NS, "root"); + private static final QName FIELD1 = new QName(NS, "field1"); + private static final QName FIELD2 = new QName(NS, "field2"); + private static final QName UUID = new QName(NS, "uuid"); + private static final QName FLAG = new QName("flag"); + @RegisterExtension Mockery context = new JUnit5Mockery(); @@ -90,19 +101,21 @@ class BuildCstVisitorTest { @NonNull private IDocumentNodeItem newTestDocument() { MockNodeItemFactory factory = new MockNodeItemFactory(context); - return factory.document(URI.create("http://example.com/content"), "root", + + return factory.document(URI.create("http://example.com/content"), ROOT, List.of( - factory.flag("uuid", IUuidItem.random())), + factory.flag(UUID, IUuidItem.random())), List.of( - factory.field("field1", IStringItem.valueOf("field1")), - factory.field("field2", IStringItem.valueOf("field2"), // NOPMD - List.of(factory.flag("flag", IStringItem.valueOf("field2-flag")))))); + factory.field(FIELD1, IStringItem.valueOf("field1")), + factory.field(FIELD2, IStringItem.valueOf("field2"), // NOPMD + List.of(factory.flag(FLAG, IStringItem.valueOf("field2-flag")))))); } @NonNull - private static DynamicContext newDynamicContext() { + private StaticContext newStaticContext() { return StaticContext.builder() - .build().dynamicContext(); + .defaultModelNamespace(NS_URI) + .build(); } private static IExpression parseExpression(@NonNull String path) { @@ -116,128 +129,147 @@ private static IExpression parseExpression(@NonNull String path) { // ParseTreePrinter cstPrinter = new ParseTreePrinter(System.out); // cstPrinter.print(tree, Arrays.asList(parser.getRuleNames())); - return new BuildCSTVisitor().visit(tree); + return new BuildCSTVisitor(StaticContext.instance()).visit(tree); } @Test void testAbbreviatedParentAxis() { + StaticContext staticContext = newStaticContext(); // compile expression String path = "../field2"; - IExpression ast = parseExpression(path); + MetapathExpression expr = MetapathExpression.compile(path, staticContext); // select starting node IDocumentNodeItem document = newTestDocument(); - IFieldNodeItem field = MetapathExpression.compile("/root/field1").evaluateAs(document, ResultType.NODE); + IFieldNodeItem field + = MetapathExpression.compile("/root/field1", staticContext).evaluateAs(document, ResultType.NODE); assert field != null; // evaluate - ISequence result = ast.accept(newDynamicContext(), ISequence.of(field)); + ISequence result = expr.evaluate(field); assertThat(result.asList(), contains( allOf( instanceOf(IFieldNodeItem.class), - hasProperty("name", equalTo("field2"))))); // NOPMD + hasProperty("name", equalTo(FIELD2))))); // NOPMD } @Test void testParentAxisMatch() { + StaticContext staticContext = newStaticContext(); + // select starting node IDocumentNodeItem document = newTestDocument(); - IFieldNodeItem field = MetapathExpression.compile("/root/field1").evaluateAs(document, ResultType.NODE); + IFieldNodeItem field = MetapathExpression.compile("/root/field1", staticContext) + .evaluateAs(document, ResultType.NODE); assert field != null; // compile expression - IItem result = MetapathExpression.compile("parent::root").evaluateAs(field, ResultType.NODE); + IItem result = MetapathExpression.compile("parent::root", staticContext) + .evaluateAs(field, ResultType.NODE); assert result != null; assertAll( () -> assertInstanceOf(IRootAssemblyNodeItem.class, result), - () -> assertEquals("root", ((IRootAssemblyNodeItem) result).getName())); + () -> assertEquals(ROOT, ((IRootAssemblyNodeItem) result).getName())); } @Test void testParentAxisNonMatch() { + StaticContext staticContext = newStaticContext(); + // select starting node IDocumentNodeItem document = newTestDocument(); - IFieldNodeItem field = MetapathExpression.compile("/root/field1").evaluateAs(document, ResultType.NODE); + IFieldNodeItem field = MetapathExpression.compile("/root/field1", staticContext) + .evaluateAs(document, ResultType.NODE); assert field != null; // compile expression String path = "parent::other"; - IExpression ast = parseExpression(path); + MetapathExpression expr = MetapathExpression.compile(path, staticContext); // evaluate - ISequence result = ast.accept(newDynamicContext(), ISequence.of(field)); + ISequence result = expr.evaluate(field); assertTrue(result.isEmpty()); } @Test void testParentAxisDocument() { + StaticContext staticContext = newStaticContext(); + // compile expression String path = "parent::other"; - IExpression ast = parseExpression(path); + MetapathExpression expr = MetapathExpression.compile(path, staticContext); // select starting node IDocumentNodeItem document = newTestDocument(); // evaluate - ISequence result = ast.accept(newDynamicContext(), ISequence.of(document)); + ISequence result = expr.evaluate(document); assertTrue(result.isEmpty()); } @Test void testAbbreviatedForwardAxisModelName() { + StaticContext staticContext = newStaticContext(); + String path = "./root"; - IExpression ast = parseExpression(path); + MetapathExpression expr = MetapathExpression.compile(path, staticContext); // select starting node IDocumentNodeItem document = newTestDocument(); // evaluate - ISequence result = ast.accept(newDynamicContext(), ISequence.of(document)); + ISequence result = expr.evaluate(document); assertThat(result.asList(), contains( allOf( instanceOf(IRootAssemblyNodeItem.class), - hasProperty("name", equalTo("root"))))); + hasProperty("name", equalTo(ROOT))))); } @Test void testAbbreviatedForwardAxisFlagName() { + StaticContext staticContext = newStaticContext(); + String path = "./@flag"; - IExpression ast = parseExpression(path); + MetapathExpression expr = MetapathExpression.compile(path, staticContext); // select starting node IDocumentNodeItem document = newTestDocument(); - IFieldNodeItem field = MetapathExpression.compile("/root/field2").evaluateAs(document, ResultType.NODE); + IFieldNodeItem field = MetapathExpression.compile("/root/field2", staticContext) + .evaluateAs(document, ResultType.NODE); assert field != null; // evaluate - ISequence result = ast.accept(newDynamicContext(), ISequence.of(field)); + ISequence result = expr.evaluate(field); assertThat(result.asList(), contains( allOf( instanceOf(IFlagNodeItem.class), - hasProperty("name", equalTo("flag"))))); + hasProperty("name", equalTo(FLAG))))); } @Test void testForwardstepChild() { + StaticContext staticContext = newStaticContext(); + String path = "child::*"; - IExpression ast = parseExpression(path); + MetapathExpression expr = MetapathExpression.compile(path, staticContext); // select starting node IDocumentNodeItem document = newTestDocument(); - IRootAssemblyNodeItem root = MetapathExpression.compile("/root").evaluateAs(document, ResultType.NODE); + IRootAssemblyNodeItem root = MetapathExpression.compile("/root", staticContext) + .evaluateAs(document, ResultType.NODE, new DynamicContext(staticContext)); assert root != null; // evaluate - ISequence result = ast.accept(newDynamicContext(), ISequence.of(root)); + ISequence result = expr.evaluate(root); assertThat(result.asList(), contains( allOf( instanceOf(IFieldNodeItem.class), - hasProperty("name", equalTo("field1"))), // NOPMD + hasProperty("name", equalTo(FIELD1))), // NOPMD allOf( instanceOf(IFieldNodeItem.class), - hasProperty("name", equalTo("field2"))))); // NOPMD + hasProperty("name", equalTo(FIELD2))))); // NOPMD } static Stream testComparison() { @@ -283,7 +315,7 @@ void testAnd(@NonNull String metapath, @NonNull IBooleanItem expectedResult) { IExpression ast = parseExpression(metapath); IDocumentNodeItem document = newTestDocument(); - ISequence result = ast.accept(newDynamicContext(), ISequence.of(document)); + ISequence result = ast.accept(new DynamicContext(), ISequence.of(document)); IItem resultItem = FunctionUtils.getFirstItem(result, false); assertAll( () -> assertEquals(And.class, ast.getClass()), @@ -308,7 +340,7 @@ void testIf(@NonNull String metapath, @NonNull IBooleanItem expectedResult) { IExpression ast = parseExpression(metapath); IDocumentNodeItem document = newTestDocument(); - ISequence result = ast.accept(newDynamicContext(), ISequence.of(document)); + ISequence result = ast.accept(new DynamicContext(), ISequence.of(document)); IItem resultItem = FunctionUtils.getFirstItem(result, false); assertAll( () -> assertEquals(If.class, ast.getClass()), @@ -339,7 +371,7 @@ void testFor(@NonNull String metapath, @NonNull ISequence expectedResult) { IExpression ast = parseExpression(metapath); IDocumentNodeItem document = newTestDocument(); - ISequence result = ast.accept(newDynamicContext(), ISequence.of(document)); + ISequence result = ast.accept(new DynamicContext(), ISequence.of(document)); assertAll( () -> assertEquals(For.class, ast.getClass()), () -> assertNotNull(result), @@ -366,7 +398,7 @@ void testSimpleMap(@NonNull String metapath, @NonNull ISequence expectedResul IExpression ast = parseExpression(metapath); IDocumentNodeItem document = newTestDocument(); - ISequence result = ast.accept(newDynamicContext(), ISequence.of(document)); + ISequence result = ast.accept(new DynamicContext(), ISequence.of(document)); assertAll( () -> assertEquals(SimpleMap.class, ast.getClass()), () -> assertNotNull(result), diff --git a/core/src/test/java/gov/nist/secauto/metaschema/core/metapath/cst/EQNameUtilsTest.java b/core/src/test/java/gov/nist/secauto/metaschema/core/metapath/cst/EQNameUtilsTest.java new file mode 100644 index 000000000..64678e516 --- /dev/null +++ b/core/src/test/java/gov/nist/secauto/metaschema/core/metapath/cst/EQNameUtilsTest.java @@ -0,0 +1,116 @@ +/* + * Portions of this software was developed by employees of the National Institute + * of Standards and Technology (NIST), an agency of the Federal Government and is + * being made available as a public service. Pursuant to title 17 United States + * Code Section 105, works of NIST employees are not subject to copyright + * protection in the United States. This software may be subject to foreign + * copyright. Permission in the United States and in foreign countries, to the + * extent that NIST may hold copyright, to use, copy, modify, create derivative + * works, and distribute this software and its documentation without fee is hereby + * granted on a non-exclusive basis, provided that this notice and disclaimer + * of warranty appears in all copies. + * + * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER + * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY + * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM + * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE + * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT + * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, + * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, + * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, + * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR + * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT + * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. + */ + +package gov.nist.secauto.metaschema.core.metapath.cst; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import gov.nist.secauto.metaschema.core.metapath.EQNameUtils; +import gov.nist.secauto.metaschema.core.metapath.EQNameUtils.IEQNamePrefixResolver; +import gov.nist.secauto.metaschema.core.metapath.StaticContext; + +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +import java.util.stream.Stream; + +import javax.xml.namespace.QName; + +import edu.umd.cs.findbugs.annotations.NonNull; + +class EQNameUtilsTest { + private static final StaticContext STATIC_CONTEXT = StaticContext.builder() + .namespace("prefix", "http://example.com/ns/prefix") + .defaultFunctionNamespace("http://example.com/ns/function") + .defaultModelNamespace("http://example.com/ns/model") + .build(); + + private enum NameType { + FUNCTION, + FLAG, + MODEL; + } + + static Stream provideValues() { + return Stream.of( + Arguments.of( + "prefix:local-name", + new QName("http://example.com/ns/prefix", "local-name"), + STATIC_CONTEXT.getFunctionPrefixResolver()), + Arguments.of( + "prefix:local-name", + new QName("http://example.com/ns/prefix", "local-name"), + STATIC_CONTEXT.getFlagPrefixResolver()), + Arguments.of( + "prefix:local-name", + new QName("http://example.com/ns/prefix", "local-name"), + STATIC_CONTEXT.getVariablePrefixResolver()), + Arguments.of( + "prefix:local-name", + new QName("http://example.com/ns/prefix", "local-name"), + STATIC_CONTEXT.getModelPrefixResolver()), + Arguments.of( + "local-name", + new QName("http://example.com/ns/function", "local-name"), + STATIC_CONTEXT.getFunctionPrefixResolver()), + Arguments.of( + "local-name", + new QName("local-name"), + STATIC_CONTEXT.getFlagPrefixResolver()), + Arguments.of( + "local-name", + new QName("local-name"), + STATIC_CONTEXT.getVariablePrefixResolver()), + Arguments.of( + "local-name", + new QName("http://example.com/ns/model", "local-name"), + STATIC_CONTEXT.getModelPrefixResolver()), + Arguments.of("Q{http://example.com/ns}local-name", + new QName("http://example.com/ns", "local-name"), + STATIC_CONTEXT.getFunctionPrefixResolver()), + Arguments.of("Q{http://example.com/ns}local-name", + new QName("http://example.com/ns", "local-name"), + STATIC_CONTEXT.getFlagPrefixResolver()), + Arguments.of("Q{http://example.com/ns}local-name", + new QName("http://example.com/ns", "local-name"), + STATIC_CONTEXT.getVariablePrefixResolver()), + Arguments.of("Q{http://example.com/ns}local-name", + new QName("http://example.com/ns", "local-name"), + STATIC_CONTEXT.getModelPrefixResolver())); + } + + @ParameterizedTest + @MethodSource("provideValues") + void test( + @NonNull String eqname, + @NonNull QName expected, + @NonNull IEQNamePrefixResolver resolver) { + + QName actual = EQNameUtils.parseName(eqname, resolver); + assertEquals(expected, actual); + } +} diff --git a/core/src/test/java/gov/nist/secauto/metaschema/core/metapath/cst/FlagTest.java b/core/src/test/java/gov/nist/secauto/metaschema/core/metapath/cst/FlagTest.java index 973e29692..d642fdd9c 100644 --- a/core/src/test/java/gov/nist/secauto/metaschema/core/metapath/cst/FlagTest.java +++ b/core/src/test/java/gov/nist/secauto/metaschema/core/metapath/cst/FlagTest.java @@ -32,6 +32,7 @@ import gov.nist.secauto.metaschema.core.metapath.ExpressionTestBase; import gov.nist.secauto.metaschema.core.metapath.ISequence; import gov.nist.secauto.metaschema.core.metapath.cst.path.Flag; +import gov.nist.secauto.metaschema.core.metapath.cst.path.NameTest; import gov.nist.secauto.metaschema.core.metapath.item.node.IFlagNodeItem; import gov.nist.secauto.metaschema.core.metapath.item.node.IModelNodeItem; import gov.nist.secauto.metaschema.core.metapath.item.node.NodeItemType; @@ -41,6 +42,8 @@ import org.jmock.Mockery; import org.junit.jupiter.api.Test; +import javax.xml.namespace.QName; + import edu.umd.cs.findbugs.annotations.NonNull; class FlagTest @@ -57,7 +60,7 @@ void testFlagWithName() { IFlagInstance instance = context.mock(IFlagInstance.class); IFlagNodeItem flagNode = context.mock(IFlagNodeItem.class); - String flagName = "test"; + QName flagName = new QName("test"); context.checking(new Expectations() { { // NOPMD - intentional @@ -77,7 +80,7 @@ void testFlagWithName() { } }); - Flag expr = new Flag(new Name(flagName)); + Flag expr = new Flag(new NameTest(flagName)); ISequence result = expr.accept(dynamicContext, ISequence.of(focusItem)); assertEquals(ISequence.of(flagNode), result, "Sequence does not match"); diff --git a/core/src/test/java/gov/nist/secauto/metaschema/core/metapath/cst/PredicateTest.java b/core/src/test/java/gov/nist/secauto/metaschema/core/metapath/cst/PredicateTest.java index 8e485412d..6ab8ee9a8 100644 --- a/core/src/test/java/gov/nist/secauto/metaschema/core/metapath/cst/PredicateTest.java +++ b/core/src/test/java/gov/nist/secauto/metaschema/core/metapath/cst/PredicateTest.java @@ -45,7 +45,7 @@ import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; @SuppressFBWarnings("RV_RETURN_VALUE_IGNORED_NO_SIDE_EFFECT") -class PredicateTest +class PredicateExpressionTest extends ExpressionTestBase { @Test @@ -83,7 +83,7 @@ void testPredicateWithValues() { } }); - Predicate expr = new Predicate(stepExpr, predicates); + PredicateExpression expr = new PredicateExpression(stepExpr, predicates); ISequence result = expr.accept(dynamicContext, ISequence.of(item)); assertEquals(ISequence.of(item), result, "Sequence does not match"); @@ -122,7 +122,7 @@ void testPredicateWithoutValues() { } }); - Predicate expr = new Predicate(stepExpr, predicates); + PredicateExpression expr = new PredicateExpression(stepExpr, predicates); ISequence result = expr.accept(dynamicContext, ISequence.of(item)); assertEquals(ISequence.of(item), result, "Sequence does not match"); diff --git a/core/src/test/java/gov/nist/secauto/metaschema/core/metapath/item/node/DefaultNodeItemFactoryTest.java b/core/src/test/java/gov/nist/secauto/metaschema/core/metapath/item/node/DefaultNodeItemFactoryTest.java index 2e1206b16..e2347a2d7 100644 --- a/core/src/test/java/gov/nist/secauto/metaschema/core/metapath/item/node/DefaultNodeItemFactoryTest.java +++ b/core/src/test/java/gov/nist/secauto/metaschema/core/metapath/item/node/DefaultNodeItemFactoryTest.java @@ -10,33 +10,44 @@ import gov.nist.secauto.metaschema.core.model.IAssemblyDefinition; import gov.nist.secauto.metaschema.core.model.IFieldInstance; import gov.nist.secauto.metaschema.core.testing.MockedModelTestSupport; +import gov.nist.secauto.metaschema.core.util.ObjectUtils; import org.hamcrest.FeatureMatcher; import org.hamcrest.Matcher; import org.jmock.Expectations; import org.junit.jupiter.api.Test; +import java.net.URI; import java.util.Collection; import java.util.List; import java.util.function.Function; import java.util.stream.Collectors; +import javax.xml.namespace.QName; + import edu.umd.cs.findbugs.annotations.NonNull; class DefaultNodeItemFactoryTest extends MockedModelTestSupport { + @NonNull + private static final URI NS_URI = ObjectUtils.notNull(URI.create("http://example.com/ns")); + @NonNull + private static final String NS = ObjectUtils.notNull(NS_URI.toASCIIString()); + @Test void testGenerateFlags() { DefaultNodeItemFactory nodeFactory = DefaultNodeItemFactory.instance(); IAssemblyDefinition parent = assembly() + .namespace(NS_URI) .name("assembly1") .toDefinition(); IFieldInstance fieldInstance = field() + .namespace(NS_URI) .name("field1") .flags(List.of( - flag().name("flag1"))) + flag().namespace(NS_URI).name("flag1"))) .toInstance(parent); Object fieldValue = "test value"; @@ -44,7 +55,7 @@ void testGenerateFlags() { // setup the value calls getContext().checking(new Expectations() { { // NOPMD - intentional - allowing(fieldInstance.getDefinition().getFlagInstanceByName("flag1")).getValue(fieldValue); + allowing(fieldInstance.getDefinition().getFlagInstanceByName(new QName(NS, "flag1"))).getValue(fieldValue); will(returnValue("flag1 value")); } }); @@ -55,18 +66,19 @@ void testGenerateFlags() { Collection flagItems = field.getFlags(); assertThat(flagItems, containsInAnyOrder( allOf( - match("name", flag -> flag.getName(), equalTo("flag1")), + match("name", flag -> flag.getName(), equalTo(new QName(NS, "flag1"))), match("value", flag -> flag.getValue(), equalTo("flag1 value"))))); // NOPMD } @Test void testGenerateModelItems() { IAssemblyDefinition assembly = assembly() + .namespace(NS_URI) .name("assembly1") .flags(List.of( - flag().name("flag1"))) + flag().namespace(NS_URI).name("flag1"))) .modelInstances(List.of( - field().name("field1"))) + field().namespace(NS_URI).name("field1"))) .toDefinition(); Object assemblyValue = "assembly value"; @@ -76,11 +88,11 @@ void testGenerateModelItems() { // Setup the value calls getContext().checking(new Expectations() { { // NOPMD - intentional - allowing(assembly.getFlagInstanceByName("flag1")).getValue(assemblyValue); + allowing(assembly.getFlagInstanceByName(new QName(NS, "flag1"))).getValue(assemblyValue); will(returnValue(flagValue)); - allowing(assembly.getNamedModelInstanceByName("field1")).getValue(assemblyValue); + allowing(assembly.getNamedModelInstanceByName(new QName(NS, "field1"))).getValue(assemblyValue); will(returnValue(fieldValue)); - allowing(assembly.getNamedModelInstanceByName("field1")).getItemValues(fieldValue); + allowing(assembly.getNamedModelInstanceByName(new QName(NS, "field1"))).getItemValues(fieldValue); will(returnValue(List.of(fieldValue))); } }); @@ -93,11 +105,11 @@ void testGenerateModelItems() { assertAll( () -> assertThat(flagItems, containsInAnyOrder( allOf( - match("name", flag -> flag.getName(), equalTo("flag1")), + match("name", flag -> flag.getName(), equalTo(new QName(NS, "flag1"))), match("value", flag -> flag.getValue(), equalTo("flag1 value"))))), () -> assertThat(modelItems, containsInAnyOrder( allOf( - match("name", model -> model.getName(), equalTo("field1")), + match("name", model -> model.getName(), equalTo(new QName(NS, "field1"))), match("value", model -> model.getValue(), equalTo("field1 value")))))); } diff --git a/core/src/test/java/gov/nist/secauto/metaschema/core/metapath/item/node/MockNodeItemFactory.java b/core/src/test/java/gov/nist/secauto/metaschema/core/metapath/item/node/MockNodeItemFactory.java index eeec8108b..f6d99a46c 100644 --- a/core/src/test/java/gov/nist/secauto/metaschema/core/metapath/item/node/MockNodeItemFactory.java +++ b/core/src/test/java/gov/nist/secauto/metaschema/core/metapath/item/node/MockNodeItemFactory.java @@ -28,6 +28,7 @@ import gov.nist.secauto.metaschema.core.metapath.item.atomic.IAnyAtomicItem; import gov.nist.secauto.metaschema.core.util.CollectionUtil; +import gov.nist.secauto.metaschema.core.util.ObjectUtils; import org.hamcrest.Description; import org.jmock.Expectations; @@ -44,6 +45,8 @@ import java.util.concurrent.atomic.AtomicInteger; import java.util.stream.Stream; +import javax.xml.namespace.QName; + import edu.umd.cs.findbugs.annotations.NonNull; import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; @@ -76,11 +79,12 @@ protected T newMock(@NonNull Class clazz, @NonNull Stri return getContext().mock(clazz, mockName); } - public IDocumentNodeItem document(@NonNull URI documentURI, @NonNull String name, + public IDocumentNodeItem document(@NonNull URI documentURI, @NonNull QName rootName, @NonNull List flags, @NonNull List> modelItems) { - IDocumentNodeItem document = newMock(IDocumentNodeItem.class, name); - IRootAssemblyNodeItem root = newMock(IRootAssemblyNodeItem.class, name); + String qname = ObjectUtils.notNull(rootName.toString()); + IDocumentNodeItem document = newMock(IDocumentNodeItem.class, qname); + IRootAssemblyNodeItem root = newMock(IRootAssemblyNodeItem.class, qname); getContext().checking(new Expectations() { { // NOPMD - intentional @@ -96,7 +100,7 @@ public IDocumentNodeItem document(@NonNull URI documentURI, @NonNull String name will(returnValue(Stream.of(document))); allowing(root).getName(); - will(returnValue(name)); + will(returnValue(rootName)); allowing(root).getNodeItem(); will(returnValue(root)); allowing(root).getDocumentNodeItem(); @@ -128,7 +132,7 @@ protected void handleChildren( will(returnValue(item)); }); - Map>> modelItemsMap = toModelItemsMap(modelItems); + Map>> modelItemsMap = toModelItemsMap(modelItems); allowing(item).getModelItems(); will(returnValue(modelItemsMap.values())); modelItemsMap.entrySet().forEach(entry -> { @@ -168,12 +172,12 @@ public Object invoke(Invocation invocation) { @SuppressWarnings("static-method") @NonNull - private Map>> + private Map>> toModelItemsMap(@NonNull List> modelItems) { - Map>> retval = new LinkedHashMap<>(); // NOPMD - intentional + Map>> retval = new LinkedHashMap<>(); // NOPMD - intentional for (IModelNodeItem item : modelItems) { - String name = item.getName(); + QName name = item.getName(); List> namedItems = retval.get(name); if (namedItems == null) { namedItems = new LinkedList<>(); // NOPMD - intentional @@ -185,8 +189,8 @@ public Object invoke(Invocation invocation) { } @NonNull - public IFlagNodeItem flag(@NonNull String name, @NonNull IAnyAtomicItem value) { - IFlagNodeItem retval = newMock(IFlagNodeItem.class, name); + public IFlagNodeItem flag(@NonNull QName name, @NonNull IAnyAtomicItem value) { + IFlagNodeItem retval = newMock(IFlagNodeItem.class, ObjectUtils.notNull(name.toString())); getContext().checking(new Expectations() { { // NOPMD - intentional @@ -210,14 +214,14 @@ public IFlagNodeItem flag(@NonNull String name, @NonNull IAnyAtomicItem value) { } @NonNull - public IFieldNodeItem field(@NonNull String name, @NonNull IAnyAtomicItem value) { + public IFieldNodeItem field(@NonNull QName name, @NonNull IAnyAtomicItem value) { return field(name, value, CollectionUtil.emptyList()); } @NonNull - public IFieldNodeItem field(@NonNull String name, @NonNull IAnyAtomicItem value, + public IFieldNodeItem field(@NonNull QName name, @NonNull IAnyAtomicItem value, @NonNull List flags) { - IFieldNodeItem retval = newMock(IFieldNodeItem.class, name); + IFieldNodeItem retval = newMock(IFieldNodeItem.class, ObjectUtils.notNull(name.toString())); getContext().checking(new Expectations() { { // NOPMD - intentional @@ -240,9 +244,9 @@ public IFieldNodeItem field(@NonNull String name, @NonNull IAnyAtomicItem value, } @NonNull - public IAssemblyNodeItem assembly(@NonNull String name, @NonNull List flags, + public IAssemblyNodeItem assembly(@NonNull QName name, @NonNull List flags, @NonNull List> modelItems) { - IAssemblyNodeItem retval = newMock(IAssemblyNodeItem.class, name); + IAssemblyNodeItem retval = newMock(IAssemblyNodeItem.class, ObjectUtils.notNull(name.toString())); getContext().checking(new Expectations() { { // NOPMD - intentional diff --git a/core/src/test/java/gov/nist/secauto/metaschema/core/model/constraint/DefaultConstraintValidatorTest.java b/core/src/test/java/gov/nist/secauto/metaschema/core/model/constraint/DefaultConstraintValidatorTest.java index deeaae5b1..9f4567f63 100644 --- a/core/src/test/java/gov/nist/secauto/metaschema/core/model/constraint/DefaultConstraintValidatorTest.java +++ b/core/src/test/java/gov/nist/secauto/metaschema/core/model/constraint/DefaultConstraintValidatorTest.java @@ -37,7 +37,6 @@ import gov.nist.secauto.metaschema.core.datatype.markup.MarkupLine; import gov.nist.secauto.metaschema.core.metapath.DynamicContext; -import gov.nist.secauto.metaschema.core.metapath.StaticContext; import gov.nist.secauto.metaschema.core.metapath.format.IPathFormatter; import gov.nist.secauto.metaschema.core.metapath.item.atomic.IStringItem; import gov.nist.secauto.metaschema.core.metapath.item.node.IFlagNodeItem; @@ -53,21 +52,31 @@ import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.RegisterExtension; +import java.net.URI; import java.util.List; +import javax.xml.namespace.QName; + import edu.umd.cs.findbugs.annotations.NonNull; @SuppressWarnings("PMD.TooManyStaticImports") class DefaultConstraintValidatorTest { + private static final String NS = URI.create("http://example.com/ns").toASCIIString(); + @RegisterExtension Mockery context = new JUnit5Mockery(); + @NonNull + private static QName qname(@NonNull String name) { + return new QName(NS, name); + } + @SuppressWarnings("null") @Test void testAllowedValuesAllowOther() { MockNodeItemFactory itemFactory = new MockNodeItemFactory(context); - IFlagNodeItem flag = itemFactory.flag("value", IStringItem.valueOf("value")); + IFlagNodeItem flag = itemFactory.flag(qname("value"), IStringItem.valueOf("value")); IFlagDefinition flagDefinition = context.mock(IFlagDefinition.class); @@ -77,8 +86,7 @@ void testAllowedValuesAllowOther() { .allowedOther(true) .build(); - DynamicContext dynamicContext = StaticContext.builder() - .build().dynamicContext(); + DynamicContext dynamicContext = new DynamicContext(); context.checking(new Expectations() { { // NOPMD - intentional @@ -114,7 +122,7 @@ void testAllowedValuesAllowOther() { void testAllowedValuesMultipleAllowOther() { MockNodeItemFactory itemFactory = new MockNodeItemFactory(context); - IFlagNodeItem flag = itemFactory.flag("value", IStringItem.valueOf("value")); + IFlagNodeItem flag = itemFactory.flag(qname("value"), IStringItem.valueOf("value")); IFlagDefinition flagDefinition = context.mock(IFlagDefinition.class); @@ -132,8 +140,7 @@ void testAllowedValuesMultipleAllowOther() { List allowedValuesConstraints = List.of(allowedValues1, allowedValues2); - DynamicContext dynamicContext = StaticContext.builder() - .build().dynamicContext(); + DynamicContext dynamicContext = new DynamicContext(); context.checking(new Expectations() { { // NOPMD - intentional @@ -169,8 +176,8 @@ void testAllowedValuesMultipleAllowOther() { void testMultipleAllowedValuesConflictingAllowOther() { MockNodeItemFactory itemFactory = new MockNodeItemFactory(context); - IFlagNodeItem flag1 = itemFactory.flag("value", IStringItem.valueOf("value")); - IFlagNodeItem flag2 = itemFactory.flag("other2", IStringItem.valueOf("other2")); + IFlagNodeItem flag1 = itemFactory.flag(qname("value"), IStringItem.valueOf("value")); + IFlagNodeItem flag2 = itemFactory.flag(qname("other2"), IStringItem.valueOf("other2")); IFlagDefinition flagDefinition = context.mock(IFlagDefinition.class); @@ -188,8 +195,7 @@ void testMultipleAllowedValuesConflictingAllowOther() { List allowedValuesConstraints = List.of(allowedValues1, allowedValues2); - DynamicContext dynamicContext = StaticContext.builder() - .build().dynamicContext(); + DynamicContext dynamicContext = new DynamicContext(); context.checking(new Expectations() { { // NOPMD - intentional diff --git a/core/src/test/java/gov/nist/secauto/metaschema/core/model/xml/MetaConstraintLoaderTest.java b/core/src/test/java/gov/nist/secauto/metaschema/core/model/xml/MetaConstraintLoaderTest.java index 0a2a24063..209399641 100644 --- a/core/src/test/java/gov/nist/secauto/metaschema/core/model/xml/MetaConstraintLoaderTest.java +++ b/core/src/test/java/gov/nist/secauto/metaschema/core/model/xml/MetaConstraintLoaderTest.java @@ -30,6 +30,7 @@ import gov.nist.secauto.metaschema.core.datatype.adapter.MetaschemaDataTypeProvider; import gov.nist.secauto.metaschema.core.metapath.MetapathExpression; +import gov.nist.secauto.metaschema.core.metapath.StaticContext; import gov.nist.secauto.metaschema.core.metapath.function.library.FnPath; import gov.nist.secauto.metaschema.core.metapath.item.IItem; import gov.nist.secauto.metaschema.core.metapath.item.node.IDefinitionNodeItem; @@ -61,7 +62,11 @@ void test() throws MetaschemaException, IOException { Paths.get("metaschema/examples/computer-example.xml").toUri()); IXmlModule module = loader.load(moduleUri); - MetapathExpression expression = MetapathExpression.compile("//@id"); + StaticContext staticContext = StaticContext.builder() + .defaultModelNamespace(module.getXmlNamespace()) + .build(); + + MetapathExpression expression = MetapathExpression.compile("//@id", staticContext); IModuleNodeItem moduleItem = INodeItemFactory.instance().newModuleNodeItem(module); for (IItem item : expression.evaluate(moduleItem)) { IDefinitionNodeItem nodeItem = (IDefinitionNodeItem) item; diff --git a/core/src/test/java/gov/nist/secauto/metaschema/core/testing/AbstractModelBuilder.java b/core/src/test/java/gov/nist/secauto/metaschema/core/testing/AbstractModelBuilder.java index 50feb73a2..85ee822e1 100644 --- a/core/src/test/java/gov/nist/secauto/metaschema/core/testing/AbstractModelBuilder.java +++ b/core/src/test/java/gov/nist/secauto/metaschema/core/testing/AbstractModelBuilder.java @@ -30,7 +30,7 @@ import gov.nist.secauto.metaschema.core.model.IDefinition; import gov.nist.secauto.metaschema.core.model.IModelDefinition; import gov.nist.secauto.metaschema.core.model.IModelElement; -import gov.nist.secauto.metaschema.core.model.INamed; +import gov.nist.secauto.metaschema.core.model.INamedModelElement; import gov.nist.secauto.metaschema.core.model.INamedInstance; import gov.nist.secauto.metaschema.core.util.CollectionUtil; @@ -38,11 +38,16 @@ import org.jmock.Expectations; import org.jmock.Mockery; +import java.net.URI; + +import javax.xml.namespace.QName; + import edu.umd.cs.findbugs.annotations.NonNull; public abstract class AbstractModelBuilder> extends MockFactory { + private String namespace; private String name; protected AbstractModelBuilder(@NonNull Mockery ctx) { @@ -53,6 +58,21 @@ protected AbstractModelBuilder(@NonNull Mockery ctx) { @SuppressWarnings("unchecked") public T reset() { this.name = null; + this.namespace = null; + return (T) this; + } + + @SuppressWarnings("unchecked") + @NonNull + public T namespace(@NonNull String name) { + this.namespace = name; + return (T) this; + } + + @SuppressWarnings("unchecked") + @NonNull + public T namespace(@NonNull URI name) { + this.namespace = name.toASCIIString(); return (T) this; } @@ -82,6 +102,10 @@ protected void applyNamedInstance( applyAttributable(instance); getContext().checking(new Expectations() { { + allowing(instance).getXmlNamespace(); + will(returnValue(namespace)); + allowing(instance).getXmlQName(); + will(returnValue(new QName(namespace, name))); allowing(instance).getDefinition(); will(returnValue(definition)); allowing(instance).getContainingDefinition(); @@ -92,7 +116,7 @@ protected void applyNamedInstance( }); } - protected void applyNamed(@NonNull INamed element) { + protected void applyNamed(@NonNull INamedModelElement element) { getContext().checking(new Expectations() { { allowing(element).getName(); diff --git a/core/src/test/java/gov/nist/secauto/metaschema/core/testing/AssemblyBuilder.java b/core/src/test/java/gov/nist/secauto/metaschema/core/testing/AssemblyBuilder.java index df1d2f575..99ab38395 100644 --- a/core/src/test/java/gov/nist/secauto/metaschema/core/testing/AssemblyBuilder.java +++ b/core/src/test/java/gov/nist/secauto/metaschema/core/testing/AssemblyBuilder.java @@ -40,6 +40,8 @@ import java.util.function.Function; import java.util.stream.Collectors; +import javax.xml.namespace.QName; + import edu.umd.cs.findbugs.annotations.NonNull; import edu.umd.cs.findbugs.annotations.Nullable; @@ -103,16 +105,16 @@ public IAssemblyDefinition toDefinition() { IAssemblyDefinition retval = mock(IAssemblyDefinition.class); applyDefinition(retval); - Map flags = this.flags.stream() + Map flags = this.flags.stream() .map(builder -> builder.toInstance(retval)) .collect(Collectors.toUnmodifiableMap( - IFlagInstance::getEffectiveName, + IFlagInstance::getXmlQName, Function.identity())); - Map modelInstances = this.modelInstances.stream() + Map modelInstances = this.modelInstances.stream() .map(builder -> builder.toInstance(retval)) .collect(Collectors.toUnmodifiableMap( - INamedModelInstanceAbsolute::getEffectiveName, + INamedModelInstanceAbsolute::getXmlQName, Function.identity())); getContext().checking(new Expectations() { diff --git a/core/src/test/java/gov/nist/secauto/metaschema/core/testing/FieldBuilder.java b/core/src/test/java/gov/nist/secauto/metaschema/core/testing/FieldBuilder.java index cb5d3aa68..dd1ec28fb 100644 --- a/core/src/test/java/gov/nist/secauto/metaschema/core/testing/FieldBuilder.java +++ b/core/src/test/java/gov/nist/secauto/metaschema/core/testing/FieldBuilder.java @@ -42,6 +42,8 @@ import java.util.function.Function; import java.util.stream.Collectors; +import javax.xml.namespace.QName; + import edu.umd.cs.findbugs.annotations.NonNull; import edu.umd.cs.findbugs.annotations.Nullable; @@ -112,10 +114,10 @@ public IFieldDefinition toDefinition() { IFieldDefinition retval = mock(IFieldDefinition.class); applyDefinition(retval); - Map flags = this.flags.stream() + Map flags = this.flags.stream() .map(builder -> builder.toInstance(retval)) .collect(Collectors.toUnmodifiableMap( - IFlagInstance::getEffectiveName, + IFlagInstance::getXmlQName, Function.identity())); getContext().checking(new Expectations() { diff --git a/core/src/test/java/gov/nist/secauto/metaschema/core/testing/FlagBuilder.java b/core/src/test/java/gov/nist/secauto/metaschema/core/testing/FlagBuilder.java index 544a32e61..6a3326f2c 100644 --- a/core/src/test/java/gov/nist/secauto/metaschema/core/testing/FlagBuilder.java +++ b/core/src/test/java/gov/nist/secauto/metaschema/core/testing/FlagBuilder.java @@ -26,6 +26,7 @@ package gov.nist.secauto.metaschema.core.testing; +import gov.nist.secauto.metaschema.core.datatype.adapter.MetaschemaDataTypeProvider; import gov.nist.secauto.metaschema.core.model.IFlagDefinition; import gov.nist.secauto.metaschema.core.model.IFlagInstance; import gov.nist.secauto.metaschema.core.model.IModelDefinition; @@ -93,6 +94,13 @@ public IFlagDefinition toDefinition() { IFlagDefinition retval = mock(IFlagDefinition.class); applyDefinition(retval); + + getContext().checking(new Expectations() { + { + allowing(retval).getJavaTypeAdapter(); + will(returnValue(MetaschemaDataTypeProvider.STRING)); + } + }); return retval; } diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/IBindingContext.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/IBindingContext.java index 0567ac59b..642f26f63 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/IBindingContext.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/IBindingContext.java @@ -304,7 +304,7 @@ default IConstraintValidator newValidator(@NonNull IConstraintValidationHandler IBoundLoader loader = newBoundLoader(); loader.disableFeature(DeserializationFeature.DESERIALIZE_VALIDATE_CONSTRAINTS); - DynamicContext context = StaticContext.instance().dynamicContext(); + DynamicContext context = new DynamicContext(); context.setDocumentLoader(loader); return new DefaultConstraintValidator(handler); @@ -320,11 +320,16 @@ default IConstraintValidator newValidator(@NonNull IConstraintValidationHandler * @throws IllegalArgumentException * if the provided class is not bound to a Module assembly or field */ - default IValidationResult validate(@NonNull INodeItem nodeItem) { + default IValidationResult validate(@NonNull INodeItem nodeItem, @NonNull IBoundLoader loader) { FindingCollectingConstraintValidationHandler handler = new FindingCollectingConstraintValidationHandler(); IConstraintValidator validator = newValidator(handler); - DynamicContext dynamicContext = StaticContext.instance().dynamicContext(); - dynamicContext.setDocumentLoader(newBoundLoader()); + + StaticContext staticContext = StaticContext.builder() + .defaultModelNamespace(nodeItem.getNamespace()) + .build(); + + DynamicContext dynamicContext = new DynamicContext(staticContext); + dynamicContext.setDocumentLoader(loader); validator.validate(nodeItem, dynamicContext); validator.finalizeValidation(dynamicContext); return handler; @@ -389,12 +394,9 @@ default IValidationResult validate( default IValidationResult validateWithConstraints(@NonNull URI target) throws IOException { IBoundLoader loader = newBoundLoader(); loader.disableFeature(DeserializationFeature.DESERIALIZE_VALIDATE_CONSTRAINTS); - - DynamicContext dynamicContext = StaticContext.instance().dynamicContext(); - dynamicContext.setDocumentLoader(loader); IDocumentNodeItem nodeItem = loader.loadAsNodeItem(target); - return validate(nodeItem); + return validate(nodeItem, loader); } interface IModuleLoaderStrategy { diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/codegen/PackageProductionImpl.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/codegen/PackageProductionImpl.java index 53b37abed..99a3ba943 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/codegen/PackageProductionImpl.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/codegen/PackageProductionImpl.java @@ -33,6 +33,7 @@ import java.nio.file.Path; import edu.umd.cs.findbugs.annotations.NonNull; +import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; class PackageProductionImpl implements IPackageProduction { @NonNull @@ -40,6 +41,7 @@ class PackageProductionImpl implements IPackageProduction { @NonNull private final IGeneratedClass packageInfoClass; + @SuppressFBWarnings(value = "CT_CONSTRUCTOR_THROW", justification = "Use of final fields") public PackageProductionImpl( @NonNull PackageMetadata metadata, @NonNull IMetaschemaClassFactory classFactory, diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/codegen/config/DefaultBindingConfiguration.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/codegen/config/DefaultBindingConfiguration.java index 767017e84..80127c95d 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/codegen/config/DefaultBindingConfiguration.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/codegen/config/DefaultBindingConfiguration.java @@ -58,7 +58,7 @@ public class DefaultBindingConfiguration implements IBindingConfiguration { private final Map namespaceToPackageNameMap = new ConcurrentHashMap<>(); - // metaschema location -> ModelType -> Definition Name -> IBindingConfiguration + // metaschema location -> ModelType -> Definition name -> IBindingConfiguration private final Map moduleUrlToMetaschemaBindingConfigurationMap = new ConcurrentHashMap<>(); diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/codegen/impl/AnnotationGenerator.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/codegen/impl/AnnotationGenerator.java index 0af383be0..7cb0548b0 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/codegen/impl/AnnotationGenerator.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/codegen/impl/AnnotationGenerator.java @@ -34,8 +34,6 @@ import gov.nist.secauto.metaschema.core.datatype.markup.MarkupMultiline; import gov.nist.secauto.metaschema.core.metapath.DynamicContext; import gov.nist.secauto.metaschema.core.metapath.ISequence; -import gov.nist.secauto.metaschema.core.metapath.MetapathExpression; -import gov.nist.secauto.metaschema.core.metapath.StaticContext; import gov.nist.secauto.metaschema.core.metapath.item.node.IAssemblyNodeItem; import gov.nist.secauto.metaschema.core.metapath.item.node.IDefinitionNodeItem; import gov.nist.secauto.metaschema.core.metapath.item.node.INodeItemFactory; @@ -135,10 +133,9 @@ private static void buildConstraint(Class annotationType, AnnotationSpec.Buil annotation.addMember("level", "$T.$L", IConstraint.Level.class, constraint.getLevel()); - MetapathExpression target = constraint.getTarget(); - String path = target.getPath(); - if (!path.equals(getDefaultValue(annotationType, "target"))) { - annotation.addMember("target", "$S", path); + String target = constraint.getTarget(); + if (!target.equals(getDefaultValue(annotationType, "target"))) { + annotation.addMember("target", "$S", target); } } @@ -249,10 +246,9 @@ private static void buildKeyFields(@NonNull Builder constraintAnnotation, for (IKeyField key : keyFields) { AnnotationSpec.Builder keyAnnotation = AnnotationSpec.builder(KeyField.class); - MetapathExpression target = key.getTarget(); - String path = target.getPath(); - if (!path.equals(getDefaultValue(KeyField.class, "target"))) { - keyAnnotation.addMember("target", "$S", path); + String target = key.getTarget(); + if (!target.equals(getDefaultValue(KeyField.class, "target"))) { + keyAnnotation.addMember("target", "$S", target); } Pattern pattern = key.getPattern(); @@ -300,8 +296,7 @@ private static void applyExpectConstraints(AnnotationSpec.Builder annotation, buildConstraint(Expect.class, constraintAnnotation, constraint); - MetapathExpression test = constraint.getTest(); - constraintAnnotation.addMember("test", "$S", test.getPath()); + constraintAnnotation.addMember("test", "$S", constraint.getTest()); if (constraint.getMessage() != null) { constraintAnnotation.addMember("message", "$S", constraint.getMessage()); @@ -374,7 +369,7 @@ private static void checkCardinalities( } else { warn.log(String.format( "Definition '%s' has min-occurs=%d cardinality constraint targeting '%s' that is not a model instance", - definition.getName(), constraint.getMinOccurs(), constraint.getTarget().getPath())); + definition.getName(), constraint.getMinOccurs(), constraint.getTarget())); } } } @@ -393,14 +388,14 @@ private static void checkMinOccurs( logBuilder.log(String.format( "Definition '%s' has min-occurs=%d cardinality constraint targeting '%s' that is redundant with a" + " targeted instance named '%s' that requires min-occurs=%d", - definition.getName(), minOccurs, constraint.getTarget().getPath(), + definition.getName(), minOccurs, constraint.getTarget(), modelInstance.getName(), modelInstance.getMinOccurs())); } else if (minOccurs < modelInstance.getMinOccurs()) { logBuilder.log(String.format( "Definition '%s' has min-occurs=%d cardinality constraint targeting '%s' that conflicts with a" + " targeted instance named '%s' that requires min-occurs=%d", - definition.getName(), minOccurs, constraint.getTarget().getPath(), + definition.getName(), minOccurs, constraint.getTarget(), modelInstance.getName(), modelInstance.getMinOccurs())); } @@ -421,14 +416,14 @@ private static void checkMaxOccurs( logBuilder.log(String.format( "Definition '%s' has max-occurs=%d cardinality constraint targeting '%s' that is redundant with a" + " targeted instance named '%s' that requires max-occurs=%d", - definition.getName(), maxOccurs, constraint.getTarget().getPath(), + definition.getName(), maxOccurs, constraint.getTarget(), modelInstance.getName(), modelInstance.getMaxOccurs())); } else if (maxOccurs < modelInstance.getMaxOccurs()) { logBuilder.log(String.format( "Definition '%s' has max-occurs=%d cardinality constraint targeting '%s' that conflicts with a" + " targeted instance named '%s' that requires max-occurs=%d", - definition.getName(), maxOccurs, constraint.getTarget().getPath(), + definition.getName(), maxOccurs, constraint.getTarget(), modelInstance.getName(), modelInstance.getMaxOccurs())); } @@ -440,7 +435,7 @@ private static void applyHasCardinalityConstraints( @NonNull AnnotationSpec.Builder annotation, @NonNull List constraints) { - DynamicContext dynamicContext = StaticContext.instance().dynamicContext(); + DynamicContext dynamicContext = new DynamicContext(); dynamicContext.disablePredicateEvaluation(); for (ICardinalityConstraint constraint : constraints) { diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/codegen/typeinfo/AbstractModelInstanceTypeInfo.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/codegen/typeinfo/AbstractModelInstanceTypeInfo.java index dfeb9b82c..2a01fcb81 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/codegen/typeinfo/AbstractModelInstanceTypeInfo.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/codegen/typeinfo/AbstractModelInstanceTypeInfo.java @@ -41,6 +41,7 @@ import gov.nist.secauto.metaschema.core.util.ObjectUtils; import gov.nist.secauto.metaschema.databind.codegen.typeinfo.def.IAssemblyDefinitionTypeInfo; import gov.nist.secauto.metaschema.databind.model.annotations.GroupAs; +import gov.nist.secauto.metaschema.databind.model.annotations.ModelUtil; import java.util.HashSet; import java.util.List; @@ -120,14 +121,13 @@ protected AnnotationSpec.Builder generateGroupAsAnnotation() { groupAsAnnoation.addMember("name", "$S", ObjectUtils.requireNonNull(modelInstance.getGroupAsName(), "The grouping name must be non-null")); - String groupAsNamespace = modelInstance.getGroupAsXmlNamespace(); - if (groupAsNamespace == null) { - groupAsAnnoation.addMember("namespace", "$S", "##default"); - } else if (groupAsNamespace.isEmpty()) { - groupAsAnnoation.addMember("namespace", "$S", "##none"); - } else if (!modelInstance.getContainingModule().getXmlNamespace().toASCIIString().equals(groupAsNamespace)) { - groupAsAnnoation.addMember("namespace", "$S", groupAsNamespace); - } // otherwise use the ##default + TypeInfoUtils.buildNamespaceBindingAnnotation( + groupAsAnnoation, + "namespace", + modelInstance.getGroupAsXmlNamespace(), + () -> modelInstance.getContainingModule().getXmlNamespace().toASCIIString(), + ModelUtil.DEFAULT_STRING_VALUE, + false); JsonGroupAsBehavior jsonGroupAsBehavior = modelInstance.getJsonGroupAsBehavior(); assert jsonGroupAsBehavior != null; diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/codegen/typeinfo/FlagInstanceTypeInfoImpl.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/codegen/typeinfo/FlagInstanceTypeInfoImpl.java index 9ff2cf4f8..2348691c5 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/codegen/typeinfo/FlagInstanceTypeInfoImpl.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/codegen/typeinfo/FlagInstanceTypeInfoImpl.java @@ -97,13 +97,13 @@ public Set buildField( annotation.addMember("useIndex", "$L", index); } - String namespace = instance.getXmlNamespace(); - if (namespace != null) { - if (namespace.equals(instance.getContainingModule().getXmlNamespace().toASCIIString())) { - namespace = ModelUtil.DEFAULT_STRING_VALUE; - } - annotation.addMember("namespace", "$S", namespace); - } + TypeInfoUtils.buildNamespaceBindingAnnotation( + annotation, + "namespace", + instance.getXmlNamespace(), + () -> instance.getContainingModule().getXmlNamespace().toASCIIString(), + ModelUtil.NO_STRING_VALUE, + true); IFlagDefinition definition = instance.getDefinition(); diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/codegen/typeinfo/TypeInfoUtils.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/codegen/typeinfo/TypeInfoUtils.java index 3bf50e2a5..ae0d875a1 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/codegen/typeinfo/TypeInfoUtils.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/codegen/typeinfo/TypeInfoUtils.java @@ -30,10 +30,14 @@ import gov.nist.secauto.metaschema.core.datatype.markup.MarkupLine; import gov.nist.secauto.metaschema.core.datatype.markup.MarkupMultiline; +import gov.nist.secauto.metaschema.core.model.INamedInstance; import gov.nist.secauto.metaschema.core.model.INamedModelInstance; import gov.nist.secauto.metaschema.databind.model.annotations.ModelUtil; +import java.util.function.Supplier; + import edu.umd.cs.findbugs.annotations.NonNull; +import edu.umd.cs.findbugs.annotations.Nullable; public final class TypeInfoUtils { private TypeInfoUtils() { @@ -61,12 +65,13 @@ public static void buildCommonBindingAnnotationValues( annotation.addMember("useIndex", "$L", index); } - String namespace = instance.getXmlNamespace(); - if (namespace == null) { - annotation.addMember("namespace", "$S", ModelUtil.NO_STRING_VALUE); - } else if (!instance.getContainingModule().getXmlNamespace().toASCIIString().equals(namespace)) { - annotation.addMember("namespace", "$S", namespace); - } // otherwise use the ##default + buildNamespaceBindingAnnotation( + annotation, + "namespace", + instance.getXmlNamespace(), + () -> instance.getContainingModule().getXmlNamespace().toASCIIString(), + ModelUtil.DEFAULT_STRING_VALUE, + false); MarkupMultiline remarks = instance.getRemarks(); if (remarks != null) { @@ -74,4 +79,28 @@ public static void buildCommonBindingAnnotationValues( } } + public static void buildNamespaceBindingAnnotation( + @NonNull AnnotationSpec.Builder annotation, + @NonNull String annotationMember, + @Nullable String namespaceUri, + @NonNull Supplier defaultSupplier, + @NonNull String annotationDefault, + boolean allowNone) { + String value; + if (namespaceUri == null) { + if (allowNone) { + value = ModelUtil.NO_STRING_VALUE; + } else { + throw new IllegalStateException("Expected non-null namespace"); + } + } else if (namespaceUri.equals(defaultSupplier.get())) { + value = ModelUtil.DEFAULT_STRING_VALUE; + } else { + value = namespaceUri; + } + + if (!annotationDefault.equals(value)) { + annotation.addMember(annotationMember, "$S", value); + } + } } diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/io/AbstractDeserializer.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/io/AbstractDeserializer.java index 7364ed274..b96d52c19 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/io/AbstractDeserializer.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/io/AbstractDeserializer.java @@ -101,7 +101,10 @@ public INodeItem deserializeToNodeItem(Reader reader, URI documentUri) throws IO } if (isValidating()) { - DynamicContext dynamicContext = StaticContext.instance().dynamicContext(); + DynamicContext dynamicContext = new DynamicContext( + StaticContext.builder() + .defaultModelNamespace(nodeItem.getNamespace()) + .build()); dynamicContext.setDocumentLoader(getBindingContext().newBoundLoader()); DefaultConstraintValidator validator = new DefaultConstraintValidator(getConstraintValidationHandler()); validator.validate(nodeItem, dynamicContext); diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/io/json/MetaschemaJsonReader.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/io/json/MetaschemaJsonReader.java index 9b850156b..a033a95d6 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/io/json/MetaschemaJsonReader.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/io/json/MetaschemaJsonReader.java @@ -68,6 +68,7 @@ import edu.umd.cs.findbugs.annotations.NonNull; import edu.umd.cs.findbugs.annotations.Nullable; +import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; import nl.talsmasoftware.lazy4j.Lazy; public class MetaschemaJsonReader @@ -95,6 +96,7 @@ public class MetaschemaJsonReader * if an error occurred while reading the JSON * @see DefaultJsonProblemHandler */ + @SuppressFBWarnings(value = "CT_CONSTRUCTOR_THROW", justification = "Use of final fields") public MetaschemaJsonReader( @NonNull JsonParser parser) throws IOException { this(parser, new DefaultJsonProblemHandler()); @@ -110,6 +112,7 @@ public MetaschemaJsonReader( * @throws IOException * if an error occurred while reading the JSON */ + @SuppressFBWarnings(value = "CT_CONSTRUCTOR_THROW", justification = "Use of final fields") public MetaschemaJsonReader( @NonNull JsonParser parser, @NonNull IJsonProblemHandler problemHandler) throws IOException { @@ -439,8 +442,7 @@ private JsonKeyBodyHandler( @Override public void accept(IBoundDefinitionModelComplex definition, Object parentItem, IJsonProblemHandler problemHandler) throws IOException { - @SuppressWarnings("resource") - JsonParser parser = getReader(); + @SuppressWarnings("resource") JsonParser parser = getReader(); JsonUtil.assertCurrent(parser, JsonToken.FIELD_NAME); // the field will be the JSON key @@ -477,8 +479,7 @@ public void accept( Object parentItem, IJsonProblemHandler problemHandler) throws IOException { - @SuppressWarnings("resource") - JsonParser parser = getReader(); + @SuppressWarnings("resource") JsonParser parser = getReader(); // advance past the start object JsonUtil.assertAndAdvance(parser, JsonToken.START_OBJECT); @@ -602,8 +603,7 @@ public boolean handleUnknownProperty(IBoundDefinitionModelComplex definition, Ob if (foundJsonValueKey) { retval = delegate.handleUnknownProperty(definition, parentItem, fieldName, reader); } else { - @SuppressWarnings("resource") - JsonParser parser = getReader(); + @SuppressWarnings("resource") JsonParser parser = getReader(); // handle JSON value key String key = ObjectUtils.notNull(parser.getCurrentName()); Object keyValue = jsonValueKyeFlag.getJavaTypeAdapter().parse(key); @@ -703,8 +703,7 @@ public List readList() throws IOException { IBoundInstanceModel instance = getCollectionInfo().getInstance(); - @SuppressWarnings("PMD.UseConcurrentHashMap") - Map items = new LinkedHashMap<>(); + @SuppressWarnings("PMD.UseConcurrentHashMap") Map items = new LinkedHashMap<>(); // A map value is always wrapped in a START_OBJECT, since fields are used for // the keys diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/io/xml/DefaultXmlDeserializer.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/io/xml/DefaultXmlDeserializer.java index 602d11656..35c07b78b 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/io/xml/DefaultXmlDeserializer.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/io/xml/DefaultXmlDeserializer.java @@ -49,6 +49,7 @@ import javax.xml.stream.XMLStreamException; import edu.umd.cs.findbugs.annotations.NonNull; +import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; public class DefaultXmlDeserializer extends AbstractDeserializer { @@ -65,13 +66,14 @@ public class DefaultXmlDeserializer * the assembly class binding describing the Java objects this * deserializer parses data into */ + @SuppressFBWarnings(value = "CT_CONSTRUCTOR_THROW", justification = "Use of final fields") public DefaultXmlDeserializer(@NonNull IBoundDefinitionModelAssembly definition) { super(definition); + this.rootDefinition = definition; if (!definition.isRoot()) { throw new UnsupportedOperationException( String.format("The assembly '%s' is not a root assembly.", definition.getBoundClass().getName())); } - this.rootDefinition = definition; } /** diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/io/xml/MetaschemaXmlReader.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/io/xml/MetaschemaXmlReader.java index 9aa5d5468..22e1cadb2 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/io/xml/MetaschemaXmlReader.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/io/xml/MetaschemaXmlReader.java @@ -490,7 +490,7 @@ public Object readItemField( try { QName wrapper = null; if (instance.isEffectiveValueWrappedInXml()) { - wrapper = new QName(instance.getXmlNamespace(), instance.getEffectiveName()); + wrapper = instance.getXmlQName(); XmlEventUtil.skipWhitespace(getReader()); XmlEventUtil.requireStartElement(getReader(), wrapper); diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundContainerFlag.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundContainerFlag.java index 8a63584b6..2f488944c 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundContainerFlag.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundContainerFlag.java @@ -26,13 +26,15 @@ package gov.nist.secauto.metaschema.databind.model; -import gov.nist.secauto.metaschema.core.model.IContainerFlag; - import java.util.Collection; +import javax.xml.namespace.QName; + +import gov.nist.secauto.metaschema.core.model.IContainerFlag; + public interface IBoundContainerFlag extends IContainerFlag { @Override - IBoundInstanceFlag getFlagInstanceByName(String name); + IBoundInstanceFlag getFlagInstanceByName(QName name); @Override Collection getFlagInstances(); diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundContainerModelAssembly.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundContainerModelAssembly.java index 8d690442b..6592eb78a 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundContainerModelAssembly.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundContainerModelAssembly.java @@ -26,13 +26,14 @@ package gov.nist.secauto.metaschema.databind.model; -import gov.nist.secauto.metaschema.core.model.IContainerModelAssembly; - import java.util.Collection; import java.util.Map; +import javax.xml.namespace.QName; + import edu.umd.cs.findbugs.annotations.NonNull; import edu.umd.cs.findbugs.annotations.Nullable; +import gov.nist.secauto.metaschema.core.model.IContainerModelAssembly; public interface IBoundContainerModelAssembly extends IContainerModelAssembly { @Override @@ -49,7 +50,7 @@ public interface IBoundContainerModelAssembly extends IContainerModelAssembly { @Override @Nullable - IBoundInstanceModelNamed getNamedModelInstanceByName(String name); + IBoundInstanceModelNamed getNamedModelInstanceByName(QName name); @Override @NonNull @@ -57,7 +58,7 @@ public interface IBoundContainerModelAssembly extends IContainerModelAssembly { @Override @Nullable - IBoundInstanceModelField getFieldInstanceByName(String name); + IBoundInstanceModelField getFieldInstanceByName(QName name); @Override @NonNull @@ -65,7 +66,7 @@ public interface IBoundContainerModelAssembly extends IContainerModelAssembly { @Override @Nullable - IBoundInstanceModelAssembly getAssemblyInstanceByName(String name); + IBoundInstanceModelAssembly getAssemblyInstanceByName(QName name); @Override IBoundInstanceModelChoiceGroup getChoiceGroupInstanceByName(String name); diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundContainerModelChoiceGroup.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundContainerModelChoiceGroup.java index e06cee619..640f6cc20 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundContainerModelChoiceGroup.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundContainerModelChoiceGroup.java @@ -26,12 +26,13 @@ package gov.nist.secauto.metaschema.databind.model; -import gov.nist.secauto.metaschema.core.model.IContainerModelGrouped; - import java.util.Collection; +import javax.xml.namespace.QName; + import edu.umd.cs.findbugs.annotations.NonNull; import edu.umd.cs.findbugs.annotations.Nullable; +import gov.nist.secauto.metaschema.core.model.IContainerModelGrouped; public interface IBoundContainerModelChoiceGroup extends IContainerModelGrouped { @@ -45,7 +46,7 @@ default Collection getModelInstances( Collection getNamedModelInstances(); @Override - IBoundInstanceModelGroupedNamed getNamedModelInstanceByName(String name); + IBoundInstanceModelGroupedNamed getNamedModelInstanceByName(QName name); @Override @NonNull @@ -53,7 +54,7 @@ default Collection getModelInstances( @Override @Nullable - IBoundInstanceModelGroupedField getFieldInstanceByName(String name); + IBoundInstanceModelGroupedField getFieldInstanceByName(QName name); @Override @NonNull @@ -61,5 +62,5 @@ default Collection getModelInstances( @Override @Nullable - IBoundInstanceModelGroupedAssembly getAssemblyInstanceByName(String name); + IBoundInstanceModelGroupedAssembly getAssemblyInstanceByName(QName name); } diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundDefinitionModelAssembly.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundDefinitionModelAssembly.java index 93970466e..58746ae9e 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundDefinitionModelAssembly.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundDefinitionModelAssembly.java @@ -69,11 +69,12 @@ default IBoundInstanceModelAssembly getInlineInstance() { // never inline return null; } - - @Override - default QName getXmlQName() { - return ObjectUtils.requireNonNull(getRootXmlQName()); - } + // + // @Override + // @NonNull + // default QName getXmlQName() { + // return ObjectUtils.requireNonNull(getRootXmlQName()); + // } @Override @NonNull diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundDefinitionModelComplex.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundDefinitionModelComplex.java index c599608f0..b629c9b44 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundDefinitionModelComplex.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundDefinitionModelComplex.java @@ -46,26 +46,6 @@ public interface IBoundDefinitionModelComplex @NonNull Class getBoundClass(); - /** - * Retrieve the XML namespace for this definition. - * - * @return the XML namespace or {@code null} if no namespace is defined - */ - @NonNull - default String getXmlNamespace() { - return ObjectUtils.notNull(getContainingModule().getXmlNamespace().toASCIIString()); - } - - /** - * Get the XML qualified name to use in XML. - * - * @return the XML qualified name, or {@code null} if there isn't one - */ - @NonNull - default QName getXmlQName() { - return new QName(getXmlNamespace(), getEffectiveName()); - } - @NonNull Map getJsonProperties(@Nullable Predicate flagFilter); } diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundInstanceModelFieldComplex.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundInstanceModelFieldComplex.java index aa49cb056..4a83597f4 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundInstanceModelFieldComplex.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundInstanceModelFieldComplex.java @@ -54,12 +54,6 @@ default boolean isEffectiveValueWrappedInXml() { return true; } - @Override - @NonNull - default QName getXmlQName() { - return ObjectUtils.notNull(IBoundInstanceModelField.super.getXmlQName()); - } - @Override default Object readItem(Object parent, IItemReadHandler handler) throws IOException { return handler.readItemField(ObjectUtils.requireNonNull(parent, "parent"), this); diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundInstanceModelGroupedNamed.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundInstanceModelGroupedNamed.java index d031f0a91..b3c824ef6 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundInstanceModelGroupedNamed.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundInstanceModelGroupedNamed.java @@ -26,13 +26,14 @@ package gov.nist.secauto.metaschema.databind.model; +import javax.xml.namespace.QName; + +import edu.umd.cs.findbugs.annotations.Nullable; import gov.nist.secauto.metaschema.core.model.INamedModelInstanceGrouped; import gov.nist.secauto.metaschema.core.model.JsonGroupAsBehavior; import gov.nist.secauto.metaschema.core.util.ObjectUtils; import gov.nist.secauto.metaschema.databind.model.info.IFeatureComplexItemValueHandler; -import edu.umd.cs.findbugs.annotations.Nullable; - /** * Represents a model instance that is a member of a choice group instance. */ @@ -49,7 +50,7 @@ default IBoundInstanceFlag getJsonKey() { String jsonKeyName = getJsonKeyFlagName(); return JsonGroupAsBehavior.KEYED.equals(getParentContainer().getJsonGroupAsBehavior()) ? ObjectUtils.requireNonNull(getDefinition().getFlagInstanceByName( - ObjectUtils.requireNonNull(jsonKeyName))) + new QName(getXmlNamespace(), ObjectUtils.requireNonNull(jsonKeyName)))) : null; } } diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundInstanceModelNamed.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundInstanceModelNamed.java index 7a2e65bec..9136e50f1 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundInstanceModelNamed.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundInstanceModelNamed.java @@ -26,16 +26,15 @@ package gov.nist.secauto.metaschema.databind.model; -import gov.nist.secauto.metaschema.core.model.INamedModelInstanceAbsolute; -import gov.nist.secauto.metaschema.core.model.JsonGroupAsBehavior; -import gov.nist.secauto.metaschema.core.util.ObjectUtils; - import java.util.Collection; import javax.xml.namespace.QName; import edu.umd.cs.findbugs.annotations.NonNull; import edu.umd.cs.findbugs.annotations.Nullable; +import gov.nist.secauto.metaschema.core.model.INamedModelInstanceAbsolute; +import gov.nist.secauto.metaschema.core.model.JsonGroupAsBehavior; +import gov.nist.secauto.metaschema.core.util.ObjectUtils; public interface IBoundInstanceModelNamed extends IBoundInstanceModel, INamedModelInstanceAbsolute { @@ -65,7 +64,7 @@ default IBoundInstanceFlag getJsonKey() { String jsonKeyName = getJsonKeyFlagName(); return JsonGroupAsBehavior.KEYED.equals(getJsonGroupAsBehavior()) ? ObjectUtils.requireNonNull(getDefinition().getFlagInstanceByName( - ObjectUtils.requireNonNull(jsonKeyName))) + getContainingModule().toFlagQName(ObjectUtils.requireNonNull(jsonKeyName)))) : null; } diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/annotations/ModelUtil.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/annotations/ModelUtil.java index cb63d0cdc..c3af610e3 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/annotations/ModelUtil.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/annotations/ModelUtil.java @@ -33,9 +33,11 @@ import gov.nist.secauto.metaschema.core.util.ObjectUtils; import gov.nist.secauto.metaschema.databind.IBindingContext; import gov.nist.secauto.metaschema.databind.model.IGroupAs; +import gov.nist.secauto.metaschema.databind.model.impl.DefaultGroupAs; import java.lang.annotation.Annotation; import java.lang.reflect.Field; +import java.util.function.Supplier; import edu.umd.cs.findbugs.annotations.NonNull; import edu.umd.cs.findbugs.annotations.Nullable; @@ -111,13 +113,10 @@ public static String resolveNoneOrDefault(@Nullable String value, @Nullable Stri } @Nullable - public static String resolveOptionalNamespace(String annotationValue) { - return resolveNamespace(annotationValue, true); - } - - @NonNull - public static String resolveNamespace(String annotationValue) { - return ObjectUtils.requireNonNull(resolveNamespace(annotationValue, false)); + public static String resolveOptionalNamespace( + @Nullable String annotationValue, + @NonNull Supplier defaultSupplier) { + return resolveNamespace(annotationValue, true, defaultSupplier); } /** @@ -134,11 +133,14 @@ public static String resolveNamespace(String annotationValue) { * if the "##none" value is honored * @return the resolved value or {@code null} if no namespace is defined */ - private static String resolveNamespace(String value, boolean allowNone) { + private static String resolveNamespace( + @Nullable String value, + boolean allowNone, + @NonNull Supplier defaultSupplier) { String retval; if (value == null || DEFAULT_STRING_VALUE.equals(value)) { // get namespace from the metaschema - retval = null; + retval = defaultSupplier.get(); } else if (allowNone && NO_STRING_VALUE.equals(value)) { retval = ""; // NOPMD - intentional } else { @@ -221,9 +223,11 @@ public static Object resolveNullOrValue( } @NonNull - public static IGroupAs groupAs(@NonNull GroupAs groupAs) { + public static IGroupAs groupAs( + @NonNull GroupAs groupAs, + @NonNull Supplier defaultSupplier) { return NULL_VALUE.equals(groupAs.name()) ? IGroupAs.SINGLETON_GROUP_AS - : new DefaultGroupAs(groupAs); + : new DefaultGroupAs(groupAs, defaultSupplier); } } diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/annotations/NullJavaTypeAdapter.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/annotations/NullJavaTypeAdapter.java index 85b941e42..a140aadca 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/annotations/NullJavaTypeAdapter.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/annotations/NullJavaTypeAdapter.java @@ -53,7 +53,6 @@ public class NullJavaTypeAdapter */ public NullJavaTypeAdapter(@NonNull Class clazz) { super(clazz); - throw new UnsupportedOperationException(NOT_VALID); } @Override diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/AbstractBoundInstanceField.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/AbstractBoundInstanceField.java index d5913d8ea..36d3e82ea 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/AbstractBoundInstanceField.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/AbstractBoundInstanceField.java @@ -38,6 +38,7 @@ import java.lang.reflect.Field; import edu.umd.cs.findbugs.annotations.NonNull; +import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; // TODO: implement getProperties() public abstract class AbstractBoundInstanceField @@ -54,11 +55,14 @@ public abstract class AbstractBoundInstanceField * @param containingDefinition * the definition containing this instance */ + @SuppressFBWarnings(value = "CT_CONSTRUCTOR_THROW", justification = "Use of final fields") protected AbstractBoundInstanceField( @NonNull Field javaField, @NonNull IBoundDefinitionModelAssembly containingDefinition) { super(javaField, BoundField.class, containingDefinition); - this.groupAs = ModelUtil.groupAs(getAnnotation().groupAs()); + this.groupAs = ModelUtil.groupAs( + getAnnotation().groupAs(), + () -> containingDefinition.getXmlNamespace()); if (getMaxOccurs() == -1 || getMaxOccurs() > 1) { if (IGroupAs.SINGLETON_GROUP_AS.equals(this.groupAs)) { throw new IllegalStateException(String.format("Field '%s' on class '%s' is missing the '%s' annotation.", @@ -102,6 +106,13 @@ public Integer getUseIndex() { return value == Integer.MIN_VALUE ? null : value; } + @Override + public String getXmlNamespace() { + return ModelUtil.resolveOptionalNamespace( + getAnnotation().namespace(), + () -> getContainingDefinition().getXmlNamespace()); + } + @Override public boolean isInXmlWrapped() { return getAnnotation().inXmlWrapped(); diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/AssemblyModelContainerSupport.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/AssemblyModelContainerSupport.java index af2074036..f3b0f09e8 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/AssemblyModelContainerSupport.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/AssemblyModelContainerSupport.java @@ -26,6 +26,21 @@ package gov.nist.secauto.metaschema.databind.model.impl; +import java.lang.reflect.Field; +import java.util.Arrays; +import java.util.Collection; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import javax.xml.namespace.QName; + +import edu.umd.cs.findbugs.annotations.NonNull; +import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; + import gov.nist.secauto.metaschema.core.model.IChoiceInstance; import gov.nist.secauto.metaschema.core.model.IContainerModelAssemblySupport; import gov.nist.secauto.metaschema.core.util.CollectionUtil; @@ -41,18 +56,6 @@ import gov.nist.secauto.metaschema.databind.model.annotations.BoundField; import gov.nist.secauto.metaschema.databind.model.annotations.Ignore; -import java.lang.reflect.Field; -import java.util.Arrays; -import java.util.Collection; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; -import java.util.Objects; -import java.util.stream.Collectors; -import java.util.stream.Stream; - -import edu.umd.cs.findbugs.annotations.NonNull; - class AssemblyModelContainerSupport implements IContainerModelAssemblySupport< IBoundInstanceModel, @@ -64,29 +67,30 @@ class AssemblyModelContainerSupport @NonNull private final List modelInstances; @NonNull - private final Map namedModelInstances; + private final Map namedModelInstances; @NonNull - private final Map fieldInstances; + private final Map fieldInstances; @NonNull - private final Map assemblyInstances; + private final Map assemblyInstances; @NonNull private final Map choiceGroupInstances; @SuppressWarnings("PMD.UseConcurrentHashMap") + @SuppressFBWarnings(value = "CT_CONSTRUCTOR_THROW", justification = "Use of final fields") public AssemblyModelContainerSupport( @NonNull DefinitionAssembly containingDefinition) { this.modelInstances = CollectionUtil.unmodifiableList(ObjectUtils.notNull( getModelInstanceStream(containingDefinition, containingDefinition.getBoundClass()) .collect(Collectors.toUnmodifiableList()))); - Map namedModelInstances = new LinkedHashMap<>(); - Map fieldInstances = new LinkedHashMap<>(); - Map assemblyInstances = new LinkedHashMap<>(); + Map namedModelInstances = new LinkedHashMap<>(); + Map fieldInstances = new LinkedHashMap<>(); + Map assemblyInstances = new LinkedHashMap<>(); Map choiceGroupInstances = new LinkedHashMap<>(); for (IBoundInstanceModel instance : this.modelInstances) { if (instance instanceof IBoundInstanceModelNamed) { IBoundInstanceModelNamed named = (IBoundInstanceModelNamed) instance; - String key = named.getEffectiveName(); + QName key = named.getXmlQName(); namedModelInstances.put(key, named); if (instance instanceof IBoundInstanceModelField) { @@ -169,17 +173,17 @@ public Collection getModelInstances() { } @Override - public Map getNamedModelInstanceMap() { + public Map getNamedModelInstanceMap() { return namedModelInstances; } @Override - public Map getFieldInstanceMap() { + public Map getFieldInstanceMap() { return fieldInstances; } @Override - public Map getAssemblyInstanceMap() { + public Map getAssemblyInstanceMap() { return assemblyInstances; } diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/ConstraintFactory.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/ConstraintFactory.java index 0537af11a..85c146c7f 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/ConstraintFactory.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/ConstraintFactory.java @@ -30,7 +30,6 @@ import gov.nist.secauto.metaschema.core.datatype.IDataTypeAdapter; import gov.nist.secauto.metaschema.core.datatype.markup.MarkupLine; import gov.nist.secauto.metaschema.core.datatype.markup.MarkupMultiline; -import gov.nist.secauto.metaschema.core.metapath.MetapathExpression; import gov.nist.secauto.metaschema.core.model.IAttributable; import gov.nist.secauto.metaschema.core.model.constraint.AbstractConstraintBuilder; import gov.nist.secauto.metaschema.core.model.constraint.AbstractKeyConstraintBuilder; @@ -68,6 +67,8 @@ import java.util.Set; import java.util.regex.Pattern; +import javax.xml.namespace.QName; + import edu.umd.cs.findbugs.annotations.NonNull; import edu.umd.cs.findbugs.annotations.Nullable; @@ -83,7 +84,7 @@ static MarkupMultiline toRemarks(@NonNull String remarks) { } @NonNull - static MetapathExpression toMetapath(@NonNull String metapath) { + static String toMetapath(@NonNull String metapath) { String path = metapath; if (path.startsWith("/")) { String newPath = "." + path; @@ -100,7 +101,7 @@ static MetapathExpression toMetapath(@NonNull String metapath) { path = newPath; } - return path.isBlank() ? IConstraint.DEFAULT_TARGET : MetapathExpression.compile(path); + return path.isBlank() ? IConstraint.DEFAULT_TARGET_METAPATH : path; } @NonNull @@ -364,6 +365,6 @@ static ICardinalityConstraint newCardinalityConstraint(@NonNull HasCardinality c @NonNull static ILet newLetExpression(@NonNull Let annotation, @NonNull ISource source) { - return ILet.of(annotation.name(), annotation.target(), source); + return ILet.of(new QName(annotation.name()), annotation.target(), source); } } diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/annotations/DefaultGroupAs.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/DefaultGroupAs.java similarity index 81% rename from databind/src/main/java/gov/nist/secauto/metaschema/databind/model/annotations/DefaultGroupAs.java rename to databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/DefaultGroupAs.java index c7a635b99..261869ca6 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/annotations/DefaultGroupAs.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/DefaultGroupAs.java @@ -24,16 +24,21 @@ * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. */ -package gov.nist.secauto.metaschema.databind.model.annotations; +package gov.nist.secauto.metaschema.databind.model.impl; import gov.nist.secauto.metaschema.core.model.JsonGroupAsBehavior; import gov.nist.secauto.metaschema.core.model.XmlGroupAsBehavior; import gov.nist.secauto.metaschema.databind.model.IGroupAs; +import gov.nist.secauto.metaschema.databind.model.annotations.GroupAs; +import gov.nist.secauto.metaschema.databind.model.annotations.ModelUtil; + +import java.util.function.Supplier; import edu.umd.cs.findbugs.annotations.NonNull; import edu.umd.cs.findbugs.annotations.Nullable; +import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; -class DefaultGroupAs implements IGroupAs { +public class DefaultGroupAs implements IGroupAs { @NonNull private final String name; @Nullable @@ -41,7 +46,10 @@ class DefaultGroupAs implements IGroupAs { @NonNull private final GroupAs annotation; - DefaultGroupAs(@NonNull GroupAs annotation) { + @SuppressFBWarnings(value = "CT_CONSTRUCTOR_THROW", justification = "Use of final fields") + public DefaultGroupAs( + @NonNull GroupAs annotation, + @NonNull Supplier defaultSupplier) { this.annotation = annotation; { String value = ModelUtil.resolveNoneOrDefault(annotation.name(), null); @@ -53,7 +61,9 @@ class DefaultGroupAs implements IGroupAs { } this.name = value; } - this.namespace = ModelUtil.resolveOptionalNamespace(annotation.namespace()); + this.namespace = ModelUtil.resolveOptionalNamespace( + annotation.namespace(), + defaultSupplier); } @Override diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/DefinitionAssembly.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/DefinitionAssembly.java index 5516cdf16..0e19ded94 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/DefinitionAssembly.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/DefinitionAssembly.java @@ -86,9 +86,11 @@ public DefinitionAssembly( this.xmlRootQName = ObjectUtils.notNull(Lazy.lazy(() -> { QName retval = null; if (rootLocalName != null) { - String namespace = ModelUtil.resolveOptionalNamespace(getAnnotation().rootNamespace()); + String namespace = ModelUtil.resolveOptionalNamespace( + getAnnotation().rootNamespace(), + () -> getContainingModule().getXmlNamespace().toASCIIString()); if (namespace == null) { - namespace = getContainingModule().getXmlNamespace().toASCIIString(); + namespace = getXmlNamespace(); } retval = new QName(namespace, rootLocalName); } diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/DefinitionField.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/DefinitionField.java index cec7806fb..b4d7ab2f3 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/DefinitionField.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/DefinitionField.java @@ -53,6 +53,7 @@ import edu.umd.cs.findbugs.annotations.NonNull; import edu.umd.cs.findbugs.annotations.Nullable; +import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; import nl.talsmasoftware.lazy4j.Lazy; //TODO: implement getProperties() @@ -113,6 +114,7 @@ private static Field getFieldValueField(Class clazz) { * @param bindingContext * the Metaschema binding context managing this class */ + @SuppressFBWarnings(value = "CT_CONSTRUCTOR_THROW", justification = "Use of final fields, except JSON key") public DefinitionField( @NonNull Class clazz, @NonNull IBindingContext bindingContext) { diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/FlagContainerSupport.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/FlagContainerSupport.java index 7d73546b6..d88292805 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/FlagContainerSupport.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/FlagContainerSupport.java @@ -26,13 +26,6 @@ package gov.nist.secauto.metaschema.databind.model.impl; -import gov.nist.secauto.metaschema.core.model.IContainerFlagSupport; -import gov.nist.secauto.metaschema.core.util.CollectionUtil; -import gov.nist.secauto.metaschema.core.util.ObjectUtils; -import gov.nist.secauto.metaschema.databind.model.IBoundInstanceFlag; -import gov.nist.secauto.metaschema.databind.model.annotations.BoundFlag; -import gov.nist.secauto.metaschema.databind.model.annotations.Ignore; - import java.lang.reflect.Field; import java.util.Collection; import java.util.Collections; @@ -45,16 +38,27 @@ import java.util.stream.Collectors; import java.util.stream.Stream; +import javax.xml.namespace.QName; + import edu.umd.cs.findbugs.annotations.NonNull; import edu.umd.cs.findbugs.annotations.Nullable; +import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; + +import gov.nist.secauto.metaschema.core.model.IContainerFlagSupport; +import gov.nist.secauto.metaschema.core.util.CollectionUtil; +import gov.nist.secauto.metaschema.core.util.ObjectUtils; +import gov.nist.secauto.metaschema.databind.model.IBoundInstanceFlag; +import gov.nist.secauto.metaschema.databind.model.annotations.BoundFlag; +import gov.nist.secauto.metaschema.databind.model.annotations.Ignore; public class FlagContainerSupport implements IContainerFlagSupport { @NonNull - private final Map flagInstances; + private final Map flagInstances; @Nullable private IBoundInstanceFlag jsonKeyFlag; @SuppressWarnings("PMD.ConstructorCallsOverridableMethod") + @SuppressFBWarnings(value = "CT_CONSTRUCTOR_THROW", justification = "Use of final fields") public FlagContainerSupport( @NonNull AbstractBoundDefinitionModelComplex definition, @Nullable Consumer peeker) { @@ -80,7 +84,7 @@ public FlagContainerSupport( this.flagInstances = CollectionUtil.unmodifiableMap(ObjectUtils.notNull(instances .peek(intermediate) .collect(Collectors.toMap( - IBoundInstanceFlag::getEffectiveName, + IBoundInstanceFlag::getXmlQName, Function.identity(), (v1, v2) -> v2, LinkedHashMap::new)))); @@ -136,7 +140,7 @@ protected void handleFlagInstance(IBoundInstanceFlag instance) { @Override @NonNull - public Map getFlagInstanceMap() { + public Map getFlagInstanceMap() { return flagInstances; } diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/IFeatureBoundContainerFlag.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/IFeatureBoundContainerFlag.java index e5d2f7976..189a0e381 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/IFeatureBoundContainerFlag.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/IFeatureBoundContainerFlag.java @@ -26,17 +26,18 @@ package gov.nist.secauto.metaschema.databind.model.impl; +import java.util.Collection; + +import javax.xml.namespace.QName; + +import edu.umd.cs.findbugs.annotations.NonNull; +import edu.umd.cs.findbugs.annotations.Nullable; import gov.nist.secauto.metaschema.core.model.IContainerFlagSupport; import gov.nist.secauto.metaschema.core.model.IFeatureContainerFlag; import gov.nist.secauto.metaschema.core.util.ObjectUtils; import gov.nist.secauto.metaschema.databind.model.IBoundContainerFlag; import gov.nist.secauto.metaschema.databind.model.IBoundInstanceFlag; -import java.util.Collection; - -import edu.umd.cs.findbugs.annotations.NonNull; -import edu.umd.cs.findbugs.annotations.Nullable; - /** * Provides default implementations based on a supporting glag container * implementation. @@ -51,7 +52,7 @@ public interface IFeatureBoundContainerFlag @Override @Nullable - default IBoundInstanceFlag getFlagInstanceByName(String name) { + default IBoundInstanceFlag getFlagInstanceByName(QName name) { return getFlagContainer().getFlagInstanceMap().get(name); } diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/IFeatureBoundContainerModelAssembly.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/IFeatureBoundContainerModelAssembly.java index a9f423051..e64c9148b 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/IFeatureBoundContainerModelAssembly.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/IFeatureBoundContainerModelAssembly.java @@ -26,6 +26,13 @@ package gov.nist.secauto.metaschema.databind.model.impl; +import java.util.Collection; +import java.util.List; +import java.util.Map; + +import javax.xml.namespace.QName; + +import edu.umd.cs.findbugs.annotations.NonNull; import gov.nist.secauto.metaschema.core.model.IChoiceInstance; import gov.nist.secauto.metaschema.core.model.IContainerModelAssemblySupport; import gov.nist.secauto.metaschema.core.model.IFeatureContainerModelAssembly; @@ -37,12 +44,6 @@ import gov.nist.secauto.metaschema.databind.model.IBoundInstanceModelField; import gov.nist.secauto.metaschema.databind.model.IBoundInstanceModelNamed; -import java.util.Collection; -import java.util.List; -import java.util.Map; - -import edu.umd.cs.findbugs.annotations.NonNull; - public interface IFeatureBoundContainerModelAssembly< MI extends IBoundInstanceModel, NMI extends IBoundInstanceModelNamed, @@ -60,7 +61,7 @@ default Collection getModelInstances() { } @Override - default NMI getNamedModelInstanceByName(String name) { + default NMI getNamedModelInstanceByName(QName name) { return getModelContainer().getNamedModelInstanceMap().get(name); } @@ -71,7 +72,7 @@ default Collection getNamedModelInstances() { } @Override - default FI getFieldInstanceByName(String name) { + default FI getFieldInstanceByName(QName name) { return getModelContainer().getFieldInstanceMap().get(name); } @@ -82,7 +83,7 @@ default Collection getFieldInstances() { } @Override - default AI getAssemblyInstanceByName(String name) { + default AI getAssemblyInstanceByName(QName name) { return getModelContainer().getAssemblyInstanceMap().get(name); } diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/IFeatureBoundContainerModelChoiceGroup.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/IFeatureBoundContainerModelChoiceGroup.java index 12e7ba376..b4b03d538 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/IFeatureBoundContainerModelChoiceGroup.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/IFeatureBoundContainerModelChoiceGroup.java @@ -26,16 +26,17 @@ package gov.nist.secauto.metaschema.databind.model.impl; +import java.util.Collection; + +import javax.xml.namespace.QName; + +import edu.umd.cs.findbugs.annotations.NonNull; import gov.nist.secauto.metaschema.core.model.IContainerModelSupport; import gov.nist.secauto.metaschema.databind.model.IBoundContainerModelChoiceGroup; import gov.nist.secauto.metaschema.databind.model.IBoundInstanceModelGroupedAssembly; import gov.nist.secauto.metaschema.databind.model.IBoundInstanceModelGroupedField; import gov.nist.secauto.metaschema.databind.model.IBoundInstanceModelGroupedNamed; -import java.util.Collection; - -import edu.umd.cs.findbugs.annotations.NonNull; - public interface IFeatureBoundContainerModelChoiceGroup extends IBoundContainerModelChoiceGroup { @@ -47,7 +48,7 @@ public interface IFeatureBoundContainerModelChoiceGroup IBoundInstanceModelGroupedAssembly> getModelContainer(); @Override - default IBoundInstanceModelGroupedNamed getNamedModelInstanceByName(String name) { + default IBoundInstanceModelGroupedNamed getNamedModelInstanceByName(QName name) { return getModelContainer().getNamedModelInstanceMap().get(name); } @@ -58,7 +59,7 @@ default Collection getNamedModelInstances() { } @Override - default IBoundInstanceModelGroupedField getFieldInstanceByName(String name) { + default IBoundInstanceModelGroupedField getFieldInstanceByName(QName name) { return getModelContainer().getFieldInstanceMap().get(name); } @@ -69,7 +70,7 @@ default Collection getFieldInstances() { } @Override - default IBoundInstanceModelGroupedAssembly getAssemblyInstanceByName(String name) { + default IBoundInstanceModelGroupedAssembly getAssemblyInstanceByName(QName name) { return getModelContainer().getAssemblyInstanceMap().get(name); } diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/InstanceFlagInline.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/InstanceFlagInline.java index 3ba3d1d2c..39a812c32 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/InstanceFlagInline.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/InstanceFlagInline.java @@ -151,7 +151,9 @@ public Integer getUseIndex() { @Override public String getXmlNamespace() { - return ModelUtil.resolveOptionalNamespace(getAnnotation().namespace()); + return ModelUtil.resolveOptionalNamespace( + getAnnotation().namespace(), + () -> getContainingDefinition().getXmlNamespace()); } @Override diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/InstanceModelAssemblyComplex.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/InstanceModelAssemblyComplex.java index d535d9180..9dd778047 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/InstanceModelAssemblyComplex.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/InstanceModelAssemblyComplex.java @@ -44,6 +44,7 @@ import java.util.function.Predicate; import edu.umd.cs.findbugs.annotations.NonNull; +import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; import nl.talsmasoftware.lazy4j.Lazy; /** @@ -73,13 +74,16 @@ public class InstanceModelAssemblyComplex * @param containingDefinition * the definition containing this instance */ + @SuppressFBWarnings(value = "CT_CONSTRUCTOR_THROW", justification = "Use of final fields") public InstanceModelAssemblyComplex( @NonNull Field javaField, @NonNull IBoundDefinitionModelAssembly definition, @NonNull IBoundDefinitionModelAssembly containingDefinition) { super(javaField, BoundAssembly.class, containingDefinition); this.definition = definition; - this.groupAs = ModelUtil.groupAs(getAnnotation().groupAs()); + this.groupAs = ModelUtil.groupAs( + getAnnotation().groupAs(), + () -> containingDefinition.getXmlNamespace()); if (getMaxOccurs() == -1 || getMaxOccurs() > 1) { if (IGroupAs.SINGLETON_GROUP_AS.equals(this.groupAs)) { throw new IllegalStateException(String.format("Field '%s' on class '%s' is missing the '%s' annotation.", @@ -143,6 +147,13 @@ public Integer getUseIndex() { return value == Integer.MIN_VALUE ? null : value; } + @Override + public String getXmlNamespace() { + return ModelUtil.resolveOptionalNamespace( + getAnnotation().namespace(), + () -> getContainingDefinition().getXmlNamespace()); + } + @Override public final int getMinOccurs() { return getAnnotation().minOccurs(); diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/InstanceModelChoiceGroup.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/InstanceModelChoiceGroup.java index 99a7d30ec..4a0eb0cea 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/InstanceModelChoiceGroup.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/InstanceModelChoiceGroup.java @@ -57,6 +57,7 @@ import edu.umd.cs.findbugs.annotations.NonNull; import edu.umd.cs.findbugs.annotations.Nullable; +import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; import nl.talsmasoftware.lazy4j.Lazy; /** @@ -85,11 +86,14 @@ public class InstanceModelChoiceGroup * @param containingDefinition * the definition containing this instance */ + @SuppressFBWarnings(value = "CT_CONSTRUCTOR_THROW", justification = "Use of final fields") public InstanceModelChoiceGroup( @NonNull Field javaField, @NonNull IBoundDefinitionModelAssembly containingDefinition) { super(javaField, BoundChoiceGroup.class, containingDefinition); - this.groupAs = ModelUtil.groupAs(getAnnotation().groupAs()); + this.groupAs = ModelUtil.groupAs( + getAnnotation().groupAs(), + () -> containingDefinition.getXmlNamespace()); if (getMaxOccurs() == -1 || getMaxOccurs() > 1) { if (IGroupAs.SINGLETON_GROUP_AS.equals(this.groupAs)) { throw new IllegalStateException(String.format("Field '%s' on class '%s' is missing the '%s' annotation.", @@ -229,7 +233,8 @@ public IBoundInstanceFlag getItemJsonKey(Object item) { Class clazz = item.getClass(); IBoundInstanceModelGroupedNamed itemInstance = getClassToInstanceMap().get(clazz); - retval = itemInstance.getDefinition().getFlagInstanceByName(jsonKeyFlagName); + retval = itemInstance.getDefinition().getFlagInstanceByName( + new QName(itemInstance.getXmlNamespace(), jsonKeyFlagName)); } return retval; } @@ -242,11 +247,11 @@ private static class ChoiceGroupModelContainerSupport IBoundInstanceModelGroupedAssembly> { @NonNull - private final Map namedModelInstances; + private final Map namedModelInstances; @NonNull - private final Map fieldInstances; + private final Map fieldInstances; @NonNull - private final Map assemblyInstances; + private final Map assemblyInstances; public ChoiceGroupModelContainerSupport( @NonNull BoundGroupedAssembly[] assemblies, @@ -259,7 +264,7 @@ public ChoiceGroupModelContainerSupport( container); }) .collect(Collectors.toMap( - instance -> instance.getEffectiveName(), + instance -> instance.getXmlQName(), Function.identity(), CustomCollectors.useLastMapper(), LinkedHashMap::new)))); @@ -269,7 +274,7 @@ public ChoiceGroupModelContainerSupport( return IBoundInstanceModelGroupedField.newInstance(instance, container); }) .collect(Collectors.toMap( - instance -> instance.getEffectiveName(), + instance -> instance.getXmlQName(), Function.identity(), CustomCollectors.useLastMapper(), LinkedHashMap::new)))); @@ -290,17 +295,17 @@ public Collection getModelInstances() { } @Override - public Map getNamedModelInstanceMap() { + public Map getNamedModelInstanceMap() { return namedModelInstances; } @Override - public Map getFieldInstanceMap() { + public Map getFieldInstanceMap() { return fieldInstances; } @Override - public Map getAssemblyInstanceMap() { + public Map getAssemblyInstanceMap() { return assemblyInstances; } } diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/InstanceModelFieldComplex.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/InstanceModelFieldComplex.java index 5f7deebef..9f3734450 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/InstanceModelFieldComplex.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/InstanceModelFieldComplex.java @@ -41,6 +41,7 @@ import java.util.function.Predicate; import edu.umd.cs.findbugs.annotations.NonNull; +import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; import nl.talsmasoftware.lazy4j.Lazy; /** @@ -69,6 +70,7 @@ public class InstanceModelFieldComplex * @param containingDefinition * the definition containing this instance */ + @SuppressFBWarnings(value = "CT_CONSTRUCTOR_THROW", justification = "Use of final fields") public InstanceModelFieldComplex( @NonNull Field javaField, @NonNull DefinitionField definition, diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/InstanceModelGroupedAssembly.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/InstanceModelGroupedAssembly.java index e9d2615e1..dfdc2e2ad 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/InstanceModelGroupedAssembly.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/InstanceModelGroupedAssembly.java @@ -125,6 +125,13 @@ public Integer getUseIndex() { return ModelUtil.resolveNullOrInteger(getAnnotation().useIndex()); } + @Override + public String getXmlNamespace() { + return ModelUtil.resolveOptionalNamespace( + getAnnotation().namespace(), + () -> getContainingDefinition().getXmlNamespace()); + } + @Override public Class getBoundClass() { return getAnnotation().binding(); diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/InstanceModelGroupedFieldComplex.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/InstanceModelGroupedFieldComplex.java index 07e65912d..c58ee02f8 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/InstanceModelGroupedFieldComplex.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/InstanceModelGroupedFieldComplex.java @@ -119,6 +119,13 @@ public Integer getUseIndex() { return ModelUtil.resolveNullOrInteger(getAnnotation().useIndex()); } + @Override + public String getXmlNamespace() { + return ModelUtil.resolveOptionalNamespace( + getAnnotation().namespace(), + () -> getContainingDefinition().getXmlNamespace()); + } + @Override public Class getBoundClass() { return getAnnotation().binding(); diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/BindingConstraintLoader.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/BindingConstraintLoader.java index d72d23c57..ee3d1899f 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/BindingConstraintLoader.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/BindingConstraintLoader.java @@ -27,7 +27,6 @@ package gov.nist.secauto.metaschema.databind.model.metaschema; import gov.nist.secauto.metaschema.core.metapath.MetapathException; -import gov.nist.secauto.metaschema.core.metapath.MetapathExpression; import gov.nist.secauto.metaschema.core.model.AbstractLoader; import gov.nist.secauto.metaschema.core.model.IAssemblyDefinition; import gov.nist.secauto.metaschema.core.model.IConstraintLoader; @@ -214,33 +213,33 @@ protected List parseScopedConstraints( private static AssemblyTargetedConstraints handleScopedAssembly( @NonNull MetaschemaModuleConstraints.Scope.Assembly obj, @NonNull ISource source) { - MetapathExpression expression = MetapathExpression.compile(ObjectUtils.requireNonNull(obj.getTarget())); - IModelConstrained constraints = new AssemblyConstraintSet(); ConstraintBindingSupport.parse(constraints, obj, source); - return new AssemblyTargetedConstraints(expression, constraints); + return new AssemblyTargetedConstraints( + ObjectUtils.requireNonNull(obj.getTarget()), + constraints); } private static FieldTargetedConstraints handleScopedField( @NonNull MetaschemaModuleConstraints.Scope.Field obj, @NonNull ISource source) { - MetapathExpression expression = MetapathExpression.compile(ObjectUtils.requireNonNull(obj.getTarget())); - IValueConstrained constraints = new ValueConstraintSet(); ConstraintBindingSupport.parse(constraints, obj, source); - return new FieldTargetedConstraints(expression, constraints); + return new FieldTargetedConstraints( + ObjectUtils.requireNonNull(obj.getTarget()), + constraints); } private static FlagTargetedConstraints handleScopedFlag( @NonNull MetaschemaModuleConstraints.Scope.Flag obj, @NonNull ISource source) { - MetapathExpression expression = MetapathExpression.compile(ObjectUtils.requireNonNull(obj.getTarget())); - IValueConstrained constraints = new ValueConstraintSet(); ConstraintBindingSupport.parse(constraints, obj, source); - return new FlagTargetedConstraints(expression, constraints); + return new FlagTargetedConstraints( + ObjectUtils.requireNonNull(obj.getTarget()), + constraints); } private Context parseContext( @@ -293,22 +292,21 @@ public Context( @NonNull IModelConstrained constraints) { this.metapaths = metapaths; this.constraints = constraints; - this.targetedConstraints = Lazy.lazy(() -> { + this.targetedConstraints = ObjectUtils.notNull(Lazy.lazy(() -> { Stream paths = getMetapaths().stream() - .map(metapath -> MetapathExpression.compile(ObjectUtils.notNull(metapath))) - .map(compiledMetapath -> new MetaTargetedContraints(ObjectUtils.notNull(compiledMetapath), constraints)); + .map(metapath -> new MetaTargetedContraints(ObjectUtils.notNull(metapath), constraints)); Stream childPaths = childContexts.stream() .flatMap(child -> child.getTargetedConstraints().stream()); return Stream.concat(paths, childPaths) .collect(Collectors.toUnmodifiableList()); - }); + })); } @NonNull public List getTargetedConstraints() { - return targetedConstraints.get(); + return ObjectUtils.notNull(targetedConstraints.get()); } public void addAll(@NonNull Collection childContexts) { @@ -326,7 +324,7 @@ private static class MetaTargetedContraints implements IFeatureModelConstrained { protected MetaTargetedContraints( - @NonNull MetapathExpression target, + @NonNull String target, @NonNull IModelConstrained constraints) { super(target, constraints); } diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/IBindingContainerModelAbsolute.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/IBindingContainerModelAbsolute.java index 98821f802..d77d7cbfe 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/IBindingContainerModelAbsolute.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/IBindingContainerModelAbsolute.java @@ -26,10 +26,12 @@ package gov.nist.secauto.metaschema.databind.model.metaschema; -import gov.nist.secauto.metaschema.core.model.IContainerModelAbsolute; - import java.util.Collection; +import javax.xml.namespace.QName; + +import gov.nist.secauto.metaschema.core.model.IContainerModelAbsolute; + public interface IBindingContainerModelAbsolute extends IContainerModelAbsolute { @Override @@ -42,17 +44,17 @@ public interface IBindingContainerModelAbsolute extends IContainerModelAbsolute Collection getNamedModelInstances(); @Override - IBindingInstanceModelNamedAbsolute getNamedModelInstanceByName(String name); + IBindingInstanceModelNamedAbsolute getNamedModelInstanceByName(QName name); @Override Collection getFieldInstances(); @Override - IBindingInstanceModelFieldAbsolute getFieldInstanceByName(String name); + IBindingInstanceModelFieldAbsolute getFieldInstanceByName(QName name); @Override Collection getAssemblyInstances(); @Override - IBindingInstanceModelAssemblyAbsolute getAssemblyInstanceByName(String name); + IBindingInstanceModelAssemblyAbsolute getAssemblyInstanceByName(QName name); } diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/IBindingContainerModelGrouped.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/IBindingContainerModelGrouped.java index 1c94fdfd5..1641c4e29 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/IBindingContainerModelGrouped.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/IBindingContainerModelGrouped.java @@ -26,10 +26,12 @@ package gov.nist.secauto.metaschema.databind.model.metaschema; -import gov.nist.secauto.metaschema.core.model.IContainerModelGrouped; - import java.util.Collection; +import javax.xml.namespace.QName; + +import gov.nist.secauto.metaschema.core.model.IContainerModelGrouped; + public interface IBindingContainerModelGrouped extends IContainerModelGrouped { @Override @@ -44,17 +46,17 @@ default Collection getModelInstance Collection getNamedModelInstances(); @Override - IBindingInstanceModelNamedGrouped getNamedModelInstanceByName(String name); + IBindingInstanceModelNamedGrouped getNamedModelInstanceByName(QName name); @Override Collection getFieldInstances(); @Override - IBindingInstanceModelFieldGrouped getFieldInstanceByName(String name); + IBindingInstanceModelFieldGrouped getFieldInstanceByName(QName name); @Override Collection getAssemblyInstances(); @Override - IBindingInstanceModelAssemblyGrouped getAssemblyInstanceByName(String name); + IBindingInstanceModelAssemblyGrouped getAssemblyInstanceByName(QName name); } diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/IBindingDefinitionModel.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/IBindingDefinitionModel.java index eb6bb5478..a660cadb6 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/IBindingDefinitionModel.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/IBindingDefinitionModel.java @@ -31,12 +31,14 @@ import java.util.Collection; +import javax.xml.namespace.QName; + import edu.umd.cs.findbugs.annotations.Nullable; public interface IBindingDefinitionModel extends IBindingDefinition, IModelDefinition { @Override - IBindingInstanceFlag getFlagInstanceByName(String name); + IBindingInstanceFlag getFlagInstanceByName(QName name); @Override Collection getFlagInstances(); @@ -47,6 +49,8 @@ public interface IBindingDefinitionModel @Override default IBindingInstanceFlag getJsonKeyFlagInstance() { String name = getJsonKeyFlagName(); - return name == null ? null : ObjectUtils.requireNonNull(getFlagInstanceByName(name)); + return name == null ? null + : ObjectUtils.requireNonNull(getFlagInstanceByName( + getContainingModule().toFlagQName(name))); } } diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/binding/FlagReference.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/binding/FlagReference.java index 3ccb2dc49..e0f529c33 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/binding/FlagReference.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/binding/FlagReference.java @@ -94,7 +94,7 @@ public class FlagReference { private String _required; @BoundField( - formalName = "Formal Name", + formalName = "Formal NameT", description = "A formal name for the data construct, to be presented in documentation.", useName = "formal-name") private String _formalName; diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/AbstractBindingModelContainerSupport.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/AbstractBindingModelContainerSupport.java index 3cd495f1e..2675858b7 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/AbstractBindingModelContainerSupport.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/AbstractBindingModelContainerSupport.java @@ -26,6 +26,12 @@ package gov.nist.secauto.metaschema.databind.model.metaschema.impl; +import java.util.List; +import java.util.Map; + +import javax.xml.namespace.QName; + +import edu.umd.cs.findbugs.annotations.NonNull; import gov.nist.secauto.metaschema.core.model.IContainerModelSupport; import gov.nist.secauto.metaschema.core.util.ObjectUtils; import gov.nist.secauto.metaschema.databind.model.IBoundInstanceModelGroupedAssembly; @@ -40,11 +46,6 @@ import gov.nist.secauto.metaschema.databind.model.metaschema.binding.AssemblyReference; import gov.nist.secauto.metaschema.databind.model.metaschema.binding.FieldReference; -import java.util.List; -import java.util.Map; - -import edu.umd.cs.findbugs.annotations.NonNull; - public abstract class AbstractBindingModelContainerSupport implements IContainerModelSupport< IBindingInstanceModelAbsolute, @@ -55,9 +56,9 @@ public abstract class AbstractBindingModelContainerSupport protected static void addInstance( @NonNull IBindingInstanceModelAssemblyAbsolute assembly, @NonNull List modelInstances, - @NonNull Map namedModelInstances, - @NonNull Map assemblyInstances) { - String effectiveName = assembly.getEffectiveName(); + @NonNull Map namedModelInstances, + @NonNull Map assemblyInstances) { + QName effectiveName = assembly.getXmlQName(); modelInstances.add(assembly); namedModelInstances.put(effectiveName, assembly); assemblyInstances.put(effectiveName, assembly); @@ -66,9 +67,9 @@ protected static void addInstance( protected static void addInstance( @NonNull IBindingInstanceModelFieldAbsolute field, @NonNull List modelInstances, - @NonNull Map namedModelInstances, - @NonNull Map fieldInstances) { - String effectiveName = field.getEffectiveName(); + @NonNull Map namedModelInstances, + @NonNull Map fieldInstances) { + QName effectiveName = field.getXmlQName(); modelInstances.add(field); namedModelInstances.put(effectiveName, field); fieldInstances.put(effectiveName, field); diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/AbstractInstanceModelGrouped.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/AbstractInstanceModelGrouped.java index df3c4e9b1..f905dca4f 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/AbstractInstanceModelGrouped.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/AbstractInstanceModelGrouped.java @@ -67,7 +67,7 @@ protected AbstractInstanceModelGrouped( this.properties = ModelSupport.parseProperties(properties); this.boundNodeItem = ObjectUtils.notNull( Lazy.lazy(() -> (IAssemblyNodeItem) getContainingDefinition().getBoundNodeItem() - .getModelItemsByName(bindingInstance.getEffectiveName()) + .getModelItemsByName(bindingInstance.getXmlQName()) .get(position))); } diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/AbstractInstanceModelNamed.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/AbstractInstanceModelNamed.java index 857b57ec2..95c2bd3c8 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/AbstractInstanceModelNamed.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/AbstractInstanceModelNamed.java @@ -93,10 +93,10 @@ protected AbstractInstanceModelNamed( @Nullable GroupAs groupAs) { super(binding, parent); this.properties = ModelSupport.parseProperties(properties); - this.groupAs = ModelSupport.groupAs(groupAs); + this.groupAs = ModelSupport.groupAs(groupAs, parent.getOwningDefinition().getXmlNamespace()); this.boundNodeItem = ObjectUtils.notNull( Lazy.lazy(() -> (IAssemblyNodeItem) getContainingDefinition().getBoundNodeItem() - .getModelItemsByName(bindingInstance.getEffectiveName()) + .getModelItemsByName(bindingInstance.getXmlQName()) .get(position))); } diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/AssemblyModelContainerSupport.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/AssemblyModelContainerSupport.java index b611feca0..6584aac0e 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/AssemblyModelContainerSupport.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/AssemblyModelContainerSupport.java @@ -26,6 +26,17 @@ package gov.nist.secauto.metaschema.databind.model.metaschema.impl; +import java.util.LinkedHashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; + +import javax.xml.namespace.QName; + +import edu.umd.cs.findbugs.annotations.NonNull; +import edu.umd.cs.findbugs.annotations.Nullable; +import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; + import gov.nist.secauto.metaschema.core.metapath.item.node.INodeItemFactory; import gov.nist.secauto.metaschema.core.model.IContainerModelAssemblySupport; import gov.nist.secauto.metaschema.core.util.CollectionUtil; @@ -48,14 +59,6 @@ import gov.nist.secauto.metaschema.databind.model.metaschema.binding.InlineDefineAssembly; import gov.nist.secauto.metaschema.databind.model.metaschema.binding.InlineDefineField; -import java.util.LinkedHashMap; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; - -import edu.umd.cs.findbugs.annotations.NonNull; -import edu.umd.cs.findbugs.annotations.Nullable; - class AssemblyModelContainerSupport extends AbstractBindingModelContainerSupport implements IContainerModelAssemblySupport< @@ -68,17 +71,18 @@ class AssemblyModelContainerSupport @NonNull private final List modelInstances; @NonNull - private final Map namedModelInstances; + private final Map namedModelInstances; @NonNull - private final Map fieldInstances; + private final Map fieldInstances; @NonNull - private final Map assemblyInstances; + private final Map assemblyInstances; @NonNull private final List choiceInstances; @NonNull private final Map choiceGroupInstances; @SuppressWarnings("PMD.ShortMethodName") + @SuppressFBWarnings(value = "CT_CONSTRUCTOR_THROW", justification = "Use of final fields") public static IContainerModelAssemblySupport< IBindingInstanceModelAbsolute, IBindingInstanceModelNamedAbsolute, @@ -113,6 +117,7 @@ IInstanceModelChoiceGroupBinding> of( * the node item factory used to generate child nodes */ @SuppressWarnings({ "PMD.AvoidInstantiatingObjectsInLoops", "PMD.UseConcurrentHashMap", "PMD.PrematureDeclaration" }) + @SuppressFBWarnings(value = "CT_CONSTRUCTOR_THROW", justification = "Use of final fields") protected AssemblyModelContainerSupport( @NonNull AssemblyModel model, @NonNull IBoundInstanceModelAssembly modelInstance, @@ -121,9 +126,9 @@ protected AssemblyModelContainerSupport( // create temporary collections to store the child binding objects final List modelInstances = new LinkedList<>(); - final Map namedModelInstances = new LinkedHashMap<>(); - final Map fieldInstances = new LinkedHashMap<>(); - final Map assemblyInstances = new LinkedHashMap<>(); + final Map namedModelInstances = new LinkedHashMap<>(); + final Map fieldInstances = new LinkedHashMap<>(); + final Map assemblyInstances = new LinkedHashMap<>(); final List choiceInstances = new LinkedList<>(); final Map choiceGroupInstances = new LinkedHashMap<>(); @@ -220,17 +225,17 @@ public List getModelInstances() { } @Override - public Map getNamedModelInstanceMap() { + public Map getNamedModelInstanceMap() { return namedModelInstances; } @Override - public Map getFieldInstanceMap() { + public Map getFieldInstanceMap() { return fieldInstances; } @Override - public Map getAssemblyInstanceMap() { + public Map getAssemblyInstanceMap() { return assemblyInstances; } diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/BindingModule.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/BindingModule.java index 354166766..dbf625f31 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/BindingModule.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/BindingModule.java @@ -53,6 +53,7 @@ import java.util.stream.Stream; import edu.umd.cs.findbugs.annotations.NonNull; +import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; import nl.talsmasoftware.lazy4j.Lazy; public class BindingModule @@ -95,6 +96,7 @@ public class BindingModule * if a processing error occurs */ @SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops") + @SuppressFBWarnings(value = "CT_CONSTRUCTOR_THROW", justification = "Use of final fields") public BindingModule( // NOPMD - unavoidable @NonNull URI resource, @NonNull IBoundDefinitionModelAssembly rootDefinition, diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/ChoiceGroupModelContainerSupport.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/ChoiceGroupModelContainerSupport.java index 623350755..ac214e16a 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/ChoiceGroupModelContainerSupport.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/ChoiceGroupModelContainerSupport.java @@ -26,6 +26,17 @@ package gov.nist.secauto.metaschema.databind.model.metaschema.impl; +import java.util.Collection; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +import javax.xml.namespace.QName; + +import edu.umd.cs.findbugs.annotations.NonNull; +import edu.umd.cs.findbugs.annotations.Nullable; +import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; + import gov.nist.secauto.metaschema.core.metapath.item.node.INodeItemFactory; import gov.nist.secauto.metaschema.core.model.IContainerModelSupport; import gov.nist.secauto.metaschema.core.util.CollectionUtil; @@ -41,14 +52,6 @@ import gov.nist.secauto.metaschema.databind.model.metaschema.IInstanceModelChoiceGroupBinding; import gov.nist.secauto.metaschema.databind.model.metaschema.binding.AssemblyModel; -import java.util.Collection; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; - -import edu.umd.cs.findbugs.annotations.NonNull; -import edu.umd.cs.findbugs.annotations.Nullable; - class ChoiceGroupModelContainerSupport implements IContainerModelSupport< IBindingInstanceModelNamedGrouped, @@ -56,11 +59,11 @@ class ChoiceGroupModelContainerSupport IBindingInstanceModelFieldGrouped, IBindingInstanceModelAssemblyGrouped> { @NonNull - private final Map namedModelInstances; + private final Map namedModelInstances; @NonNull - private final Map fieldInstances; + private final Map fieldInstances; @NonNull - private final Map assemblyInstances; + private final Map assemblyInstances; @SuppressWarnings("PMD.ShortMethodName") public static IContainerModelSupport< @@ -95,6 +98,7 @@ IBindingInstanceModelAssemblyGrouped> of( * the node item factory used to generate child nodes */ @SuppressWarnings({ "PMD.AvoidInstantiatingObjectsInLoops", "PMD.UseConcurrentHashMap", "PMD.PrematureDeclaration" }) + @SuppressFBWarnings(value = "CT_CONSTRUCTOR_THROW", justification = "Use of final fields") public ChoiceGroupModelContainerSupport( @NonNull AssemblyModel.ChoiceGroup binding, @NonNull IBoundInstanceModelGroupedAssembly bindingInstance, @@ -102,9 +106,9 @@ public ChoiceGroupModelContainerSupport( @NonNull INodeItemFactory nodeItemFactory) { // create temporary collections to store the child binding objects - final Map namedModelInstances = new LinkedHashMap<>(); - final Map fieldInstances = new LinkedHashMap<>(); - final Map assemblyInstances = new LinkedHashMap<>(); + final Map namedModelInstances = new LinkedHashMap<>(); + final Map fieldInstances = new LinkedHashMap<>(); + final Map assemblyInstances = new LinkedHashMap<>(); // create counters to track child positions int assemblyReferencePosition = 0; @@ -167,18 +171,18 @@ public ChoiceGroupModelContainerSupport( protected static void addInstance( @NonNull IBindingInstanceModelAssemblyGrouped assembly, - @NonNull Map namedModelInstances, - @NonNull Map assemblyInstances) { - String effectiveName = assembly.getEffectiveName(); + @NonNull Map namedModelInstances, + @NonNull Map assemblyInstances) { + QName effectiveName = assembly.getXmlQName(); namedModelInstances.put(effectiveName, assembly); assemblyInstances.put(effectiveName, assembly); } protected static void addInstance( @NonNull IBindingInstanceModelFieldGrouped field, - @NonNull Map namedModelInstances, - @NonNull Map fieldInstances) { - String effectiveName = field.getEffectiveName(); + @NonNull Map namedModelInstances, + @NonNull Map fieldInstances) { + QName effectiveName = field.getXmlQName(); namedModelInstances.put(effectiveName, field); fieldInstances.put(effectiveName, field); } @@ -233,17 +237,17 @@ public Collection getModelInstances() { } @Override - public Map getNamedModelInstanceMap() { + public Map getNamedModelInstanceMap() { return namedModelInstances; } @Override - public Map getFieldInstanceMap() { + public Map getFieldInstanceMap() { return fieldInstances; } @Override - public Map getAssemblyInstanceMap() { + public Map getAssemblyInstanceMap() { return assemblyInstances; } } diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/ChoiceModelContainerSupport.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/ChoiceModelContainerSupport.java index d7b817859..2becf1dc6 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/ChoiceModelContainerSupport.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/ChoiceModelContainerSupport.java @@ -26,6 +26,17 @@ package gov.nist.secauto.metaschema.databind.model.metaschema.impl; +import java.util.LinkedHashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; + +import javax.xml.namespace.QName; + +import edu.umd.cs.findbugs.annotations.NonNull; +import edu.umd.cs.findbugs.annotations.Nullable; +import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; + import gov.nist.secauto.metaschema.core.metapath.item.node.INodeItemFactory; import gov.nist.secauto.metaschema.core.model.IContainerModelSupport; import gov.nist.secauto.metaschema.core.util.CollectionUtil; @@ -43,24 +54,16 @@ import gov.nist.secauto.metaschema.databind.model.metaschema.binding.InlineDefineAssembly; import gov.nist.secauto.metaschema.databind.model.metaschema.binding.InlineDefineField; -import java.util.LinkedHashMap; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; - -import edu.umd.cs.findbugs.annotations.NonNull; -import edu.umd.cs.findbugs.annotations.Nullable; - class ChoiceModelContainerSupport extends AbstractBindingModelContainerSupport { @NonNull private final List modelInstances; @NonNull - private final Map namedModelInstances; + private final Map namedModelInstances; @NonNull - private final Map fieldInstances; + private final Map fieldInstances; @NonNull - private final Map assemblyInstances; + private final Map assemblyInstances; @SuppressWarnings("PMD.ShortMethodName") public static IContainerModelSupport< @@ -95,6 +98,7 @@ IBindingInstanceModelAssemblyAbsolute> of( * the node item factory used to generate child nodes */ @SuppressWarnings({ "PMD.AvoidInstantiatingObjectsInLoops", "PMD.UseConcurrentHashMap", "PMD.PrematureDeclaration" }) + @SuppressFBWarnings(value = "CT_CONSTRUCTOR_THROW", justification = "Use of final fields") public ChoiceModelContainerSupport( @NonNull Choice binding, @NonNull IBoundInstanceModelGroupedAssembly bindingInstance, @@ -103,9 +107,9 @@ public ChoiceModelContainerSupport( // create temporary collections to store the child binding objects final List modelInstances = new LinkedList<>(); - final Map namedModelInstances = new LinkedHashMap<>(); - final Map fieldInstances = new LinkedHashMap<>(); - final Map assemblyInstances = new LinkedHashMap<>(); + final Map namedModelInstances = new LinkedHashMap<>(); + final Map fieldInstances = new LinkedHashMap<>(); + final Map assemblyInstances = new LinkedHashMap<>(); // create counters to track child positions int assemblyReferencePosition = 0; @@ -174,17 +178,17 @@ public List getModelInstances() { } @Override - public Map getNamedModelInstanceMap() { + public Map getNamedModelInstanceMap() { return namedModelInstances; } @Override - public Map getFieldInstanceMap() { + public Map getFieldInstanceMap() { return fieldInstances; } @Override - public Map getAssemblyInstanceMap() { + public Map getAssemblyInstanceMap() { return assemblyInstances; } } diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/ConstraintBindingSupport.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/ConstraintBindingSupport.java index 63e10c9f4..527e36fbd 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/ConstraintBindingSupport.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/ConstraintBindingSupport.java @@ -28,7 +28,6 @@ import gov.nist.secauto.metaschema.core.datatype.IDataTypeAdapter; import gov.nist.secauto.metaschema.core.datatype.markup.MarkupLine; -import gov.nist.secauto.metaschema.core.metapath.MetapathExpression; import gov.nist.secauto.metaschema.core.model.constraint.AbstractConstraintBuilder; import gov.nist.secauto.metaschema.core.model.constraint.AbstractKeyConstraintBuilder; import gov.nist.secauto.metaschema.core.model.constraint.IAllowedValuesConstraint; @@ -69,6 +68,8 @@ import java.util.List; import java.util.regex.Pattern; +import javax.xml.namespace.QName; + import edu.umd.cs.findbugs.annotations.NonNull; import edu.umd.cs.findbugs.annotations.Nullable; @@ -108,7 +109,7 @@ public static void parseLet( // parse let expressions for (ConstraintLetExpression letObj : constraints.getLets()) { ILet let = ILet.of( - ObjectUtils.requireNonNull(letObj.getVar()), + ObjectUtils.requireNonNull(new QName(letObj.getVar())), ObjectUtils.requireNonNull(letObj.getExpression()), source); constraintSet.addLetExpression(let); } @@ -370,10 +371,10 @@ private static IUniqueConstraint newUnique( } @NonNull - private static MetapathExpression target(@Nullable String target) { + private static String target(@Nullable String target) { return target == null - ? MetapathExpression.CONTEXT_NODE - : MetapathExpression.compile(target); + ? IConstraint.DEFAULT_TARGET_METAPATH + : target; } @NonNull diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/DefinitionAssemblyGlobal.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/DefinitionAssemblyGlobal.java index 235a1b6aa..da31161bc 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/DefinitionAssemblyGlobal.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/DefinitionAssemblyGlobal.java @@ -31,6 +31,7 @@ import gov.nist.secauto.metaschema.core.metapath.item.node.IAssemblyNodeItem; import gov.nist.secauto.metaschema.core.metapath.item.node.INodeItem; import gov.nist.secauto.metaschema.core.metapath.item.node.INodeItemFactory; +import gov.nist.secauto.metaschema.core.model.IAssemblyDefinition; import gov.nist.secauto.metaschema.core.model.IAttributable; import gov.nist.secauto.metaschema.core.model.IContainerFlagSupport; import gov.nist.secauto.metaschema.core.model.IContainerModelAssemblySupport; @@ -64,6 +65,7 @@ public class DefinitionAssemblyGlobal implements IBindingDefinitionAssembly, IFeatureBindingContainerFlag, IFeatureBindingContainerModelAssembly { + @NonNull private final Map> properties; @NonNull @@ -111,7 +113,8 @@ public DefinitionAssemblyGlobal( this))); this.modelContainer = ObjectUtils.notNull(Lazy.lazy(() -> AssemblyModelContainerSupport.of( binding.getModel(), - ObjectUtils.requireNonNull(bindingInstance.getDefinition().getAssemblyInstanceByName("model")), + ObjectUtils + .requireNonNull(bindingInstance.getDefinition().getAssemblyInstanceByName(IAssemblyDefinition.MODEL_QNAME)), this, nodeItemFactory))); this.modelConstraints = ObjectUtils.notNull(Lazy.lazy(() -> @@ -127,7 +130,7 @@ public DefinitionAssemblyGlobal( this.boundNodeItem = ObjectUtils.notNull(Lazy.lazy(() -> (IAssemblyNodeItem) getContainingModule().getBoundNodeItem() - .getModelItemsByName(bindingInstance.getEffectiveName()) + .getModelItemsByName(bindingInstance.getXmlQName()) .get(position))); } diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/DefinitionFieldGlobal.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/DefinitionFieldGlobal.java index eef7c4295..7911f1fcb 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/DefinitionFieldGlobal.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/DefinitionFieldGlobal.java @@ -51,6 +51,8 @@ import java.util.Map; import java.util.Set; +import javax.xml.namespace.QName; + import edu.umd.cs.findbugs.annotations.NonNull; import edu.umd.cs.findbugs.annotations.Nullable; import nl.talsmasoftware.lazy4j.Lazy; @@ -95,7 +97,7 @@ public DefinitionFieldGlobal( })); this.boundNodeItem = ObjectUtils.notNull( Lazy.lazy(() -> (IAssemblyNodeItem) getContainingModule().getBoundNodeItem() - .getModelItemsByName(bindingInstance.getEffectiveName()) + .getModelItemsByName(bindingInstance.getXmlQName()) .get(position))); } @@ -173,8 +175,10 @@ public String getJsonKeyFlagName() { @Override public IFlagInstance getJsonValueKeyFlagInstance() { JsonValueKeyFlag obj = getBinding().getJsonValueKeyFlag(); - String flagName = obj == null ? null : obj.getFlagRef(); - return flagName == null ? null : getFlagInstanceByName(flagName); + String name = obj == null ? null : obj.getFlagRef(); + return name == null ? null + : ObjectUtils.requireNonNull(getFlagInstanceByName( + getContainingModule().toFlagQName(name))); } @Override diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/DefinitionFlagGlobal.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/DefinitionFlagGlobal.java index 7bf9db49f..e506fe03d 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/DefinitionFlagGlobal.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/DefinitionFlagGlobal.java @@ -97,7 +97,7 @@ public DefinitionFlagGlobal( })); this.boundNodeItem = ObjectUtils.notNull( Lazy.lazy(() -> (IAssemblyNodeItem) getContainingModule().getBoundNodeItem() - .getModelItemsByName(bindingInstance.getEffectiveName()) + .getModelItemsByName(bindingInstance.getXmlQName()) .get(position))); // new NodeItem(bindingInstance, position, generator) } diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/FlagContainerSupport.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/FlagContainerSupport.java index 8acc7322d..653b6f51e 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/FlagContainerSupport.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/FlagContainerSupport.java @@ -26,6 +26,19 @@ package gov.nist.secauto.metaschema.databind.model.metaschema.impl; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +import javax.xml.namespace.QName; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import edu.umd.cs.findbugs.annotations.NonNull; +import edu.umd.cs.findbugs.annotations.Nullable; +import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; + import gov.nist.secauto.metaschema.core.model.IContainerFlagSupport; import gov.nist.secauto.metaschema.core.model.IModelDefinition; import gov.nist.secauto.metaschema.core.model.IModule; @@ -40,20 +53,10 @@ import gov.nist.secauto.metaschema.databind.model.metaschema.binding.FlagReference; import gov.nist.secauto.metaschema.databind.model.metaschema.binding.InlineDefineFlag; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; - -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; - -import edu.umd.cs.findbugs.annotations.NonNull; -import edu.umd.cs.findbugs.annotations.Nullable; - public class FlagContainerSupport implements IContainerFlagSupport { private static final Logger LOGGER = LogManager.getLogger(FlagContainerSupport.class); @NonNull - private final Map flagInstances; + private final Map flagInstances; @SuppressWarnings("PMD.ShortMethodName") public static IContainerFlagSupport of( @@ -69,12 +72,13 @@ public static IContainerFlagSupport of( } @SuppressWarnings("PMD.UseConcurrentHashMap") + @SuppressFBWarnings(value = "CT_CONSTRUCTOR_THROW", justification = "Use of final fields") public FlagContainerSupport( @NonNull List flags, @NonNull IBoundInstanceModelGroupedAssembly bindingInstance, @NonNull IBindingDefinitionModel parent) { // create temporary collections to store the child binding objects - final Map flagInstances = new LinkedHashMap<>(); + final Map flagInstances = new LinkedHashMap<>(); // create counter to track child positions int flagReferencePosition = 0; @@ -104,7 +108,7 @@ public FlagContainerSupport( } String key = flag.getEffectiveName(); - flagInstances.merge(flag.getEffectiveName(), flag, (v1, v2) -> { + flagInstances.merge(flag.getXmlQName(), flag, (v1, v2) -> { if (LOGGER.isErrorEnabled()) { IModelDefinition owningDefinition = v1.getContainingDefinition(); IModule module = owningDefinition.getContainingModule(); @@ -126,7 +130,7 @@ public FlagContainerSupport( } @Override - public Map getFlagInstanceMap() { + public Map getFlagInstanceMap() { return flagInstances; } diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/GroupAsImpl.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/GroupAsImpl.java index 521c6823e..d3ce622cc 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/GroupAsImpl.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/GroupAsImpl.java @@ -45,9 +45,9 @@ class GroupAsImpl implements IGroupAs { @NonNull private final XmlGroupAsBehavior xmlBehavior; - public GroupAsImpl(@NonNull GroupAs groupAs) { + public GroupAsImpl(@NonNull GroupAs groupAs, @Nullable String namespace) { this.name = ObjectUtils.requireNonNull(groupAs.getName()); - this.namespace = null; + this.namespace = namespace; this.jsonBehavior = ModelSupport.groupAsJsonBehavior(groupAs.getInJson()); this.xmlBehavior = ModelSupport.groupAsXmlBehavior(groupAs.getInXml()); } diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/IFeatureBindingContainerFlag.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/IFeatureBindingContainerFlag.java index 4fb0cd328..2661e5898 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/IFeatureBindingContainerFlag.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/IFeatureBindingContainerFlag.java @@ -26,17 +26,18 @@ package gov.nist.secauto.metaschema.databind.model.metaschema.impl; +import java.util.Collection; + +import javax.xml.namespace.QName; + +import edu.umd.cs.findbugs.annotations.NonNull; +import edu.umd.cs.findbugs.annotations.Nullable; import gov.nist.secauto.metaschema.core.model.IContainerFlagSupport; import gov.nist.secauto.metaschema.core.model.IFeatureContainerFlag; import gov.nist.secauto.metaschema.core.util.ObjectUtils; import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingDefinitionModel; import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingInstanceFlag; -import java.util.Collection; - -import edu.umd.cs.findbugs.annotations.NonNull; -import edu.umd.cs.findbugs.annotations.Nullable; - /** * Represents a definition that may contain flags. */ @@ -49,7 +50,7 @@ public interface IFeatureBindingContainerFlag @Override @Nullable - default IBindingInstanceFlag getFlagInstanceByName(String name) { + default IBindingInstanceFlag getFlagInstanceByName(QName name) { return getFlagContainer().getFlagInstanceMap().get(name); } diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/IFeatureBindingContainerModel.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/IFeatureBindingContainerModel.java index 0ca3cec55..63be50118 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/IFeatureBindingContainerModel.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/IFeatureBindingContainerModel.java @@ -26,6 +26,11 @@ package gov.nist.secauto.metaschema.databind.model.metaschema.impl; +import java.util.Collection; + +import javax.xml.namespace.QName; + +import edu.umd.cs.findbugs.annotations.NonNull; import gov.nist.secauto.metaschema.core.model.IContainerModelSupport; import gov.nist.secauto.metaschema.core.model.IFeatureContainerModel; import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingContainerModelAbsolute; @@ -34,10 +39,6 @@ import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingInstanceModelFieldAbsolute; import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingInstanceModelNamedAbsolute; -import java.util.Collection; - -import edu.umd.cs.findbugs.annotations.NonNull; - public interface IFeatureBindingContainerModel extends IBindingContainerModelAbsolute, IFeatureContainerModel< IBindingInstanceModelAbsolute, @@ -58,7 +59,7 @@ default Collection getModelInstances() { } @Override - default IBindingInstanceModelNamedAbsolute getNamedModelInstanceByName(String name) { + default IBindingInstanceModelNamedAbsolute getNamedModelInstanceByName(QName name) { return getModelContainer().getNamedModelInstanceMap().get(name); } @@ -69,7 +70,7 @@ default Collection getNamedModelInstances() } @Override - default IBindingInstanceModelFieldAbsolute getFieldInstanceByName(String name) { + default IBindingInstanceModelFieldAbsolute getFieldInstanceByName(QName name) { return getModelContainer().getFieldInstanceMap().get(name); } @@ -80,7 +81,7 @@ default Collection getFieldInstances() { } @Override - default IBindingInstanceModelAssemblyAbsolute getAssemblyInstanceByName(String name) { + default IBindingInstanceModelAssemblyAbsolute getAssemblyInstanceByName(QName name) { return getModelContainer().getAssemblyInstanceMap().get(name); } diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/IFeatureBindingContainerModelAssembly.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/IFeatureBindingContainerModelAssembly.java index a69b3489b..96a682e3d 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/IFeatureBindingContainerModelAssembly.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/IFeatureBindingContainerModelAssembly.java @@ -26,6 +26,13 @@ package gov.nist.secauto.metaschema.databind.model.metaschema.impl; +import java.util.Collection; +import java.util.List; +import java.util.Map; + +import javax.xml.namespace.QName; + +import edu.umd.cs.findbugs.annotations.NonNull; import gov.nist.secauto.metaschema.core.model.IContainerModelAssemblySupport; import gov.nist.secauto.metaschema.core.model.IFeatureContainerModelAssembly; import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingContainerModelAssembly; @@ -36,12 +43,6 @@ import gov.nist.secauto.metaschema.databind.model.metaschema.IInstanceModelChoiceBinding; import gov.nist.secauto.metaschema.databind.model.metaschema.IInstanceModelChoiceGroupBinding; -import java.util.Collection; -import java.util.List; -import java.util.Map; - -import edu.umd.cs.findbugs.annotations.NonNull; - public interface IFeatureBindingContainerModelAssembly extends IBindingContainerModelAssembly, IFeatureBindingContainerModel, @@ -68,7 +69,7 @@ default Collection getModelInstances() { } @Override - default IBindingInstanceModelNamedAbsolute getNamedModelInstanceByName(String name) { + default IBindingInstanceModelNamedAbsolute getNamedModelInstanceByName(QName name) { return getModelContainer().getNamedModelInstanceMap().get(name); } @@ -79,7 +80,7 @@ default Collection getNamedModelInstances() } @Override - default IBindingInstanceModelFieldAbsolute getFieldInstanceByName(String name) { + default IBindingInstanceModelFieldAbsolute getFieldInstanceByName(QName name) { return getModelContainer().getFieldInstanceMap().get(name); } @@ -90,7 +91,7 @@ default Collection getFieldInstances() { } @Override - default IBindingInstanceModelAssemblyAbsolute getAssemblyInstanceByName(String name) { + default IBindingInstanceModelAssemblyAbsolute getAssemblyInstanceByName(QName name) { return getModelContainer().getAssemblyInstanceMap().get(name); } diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/InstanceFlagInline.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/InstanceFlagInline.java index 5aad8f90d..afac75db4 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/InstanceFlagInline.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/InstanceFlagInline.java @@ -85,7 +85,7 @@ public InstanceFlagInline( })); this.boundNodeItem = ObjectUtils.notNull( Lazy.lazy(() -> (IAssemblyNodeItem) getContainingModule().getBoundNodeItem() - .getModelItemsByName(bindingInstance.getEffectiveName()) + .getModelItemsByName(bindingInstance.getXmlQName()) .get(position))); } diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/InstanceFlagReference.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/InstanceFlagReference.java index a4402a51d..7710abebe 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/InstanceFlagReference.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/InstanceFlagReference.java @@ -71,7 +71,7 @@ public InstanceFlagReference( this.defaultValue = ModelSupport.defaultValue(getBinding().getDefault(), definition.getJavaTypeAdapter()); this.boundNodeItem = ObjectUtils.notNull( Lazy.lazy(() -> (IAssemblyNodeItem) getContainingModule().getBoundNodeItem() - .getModelItemsByName(bindingInstance.getEffectiveName()) + .getModelItemsByName(bindingInstance.getXmlQName()) .get(position))); } diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/InstanceModelAssemblyInline.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/InstanceModelAssemblyInline.java index b9801eb8a..9cc18c5c9 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/InstanceModelAssemblyInline.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/InstanceModelAssemblyInline.java @@ -29,6 +29,7 @@ import gov.nist.secauto.metaschema.core.datatype.markup.MarkupLine; import gov.nist.secauto.metaschema.core.datatype.markup.MarkupMultiline; import gov.nist.secauto.metaschema.core.metapath.item.node.INodeItemFactory; +import gov.nist.secauto.metaschema.core.model.IAssemblyDefinition; import gov.nist.secauto.metaschema.core.model.IContainerFlagSupport; import gov.nist.secauto.metaschema.core.model.IContainerModelAssemblySupport; import gov.nist.secauto.metaschema.core.model.IFeatureDefinitionInstanceInlined; @@ -110,7 +111,8 @@ public InstanceModelAssemblyInline( this))); this.modelContainer = ObjectUtils.notNull(Lazy.lazy(() -> AssemblyModelContainerSupport.of( binding.getModel(), - ObjectUtils.requireNonNull(bindingInstance.getDefinition().getAssemblyInstanceByName("model")), + ObjectUtils + .requireNonNull(bindingInstance.getDefinition().getAssemblyInstanceByName(IAssemblyDefinition.MODEL_QNAME)), this, nodeItemFactory))); this.modelConstraints = ObjectUtils.notNull(Lazy.lazy(() -> { diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/InstanceModelChoice.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/InstanceModelChoice.java index cea342f94..387e0388c 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/InstanceModelChoice.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/InstanceModelChoice.java @@ -71,7 +71,7 @@ public InstanceModelChoice( nodeItemFactory))); this.boundNodeItem = ObjectUtils.notNull( Lazy.lazy(() -> (IAssemblyNodeItem) getContainingDefinition().getBoundNodeItem() - .getModelItemsByName(bindingInstance.getEffectiveName()) + .getModelItemsByName(bindingInstance.getXmlQName()) .get(position))); } @@ -105,4 +105,9 @@ public MarkupMultiline getRemarks() { // no remarks return null; } + + @Override + public String getGroupAsXmlNamespace() { + return null; + } } diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/InstanceModelChoiceGroup.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/InstanceModelChoiceGroup.java index 1ecc3cf34..6d4a6dd1d 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/InstanceModelChoiceGroup.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/InstanceModelChoiceGroup.java @@ -75,7 +75,7 @@ public InstanceModelChoiceGroup( @NonNull IBindingDefinitionAssembly parent, @NonNull INodeItemFactory nodeItemFactory) { super(binding, parent); - this.groupAs = ModelSupport.groupAs(binding.getGroupAs()); + this.groupAs = ModelSupport.groupAs(binding.getGroupAs(), parent.getXmlNamespace()); this.modelContainer = ObjectUtils.notNull(Lazy.lazy(() -> ChoiceGroupModelContainerSupport.of( binding, bindingInstance, @@ -83,7 +83,7 @@ public InstanceModelChoiceGroup( nodeItemFactory))); this.boundNodeItem = ObjectUtils.notNull( Lazy.lazy(() -> (IAssemblyNodeItem) getContainingDefinition().getBoundNodeItem() - .getModelItemsByName(bindingInstance.getJsonName()) + .getModelItemsByName(bindingInstance.getXmlQName()) .get(position))); } diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/InstanceModelFieldInline.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/InstanceModelFieldInline.java index 1ea239a10..3de41f937 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/InstanceModelFieldInline.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/InstanceModelFieldInline.java @@ -49,6 +49,8 @@ import java.math.BigInteger; +import javax.xml.namespace.QName; + import edu.umd.cs.findbugs.annotations.NonNull; import edu.umd.cs.findbugs.annotations.Nullable; import nl.talsmasoftware.lazy4j.Lazy; @@ -181,8 +183,11 @@ public int getMaxOccurs() { @Override public IFlagInstance getJsonValueKeyFlagInstance() { JsonValueKeyFlag obj = getBinding().getJsonValueKeyFlag(); - String flagName = obj == null ? null : obj.getFlagRef(); - return flagName == null ? null : getFlagInstanceByName(flagName); + String name = obj == null ? null : obj.getFlagRef(); + String namespace = getContainingModule().getXmlNamespace().toASCIIString(); + return name == null ? null + : ObjectUtils.requireNonNull(getFlagInstanceByName( + new QName(namespace, name))); } @Override diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/InstanceModelGroupedAssemblyInline.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/InstanceModelGroupedAssemblyInline.java index 4f5757c98..034d116cd 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/InstanceModelGroupedAssemblyInline.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/InstanceModelGroupedAssemblyInline.java @@ -31,6 +31,7 @@ import gov.nist.secauto.metaschema.core.metapath.item.node.IAssemblyNodeItem; import gov.nist.secauto.metaschema.core.metapath.item.node.INodeItem; import gov.nist.secauto.metaschema.core.metapath.item.node.INodeItemFactory; +import gov.nist.secauto.metaschema.core.model.IAssemblyDefinition; import gov.nist.secauto.metaschema.core.model.IAssemblyInstanceGrouped; import gov.nist.secauto.metaschema.core.model.IContainerFlagSupport; import gov.nist.secauto.metaschema.core.model.IContainerModelAssemblySupport; @@ -89,7 +90,8 @@ public InstanceModelGroupedAssemblyInline( this))); this.modelContainer = ObjectUtils.notNull(Lazy.lazy(() -> AssemblyModelContainerSupport.of( binding.getModel(), - ObjectUtils.requireNonNull(bindingInstance.getDefinition().getAssemblyInstanceByName("model")), + ObjectUtils + .requireNonNull(bindingInstance.getDefinition().getAssemblyInstanceByName(IAssemblyDefinition.MODEL_QNAME)), this, nodeItemFactory))); this.modelConstraints = ObjectUtils.notNull(Lazy.lazy(() -> { @@ -105,7 +107,7 @@ public InstanceModelGroupedAssemblyInline( })); this.boundNodeItem = ObjectUtils.notNull( Lazy.lazy(() -> (IAssemblyNodeItem) getContainingDefinition().getBoundNodeItem() - .getModelItemsByName(bindingInstance.getJsonName()) + .getModelItemsByName(bindingInstance.getXmlQName()) .get(position))); } diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/InstanceModelGroupedFieldInline.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/InstanceModelGroupedFieldInline.java index 13cf83d7e..237dce80b 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/InstanceModelGroupedFieldInline.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/InstanceModelGroupedFieldInline.java @@ -26,6 +26,10 @@ package gov.nist.secauto.metaschema.databind.model.metaschema.impl; +import javax.xml.namespace.QName; + +import edu.umd.cs.findbugs.annotations.NonNull; +import edu.umd.cs.findbugs.annotations.Nullable; import gov.nist.secauto.metaschema.core.datatype.IDataTypeAdapter; import gov.nist.secauto.metaschema.core.datatype.markup.MarkupLine; import gov.nist.secauto.metaschema.core.datatype.markup.MarkupMultiline; @@ -45,9 +49,6 @@ import gov.nist.secauto.metaschema.databind.model.metaschema.binding.AssemblyModel; import gov.nist.secauto.metaschema.databind.model.metaschema.binding.FieldConstraints; import gov.nist.secauto.metaschema.databind.model.metaschema.binding.JsonValueKeyFlag; - -import edu.umd.cs.findbugs.annotations.NonNull; -import edu.umd.cs.findbugs.annotations.Nullable; import nl.talsmasoftware.lazy4j.Lazy; public class InstanceModelGroupedFieldInline @@ -153,7 +154,7 @@ public String getDiscriminatorValue() { public IFlagInstance getJsonValueKeyFlagInstance() { JsonValueKeyFlag obj = getBinding().getJsonValueKeyFlag(); String flagName = obj == null ? null : obj.getFlagRef(); - return flagName == null ? null : getFlagInstanceByName(flagName); + return flagName == null ? null : getFlagInstanceByName(new QName(getXmlNamespace(), flagName)); } @Override diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/ModelSupport.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/ModelSupport.java index b927dcb98..caee807b3 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/ModelSupport.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/ModelSupport.java @@ -181,10 +181,12 @@ public static boolean fieldInXml(@Nullable String inXml) { } @NonNull - public static IGroupAs groupAs(@Nullable GroupAs groupAs) { + public static IGroupAs groupAs( + @Nullable GroupAs groupAs, + @Nullable String groupAsNamespace) { return groupAs == null ? IGroupAs.SINGLETON_GROUP_AS - : new GroupAsImpl(groupAs); + : new GroupAsImpl(groupAs, groupAsNamespace); } @NonNull diff --git a/databind/src/test/java/gov/nist/secauto/metaschema/databind/io/xml/XmlParserTest.java b/databind/src/test/java/gov/nist/secauto/metaschema/databind/io/xml/XmlParserTest.java index 2cd34e486..17657d21a 100644 --- a/databind/src/test/java/gov/nist/secauto/metaschema/databind/io/xml/XmlParserTest.java +++ b/databind/src/test/java/gov/nist/secauto/metaschema/databind/io/xml/XmlParserTest.java @@ -50,6 +50,7 @@ import java.io.StringReader; import java.util.Collections; +import javax.xml.namespace.QName; import javax.xml.stream.XMLInputFactory; import javax.xml.stream.XMLStreamConstants; import javax.xml.stream.XMLStreamException; @@ -60,6 +61,8 @@ class XmlParserTest extends AbstractBoundModelTestSupport { + private static final String NS = "https://csrc.nist.gov/ns/test/xml"; + @Test void testXmlRead() throws IOException, XMLStreamException { String xml = "" @@ -86,9 +89,11 @@ void testXmlRead() throws IOException, XMLStreamException { = ObjectUtils.requireNonNull( (IBoundDefinitionModelAssembly) bindingContext.getBoundDefinitionForClass(MultiFieldAssembly.class)); - IBoundInstanceModelField field1Instance = ObjectUtils.requireNonNull(assembly.getFieldInstanceByName("field1")); + IBoundInstanceModelField field1Instance = ObjectUtils.requireNonNull(assembly.getFieldInstanceByName( + new QName(NS, "field1"))); - IBoundInstanceModelField field2Instance = ObjectUtils.requireNonNull(assembly.getFieldInstanceByName("field2")); + IBoundInstanceModelField field2Instance = ObjectUtils.requireNonNull(assembly.getFieldInstanceByName( + new QName(NS, "field2"))); MultiFieldAssembly obj = new MultiFieldAssembly(); @@ -114,7 +119,7 @@ void testXmlReadFlagProperty() throws JsonParseException, IOException, .requireNonNull( (IBoundDefinitionModelAssembly) bindingContext.getBoundDefinitionForClass(FlaggedAssembly.class)); - IBoundInstanceFlag idProperty = assembly.getFlagInstanceByName("id"); + IBoundInstanceFlag idProperty = assembly.getFlagInstanceByName(new QName("id")); assert idProperty != null; assertEquals(XMLStreamConstants.START_DOCUMENT, eventReader.nextEvent().getEventType()); @@ -159,10 +164,10 @@ void testXmlReadGroupedField() throws JsonParseException, IOException, XMLStream (IBoundDefinitionModelAssembly) bindingContext.getBoundDefinitionForClass(MultiFieldAssembly.class)); IBoundInstanceModelField field1Instance - = ObjectUtils.requireNonNull(assembly.getFieldInstanceByName("field1")); + = ObjectUtils.requireNonNull(assembly.getFieldInstanceByName(new QName(NS, "field1"))); IBoundInstanceModelField field2Instance - = ObjectUtils.requireNonNull(assembly.getFieldInstanceByName("field2")); + = ObjectUtils.requireNonNull(assembly.getFieldInstanceByName(new QName(NS, "field2"))); MultiFieldAssembly obj = new MultiFieldAssembly(); diff --git a/databind/src/test/java/gov/nist/secauto/metaschema/databind/testing/model/ModelTest.java b/databind/src/test/java/gov/nist/secauto/metaschema/databind/testing/model/ModelTest.java index d27d119d2..09a7c7328 100644 --- a/databind/src/test/java/gov/nist/secauto/metaschema/databind/testing/model/ModelTest.java +++ b/databind/src/test/java/gov/nist/secauto/metaschema/databind/testing/model/ModelTest.java @@ -55,8 +55,12 @@ import java.util.List; import java.util.Map; +import javax.xml.namespace.QName; + class ModelTest extends ModelTestBase { + private static final String NS = "https://csrc.nist.gov/ns/test/xml"; + @Nested class TestRootAssemblyWithFlags { @@ -67,9 +71,12 @@ void testRootAssemblyWithFlags() { IBoundDefinitionModelComplex definition = ObjectUtils.requireNonNull( context.getBoundDefinitionForClass(RootAssemblyWithFlags.class)); - IBoundInstanceFlag idFlag = ObjectUtils.requireNonNull(definition.getFlagInstanceByName("id")); - IBoundInstanceFlag defaultFlag = ObjectUtils.requireNonNull(definition.getFlagInstanceByName("defaultFlag")); - IBoundInstanceFlag numberFlag = ObjectUtils.requireNonNull(definition.getFlagInstanceByName("number")); + IBoundInstanceFlag idFlag = ObjectUtils.requireNonNull(definition.getFlagInstanceByName( + new QName("id"))); + IBoundInstanceFlag defaultFlag = ObjectUtils.requireNonNull(definition.getFlagInstanceByName( + new QName("defaultFlag"))); + IBoundInstanceFlag numberFlag = ObjectUtils.requireNonNull(definition.getFlagInstanceByName( + new QName("number"))); assertAll( "root assembly", @@ -152,17 +159,17 @@ void testRootAssemblyWithFields() { (IBoundDefinitionModelAssembly) context.getBoundDefinitionForClass(RootAssemblyWithFields.class)); IBoundInstanceModelField defaultField = ObjectUtils.requireNonNull( - definition.getFieldInstanceByName("defaultField")); + definition.getFieldInstanceByName(new QName(NS, "defaultField"))); IBoundInstanceModelField collectionField = ObjectUtils.requireNonNull( - definition.getFieldInstanceByName("field2")); + definition.getFieldInstanceByName(new QName(NS, "field2"))); IBoundInstanceModelField specifiedValueKeyField = ObjectUtils.requireNonNull( - definition.getFieldInstanceByName("field-value-key")); + definition.getFieldInstanceByName(new QName(NS, "field-value-key"))); IBoundInstanceModelField defaultValueKeyField = ObjectUtils.requireNonNull( - definition.getFieldInstanceByName("field-default-value-key")); + definition.getFieldInstanceByName(new QName(NS, "field-default-value-key"))); IBoundInstanceModelField flagValueKeyField = ObjectUtils.requireNonNull( - definition.getFieldInstanceByName("field-flag-value-key")); + definition.getFieldInstanceByName(new QName(NS, "field-flag-value-key"))); IBoundInstanceModelField flagJsonKeyField = ObjectUtils.requireNonNull( - definition.getFieldInstanceByName("field-json-key")); + definition.getFieldInstanceByName(new QName(NS, "field-json-key"))); assertAll( "root assembly", diff --git a/databind/src/test/java/gov/nist/secauto/metaschema/databind/testing/model/ModelTestBase.java b/databind/src/test/java/gov/nist/secauto/metaschema/databind/testing/model/ModelTestBase.java index b0b63940f..4128e3722 100644 --- a/databind/src/test/java/gov/nist/secauto/metaschema/databind/testing/model/ModelTestBase.java +++ b/databind/src/test/java/gov/nist/secauto/metaschema/databind/testing/model/ModelTestBase.java @@ -84,8 +84,9 @@ public static void assertAssemblyDefinition( assembly.getRootName(), "rootName"), () -> assertEquals( - Optional.ofNullable(ModelUtil.resolveOptionalNamespace(annotation.rootNamespace())) - .orElse(assembly.getContainingModule().getXmlNamespace().toASCIIString()), + ModelUtil.resolveOptionalNamespace( + annotation.rootNamespace(), + () -> assembly.getContainingModule().getXmlNamespace().toASCIIString()), assembly.getRootXmlQName().getNamespaceURI(), "rootNamespace"), () -> assertTrue(true)); @@ -184,8 +185,9 @@ public static void assertFieldInstance( field.getDefaultValue(), "defaultValue"), () -> assertEquals( - Optional.ofNullable(ModelUtil.resolveOptionalNamespace(annotation.namespace())) - .orElse(field.getContainingModule().getXmlNamespace().toASCIIString()), + ModelUtil.resolveOptionalNamespace( + annotation.namespace(), + () -> field.getContainingModule().getXmlNamespace().toASCIIString()), field.getXmlNamespace(), "namespace"), () -> assertEquals( diff --git a/databind/src/test/resources/META-INF/services/org.junit.jupiter.api.extension.Extension-removed b/databind/src/test/resources/META-INF/services/org.junit.jupiter.api.extension.Extension-removed index a65f5a7fa..bc57c64b8 100644 --- a/databind/src/test/resources/META-INF/services/org.junit.jupiter.api.extension.Extension-removed +++ b/databind/src/test/resources/META-INF/services/org.junit.jupiter.api.extension.Extension-removed @@ -1 +1 @@ -gov.nist.secauto.metaschema.databind.test.Log4jTestWatcher +gov.nist.secauto.metaschema.databind.test.Log4jTestWatcher diff --git a/metaschema-cli/src/main/java/gov/nist/secauto/metaschema/cli/commands/AbstractValidateContentCommand.java b/metaschema-cli/src/main/java/gov/nist/secauto/metaschema/cli/commands/AbstractValidateContentCommand.java index 170e72d6e..20ba5fce4 100644 --- a/metaschema-cli/src/main/java/gov/nist/secauto/metaschema/cli/commands/AbstractValidateContentCommand.java +++ b/metaschema-cli/src/main/java/gov/nist/secauto/metaschema/cli/commands/AbstractValidateContentCommand.java @@ -250,8 +250,8 @@ public ExitStatus execute() { return ExitCode.PROCESSING_ERROR.exit().withThrowable(ex); } - if (LOGGER.isInfoEnabled()) { - LOGGER.info("Validation identified the following in file '{}'.", source); + if (LOGGER.isInfoEnabled() && !validationResult.isPassing()) { + LOGGER.info("Validation identified the following issues:", source); } LoggingValidationHandler.instance().handleValidationResults(validationResult); diff --git a/metaschema-cli/src/main/java/gov/nist/secauto/metaschema/cli/util/LoggingValidationHandler.java b/metaschema-cli/src/main/java/gov/nist/secauto/metaschema/cli/util/LoggingValidationHandler.java index 5c6e094e5..75a334f92 100644 --- a/metaschema-cli/src/main/java/gov/nist/secauto/metaschema/cli/util/LoggingValidationHandler.java +++ b/metaschema-cli/src/main/java/gov/nist/secauto/metaschema/cli/util/LoggingValidationHandler.java @@ -45,6 +45,7 @@ import java.util.List; import edu.umd.cs.findbugs.annotations.NonNull; +import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; public final class LoggingValidationHandler { private static final Logger LOGGER = LogManager.getLogger(LoggingValidationHandler.class); @@ -58,11 +59,13 @@ public static LoggingValidationHandler instance() { return instance(false); } + @SuppressFBWarnings(value = "SING_SINGLETON_GETTER_NOT_SYNCHRONIZED", + justification = "both values are class initialized") public static LoggingValidationHandler instance(boolean logExceptions) { return logExceptions ? LOG_EXCPTION_INSTANCE : NO_LOG_EXCPTION_INSTANCE; } - protected LoggingValidationHandler(boolean logExceptions) { + private LoggingValidationHandler(boolean logExceptions) { this.logExceptions = logExceptions; } diff --git a/metaschema-documentation-generator/xml-outline.html b/metaschema-documentation-generator/xml-outline.html index b1bd1b1c8..8b54880cf 100644 --- a/metaschema-documentation-generator/xml-outline.html +++ b/metaschema-documentation-generator/xml-outline.html @@ -1,51 +1,51 @@ -
-
-
- - <assessment-plan uuid="uuid"></assessment-plan> [1] - -
-
-
-
- - <assessment-plan uuid="uuid"></assessment-plan> [1] - -
-
-
-
- - <assessment-plan uuid="uuid"></assessment-plan> [1] - -
-
-
-
- - <assessment-plan uuid="uuid"></assessment-plan> [1] - -
-
-
-
- - <assessment-plan uuid="uuid"></assessment-plan> [1] - -
-
-
-
- - <assessment-plan uuid="uuid"></assessment-plan> [1] - -
-
-
-
- - <assessment-plan uuid="uuid"></assessment-plan> [1] - -
-
+
+
+
+ + <assessment-plan uuid="uuid"></assessment-plan> [1] + +
+
+
+
+ + <assessment-plan uuid="uuid"></assessment-plan> [1] + +
+
+
+
+ + <assessment-plan uuid="uuid"></assessment-plan> [1] + +
+
+
+
+ + <assessment-plan uuid="uuid"></assessment-plan> [1] + +
+
+
+
+ + <assessment-plan uuid="uuid"></assessment-plan> [1] + +
+
+
+
+ + <assessment-plan uuid="uuid"></assessment-plan> [1] + +
+
+
+
+ + <assessment-plan uuid="uuid"></assessment-plan> [1] + +
+
\ No newline at end of file diff --git a/metaschema-maven-plugin/src/it/generate-sources/verify.groovy b/metaschema-maven-plugin/src/it/generate-sources/verify.groovy index fae28bc02..579e8d141 100644 --- a/metaschema-maven-plugin/src/it/generate-sources/verify.groovy +++ b/metaschema-maven-plugin/src/it/generate-sources/verify.groovy @@ -1,2 +1,2 @@ -File genSources = new File(basedir, 'target/generated-sources/metaschema/'); +File genSources = new File(basedir, 'target/generated-sources/metaschema/'); assert genSources.exists(), "Didn't find generated sources [" + genSources.getAbsolutePath() + "]"; \ No newline at end of file diff --git a/pom.xml b/pom.xml index ef9c7d849..7f18f5a40 100644 --- a/pom.xml +++ b/pom.xml @@ -156,8 +156,8 @@ 2.12.1 3.6.0 3.1.0 + 4.8.4.0 3.0.0 - + gov/nist/secauto/metaschema/core/model/xml/xmlbeans/**/* org/apache/xmlbeans/**/* + gov/nist/secauto/metaschema/core/metapath/antlr/* diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/AbstractDataTypeProvider.java b/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/AbstractDataTypeProvider.java index 256bb755a..9fac25c1a 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/AbstractDataTypeProvider.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/AbstractDataTypeProvider.java @@ -61,7 +61,7 @@ public List> getJavaTypeAdapters() { * if another type adapter has no name */ protected void registerDatatype(@NonNull IDataTypeAdapter adapter) { - if (adapter.getNames().size() == 0) { + if (adapter.getNames().isEmpty()) { throw new IllegalArgumentException("The adapter has no name: " + adapter.getClass().getName()); } synchronized (this) { diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/markup/flexmark/HtmlQuoteTagExtension.java b/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/markup/flexmark/HtmlQuoteTagExtension.java index e5cc2434a..894eb506f 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/markup/flexmark/HtmlQuoteTagExtension.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/markup/flexmark/HtmlQuoteTagExtension.java @@ -172,6 +172,7 @@ public HtmlNodeRenderer apply(DataHolder options) { public static class DoubleQuoteNode extends TypographicQuotes { + @SuppressWarnings("PMD.ConstructorCallsOverridableMethod") public DoubleQuoteNode(TypographicQuotes node) { super(node.getOpeningMarker(), node.getText(), node.getClosingMarker()); setTypographicOpening(node.getTypographicOpening()); diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/ISequence.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/ISequence.java index 569f3ccac..229b65f6e 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/ISequence.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/ISequence.java @@ -240,7 +240,7 @@ public Supplier> supplier() { @Override public BiConsumer, ITEM_TYPE> accumulator() { - return (list, value) -> list.add(value); + return List::add; } @Override @@ -280,7 +280,7 @@ static ISequence map( @NonNull Function mapFunction, @NonNull ISequence seq) { return seq.safeStream() - .map(item -> mapFunction.apply(item)) + .map(mapFunction::apply) .collect(toSequence()); } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/BuildCSTVisitor.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/BuildCSTVisitor.java index 3cd8b46f8..471d2acd1 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/BuildCSTVisitor.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/BuildCSTVisitor.java @@ -138,8 +138,9 @@ public BuildCSTVisitor(@NonNull StaticContext context) { this.context = context; } - /* ============================================================ - * Expressions - https://www.w3.org/TR/xpath-31/#id-expressions + /* + * ============================================================ Expressions - + * https://www.w3.org/TR/xpath-31/#id-expressions * ============================================================ */ @NonNull @@ -155,8 +156,9 @@ protected IExpression handleExpr(ExprContext ctx) { }); } - /* ================================================================= - * Literal Expressions - https://www.w3.org/TR/xpath-31/#id-literals + /* + * ================================================================= Literal + * Expressions - https://www.w3.org/TR/xpath-31/#id-literals * ================================================================= */ @@ -185,8 +187,9 @@ protected IExpression handleNumericLiteral(NumericliteralContext ctx) { return retval; } - /* ================================================================== - * Variable References - https://www.w3.org/TR/xpath-31/#id-variables + /* + * ================================================================== Variable + * References - https://www.w3.org/TR/xpath-31/#id-variables * ================================================================== */ @@ -198,8 +201,9 @@ protected IExpression handleVarref(VarrefContext ctx) { getContext().getVariablePrefixResolver())); } - /* ==================================================================== - * For Expressions - https://www.w3.org/TR/xpath-31/#id-for-expressions + /* + * ==================================================================== For + * Expressions - https://www.w3.org/TR/xpath-31/#id-for-expressions * ==================================================================== */ @@ -234,8 +238,9 @@ protected IExpression handleForexpr(ForexprContext ctx) { return retval; } - /* ==================================================================== - * Let Expressions - https://www.w3.org/TR/xpath-31/#id-let-expressions + /* + * ==================================================================== Let + * Expressions - https://www.w3.org/TR/xpath-31/#id-let-expressions * ==================================================================== */ @@ -262,9 +267,12 @@ protected IExpression handleLet(LetexprContext context) { return retval; } - /* ================================================================================== - * Quantified Expressions - https://www.w3.org/TR/xpath-31/#id-quantified-expressions - * ================================================================================== + /* + * ============================================================================= + * ===== Quantified Expressions - + * https://www.w3.org/TR/xpath-31/#id-quantified-expressions + * ============================================================================= + * ===== */ @Override @@ -302,8 +310,9 @@ protected IExpression handleQuantifiedexpr(QuantifiedexprContext ctx) { return new Quantified(quantifier, vars, satisfies); } - /* ======================================================================= - * Arrow operator (=>) - https://www.w3.org/TR/xpath-31/#id-arrow-operator + /* + * ======================================================================= Arrow + * operator (=>) - https://www.w3.org/TR/xpath-31/#id-arrow-operator * ======================================================================= */ @@ -333,9 +342,12 @@ protected IExpression handleArrowexpr(ArrowexprContext context) { }); } - /* ================================================================================= - * Parenthesized Expressions - https://www.w3.org/TR/xpath-31/#id-paren-expressions - * ================================================================================= + /* + * ============================================================================= + * ==== Parenthesized Expressions - + * https://www.w3.org/TR/xpath-31/#id-paren-expressions + * ============================================================================= + * ==== */ @Override @@ -343,9 +355,12 @@ protected IExpression handleEmptyParenthesizedexpr(ParenthesizedexprContext ctx) return EmptySequence.instance(); } - /* ===================================================================================== - * Context Item Expression - https://www.w3.org/TR/xpath-31/#id-context-item-expression - * ===================================================================================== + /* + * ============================================================================= + * ======== Context Item Expression - + * https://www.w3.org/TR/xpath-31/#id-context-item-expression + * ============================================================================= + * ======== */ @Override @@ -353,7 +368,8 @@ protected IExpression handleContextitemexpr(ContextitemexprContext ctx) { return ContextItem.instance(); } - /* ========================================================================= + /* + * ========================================================================= * Static Function Calls - https://www.w3.org/TR/xpath-31/#id-function-calls * ========================================================================= */ @@ -396,7 +412,8 @@ protected IExpression handleFunctioncall(FunctioncallContext ctx) { .collect(Collectors.toUnmodifiableList()))); } - /* ========================================================================= + /* + * ========================================================================= * Filter Expressions - https://www.w3.org/TR/xpath-31/#id-filter-expression * ========================================================================= */ @@ -461,8 +478,9 @@ protected IExpression handlePostfixexpr(PostfixexprContext ctx) { } return retval; } - /* ====================================================================== - * Path Expressions - https://www.w3.org/TR/xpath-31/#id-path-expressions + /* + * ====================================================================== Path + * Expressions - https://www.w3.org/TR/xpath-31/#id-path-expressions * ====================================================================== */ @@ -498,9 +516,12 @@ protected IExpression handlePathexpr(PathexprContext ctx) { return retval; } - /* ======================================================================================= - * RelativePath Expressions - https://www.w3.org/TR/xpath-31/#id-relative-path-expressions - * ======================================================================================= + /* + * ============================================================================= + * ========== RelativePath Expressions - + * https://www.w3.org/TR/xpath-31/#id-relative-path-expressions + * ============================================================================= + * ========== */ @Override @@ -528,8 +549,9 @@ protected IExpression handleRelativepathexpr(RelativepathexprContext context) { }); } - /* ================================================ - * Steps - https://www.w3.org/TR/xpath-31/#id-steps + /* + * ================================================ Steps - + * https://www.w3.org/TR/xpath-31/#id-steps * ================================================ */ @@ -594,13 +616,15 @@ protected IExpression handleReversestep(ReversestepContext ctx) { return new Step(axis, parseNodeTest(ctx.nodetest(), false)); } - /* ======================================================= - * Node Tests - https://www.w3.org/TR/xpath-31/#node-tests + /* + * ======================================================= Node Tests - + * https://www.w3.org/TR/xpath-31/#node-tests * ======================================================= */ - /* ======================================================= - * Node Tests - https://www.w3.org/TR/xpath-31/#node-tests + /* + * ======================================================= Node Tests - + * https://www.w3.org/TR/xpath-31/#node-tests * ======================================================= */ @@ -627,9 +651,8 @@ protected INameTestExpression parseNameTest(NametestContext ctx, boolean flag) { @Override protected Wildcard handleWildcard(WildcardContext ctx) { Predicate> matcher = null; - TerminalNode node; - if ((node = ctx.STAR()) == null) { - if ((node = ctx.CS()) != null) { + if (ctx.STAR() == null) { + if (ctx.CS() != null) { // specified prefix, any local-name String prefix = ctx.NCName().getText(); String namespace = getContext().lookupNamespaceForPrefix(prefix); @@ -637,7 +660,7 @@ protected Wildcard handleWildcard(WildcardContext ctx) { throw new IllegalStateException(String.format("Prefix '%s' did not map to a namespace.", prefix)); } matcher = new Wildcard.MatchAnyLocalName(namespace); - } else if ((node = ctx.SC()) != null) { + } else if (ctx.SC() != null) { // any prefix, specified local-name matcher = new Wildcard.MatchAnyNamespace(ctx.NCName().getText()); } else { @@ -651,7 +674,8 @@ protected Wildcard handleWildcard(WildcardContext ctx) { return new Wildcard(matcher); } - /* ====================================================================== + /* + * ====================================================================== * Predicates within Steps - https://www.w3.org/TR/xpath-31/#id-predicate * ====================================================================== */ @@ -667,8 +691,9 @@ protected IExpression handleAxisstep(AxisstepContext ctx) { return predicates.isEmpty() ? step : new PredicateExpression(step, predicates); } - /* =========================================================== - * Abbreviated Syntax - https://www.w3.org/TR/xpath-31/#abbrev + /* + * =========================================================== Abbreviated + * Syntax - https://www.w3.org/TR/xpath-31/#abbrev * =========================================================== */ @@ -691,7 +716,8 @@ protected IExpression handleAbbrevreversestep(AbbrevreversestepContext ctx) { return Axis.PARENT; } - /* ====================================================================== + /* + * ====================================================================== * Constructing Sequences - https://www.w3.org/TR/xpath-31/#construct_seq * ====================================================================== */ @@ -706,7 +732,8 @@ protected IExpression handleRangeexpr(RangeexprContext ctx) { return new Range(left, right); } - /* ======================================================================== + /* + * ======================================================================== * Combining Node Sequences - https://www.w3.org/TR/xpath-31/#combining_seq * ======================================================================== */ @@ -744,7 +771,8 @@ protected IExpression handleIntersectexceptexpr(IntersectexceptexprContext conte }); } - /* ====================================================================== + /* + * ====================================================================== * Arithmetic Expressions - https://www.w3.org/TR/xpath-31/#id-arithmetic * ====================================================================== */ @@ -835,9 +863,12 @@ protected IExpression handleUnaryexpr(UnaryexprContext ctx) { return retval; } - /* ======================================================================================== - * String Concatenation Expressions - https://www.w3.org/TR/xpath-31/#id-string-concat-expr - * ======================================================================================== + /* + * ============================================================================= + * =========== String Concatenation Expressions - + * https://www.w3.org/TR/xpath-31/#id-string-concat-expr + * ============================================================================= + * =========== */ @Override @@ -848,7 +879,8 @@ protected IExpression handleStringconcatexpr(StringconcatexprContext ctx) { }); } - /* ======================================================================= + /* + * ======================================================================= * Comparison Expressions - https://www.w3.org/TR/xpath-31/#id-comparisons * ======================================================================= */ @@ -924,7 +956,8 @@ protected IExpression handleComparisonexpr(ComparisonexprContext ctx) { // NOPMD return retval; } - /* ============================================================================ + /* + * ============================================================================ * Logical Expressions - https://www.w3.org/TR/xpath-31/#id-logical-expressions * ============================================================================ */ @@ -945,7 +978,8 @@ protected IExpression handleAndexpr(AndexprContext ctx) { }); } - /* ========================================================================= + /* + * ========================================================================= * Conditional Expressions - https://www.w3.org/TR/xpath-31/#id-conditionals * ========================================================================= */ @@ -959,7 +993,8 @@ protected IExpression handleIfexpr(IfexprContext ctx) { return new If(testExpr, thenExpr, elseExpr); } - /* ========================================================================= + /* + * ========================================================================= * Simple map operator (!) - https://www.w3.org/TR/xpath-31/#id-map-operator * ========================================================================= */ diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/ExpressionUtils.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/ExpressionUtils.java index 5fc6ec69f..d900ed4ae 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/ExpressionUtils.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/ExpressionUtils.java @@ -61,7 +61,7 @@ public static Class analyzeStaticResultType retval = baseType; } else { List> expressionClasses = ObjectUtils.notNull(expressions.stream() - .map(expr -> expr.getStaticResultType()).collect(Collectors.toList())); + .map(IExpression::getStaticResultType).collect(Collectors.toList())); // check if the expression classes, are derived from the base type if (checkDerivedFrom(baseType, expressionClasses)) { diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/PredicateExpression.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/PredicateExpression.java index b51633ba2..8e33e838b 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/PredicateExpression.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/PredicateExpression.java @@ -36,6 +36,7 @@ import java.math.BigInteger; import java.util.List; import java.util.Map; +import java.util.Map.Entry; import java.util.concurrent.atomic.AtomicInteger; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -125,7 +126,7 @@ public List getChildren() { } return bool; }).anyMatch(x -> !x); - }).map(entry -> entry.getValue())); + }).map(Entry::getValue)); retval = ISequence.of(stream); } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/path/AbstractPathExpression.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/path/AbstractPathExpression.java index 410e900f0..a381d9419 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/path/AbstractPathExpression.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/path/AbstractPathExpression.java @@ -76,7 +76,7 @@ protected Stream searchExpression( = (Stream) expression.accept(dynamicContext, outerFocus).asStream(); Stream childMatches = outerFocus.asStream() - .map(item -> ItemUtils.checkItemIsNodeItemForStep(item)) + .map(ItemUtils::checkItemIsNodeItemForStep) .flatMap(focusedNode -> { Stream matches; diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/path/Axis.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/path/Axis.java index 1895941a9..8965ee515 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/path/Axis.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/path/Axis.java @@ -43,7 +43,7 @@ @SuppressWarnings("PMD.ShortClassName") // intentional public enum Axis implements IExpression { - SELF(focus -> Stream.of(focus)), + SELF(Stream::of), PARENT(focus -> Stream.ofNullable(focus.getParentNodeItem())), ANCESTOR(INodeItem::ancestor), ANCESTOR_OR_SELF(INodeItem::ancestorOrSelf), @@ -99,7 +99,7 @@ public ISequence accept( retval = ISequence.empty(); } else { retval = ISequence.of(outerFocus.asStream() - .map(item -> ItemUtils.checkItemIsNodeItemForStep(item)) + .map(ItemUtils::checkItemIsNodeItemForStep) .flatMap(item -> { assert item != null; return execute(item); diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/path/Flag.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/path/Flag.java index 8772aaa78..9dd6b5fea 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/path/Flag.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/path/Flag.java @@ -70,7 +70,7 @@ public ISequence accept( DynamicContext dynamicContext, ISequence focus) { return ISequence.of(focus.asStream() - .map(item -> ItemUtils.checkItemIsNodeItemForStep(item)) + .map(ItemUtils::checkItemIsNodeItemForStep) .flatMap(item -> { assert item != null; return match(item); diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/path/ModelInstance.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/path/ModelInstance.java index 4ae747583..4b17abc81 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/path/ModelInstance.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/path/ModelInstance.java @@ -72,7 +72,7 @@ public RESULT accept(IExpressionVisitor visit DynamicContext dynamicContext, ISequence focus) { return ISequence.of(focus.asStream() - .map(item -> ItemUtils.checkItemIsNodeItemForStep(item)) + .map(ItemUtils::checkItemIsNodeItemForStep) .flatMap(item -> { assert item != null; return match(item); diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/path/NameTest.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/path/NameTest.java index 8b0d4f5c7..d23d39582 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/path/NameTest.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/path/NameTest.java @@ -78,7 +78,7 @@ public ISequence accept( DynamicContext dynamicContext, ISequence focus) { return ISequence.of(focus.asStream() - .map(item -> ItemUtils.checkItemIsNodeItemForStep(item)) + .map(ItemUtils::checkItemIsNodeItemForStep) .filter(this::match)); } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/path/RootSlashOnlyPath.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/path/RootSlashOnlyPath.java index c5e5902ea..a83c31a4b 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/path/RootSlashOnlyPath.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/path/RootSlashOnlyPath.java @@ -61,7 +61,7 @@ public ISequence accept( ISequence focus) { return ObjectUtils.notNull(focus.asStream() - .map(item -> ItemUtils.checkItemIsNodeItemForStep(item)) + .map(ItemUtils::checkItemIsNodeItemForStep) .map(item -> Axis.ANCESTOR_OR_SELF.execute(ObjectUtils.notNull(item)).findFirst().get()) .collect(ISequence.toSequence())); } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/path/RootSlashPath.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/path/RootSlashPath.java index c33b16189..df4a9a98d 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/path/RootSlashPath.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/path/RootSlashPath.java @@ -60,7 +60,7 @@ public ISequence accept( ISequence focus) { ISequence roots = ObjectUtils.notNull(focus.asStream() - .map(item -> ItemUtils.checkItemIsNodeItemForStep(item)) + .map(ItemUtils::checkItemIsNodeItemForStep) .map(item -> Axis.ANCESTOR_OR_SELF.execute(ObjectUtils.notNull(item)).findFirst().get()) .collect(ISequence.toSequence())); diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/path/Wildcard.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/path/Wildcard.java index 3ac26f514..12174dfa8 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/path/Wildcard.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/path/Wildcard.java @@ -60,8 +60,7 @@ public RESULT accept(IExpressionVisitor visit @Override public ISequence accept( DynamicContext dynamicContext, ISequence focus) { - Stream nodes = focus.asStream() - .map(item -> ItemUtils.checkItemIsNodeItemForStep(item)); + Stream nodes = focus.asStream().map(ItemUtils::checkItemIsNodeItemForStep); if (matcher != null) { Predicate> test = matcher; nodes = nodes.filter(item -> { @@ -75,7 +74,7 @@ public ISequence accept( public static class MatchAnyNamespace implements Predicate> { @NonNull - private String localName; + private final String localName; public MatchAnyNamespace(@NonNull String localName) { this.localName = localName; @@ -89,7 +88,7 @@ public boolean test(IDefinitionNodeItem item) { public static class MatchAnyLocalName implements Predicate> { @NonNull - private String namespace; + private final String namespace; public MatchAnyLocalName(@NonNull String namespace) { this.namespace = namespace; diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/DefaultFunction.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/DefaultFunction.java index 8907d8d66..2540ce4e5 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/DefaultFunction.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/DefaultFunction.java @@ -394,7 +394,7 @@ public String toSignature() { if (arguments.isEmpty()) { builder.append("()"); } else { - builder.append(arguments.stream().map(argument -> argument.toSignature()).collect(Collectors.joining(","))); + builder.append(arguments.stream().map(IArgument::toSignature).collect(Collectors.joining(","))); if (isArityUnbounded()) { builder.append(", ..."); diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/FunctionLibrary.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/FunctionLibrary.java index 739059485..a568c8451 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/FunctionLibrary.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/FunctionLibrary.java @@ -53,7 +53,7 @@ public class FunctionLibrary implements IFunctionLibrary { * if the provided function has the same arity as a previously * registered function with the same name */ - public void registerFunction(@NonNull IFunction function) { + public final void registerFunction(@NonNull IFunction function) { registerFunctionByQName(function); registerFunctionByName(function); } @@ -91,10 +91,7 @@ private void registerFunctionByName(@NonNull IFunction function) { @Override public Stream getFunctionsAsStream() { synchronized (this) { - return ObjectUtils.notNull( - libraryByQName.values().stream().flatMap(set -> { - return set.getFunctionsAsStream(); - })); + return ObjectUtils.notNull(libraryByQName.values().stream().flatMap(NamedFunctionSet::getFunctionsAsStream)); } } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/FunctionService.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/FunctionService.java index 07c94382e..22b456138 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/FunctionService.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/FunctionService.java @@ -74,9 +74,7 @@ public IFunctionLibrary load() { FunctionLibrary functionLibrary = new FunctionLibrary(); loader.stream() .map(Provider::get) - .flatMap(library -> { - return library.getFunctionsAsStream(); - }) + .flatMap(IFunctionLibrary::getFunctionsAsStream) .forEachOrdered(function -> functionLibrary.registerFunction(ObjectUtils.notNull(function))); synchronized (this) { diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/FunctionUtils.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/FunctionUtils.java index 3a9790afe..567f95257 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/FunctionUtils.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/FunctionUtils.java @@ -373,7 +373,7 @@ public static TYPE requireTypeOrNull(Class clazz, @Nu */ @NonNull public static Stream> getTypes(@NonNull Stream items) { - return ObjectUtils.notNull(items.map(item -> item.getClass())); + return ObjectUtils.notNull(items.map(Object::getClass)); } /** diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/FnAvg.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/FnAvg.java index 2f829a3c5..07fb5b0a0 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/FnAvg.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/FnAvg.java @@ -163,7 +163,7 @@ public static IAnyAtomicItem average(@NonNull Collection type.getName()) + .map(Class::getName) .collect(CustomCollectors.joiningWithOxfordComma(",")))); } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/FnMinMax.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/FnMinMax.java index 0035a917e..12c6043a2 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/FnMinMax.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/FnMinMax.java @@ -179,7 +179,7 @@ private static Stream normalize( int size = resultingItems.size(); if (counts.getOrDefault(IStringItem.class, 0) + counts.getOrDefault(IAnyUriItem.class, 0) == size) { stream = resultingItems.stream() - .map(item -> item.asStringItem()); + .map(IAnyAtomicItem::asStringItem); } else if (counts.getOrDefault(IDecimalItem.class, 0) == size) { stream = resultingItems.stream() .map(item -> (IDecimalItem) item); @@ -191,7 +191,7 @@ private static Stream normalize( InvalidArgumentFunctionException.INVALID_ARGUMENT_TYPE, String.format("Values must all be of a single atomic type. Their types are '%s'.", FunctionUtils.getTypes(resultingItems).stream() - .map(clazz -> clazz.getName()) + .map(Class::getName) .collect(Collectors.joining(",")))); } return stream; diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/FnSum.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/FnSum.java index 93c418e83..538969662 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/FnSum.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/FnSum.java @@ -208,7 +208,7 @@ public static IAnyAtomicItem sum( InvalidArgumentFunctionException.INVALID_ARGUMENT_TYPE, String.format("Values must all be of type '%s'.", OperationFunctions.AGGREGATE_MATH_TYPES.stream() - .map(type -> type.getName()) + .map(Class::getName) .collect(CustomCollectors.joiningWithOxfordComma(",")))); } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/item/node/FlagInstanceNodeItemImpl.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/item/node/FlagInstanceNodeItemImpl.java index bc251175b..2dcae87ce 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/item/node/FlagInstanceNodeItemImpl.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/item/node/FlagInstanceNodeItemImpl.java @@ -56,7 +56,7 @@ public FlagInstanceNodeItemImpl( @NonNull Object value) { super(instance, parent); this.value = value; - this.atomicItem = ObjectUtils.notNull(Lazy.lazy(() -> newAtomicItem())); + this.atomicItem = ObjectUtils.notNull(Lazy.lazy(this::newAtomicItem)); } @Override diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/item/node/IFeatureFlagContainerItem.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/item/node/IFeatureFlagContainerItem.java index 324ade525..a3e1de7a1 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/item/node/IFeatureFlagContainerItem.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/item/node/IFeatureFlagContainerItem.java @@ -26,7 +26,7 @@ package gov.nist.secauto.metaschema.core.metapath.item.node; -import gov.nist.secauto.metaschema.core.model.IContainerFlag; +import gov.nist.secauto.metaschema.core.model.IModelDefinition; import gov.nist.secauto.metaschema.core.util.CollectionUtil; import java.util.Collection; @@ -40,7 +40,8 @@ /** * This mixin interface indicates that the implementation is a {@link INodeItem} - * that is based on an {@link IContainerFlag}. This means it has flag children. + * that is based on an {@link IModelDefinition}. This means it has flag + * children. *

* If an implementation may have flag and model children, or model children * only, then the {@link IFeatureModelContainerItem} should be used instead. diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/item/node/INodeItem.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/item/node/INodeItem.java index a7811e867..e22c4aa4d 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/item/node/INodeItem.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/item/node/INodeItem.java @@ -298,6 +298,6 @@ default Stream flags() { @SuppressWarnings("null") @NonNull default Stream> modelItems() { - return getModelItems().stream().flatMap(list -> list.stream()); + return getModelItems().stream().flatMap(Collection::stream); } } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/AbstractAssemblyInstance.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/AbstractAssemblyInstance.java new file mode 100644 index 000000000..640761284 --- /dev/null +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/AbstractAssemblyInstance.java @@ -0,0 +1,69 @@ +/* + * Portions of this software was developed by employees of the National Institute + * of Standards and Technology (NIST), an agency of the Federal Government and is + * being made available as a public service. Pursuant to title 17 United States + * Code Section 105, works of NIST employees are not subject to copyright + * protection in the United States. This software may be subject to foreign + * copyright. Permission in the United States and in foreign countries, to the + * extent that NIST may hold copyright, to use, copy, modify, create derivative + * works, and distribute this software and its documentation without fee is hereby + * granted on a non-exclusive basis, provided that this notice and disclaimer + * of warranty appears in all copies. + * + * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER + * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY + * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM + * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE + * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT + * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, + * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, + * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, + * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR + * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT + * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. + */ + +package gov.nist.secauto.metaschema.core.model; + +import gov.nist.secauto.metaschema.core.util.ObjectUtils; + +import edu.umd.cs.findbugs.annotations.NonNull; + +public abstract class AbstractAssemblyInstance< + PARENT extends IContainerModel, + DEFINITION extends IAssemblyDefinition, + INSTANCE extends IAssemblyInstance, + PARENT_DEFINITION extends IAssemblyDefinition> + extends AbstractNamedModelInstance + implements IAssemblyInstance, IFeatureDefinitionReferenceInstance { + + protected AbstractAssemblyInstance(@NonNull PARENT parent) { + super(parent); + } + + @Override + public DEFINITION getDefinition() { + // this should always be not null + return ObjectUtils.asType(ObjectUtils.requireNonNull( + getContainingModule() + .getScopedAssemblyDefinitionByName(getReferencedDefinitionQName()))); + } + + /** + * Generates a "coordinate" string for the assembly instance. + * + * @return the coordinate + */ + @SuppressWarnings("null") + @Override + public String toCoordinates() { + IDefinition definition = getDefinition(); + return String.format("assembly instance %s -> %s in module %s (@%d(%d)", + getXmlQName(), + definition.getDefinitionQName(), + getContainingDefinition().getContainingModule().getShortName(), + hashCode(), + definition.hashCode()); + } +} diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/AbstractNamedModelInstanceGrouped.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/AbstractChoiceGroupInstance.java similarity index 78% rename from core/src/main/java/gov/nist/secauto/metaschema/core/model/AbstractNamedModelInstanceGrouped.java rename to core/src/main/java/gov/nist/secauto/metaschema/core/model/AbstractChoiceGroupInstance.java index bc526b861..fc91042b0 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/AbstractNamedModelInstanceGrouped.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/AbstractChoiceGroupInstance.java @@ -28,22 +28,15 @@ import edu.umd.cs.findbugs.annotations.NonNull; -public abstract class AbstractNamedModelInstanceGrouped implements INamedModelInstanceGrouped { - @NonNull - private final IChoiceGroupInstance parent; +public abstract class AbstractChoiceGroupInstance< + PARENT extends IAssemblyDefinition, + NAMED_MODEL extends INamedModelInstanceGrouped, + FIELD extends IFieldInstanceGrouped, + ASSEMBLY extends IAssemblyInstanceGrouped> + extends AbstractInstance + implements IChoiceGroupInstance, IFeatureContainerModelGrouped { - /** - * Construct a new named model instance that is part of a choice group. - * - * @param parent - * the containing choice group - */ - protected AbstractNamedModelInstanceGrouped(@NonNull IChoiceGroupInstance parent) { - this.parent = parent; - } - - @Override - public IChoiceGroupInstance getParentContainer() { - return parent; + protected AbstractChoiceGroupInstance(@NonNull PARENT parent) { + super(parent); } } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/AbstractChoiceInstance.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/AbstractChoiceInstance.java new file mode 100644 index 000000000..4d3eaa901 --- /dev/null +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/AbstractChoiceInstance.java @@ -0,0 +1,49 @@ +/* + * Portions of this software was developed by employees of the National Institute + * of Standards and Technology (NIST), an agency of the Federal Government and is + * being made available as a public service. Pursuant to title 17 United States + * Code Section 105, works of NIST employees are not subject to copyright + * protection in the United States. This software may be subject to foreign + * copyright. Permission in the United States and in foreign countries, to the + * extent that NIST may hold copyright, to use, copy, modify, create derivative + * works, and distribute this software and its documentation without fee is hereby + * granted on a non-exclusive basis, provided that this notice and disclaimer + * of warranty appears in all copies. + * + * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER + * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY + * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM + * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE + * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT + * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, + * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, + * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, + * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR + * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT + * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. + */ + +package gov.nist.secauto.metaschema.core.model; + +import edu.umd.cs.findbugs.annotations.NonNull; + +public abstract class AbstractChoiceInstance< + PARENT extends IAssemblyDefinition, + MODEL extends IModelInstanceAbsolute, + NAMED_MODEL extends INamedModelInstanceAbsolute, + FIELD extends IFieldInstanceAbsolute, + ASSEMBLY extends IAssemblyInstanceAbsolute> + extends AbstractInstance + implements IChoiceInstance, IFeatureContainerModelAbsolute { + + protected AbstractChoiceInstance(@NonNull PARENT parent) { + super(parent); + } + + @Override + public String getGroupAsName() { + // a choice does not have a groups-as name + return null; + } +} diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/IContainerFlag.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/AbstractDefinition.java similarity index 68% rename from core/src/main/java/gov/nist/secauto/metaschema/core/model/IContainerFlag.java rename to core/src/main/java/gov/nist/secauto/metaschema/core/model/AbstractDefinition.java index 599a2f122..4d974917f 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/IContainerFlag.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/AbstractDefinition.java @@ -26,42 +26,40 @@ package gov.nist.secauto.metaschema.core.model; -import java.util.Collection; +import gov.nist.secauto.metaschema.core.util.ObjectUtils; import javax.xml.namespace.QName; import edu.umd.cs.findbugs.annotations.NonNull; -import edu.umd.cs.findbugs.annotations.Nullable; +import nl.talsmasoftware.lazy4j.Lazy; -/** - * This marker interface identifies a type of definition that contains flags, - * either a field or assembly. - * - */ -public interface IContainerFlag extends IContainer { +public abstract class AbstractDefinition + implements IDefinition { + @NonNull + private final Lazy qname; + @NonNull + private final Lazy definitionQName; + + protected AbstractDefinition(@NonNull NameInitializer initializer) { + this.qname = ObjectUtils.notNull(Lazy.lazy(() -> initializer.apply(getEffectiveName()))); + this.definitionQName = ObjectUtils.notNull(Lazy.lazy(() -> initializer.apply(getName()))); + } + + @SuppressWarnings("null") @Override - default boolean hasChildren() { - return !getFlagInstances().isEmpty(); + public final QName getXmlQName() { + return qname.get(); } - /** - * Retrieves a flag instance, by the flag's effective name, that is defined on - * the containing definition. - * - * @param name - * the flag's name - * @return the matching flag instance, or {@code null} if there is no flag - * matching the specified name - */ - @Nullable - IFlagInstance getFlagInstanceByName(@NonNull QName name); + @SuppressWarnings("null") + @Override + public final QName getDefinitionQName() { + return definitionQName.get(); + } - /** - * Retrieves the flag instances for all flags defined on the containing - * definition. - * - * @return the flags - */ - @NonNull - Collection getFlagInstances(); + @FunctionalInterface + public interface NameInitializer { + @NonNull + QName apply(@NonNull String name); + } } diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/IFeatureBoundContainerFlag.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/AbstractFieldInstance.java similarity index 60% rename from databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/IFeatureBoundContainerFlag.java rename to core/src/main/java/gov/nist/secauto/metaschema/core/model/AbstractFieldInstance.java index 189a0e381..630a336e4 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/IFeatureBoundContainerFlag.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/AbstractFieldInstance.java @@ -24,41 +24,46 @@ * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. */ -package gov.nist.secauto.metaschema.databind.model.impl; +package gov.nist.secauto.metaschema.core.model; -import java.util.Collection; - -import javax.xml.namespace.QName; +import gov.nist.secauto.metaschema.core.util.ObjectUtils; import edu.umd.cs.findbugs.annotations.NonNull; -import edu.umd.cs.findbugs.annotations.Nullable; -import gov.nist.secauto.metaschema.core.model.IContainerFlagSupport; -import gov.nist.secauto.metaschema.core.model.IFeatureContainerFlag; -import gov.nist.secauto.metaschema.core.util.ObjectUtils; -import gov.nist.secauto.metaschema.databind.model.IBoundContainerFlag; -import gov.nist.secauto.metaschema.databind.model.IBoundInstanceFlag; -/** - * Provides default implementations based on a supporting glag container - * implementation. - */ -public interface IFeatureBoundContainerFlag - extends IBoundContainerFlag, - IFeatureContainerFlag { +public abstract class AbstractFieldInstance< + PARENT extends IContainerModel, + DEFINITION extends IFieldDefinition, + INSTANCE extends IFieldInstance, + PARENT_DEFINITION extends IAssemblyDefinition> + extends AbstractNamedModelInstance + implements IFieldInstance, IFeatureDefinitionReferenceInstance { - @Override - @NonNull - IContainerFlagSupport getFlagContainer(); + protected AbstractFieldInstance(@NonNull PARENT parent) { + super(parent); + } @Override - @Nullable - default IBoundInstanceFlag getFlagInstanceByName(QName name) { - return getFlagContainer().getFlagInstanceMap().get(name); + public DEFINITION getDefinition() { + // this should always be not null + return ObjectUtils.asType(ObjectUtils.requireNonNull( + getContainingModule() + .getScopedFieldDefinitionByName(getReferencedDefinitionQName()))); } + /** + * Generates a "coordinate" string for the assembly instance. + * + * @return the coordinate + */ + @SuppressWarnings("null") @Override - @NonNull - default Collection getFlagInstances() { - return ObjectUtils.notNull(getFlagContainer().getFlagInstanceMap().values()); + public String toCoordinates() { + IDefinition definition = getDefinition(); + return String.format("field instance %s -> %s in module %s (@%d(%d)", + getXmlQName(), + definition.getDefinitionQName(), + getContainingDefinition().getContainingModule().getShortName(), + hashCode(), + definition.hashCode()); } } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/AbstractFlagInstance.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/AbstractFlagInstance.java new file mode 100644 index 000000000..82ca43adb --- /dev/null +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/AbstractFlagInstance.java @@ -0,0 +1,73 @@ +/* + * Portions of this software was developed by employees of the National Institute + * of Standards and Technology (NIST), an agency of the Federal Government and is + * being made available as a public service. Pursuant to title 17 United States + * Code Section 105, works of NIST employees are not subject to copyright + * protection in the United States. This software may be subject to foreign + * copyright. Permission in the United States and in foreign countries, to the + * extent that NIST may hold copyright, to use, copy, modify, create derivative + * works, and distribute this software and its documentation without fee is hereby + * granted on a non-exclusive basis, provided that this notice and disclaimer + * of warranty appears in all copies. + * + * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER + * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY + * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM + * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE + * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT + * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, + * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, + * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, + * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR + * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT + * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. + */ + +package gov.nist.secauto.metaschema.core.model; + +import gov.nist.secauto.metaschema.core.util.ObjectUtils; + +import edu.umd.cs.findbugs.annotations.NonNull; + +public abstract class AbstractFlagInstance< + PARENT extends IModelDefinition, + DEFINITION extends IFlagDefinition, + INSTANCE extends IFlagInstance> + extends AbstractNamedInstance + implements IFlagInstance, IFeatureDefinitionReferenceInstance { + + protected AbstractFlagInstance(@NonNull PARENT parent) { + super(parent, name -> parent.getContainingModule().toFlagQName(name)); + } + + @Override + public DEFINITION getDefinition() { + // this should always be not null + return ObjectUtils.asType(ObjectUtils.requireNonNull( + getContainingModule() + .getScopedFlagDefinitionByName(getReferencedDefinitionQName()))); + } + + @Override + public final PARENT getContainingDefinition() { + return getParentContainer(); + } + + /** + * Generates a "coordinate" string for the assembly instance. + * + * @return the coordinate + */ + @SuppressWarnings("null") + @Override + public String toCoordinates() { + IDefinition definition = getDefinition(); + return String.format("flag instance %s -> %s in module %s (@%d(%d)", + getXmlQName(), + definition.getDefinitionQName(), + getContainingDefinition().getContainingModule().getShortName(), + hashCode(), + definition.hashCode()); + } +} diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/AbstractGlobalAssemblyDefinition.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/AbstractGlobalAssemblyDefinition.java new file mode 100644 index 000000000..b36c09198 --- /dev/null +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/AbstractGlobalAssemblyDefinition.java @@ -0,0 +1,53 @@ +/* + * Portions of this software was developed by employees of the National Institute + * of Standards and Technology (NIST), an agency of the Federal Government and is + * being made available as a public service. Pursuant to title 17 United States + * Code Section 105, works of NIST employees are not subject to copyright + * protection in the United States. This software may be subject to foreign + * copyright. Permission in the United States and in foreign countries, to the + * extent that NIST may hold copyright, to use, copy, modify, create derivative + * works, and distribute this software and its documentation without fee is hereby + * granted on a non-exclusive basis, provided that this notice and disclaimer + * of warranty appears in all copies. + * + * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER + * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY + * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM + * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE + * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT + * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, + * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, + * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, + * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR + * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT + * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. + */ + +package gov.nist.secauto.metaschema.core.model; + +import edu.umd.cs.findbugs.annotations.NonNull; + +public abstract class AbstractGlobalAssemblyDefinition< + MODULE extends IModule, + INSTANCE extends IAssemblyInstance, + FLAG extends IFlagInstance, + MODEL extends IModelInstanceAbsolute, + NAMED_MODEL extends INamedModelInstanceAbsolute, + FIELD extends IFieldInstanceAbsolute, + ASSEMBLY extends IAssemblyInstanceAbsolute, + CHOICE extends IChoiceInstance, + CHOICE_GROUP extends IChoiceGroupInstance> + extends AbstractGlobalDefinition + implements IAssemblyDefinition, IFeatureContainerFlag, IFeatureContainerModelAssembly< + MODEL, + NAMED_MODEL, + FIELD, + ASSEMBLY, + CHOICE, + CHOICE_GROUP> { + + protected AbstractGlobalAssemblyDefinition(@NonNull MODULE module) { + super(module, name -> module.toModelQName(name)); + } +} diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/AbstractDefinition.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/AbstractGlobalDefinition.java similarity index 76% rename from databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/AbstractDefinition.java rename to core/src/main/java/gov/nist/secauto/metaschema/core/model/AbstractGlobalDefinition.java index 1af2b5dda..aec1bc70c 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/AbstractDefinition.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/AbstractGlobalDefinition.java @@ -24,29 +24,34 @@ * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. */ -package gov.nist.secauto.metaschema.databind.model.metaschema.impl; - -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingModule; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingDefinition; +package gov.nist.secauto.metaschema.core.model; import edu.umd.cs.findbugs.annotations.NonNull; -public abstract class AbstractDefinition - extends AbstractBinding - implements IBindingDefinition { +public abstract class AbstractGlobalDefinition + extends AbstractDefinition { @NonNull - private final IBindingModule module; + private final MODULE module; - protected AbstractDefinition( - @NonNull BINDING binding, - @NonNull IBindingModule module) { - super(binding); + protected AbstractGlobalDefinition(@NonNull MODULE module, @NonNull NameInitializer initializer) { + super(initializer); this.module = module; - } @Override - public IBindingModule getContainingModule() { + public final MODULE getContainingModule() { return module; } + + @Override + public final boolean isInline() { + // never inline + return false; + } + + @Override + public final INSTANCE getInlineInstance() { + // never inline + return null; + } } diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/AbstractBinding.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/AbstractGlobalFieldDefinition.java similarity index 79% rename from databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/AbstractBinding.java rename to core/src/main/java/gov/nist/secauto/metaschema/core/model/AbstractGlobalFieldDefinition.java index 2a60a5bbb..f5466ad4d 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/AbstractBinding.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/AbstractGlobalFieldDefinition.java @@ -24,25 +24,18 @@ * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. */ -package gov.nist.secauto.metaschema.databind.model.metaschema.impl; - -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingModelElement; +package gov.nist.secauto.metaschema.core.model; import edu.umd.cs.findbugs.annotations.NonNull; -public abstract class AbstractBinding implements IBindingModelElement { - @NonNull - private final BINDING binding; - - protected AbstractBinding( - @NonNull BINDING binding) { - this.binding = binding; - } +public abstract class AbstractGlobalFieldDefinition< + MODULE extends IModule, + INSTANCE extends IFieldInstance, + FLAG extends IFlagInstance> + extends AbstractGlobalDefinition + implements IFieldDefinition, IFeatureContainerFlag { - @Override - @NonNull - public BINDING getBinding() { - return binding; + protected AbstractGlobalFieldDefinition(@NonNull MODULE module) { + super(module, name -> module.toModelQName(name)); } - } diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/IBindingInstance.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/AbstractGlobalFlagDefinition.java similarity index 81% rename from databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/IBindingInstance.java rename to core/src/main/java/gov/nist/secauto/metaschema/core/model/AbstractGlobalFlagDefinition.java index 0c626c4eb..4748c2327 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/IBindingInstance.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/AbstractGlobalFlagDefinition.java @@ -24,19 +24,15 @@ * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. */ -package gov.nist.secauto.metaschema.databind.model.metaschema; - -import gov.nist.secauto.metaschema.core.model.IInstance; +package gov.nist.secauto.metaschema.core.model; import edu.umd.cs.findbugs.annotations.NonNull; -public interface IBindingInstance extends IInstance, IBindingModelElement { - @Override - @NonNull - IBindingDefinitionModel getContainingDefinition(); +public abstract class AbstractGlobalFlagDefinition + extends AbstractGlobalDefinition + implements IFlagDefinition { - @Override - default IBindingModule getContainingModule() { - return getContainingDefinition().getContainingModule(); + protected AbstractGlobalFlagDefinition(@NonNull MODULE module) { + super(module, name -> module.toFlagQName(name)); } } diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/IFeatureBindingDefinitionInline.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/AbstractInlineAssemblyDefinition.java similarity index 59% rename from databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/IFeatureBindingDefinitionInline.java rename to core/src/main/java/gov/nist/secauto/metaschema/core/model/AbstractInlineAssemblyDefinition.java index 51726dd86..563bed25d 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/IFeatureBindingDefinitionInline.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/AbstractInlineAssemblyDefinition.java @@ -24,40 +24,52 @@ * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. */ -package gov.nist.secauto.metaschema.databind.model.metaschema.impl; +package gov.nist.secauto.metaschema.core.model; -import gov.nist.secauto.metaschema.core.model.IFeatureDefinitionInstanceInlined; -import gov.nist.secauto.metaschema.core.model.INamedInstance; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingDefinition; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingInstance; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingModule; +import gov.nist.secauto.metaschema.core.util.ObjectUtils; import edu.umd.cs.findbugs.annotations.NonNull; -// REFACTOR: Try to eliminate this interface in favor of IFeatureInlinedDefinitionInstance -public interface IFeatureBindingDefinitionInline< - DEFINITION extends IBindingDefinition, - INSTANCE extends IBindingInstance & INamedInstance> - extends IBindingDefinition, IBindingInstance, +public abstract class AbstractInlineAssemblyDefinition< + PARENT extends IContainerModel, + DEFINITION extends IAssemblyDefinition, + INSTANCE extends IAssemblyInstance, + PARENT_DEFINITION extends IAssemblyDefinition, + FLAG extends IFlagInstance, + MODEL extends IModelInstanceAbsolute, + NAMED_MODEL extends INamedModelInstanceAbsolute, + FIELD extends IFieldInstanceAbsolute, + ASSEMBLY extends IAssemblyInstanceAbsolute, + CHOICE extends IChoiceInstance, + CHOICE_GROUP extends IChoiceGroupInstance> + extends AbstractNamedModelInstance + implements IAssemblyInstance, IAssemblyDefinition, IFeatureContainerFlag, + IFeatureContainerModelAssembly< + MODEL, + NAMED_MODEL, + FIELD, + ASSEMBLY, + CHOICE, + CHOICE_GROUP>, IFeatureDefinitionInstanceInlined { + protected AbstractInlineAssemblyDefinition(@NonNull PARENT parent) { + super(parent); + } + + @Override + public final DEFINITION getDefinition() { + return ObjectUtils.asType(this); + } + @Override @NonNull - default IBindingModule getContainingModule() { - // this is the same as IBoundInstance, but is needed since IBoundDefinition - // and IBoundInstance both declare it - return getContainingDefinition().getContainingModule(); + public final INSTANCE getInlineInstance() { + return ObjectUtils.asType(this); } - /** - * {@inheritDoc} - *

- * Use the effective name of the instance. - */ @Override - default String getJsonName() { - // this is the same as INamedModelElement, but is needed since IBoundProperty - // also declares it - return getEffectiveName(); + public final FLAG getJsonKey() { + return IFeatureContainerFlag.super.getJsonKey(); } } diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/AbstractInstance.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/AbstractInlineFieldDefinition.java similarity index 66% rename from databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/AbstractInstance.java rename to core/src/main/java/gov/nist/secauto/metaschema/core/model/AbstractInlineFieldDefinition.java index fea395302..17ea2e6ea 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/AbstractInstance.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/AbstractInlineFieldDefinition.java @@ -24,39 +24,40 @@ * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. */ -package gov.nist.secauto.metaschema.databind.model.metaschema.impl; +package gov.nist.secauto.metaschema.core.model; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingDefinitionModel; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingInstance; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingModule; +import gov.nist.secauto.metaschema.core.util.ObjectUtils; import edu.umd.cs.findbugs.annotations.NonNull; -public abstract class AbstractInstance - extends AbstractBinding - implements IBindingInstance, IFeatureValueless { - @NonNull - private final IBindingDefinitionModel parent; - - protected AbstractInstance( - @NonNull BINDING binding, - @NonNull IBindingDefinitionModel parent) { - super(binding); - this.parent = parent; +public abstract class AbstractInlineFieldDefinition< + PARENT extends IContainerModel, + DEFINITION extends IFieldDefinition, + INSTANCE extends IFieldInstance, + PARENT_DEFINITION extends IAssemblyDefinition, + FLAG extends IFlagInstance> + extends AbstractNamedModelInstance + implements IFieldInstance, IFieldDefinition, + IFeatureContainerFlag, + IFeatureDefinitionInstanceInlined { + + protected AbstractInlineFieldDefinition(@NonNull PARENT parent) { + super(parent); } @Override - public IBindingDefinitionModel getParentContainer() { - return parent; + public final DEFINITION getDefinition() { + return ObjectUtils.asType(this); } @Override - public IBindingDefinitionModel getContainingDefinition() { - return parent; + @NonNull + public final INSTANCE getInlineInstance() { + return ObjectUtils.asType(this); } @Override - public IBindingModule getContainingModule() { - return getContainingDefinition().getContainingModule(); + public final FLAG getJsonKey() { + return IFeatureContainerFlag.super.getJsonKey(); } } diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/IBindingDefinitionModel.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/AbstractInlineFlagDefinition.java similarity index 69% rename from databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/IBindingDefinitionModel.java rename to core/src/main/java/gov/nist/secauto/metaschema/core/model/AbstractInlineFlagDefinition.java index a660cadb6..927efc077 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/IBindingDefinitionModel.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/AbstractInlineFlagDefinition.java @@ -24,33 +24,37 @@ * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. */ -package gov.nist.secauto.metaschema.databind.model.metaschema; +package gov.nist.secauto.metaschema.core.model; -import gov.nist.secauto.metaschema.core.model.IModelDefinition; import gov.nist.secauto.metaschema.core.util.ObjectUtils; -import java.util.Collection; +import edu.umd.cs.findbugs.annotations.NonNull; -import javax.xml.namespace.QName; +public abstract class AbstractInlineFlagDefinition< + PARENT extends IModelDefinition, + DEFINITION extends IFlagDefinition, + INSTANCE extends IFlagInstance> + extends AbstractNamedInstance + implements IFlagInstance, IFlagDefinition, + IFeatureDefinitionInstanceInlined { -import edu.umd.cs.findbugs.annotations.Nullable; + protected AbstractInlineFlagDefinition(@NonNull PARENT parent) { + super(parent, name -> parent.getContainingModule().toFlagQName(name)); + } -public interface IBindingDefinitionModel - extends IBindingDefinition, IModelDefinition { @Override - IBindingInstanceFlag getFlagInstanceByName(QName name); + public final DEFINITION getDefinition() { + return ObjectUtils.asType(this); + } @Override - Collection getFlagInstances(); - - @Nullable - String getJsonKeyFlagName(); + @NonNull + public final INSTANCE getInlineInstance() { + return ObjectUtils.asType(this); + } @Override - default IBindingInstanceFlag getJsonKeyFlagInstance() { - String name = getJsonKeyFlagName(); - return name == null ? null - : ObjectUtils.requireNonNull(getFlagInstanceByName( - getContainingModule().toFlagQName(name))); + public PARENT getContainingDefinition() { + return getParentContainer(); } } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/AbstractInstance.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/AbstractInstance.java index 03154c372..e451ba5e0 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/AbstractInstance.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/AbstractInstance.java @@ -27,9 +27,8 @@ package gov.nist.secauto.metaschema.core.model; import edu.umd.cs.findbugs.annotations.NonNull; -import edu.umd.cs.findbugs.annotations.Nullable; -public abstract class AbstractInstance

implements IInstanceAbsolute { +public abstract class AbstractInstance

implements IInstance { @NonNull private final P parent; @@ -45,13 +44,7 @@ protected AbstractInstance(@NonNull P parent) { @Override @NonNull - public P getParentContainer() { + public final P getParentContainer() { return parent; } - - @Override - @Nullable - public Object getValue(Object parent) { - return getResolvedDefaultValue(); - } } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/AbstractLoader.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/AbstractLoader.java index dbdd06faf..e69eeaf76 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/AbstractLoader.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/AbstractLoader.java @@ -148,7 +148,7 @@ protected T loadInternal(@NonNull URI resource, @NonNull Deque visitedResou // first check if the current resource has been visited to prevent cycles if (visitedResources.contains(resource)) { throw new MetaschemaException("Cycle detected in metaschema includes for '" + resource + "'. Call stack: '" - + visitedResources.stream().map(n -> n.toString()).collect(Collectors.joining(","))); + + visitedResources.stream().map(URI::toString).collect(Collectors.joining(","))); } T retval = cache.get(resource); diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/AbstractModule.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/AbstractModule.java index 8ee1c2cc6..515387585 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/AbstractModule.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/AbstractModule.java @@ -42,6 +42,8 @@ import java.util.stream.Collectors; import java.util.stream.Stream; +import javax.xml.namespace.QName; + import edu.umd.cs.findbugs.annotations.NonNull; import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; import nl.talsmasoftware.lazy4j.Lazy; @@ -116,7 +118,7 @@ public Collection getExportedFlagDefinitions() { } @Override - public FL getExportedFlagDefinitionByName(String name) { + public FL getExportedFlagDefinitionByName(QName name) { return getExports().getExportedFlagDefinitionMap().get(name); } @@ -127,7 +129,7 @@ public Collection getExportedFieldDefinitions() { } @Override - public FI getExportedFieldDefinitionByName(String name) { + public FI getExportedFieldDefinitionByName(QName name) { return getExports().getExportedFieldDefinitionMap().get(name); } @@ -138,13 +140,20 @@ public Collection getExportedAssemblyDefinitions() { } @Override - public A getExportedAssemblyDefinitionByName(String name) { + public A getExportedAssemblyDefinitionByName(QName name) { return getExports().getExportedAssemblyDefinitionMap().get(name); } + @Override + public A getExportedRootAssemblyDefinitionByName(QName name) { + return getExports().getExportedRootAssemblyDefinitionMap().get(name); + } + @SuppressWarnings({ "unused", "PMD.UnusedPrivateMethod" }) // used by lambda private static DEF handleShadowedDefinitions( - @SuppressWarnings("unused") @NonNull String key, @NonNull DEF oldDef, @NonNull DEF newDef) { + @SuppressWarnings("unused") @NonNull QName key, + @NonNull DEF oldDef, + @NonNull DEF newDef) { if (!oldDef.equals(newDef) && LOGGER.isWarnEnabled()) { LOGGER.warn("The {} '{}' from metaschema '{}' is shadowing '{}' from metaschema '{}'", newDef.getModelType().name().toLowerCase(Locale.ROOT), @@ -158,11 +167,13 @@ private static DEF handleShadowedDefinitions( private class Exports { @NonNull - private final Map exportedFlagDefinitions; + private final Map exportedFlagDefinitions; + @NonNull + private final Map exportedFieldDefinitions; @NonNull - private final Map exportedFieldDefinitions; + private final Map exportedAssemblyDefinitions; @NonNull - private final Map exportedAssemblyDefinitions; + private final Map exportedRootAssemblyDefinitions; @SuppressWarnings("PMD.ConstructorCallsOverridableMethod") public Exports(@NonNull List importedModules) { @@ -196,19 +207,19 @@ public Exports(@NonNull List importedModules) { // Build the maps. Definitions from this module will take priority, with // shadowing being reported when a definition from this module has the same name // as an imported one - Map exportedFlagDefinitions = flags.collect( + Map exportedFlagDefinitions = flags.collect( CustomCollectors.toMap( - IFlagDefinition::getName, + IFlagDefinition::getDefinitionQName, CustomCollectors.identity(), AbstractModule::handleShadowedDefinitions)); - Map exportedFieldDefinitions = fields.collect( + Map exportedFieldDefinitions = fields.collect( CustomCollectors.toMap( - IFieldDefinition::getName, + IFieldDefinition::getDefinitionQName, CustomCollectors.identity(), AbstractModule::handleShadowedDefinitions)); - Map exportedAssemblyDefinitions = assemblies.collect( + Map exportedAssemblyDefinitions = assemblies.collect( CustomCollectors.toMap( - IAssemblyDefinition::getName, + IAssemblyDefinition::getDefinitionQName, CustomCollectors.identity(), AbstractModule::handleShadowedDefinitions)); @@ -221,22 +232,34 @@ public Exports(@NonNull List importedModules) { this.exportedAssemblyDefinitions = exportedAssemblyDefinitions.isEmpty() ? CollectionUtil.emptyMap() : CollectionUtil.unmodifiableMap(exportedAssemblyDefinitions); - + this.exportedRootAssemblyDefinitions = exportedAssemblyDefinitions.isEmpty() + ? CollectionUtil.emptyMap() + : CollectionUtil.unmodifiableMap(ObjectUtils.notNull(exportedAssemblyDefinitions.values().stream() + .filter(IAssemblyDefinition::isRoot) + .collect(CustomCollectors.toMap( + IAssemblyDefinition::getRootXmlQName, + CustomCollectors.identity(), + AbstractModule::handleShadowedDefinitions)))); } @NonNull - public Map getExportedFlagDefinitionMap() { + public Map getExportedFlagDefinitionMap() { return this.exportedFlagDefinitions; } @NonNull - public Map getExportedFieldDefinitionMap() { + public Map getExportedFieldDefinitionMap() { return this.exportedFieldDefinitions; } @NonNull - public Map getExportedAssemblyDefinitionMap() { + public Map getExportedAssemblyDefinitionMap() { return this.exportedAssemblyDefinitions; } + + @NonNull + public Map getExportedRootAssemblyDefinitionMap() { + return this.exportedRootAssemblyDefinitions; + } } } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/AbstractModuleLoader.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/AbstractModuleLoader.java index a89cf7c50..d3685383b 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/AbstractModuleLoader.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/AbstractModuleLoader.java @@ -98,7 +98,7 @@ protected List getModulePostProcessors() { protected abstract M newModule( @NonNull URI resource, @NonNull T binding, - @NonNull List importedModules) throws MetaschemaException; + @NonNull List importedModules) throws MetaschemaException; /** * Get the list of Metaschema module URIs associated with the provided binding. diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/IFeatureBindingContainerFlag.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/AbstractNamedInstance.java similarity index 63% rename from databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/IFeatureBindingContainerFlag.java rename to core/src/main/java/gov/nist/secauto/metaschema/core/model/AbstractNamedInstance.java index 2661e5898..e651b9ee9 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/IFeatureBindingContainerFlag.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/AbstractNamedInstance.java @@ -24,39 +24,48 @@ * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. */ -package gov.nist.secauto.metaschema.databind.model.metaschema.impl; +package gov.nist.secauto.metaschema.core.model; -import java.util.Collection; +import gov.nist.secauto.metaschema.core.model.AbstractDefinition.NameInitializer; +import gov.nist.secauto.metaschema.core.util.ObjectUtils; import javax.xml.namespace.QName; import edu.umd.cs.findbugs.annotations.NonNull; -import edu.umd.cs.findbugs.annotations.Nullable; -import gov.nist.secauto.metaschema.core.model.IContainerFlagSupport; -import gov.nist.secauto.metaschema.core.model.IFeatureContainerFlag; -import gov.nist.secauto.metaschema.core.util.ObjectUtils; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingDefinitionModel; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingInstanceFlag; +import nl.talsmasoftware.lazy4j.Lazy; -/** - * Represents a definition that may contain flags. - */ -public interface IFeatureBindingContainerFlag - extends IFeatureContainerFlag, IBindingDefinitionModel { - - @Override +public abstract class AbstractNamedInstance< + PARENT extends IContainer> + extends AbstractInstance + implements INamedInstance { @NonNull - IContainerFlagSupport getFlagContainer(); + private final Lazy qname; + @NonNull + private final Lazy definitionQName; + + /** + * Construct a new instance. + * + * @param parent + * the parent containing the instance + * @param initializer + * used to generate the instance qualified name + */ + protected AbstractNamedInstance(@NonNull PARENT parent, @NonNull NameInitializer initializer) { + super(parent); + this.qname = ObjectUtils.notNull(Lazy.lazy(() -> initializer.apply(getEffectiveName()))); + this.definitionQName = ObjectUtils.notNull(Lazy.lazy(() -> initializer.apply(getName()))); + } + @SuppressWarnings("null") @Override - @Nullable - default IBindingInstanceFlag getFlagInstanceByName(QName name) { - return getFlagContainer().getFlagInstanceMap().get(name); + public final QName getXmlQName() { + return qname.get(); } + @SuppressWarnings("null") @Override - @NonNull - default Collection getFlagInstances() { - return ObjectUtils.notNull(getFlagContainer().getFlagInstanceMap().values()); + public final QName getReferencedDefinitionQName() { + return definitionQName.get(); } } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/AbstractNamedModelInstance.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/AbstractNamedModelInstance.java new file mode 100644 index 000000000..b5d7a8ba2 --- /dev/null +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/AbstractNamedModelInstance.java @@ -0,0 +1,47 @@ +/* + * Portions of this software was developed by employees of the National Institute + * of Standards and Technology (NIST), an agency of the Federal Government and is + * being made available as a public service. Pursuant to title 17 United States + * Code Section 105, works of NIST employees are not subject to copyright + * protection in the United States. This software may be subject to foreign + * copyright. Permission in the United States and in foreign countries, to the + * extent that NIST may hold copyright, to use, copy, modify, create derivative + * works, and distribute this software and its documentation without fee is hereby + * granted on a non-exclusive basis, provided that this notice and disclaimer + * of warranty appears in all copies. + * + * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER + * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY + * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM + * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE + * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT + * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, + * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, + * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, + * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR + * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT + * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. + */ + +package gov.nist.secauto.metaschema.core.model; + +import gov.nist.secauto.metaschema.core.util.ObjectUtils; + +import edu.umd.cs.findbugs.annotations.NonNull; + +public abstract class AbstractNamedModelInstance< + PARENT extends IContainerModel, + PARENT_DEFINITION extends IAssemblyDefinition> + extends AbstractNamedInstance + implements INamedModelInstance { + + protected AbstractNamedModelInstance(@NonNull PARENT parent) { + super(parent, name -> parent.getOwningDefinition().getContainingModule().toModelQName(name)); + } + + @Override + public final PARENT_DEFINITION getContainingDefinition() { + return ObjectUtils.asType(getParentContainer().getOwningDefinition()); + } +} diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/FlagContainerBuilder.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/FlagContainerBuilder.java new file mode 100644 index 000000000..b702706e8 --- /dev/null +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/FlagContainerBuilder.java @@ -0,0 +1,102 @@ +/* + * Portions of this software was developed by employees of the National Institute + * of Standards and Technology (NIST), an agency of the Federal Government and is + * being made available as a public service. Pursuant to title 17 United States + * Code Section 105, works of NIST employees are not subject to copyright + * protection in the United States. This software may be subject to foreign + * copyright. Permission in the United States and in foreign countries, to the + * extent that NIST may hold copyright, to use, copy, modify, create derivative + * works, and distribute this software and its documentation without fee is hereby + * granted on a non-exclusive basis, provided that this notice and disclaimer + * of warranty appears in all copies. + * + * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER + * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY + * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM + * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE + * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT + * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, + * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, + * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, + * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR + * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT + * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. + */ + +package gov.nist.secauto.metaschema.core.model; + +import gov.nist.secauto.metaschema.core.model.impl.DefaultContainerFlagSupport; +import gov.nist.secauto.metaschema.core.util.CollectionUtil; +import gov.nist.secauto.metaschema.core.util.CustomCollectors; +import gov.nist.secauto.metaschema.core.util.ObjectUtils; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import java.util.LinkedHashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; + +import javax.xml.namespace.QName; + +import edu.umd.cs.findbugs.annotations.NonNull; +import edu.umd.cs.findbugs.annotations.Nullable; + +public class FlagContainerBuilder implements IFlagContainerBuilder { + private static final Logger LOGGER = LogManager.getLogger(FlagContainerBuilder.class); + + @Nullable + private final QName jsonKeyName; + @NonNull + private final List flags; + + public FlagContainerBuilder(@Nullable QName jsonKeyName) { + this.jsonKeyName = jsonKeyName; + this.flags = new LinkedList<>(); + } + + @Override + @NonNull + public IFlagContainerBuilder flag(@NonNull T instance) { + flags.add(instance); + return this; + } + + @Override + public IContainerFlagSupport build() { + IContainerFlagSupport retval = null; + if (flags.isEmpty()) { + retval = IContainerFlagSupport.empty(); + } else { + Map flagMap = CollectionUtil.unmodifiableMap(ObjectUtils.notNull(flags.stream() + .collect( + CustomCollectors.toMap( + INamed::getXmlQName, + CustomCollectors.identity(), + FlagContainerBuilder::handleShadowedInstances, + LinkedHashMap::new)))); + + T jsonKey = jsonKeyName == null ? null : flagMap.get(jsonKeyName); + retval = new DefaultContainerFlagSupport<>(flagMap, jsonKey); + } + return retval; + } + + private static INSTANCE handleShadowedInstances( + @NonNull QName key, + @NonNull INSTANCE shadowed, + @NonNull INSTANCE shadowing) { + if (!shadowed.equals(shadowing) && LOGGER.isErrorEnabled()) { + IModelDefinition owningDefinition = shadowing.getContainingDefinition(); + IModule module = owningDefinition.getContainingModule(); + LOGGER.error("Unexpected duplicate flag instance named '%s' in definition '%s' in module name '%s' at '%s'", + key, + owningDefinition.getDefinitionQName(), + module.getShortName(), + module.getLocation()); + } + return shadowing; + } +} diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/IAssemblyDefinition.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/IAssemblyDefinition.java index ec9bfbc96..a204152cf 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/IAssemblyDefinition.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/IAssemblyDefinition.java @@ -26,6 +26,7 @@ package gov.nist.secauto.metaschema.core.model; +import gov.nist.secauto.metaschema.core.metapath.item.node.IAssemblyNodeItem; import gov.nist.secauto.metaschema.core.model.constraint.IFeatureModelConstrained; import javax.xml.namespace.QName; @@ -108,6 +109,11 @@ default IAssemblyDefinition getOwningDefinition() { return this; } + @Override + default IAssemblyNodeItem getNodeItem() { + return null; + } + @Override default boolean hasChildren() { return IModelDefinition.super.hasChildren() || IContainerModelAssembly.super.hasChildren(); diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/IChoiceGroupInstance.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/IChoiceGroupInstance.java index 727e000f9..4a43cea19 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/IChoiceGroupInstance.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/IChoiceGroupInstance.java @@ -31,6 +31,7 @@ import java.util.Locale; import edu.umd.cs.findbugs.annotations.NonNull; +import edu.umd.cs.findbugs.annotations.Nullable; public interface IChoiceGroupInstance extends IModelInstanceAbsolute, IContainerModelGrouped { @@ -84,6 +85,18 @@ default boolean isEffectiveValueWrappedInXml() { return true; } + /** + * Get the effective name of the JSON key flag, if a JSON key is configured. + *

+ * This name is expected to be in the same namespace as the containing model + * element (i.e. choice group, assembly, field). + * + * @return the name of the JSON key flag if configured, or {@code null} + * otherwise + */ + @Nullable + String getJsonKeyFlagInstanceName(); + /** * Get the named model instance for the provided choice group item. * diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/IContainerFlagSupport.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/IContainerFlagSupport.java index 24ef98b9e..112c0031b 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/IContainerFlagSupport.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/IContainerFlagSupport.java @@ -26,11 +26,14 @@ package gov.nist.secauto.metaschema.core.model; +import gov.nist.secauto.metaschema.core.model.impl.EmptyFlagContainer; + import java.util.Map; import javax.xml.namespace.QName; import edu.umd.cs.findbugs.annotations.NonNull; +import edu.umd.cs.findbugs.annotations.Nullable; public interface IContainerFlagSupport { /** @@ -46,6 +49,16 @@ static IContainerFlagSupport empty() { return (IContainerFlagSupport) EmptyFlagContainer.EMPTY; } + @NonNull + static IFlagContainerBuilder builder() { + return new FlagContainerBuilder<>(null); + } + + @NonNull + static IFlagContainerBuilder builder(@NonNull QName jsonKey) { + return new FlagContainerBuilder<>(jsonKey); + } + /** * Get a mapping of flag effective name to flag instance. * @@ -53,6 +66,7 @@ static IContainerFlagSupport empty() { */ @NonNull Map getFlagInstanceMap(); - // @Nullable - // FI getJsonKeyFlagInstance(); + + @Nullable + FI getJsonKeyFlagInstance(); } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/IDefinition.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/IDefinition.java index e6a367f3b..b4f6c1131 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/IDefinition.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/IDefinition.java @@ -30,6 +30,8 @@ import java.util.Locale; +import javax.xml.namespace.QName; + import edu.umd.cs.findbugs.annotations.NonNull; public interface IDefinition extends INamedModelElement, IAttributable, IFeatureValueConstrained { @@ -47,6 +49,21 @@ default ModuleScopeEnum getModuleScope() { return ModuleScopeEnum.LOCAL; } + /** + * The qualified name for the definition. + *

+ * This name is the combination of the definition's namespace, which is the + * module's namespace, and the definition's name. + * + * @return the definition's qualified name + */ + @NonNull + default QName getDefinitionQName() { + return new QName( + getContainingModule().getXmlNamespace().toASCIIString(), + getName()); + } + /** * Determine if the definition is defined inline, meaning the definition is * declared where it is used. @@ -55,7 +72,7 @@ default ModuleScopeEnum getModuleScope() { * the definition is able to be globally referenced */ default boolean isInline() { - return false; + return getInlineInstance() != null; } /** diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/IDescribable.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/IDescribable.java index 5aed8308e..e9a2c4530 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/IDescribable.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/IDescribable.java @@ -48,4 +48,28 @@ public interface IDescribable { // from INamedModelElement @Nullable MarkupLine getDescription(); + + /** + * The resolved formal display name, which allows an instance to override a + * definition's name. + * + * @return the formal name or {@code null} if not defined + */ + // from INamedModelElement + @Nullable + default String getEffectiveFormalName() { + return getFormalName(); + } + + /** + * Get the text that describes the basic use of the element, which allows an + * instance to override a definition's description. + * + * @return a line of markup text or {@code null} if not defined + */ + // from INamedModelElement + @Nullable + default MarkupLine getEffectiveDescription() { + return getDescription(); + } } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/IFeatureContainerFlag.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/IFeatureContainerFlag.java index 72ee85aba..51ac31b1a 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/IFeatureContainerFlag.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/IFeatureContainerFlag.java @@ -41,7 +41,7 @@ * @param * the flag instance Java type */ -public interface IFeatureContainerFlag extends IContainerFlag { +public interface IFeatureContainerFlag extends IModelDefinition { /** * Lazy initialize the flag instances associated with this definition. * @@ -61,4 +61,9 @@ default FI getFlagInstanceByName(QName name) { default Collection getFlagInstances() { return ObjectUtils.notNull(getFlagContainer().getFlagInstanceMap().values()); } + + @Override + default FI getJsonKey() { + return getFlagContainer().getJsonKeyFlagInstance(); + } } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/IFeatureContainerModelAbsolute.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/IFeatureContainerModelAbsolute.java index 4d3fddc78..03d6e5cd2 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/IFeatureContainerModelAbsolute.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/IFeatureContainerModelAbsolute.java @@ -46,7 +46,8 @@ public interface IFeatureContainerModelAbsolute< MI extends IModelInstanceAbsolute, NMI extends INamedModelInstanceAbsolute, FI extends IFieldInstanceAbsolute, - AI extends IAssemblyInstanceAbsolute> extends IContainerModelAbsolute, IFeatureContainerModel { + AI extends IAssemblyInstanceAbsolute> + extends IContainerModelAbsolute, IFeatureContainerModel { @Override default Collection getModelInstances() { return getModelContainer().getModelInstances(); diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/IFeatureDefinitionInstanceInlined.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/IFeatureDefinitionInstanceInlined.java index 15c748dbb..11b3f48ea 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/IFeatureDefinitionInstanceInlined.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/IFeatureDefinitionInstanceInlined.java @@ -27,9 +27,12 @@ package gov.nist.secauto.metaschema.core.model; import gov.nist.secauto.metaschema.core.datatype.markup.MarkupLine; +import gov.nist.secauto.metaschema.core.util.ObjectUtils; import java.util.Locale; +import javax.xml.namespace.QName; + import edu.umd.cs.findbugs.annotations.NonNull; import edu.umd.cs.findbugs.annotations.Nullable; @@ -42,7 +45,9 @@ * @param * the associated instance Java type */ -public interface IFeatureDefinitionInstanceInlined +public interface IFeatureDefinitionInstanceInlined< + DEFINITION extends IDefinition, + INSTANCE extends INamedInstance> extends IDefinition, INamedInstance { @Override default boolean isInline() { @@ -51,11 +56,20 @@ default boolean isInline() { } @Override - DEFINITION getDefinition(); + default QName getDefinitionQName() { + return getReferencedDefinitionQName(); + } + + @Override + default DEFINITION getDefinition() { + return ObjectUtils.asType(this); + } @Override @NonNull - INSTANCE getInlineInstance(); + default INSTANCE getInlineInstance() { + return ObjectUtils.asType(this); + } @Override default String getEffectiveFormalName() { diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/IFeatureDefinitionReferenceInstance.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/IFeatureDefinitionReferenceInstance.java index 16e54f0ff..e4fc03b98 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/IFeatureDefinitionReferenceInstance.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/IFeatureDefinitionReferenceInstance.java @@ -28,8 +28,6 @@ import gov.nist.secauto.metaschema.core.datatype.markup.MarkupLine; -import java.util.Locale; - import edu.umd.cs.findbugs.annotations.NonNull; import edu.umd.cs.findbugs.annotations.Nullable; @@ -103,32 +101,4 @@ default Object getEffectiveDefaultValue() { } return retval; } - - /** - * Generates a "coordinate" string for the provided information element - * instance. - * - * A coordinate consists of the element's: - *

- * - * @return the coordinate - */ - @SuppressWarnings("null") - @Override - default String toCoordinates() { - IDefinition definition = getDefinition(); - return String.format("%s-instance:%s:%s/%s@%d(%d)", - getModelType().toString().toLowerCase(Locale.ROOT), - getContainingDefinition().getContainingModule().getShortName(), - definition.getName(), - getName(), - hashCode(), - definition.hashCode()); - } } diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/IFeatureValueless.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/IFeatureValueless.java similarity index 93% rename from databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/IFeatureValueless.java rename to core/src/main/java/gov/nist/secauto/metaschema/core/model/IFeatureValueless.java index cfbcae651..59f0b9527 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/IFeatureValueless.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/IFeatureValueless.java @@ -24,10 +24,9 @@ * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. */ -package gov.nist.secauto.metaschema.databind.model.metaschema.impl; - -import gov.nist.secauto.metaschema.core.model.IInstanceAbsolute; +package gov.nist.secauto.metaschema.core.model; +// REFACTOR: rename to IFeatureValuelessInstance public interface IFeatureValueless extends IInstanceAbsolute { @Override default Object getValue(Object parent) { diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/IFlag.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/IFlag.java index 9e08aed4a..a76a352f3 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/IFlag.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/IFlag.java @@ -40,14 +40,4 @@ public interface IFlag extends INamedModelElement, IAttributable { default ModelType getModelType() { return ModelType.FLAG; } - - /** - * {@inheritDoc} - *

- * Flags by default do not have namespaces. - */ - @Override - default String getXmlNamespace() { - return null; - } } diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/IBinding.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/IFlagContainerBuilder.java similarity index 89% rename from databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/IBinding.java rename to core/src/main/java/gov/nist/secauto/metaschema/core/model/IFlagContainerBuilder.java index fffff52dd..59ffae730 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/IBinding.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/IFlagContainerBuilder.java @@ -24,16 +24,14 @@ * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. */ -package gov.nist.secauto.metaschema.databind.model.metaschema; - -import gov.nist.secauto.metaschema.core.metapath.item.node.INodeItem; +package gov.nist.secauto.metaschema.core.model; import edu.umd.cs.findbugs.annotations.NonNull; -public interface IBinding { +public interface IFlagContainerBuilder { @NonNull - Object getBinding(); + IFlagContainerBuilder flag(@NonNull T instance); @NonNull - INodeItem getBoundNodeItem(); + IContainerFlagSupport build(); } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/IFlagDefinition.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/IFlagDefinition.java index 7aa06994e..727a7fd69 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/IFlagDefinition.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/IFlagDefinition.java @@ -28,8 +28,5 @@ public interface IFlagDefinition extends IValuedDefinition, IFlag { @Override - default IFlagInstance getInlineInstance() { - // not inline by default - return null; - } + IFlagInstance getInlineInstance(); } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/IFlagInstance.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/IFlagInstance.java index f927a2965..3b6038ea9 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/IFlagInstance.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/IFlagInstance.java @@ -26,16 +26,23 @@ package gov.nist.secauto.metaschema.core.model; +import javax.xml.namespace.QName; + public interface IFlagInstance extends IFlag, IValuedInstance, IInstanceAbsolute { boolean DEFAULT_FLAG_REQUIRED = false; @Override - IContainerFlag getParentContainer(); + IModelDefinition getParentContainer(); @Override IFlagDefinition getDefinition(); + @Override + default IModelDefinition getContainingDefinition() { + return getParentContainer(); + } + /** * Determines if a flag value is required to be provided. * @@ -45,4 +52,15 @@ public interface IFlagInstance extends IFlag, IValuedInstance, IInstanceAbsolute default boolean isRequired() { return DEFAULT_FLAG_REQUIRED; } + + @Override + default QName getReferencedDefinitionQName() { + return getContainingModule().toFlagQName(getName()); + } + + @Override + default QName getXmlQName() { + // flags do not have a namespace by default + return getContainingModule().toFlagQName(getEffectiveName()); + } } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/IGroupable.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/IGroupable.java index 6cbb04920..e06412a16 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/IGroupable.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/IGroupable.java @@ -36,7 +36,7 @@ import edu.umd.cs.findbugs.annotations.NonNull; import edu.umd.cs.findbugs.annotations.Nullable; -public interface IGroupable extends IInstanceAbsolute, IKeyed { +public interface IGroupable extends IInstanceAbsolute { int DEFAULT_GROUP_AS_MIN_OCCURS = 0; int DEFAULT_GROUP_AS_MAX_OCCURS = 1; diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/IKeyed.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/IJsonNamed.java similarity index 86% rename from core/src/main/java/gov/nist/secauto/metaschema/core/model/IKeyed.java rename to core/src/main/java/gov/nist/secauto/metaschema/core/model/IJsonNamed.java index 88df9aae9..0b1ed76e2 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/IKeyed.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/IJsonNamed.java @@ -26,16 +26,16 @@ package gov.nist.secauto.metaschema.core.model; -import edu.umd.cs.findbugs.annotations.Nullable; +import edu.umd.cs.findbugs.annotations.NonNull; -public interface IKeyed { +public interface IJsonNamed { /** - * Get the name of the JSON key, if a JSON key is configured. + * Get the name used for the associated property in JSON/YAML + * serialization-related operations. * - * @return the name of the JSON key flag if configured, or {@code null} - * otherwise + * @return the JSON property name */ - @Nullable - String getJsonKeyFlagName(); + @NonNull + String getJsonName(); } diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundInstanceNamed.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/IMetapathQueryable.java similarity index 77% rename from databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundInstanceNamed.java rename to core/src/main/java/gov/nist/secauto/metaschema/core/model/IMetapathQueryable.java index e90e5ebc6..ac8f757a8 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundInstanceNamed.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/IMetapathQueryable.java @@ -24,15 +24,22 @@ * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. */ -package gov.nist.secauto.metaschema.databind.model; +package gov.nist.secauto.metaschema.core.model; -import gov.nist.secauto.metaschema.core.model.INamedInstance; +import gov.nist.secauto.metaschema.core.metapath.item.node.INodeItem; -public interface IBoundInstanceNamed extends IBoundInstance, INamedInstance { +import edu.umd.cs.findbugs.annotations.Nullable; - @Override - default String getJsonName() { - // needed to avoid conflict between IBoundInstance and INamedInstance - return INamedInstance.super.getJsonName(); +public interface IMetapathQueryable { + /** + * Get the Metapath node item for this Metaschema module construct, which can be + * used to query it. + * + * @return the node item if the implementation can be queried by a Metapath or + * {@code null} otherwise + */ + @Nullable + default INodeItem getNodeItem() { + return null; } } diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/IBindingInstanceModelAbsolute.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/IMetaschemaModule.java similarity index 87% rename from databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/IBindingInstanceModelAbsolute.java rename to core/src/main/java/gov/nist/secauto/metaschema/core/model/IMetaschemaModule.java index 28c6a7601..8400ef50a 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/IBindingInstanceModelAbsolute.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/IMetaschemaModule.java @@ -24,10 +24,14 @@ * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. */ -package gov.nist.secauto.metaschema.databind.model.metaschema; +package gov.nist.secauto.metaschema.core.model; -import gov.nist.secauto.metaschema.core.model.IModelInstanceAbsolute; - -public interface IBindingInstanceModelAbsolute extends IModelInstanceAbsolute, IBindingInstanceModel { +public interface IMetaschemaModule + extends IModuleExtended< + IMetaschemaModule, + IModelDefinition, + IFlagDefinition, + IFieldDefinition, + IAssemblyDefinition> { // no additional methods } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/IModelDefinition.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/IModelDefinition.java index 8f6fd06dd..2d8317dda 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/IModelDefinition.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/IModelDefinition.java @@ -26,26 +26,39 @@ package gov.nist.secauto.metaschema.core.model; +import java.util.Collection; + +import javax.xml.namespace.QName; + +import edu.umd.cs.findbugs.annotations.NonNull; import edu.umd.cs.findbugs.annotations.Nullable; -public interface IModelDefinition extends IDefinition, IContainerFlag { +public interface IModelDefinition extends IDefinition, IContainer { + @Override + default boolean hasChildren() { + return !getFlagInstances().isEmpty(); + } /** - * Indicates if a flag's value can be used as a property name in the containing - * object in JSON who's value will be the object containing the flag. In such - * cases, the flag will not appear in the object. This is only allowed if the - * flag is required, as determined by a {@code true} result from - * {@link IFlagInstance#isRequired()}. The {@link IFlagInstance} can be - * retrieved using {@link #getJsonKeyFlagInstance()}. + * Retrieves a flag instance, by the flag's effective name, that is defined on + * the containing definition. * - * @return {@code true} if the flag's value can be used as a property name, or - * {@code false} otherwise - * @see #getJsonKeyFlagInstance() + * @param name + * the flag's name + * @return the matching flag instance, or {@code null} if there is no flag + * matching the specified name */ - // TODO: remove once moved to the instance side - default boolean hasJsonKey() { - return getJsonKeyFlagInstance() != null; - } + @Nullable + IFlagInstance getFlagInstanceByName(@NonNull QName name); + + /** + * Retrieves the flag instances for all flags defined on the containing + * definition. + * + * @return the flags + */ + @NonNull + Collection getFlagInstances(); /** * Retrieves the flag instance to use as as the property name for the containing @@ -53,9 +66,9 @@ default boolean hasJsonKey() { * * @return the flag instance if a JSON key is configured, or {@code null} * otherwise - * @see #hasJsonKey() */ // TODO: remove once moved to the instance side + // TODO: Reconsider using toFlagName in favor of getReferencedDefinitionQName @Nullable - IFlagInstance getJsonKeyFlagInstance(); + IFlagInstance getJsonKey(); } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/IModelElement.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/IModelElement.java index b6411e27b..b4da4e52d 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/IModelElement.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/IModelElement.java @@ -35,7 +35,7 @@ * A marker interface for Metaschema constructs that can be members of a * Metaschema definition's model. */ -public interface IModelElement extends IDefaultable { +public interface IModelElement extends IDefaultable, IMetapathQueryable { /** * Get the Metaschema model type of the information element. diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/IModelInstance.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/IModelInstance.java index f525d5c4b..d2e222590 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/IModelInstance.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/IModelInstance.java @@ -35,9 +35,7 @@ public interface IModelInstance extends IInstance { IContainerModel getParentContainer(); @Override - default IAssemblyDefinition getContainingDefinition() { - return getParentContainer().getOwningDefinition(); - } + IAssemblyDefinition getContainingDefinition(); /** * Indicate if the instance allows values without an XML element wrapper. diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/IModule.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/IModule.java index bb44743e3..02aae7400 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/IModule.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/IModule.java @@ -28,6 +28,7 @@ import gov.nist.secauto.metaschema.core.datatype.markup.MarkupLine; import gov.nist.secauto.metaschema.core.datatype.markup.MarkupMultiline; +import gov.nist.secauto.metaschema.core.metapath.item.node.IDocumentNodeItem; import java.net.URI; import java.util.Collection; @@ -38,7 +39,7 @@ import edu.umd.cs.findbugs.annotations.NonNull; import edu.umd.cs.findbugs.annotations.Nullable; -public interface IModule { +public interface IModule extends IMetapathQueryable { String XML_NAMESPACE = "http://csrc.nist.gov/ns/oscal/metaschema/1.0"; @@ -144,7 +145,7 @@ default QName getQName() { * @return the matching assembly definition, or {@code null} if none match */ @Nullable - IAssemblyDefinition getAssemblyDefinitionByName(@NonNull String name); + IAssemblyDefinition getAssemblyDefinitionByName(@NonNull QName name); /** * Retrieves the top-level field definitions in this Metaschema module. @@ -164,7 +165,7 @@ default QName getQName() { * @return the matching field definition, or {@code null} if none match */ @Nullable - IFieldDefinition getFieldDefinitionByName(@NonNull String name); + IFieldDefinition getFieldDefinitionByName(@NonNull QName name); /** * Retrieves the top-level assembly and field definitions in this Metaschema @@ -194,7 +195,7 @@ default QName getQName() { * @return the matching flag definition, or {@code null} if none match */ @Nullable - IFlagDefinition getFlagDefinitionByName(@NonNull String name); + IFlagDefinition getFlagDefinitionByName(@NonNull QName name); /** * Retrieves the assembly definition with a matching name from either: 1) the @@ -207,7 +208,7 @@ default QName getQName() { * @return the assembly definition */ @Nullable - IAssemblyDefinition getScopedAssemblyDefinitionByName(@NonNull String name); + IAssemblyDefinition getScopedAssemblyDefinitionByName(@NonNull QName name); /** * Retrieves the field definition with a matching name from either: 1) the @@ -219,7 +220,7 @@ default QName getQName() { * @return the field definition */ @Nullable - IFieldDefinition getScopedFieldDefinitionByName(@NonNull String name); + IFieldDefinition getScopedFieldDefinitionByName(@NonNull QName name); /** * Retrieves the flag definition with a matching name from either: 1) the @@ -231,16 +232,7 @@ default QName getQName() { * @return the flag definition */ @Nullable - IFlagDefinition getScopedFlagDefinitionByName(@NonNull String name); - - /** - * Retrieves the top-level assembly definitions that are marked as roots from - * the current Metaschema module and any imported Metaschema modules. - * - * @return a listing of assembly definitions marked as root - */ - @NonNull - Collection getExportedRootAssemblyDefinitions(); + IFlagDefinition getScopedFlagDefinitionByName(@NonNull QName name); /** * Retrieves the top-level assembly definitions that are marked as roots from @@ -276,7 +268,7 @@ default QName getQName() { * @return the flag definition, or {@code null} if it doesn't exist. */ @Nullable - IFlagDefinition getExportedFlagDefinitionByName(String name); + IFlagDefinition getExportedFlagDefinitionByName(@NonNull QName name); /** * Retrieve the top-level field definitions that are marked global in this @@ -303,7 +295,7 @@ default QName getQName() { * @return the field definition, or {@code null} if it doesn't exist. */ @Nullable - IFieldDefinition getExportedFieldDefinitionByName(String name); + IFieldDefinition getExportedFieldDefinitionByName(@NonNull QName name); /** * Retrieve the top-level assembly definitions that are marked global in this @@ -323,14 +315,36 @@ default QName getQName() { * Retrieves the exported named assembly definition, if it exists. *

* For information about how assembly definitions are exported see - * {@link #getExportedFieldDefinitions()}. + * {@link #getExportedAssemblyDefinitions()}. * * @param name * the definition name * @return the assembly definition, or {@code null} if it doesn't exist. */ @Nullable - IAssemblyDefinition getExportedAssemblyDefinitionByName(String name); + IAssemblyDefinition getExportedAssemblyDefinitionByName(@NonNull QName name); + + /** + * Retrieves the top-level assembly definitions that are marked as roots from + * the current Metaschema module and any imported Metaschema modules. + * + * @return a listing of assembly definitions marked as root + */ + @NonNull + Collection getExportedRootAssemblyDefinitions(); + + /** + * Retrieves the exported named root assembly definition, if it exists. + *

+ * For information about how assembly definitions are exported see + * {@link #getExportedAssemblyDefinitions()}. + * + * @param name + * the root name + * @return the assembly definition, or {@code null} if it doesn't exist. + */ + @Nullable + IAssemblyDefinition getExportedRootAssemblyDefinitionByName(QName name); @NonNull default QName toFlagQName(@NonNull String nameRef) { @@ -338,9 +352,35 @@ default QName toFlagQName(@NonNull String nameRef) { return new QName(nameRef); } + @NonNull + default QName toFlagQName(@Nullable String modelNamespace, @NonNull String nameRef) { + // TODO: handle namespace prefix + return modelNamespace == null + ? new QName(nameRef) + : new QName(modelNamespace, nameRef); + } + @NonNull default QName toModelQName(@NonNull String nameRef) { + return toModelQName(null, nameRef); + } + + @NonNull + default QName toModelQName(@Nullable String modelNamespace, @NonNull String nameRef) { // TODO: handle namespace prefix - return new QName(getXmlNamespace().toASCIIString(), nameRef); + String namespace = modelNamespace == null ? getXmlNamespace().toASCIIString() : modelNamespace; + return new QName(namespace, nameRef); + } + + /** + * If this module is supported by an underlying Metaschema module + * representation, this method retrieves that representation. + * + * @return the underlying document as a Metapath node item or {@code null} + */ + @Override + @Nullable + default IDocumentNodeItem getNodeItem() { + return null; } } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/IModuleExtended.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/IModuleExtended.java index fa0afbf91..8e259c499 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/IModuleExtended.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/IModuleExtended.java @@ -34,6 +34,8 @@ import java.util.stream.Collectors; import java.util.stream.Stream; +import javax.xml.namespace.QName; + import edu.umd.cs.findbugs.annotations.NonNull; import edu.umd.cs.findbugs.annotations.Nullable; @@ -44,9 +46,9 @@ * can be accessed using {@link #getImportedModules()}. *

* Global scoped Metaschema definitions can be accessed using - * {@link #getScopedAssemblyDefinitionByName(String)}, - * {@link #getScopedFieldDefinitionByName(String)}, and - * {@link #getScopedFlagDefinitionByName(String)}. These methods take into + * {@link #getScopedAssemblyDefinitionByName(QName)}, + * {@link #getScopedFieldDefinitionByName(QName)}, and + * {@link #getScopedFlagDefinitionByName(QName)}. These methods take into * consideration the import order to provide the global definitions that are in * scope within the given Metschema module. *

@@ -120,7 +122,7 @@ static Predicate allRootAssemblyDefinitions() { @Override @Nullable - FL getFlagDefinitionByName(@NonNull String name); + FL getFlagDefinitionByName(@NonNull QName name); @Override @NonNull @@ -128,7 +130,7 @@ static Predicate allRootAssemblyDefinitions() { @Override @Nullable - A getAssemblyDefinitionByName(@NonNull String name); + A getAssemblyDefinitionByName(@NonNull QName name); @Override @NonNull @@ -136,7 +138,7 @@ static Predicate allRootAssemblyDefinitions() { @Override @Nullable - FI getFieldDefinitionByName(@NonNull String name); + FI getFieldDefinitionByName(@NonNull QName name); @Override @SuppressWarnings("unchecked") @@ -151,7 +153,7 @@ default List getAssemblyAndFieldDefinitions() { @Override @Nullable - default A getScopedAssemblyDefinitionByName(@NonNull String name) { + default A getScopedAssemblyDefinitionByName(@NonNull QName name) { // first try local/global top-level definitions from current metaschema module A retval = getAssemblyDefinitionByName(name); if (retval == null) { @@ -163,7 +165,7 @@ default A getScopedAssemblyDefinitionByName(@NonNull String name) { @Override @Nullable - default FI getScopedFieldDefinitionByName(@NonNull String name) { + default FI getScopedFieldDefinitionByName(@NonNull QName name) { // first try local/global top-level definitions from current metaschema module FI retval = getFieldDefinitionByName(name); if (retval == null) { @@ -175,7 +177,7 @@ default FI getScopedFieldDefinitionByName(@NonNull String name) { @Override @Nullable - default FL getScopedFlagDefinitionByName(@NonNull String name) { + default FL getScopedFlagDefinitionByName(@NonNull QName name) { // first try local/global top-level definitions from current metaschema module FL retval = getFlagDefinitionByName(name); if (retval == null) { @@ -207,7 +209,7 @@ default Collection getRootAssemblyDefinitions() { @Override @Nullable - FL getExportedFlagDefinitionByName(String name); + FL getExportedFlagDefinitionByName(QName name); @Override @NonNull @@ -215,7 +217,7 @@ default Collection getRootAssemblyDefinitions() { @Override @Nullable - FI getExportedFieldDefinitionByName(String name); + FI getExportedFieldDefinitionByName(QName name); @Override @NonNull @@ -223,5 +225,5 @@ default Collection getRootAssemblyDefinitions() { @Override @Nullable - A getExportedAssemblyDefinitionByName(String name); + A getExportedAssemblyDefinitionByName(QName name); } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/INamed.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/INamed.java new file mode 100644 index 000000000..33b3eb8a7 --- /dev/null +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/INamed.java @@ -0,0 +1,153 @@ +/* + * Portions of this software was developed by employees of the National Institute + * of Standards and Technology (NIST), an agency of the Federal Government and is + * being made available as a public service. Pursuant to title 17 United States + * Code Section 105, works of NIST employees are not subject to copyright + * protection in the United States. This software may be subject to foreign + * copyright. Permission in the United States and in foreign countries, to the + * extent that NIST may hold copyright, to use, copy, modify, create derivative + * works, and distribute this software and its documentation without fee is hereby + * granted on a non-exclusive basis, provided that this notice and disclaimer + * of warranty appears in all copies. + * + * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER + * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY + * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM + * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE + * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT + * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, + * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, + * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, + * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR + * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT + * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. + */ + +package gov.nist.secauto.metaschema.core.model; + +import javax.xml.XMLConstants; +import javax.xml.namespace.QName; + +import edu.umd.cs.findbugs.annotations.NonNull; +import edu.umd.cs.findbugs.annotations.Nullable; + +public interface INamed { + + /** + * Retrieve the name of the model element. + * + * @return the name + */ + @NonNull + String getName(); + + /** + * Retrieve the name to use for the model element, instead of the name. + * + * @return the use name or {@code null} if no use name is defined + */ + // from INamedModelElement + @Nullable + default String getUseName() { + // no use-name by default + return null; + } + + /** + * Get the name to use based on the provided names. This method will return the + * use name provided by {@link #getUseName()} if the call is not {@code null}, + * and fall back to the name provided by {@link #getName()} otherwise. This is + * the model name to use for the for an instance where the instance is + * referenced. + * + * @return the use name if available, or the name if not + * + * @see #getUseName() + * @see #getName() + */ + // from INamedModelElement + @NonNull + default String getEffectiveName() { + @Nullable String useName = getUseName(); + return useName == null ? getName() : useName; + } + + /** + * Retrieve the XML namespace for this instance. + *

+ * Multiple calls to this method are expected to produce the same, deterministic + * return value. + * + * @return the XML namespace or {@code null} if no namespace is defined + */ + @Nullable + default String getXmlNamespace() { + return getXmlQName().getNamespaceURI(); + } + + /** + * Get the unique XML qualified name for this model element. + *

+ * The qualified name is considered to be unique relative to all sibling + * elements. For a flag, this name will be unique among all flag instances on + * the same field or assembly definition. For a field or assembly, this name + * will be unique among all sibling field or assembly instances on the same + * assembly definition. + *

+ * Multiple calls to this method are expected to produce the same, deterministic + * return value. + *

+ * If {@link #getXmlNamespace()} is {@code null}, the the resulting QName will + * have the namespace {@link XMLConstants#NULL_NS_URI}. + *

+ * This implementation may be overridden by implementation that cache the QName + * or provide for a more efficient method for QName creation. + * + * @return the XML qualified name, or {@code null} if there isn't one + */ + // REFACTOR: rename to getQName + @NonNull + QName getXmlQName(); + + /** + * Retrieve the index value to use for binary naming. + * + * @return the name index or {@code null} if no name index is defined + */ + // from INamedModelElement + @Nullable + default Integer getIndex() { + // no index by default + return null; + } + + /** + * Retrieve the index value to use for binary naming, instead of the name. + * + * @return the use name index or {@code null} if no use name index is defined + */ + // from INamedModelElement + @Nullable + default Integer getUseIndex() { + // no use-name index by default + return null; + } + + /** + * Get the index value to use for binary naming based on the provided index + * values. + *

+ * This method will return the use index value provided by + * {@link #getUseIndex()} if the call result is not {@code null}, and fall back + * to the index value provided by {@link #getIndex()} otherwise. + * + * @return the index value if available, or {@code null} otherwise + */ + // from INamedModelElement + @Nullable + default Integer getEffectiveIndex() { + @Nullable Integer useIndex = getUseIndex(); + return useIndex == null ? getIndex() : useIndex; + } +} diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/INamedInstance.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/INamedInstance.java index fac230666..bb695f1fa 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/INamedInstance.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/INamedInstance.java @@ -26,6 +26,8 @@ package gov.nist.secauto.metaschema.core.model; +import javax.xml.namespace.QName; + import edu.umd.cs.findbugs.annotations.NonNull; /** @@ -43,4 +45,12 @@ public interface INamedInstance extends INamedModelElement, IAttributable, IInst @NonNull IDefinition getDefinition(); + /** + * This represents the qualified name of a referenced definition. + * + * @return the qualified name + * @see IDefinition#getDefinitionQName() + */ + @NonNull + QName getReferencedDefinitionQName(); } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/INamedModelElement.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/INamedModelElement.java index cca624f0a..3871afb15 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/INamedModelElement.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/INamedModelElement.java @@ -26,169 +26,15 @@ package gov.nist.secauto.metaschema.core.model; -import gov.nist.secauto.metaschema.core.datatype.markup.MarkupLine; - -import javax.xml.XMLConstants; -import javax.xml.namespace.QName; - import edu.umd.cs.findbugs.annotations.NonNull; -import edu.umd.cs.findbugs.annotations.Nullable; /** * A marker interface for Metaschema constructs that can be members of a * Metaschema module's model that have a name and other identifying * characteristics. */ -public interface INamedModelElement extends IDescribable, IModelElement { - /** - * The resolved formal display name, which allows an instance to override a - * definition's name. - * - * @return the formal name or {@code null} if not defined - */ - // from INamedModelElement - @Nullable - default String getEffectiveFormalName() { - return getFormalName(); - } - - /** - * Get the text that describes the basic use of the element, which allows an - * instance to override a definition's description. - * - * @return a line of markup text or {@code null} if not defined - */ - // from INamedModelElement - @Nullable - default MarkupLine getEffectiveDescription() { - return getDescription(); - } - - /** - * Retrieve the name of the model element. - * - * @return the name - */ - @NonNull - String getName(); - - /** - * Retrieve the name to use for the model element, instead of the name. - * - * @return the use name or {@code null} if no use name is defined - */ - // from INamedModelElement - @Nullable - default String getUseName() { - // no use-name by default - return null; - } - - /** - * Get the name to use based on the provided names. This method will return the - * use name provided by {@link #getUseName()} if the call is not {@code null}, - * and fall back to the name provided by {@link #getName()} otherwise. This is - * the model name to use for the for an instance where the instance is - * referenced. - * - * @return the use name if available, or the name if not - * - * @see #getUseName() - * @see #getName() - */ - // from INamedModelElement - @NonNull - default String getEffectiveName() { - @Nullable String useName = getUseName(); - return useName == null ? getName() : useName; - } - - /** - * Retrieve the XML namespace for this instance. - *

- * Multiple calls to this method are expected to produce the same, deterministic - * return value. - * - * @return the XML namespace or {@code null} if no namespace is defined - */ - @Nullable - default String getXmlNamespace() { - return getContainingModule().getXmlNamespace().toASCIIString(); - } - - /** - * Get the unique XML qualified name for this model element. - *

- * The qualified name is considered to be unique relative to all sibling - * elements. For a flag, this name will be unique among all flag instances on - * the same field or assembly definition. For a field or assembly, this name - * will be unique among all sibling field or assembly instances on the same - * assembly definition. - *

- * Multiple calls to this method are expected to produce the same, deterministic - * return value. - *

- * If {@link #getXmlNamespace()} is {@code null}, the the resulting QName will - * have the namespace {@link XMLConstants#NULL_NS_URI}. - *

- * This implementation may be overridden by implementation that cache the QName - * or provide for a more efficient method for QName creation. - * - * @return the XML qualified name, or {@code null} if there isn't one - */ - // REFACTOR: rename to getQName - @NonNull - default QName getXmlQName() { - return new QName(getXmlNamespace(), getEffectiveName()); - } - - /** - * Retrieve the index value to use for binary naming. - * - * @return the name index or {@code null} if no name index is defined - */ - // from INamedModelElement - @Nullable - default Integer getIndex() { - // no index by default - return null; - } - - /** - * Retrieve the index value to use for binary naming, instead of the name. - * - * @return the use name index or {@code null} if no use name index is defined - */ - // from INamedModelElement - @Nullable - default Integer getUseIndex() { - // no use-name index by default - return null; - } - - /** - * Get the index value to use for binary naming based on the provided index - * values. - *

- * This method will return the use index value provided by - * {@link #getUseIndex()} if the call result is not {@code null}, and fall back - * to the index value provided by {@link #getIndex()} otherwise. - * - * @return the index value if available, or {@code null} otherwise - */ - // from INamedModelElement - @Nullable - default Integer getEffectiveIndex() { - @Nullable Integer useIndex = getUseIndex(); - return useIndex == null ? getIndex() : useIndex; - } - - /** - * Get the name used for the associated property in JSON/YAML. - * - * @return the JSON property name - */ - // from INamedModelElement +public interface INamedModelElement extends IDescribable, IModelElement, IJsonNamed, INamed { + @Override @NonNull default String getJsonName() { return getEffectiveName(); diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/INamedModelInstance.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/INamedModelInstance.java index b87337a72..47c14c813 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/INamedModelInstance.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/INamedModelInstance.java @@ -26,10 +26,57 @@ package gov.nist.secauto.metaschema.core.model; +import javax.xml.namespace.QName; + import edu.umd.cs.findbugs.annotations.NonNull; +import edu.umd.cs.findbugs.annotations.Nullable; -public interface INamedModelInstance extends IModelInstance, INamedInstance, IKeyed { +public interface INamedModelInstance extends IModelInstance, INamedInstance { @Override @NonNull IModelDefinition getDefinition(); + + /** + * Indicates if a flag's value can be used as a property name in the containing + * object in JSON who's value will be the object containing the flag. In such + * cases, the flag will not appear in the object. This is only allowed if the + * flag is required, as determined by a {@code true} result from + * {@link IFlagInstance#isRequired()}. The {@link IFlagInstance} can be + * retrieved using {@link #getEffectiveJsonKey()}. + * + * @return {@code true} if the flag's value can be used as a property name, or + * {@code false} otherwise + * @see #getEffectiveJsonKey() + */ + // TODO: remove once moved to the instance side + default boolean hasJsonKey() { + return getEffectiveJsonKey() != null; + } + + /** + * Get the JSON key flag instance for this model instance, if one is configured. + * + * @return the JSON key flag instance or {@code null} if a JSON key is + * configured + */ + @Nullable + IFlagInstance getEffectiveJsonKey(); + + /** + * Get the JSON key associated with this instance. + * + * @return the configured JSON key or {@code null} if no JSON key is configured + */ + @Nullable + IFlagInstance getJsonKey(); + + @Override + default QName getReferencedDefinitionQName() { + return getContainingModule().toModelQName(getName()); + } + + @Override + default QName getXmlQName() { + return getContainingModule().toModelQName(getEffectiveName()); + } } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/INamedModelInstanceAbsolute.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/INamedModelInstanceAbsolute.java index a80241dae..4e6a6b6cb 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/INamedModelInstanceAbsolute.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/INamedModelInstanceAbsolute.java @@ -29,6 +29,7 @@ import gov.nist.secauto.metaschema.core.util.ObjectUtils; import edu.umd.cs.findbugs.annotations.NonNull; +import edu.umd.cs.findbugs.annotations.Nullable; public interface INamedModelInstanceAbsolute extends INamedModelInstance, IModelInstanceAbsolute { @Override @@ -48,14 +49,16 @@ default String getJsonName() { } @Override - default String getJsonKeyFlagName() { - String retval = null; - if (JsonGroupAsBehavior.KEYED.equals(getJsonGroupAsBehavior())) { - IFlagInstance jsonKeyFlag = getDefinition().getJsonKeyFlagInstance(); - if (jsonKeyFlag != null) { - retval = jsonKeyFlag.getEffectiveName(); - } - } - return retval; + @Nullable + default IFlagInstance getEffectiveJsonKey() { + return JsonGroupAsBehavior.KEYED.equals(getJsonGroupAsBehavior()) + ? getJsonKey() + : null; + } + + @Override + @Nullable + default IFlagInstance getJsonKey() { + return getDefinition().getJsonKey(); } } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/INamedModelInstanceGrouped.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/INamedModelInstanceGrouped.java index 9b4f06699..e5c23a8c6 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/INamedModelInstanceGrouped.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/INamedModelInstanceGrouped.java @@ -69,10 +69,19 @@ default String getEffectiveDisciminatorValue() { } @Override - default String getJsonKeyFlagName() { - IChoiceGroupInstance choiceGroup = getParentContainer(); - return JsonGroupAsBehavior.KEYED.equals(choiceGroup.getJsonGroupAsBehavior()) - ? ObjectUtils.notNull(choiceGroup.getJsonKeyFlagName()) + @Nullable + default IFlagInstance getEffectiveJsonKey() { + return JsonGroupAsBehavior.KEYED.equals(getParentContainer().getJsonGroupAsBehavior()) + ? ObjectUtils.requireNonNull(getJsonKey()) : null; } + + @Override + @Nullable + default IFlagInstance getJsonKey() { + String name = getParentContainer().getJsonKeyFlagInstanceName(); + return name == null + ? null + : ObjectUtils.requireNonNull(getDefinition().getFlagInstanceByName(getContainingModule().toFlagQName(name))); + } } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/IValued.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/IValued.java index a86258466..643f7e582 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/IValued.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/IValued.java @@ -45,5 +45,7 @@ public interface IValued extends IDefaultable { */ // from IInstanceAbsolute @Nullable - Object getValue(@NonNull Object parent); + default Object getValue(@NonNull Object parent) { + return getResolvedDefaultValue(); + } } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/AbstractConstraintValidationHandler.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/AbstractConstraintValidationHandler.java index 505dccaef..0307dad4e 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/AbstractConstraintValidationHandler.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/AbstractConstraintValidationHandler.java @@ -295,7 +295,7 @@ protected CharSequence newAllowedValuesViolationMessage( String allowedValues = constraints.stream() .flatMap(constraint -> constraint.getAllowedValues().values().stream()) - .map(allowedValue -> allowedValue.getValue()) + .map(IAllowedValue::getValue) .sorted() .distinct() .collect(CustomCollectors.joiningWithOxfordComma("or")); diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/AssemblyTargetedConstraints.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/AssemblyTargetedConstraints.java index 5595e0459..0822720e3 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/AssemblyTargetedConstraints.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/AssemblyTargetedConstraints.java @@ -61,11 +61,10 @@ public void target(@NonNull IAssemblyDefinition definition) { } @Override - @SuppressWarnings("null") protected void applyTo(@NonNull IAssemblyDefinition definition) { super.applyTo(definition); - getIndexConstraints().forEach(constraint -> definition.addConstraint(constraint)); - getUniqueConstraints().forEach(constraint -> definition.addConstraint(constraint)); - getHasCardinalityConstraints().forEach(constraint -> definition.addConstraint(constraint)); + getIndexConstraints().forEach(definition::addConstraint); + getUniqueConstraints().forEach(definition::addConstraint); + getHasCardinalityConstraints().forEach(definition::addConstraint); } } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/DefaultConstraintValidator.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/DefaultConstraintValidator.java index 4a45b9688..0b276281f 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/DefaultConstraintValidator.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/DefaultConstraintValidator.java @@ -36,7 +36,6 @@ import gov.nist.secauto.metaschema.core.metapath.item.node.AbstractNodeItemVisitor; import gov.nist.secauto.metaschema.core.metapath.item.node.IAssemblyNodeItem; import gov.nist.secauto.metaschema.core.metapath.item.node.IDefinitionNodeItem; -import gov.nist.secauto.metaschema.core.metapath.item.node.IDocumentNodeItem; import gov.nist.secauto.metaschema.core.metapath.item.node.IFieldNodeItem; import gov.nist.secauto.metaschema.core.metapath.item.node.IFlagNodeItem; import gov.nist.secauto.metaschema.core.metapath.item.node.IModuleNodeItem; @@ -794,11 +793,6 @@ private DynamicContext handleLetStatements( return retval; } - @Override - public Void visitDocument(@NonNull IDocumentNodeItem item, DynamicContext context) { - return super.visitDocument(item, context); - } - @Override public Void visitFlag(@NonNull IFlagNodeItem item, DynamicContext context) { assert context != null; diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/impl/AbstractDefinitionTargetedConstraints.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/impl/AbstractDefinitionTargetedConstraints.java index 11dec4e6a..b38f5a36c 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/impl/AbstractDefinitionTargetedConstraints.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/impl/AbstractDefinitionTargetedConstraints.java @@ -75,12 +75,11 @@ protected AbstractDefinitionTargetedConstraints( * @param definition * the definition to apply the constraints to. */ - @SuppressWarnings("null") protected void applyTo(@NonNull T definition) { - getAllowedValuesConstraints().forEach(constraint -> definition.addConstraint(constraint)); - getMatchesConstraints().forEach(constraint -> definition.addConstraint(constraint)); - getIndexHasKeyConstraints().forEach(constraint -> definition.addConstraint(constraint)); - getExpectConstraints().forEach(constraint -> definition.addConstraint(constraint)); + getAllowedValuesConstraints().forEach(definition::addConstraint); + getMatchesConstraints().forEach(definition::addConstraint); + getIndexHasKeyConstraints().forEach(definition::addConstraint); + getExpectConstraints().forEach(definition::addConstraint); } @Override diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/DefaultContainerFlagSupport.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/impl/DefaultContainerFlagSupport.java similarity index 97% rename from core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/DefaultContainerFlagSupport.java rename to core/src/main/java/gov/nist/secauto/metaschema/core/model/impl/DefaultContainerFlagSupport.java index a15cbf6c7..4a70a2d45 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/DefaultContainerFlagSupport.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/impl/DefaultContainerFlagSupport.java @@ -24,7 +24,7 @@ * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. */ -package gov.nist.secauto.metaschema.core.model.xml.impl; +package gov.nist.secauto.metaschema.core.model.impl; import gov.nist.secauto.metaschema.core.model.IContainerFlagSupport; import gov.nist.secauto.metaschema.core.model.IFlagInstance; @@ -70,6 +70,7 @@ public Map getFlagInstanceMap() { * @return the flag instance if a JSON key is configured, or {@code null} * otherwise */ + @Override public FI getJsonKeyFlagInstance() { return jsonKey; } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/EmptyFlagContainer.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/impl/EmptyFlagContainer.java similarity index 91% rename from core/src/main/java/gov/nist/secauto/metaschema/core/model/EmptyFlagContainer.java rename to core/src/main/java/gov/nist/secauto/metaschema/core/model/impl/EmptyFlagContainer.java index ca8c4d904..d66a8e722 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/EmptyFlagContainer.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/impl/EmptyFlagContainer.java @@ -59,8 +59,10 @@ * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. */ -package gov.nist.secauto.metaschema.core.model; +package gov.nist.secauto.metaschema.core.model.impl; +import gov.nist.secauto.metaschema.core.model.IContainerFlagSupport; +import gov.nist.secauto.metaschema.core.model.IFlagInstance; import gov.nist.secauto.metaschema.core.util.CollectionUtil; import java.util.Map; @@ -69,7 +71,7 @@ import edu.umd.cs.findbugs.annotations.NonNull; -class EmptyFlagContainer implements IContainerFlagSupport { +public class EmptyFlagContainer implements IContainerFlagSupport { @NonNull public static final EmptyFlagContainer EMPTY = new EmptyFlagContainer<>(); @@ -77,4 +79,10 @@ class EmptyFlagContainer implements IContainerFlagSupp public Map getFlagInstanceMap() { return CollectionUtil.emptyMap(); } + + @Override + public FI getJsonKeyFlagInstance() { + // no JSON key + return null; + } } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/util/JsonUtil.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/util/JsonUtil.java index e25f62720..e4691f77d 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/util/JsonUtil.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/util/JsonUtil.java @@ -257,7 +257,7 @@ public static String getAssertMessage( JsonToken actual) { return ObjectUtils.notNull( String.format("Expected JsonToken(s) '%s', but found JsonToken '%s'%s.", - expected.stream().map(token -> token.name()).collect(CustomCollectors.joiningWithOxfordComma("or")), + expected.stream().map(Enum::name).collect(CustomCollectors.joiningWithOxfordComma("or")), actual, generateLocationMessage(parser))); } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/util/XmlEventUtil.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/util/XmlEventUtil.java index 4a49bf509..3ad22a6ce 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/util/XmlEventUtil.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/util/XmlEventUtil.java @@ -81,7 +81,9 @@ private XmlEventUtil() { @SuppressWarnings("null") @NonNull private static Object escape(@NonNull String data) { - return data.chars().mapToObj(c -> (char) c).map(c -> escape(c)).collect(Collectors.joining()); + return data.chars() + .mapToObj(c -> (char) c) + .map(XmlEventUtil::escape).collect(Collectors.joining()); } @SuppressWarnings("null") diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/IXmlModule.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/IXmlModule.java deleted file mode 100644 index d93b289ba..000000000 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/IXmlModule.java +++ /dev/null @@ -1,43 +0,0 @@ -/* - * Portions of this software was developed by employees of the National Institute - * of Standards and Technology (NIST), an agency of the Federal Government and is - * being made available as a public service. Pursuant to title 17 United States - * Code Section 105, works of NIST employees are not subject to copyright - * protection in the United States. This software may be subject to foreign - * copyright. Permission in the United States and in foreign countries, to the - * extent that NIST may hold copyright, to use, copy, modify, create derivative - * works, and distribute this software and its documentation without fee is hereby - * granted on a non-exclusive basis, provided that this notice and disclaimer - * of warranty appears in all copies. - * - * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER - * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY - * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM - * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE - * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT - * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, - * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, - * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, - * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR - * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT - * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. - */ - -package gov.nist.secauto.metaschema.core.model.xml; - -import gov.nist.secauto.metaschema.core.model.IAssemblyDefinition; -import gov.nist.secauto.metaschema.core.model.IFieldDefinition; -import gov.nist.secauto.metaschema.core.model.IFlagDefinition; -import gov.nist.secauto.metaschema.core.model.IModelDefinition; -import gov.nist.secauto.metaschema.core.model.IModuleExtended; - -public interface IXmlModule - extends IModuleExtended< - IXmlModule, - IModelDefinition, - IFlagDefinition, - IFieldDefinition, - IAssemblyDefinition> { - // no additional methods -} diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/ModuleLoader.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/ModuleLoader.java index d966fee1e..7d2d75b3a 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/ModuleLoader.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/ModuleLoader.java @@ -27,6 +27,7 @@ package gov.nist.secauto.metaschema.core.model.xml; import gov.nist.secauto.metaschema.core.model.AbstractModuleLoader; +import gov.nist.secauto.metaschema.core.model.IMetaschemaModule; import gov.nist.secauto.metaschema.core.model.IModuleLoader; import gov.nist.secauto.metaschema.core.model.MetaschemaException; import gov.nist.secauto.metaschema.core.model.xml.impl.XmlModule; @@ -60,7 +61,7 @@ * every use. Any Metaschema imported is also loaded and cached automatically. */ public class ModuleLoader - extends AbstractModuleLoader { + extends AbstractModuleLoader { private boolean resolveEntities; // = false; /** @@ -92,7 +93,8 @@ public void allowEntityResolution() { } @Override - protected IXmlModule newModule(URI resource, METASCHEMADocument binding, List importedModules) + protected IMetaschemaModule newModule(URI resource, METASCHEMADocument binding, + List importedModules) throws MetaschemaException { return new XmlModule(resource, binding, importedModules); } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/XmlMetaConstraintLoader.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/XmlMetaConstraintLoader.java index 98fa49bf2..11e0fae12 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/XmlMetaConstraintLoader.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/XmlMetaConstraintLoader.java @@ -42,6 +42,7 @@ import gov.nist.secauto.metaschema.core.model.constraint.ITargetedConstraints; import gov.nist.secauto.metaschema.core.model.xml.impl.ConstraintXmlSupport; import gov.nist.secauto.metaschema.core.model.xml.xmlbeans.MetaschemaMetaConstraintsDocument; +import gov.nist.secauto.metaschema.core.model.xml.xmlbeans.MetaschemaMetapathReferenceType; import gov.nist.secauto.metaschema.core.model.xml.xmlbeans.ModelContextType; import gov.nist.secauto.metaschema.core.util.CollectionUtil; import gov.nist.secauto.metaschema.core.util.ObjectUtils; @@ -89,13 +90,13 @@ private Context parseContext( List metapaths; if (parent == null) { metapaths = ObjectUtils.notNull(contextObj.getMetapathList().stream() - .map(metapath -> metapath.getTarget()) + .map(MetaschemaMetapathReferenceType::getTarget) .collect(Collectors.toList())); } else { List parentMetapaths = parent.getMetapaths().stream() .collect(Collectors.toList()); metapaths = ObjectUtils.notNull(contextObj.getMetapathList().stream() - .map(metapath -> metapath.getTarget()) + .map(MetaschemaMetapathReferenceType::getTarget) .flatMap(childPath -> { return parentMetapaths.stream() .map(parentPath -> parentPath + '/' + childPath); @@ -190,20 +191,18 @@ protected MetaTargetedContraints( * @param definition * the definition to apply the constraints to. */ - @SuppressWarnings("null") protected void applyTo(@NonNull IDefinition definition) { - getAllowedValuesConstraints().forEach(constraint -> definition.addConstraint(constraint)); - getMatchesConstraints().forEach(constraint -> definition.addConstraint(constraint)); - getIndexHasKeyConstraints().forEach(constraint -> definition.addConstraint(constraint)); - getExpectConstraints().forEach(constraint -> definition.addConstraint(constraint)); + getAllowedValuesConstraints().forEach(definition::addConstraint); + getMatchesConstraints().forEach(definition::addConstraint); + getIndexHasKeyConstraints().forEach(definition::addConstraint); + getExpectConstraints().forEach(definition::addConstraint); } - @SuppressWarnings("null") protected void applyTo(@NonNull IAssemblyDefinition definition) { applyTo((IDefinition) definition); - getIndexConstraints().forEach(constraint -> definition.addConstraint(constraint)); - getUniqueConstraints().forEach(constraint -> definition.addConstraint(constraint)); - getHasCardinalityConstraints().forEach(constraint -> definition.addConstraint(constraint)); + getIndexConstraints().forEach(definition::addConstraint); + getUniqueConstraints().forEach(definition::addConstraint); + getHasCardinalityConstraints().forEach(definition::addConstraint); } @Override diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/XmlAssemblyInstance.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/XmlAssemblyInstance.java index 2bbad0e53..1795b7d44 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/XmlAssemblyInstance.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/XmlAssemblyInstance.java @@ -28,19 +28,17 @@ import gov.nist.secauto.metaschema.core.datatype.markup.MarkupLine; import gov.nist.secauto.metaschema.core.datatype.markup.MarkupMultiline; -import gov.nist.secauto.metaschema.core.model.AbstractInstance; +import gov.nist.secauto.metaschema.core.model.AbstractAssemblyInstance; import gov.nist.secauto.metaschema.core.model.IAssemblyDefinition; import gov.nist.secauto.metaschema.core.model.IAssemblyInstanceAbsolute; import gov.nist.secauto.metaschema.core.model.IAttributable; import gov.nist.secauto.metaschema.core.model.IContainerModel; -import gov.nist.secauto.metaschema.core.model.IFeatureDefinitionReferenceInstance; import gov.nist.secauto.metaschema.core.model.JsonGroupAsBehavior; import gov.nist.secauto.metaschema.core.model.XmlGroupAsBehavior; import gov.nist.secauto.metaschema.core.model.xml.xmlbeans.AssemblyReferenceType; import gov.nist.secauto.metaschema.core.model.xml.xmlbeans.MarkupLineDatatype; import gov.nist.secauto.metaschema.core.model.xml.xmlbeans.UseNameType; import gov.nist.secauto.metaschema.core.util.CollectionUtil; -import gov.nist.secauto.metaschema.core.util.ObjectUtils; import java.util.Map; import java.util.Set; @@ -48,9 +46,13 @@ import edu.umd.cs.findbugs.annotations.NonNull; class XmlAssemblyInstance - extends AbstractInstance - implements IAssemblyInstanceAbsolute, - IFeatureDefinitionReferenceInstance { + extends AbstractAssemblyInstance< + IContainerModel, + IAssemblyDefinition, + IAssemblyInstanceAbsolute, + IAssemblyDefinition> + implements IAssemblyInstanceAbsolute { + @NonNull private final AssemblyReferenceType xmlObject; /** @@ -69,13 +71,6 @@ public XmlAssemblyInstance( this.xmlObject = xmlObject; } - @Override - public IAssemblyDefinition getDefinition() { - // This will always be not null - return ObjectUtils.notNull(getContainingModule() - .getScopedAssemblyDefinitionByName(getName())); - } - // ---------------------------------------- // - Start XmlBeans driven code - CPD-OFF - // ---------------------------------------- @@ -85,6 +80,7 @@ public IAssemblyDefinition getDefinition() { * * @return the underlying XML data */ + @NonNull protected AssemblyReferenceType getXmlObject() { return xmlObject; } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/XmlChoiceGroupInstance.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/XmlChoiceGroupInstance.java index 9a847300f..426f66f37 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/XmlChoiceGroupInstance.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/XmlChoiceGroupInstance.java @@ -27,11 +27,10 @@ package gov.nist.secauto.metaschema.core.model.xml.impl; import gov.nist.secauto.metaschema.core.datatype.markup.MarkupMultiline; -import gov.nist.secauto.metaschema.core.model.AbstractInstance; +import gov.nist.secauto.metaschema.core.model.AbstractChoiceGroupInstance; import gov.nist.secauto.metaschema.core.model.IAssemblyDefinition; import gov.nist.secauto.metaschema.core.model.IAssemblyInstanceGrouped; import gov.nist.secauto.metaschema.core.model.IChoiceGroupInstance; -import gov.nist.secauto.metaschema.core.model.IFeatureContainerModelGrouped; import gov.nist.secauto.metaschema.core.model.IFieldInstanceGrouped; import gov.nist.secauto.metaschema.core.model.IModule; import gov.nist.secauto.metaschema.core.model.INamedModelInstanceGrouped; @@ -56,9 +55,11 @@ import nl.talsmasoftware.lazy4j.Lazy; class XmlChoiceGroupInstance - extends AbstractInstance - implements IChoiceGroupInstance, - IFeatureContainerModelGrouped { + extends AbstractChoiceGroupInstance< + IAssemblyDefinition, + INamedModelInstanceGrouped, + IFieldInstanceGrouped, + IAssemblyInstanceGrouped> { @NonNull private final GroupedChoiceType xmlObject; @NonNull @@ -108,11 +109,11 @@ protected GroupedChoiceType getXmlObject() { public String getJsonDiscriminatorProperty() { return getXmlObject().isSetDiscriminator() ? ObjectUtils.requireNonNull(getXmlObject().getDiscriminator()) - : IChoiceGroupInstance.DEFAULT_JSON_DISCRIMINATOR_PROPERTY_NAME; + : DEFAULT_JSON_DISCRIMINATOR_PROPERTY_NAME; } @Override - public String getJsonKeyFlagName() { + public String getJsonKeyFlagInstanceName() { return getXmlObject().isSetJsonKey() ? getXmlObject().getJsonKey().getFlagRef() : null; } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/XmlChoiceInstance.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/XmlChoiceInstance.java index 9c2d24ae0..e6a78645c 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/XmlChoiceInstance.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/XmlChoiceInstance.java @@ -27,11 +27,10 @@ package gov.nist.secauto.metaschema.core.model.xml.impl; import gov.nist.secauto.metaschema.core.datatype.markup.MarkupMultiline; -import gov.nist.secauto.metaschema.core.model.AbstractInstance; +import gov.nist.secauto.metaschema.core.model.AbstractChoiceInstance; import gov.nist.secauto.metaschema.core.model.IAssemblyDefinition; import gov.nist.secauto.metaschema.core.model.IAssemblyInstanceAbsolute; import gov.nist.secauto.metaschema.core.model.IChoiceInstance; -import gov.nist.secauto.metaschema.core.model.IFeatureContainerModelAbsolute; import gov.nist.secauto.metaschema.core.model.IFieldInstanceAbsolute; import gov.nist.secauto.metaschema.core.model.IModelInstanceAbsolute; import gov.nist.secauto.metaschema.core.model.IModule; @@ -55,9 +54,8 @@ import nl.talsmasoftware.lazy4j.Lazy; class XmlChoiceInstance - extends AbstractInstance - implements IChoiceInstance, - IFeatureContainerModelAbsolute< + extends AbstractChoiceInstance< + IAssemblyDefinition, IModelInstanceAbsolute, INamedModelInstanceAbsolute, IFieldInstanceAbsolute, @@ -88,23 +86,6 @@ public XmlModelContainer getModelContainer() { return ObjectUtils.notNull(modelContainer.get()); } - @Override - public String getGroupAsName() { - // a choice does not have a groups-as name - return null; - } - - @Override - public String getJsonKeyFlagName() { - // choices do not have a JSON key flag - return null; - } - - @Override - public boolean hasChildren() { - return !getModelContainer().getModelInstances().isEmpty(); - } - // ---------------------------------------- // - Start XmlBeans driven code - CPD-OFF - // ---------------------------------------- @@ -131,36 +112,33 @@ public MarkupMultiline getRemarks() { @SuppressWarnings("PMD.UseConcurrentHashMap") @NonNull - private static final XmlObjectParser> XML_MODEL_PARSER - = new XmlObjectParser<>(ObjectUtils.notNull( - Map.ofEntries( - Map.entry(new QName(IModule.XML_NAMESPACE, "assembly"), - XmlChoiceInstance::handleAssembly), - Map.entry(new QName(IModule.XML_NAMESPACE, "define-assembly"), - XmlChoiceInstance::handleDefineAssembly), - Map.entry(new QName(IModule.XML_NAMESPACE, "field"), - XmlChoiceInstance::handleField), - Map.entry(new QName(IModule.XML_NAMESPACE, "define-field"), - XmlChoiceInstance::handleDefineField)))) { - - @Override - protected Handler> - identifyHandler(XmlCursor cursor, XmlObject obj) { - Handler> retval; - if (obj instanceof FieldReferenceType) { - retval = XmlChoiceInstance::handleField; - } else if (obj instanceof InlineFieldDefinitionType) { - retval = XmlChoiceInstance::handleDefineField; - } else if (obj instanceof AssemblyReferenceType) { - retval = XmlChoiceInstance::handleAssembly; - } else if (obj instanceof InlineAssemblyDefinitionType) { - retval = XmlChoiceInstance::handleDefineAssembly; - } else { - retval = super.identifyHandler(cursor, obj); - } - return retval; - } - }; + private static final XmlObjectParser> XML_MODEL_PARSER + = new XmlObjectParser<>(ObjectUtils.notNull( + Map.ofEntries(Map.entry(new QName(IModule.XML_NAMESPACE, "assembly"), XmlChoiceInstance::handleAssembly), + Map.entry(new QName(IModule.XML_NAMESPACE, "define-assembly"), + XmlChoiceInstance::handleDefineAssembly), + Map.entry(new QName(IModule.XML_NAMESPACE, "field"), XmlChoiceInstance::handleField), + Map.entry(new QName(IModule.XML_NAMESPACE, "define-field"), XmlChoiceInstance::handleDefineField)))) { + + @Override + protected Handler> identifyHandler(XmlCursor cursor, + XmlObject obj) { + Handler> retval; + if (obj instanceof FieldReferenceType) { + retval = XmlChoiceInstance::handleField; + } else if (obj instanceof InlineFieldDefinitionType) { + retval = XmlChoiceInstance::handleDefineField; + } else if (obj instanceof AssemblyReferenceType) { + retval = XmlChoiceInstance::handleAssembly; + } else if (obj instanceof InlineAssemblyDefinitionType) { + retval = XmlChoiceInstance::handleDefineAssembly; + } else { + retval = super.identifyHandler(cursor, obj); + } + return retval; + } + }; private static void handleField( // NOPMD false positive @NonNull XmlObject obj, diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/XmlFieldInstance.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/XmlFieldInstance.java index 4762fe67b..2db9e884c 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/XmlFieldInstance.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/XmlFieldInstance.java @@ -28,11 +28,10 @@ import gov.nist.secauto.metaschema.core.datatype.markup.MarkupLine; import gov.nist.secauto.metaschema.core.datatype.markup.MarkupMultiline; -import gov.nist.secauto.metaschema.core.model.AbstractInstance; +import gov.nist.secauto.metaschema.core.model.AbstractFieldInstance; import gov.nist.secauto.metaschema.core.model.IAssemblyDefinition; import gov.nist.secauto.metaschema.core.model.IAttributable; import gov.nist.secauto.metaschema.core.model.IContainerModel; -import gov.nist.secauto.metaschema.core.model.IFeatureDefinitionReferenceInstance; import gov.nist.secauto.metaschema.core.model.IFieldDefinition; import gov.nist.secauto.metaschema.core.model.IFieldInstanceAbsolute; import gov.nist.secauto.metaschema.core.model.JsonGroupAsBehavior; @@ -49,9 +48,8 @@ import edu.umd.cs.findbugs.annotations.Nullable; class XmlFieldInstance - extends AbstractInstance - implements IFieldInstanceAbsolute, - IFeatureDefinitionReferenceInstance { + extends AbstractFieldInstance + implements IFieldInstanceAbsolute { @NonNull private final FieldReferenceType xmlObject; @Nullable @@ -66,7 +64,7 @@ class XmlFieldInstance * @param container * the parent container, either a choice or assembly */ - @SuppressWarnings("PMD.NullAssignment") + @SuppressWarnings({ "PMD.NullAssignment", "PMD.ConstructorCallsOverridableMethod" }) public XmlFieldInstance( @NonNull FieldReferenceType xmlObject, @NonNull IContainerModel container) { @@ -77,17 +75,6 @@ public XmlFieldInstance( : null; } - @Override - public final IFieldDefinition getDefinition() { - // this will always be not null - return ObjectUtils.notNull(getContainingModule().getScopedFieldDefinitionByName(getName())); - } - - @Override - public IAssemblyDefinition getContainingDefinition() { - return getParentContainer().getOwningDefinition(); - } - // ---------------------------------------- // - Start XmlBeans driven code - CPD-OFF - // ---------------------------------------- @@ -97,6 +84,7 @@ public IAssemblyDefinition getContainingDefinition() { * * @return the underlying XML data */ + @NonNull protected final FieldReferenceType getXmlObject() { return xmlObject; } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/XmlFlagContainerSupport.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/XmlFlagContainerSupport.java index 6a9ff11ca..5f3c64b7e 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/XmlFlagContainerSupport.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/XmlFlagContainerSupport.java @@ -27,7 +27,9 @@ package gov.nist.secauto.metaschema.core.model.xml.impl; import gov.nist.secauto.metaschema.core.model.IAssemblyDefinition; +import gov.nist.secauto.metaschema.core.model.IContainerFlagSupport; import gov.nist.secauto.metaschema.core.model.IFieldDefinition; +import gov.nist.secauto.metaschema.core.model.IFlagContainerBuilder; import gov.nist.secauto.metaschema.core.model.IFlagInstance; import gov.nist.secauto.metaschema.core.model.IModelDefinition; import gov.nist.secauto.metaschema.core.model.IModule; @@ -39,14 +41,12 @@ import gov.nist.secauto.metaschema.core.model.xml.xmlbeans.InlineAssemblyDefinitionType; import gov.nist.secauto.metaschema.core.model.xml.xmlbeans.InlineFieldDefinitionType; import gov.nist.secauto.metaschema.core.model.xml.xmlbeans.InlineFlagDefinitionType; -import gov.nist.secauto.metaschema.core.util.CollectionUtil; import gov.nist.secauto.metaschema.core.util.ObjectUtils; import org.apache.commons.lang3.tuple.Pair; import org.apache.xmlbeans.XmlCursor; import org.apache.xmlbeans.XmlObject; -import java.util.LinkedHashMap; import java.util.Map; import javax.xml.namespace.QName; @@ -54,12 +54,10 @@ import edu.umd.cs.findbugs.annotations.NonNull; import edu.umd.cs.findbugs.annotations.Nullable; -@SuppressWarnings("PMD.NullAssignment") // readability -class XmlFlagContainerSupport - extends DefaultContainerFlagSupport { +final class XmlFlagContainerSupport { @SuppressWarnings("PMD.UseConcurrentHashMap") @NonNull - private static final XmlObjectParser>> XML_MODEL_PARSER + private static final XmlObjectParser>> XML_MODEL_PARSER = new XmlObjectParser<>(ObjectUtils.notNull( Map.ofEntries( Map.entry(new QName(IModule.XML_NAMESPACE, "flag"), @@ -68,9 +66,10 @@ class XmlFlagContainerSupport XmlFlagContainerSupport::handleDefineFlag)))) { @Override - protected Handler>> identifyHandler(XmlCursor cursor, + protected Handler>> identifyHandler( + XmlCursor cursor, XmlObject obj) { - Handler>> retval; + Handler>> retval; if (obj instanceof FlagReferenceType) { retval = XmlFlagContainerSupport::handleFlag; } else if (obj instanceof InlineFlagDefinitionType) { @@ -84,154 +83,163 @@ protected Handler>> identifyHan private static void handleFlag( // NOPMD false positive @NonNull XmlObject obj, - Pair> state) { + Pair> state) { XmlFlagInstance flagInstance = new XmlFlagInstance( (FlagReferenceType) obj, ObjectUtils.notNull(state.getLeft())); - state.getRight().put(flagInstance.getXmlQName(), flagInstance); + state.getRight().flag(flagInstance); } private static void handleDefineFlag( // NOPMD false positive @NonNull XmlObject obj, - Pair> state) { + Pair> state) { XmlInlineFlagDefinition flagInstance = new XmlInlineFlagDefinition( (InlineFlagDefinitionType) obj, ObjectUtils.notNull(state.getLeft())); - state.getRight().put(flagInstance.getXmlQName(), flagInstance); - } - - protected XmlFlagContainerSupport( - @NonNull Map flagInstanceMap, - @Nullable QName jsonKeyFlagName) { - super(flagInstanceMap, - jsonKeyFlagName == null ? null : ObjectUtils.requireNonNull(flagInstanceMap.get(jsonKeyFlagName))); + state.getRight().flag(flagInstance); } /** - * Generate a set of constraints from the provided XMLBeans instance. + * Generate a flag container from the provided XMLBeans instance. * * @param xmlField * the XMLBeans instance * @param container * the field containing the flag */ - public XmlFlagContainerSupport( + static IContainerFlagSupport newInstance( @NonNull GlobalFieldDefinitionType xmlField, @NonNull IFieldDefinition container) { - this( - xmlField.getFlagList().size() > 0 || xmlField.getDefineFlagList().size() > 0 - ? parseLocalFlags(xmlField, container) - : CollectionUtil.emptyMap(), - xmlField.isSetJsonKey() - ? container.getContainingModule().toFlagQName(xmlField.getJsonKey().getFlagRef()) - : null); + if (!xmlField.getFlagList().isEmpty() || !xmlField.getDefineFlagList().isEmpty()) { + IFlagContainerBuilder builder = xmlField.isSetJsonKey() + ? IContainerFlagSupport.builder(container.getContainingModule().toFlagQName( + ObjectUtils.requireNonNull(xmlField.getJsonKey().getFlagRef()))) + : IContainerFlagSupport.builder(); + parseLocalFlags(xmlField, container, builder); + return builder.build(); + } + return IContainerFlagSupport.empty(); } /** - * Generate a set of constraints from the provided XMLBeans instance. + * Generate a flag container from the provided XMLBeans instance. * * @param xmlField * the XMLBeans instance * @param container * the field containing the flag */ - public XmlFlagContainerSupport( + static IContainerFlagSupport newInstance( @NonNull InlineFieldDefinitionType xmlField, @NonNull IFieldDefinition container) { - this( - xmlField.getFlagList().size() > 0 || xmlField.getDefineFlagList().size() > 0 - ? parseLocalFlags(xmlField, container) - : CollectionUtil.emptyMap(), - xmlField.isSetJsonKey() - ? container.getContainingModule().toFlagQName(xmlField.getJsonKey().getFlagRef()) - : null); + if (!xmlField.getFlagList().isEmpty() || !xmlField.getDefineFlagList().isEmpty()) { + IFlagContainerBuilder builder = xmlField.isSetJsonKey() + ? IContainerFlagSupport.builder(container.getContainingModule().toFlagQName( + ObjectUtils.requireNonNull(xmlField.getJsonKey().getFlagRef()))) + : IContainerFlagSupport.builder(); + parseLocalFlags(xmlField, container, builder); + return builder.build(); + } + return IContainerFlagSupport.empty(); } /** - * Generate a set of constraints from the provided XMLBeans instance. + * Generate a flag container from the provided XMLBeans instance. * * @param xmlField * the XMLBeans instance * @param container * the field containing the flag */ - public XmlFlagContainerSupport( + static IContainerFlagSupport newInstance( @NonNull GroupedInlineFieldDefinitionType xmlField, - @NonNull IFieldDefinition container) { - this( - xmlField.getFlagList().size() > 0 || xmlField.getDefineFlagList().size() > 0 - ? parseLocalFlags(xmlField, container) - : CollectionUtil.emptyMap(), - null); + @NonNull IFieldDefinition container, + @Nullable String jsonKeyName) { + if (!xmlField.getFlagList().isEmpty() || !xmlField.getDefineFlagList().isEmpty()) { + IFlagContainerBuilder builder = jsonKeyName == null + ? IContainerFlagSupport.builder() + : IContainerFlagSupport.builder(container.getContainingModule().toFlagQName(jsonKeyName)); + parseLocalFlags(xmlField, container, builder); + return builder.build(); + } + return IContainerFlagSupport.empty(); } /** - * Generate a set of constraints from the provided XMLBeans instance. + * Generate a flag container from the provided XMLBeans instance. * - * @param xmlAssembly + * @param xmlField * the XMLBeans instance * @param container - * the assembly containing the flag + * the field containing the flag */ - public XmlFlagContainerSupport( + static IContainerFlagSupport newInstance( @NonNull GlobalAssemblyDefinitionType xmlAssembly, @NonNull IAssemblyDefinition container) { - this( - xmlAssembly.getFlagList().size() > 0 || xmlAssembly.getDefineFlagList().size() > 0 - ? parseLocalFlags(xmlAssembly, container) - : CollectionUtil.emptyMap(), - xmlAssembly.isSetJsonKey() - ? container.getContainingModule().toFlagQName(xmlAssembly.getJsonKey().getFlagRef()) - : null); + if (!xmlAssembly.getFlagList().isEmpty() || !xmlAssembly.getDefineFlagList().isEmpty()) { + IFlagContainerBuilder builder = xmlAssembly.isSetJsonKey() + ? IContainerFlagSupport.builder(container.getContainingModule().toFlagQName( + ObjectUtils.requireNonNull(xmlAssembly.getJsonKey().getFlagRef()))) + : IContainerFlagSupport.builder(); + parseLocalFlags(xmlAssembly, container, builder); + return builder.build(); + } + return IContainerFlagSupport.empty(); } /** - * Generate a set of constraints from the provided XMLBeans instance. + * Generate a flag container from the provided XMLBeans instance. * - * @param xmlAssembly + * @param xmlField * the XMLBeans instance * @param container - * the assembly containing the flag + * the field containing the flag */ - public XmlFlagContainerSupport( + static IContainerFlagSupport newInstance( @NonNull InlineAssemblyDefinitionType xmlAssembly, @NonNull IAssemblyDefinition container) { - this( - xmlAssembly.getFlagList().size() > 0 || xmlAssembly.getDefineFlagList().size() > 0 - ? parseLocalFlags(xmlAssembly, container) - : CollectionUtil.emptyMap(), - xmlAssembly.isSetJsonKey() - ? container.getContainingModule().toFlagQName(xmlAssembly.getJsonKey().getFlagRef()) - : null); + if (!xmlAssembly.getFlagList().isEmpty() || !xmlAssembly.getDefineFlagList().isEmpty()) { + IFlagContainerBuilder builder = xmlAssembly.isSetJsonKey() + ? IContainerFlagSupport.builder(container.getContainingModule().toFlagQName( + ObjectUtils.requireNonNull(xmlAssembly.getJsonKey().getFlagRef()))) + : IContainerFlagSupport.builder(); + parseLocalFlags(xmlAssembly, container, builder); + return builder.build(); + } + return IContainerFlagSupport.empty(); } /** - * Generate a set of constraints from the provided XMLBeans instance. + * Generate a flag container from the provided XMLBeans instance. * - * @param xmlAssembly + * @param xmlField * the XMLBeans instance * @param container - * the assembly containing the flag + * the field containing the flag */ - public XmlFlagContainerSupport( + static IContainerFlagSupport newInstance( @NonNull GroupedInlineAssemblyDefinitionType xmlAssembly, - @NonNull IAssemblyDefinition container) { - this( - xmlAssembly.getFlagList().size() > 0 || xmlAssembly.getDefineFlagList().size() > 0 - ? parseLocalFlags(xmlAssembly, container) - : CollectionUtil.emptyMap(), - null); + @NonNull IAssemblyDefinition container, + @Nullable String jsonKeyName) { + if (!xmlAssembly.getFlagList().isEmpty() || !xmlAssembly.getDefineFlagList().isEmpty()) { + IFlagContainerBuilder builder = jsonKeyName == null + ? IContainerFlagSupport.builder() + : IContainerFlagSupport.builder(container.getContainingModule().toFlagQName(jsonKeyName)); + parseLocalFlags(xmlAssembly, container, builder); + return builder.build(); + } + return IContainerFlagSupport.empty(); } - @NonNull - private static Map parseLocalFlags( + private static void parseLocalFlags( @NonNull XmlObject xmlObject, - @NonNull IModelDefinition parent) { + @NonNull IModelDefinition parent, + @NonNull IFlagContainerBuilder builder) { // handle flags - Map flagInstances = new LinkedHashMap<>(); // NOPMD - intentional - - XML_MODEL_PARSER.parse(xmlObject, Pair.of(parent, flagInstances)); + XML_MODEL_PARSER.parse(xmlObject, Pair.of(parent, builder)); + } - return flagInstances.isEmpty() ? CollectionUtil.emptyMap() : CollectionUtil.unmodifiableMap(flagInstances); + private XmlFlagContainerSupport() { + // disable construction } } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/XmlFlagInstance.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/XmlFlagInstance.java index ad8ba9511..895c3fe29 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/XmlFlagInstance.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/XmlFlagInstance.java @@ -28,9 +28,8 @@ import gov.nist.secauto.metaschema.core.datatype.markup.MarkupLine; import gov.nist.secauto.metaschema.core.datatype.markup.MarkupMultiline; -import gov.nist.secauto.metaschema.core.model.AbstractInstance; +import gov.nist.secauto.metaschema.core.model.AbstractFlagInstance; import gov.nist.secauto.metaschema.core.model.IAttributable; -import gov.nist.secauto.metaschema.core.model.IFeatureDefinitionReferenceInstance; import gov.nist.secauto.metaschema.core.model.IFlagDefinition; import gov.nist.secauto.metaschema.core.model.IFlagInstance; import gov.nist.secauto.metaschema.core.model.IModelDefinition; @@ -46,9 +45,7 @@ import edu.umd.cs.findbugs.annotations.Nullable; class XmlFlagInstance - extends AbstractInstance - implements IFlagInstance, - IFeatureDefinitionReferenceInstance { + extends AbstractFlagInstance { @NonNull private final FlagReferenceType xmlFlag; @Nullable @@ -63,6 +60,7 @@ class XmlFlagInstance * @param parent * the field definition this object is an instance of */ + @SuppressWarnings("PMD.ConstructorCallsOverridableMethod") public XmlFlagInstance(@NonNull FlagReferenceType xmlObject, @NonNull IModelDefinition parent) { super(parent); this.xmlFlag = xmlObject; @@ -71,6 +69,10 @@ public XmlFlagInstance(@NonNull FlagReferenceType xmlObject, @NonNull IModelDefi : null; // NOPMD needed for final variable } + // ---------------------------------------- + // - Start XmlBeans driven code - CPD-OFF - + // ---------------------------------------- + /** * Get the underlying XML data. * @@ -80,22 +82,6 @@ protected final FlagReferenceType getXmlObject() { return xmlFlag; } - @Override - public final IFlagDefinition getDefinition() { - // this should always be not null - return ObjectUtils.requireNonNull( - getContainingDefinition().getContainingModule().getScopedFlagDefinitionByName(getName())); - } - - @Override - public final IModelDefinition getContainingDefinition() { - return getParentContainer(); - } - - // ---------------------------------------- - // - Start XmlBeans driven code - CPD-OFF - - // ---------------------------------------- - @Override public String getFormalName() { return getXmlObject().isSetFormalName() ? getXmlObject().getFormalName() : null; diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/XmlGlobalAssemblyDefinition.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/XmlGlobalAssemblyDefinition.java index 4e0aa165a..8959d04f3 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/XmlGlobalAssemblyDefinition.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/XmlGlobalAssemblyDefinition.java @@ -28,15 +28,15 @@ import gov.nist.secauto.metaschema.core.datatype.markup.MarkupLine; import gov.nist.secauto.metaschema.core.datatype.markup.MarkupMultiline; -import gov.nist.secauto.metaschema.core.model.IAssemblyDefinition; +import gov.nist.secauto.metaschema.core.model.AbstractGlobalAssemblyDefinition; +import gov.nist.secauto.metaschema.core.model.IAssemblyInstance; import gov.nist.secauto.metaschema.core.model.IAssemblyInstanceAbsolute; import gov.nist.secauto.metaschema.core.model.IAttributable; import gov.nist.secauto.metaschema.core.model.IChoiceGroupInstance; import gov.nist.secauto.metaschema.core.model.IChoiceInstance; +import gov.nist.secauto.metaschema.core.model.IContainerFlagSupport; import gov.nist.secauto.metaschema.core.model.IContainerModelAssemblySupport; import gov.nist.secauto.metaschema.core.model.IDefinition; -import gov.nist.secauto.metaschema.core.model.IFeatureContainerFlag; -import gov.nist.secauto.metaschema.core.model.IFeatureContainerModelAssembly; import gov.nist.secauto.metaschema.core.model.IFieldInstanceAbsolute; import gov.nist.secauto.metaschema.core.model.IFlagInstance; import gov.nist.secauto.metaschema.core.model.IModelInstanceAbsolute; @@ -58,22 +58,21 @@ @SuppressWarnings("PMD.CouplingBetweenObjects") class XmlGlobalAssemblyDefinition - implements IAssemblyDefinition, - IFeatureContainerModelAssembly< + extends AbstractGlobalAssemblyDefinition< + XmlModule, + IAssemblyInstance, + IFlagInstance, IModelInstanceAbsolute, INamedModelInstanceAbsolute, IFieldInstanceAbsolute, IAssemblyInstanceAbsolute, IChoiceInstance, - IChoiceGroupInstance>, - IFeatureContainerFlag { + IChoiceGroupInstance> { @NonNull private final GlobalAssemblyDefinitionType xmlAssembly; @NonNull - private final XmlModule metaschema; - @NonNull - private final Lazy flagContainer; + private final Lazy> flagContainer; @NonNull private final Lazy new XmlFlagContainerSupport(xmlObject, this))); + this.flagContainer = ObjectUtils.notNull(Lazy.lazy(() -> XmlFlagContainerSupport.newInstance(xmlObject, this))); this.modelContainer = ObjectUtils.notNull( Lazy.lazy(() -> XmlAssemblyModelContainer.of(xmlObject.getModel(), this))); this.constraints = ObjectUtils.notNull(Lazy.lazy(() -> { @@ -113,7 +112,7 @@ public XmlGlobalAssemblyDefinition( } @Override - public XmlFlagContainerSupport getFlagContainer() { + public IContainerFlagSupport getFlagContainer() { return ObjectUtils.notNull(flagContainer.get()); } @@ -133,16 +132,6 @@ public IModelConstrained getConstraintSupport() { return ObjectUtils.notNull(constraints.get()); } - @Override - public XmlModule getContainingModule() { - return metaschema; - } - - @Override - public IFlagInstance getJsonKeyFlagInstance() { - return getFlagContainer().getJsonKeyFlagInstance(); - } - // ---------------------------------------- // - Start XmlBeans driven code - CPD-OFF - // ---------------------------------------- diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/XmlGlobalFieldDefinition.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/XmlGlobalFieldDefinition.java index 2e5df30d5..299b0d86d 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/XmlGlobalFieldDefinition.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/XmlGlobalFieldDefinition.java @@ -30,10 +30,11 @@ import gov.nist.secauto.metaschema.core.datatype.adapter.MetaschemaDataTypeProvider; import gov.nist.secauto.metaschema.core.datatype.markup.MarkupLine; import gov.nist.secauto.metaschema.core.datatype.markup.MarkupMultiline; +import gov.nist.secauto.metaschema.core.model.AbstractGlobalFieldDefinition; import gov.nist.secauto.metaschema.core.model.IAttributable; +import gov.nist.secauto.metaschema.core.model.IContainerFlagSupport; import gov.nist.secauto.metaschema.core.model.IDefinition; -import gov.nist.secauto.metaschema.core.model.IFeatureContainerFlag; -import gov.nist.secauto.metaschema.core.model.IFieldDefinition; +import gov.nist.secauto.metaschema.core.model.IFieldInstance; import gov.nist.secauto.metaschema.core.model.IFlagInstance; import gov.nist.secauto.metaschema.core.model.ModuleScopeEnum; import gov.nist.secauto.metaschema.core.model.constraint.ISource; @@ -55,16 +56,13 @@ @SuppressWarnings({ "PMD.GodClass", "PMD.CouplingBetweenObjects" }) class XmlGlobalFieldDefinition - implements IFieldDefinition, - IFeatureContainerFlag { + extends AbstractGlobalFieldDefinition { @NonNull private final GlobalFieldDefinitionType xmlObject; - @NonNull - private final XmlModule module; @Nullable private final Object defaultValue; @NonNull - private final Lazy flagContainer; + private final Lazy> flagContainer; @NonNull private final Lazy constraints; @@ -78,14 +76,14 @@ class XmlGlobalFieldDefinition * the containing Metaschema module */ public XmlGlobalFieldDefinition(@NonNull GlobalFieldDefinitionType xmlObject, @NonNull XmlModule module) { + super(module); this.xmlObject = xmlObject; - this.module = module; Object defaultValue = null; if (xmlObject.isSetDefault()) { defaultValue = getJavaTypeAdapter().parse(ObjectUtils.requireNonNull(xmlObject.getDefault())); } this.defaultValue = defaultValue; - this.flagContainer = ObjectUtils.notNull(Lazy.lazy(() -> new XmlFlagContainerSupport(xmlObject, this))); + this.flagContainer = ObjectUtils.notNull(Lazy.lazy(() -> XmlFlagContainerSupport.newInstance(xmlObject, this))); this.constraints = ObjectUtils.notNull(Lazy.lazy(() -> { IValueConstrained retval = new ValueConstraintSet(); if (getXmlObject().isSetConstraint()) { @@ -103,7 +101,7 @@ public XmlGlobalFieldDefinition(@NonNull GlobalFieldDefinitionType xmlObject, @N */ @SuppressWarnings("null") @Override - public XmlFlagContainerSupport getFlagContainer() { + public IContainerFlagSupport getFlagContainer() { return flagContainer.get(); } @@ -119,21 +117,11 @@ public IValueConstrained getConstraintSupport() { return constraints.get(); } - @Override - public XmlModule getContainingModule() { - return module; - } - @Override public Object getDefaultValue() { return defaultValue; } - @Override - public IFlagInstance getJsonKeyFlagInstance() { - return getFlagContainer().getJsonKeyFlagInstance(); - } - // ---------------------------------------- // - Start XmlBeans driven code - CPD-OFF - // ---------------------------------------- diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/XmlGlobalFlagDefinition.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/XmlGlobalFlagDefinition.java index a3b5555c2..a9e14eda2 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/XmlGlobalFlagDefinition.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/XmlGlobalFlagDefinition.java @@ -30,9 +30,10 @@ import gov.nist.secauto.metaschema.core.datatype.adapter.MetaschemaDataTypeProvider; import gov.nist.secauto.metaschema.core.datatype.markup.MarkupLine; import gov.nist.secauto.metaschema.core.datatype.markup.MarkupMultiline; +import gov.nist.secauto.metaschema.core.model.AbstractGlobalFlagDefinition; import gov.nist.secauto.metaschema.core.model.IAttributable; import gov.nist.secauto.metaschema.core.model.IDefinition; -import gov.nist.secauto.metaschema.core.model.IFlagDefinition; +import gov.nist.secauto.metaschema.core.model.IFlagInstance; import gov.nist.secauto.metaschema.core.model.ModuleScopeEnum; import gov.nist.secauto.metaschema.core.model.constraint.ISource; import gov.nist.secauto.metaschema.core.model.constraint.IValueConstrained; @@ -49,11 +50,10 @@ import edu.umd.cs.findbugs.annotations.Nullable; import nl.talsmasoftware.lazy4j.Lazy; -class XmlGlobalFlagDefinition implements IFlagDefinition { +class XmlGlobalFlagDefinition + extends AbstractGlobalFlagDefinition { @NonNull private final GlobalFlagDefinitionType xmlFlag; - @NonNull - private final XmlModule module; @Nullable private final Object defaultValue; private final Lazy constraints; @@ -70,9 +70,8 @@ class XmlGlobalFlagDefinition implements IFlagDefinition { public XmlGlobalFlagDefinition( @NonNull GlobalFlagDefinitionType xmlFlag, @NonNull XmlModule module) { + super(module); this.xmlFlag = xmlFlag; - this.module = module; - Object defaultValue = null; if (xmlFlag.isSetDefault()) { defaultValue = getJavaTypeAdapter().parse(ObjectUtils.requireNonNull(xmlFlag.getDefault())); @@ -100,11 +99,6 @@ public IValueConstrained getConstraintSupport() { return constraints.get(); } - @Override - public XmlModule getContainingModule() { - return module; - } - @Override public Object getDefaultValue() { return defaultValue; diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/XmlGroupedAssemblyInstance.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/XmlGroupedAssemblyInstance.java index c40234c3a..9894df806 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/XmlGroupedAssemblyInstance.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/XmlGroupedAssemblyInstance.java @@ -28,12 +28,11 @@ import gov.nist.secauto.metaschema.core.datatype.markup.MarkupLine; import gov.nist.secauto.metaschema.core.datatype.markup.MarkupMultiline; -import gov.nist.secauto.metaschema.core.model.AbstractNamedModelInstanceGrouped; +import gov.nist.secauto.metaschema.core.model.AbstractAssemblyInstance; import gov.nist.secauto.metaschema.core.model.IAssemblyDefinition; import gov.nist.secauto.metaschema.core.model.IAssemblyInstanceGrouped; import gov.nist.secauto.metaschema.core.model.IAttributable; import gov.nist.secauto.metaschema.core.model.IChoiceGroupInstance; -import gov.nist.secauto.metaschema.core.model.IFeatureDefinitionReferenceInstance; import gov.nist.secauto.metaschema.core.model.xml.xmlbeans.GroupedAssemblyReferenceType; import gov.nist.secauto.metaschema.core.model.xml.xmlbeans.UseNameType; import gov.nist.secauto.metaschema.core.util.CollectionUtil; @@ -45,9 +44,12 @@ import edu.umd.cs.findbugs.annotations.NonNull; public class XmlGroupedAssemblyInstance - extends AbstractNamedModelInstanceGrouped - implements IAssemblyInstanceGrouped, - IFeatureDefinitionReferenceInstance { + extends AbstractAssemblyInstance< + IChoiceGroupInstance, + IAssemblyDefinition, + IAssemblyInstanceGrouped, + IAssemblyDefinition> + implements IAssemblyInstanceGrouped { @NonNull private final GroupedAssemblyReferenceType xmlObject; @@ -72,16 +74,11 @@ public XmlGroupedAssemblyInstance( * * @return the underlying XML data */ + @NonNull protected GroupedAssemblyReferenceType getXmlObject() { return xmlObject; } - @Override - public IAssemblyDefinition getDefinition() { - // this will always be not null - return ObjectUtils.notNull(getContainingModule().getScopedAssemblyDefinitionByName(getName())); - } - // ---------------------------------------- // - Start XmlBeans driven code - CPD-OFF - // ---------------------------------------- diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/XmlGroupedFieldInstance.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/XmlGroupedFieldInstance.java index 32471a5ea..d73189fb3 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/XmlGroupedFieldInstance.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/XmlGroupedFieldInstance.java @@ -28,10 +28,10 @@ import gov.nist.secauto.metaschema.core.datatype.markup.MarkupLine; import gov.nist.secauto.metaschema.core.datatype.markup.MarkupMultiline; -import gov.nist.secauto.metaschema.core.model.AbstractNamedModelInstanceGrouped; +import gov.nist.secauto.metaschema.core.model.AbstractFieldInstance; +import gov.nist.secauto.metaschema.core.model.IAssemblyDefinition; import gov.nist.secauto.metaschema.core.model.IAttributable; import gov.nist.secauto.metaschema.core.model.IChoiceGroupInstance; -import gov.nist.secauto.metaschema.core.model.IFeatureDefinitionReferenceInstance; import gov.nist.secauto.metaschema.core.model.IFieldDefinition; import gov.nist.secauto.metaschema.core.model.IFieldInstanceGrouped; import gov.nist.secauto.metaschema.core.model.xml.xmlbeans.GroupedFieldReferenceType; @@ -45,9 +45,8 @@ import edu.umd.cs.findbugs.annotations.NonNull; public class XmlGroupedFieldInstance - extends AbstractNamedModelInstanceGrouped - implements IFieldInstanceGrouped, - IFeatureDefinitionReferenceInstance { + extends AbstractFieldInstance + implements IFieldInstanceGrouped { @NonNull private final GroupedFieldReferenceType xmlObject; @@ -68,12 +67,6 @@ public XmlGroupedFieldInstance( this.xmlObject = xmlObject; } - @Override - public IFieldDefinition getDefinition() { - // this will always be not null - return ObjectUtils.notNull(getContainingModule().getScopedFieldDefinitionByName(getName())); - } - // ---------------------------------------- // - Start XmlBeans driven code - CPD-OFF - // ---------------------------------------- @@ -83,6 +76,7 @@ public IFieldDefinition getDefinition() { * * @return the underlying XML data */ + @NonNull protected GroupedFieldReferenceType getXmlObject() { return xmlObject; } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/XmlGroupedInlineAssemblyDefinition.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/XmlGroupedInlineAssemblyDefinition.java index e0241ef54..43a25750c 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/XmlGroupedInlineAssemblyDefinition.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/XmlGroupedInlineAssemblyDefinition.java @@ -28,17 +28,15 @@ import gov.nist.secauto.metaschema.core.datatype.markup.MarkupLine; import gov.nist.secauto.metaschema.core.datatype.markup.MarkupMultiline; -import gov.nist.secauto.metaschema.core.model.AbstractNamedModelInstanceGrouped; +import gov.nist.secauto.metaschema.core.model.AbstractInlineAssemblyDefinition; import gov.nist.secauto.metaschema.core.model.IAssemblyDefinition; import gov.nist.secauto.metaschema.core.model.IAssemblyInstanceAbsolute; import gov.nist.secauto.metaschema.core.model.IAssemblyInstanceGrouped; import gov.nist.secauto.metaschema.core.model.IAttributable; import gov.nist.secauto.metaschema.core.model.IChoiceGroupInstance; import gov.nist.secauto.metaschema.core.model.IChoiceInstance; +import gov.nist.secauto.metaschema.core.model.IContainerFlagSupport; import gov.nist.secauto.metaschema.core.model.IContainerModelAssemblySupport; -import gov.nist.secauto.metaschema.core.model.IFeatureContainerFlag; -import gov.nist.secauto.metaschema.core.model.IFeatureContainerModelAssembly; -import gov.nist.secauto.metaschema.core.model.IFeatureDefinitionInstanceInlined; import gov.nist.secauto.metaschema.core.model.IFieldInstanceAbsolute; import gov.nist.secauto.metaschema.core.model.IFlagInstance; import gov.nist.secauto.metaschema.core.model.IModelInstanceAbsolute; @@ -57,22 +55,28 @@ import nl.talsmasoftware.lazy4j.Lazy; public class XmlGroupedInlineAssemblyDefinition - extends AbstractNamedModelInstanceGrouped - implements IAssemblyInstanceGrouped, IAssemblyDefinition, - IFeatureContainerModelAssembly< + extends AbstractInlineAssemblyDefinition< + IChoiceGroupInstance, + IAssemblyDefinition, + IAssemblyInstanceGrouped, + IAssemblyDefinition, + IFlagInstance, IModelInstanceAbsolute, INamedModelInstanceAbsolute, IFieldInstanceAbsolute, IAssemblyInstanceAbsolute, IChoiceInstance, - IChoiceGroupInstance>, - IFeatureContainerFlag, - IFeatureDefinitionInstanceInlined { + IChoiceGroupInstance> + implements IAssemblyInstanceGrouped { + + // ---------------------------------------- + // - Start XmlBeans driven code - CPD-OFF - + // ---------------------------------------- @NonNull private final GroupedInlineAssemblyDefinitionType xmlObject; @NonNull - private final Lazy flagContainer; + private final Lazy> flagContainer; @NonNull private final Lazy new XmlFlagContainerSupport(xmlObject, this))); + this.flagContainer = ObjectUtils.notNull(Lazy.lazy(() -> XmlFlagContainerSupport.newInstance( + xmlObject, + this, + parent.getJsonKeyFlagInstanceName()))); this.modelContainer = ObjectUtils.notNull( Lazy.lazy(() -> XmlAssemblyModelContainer.of(xmlObject.getModel(), this))); this.constraints = ObjectUtils.notNull(Lazy.lazy(() -> { @@ -113,17 +120,7 @@ public XmlGroupedInlineAssemblyDefinition( } @Override - public IAssemblyDefinition getDefinition() { - return this; - } - - @Override - public IAssemblyInstanceGrouped getInlineInstance() { - return this; - } - - @Override - public XmlFlagContainerSupport getFlagContainer() { + public IContainerFlagSupport getFlagContainer() { return ObjectUtils.notNull(flagContainer.get()); } @@ -144,15 +141,6 @@ public IModelConstrained getConstraintSupport() { return ObjectUtils.notNull(constraints.get()); } - @Override - public IFlagInstance getJsonKeyFlagInstance() { - return getFlagContainer().getJsonKeyFlagInstance(); - } - - // ---------------------------------------- - // - Start XmlBeans driven code - CPD-OFF - - // ---------------------------------------- - /** * Get the underlying XML model. * diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/XmlGroupedInlineFieldDefinition.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/XmlGroupedInlineFieldDefinition.java index 99ed78671..739e2d619 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/XmlGroupedInlineFieldDefinition.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/XmlGroupedInlineFieldDefinition.java @@ -30,11 +30,11 @@ import gov.nist.secauto.metaschema.core.datatype.adapter.MetaschemaDataTypeProvider; import gov.nist.secauto.metaschema.core.datatype.markup.MarkupLine; import gov.nist.secauto.metaschema.core.datatype.markup.MarkupMultiline; -import gov.nist.secauto.metaschema.core.model.AbstractNamedModelInstanceGrouped; +import gov.nist.secauto.metaschema.core.model.AbstractInlineFieldDefinition; +import gov.nist.secauto.metaschema.core.model.IAssemblyDefinition; import gov.nist.secauto.metaschema.core.model.IAttributable; import gov.nist.secauto.metaschema.core.model.IChoiceGroupInstance; -import gov.nist.secauto.metaschema.core.model.IFeatureContainerFlag; -import gov.nist.secauto.metaschema.core.model.IFeatureDefinitionInstanceInlined; +import gov.nist.secauto.metaschema.core.model.IContainerFlagSupport; import gov.nist.secauto.metaschema.core.model.IFieldDefinition; import gov.nist.secauto.metaschema.core.model.IFieldInstanceGrouped; import gov.nist.secauto.metaschema.core.model.IFlagInstance; @@ -54,15 +54,18 @@ import nl.talsmasoftware.lazy4j.Lazy; public class XmlGroupedInlineFieldDefinition - extends AbstractNamedModelInstanceGrouped - implements IFieldInstanceGrouped, IFieldDefinition, - IFeatureDefinitionInstanceInlined, - IFeatureContainerFlag { + extends AbstractInlineFieldDefinition< + IChoiceGroupInstance, + IFieldDefinition, + IFieldInstanceGrouped, + IAssemblyDefinition, + IFlagInstance> + implements IFieldInstanceGrouped { @NonNull private final GroupedInlineFieldDefinitionType xmlObject; @NonNull - private final Lazy flagContainer; + private final Lazy> flagContainer; @NonNull private final Lazy constraints; @@ -81,7 +84,10 @@ public XmlGroupedInlineFieldDefinition( @NonNull IChoiceGroupInstance parent) { super(parent); this.xmlObject = xmlObject; - this.flagContainer = ObjectUtils.notNull(Lazy.lazy(() -> new XmlFlagContainerSupport(xmlObject, this))); + this.flagContainer = ObjectUtils.notNull(Lazy.lazy(() -> XmlFlagContainerSupport.newInstance( + xmlObject, + this, + parent.getJsonKeyFlagInstanceName()))); this.constraints = ObjectUtils.notNull(Lazy.lazy(() -> { IValueConstrained retval = new ValueConstraintSet(); if (getXmlObject().isSetConstraint()) { @@ -93,17 +99,7 @@ public XmlGroupedInlineFieldDefinition( } @Override - public IFieldDefinition getDefinition() { - return this; - } - - @Override - public XmlGroupedInlineFieldDefinition getInlineInstance() { - return this; - } - - @Override - public XmlFlagContainerSupport getFlagContainer() { + public IContainerFlagSupport getFlagContainer() { return ObjectUtils.notNull(flagContainer.get()); } @@ -112,10 +108,6 @@ public IValueConstrained getConstraintSupport() { return ObjectUtils.notNull(constraints.get()); } - @Override - public IFlagInstance getJsonKeyFlagInstance() { - return getFlagContainer().getJsonKeyFlagInstance(); - } // ---------------------------------------- // - Start XmlBeans driven code - CPD-OFF - // ---------------------------------------- diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/XmlInlineAssemblyDefinition.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/XmlInlineAssemblyDefinition.java index c7ea0532d..af3d1fd92 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/XmlInlineAssemblyDefinition.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/XmlInlineAssemblyDefinition.java @@ -28,17 +28,15 @@ import gov.nist.secauto.metaschema.core.datatype.markup.MarkupLine; import gov.nist.secauto.metaschema.core.datatype.markup.MarkupMultiline; -import gov.nist.secauto.metaschema.core.model.AbstractInstance; +import gov.nist.secauto.metaschema.core.model.AbstractInlineAssemblyDefinition; import gov.nist.secauto.metaschema.core.model.IAssemblyDefinition; import gov.nist.secauto.metaschema.core.model.IAssemblyInstanceAbsolute; import gov.nist.secauto.metaschema.core.model.IAttributable; import gov.nist.secauto.metaschema.core.model.IChoiceGroupInstance; import gov.nist.secauto.metaschema.core.model.IChoiceInstance; +import gov.nist.secauto.metaschema.core.model.IContainerFlagSupport; import gov.nist.secauto.metaschema.core.model.IContainerModel; import gov.nist.secauto.metaschema.core.model.IContainerModelAssemblySupport; -import gov.nist.secauto.metaschema.core.model.IFeatureContainerFlag; -import gov.nist.secauto.metaschema.core.model.IFeatureContainerModelAssembly; -import gov.nist.secauto.metaschema.core.model.IFeatureDefinitionInstanceInlined; import gov.nist.secauto.metaschema.core.model.IFieldInstanceAbsolute; import gov.nist.secauto.metaschema.core.model.IFlagInstance; import gov.nist.secauto.metaschema.core.model.IModelInstanceAbsolute; @@ -62,21 +60,28 @@ * Represents a Metaschema assembly definition declared locally as an instance. */ class XmlInlineAssemblyDefinition - extends AbstractInstance - implements IAssemblyInstanceAbsolute, IAssemblyDefinition, - IFeatureContainerModelAssembly< + extends AbstractInlineAssemblyDefinition< + IContainerModel, + IAssemblyDefinition, + IAssemblyInstanceAbsolute, + IAssemblyDefinition, + IFlagInstance, IModelInstanceAbsolute, INamedModelInstanceAbsolute, IFieldInstanceAbsolute, IAssemblyInstanceAbsolute, IChoiceInstance, - IChoiceGroupInstance>, - IFeatureContainerFlag, - IFeatureDefinitionInstanceInlined { + IChoiceGroupInstance> + implements IAssemblyInstanceAbsolute { + + // ---------------------------------------- + // - Start XmlBeans driven code - CPD-OFF - + // ---------------------------------------- + @NonNull private final InlineAssemblyDefinitionType xmlObject; @NonNull - private final Lazy flagContainer; + private final Lazy> flagContainer; @NonNull private final Lazy new XmlFlagContainerSupport(xmlObject, this))); + this.flagContainer = ObjectUtils.notNull(Lazy.lazy(() -> XmlFlagContainerSupport.newInstance(xmlObject, this))); this.modelContainer = ObjectUtils.notNull(Lazy.lazy(() -> XmlAssemblyModelContainer.of(xmlObject.getModel(), this))); this.constraints = ObjectUtils.notNull(Lazy.lazy(() -> { @@ -115,20 +120,9 @@ public XmlInlineAssemblyDefinition( })); } - @Override - public IAssemblyDefinition getDefinition() { - return this; - } - - @Override - @NonNull - public IAssemblyInstanceAbsolute getInlineInstance() { - return this; - } - @SuppressWarnings("null") @Override - public XmlFlagContainerSupport getFlagContainer() { + public IContainerFlagSupport getFlagContainer() { return flagContainer.get(); } @@ -150,15 +144,6 @@ public IModelConstrained getConstraintSupport() { return constraints.get(); } - @Override - public IFlagInstance getJsonKeyFlagInstance() { - return getFlagContainer().getJsonKeyFlagInstance(); - } - - // ---------------------------------------- - // - Start XmlBeans driven code - CPD-OFF - - // ---------------------------------------- - /** * Get the underlying XML model. * diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/XmlInlineFieldDefinition.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/XmlInlineFieldDefinition.java index 2cf746f64..a2029c40a 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/XmlInlineFieldDefinition.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/XmlInlineFieldDefinition.java @@ -30,11 +30,11 @@ import gov.nist.secauto.metaschema.core.datatype.adapter.MetaschemaDataTypeProvider; import gov.nist.secauto.metaschema.core.datatype.markup.MarkupLine; import gov.nist.secauto.metaschema.core.datatype.markup.MarkupMultiline; -import gov.nist.secauto.metaschema.core.model.AbstractInstance; +import gov.nist.secauto.metaschema.core.model.AbstractInlineFieldDefinition; +import gov.nist.secauto.metaschema.core.model.IAssemblyDefinition; import gov.nist.secauto.metaschema.core.model.IAttributable; +import gov.nist.secauto.metaschema.core.model.IContainerFlagSupport; import gov.nist.secauto.metaschema.core.model.IContainerModel; -import gov.nist.secauto.metaschema.core.model.IFeatureContainerFlag; -import gov.nist.secauto.metaschema.core.model.IFeatureDefinitionInstanceInlined; import gov.nist.secauto.metaschema.core.model.IFieldDefinition; import gov.nist.secauto.metaschema.core.model.IFieldInstanceAbsolute; import gov.nist.secauto.metaschema.core.model.IFlagInstance; @@ -57,16 +57,19 @@ import nl.talsmasoftware.lazy4j.Lazy; class XmlInlineFieldDefinition - extends AbstractInstance - implements IFieldInstanceAbsolute, IFieldDefinition, - IFeatureContainerFlag, - IFeatureDefinitionInstanceInlined { + extends AbstractInlineFieldDefinition< + IContainerModel, + IFieldDefinition, + IFieldInstanceAbsolute, + IAssemblyDefinition, + IFlagInstance> + implements IFieldInstanceAbsolute { @NonNull private final InlineFieldDefinitionType xmlObject; @Nullable private final Object defaultValue; @NonNull - private final Lazy flagContainer; + private final Lazy> flagContainer; @NonNull private final Lazy constraints; @@ -88,7 +91,7 @@ public XmlInlineFieldDefinition( this.defaultValue = xmlObject.isSetDefault() ? getJavaTypeAdapter().parse(ObjectUtils.requireNonNull(xmlObject.getDefault())) : null; - this.flagContainer = ObjectUtils.notNull(Lazy.lazy(() -> new XmlFlagContainerSupport(xmlObject, this))); + this.flagContainer = ObjectUtils.notNull(Lazy.lazy(() -> XmlFlagContainerSupport.newInstance(xmlObject, this))); this.constraints = ObjectUtils.notNull(Lazy.lazy(() -> { IValueConstrained retval = new ValueConstraintSet(); if (getXmlObject().isSetConstraint()) { @@ -113,28 +116,12 @@ protected final InlineFieldDefinitionType getXmlObject() { return xmlObject; } - @Override - public IFieldDefinition getDefinition() { - return this; - } - - @Override - @NonNull - public IFieldInstanceAbsolute getInlineInstance() { - return this; - } - @SuppressWarnings("null") @Override - public XmlFlagContainerSupport getFlagContainer() { + public IContainerFlagSupport getFlagContainer() { return flagContainer.get(); } - @Override - public IFlagInstance getJsonKeyFlagInstance() { - return getFlagContainer().getJsonKeyFlagInstance(); - } - /** * Used to generate the instances for the constraints in a lazy fashion when the * constraints are first accessed. @@ -244,6 +231,11 @@ public String getJsonValueKeyName() { return getXmlObject().getJsonValueKey(); } + // @Override + // public String getJsonKeyFlagInstanceName() { + // return IFeatureXmlContainerFlag.super.getJsonKeyFlagInstanceName(); + // } + // ------------------------------------- // - End XmlBeans driven code - CPD-ON - // ------------------------------------- diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/XmlInlineFlagDefinition.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/XmlInlineFlagDefinition.java index 00702587b..12a99b3de 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/XmlInlineFlagDefinition.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/XmlInlineFlagDefinition.java @@ -30,9 +30,8 @@ import gov.nist.secauto.metaschema.core.datatype.adapter.MetaschemaDataTypeProvider; import gov.nist.secauto.metaschema.core.datatype.markup.MarkupLine; import gov.nist.secauto.metaschema.core.datatype.markup.MarkupMultiline; -import gov.nist.secauto.metaschema.core.model.AbstractInstance; +import gov.nist.secauto.metaschema.core.model.AbstractInlineFlagDefinition; import gov.nist.secauto.metaschema.core.model.IAttributable; -import gov.nist.secauto.metaschema.core.model.IFeatureDefinitionInstanceInlined; import gov.nist.secauto.metaschema.core.model.IFlagDefinition; import gov.nist.secauto.metaschema.core.model.IFlagInstance; import gov.nist.secauto.metaschema.core.model.IModelDefinition; @@ -51,9 +50,8 @@ import nl.talsmasoftware.lazy4j.Lazy; class XmlInlineFlagDefinition - extends AbstractInstance - implements IFlagInstance, IFlagDefinition, - IFeatureDefinitionInstanceInlined { + extends AbstractInlineFlagDefinition { + @NonNull private final InlineFlagDefinitionType xmlFlag; @Nullable @@ -88,17 +86,6 @@ public XmlInlineFlagDefinition(@NonNull InlineFlagDefinitionType xmlObject, @Non })); } - @Override - public IFlagDefinition getDefinition() { - return this; - } - - @Override - @NonNull - public IFlagInstance getInlineInstance() { - return this; - } - /** * Used to generate the instances for the constraints in a lazy fashion when the * constraints are first accessed. @@ -114,11 +101,6 @@ public Object getDefaultValue() { return defaultValue; } - @Override - public IModelDefinition getContainingDefinition() { - return getParentContainer(); - } - // ---------------------------------------- // - Start XmlBeans driven code - CPD-OFF - // ---------------------------------------- diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/XmlModule.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/XmlModule.java index f5e791e8b..7efcb9750 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/XmlModule.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/XmlModule.java @@ -32,9 +32,9 @@ import gov.nist.secauto.metaschema.core.model.IAssemblyDefinition; import gov.nist.secauto.metaschema.core.model.IFieldDefinition; import gov.nist.secauto.metaschema.core.model.IFlagDefinition; +import gov.nist.secauto.metaschema.core.model.IMetaschemaModule; import gov.nist.secauto.metaschema.core.model.IModelDefinition; import gov.nist.secauto.metaschema.core.model.MetaschemaException; -import gov.nist.secauto.metaschema.core.model.xml.IXmlModule; import gov.nist.secauto.metaschema.core.model.xml.xmlbeans.GlobalAssemblyDefinitionType; import gov.nist.secauto.metaschema.core.model.xml.xmlbeans.GlobalFieldDefinitionType; import gov.nist.secauto.metaschema.core.model.xml.xmlbeans.GlobalFlagDefinitionType; @@ -56,27 +56,27 @@ import java.util.stream.Collectors; import java.util.stream.Stream; +import javax.xml.namespace.QName; + import edu.umd.cs.findbugs.annotations.NonNull; +import nl.talsmasoftware.lazy4j.Lazy; @SuppressWarnings("PMD.CouplingBetweenObjects") public class XmlModule extends AbstractModule< - IXmlModule, + IMetaschemaModule, IModelDefinition, IFlagDefinition, IFieldDefinition, IAssemblyDefinition> - implements IXmlModule { + implements IMetaschemaModule { private static final Logger LOGGER = LogManager.getLogger(XmlModule.class); @NonNull private final URI location; @NonNull private final METASCHEMADocument module; - private final Map flagDefinitions; - private final Map fieldDefinitions; - private final Map assemblyDefinitions; - private final Map rootAssemblyDefinitions; + private final Lazy definitions; /** * Constructs a new Metaschema instance. @@ -93,81 +93,13 @@ public class XmlModule public XmlModule( // NOPMD - unavoidable @NonNull URI resource, @NonNull METASCHEMADocument moduleXml, - @NonNull List importedModules) throws MetaschemaException { + @NonNull List importedModules) throws MetaschemaException { super(importedModules); this.location = ObjectUtils.requireNonNull(resource, "resource"); Objects.requireNonNull(moduleXml.getMETASCHEMA()); this.module = moduleXml; - METASCHEMA metaschemaNode = module.getMETASCHEMA(); - - // handle definitions in this module - // TODO: switch implementation to use the XmlObjectParser - { - // start with flag definitions - try (XmlCursor cursor = metaschemaNode.newCursor()) { - cursor.selectPath("declare namespace m='http://csrc.nist.gov/ns/oscal/metaschema/1.0';$this/m:define-flag"); - - Map flagDefinitions = new LinkedHashMap<>(); // NOPMD - intentional - while (cursor.toNextSelection()) { - GlobalFlagDefinitionType obj = ObjectUtils.notNull((GlobalFlagDefinitionType) cursor.getObject()); - XmlGlobalFlagDefinition flag = new XmlGlobalFlagDefinition(obj, this); // NOPMD - intentional - if (LOGGER.isTraceEnabled()) { - LOGGER.trace("New flag definition '{}'", flag.toCoordinates()); - } - flagDefinitions.put(flag.getName(), flag); - } - this.flagDefinitions - = flagDefinitions.isEmpty() ? Collections.emptyMap() : Collections.unmodifiableMap(flagDefinitions); - } - } - - { - // now field definitions - try (XmlCursor cursor = metaschemaNode.newCursor()) { - cursor.selectPath("declare namespace m='http://csrc.nist.gov/ns/oscal/metaschema/1.0';$this/m:define-field"); - - Map fieldDefinitions = new LinkedHashMap<>(); // NOPMD - intentional - while (cursor.toNextSelection()) { - GlobalFieldDefinitionType obj = ObjectUtils.notNull((GlobalFieldDefinitionType) cursor.getObject()); - XmlGlobalFieldDefinition field = new XmlGlobalFieldDefinition(obj, this); // NOPMD - intentional - if (LOGGER.isTraceEnabled()) { - LOGGER.trace("New field definition '{}'", field.toCoordinates()); - } - fieldDefinitions.put(field.getName(), field); - } - this.fieldDefinitions - = fieldDefinitions.isEmpty() ? Collections.emptyMap() : Collections.unmodifiableMap(fieldDefinitions); - } - } - - { - // finally assembly definitions - Map assemblyDefinitions = new LinkedHashMap<>(); // NOPMD - intentional - Map rootAssemblyDefinitions = new LinkedHashMap<>(); // NOPMD - intentional - - try (XmlCursor cursor = metaschemaNode.newCursor()) { - cursor.selectPath( - "declare namespace m='http://csrc.nist.gov/ns/oscal/metaschema/1.0';$this/m:define-assembly"); - - while (cursor.toNextSelection()) { - GlobalAssemblyDefinitionType obj = ObjectUtils.notNull((GlobalAssemblyDefinitionType) cursor.getObject()); - XmlGlobalAssemblyDefinition assembly = new XmlGlobalAssemblyDefinition(obj, this); // NOPMD - intentional - if (LOGGER.isTraceEnabled()) { - LOGGER.trace("New assembly definition '{}'", assembly.toCoordinates()); - } - assemblyDefinitions.put(assembly.getName(), assembly); - if (assembly.isRoot()) { - rootAssemblyDefinitions.put(ObjectUtils.notNull(assembly.getRootName()), assembly); - } - } - - this.assemblyDefinitions - = assemblyDefinitions.isEmpty() ? Collections.emptyMap() : Collections.unmodifiableMap(assemblyDefinitions); - this.rootAssemblyDefinitions = rootAssemblyDefinitions.isEmpty() ? Collections.emptyMap() - : Collections.unmodifiableMap(rootAssemblyDefinitions); - } - } + this.definitions = Lazy.lazy(() -> new Definitions(ObjectUtils.requireNonNull(module.getMETASCHEMA()))); } @NonNull @@ -223,34 +155,31 @@ public URI getJsonBaseUri() { return URI.create(getXmlModule().getJsonBaseUri()); } - private Map getAssemblyDefinitionMap() { - return assemblyDefinitions; + @NonNull + private Definitions getDefinitions() { + return ObjectUtils.notNull(definitions.get()); } @SuppressWarnings("null") @Override public Collection getAssemblyDefinitions() { - return getAssemblyDefinitionMap().values(); + return getDefinitions().getAssemblyDefinitionMap().values(); } @Override - public IAssemblyDefinition getAssemblyDefinitionByName(@NonNull String name) { - return getAssemblyDefinitionMap().get(name); - } - - private Map getFieldDefinitionMap() { - return fieldDefinitions; + public IAssemblyDefinition getAssemblyDefinitionByName(@NonNull QName name) { + return getDefinitions().getAssemblyDefinitionMap().get(name); } @SuppressWarnings("null") @Override public Collection getFieldDefinitions() { - return getFieldDefinitionMap().values(); + return getDefinitions().getFieldDefinitionMap().values(); } @Override - public IFieldDefinition getFieldDefinitionByName(@NonNull String name) { - return getFieldDefinitionMap().get(name); + public IFieldDefinition getFieldDefinitionByName(@NonNull QName name) { + return getDefinitions().getFieldDefinitionMap().get(name); } @SuppressWarnings("null") @@ -260,28 +189,117 @@ public List getAssemblyAndFieldDefinitions() { .collect(Collectors.toList()); } - private Map getFlagDefinitionMap() { - return flagDefinitions; - } - @SuppressWarnings("null") @Override public Collection getFlagDefinitions() { - return getFlagDefinitionMap().values(); + return getDefinitions().getFlagDefinitionMap().values(); } @Override - public IFlagDefinition getFlagDefinitionByName(@NonNull String name) { - return getFlagDefinitionMap().get(name); - } - - private Map getRootAssemblyDefinitionMap() { - return rootAssemblyDefinitions; + public IFlagDefinition getFlagDefinitionByName(@NonNull QName name) { + return getDefinitions().getFlagDefinitionMap().get(name); } @SuppressWarnings("null") @Override public Collection getRootAssemblyDefinitions() { - return getRootAssemblyDefinitionMap().values(); + return getDefinitions().getRootAssemblyDefinitionMap().values(); + } + + private final class Definitions { + private final Map flagDefinitions; + private final Map fieldDefinitions; + private final Map assemblyDefinitions; + private final Map rootAssemblyDefinitions; + + @SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops") + private Definitions(@NonNull METASCHEMA metaschemaNode) { + + // handle definitions in this module + // TODO: switch implementation to use the XmlObjectParser + { + // start with flag definitions + try (XmlCursor cursor = metaschemaNode.newCursor()) { + cursor.selectPath("declare namespace m='http://csrc.nist.gov/ns/oscal/metaschema/1.0';$this/m:define-flag"); + + Map flagDefinitions = new LinkedHashMap<>(); // NOPMD - intentional + while (cursor.toNextSelection()) { + GlobalFlagDefinitionType obj = ObjectUtils.notNull((GlobalFlagDefinitionType) cursor.getObject()); + XmlGlobalFlagDefinition flag = new XmlGlobalFlagDefinition(obj, XmlModule.this); // NOPMD - intentional + if (LOGGER.isTraceEnabled()) { + LOGGER.trace("New flag definition '{}'", flag.toCoordinates()); + } + flagDefinitions.put(flag.getDefinitionQName(), flag); + } + this.flagDefinitions + = flagDefinitions.isEmpty() ? Collections.emptyMap() : Collections.unmodifiableMap(flagDefinitions); + } + } + + { + // now field definitions + try (XmlCursor cursor = metaschemaNode.newCursor()) { + cursor.selectPath("declare namespace m='http://csrc.nist.gov/ns/oscal/metaschema/1.0';$this/m:define-field"); + + Map fieldDefinitions = new LinkedHashMap<>(); // NOPMD - intentional + while (cursor.toNextSelection()) { + GlobalFieldDefinitionType obj = ObjectUtils.notNull((GlobalFieldDefinitionType) cursor.getObject()); + XmlGlobalFieldDefinition field = new XmlGlobalFieldDefinition(obj, XmlModule.this); // NOPMD - intentional + if (LOGGER.isTraceEnabled()) { + LOGGER.trace("New field definition '{}'", field.toCoordinates()); + } + fieldDefinitions.put(field.getDefinitionQName(), field); + } + this.fieldDefinitions + = fieldDefinitions.isEmpty() ? Collections.emptyMap() : Collections.unmodifiableMap(fieldDefinitions); + } + } + + { + // finally assembly definitions + Map assemblyDefinitions = new LinkedHashMap<>(); // NOPMD - intentional + Map rootAssemblyDefinitions = new LinkedHashMap<>(); // NOPMD - intentional + + try (XmlCursor cursor = metaschemaNode.newCursor()) { + cursor.selectPath( + "declare namespace m='http://csrc.nist.gov/ns/oscal/metaschema/1.0';$this/m:define-assembly"); + + while (cursor.toNextSelection()) { + GlobalAssemblyDefinitionType obj = ObjectUtils.notNull((GlobalAssemblyDefinitionType) cursor.getObject()); + XmlGlobalAssemblyDefinition assembly = new XmlGlobalAssemblyDefinition(obj, XmlModule.this); + if (LOGGER.isTraceEnabled()) { + LOGGER.trace("New assembly definition '{}'", assembly.toCoordinates()); + } + assemblyDefinitions.put(assembly.getDefinitionQName(), assembly); + if (assembly.isRoot()) { + rootAssemblyDefinitions.put(ObjectUtils.notNull(assembly.getRootXmlQName()), assembly); + } + } + + this.assemblyDefinitions + = assemblyDefinitions.isEmpty() ? Collections.emptyMap() + : Collections.unmodifiableMap(assemblyDefinitions); + this.rootAssemblyDefinitions = rootAssemblyDefinitions.isEmpty() ? Collections.emptyMap() + : Collections.unmodifiableMap(rootAssemblyDefinitions); + } + } + } + + public Map getFlagDefinitionMap() { + return flagDefinitions; + } + + public Map getFieldDefinitionMap() { + return fieldDefinitions; + } + + public Map getAssemblyDefinitionMap() { + return assemblyDefinitions; + } + + public Map getRootAssemblyDefinitionMap() { + return rootAssemblyDefinitions; + } + } } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/XmlObjectParser.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/XmlObjectParser.java index 6dc871b1d..878e179db 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/XmlObjectParser.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/XmlObjectParser.java @@ -55,7 +55,7 @@ private static String generatePath(@NonNull Collection nodes) { // build a mapping of namespace prefix to namespace AtomicInteger count = new AtomicInteger(); Map namespaceToPrefixMap = nodes.stream() - .map(qname -> qname.getNamespaceURI()) + .map(QName::getNamespaceURI) .distinct() .map(ns -> Pair.of(ns, "m" + count.getAndIncrement())) .collect(Collectors.toMap( diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/XmlbeansMarkupVisitor.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/XmlbeansMarkupVisitor.java index 010844bec..6ecac5ffe 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/XmlbeansMarkupVisitor.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/xml/impl/XmlbeansMarkupVisitor.java @@ -106,11 +106,11 @@ protected XmlbeansMarkupVisitor( @Override public void writeEmptyElement(QName qname, Map attributes) throws IllegalArgumentException { - @SuppressWarnings("resource") // not owned + @SuppressWarnings({ "resource", "PMD.CloseResource" }) // not owned XmlCursor cursor = getStream(); cursor.beginElement(qname); - attributes.forEach((name, value) -> cursor.insertAttributeWithValue(name, value)); + attributes.forEach(cursor::insertAttributeWithValue); // go to the end of the new element cursor.toEndToken(); @@ -122,11 +122,11 @@ public void writeEmptyElement(QName qname, Map attributes) @Override public void writeElementStart(QName qname, Map attributes) throws IllegalArgumentException { - @SuppressWarnings("resource") // not owned + @SuppressWarnings({ "resource", "PMD.CloseResource" }) // not owned XmlCursor cursor = getStream(); cursor.beginElement(qname); - attributes.forEach((name, value) -> cursor.insertAttributeWithValue(name, value)); + attributes.forEach(cursor::insertAttributeWithValue); // save the current location state cursor.push(); @@ -134,7 +134,7 @@ public void writeElementStart(QName qname, Map attributes) @Override public void writeElementEnd(QName qname) throws IllegalArgumentException { - @SuppressWarnings("resource") // not owned + @SuppressWarnings({ "resource", "PMD.CloseResource" }) // not owned XmlCursor cursor = getStream(); // restore location to end of start element @@ -149,14 +149,14 @@ public void writeElementEnd(QName qname) throws IllegalArgumentException { @Override public void writeText(CharSequence text) throws IllegalArgumentException { - @SuppressWarnings("resource") // not owned + @SuppressWarnings({ "resource", "PMD.CloseResource" }) // not owned XmlCursor cursor = getStream(); cursor.insertChars(text.toString()); } @Override protected void writeComment(CharSequence text) throws IllegalArgumentException { - @SuppressWarnings("resource") // not owned + @SuppressWarnings({ "resource", "PMD.CloseResource" }) // not owned XmlCursor cursor = getStream(); cursor.insertComment(text.toString()); } diff --git a/core/src/test/java/gov/nist/secauto/metaschema/core/metapath/cst/BuildCstVisitorTest.java b/core/src/test/java/gov/nist/secauto/metaschema/core/metapath/cst/BuildCstVisitorTest.java index 494fb5f0a..f1b8d1cde 100644 --- a/core/src/test/java/gov/nist/secauto/metaschema/core/metapath/cst/BuildCstVisitorTest.java +++ b/core/src/test/java/gov/nist/secauto/metaschema/core/metapath/cst/BuildCstVisitorTest.java @@ -112,7 +112,7 @@ private IDocumentNodeItem newTestDocument() { } @NonNull - private StaticContext newStaticContext() { + private static StaticContext newStaticContext() { return StaticContext.builder() .defaultModelNamespace(NS_URI) .build(); diff --git a/core/src/test/java/gov/nist/secauto/metaschema/core/metapath/cst/EQNameUtilsTest.java b/core/src/test/java/gov/nist/secauto/metaschema/core/metapath/cst/EQNameUtilsTest.java index 64678e516..357417b34 100644 --- a/core/src/test/java/gov/nist/secauto/metaschema/core/metapath/cst/EQNameUtilsTest.java +++ b/core/src/test/java/gov/nist/secauto/metaschema/core/metapath/cst/EQNameUtilsTest.java @@ -49,12 +49,6 @@ class EQNameUtilsTest { .defaultModelNamespace("http://example.com/ns/model") .build(); - private enum NameType { - FUNCTION, - FLAG, - MODEL; - } - static Stream provideValues() { return Stream.of( Arguments.of( diff --git a/core/src/test/java/gov/nist/secauto/metaschema/core/metapath/cst/PredicateTest.java b/core/src/test/java/gov/nist/secauto/metaschema/core/metapath/cst/PredicateExpressionTest.java similarity index 100% rename from core/src/test/java/gov/nist/secauto/metaschema/core/metapath/cst/PredicateTest.java rename to core/src/test/java/gov/nist/secauto/metaschema/core/metapath/cst/PredicateExpressionTest.java diff --git a/core/src/test/java/gov/nist/secauto/metaschema/core/model/xml/ExamplesTest.java b/core/src/test/java/gov/nist/secauto/metaschema/core/model/xml/ExamplesTest.java index 447aa30ad..dad9cc9dc 100644 --- a/core/src/test/java/gov/nist/secauto/metaschema/core/model/xml/ExamplesTest.java +++ b/core/src/test/java/gov/nist/secauto/metaschema/core/model/xml/ExamplesTest.java @@ -29,6 +29,7 @@ import static org.junit.jupiter.api.Assertions.assertNotNull; import gov.nist.secauto.metaschema.core.model.IAssemblyDefinition; +import gov.nist.secauto.metaschema.core.model.IMetaschemaModule; import gov.nist.secauto.metaschema.core.model.MetaschemaException; import gov.nist.secauto.metaschema.core.util.ObjectUtils; @@ -37,6 +38,8 @@ import java.io.IOException; import java.net.URI; +import javax.xml.namespace.QName; + class ExamplesTest { @Test @@ -46,7 +49,7 @@ void testLoadMetaschema() throws MetaschemaException, IOException { URI moduleUri = ObjectUtils.notNull(URI.create( "https://raw.githubusercontent.com/usnistgov/OSCAL/v1.0.0/src/metaschema/oscal_complete_metaschema.xml")); - IXmlModule module = loader.load(moduleUri); + IMetaschemaModule module = loader.load(moduleUri); assertNotNull(module, "metaschema not found"); } @@ -56,9 +59,10 @@ void testExamineAssemblyDefinitionByName() throws MetaschemaException, IOExcepti loader.allowEntityResolution(); URI moduleUri = ObjectUtils.notNull(URI.create( "https://raw.githubusercontent.com/usnistgov/OSCAL/v1.0.0/src/metaschema/oscal_complete_metaschema.xml")); - IXmlModule module = loader.load(moduleUri); + IMetaschemaModule module = loader.load(moduleUri); - IAssemblyDefinition definition = module.getScopedAssemblyDefinitionByName("property"); + IAssemblyDefinition definition = module.getScopedAssemblyDefinitionByName( + new QName("http://csrc.nist.gov/ns/oscal/1.0", "property")); assertNotNull(definition, "definition not found"); } diff --git a/core/src/test/java/gov/nist/secauto/metaschema/core/model/xml/MetaConstraintLoaderTest.java b/core/src/test/java/gov/nist/secauto/metaschema/core/model/xml/MetaConstraintLoaderTest.java index 209399641..f16fc2a1d 100644 --- a/core/src/test/java/gov/nist/secauto/metaschema/core/model/xml/MetaConstraintLoaderTest.java +++ b/core/src/test/java/gov/nist/secauto/metaschema/core/model/xml/MetaConstraintLoaderTest.java @@ -36,6 +36,7 @@ import gov.nist.secauto.metaschema.core.metapath.item.node.IDefinitionNodeItem; import gov.nist.secauto.metaschema.core.metapath.item.node.IModuleNodeItem; import gov.nist.secauto.metaschema.core.metapath.item.node.INodeItemFactory; +import gov.nist.secauto.metaschema.core.model.IMetaschemaModule; import gov.nist.secauto.metaschema.core.model.MetaschemaException; import gov.nist.secauto.metaschema.core.model.constraint.IConstraintSet; import gov.nist.secauto.metaschema.core.util.CollectionUtil; @@ -60,7 +61,7 @@ void test() throws MetaschemaException, IOException { ModuleLoader loader = new ModuleLoader(CollectionUtil.singletonList(postProcessor)); URI moduleUri = ObjectUtils.notNull( Paths.get("metaschema/examples/computer-example.xml").toUri()); - IXmlModule module = loader.load(moduleUri); + IMetaschemaModule module = loader.load(moduleUri); StaticContext staticContext = StaticContext.builder() .defaultModelNamespace(module.getXmlNamespace()) diff --git a/core/src/test/java/gov/nist/secauto/metaschema/core/model/xml/MetaschemaModuleTest.java b/core/src/test/java/gov/nist/secauto/metaschema/core/model/xml/MetaschemaModuleTest.java index cb2635089..0eb130b3b 100644 --- a/core/src/test/java/gov/nist/secauto/metaschema/core/model/xml/MetaschemaModuleTest.java +++ b/core/src/test/java/gov/nist/secauto/metaschema/core/model/xml/MetaschemaModuleTest.java @@ -28,6 +28,7 @@ import static org.junit.jupiter.api.Assertions.assertFalse; +import gov.nist.secauto.metaschema.core.model.IMetaschemaModule; import gov.nist.secauto.metaschema.core.model.MetaschemaException; import gov.nist.secauto.metaschema.core.util.ObjectUtils; @@ -44,7 +45,7 @@ void testFile() throws MetaschemaException, IOException { ModuleLoader loader = new ModuleLoader(); URI moduleUri = ObjectUtils.notNull( Paths.get("metaschema/schema/metaschema/metaschema-module-metaschema.xml").toUri()); - IXmlModule module = loader.load(moduleUri); + IMetaschemaModule module = loader.load(moduleUri); assertFalse(module.getExportedRootAssemblyDefinitions().isEmpty(), "no roots found"); } } diff --git a/core/src/test/java/gov/nist/secauto/metaschema/core/model/xml/ModuleLoaderTest.java b/core/src/test/java/gov/nist/secauto/metaschema/core/model/xml/ModuleLoaderTest.java index b39071c2c..0c1ea4c65 100644 --- a/core/src/test/java/gov/nist/secauto/metaschema/core/model/xml/ModuleLoaderTest.java +++ b/core/src/test/java/gov/nist/secauto/metaschema/core/model/xml/ModuleLoaderTest.java @@ -34,6 +34,7 @@ import gov.nist.secauto.metaschema.core.model.IAssemblyDefinition; import gov.nist.secauto.metaschema.core.model.IConstraintLoader; import gov.nist.secauto.metaschema.core.model.IFlagDefinition; +import gov.nist.secauto.metaschema.core.model.IMetaschemaModule; import gov.nist.secauto.metaschema.core.model.MetaschemaException; import gov.nist.secauto.metaschema.core.model.constraint.IAllowedValuesConstraint; import gov.nist.secauto.metaschema.core.model.constraint.IConstraint; @@ -48,6 +49,8 @@ import java.nio.file.Paths; import java.util.List; +import javax.xml.namespace.QName; + class ModuleLoaderTest { @Test @@ -56,13 +59,14 @@ void testUrl() throws MetaschemaException, IOException { // NOPMD - intentional loader.allowEntityResolution(); URI moduleUri = ObjectUtils.notNull(URI.create( "https://raw.githubusercontent.com/usnistgov/OSCAL/v1.0.0/src/metaschema/oscal_complete_metaschema.xml")); - IXmlModule module = loader.load(moduleUri); + IMetaschemaModule module = loader.load(moduleUri); - IXmlModule oscalCatalogModule = module.getImportedModuleByShortName("oscal-catalog"); + IMetaschemaModule oscalCatalogModule = module.getImportedModuleByShortName("oscal-catalog"); assertNotNull(oscalCatalogModule, "catalog metaschema not found"); - IXmlModule metadataModule = oscalCatalogModule.getImportedModuleByShortName("oscal-metadata"); + IMetaschemaModule metadataModule = oscalCatalogModule.getImportedModuleByShortName("oscal-metadata"); assertNotNull(metadataModule, "metadata metaschema not found"); - IFlagDefinition flag = metadataModule.getScopedFlagDefinitionByName("location-type"); + IFlagDefinition flag + = metadataModule.getScopedFlagDefinitionByName(new QName("location-type")); assertNotNull(flag, "flag not found"); List constraints = flag.getConstraints(); assertFalse(constraints.isEmpty(), "a constraint was expected"); @@ -73,7 +77,7 @@ void testFile() throws MetaschemaException, IOException { ModuleLoader loader = new ModuleLoader(); URI moduleUri = ObjectUtils.notNull( Paths.get("src/test/resources/content/custom-entity-metaschema.xml").toUri()); - IXmlModule module = loader.load(moduleUri); + IMetaschemaModule module = loader.load(moduleUri); assertFalse(module.getExportedRootAssemblyDefinitions().isEmpty(), "no roots found"); } @@ -89,8 +93,9 @@ void testConstraints() throws MetaschemaException, IOException { // NOPMD - inte loader.allowEntityResolution(); URI moduleUri = ObjectUtils.notNull(URI.create( "https://raw.githubusercontent.com/usnistgov/OSCAL/v1.0.0/src/metaschema/oscal_complete_metaschema.xml")); - IXmlModule module = loader.load(moduleUri); - IAssemblyDefinition catalog = module.getExportedAssemblyDefinitionByName("catalog"); + IMetaschemaModule module = loader.load(moduleUri); + IAssemblyDefinition catalog + = module.getExportedAssemblyDefinitionByName(new QName("http://csrc.nist.gov/ns/oscal/1.0", "catalog")); assertNotNull(catalog, "catalog not found"); List constraints = catalog.getConstraints(); @@ -101,10 +106,11 @@ void testConstraints() throws MetaschemaException, IOException { // NOPMD - inte void testLoadMetaschemaWithExternalEntity() throws MetaschemaException, IOException { ModuleLoader loader = new ModuleLoader(); loader.allowEntityResolution(); - IXmlModule module + IMetaschemaModule module = loader.load(ObjectUtils.notNull(Paths.get("src/test/resources/content/custom-entity-metaschema.xml"))); - IAssemblyDefinition root = module.getAssemblyDefinitionByName("root"); + IAssemblyDefinition root = module.getExportedRootAssemblyDefinitionByName( + new QName("http://csrc.nist.gov/ns/test/metaschema/entity", "root")); assert root != null; List allowedValues = root.getAllowedValuesConstraints(); diff --git a/core/src/test/java/gov/nist/secauto/metaschema/core/model/xml/UsedDefinitionModelWalkerTest.java b/core/src/test/java/gov/nist/secauto/metaschema/core/model/xml/UsedDefinitionModelWalkerTest.java index 1e2029bb1..9cf2e544e 100644 --- a/core/src/test/java/gov/nist/secauto/metaschema/core/model/xml/UsedDefinitionModelWalkerTest.java +++ b/core/src/test/java/gov/nist/secauto/metaschema/core/model/xml/UsedDefinitionModelWalkerTest.java @@ -29,6 +29,7 @@ import static org.junit.jupiter.api.Assertions.assertFalse; import gov.nist.secauto.metaschema.core.model.IDefinition; +import gov.nist.secauto.metaschema.core.model.IMetaschemaModule; import gov.nist.secauto.metaschema.core.model.MetaschemaException; import gov.nist.secauto.metaschema.core.model.UsedDefinitionModelWalker; @@ -50,7 +51,7 @@ void test() throws MetaschemaException, IOException { ModuleLoader loader = new ModuleLoader(); loader.allowEntityResolution(); - IXmlModule module = loader.load(new URL( + IMetaschemaModule module = loader.load(new URL( "https://raw.githubusercontent.com/usnistgov/OSCAL/v1.0.0/src/metaschema/oscal_complete_metaschema.xml")); Collection definitions diff --git a/core/src/test/java/gov/nist/secauto/metaschema/core/testing/AbstractModelBuilder.java b/core/src/test/java/gov/nist/secauto/metaschema/core/testing/AbstractModelBuilder.java index 85ee822e1..dec8b1726 100644 --- a/core/src/test/java/gov/nist/secauto/metaschema/core/testing/AbstractModelBuilder.java +++ b/core/src/test/java/gov/nist/secauto/metaschema/core/testing/AbstractModelBuilder.java @@ -30,8 +30,8 @@ import gov.nist.secauto.metaschema.core.model.IDefinition; import gov.nist.secauto.metaschema.core.model.IModelDefinition; import gov.nist.secauto.metaschema.core.model.IModelElement; -import gov.nist.secauto.metaschema.core.model.INamedModelElement; import gov.nist.secauto.metaschema.core.model.INamedInstance; +import gov.nist.secauto.metaschema.core.model.INamedModelElement; import gov.nist.secauto.metaschema.core.util.CollectionUtil; import org.apache.commons.lang3.ObjectUtils; diff --git a/core/src/test/java/gov/nist/secauto/metaschema/core/testing/FlagBuilder.java b/core/src/test/java/gov/nist/secauto/metaschema/core/testing/FlagBuilder.java index 6a3326f2c..35053427d 100644 --- a/core/src/test/java/gov/nist/secauto/metaschema/core/testing/FlagBuilder.java +++ b/core/src/test/java/gov/nist/secauto/metaschema/core/testing/FlagBuilder.java @@ -103,11 +103,4 @@ public IFlagDefinition toDefinition() { }); return retval; } - - // @Override - // public Object getValue(Object parentInstance) { - // // TODO Auto-generated method stub - // return null; - // } - // } } diff --git a/databind/pom.xml b/databind/pom.xml index 5810ed93a..0e64f3e69 100644 --- a/databind/pom.xml +++ b/databind/pom.xml @@ -101,7 +101,7 @@ target/generated-sources/xmlbeans - **/gov/nist/secauto/metaschema/databind/model/metaschema/*.java + gov/nist/secauto/metaschema/databind/model/metaschema/binding/*.java @@ -117,8 +117,10 @@ jacoco-maven-plugin + gov/nist/secauto/metaschema/databind/codegen/xmlbeans/**/* org/apache/xmlbeans/**/* + gov/nist/secauto/metaschema/databind/model/metaschema/binding/* diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/DefaultBindingContext.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/DefaultBindingContext.java index aafb556b6..4989b6707 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/DefaultBindingContext.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/DefaultBindingContext.java @@ -138,12 +138,12 @@ protected Collection getBindingMatchers() { @Override @NonNull - public IBindingMatcher registerBindingMatcher(@NonNull IBoundDefinitionModelAssembly definition) { - return ObjectUtils.notNull(bindingMatchers.computeIfAbsent(definition, (key) -> IBindingMatcher.of(definition))); + public final IBindingMatcher registerBindingMatcher(@NonNull IBoundDefinitionModelAssembly definition) { + return ObjectUtils.notNull(bindingMatchers.computeIfAbsent(definition, key -> IBindingMatcher.of(definition))); } @Override - public IBindingMatcher registerBindingMatcher(@NonNull Class clazz) { + public final IBindingMatcher registerBindingMatcher(@NonNull Class clazz) { IBoundDefinitionModelComplex definition = getBoundDefinitionForClass(clazz); if (definition == null) { throw new IllegalArgumentException(String.format("Unable to find bound definition for class '%s'.", @@ -160,7 +160,7 @@ public IBindingMatcher registerBindingMatcher(@NonNull Class clazz) { } @Override - public IBoundDefinitionModelComplex registerClassBinding(IBoundDefinitionModelComplex definition) { + public final IBoundDefinitionModelComplex registerClassBinding(IBoundDefinitionModelComplex definition) { Class clazz = definition.getBoundClass(); return boundClassToStrategyMap.computeIfAbsent(clazz, k -> definition); } @@ -322,7 +322,6 @@ public CLASS deepCopy(@NonNull CLASS other, Object parentInstance) throw if (definition == null) { throw new IllegalStateException(String.format("Class '%s' is not bound", other.getClass().getName())); } - @SuppressWarnings("unchecked") CLASS retval = (CLASS) definition.deepCopyItem(other, parentInstance); - return retval; + return ObjectUtils.asType(definition.deepCopyItem(other, parentInstance)); } } diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/IBindingContext.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/IBindingContext.java index 642f26f63..c7b5ca36c 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/IBindingContext.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/IBindingContext.java @@ -316,6 +316,8 @@ default IConstraintValidator newValidator(@NonNull IConstraintValidationHandler * * @param nodeItem * the node item to validate + * @param loader + * a module loader used to load and resolve referenced resources * @return the validation result * @throws IllegalArgumentException * if the provided class is not bound to a Module assembly or field diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/PostProcessingModuleLoaderStrategy.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/PostProcessingModuleLoaderStrategy.java index 438e8c1d0..2e61cfa54 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/PostProcessingModuleLoaderStrategy.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/PostProcessingModuleLoaderStrategy.java @@ -26,10 +26,10 @@ package gov.nist.secauto.metaschema.databind; +import gov.nist.secauto.metaschema.core.model.IModule; import gov.nist.secauto.metaschema.core.model.IModuleLoader; import gov.nist.secauto.metaschema.core.util.CollectionUtil; import gov.nist.secauto.metaschema.databind.model.IBoundDefinitionModelComplex; -import gov.nist.secauto.metaschema.databind.model.IBoundModule; import java.util.ArrayList; import java.util.HashSet; @@ -42,7 +42,7 @@ class PostProcessingModuleLoaderStrategy extends SimpleModuleLoaderStrategy { @NonNull private final List modulePostProcessors; - private final Set resolvedModules = new HashSet<>(); + private final Set resolvedModules = new HashSet<>(); protected PostProcessingModuleLoaderStrategy( @NonNull IBindingContext bindingContext, @@ -61,7 +61,7 @@ public IBoundDefinitionModelComplex getBoundDefinitionForClass(@NonNull Class IBoundDefinitionModelComplex retval = super.getBoundDefinitionForClass(clazz); if (retval != null) { // force loading of metaschema information to apply constraints - IBoundModule module = retval.getContainingModule(); + IModule module = retval.getContainingModule(); synchronized (resolvedModules) { if (!resolvedModules.contains(module)) { // add first, to avoid loops @@ -73,7 +73,7 @@ public IBoundDefinitionModelComplex getBoundDefinitionForClass(@NonNull Class return retval; } - private void handleModule(@NonNull IBoundModule module) { + private void handleModule(@NonNull IModule module) { for (IModuleLoader.IModulePostProcessor postProcessor : getModulePostProcessors()) { postProcessor.processModule(module); } diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/SimpleModuleLoaderStrategy.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/SimpleModuleLoaderStrategy.java index 9399542da..f65409e0f 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/SimpleModuleLoaderStrategy.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/SimpleModuleLoaderStrategy.java @@ -90,9 +90,9 @@ public IBoundDefinitionModelComplex getBoundDefinitionForClass(@NonNull Class private IBoundDefinitionModelComplex newBoundDefinition(@NonNull Class clazz) { IBoundDefinitionModelComplex retval = null; if (clazz.isAnnotationPresent(MetaschemaAssembly.class)) { - retval = new DefinitionAssembly(clazz, getBindingContext()); + retval = DefinitionAssembly.newInstance(clazz, getBindingContext()); } else if (clazz.isAnnotationPresent(MetaschemaField.class)) { - retval = new DefinitionField(clazz, getBindingContext()); + retval = DefinitionField.newInstance(clazz, getBindingContext()); } return retval; } diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/codegen/ClassUtils.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/codegen/ClassUtils.java index 6953cc97d..be18276b6 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/codegen/ClassUtils.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/codegen/ClassUtils.java @@ -26,7 +26,9 @@ package gov.nist.secauto.metaschema.databind.codegen; -import gov.nist.secauto.metaschema.databind.codegen.impl.NameConverter; +import org.apache.xmlbeans.impl.common.NameUtil; + +import java.util.Map; import edu.umd.cs.findbugs.annotations.NonNull; @@ -34,6 +36,9 @@ * A variety of utility methods for normalizing Java class related names. */ public final class ClassUtils { + private static final Map JAVA_NAME_MAPPER = Map.ofEntries( + Map.entry("Class", "Clazz")); + private ClassUtils() { // disable construction } @@ -49,7 +54,8 @@ private ClassUtils() { @SuppressWarnings("null") @NonNull public static String toPropertyName(@NonNull String name) { - return NameConverter.STANDARD.toPropertyName(name); + String property = NameUtil.upperCamelCase(name); + return JAVA_NAME_MAPPER.getOrDefault(property, property); } /** @@ -63,7 +69,7 @@ public static String toPropertyName(@NonNull String name) { @SuppressWarnings("null") @NonNull public static String toVariableName(@NonNull String name) { - return NameConverter.STANDARD.toVariableName(name); + return NameUtil.lowerCamelCase(name); } /** @@ -77,7 +83,7 @@ public static String toVariableName(@NonNull String name) { @SuppressWarnings("null") @NonNull public static String toClassName(@NonNull String name) { - return NameConverter.STANDARD.toClassName(name); + return NameUtil.upperCamelCase(name, false); } /** @@ -91,7 +97,6 @@ public static String toClassName(@NonNull String name) { @SuppressWarnings("null") @NonNull public static String toPackageName(@NonNull String name) { - return NameConverter.STANDARD.toPackageName(name); + return NameUtil.getPackageFromNamespace(name, false); } - } diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/codegen/IGeneratedDefinitionClass.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/codegen/IGeneratedDefinitionClass.java index c84080227..3fee0a0e8 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/codegen/IGeneratedDefinitionClass.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/codegen/IGeneratedDefinitionClass.java @@ -26,7 +26,7 @@ package gov.nist.secauto.metaschema.databind.codegen; -import gov.nist.secauto.metaschema.core.model.IContainerFlag; +import gov.nist.secauto.metaschema.core.model.IModelDefinition; import edu.umd.cs.findbugs.annotations.NonNull; @@ -38,7 +38,7 @@ public interface IGeneratedDefinitionClass extends IGeneratedClass { * @return the definition */ @NonNull - IContainerFlag getDefinition(); + IModelDefinition getDefinition(); /** * Indicates if the class represents a root Module assembly which can be the diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/codegen/impl/DefaultGeneratedDefinitionClass.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/codegen/impl/DefaultGeneratedDefinitionClass.java index 5493a2dbe..00ae607ec 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/codegen/impl/DefaultGeneratedDefinitionClass.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/codegen/impl/DefaultGeneratedDefinitionClass.java @@ -29,7 +29,7 @@ import com.squareup.javapoet.ClassName; import gov.nist.secauto.metaschema.core.model.IAssemblyDefinition; -import gov.nist.secauto.metaschema.core.model.IContainerFlag; +import gov.nist.secauto.metaschema.core.model.IModelDefinition; import gov.nist.secauto.metaschema.databind.codegen.IGeneratedDefinitionClass; import java.nio.file.Path; @@ -44,7 +44,7 @@ public class DefaultGeneratedDefinitionClass extends DefaultGeneratedClass implements IGeneratedDefinitionClass { @NonNull - private final IContainerFlag definition; + private final IModelDefinition definition; /** * Construct a new class information object for a generated class. @@ -57,13 +57,13 @@ public class DefaultGeneratedDefinitionClass * the definition on which the class was based */ public DefaultGeneratedDefinitionClass(@NonNull Path classFile, @NonNull ClassName className, - @NonNull IContainerFlag definition) { + @NonNull IModelDefinition definition) { super(classFile, className); this.definition = definition; } @Override - public IContainerFlag getDefinition() { + public IModelDefinition getDefinition() { return definition; } diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/codegen/impl/DefaultGeneratedModuleClass.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/codegen/impl/DefaultGeneratedModuleClass.java index e6b06e38a..36a3e581a 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/codegen/impl/DefaultGeneratedModuleClass.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/codegen/impl/DefaultGeneratedModuleClass.java @@ -28,7 +28,7 @@ import com.squareup.javapoet.ClassName; -import gov.nist.secauto.metaschema.core.model.IContainerFlag; +import gov.nist.secauto.metaschema.core.model.IModelDefinition; import gov.nist.secauto.metaschema.core.model.IModule; import gov.nist.secauto.metaschema.core.util.CollectionUtil; import gov.nist.secauto.metaschema.core.util.ObjectUtils; @@ -47,7 +47,7 @@ public class DefaultGeneratedModuleClass @NonNull private final IModule module; @NonNull - private final Map definitionClassMap; + private final Map definitionClassMap; @NonNull private final String packageName; @@ -55,7 +55,7 @@ public DefaultGeneratedModuleClass( @NonNull IModule module, @NonNull ClassName className, @NonNull Path classFile, - @NonNull Map definitionClassMap, + @NonNull Map definitionClassMap, @NonNull String packageName) { super(classFile, className); this.module = module; diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/codegen/impl/NameConverter.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/codegen/impl/NameConverter.java deleted file mode 100644 index 709db26f5..000000000 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/codegen/impl/NameConverter.java +++ /dev/null @@ -1,281 +0,0 @@ -/* - * Portions of this software was developed by employees of the National Institute - * of Standards and Technology (NIST), an agency of the Federal Government and is - * being made available as a public service. Pursuant to title 17 United States - * Code Section 105, works of NIST employees are not subject to copyright - * protection in the United States. This software may be subject to foreign - * copyright. Permission in the United States and in foreign countries, to the - * extent that NIST may hold copyright, to use, copy, modify, create derivative - * works, and distribute this software and its documentation without fee is hereby - * granted on a non-exclusive basis, provided that this notice and disclaimer - * of warranty appears in all copies. - * - * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER - * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY - * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM - * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE - * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT - * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, - * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, - * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, - * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR - * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT - * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. - */ -/* - * Copyright (c) 1997, 2022 Oracle and/or its affiliates. All rights reserved. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Distribution License v. 1.0, which is available at - * http://www.eclipse.org/org/documents/edl-v10.php. - * - * SPDX-License-Identifier: BSD-3-Clause - */ - -package gov.nist.secauto.metaschema.databind.codegen.impl; - -import java.util.ArrayList; -import java.util.List; -import java.util.Locale; -import java.util.StringTokenizer; - -import javax.lang.model.SourceVersion; - -/** - * Converts aribitrary strings into Java identifiers. - * - * @author Kohsuke KAWAGUCHI - */ -public interface NameConverter { - /** - * The name converter implemented by Code Model. - * - * This is the standard name conversion for JAXB. - */ - NameConverter STANDARD = new Standard(); - - /** - * converts a string into an identifier suitable for classes. - * - * In general, this operation should generate "NamesLikeThis". - * - * @param token - * the string to convert - * @return the equivalent class name - */ - String toClassName(String token); - - /** - * converts a string into an identifier suitable for interfaces. - * - * In general, this operation should generate "NamesLikeThis". But for example, - * it can prepend every interface with 'I'. - * - * @param token - * the string to convert - * @return the equivalent interface name - */ - String toInterfaceName(String token); - - /** - * Converts a string into an identifier suitable for Java properties. - *

- * In general, this operation should generate upper camel case "NamesLikeThis", - * which will be used with known prefixes like "get" or "set". - * - * @param token - * the string to convert - * @return the equivalent Java property name - */ - String toPropertyName(String token); - - /** - * converts a string into an identifier suitable for constants. - *

- * In the standard Java naming convention, this operation should generate upper - * case "NAMES_LIKE_THIS". - * - * @param token - * the string to convert - * @return the equivalent constant name - */ - String toConstantName(String token); - - /** - * Converts a string into an identifier suitable for variables. - *

- * In general it should generate lower camel case "namesLikeThis". - * - * @param token - * the string to convert - * @return the equivalent variable name - */ - String toVariableName(String token); - - /** - * Converts a namespace URI into a package name. This method should expect - * strings like "http://foo.bar.zot/org", "urn:abc:def:ghi" "", or even "###" - * (basically anything) and expected to return a package name, like - * "org.acme.foo". - * - * @param namespaceUri - * the string to convert - * @return the equivalent package name - */ - String toPackageName(String namespaceUri); - - class Standard - extends NameUtil - implements NameConverter { - - @Override - public String toClassName(String token) { - return toMixedCaseName(toWordList(token), true); - } - - @Override - public String toVariableName(String token) { - return toMixedCaseName(toWordList(token), false); - } - - @Override - public String toInterfaceName(String token) { - return toClassName(token); - } - - @Override - public String toPropertyName(String token) { - String prop = toClassName(token); - // property name "Class" with collide with Object.getClass, - // so escape this. - if ("Class".equals(prop)) { - prop = "Clazz"; - } - return prop; - } - - /** - * Computes a Java package name from a namespace URI, as specified in the spec. - * - * @return {@code null} if it fails to derive a package name. - */ - @SuppressWarnings({ - "PMD.CyclomaticComplexity", "PMD.NPathComplexity", // acceptable - "PMD.OnlyOneReturn", // readability - "PMD.UseStringBufferForStringAppends" // ok - }) - @Override - public String toPackageName(String uri) { - String nsUri = uri; - // remove scheme and :, if present - // spec only requires us to remove 'http' and 'urn'... - int idx = nsUri.indexOf(':'); - String scheme = ""; - if (idx >= 0) { - scheme = nsUri.substring(0, idx); - if ("http".equalsIgnoreCase(scheme) || "urn".equalsIgnoreCase(scheme)) { - nsUri = nsUri.substring(idx + 1); - } - } - - // tokenize string - List tokens = tokenize(nsUri, "/: "); - if (tokens.isEmpty()) { - return null; - } - - // remove trailing file type, if necessary - if (tokens.size() > 1) { - // for uri's like "www.foo.com" and "foo.com", there is no trailing - // file, so there's no need to look at the last '.' and substring - // otherwise, we loose the "com" (which would be wrong) - String lastToken = tokens.get(tokens.size() - 1); - idx = lastToken.lastIndexOf('.'); - if (idx > 0) { - lastToken = lastToken.substring(0, idx); - tokens.set(tokens.size() - 1, lastToken); - } - } - - // tokenize domain name and reverse. Also remove :port if it exists - String domain = tokens.get(0); - idx = domain.indexOf(':'); - if (idx >= 0) { - domain = domain.substring(0, idx); - } - List rev = reverse(tokenize(domain, "urn".equals(scheme) ? ".-" : ".")); - if ("www".equalsIgnoreCase(rev.get(rev.size() - 1))) { - // remove leading www - rev.remove(rev.size() - 1); - } - - // replace the domain name with tokenized items - tokens.addAll(1, rev); - tokens.remove(0); - - // iterate through the tokens and apply xml->java name algorithm - for (int i = 0; i < tokens.size(); i++) { - - // get the token and remove illegal chars - String token = tokens.get(i); - token = removeIllegalIdentifierChars(token); - - // this will check for reserved keywords - if (SourceVersion.isKeyword(token.toLowerCase(Locale.ENGLISH))) { - token = '_' + token; - } - - tokens.set(i, token.toLowerCase(Locale.ENGLISH)); - } - - // concat all the pieces and return it - return combine(tokens, '.'); - } - - private static String removeIllegalIdentifierChars(String token) { - StringBuilder newToken = new StringBuilder(token.length() * 2); // max expected length - for (int i = 0; i < token.length(); i++) { - char ch = token.charAt(i); - if (i == 0 && !Character.isJavaIdentifierStart(ch)) { // ch can't be used as FIRST char - newToken.append('_'); - } - if (Character.isJavaIdentifierPart(ch)) { - newToken.append(ch); // ch is valid - } else { - newToken.append('_'); // ch can't be used - } - } - return newToken.toString(); - } - - private static List tokenize(String str, String sep) { - StringTokenizer tokens = new StringTokenizer(str, sep); - List retval = new ArrayList<>(); - - while (tokens.hasMoreTokens()) { - retval.add(tokens.nextToken()); - } - return retval; - } - - private static List reverse(List list) { - List rev = new ArrayList<>(list.size()); - - for (int i = list.size() - 1; i >= 0; i--) { - rev.add(list.get(i)); - } - return rev; - } - - private static String combine(List tokens, char sep) { - StringBuilder buf = new StringBuilder(tokens.get(0)); - - for (int i = 1; i < tokens.size(); i++) { - buf.append(sep) - .append(tokens.get(i)); - } - return buf.toString(); - } - } -} diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/codegen/impl/NameUtil.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/codegen/impl/NameUtil.java deleted file mode 100644 index c54f0d736..000000000 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/codegen/impl/NameUtil.java +++ /dev/null @@ -1,348 +0,0 @@ -/* - * Portions of this software was developed by employees of the National Institute - * of Standards and Technology (NIST), an agency of the Federal Government and is - * being made available as a public service. Pursuant to title 17 United States - * Code Section 105, works of NIST employees are not subject to copyright - * protection in the United States. This software may be subject to foreign - * copyright. Permission in the United States and in foreign countries, to the - * extent that NIST may hold copyright, to use, copy, modify, create derivative - * works, and distribute this software and its documentation without fee is hereby - * granted on a non-exclusive basis, provided that this notice and disclaimer - * of warranty appears in all copies. - * - * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER - * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY - * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM - * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE - * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT - * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, - * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, - * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, - * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR - * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT - * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. - */ - -/* - * This code is based on https://github.com/eclipse-ee4j/jaxb-ri/blob/master/jaxb-ri/core/src/main/java/org/glassfish/jaxb/core/api/impl/NameUtil.java - * - * Copyright (c) 1997, 2022 Oracle and/or its affiliates. All rights reserved. - * - * This program and the accompanying materials are made available under the - * terms of the Eclipse Distribution License v. 1.0, which is available at - * http://www.eclipse.org/org/documents/edl-v10.php. - * - * SPDX-License-Identifier: BSD-3-Clause - */ - -package gov.nist.secauto.metaschema.databind.codegen.impl; - -import java.util.ArrayList; -import java.util.List; -import java.util.Locale; - -/** - * Methods that convert strings into various formats. - */ -@SuppressWarnings("PMD") -class NameUtil { - protected static boolean isPunct(char ch) { - return ch == '-' || ch == '.' || ch == ':' || ch == '_' || ch == '·' || ch == '·' || ch == '۝' || ch == '۞'; - } - - /** - * Capitalizes the first character of the specified string, and de-capitalize - * the rest of characters. - * - * @param str - * the string to capitalize - * @return the capitalized string - */ - public static String capitalize(String str) { - if (!Character.isLowerCase(str.charAt(0))) { - return str; - } - StringBuilder sb = new StringBuilder(str.length()); - sb.append(String.valueOf(str.charAt(0)).toUpperCase(Locale.ENGLISH)); - sb.append(str.substring(1).toLowerCase(Locale.ENGLISH)); - return sb.toString(); - } - - // Precondition: s[start] is not punctuation - @SuppressWarnings({ - "checkstyle:MissingSwitchDefaultCheck", // intentional - }) - private static int nextBreak(String str, int start) { - int len = str.length(); - - char c1 = str.charAt(start); - int t1 = classify(c1); - - for (int i = start + 1; i < len; i++) { - // shift (c1,t1) into (c0,t0) - // char c0 = c1; --- conceptually, but c0 won't be used - int t0 = t1; - - c1 = str.charAt(i); - t1 = classify(c1); - - switch (actionTable[t0 * 5 + t1]) { - case ACTION_CHECK_PUNCT: - if (isPunct(c1)) { - return i; - } - break; - case ACTION_CHECK_C2: - if (i < len - 1) { - char c2 = str.charAt(i + 1); - if (Character.isLowerCase(c2)) { - return i; - } - } - break; - case ACTION_BREAK: - return i; - } - } - return -1; - } - - // the 5-category classification that we use in this code - // to find work breaks - protected static final int UPPER_LETTER = 0; - protected static final int LOWER_LETTER = 1; - protected static final int OTHER_LETTER = 2; - protected static final int DIGIT = 3; - protected static final int OTHER = 4; - - /** - * Look up table for actions. type0*5+type1 would yield the action to be taken. - */ - private static final byte[] actionTable = new byte[5 * 5]; - - // action constants. see nextBreak for the meaning - private static final byte ACTION_CHECK_PUNCT = 0; - private static final byte ACTION_CHECK_C2 = 1; - private static final byte ACTION_BREAK = 2; - private static final byte ACTION_NOBREAK = 3; - - /** - * Decide the action to be taken given the classification of the preceding - * character 't0' and the classification of the next character 't1'. - */ - private static byte decideAction(int t0, int t1) { - if (t0 == OTHER && t1 == OTHER) { - return ACTION_CHECK_PUNCT; - } - if ((t0 == DIGIT) ^ (t1 == DIGIT)) { - return ACTION_BREAK; - } - if (t0 == LOWER_LETTER && t1 != LOWER_LETTER) { - return ACTION_BREAK; - } - if ((t0 <= OTHER_LETTER) ^ (t1 <= OTHER_LETTER)) { - return ACTION_BREAK; - } - if ((t0 == OTHER_LETTER) ^ (t1 == OTHER_LETTER)) { - return ACTION_BREAK; - } - if (t0 == UPPER_LETTER && t1 == UPPER_LETTER) { - return ACTION_CHECK_C2; - } - - return ACTION_NOBREAK; - } - - static { - // initialize the action table - for (int t0 = 0; t0 < 5; t0++) { - for (int t1 = 0; t1 < 5; t1++) { - actionTable[t0 * 5 + t1] = decideAction(t0, t1); - } - } - } - - /** - * Classify a character into 5 categories that determine the word break. - * - * @param ch - * the character - * @return the categorization - */ - protected static int classify(char ch) { - switch (Character.getType(ch)) { - case Character.UPPERCASE_LETTER: - return UPPER_LETTER; - case Character.LOWERCASE_LETTER: - return LOWER_LETTER; - case Character.TITLECASE_LETTER: - case Character.MODIFIER_LETTER: - case Character.OTHER_LETTER: - return OTHER_LETTER; - case Character.DECIMAL_DIGIT_NUMBER: - return DIGIT; - default: - return OTHER; - } - } - - /** - * Tokenizes a string into words and capitalizes the first character of each - * word. - *

- * This method uses a change in character type as a splitter of two words. For - * example, "abc100ghi" will be splitted into {"Abc", "100","Ghi"}. - * - * @param str - * the string to split into a word list - * @return the word list - */ - public static List toWordList(String str) { - ArrayList retval = new ArrayList<>(); - int len = str.length(); - for (int i = 0; i < len;) { - - // Skip punctuation - while (i < len) { - if (!isPunct(str.charAt(i))) { - break; - } - i++; - } - if (i >= len) { - break; - } - - // Find next break and collect word - int breakPos = nextBreak(str, i); - String word = (breakPos == -1) ? str.substring(i) : str.substring(i, breakPos); - retval.add(escape(capitalize(word))); - if (breakPos == -1) { - break; - } - i = breakPos; - } - - // we can't guarantee a valid Java identifier anyway, - // so there's not much point in rejecting things in this way. - // if (ss.size() == 0) - // throw new IllegalArgumentException("Zero-length identifier"); - return retval; - } - - protected static String toMixedCaseName(List ss, boolean startUpper) { - StringBuilder sb = new StringBuilder(); - if (!ss.isEmpty()) { - sb.append(startUpper ? ss.get(0) : ss.get(0).toLowerCase(Locale.ENGLISH)); - for (int i = 1; i < ss.size(); i++) { - sb.append(ss.get(i)); - } - } - return sb.toString(); - } - - protected static String toMixedCaseVariableName(String[] ss, - boolean startUpper, - boolean cdrUpper) { - if (cdrUpper) { - for (int i = 1; i < ss.length; i++) { - ss[i] = capitalize(ss[i]); - } - } - StringBuilder sb = new StringBuilder(); - if (ss.length > 0) { - sb.append(startUpper ? ss[0] : ss[0].toLowerCase(Locale.ENGLISH)); - for (int i = 1; i < ss.length; i++) { - sb.append(ss[i]); - } - } - return sb.toString(); - } - - /** - * Formats a string into "THIS_KIND_OF_FORMAT_ABC_DEF". - * - * @param str - * the string to format - * @return Always return a string but there's no guarantee that the generated - * code is a valid Java identifier. - */ - public String toConstantName(String str) { - return toConstantName(toWordList(str)); - } - - /** - * Formats a string into "THIS_KIND_OF_FORMAT_ABC_DEF". - * - * @param ss - * a list of words - * @return Always return a string but there's no guarantee that the generated - * code is a valid Java identifier. - */ - public String toConstantName(List ss) { - StringBuilder sb = new StringBuilder(); - if (!ss.isEmpty()) { - sb.append(ss.get(0).toUpperCase(Locale.ENGLISH)); - for (int i = 1; i < ss.size(); i++) { - sb.append('_'); - sb.append(ss.get(i).toUpperCase(Locale.ENGLISH)); - } - } - return sb.toString(); - } - - /** - * Escapes characters is the given string so that they can be printed by only - * using US-ASCII characters. - * - * The escaped characters will be appended to the given StringBuffer. - * - * @param sb - * StringBuffer that receives escaped string. - * @param str - * String to be escaped. s.substring(start) will be - * escaped and copied to the string buffer. - * @param start - * the starting position in the string - */ - @SuppressWarnings({ - "checkstyle:MissingSwitchDefaultCheck", // intentional - "checkstyle:AvoidEscapedUnicodeCharactersCheck" // ok - }) - public static void escape(StringBuilder sb, String str, int start) { - int len = str.length(); - for (int i = start; i < len; i++) { - char ch = str.charAt(i); - if (Character.isJavaIdentifierPart(ch)) { - sb.append(ch); - } else { - sb.append('_'); - if (ch <= '\u000f') { - sb.append("000"); - } else if (ch <= '\u00ff') { - sb.append("00"); - } else if (ch <= '\u0fff') { - sb.append('0'); - } - sb.append(Integer.toString(ch, 16)); - } - } - } - - /** - * Escapes characters that are unusable as Java identifiers by replacing unsafe - * characters with safe characters. - */ - private static String escape(String str) { - int len = str.length(); - for (int i = 0; i < len; i++) { - if (!Character.isJavaIdentifierPart(str.charAt(i))) { - StringBuilder sb = new StringBuilder(str.substring(0, i)); - escape(sb, str, i); - return sb.toString(); - } - } - return str; - } -} diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/codegen/typeinfo/AbstractModelInstanceTypeInfo.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/codegen/typeinfo/AbstractModelInstanceTypeInfo.java index 2a01fcb81..b9bf12544 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/codegen/typeinfo/AbstractModelInstanceTypeInfo.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/codegen/typeinfo/AbstractModelInstanceTypeInfo.java @@ -41,7 +41,6 @@ import gov.nist.secauto.metaschema.core.util.ObjectUtils; import gov.nist.secauto.metaschema.databind.codegen.typeinfo.def.IAssemblyDefinitionTypeInfo; import gov.nist.secauto.metaschema.databind.model.annotations.GroupAs; -import gov.nist.secauto.metaschema.databind.model.annotations.ModelUtil; import java.util.HashSet; import java.util.List; @@ -121,13 +120,7 @@ protected AnnotationSpec.Builder generateGroupAsAnnotation() { groupAsAnnoation.addMember("name", "$S", ObjectUtils.requireNonNull(modelInstance.getGroupAsName(), "The grouping name must be non-null")); - TypeInfoUtils.buildNamespaceBindingAnnotation( - groupAsAnnoation, - "namespace", - modelInstance.getGroupAsXmlNamespace(), - () -> modelInstance.getContainingModule().getXmlNamespace().toASCIIString(), - ModelUtil.DEFAULT_STRING_VALUE, - false); + // TODO: handle group-as namespace as a prefix JsonGroupAsBehavior jsonGroupAsBehavior = modelInstance.getJsonGroupAsBehavior(); assert jsonGroupAsBehavior != null; diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/codegen/typeinfo/AbstractNamedModelInstanceTypeInfo.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/codegen/typeinfo/AbstractNamedModelInstanceTypeInfo.java index de2a8f621..5aed79a10 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/codegen/typeinfo/AbstractNamedModelInstanceTypeInfo.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/codegen/typeinfo/AbstractNamedModelInstanceTypeInfo.java @@ -141,7 +141,7 @@ protected void buildExtraMethods(TypeSpec.Builder builder, FieldSpec valueField) String itemPropertyName = ClassUtils.toPropertyName(getItemBaseName()); if (JsonGroupAsBehavior.KEYED.equals(instance.getJsonGroupAsBehavior())) { - IFlagInstance jsonKey = instance.getDefinition().getJsonKeyFlagInstance(); + IFlagInstance jsonKey = instance.getDefinition().getJsonKey(); if (jsonKey == null) { throw new IllegalStateException( String.format("JSON key not defined for property: %s", instance.toCoordinates())); diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/codegen/typeinfo/ChoiceGroupTypeInfoImpl.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/codegen/typeinfo/ChoiceGroupTypeInfoImpl.java index c859504ff..e89911045 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/codegen/typeinfo/ChoiceGroupTypeInfoImpl.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/codegen/typeinfo/ChoiceGroupTypeInfoImpl.java @@ -93,7 +93,7 @@ public Set buildBindingAnnotation( annotation.addMember("maxOccurs", "$L", maxOccurs); } - String jsonKeyName = choiceGroup.getJsonKeyFlagName(); + String jsonKeyName = choiceGroup.getJsonKeyFlagInstanceName(); if (jsonKeyName != null) { annotation.addMember("jsonKey", "$S", jsonKeyName); } diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/codegen/typeinfo/DefaultMetaschemaClassFactory.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/codegen/typeinfo/DefaultMetaschemaClassFactory.java index 21e719c99..f7dbbd415 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/codegen/typeinfo/DefaultMetaschemaClassFactory.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/codegen/typeinfo/DefaultMetaschemaClassFactory.java @@ -31,28 +31,20 @@ import com.squareup.javapoet.FieldSpec; import com.squareup.javapoet.JavaFile; import com.squareup.javapoet.MethodSpec; -import com.squareup.javapoet.ParameterSpec; import com.squareup.javapoet.ParameterizedTypeName; -import com.squareup.javapoet.TypeName; import com.squareup.javapoet.TypeSpec; import com.squareup.javapoet.WildcardTypeName; import gov.nist.secauto.metaschema.core.datatype.markup.MarkupLine; import gov.nist.secauto.metaschema.core.datatype.markup.MarkupMultiline; import gov.nist.secauto.metaschema.core.model.IAssemblyDefinition; -import gov.nist.secauto.metaschema.core.model.IContainerFlag; import gov.nist.secauto.metaschema.core.model.IDefinition; import gov.nist.secauto.metaschema.core.model.IFieldDefinition; -import gov.nist.secauto.metaschema.core.model.IFlagInstance; import gov.nist.secauto.metaschema.core.model.IModelDefinition; -import gov.nist.secauto.metaschema.core.model.IModelInstanceAbsolute; import gov.nist.secauto.metaschema.core.model.IModule; -import gov.nist.secauto.metaschema.core.model.INamedModelInstance; -import gov.nist.secauto.metaschema.core.model.JsonGroupAsBehavior; import gov.nist.secauto.metaschema.core.util.CollectionUtil; import gov.nist.secauto.metaschema.core.util.ObjectUtils; import gov.nist.secauto.metaschema.databind.IBindingContext; -import gov.nist.secauto.metaschema.databind.codegen.ClassUtils; import gov.nist.secauto.metaschema.databind.codegen.IGeneratedClass; import gov.nist.secauto.metaschema.databind.codegen.IGeneratedDefinitionClass; import gov.nist.secauto.metaschema.databind.codegen.IGeneratedModuleClass; @@ -84,8 +76,6 @@ import java.nio.file.StandardOpenOption; import java.util.Collection; import java.util.HashSet; -import java.util.LinkedHashMap; -import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Set; @@ -152,14 +142,14 @@ public IGeneratedModuleClass generateClass( Path classFile = ObjectUtils.notNull(javaFile.writeToPath(targetDirectory)); // now generate all related definition classes - Stream globalDefinitions = Stream.concat( + Stream globalDefinitions = Stream.concat( module.getAssemblyDefinitions().stream(), module.getFieldDefinitions().stream()); Set classNames = new HashSet<>(); @SuppressWarnings("PMD.UseConcurrentHashMap") // map is unmodifiable - Map definitionProductions + Map definitionProductions = ObjectUtils.notNull(globalDefinitions // Get type information for assembly and field definitions. // Avoid field definitions without flags that don't require a generated class @@ -474,9 +464,7 @@ protected TypeSpec.Builder newClassBuilder( protected Set buildClass( @NonNull IAssemblyDefinitionTypeInfo typeInfo, @NonNull TypeSpec.Builder builder) { - Set retval = new HashSet<>(); - - retval.addAll(buildClass((IModelDefinitionTypeInfo) typeInfo, builder)); + Set retval = new HashSet<>(buildClass((IModelDefinitionTypeInfo) typeInfo, builder)); AnnotationSpec.Builder metaschemaAssembly = ObjectUtils.notNull(AnnotationSpec.builder(MetaschemaAssembly.class)); @@ -513,9 +501,7 @@ protected Set buildClass( protected Set buildClass( @NonNull IFieldDefinitionTypeInfo typeInfo, @NonNull TypeSpec.Builder builder) { - Set retval = new HashSet<>(); - retval.addAll(buildClass((IModelDefinitionTypeInfo) typeInfo, builder)); - + Set retval = new HashSet<>(buildClass((IModelDefinitionTypeInfo) typeInfo, builder)); AnnotationSpec.Builder metaschemaField = ObjectUtils.notNull(AnnotationSpec.builder(MetaschemaField.class)); buildCommonProperties(typeInfo, metaschemaField); @@ -604,162 +590,4 @@ protected void buildCommonProperties( IModule module = definition.getContainingModule(); builder.addMember("moduleClass", "$T.class", getTypeResolver().getClassName(module)); } - - /* - protected void buildFieldForFieldValue( - @NonNull IFieldValueTypeInfo typeInfo, - @NonNull FieldSpec.Builder builder) { - IFieldDefinition definition = typeInfo.getParentDefinitionTypeInfo().getDefinition(); - AnnotationSpec.Builder fieldValue = AnnotationSpec.builder(BoundFieldValue.class); - - IDataTypeAdapter valueDataType = definition.getJavaTypeAdapter(); - - // a field object always has a single value - if (!definition.hasJsonValueKeyFlagInstance()) { - fieldValue.addMember("valueKeyName", "$S", definition.getJsonValueKeyName()); - } // else do nothing, the annotation will be on the flag - - if (!MetaschemaDataTypeProvider.DEFAULT_DATA_TYPE.equals(valueDataType)) { - fieldValue.addMember("typeAdapter", "$T.class", valueDataType.getClass()); - } - - Object defaultValue = definition.getDefaultValue(); - if (defaultValue != null) { - fieldValue.addMember("defaultValue", "$S", valueDataType.asString(defaultValue)); - } - - builder.addAnnotation(fieldValue.build()); - } - */ - /** - * This method can be implemented by subclasses to create additional methods. - * - * @param typeInfo - * the type information for the Java property to build - * @param builder - * the class builder - * @param valueField - * the field corresponding to this property - */ - @SuppressWarnings("PMD.LooseCoupling") // need implementation classes - protected void buildExtraMethods( // NOPMD - intentional - @NonNull IModelInstanceTypeInfo typeInfo, - @NonNull TypeSpec.Builder builder, - @NonNull FieldSpec valueField) { - IModelInstanceAbsolute instance = typeInfo.getInstance(); - int maxOccurance = instance.getMaxOccurs(); - if (maxOccurance == -1 || maxOccurance > 1) { - TypeName itemType = typeInfo.getJavaItemType(); - ParameterSpec valueParam = ObjectUtils.notNull(ParameterSpec.builder(itemType, "item").build()); - - String itemPropertyName = ClassUtils.toPropertyName(typeInfo.getItemBaseName()); - - if (JsonGroupAsBehavior.KEYED.equals(instance.getJsonGroupAsBehavior())) { - if (instance instanceof INamedModelInstance) { - buildKeyedMethods((INamedModelInstance) instance, builder, itemType, itemPropertyName, valueParam, - valueField); - } else { - // FIXME: implement choice group - throw new UnsupportedOperationException("implement"); - } - } else { - { - // create add method - MethodSpec.Builder method = MethodSpec.methodBuilder("add" + itemPropertyName) - .addParameter(valueParam) - .returns(TypeName.BOOLEAN) - .addModifiers(Modifier.PUBLIC) - .addJavadoc("Add a new {@link $T} item to the underlying collection.\n", itemType) - .addJavadoc("@param item the item to add\n") - .addJavadoc("@return {@code true}\n") - .addStatement("$T value = $T.requireNonNull($N,\"$N cannot be null\")", - itemType, ObjectUtils.class, valueParam, valueParam) - .beginControlFlow("if ($N == null)", valueField) - .addStatement("$N = new $T<>()", valueField, LinkedList.class) - .endControlFlow() - .addStatement("return $N.add(value)", valueField); - - builder.addMethod(method.build()); - } - - { - // create remove method - MethodSpec.Builder method = MethodSpec.methodBuilder("remove" + itemPropertyName) - .addParameter(valueParam) - .returns(TypeName.BOOLEAN) - .addModifiers(Modifier.PUBLIC) - .addJavadoc("Remove the first matching {@link $T} item from the underlying collection.\n", itemType) - .addJavadoc("@param item the item to remove\n") - .addJavadoc("@return {@code true} if the item was removed or {@code false} otherwise\n") - .addStatement("$T value = $T.requireNonNull($N,\"$N cannot be null\")", - itemType, ObjectUtils.class, valueParam, valueParam) - .addStatement("return $1N != null && $1N.remove(value)", valueField); - builder.addMethod(method.build()); - } - } - } - } - - private void buildKeyedMethods( - @NonNull INamedModelInstance instance, - @NonNull TypeSpec.Builder builder, - @NonNull TypeName itemType, - @NonNull String itemPropertyName, - @NonNull ParameterSpec valueParam, - @NonNull FieldSpec valueField) { - - IFlagInstance jsonKey = instance.getDefinition().getJsonKeyFlagInstance(); - if (jsonKey == null) { - throw new IllegalStateException( - String.format("JSON key not defined for property: %s", instance.toCoordinates())); - } - - // get the json key property on the instance's definition - IModelDefinitionTypeInfo instanceTypeInfo = typeResolver.getTypeInfo(instance.getDefinition()); - IFlagInstanceTypeInfo jsonKeyTypeInfo = instanceTypeInfo.getFlagInstanceTypeInfo(jsonKey); - - if (jsonKeyTypeInfo == null) { - throw new IllegalStateException( - String.format("Unable to identify JSON key for property: %s", instance.toCoordinates())); - } - - { - // create add method - MethodSpec.Builder method = MethodSpec.methodBuilder("add" + itemPropertyName) - .addParameter(valueParam) - .returns(itemType) - .addModifiers(Modifier.PUBLIC) - .addJavadoc("Add a new {@link $T} item to the underlying collection.\n", itemType) - .addJavadoc("@param item the item to add\n") - .addJavadoc("@return the existing {@link $T} item in the collection or {@code null} if not item exists\n", - itemType) - .addStatement("$1T value = $2T.requireNonNull($3N,\"$3N value cannot be null\")", - itemType, ObjectUtils.class, valueParam) - .addStatement("$1T key = $2T.requireNonNull($3N.$4N(),\"$3N key cannot be null\")", - String.class, ObjectUtils.class, valueParam, "get" + jsonKeyTypeInfo.getPropertyName()) - .beginControlFlow("if ($N == null)", valueField) - .addStatement("$N = new $T<>()", valueField, LinkedHashMap.class) // NOPMD required - .endControlFlow() - .addStatement("return $N.put(key, value)", valueField); - - builder.addMethod(method.build()); - } - { - // create remove method - MethodSpec.Builder method = MethodSpec.methodBuilder("remove" + itemPropertyName) - .addParameter(valueParam) - .returns(TypeName.BOOLEAN) - .addModifiers(Modifier.PUBLIC) - .addJavadoc("Remove the {@link $T} item from the underlying collection.\n", itemType) - .addJavadoc("@param item the item to remove\n") - .addJavadoc("@return {@code true} if the item was removed or {@code false} otherwise\n") - .addStatement("$1T value = $2T.requireNonNull($3N,\"$3N value cannot be null\")", - itemType, ObjectUtils.class, valueParam) - .addStatement("$1T key = $2T.requireNonNull($3N.$4N(),\"$3N key cannot be null\")", - String.class, ObjectUtils.class, valueParam, "get" + jsonKeyTypeInfo.getPropertyName()) - .addStatement("return $1N != null && $1N.remove(key, value)", valueField); - builder.addMethod(method.build()); - } - - } } diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/codegen/typeinfo/FlagInstanceTypeInfoImpl.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/codegen/typeinfo/FlagInstanceTypeInfoImpl.java index 2348691c5..1896bc37d 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/codegen/typeinfo/FlagInstanceTypeInfoImpl.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/codegen/typeinfo/FlagInstanceTypeInfoImpl.java @@ -46,7 +46,6 @@ import gov.nist.secauto.metaschema.databind.model.annotations.BoundFlag; import gov.nist.secauto.metaschema.databind.model.annotations.JsonFieldValueKeyFlag; import gov.nist.secauto.metaschema.databind.model.annotations.JsonKey; -import gov.nist.secauto.metaschema.databind.model.annotations.ModelUtil; import java.util.Set; @@ -97,13 +96,7 @@ public Set buildField( annotation.addMember("useIndex", "$L", index); } - TypeInfoUtils.buildNamespaceBindingAnnotation( - annotation, - "namespace", - instance.getXmlNamespace(), - () -> instance.getContainingModule().getXmlNamespace().toASCIIString(), - ModelUtil.NO_STRING_VALUE, - true); + // TODO: handle flag namespace as a prefix IFlagDefinition definition = instance.getDefinition(); @@ -128,7 +121,8 @@ public Set buildField( fieldBuilder.addAnnotation(annotation.build()); IModelDefinition parent = instance.getContainingDefinition(); - if (parent.hasJsonKey() && instance.equals(parent.getJsonKeyFlagInstance())) { + IFlagInstance jsonKey = parent.getJsonKey(); + if (instance.equals(jsonKey)) { fieldBuilder.addAnnotation(JsonKey.class); } diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/codegen/typeinfo/TypeInfoUtils.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/codegen/typeinfo/TypeInfoUtils.java index ae0d875a1..5a3f9491c 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/codegen/typeinfo/TypeInfoUtils.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/codegen/typeinfo/TypeInfoUtils.java @@ -30,14 +30,9 @@ import gov.nist.secauto.metaschema.core.datatype.markup.MarkupLine; import gov.nist.secauto.metaschema.core.datatype.markup.MarkupMultiline; -import gov.nist.secauto.metaschema.core.model.INamedInstance; import gov.nist.secauto.metaschema.core.model.INamedModelInstance; -import gov.nist.secauto.metaschema.databind.model.annotations.ModelUtil; - -import java.util.function.Supplier; import edu.umd.cs.findbugs.annotations.NonNull; -import edu.umd.cs.findbugs.annotations.Nullable; public final class TypeInfoUtils { private TypeInfoUtils() { @@ -65,42 +60,11 @@ public static void buildCommonBindingAnnotationValues( annotation.addMember("useIndex", "$L", index); } - buildNamespaceBindingAnnotation( - annotation, - "namespace", - instance.getXmlNamespace(), - () -> instance.getContainingModule().getXmlNamespace().toASCIIString(), - ModelUtil.DEFAULT_STRING_VALUE, - false); + // TODO: handle instance namespace as a prefix MarkupMultiline remarks = instance.getRemarks(); if (remarks != null) { annotation.addMember("remarks", "$S", remarks.toMarkdown()); } } - - public static void buildNamespaceBindingAnnotation( - @NonNull AnnotationSpec.Builder annotation, - @NonNull String annotationMember, - @Nullable String namespaceUri, - @NonNull Supplier defaultSupplier, - @NonNull String annotationDefault, - boolean allowNone) { - String value; - if (namespaceUri == null) { - if (allowNone) { - value = ModelUtil.NO_STRING_VALUE; - } else { - throw new IllegalStateException("Expected non-null namespace"); - } - } else if (namespaceUri.equals(defaultSupplier.get())) { - value = ModelUtil.DEFAULT_STRING_VALUE; - } else { - value = namespaceUri; - } - - if (!annotationDefault.equals(value)) { - annotation.addMember(annotationMember, "$S", value); - } - } } diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/codegen/typeinfo/def/AbstractModelDefinitionTypeInfo.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/codegen/typeinfo/def/AbstractModelDefinitionTypeInfo.java index c820d07ca..1dd1a0af1 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/codegen/typeinfo/def/AbstractModelDefinitionTypeInfo.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/codegen/typeinfo/def/AbstractModelDefinitionTypeInfo.java @@ -37,6 +37,7 @@ import gov.nist.secauto.metaschema.databind.codegen.typeinfo.IFlagInstanceTypeInfo; import gov.nist.secauto.metaschema.databind.codegen.typeinfo.IInstanceTypeInfo; import gov.nist.secauto.metaschema.databind.codegen.typeinfo.IPropertyTypeInfo; +import gov.nist.secauto.metaschema.databind.codegen.typeinfo.ITypeInfo; import gov.nist.secauto.metaschema.databind.codegen.typeinfo.ITypeResolver; import org.apache.logging.log4j.LogManager; @@ -73,8 +74,8 @@ public AbstractModelDefinitionTypeInfo( this.baseClassName = typeResolver.getBaseClassName(definition); this.flagTypeInfos = ObjectUtils.notNull(Lazy.lazy(() -> flags() .collect(CustomCollectors.toMap( - (typeInfo) -> typeInfo.getPropertyName(), - (typeInfo) -> typeInfo, + ITypeInfo::getPropertyName, + CustomCollectors.identity(), (key, v1, v2) -> { if (LOGGER.isErrorEnabled()) { LOGGER.error(String.format("Unexpected duplicate flag property name '%s'", key)); diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/codegen/typeinfo/def/AssemblyDefinitionTypeInfoImpl.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/codegen/typeinfo/def/AssemblyDefinitionTypeInfoImpl.java index 1ff07d649..77a361849 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/codegen/typeinfo/def/AssemblyDefinitionTypeInfoImpl.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/codegen/typeinfo/def/AssemblyDefinitionTypeInfoImpl.java @@ -45,6 +45,7 @@ import java.util.LinkedHashMap; import java.util.Map; +import java.util.function.Function; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -67,8 +68,8 @@ public AssemblyDefinitionTypeInfoImpl(@NonNull IAssemblyDefinition definition, @ getFlagInstanceTypeInfos().stream(), processModel(definition)) .collect(CustomCollectors.toMap( - (typeInfo) -> typeInfo.getInstance(), - (typeInfo) -> typeInfo, + IInstanceTypeInfo::getInstance, + CustomCollectors.identity(), (key, v1, v2) -> { if (LOGGER.isErrorEnabled()) { LOGGER.error(String.format("Unexpected duplicate property name '%s'", key)); @@ -78,8 +79,8 @@ public AssemblyDefinitionTypeInfoImpl(@NonNull IAssemblyDefinition definition, @ LinkedHashMap::new)))); this.propertyNameToTypeInfoMap = ObjectUtils.notNull(Lazy.lazy(() -> getInstanceTypeInfoMap().values().stream() .collect(Collectors.toMap( - (typeInfo) -> typeInfo.getPropertyName(), - (typeInfo) -> typeInfo, + IInstanceTypeInfo::getPropertyName, + Function.identity(), (v1, v2) -> v2, LinkedHashMap::new)))); } diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/codegen/typeinfo/def/FieldDefinitionTypeInfoImpl.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/codegen/typeinfo/def/FieldDefinitionTypeInfoImpl.java index 04573b260..078c00ac3 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/codegen/typeinfo/def/FieldDefinitionTypeInfoImpl.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/codegen/typeinfo/def/FieldDefinitionTypeInfoImpl.java @@ -31,6 +31,7 @@ import gov.nist.secauto.metaschema.core.util.CustomCollectors; import gov.nist.secauto.metaschema.core.util.ObjectUtils; import gov.nist.secauto.metaschema.databind.codegen.typeinfo.IFieldValueTypeInfo; +import gov.nist.secauto.metaschema.databind.codegen.typeinfo.IFlagInstanceTypeInfo; import gov.nist.secauto.metaschema.databind.codegen.typeinfo.IInstanceTypeInfo; import gov.nist.secauto.metaschema.databind.codegen.typeinfo.IPropertyTypeInfo; import gov.nist.secauto.metaschema.databind.codegen.typeinfo.ITypeResolver; @@ -40,6 +41,7 @@ import java.util.LinkedHashMap; import java.util.Map; +import java.util.function.Function; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -60,8 +62,8 @@ public FieldDefinitionTypeInfoImpl(@NonNull IFieldDefinition definition, @NonNul super(definition, typeResolver); this.instanceToTypeInfoMap = ObjectUtils.notNull(Lazy.lazy(() -> getFlagInstanceTypeInfos().stream() .collect(CustomCollectors.toMap( - (typeInfo) -> typeInfo.getInstance(), - (typeInfo) -> typeInfo, + IFlagInstanceTypeInfo::getInstance, + CustomCollectors.identity(), (key, v1, v2) -> { if (LOGGER.isErrorEnabled()) { LOGGER.error(String.format("Unexpected duplicate property name '%s'", key)); @@ -73,8 +75,8 @@ public FieldDefinitionTypeInfoImpl(@NonNull IFieldDefinition definition, @NonNul getInstanceTypeInfoMap().values().stream(), Stream.of((IPropertyTypeInfo) IFieldValueTypeInfo.newTypeInfo(this))) .collect(Collectors.toMap( - (typeInfo) -> typeInfo.getPropertyName(), - (typeInfo) -> typeInfo, + IPropertyTypeInfo::getPropertyName, + Function.identity(), (v1, v2) -> v2, LinkedHashMap::new)))); } diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/io/Format.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/io/Format.java index 566f5a3eb..ec3d67b4b 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/io/Format.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/io/Format.java @@ -26,9 +26,13 @@ package gov.nist.secauto.metaschema.databind.io; +import gov.nist.secauto.metaschema.core.util.CollectionUtil; +import gov.nist.secauto.metaschema.core.util.ObjectUtils; + import java.util.Arrays; import java.util.List; import java.util.Locale; +import java.util.Set; import java.util.stream.Collectors; import edu.umd.cs.findbugs.annotations.NonNull; @@ -41,20 +45,20 @@ public enum Format { /** * The Extensible Markup Language format. */ - XML(".xml"), + XML(Set.of(".xml")), /** * The JavaScript Object Notation format. */ - JSON(".json"), + JSON(Set.of(".json")), /** * The YAML Ain't Markup Language format. */ - YAML(".yml"); + YAML(Set.of(".yml", ".yaml")); private static final List NAMES; @NonNull - private final String defaultExtension; + private final Set defaultExtensions; static { NAMES = Arrays.stream(values()) @@ -72,8 +76,8 @@ public static List names() { return NAMES; } - Format(@NonNull String defaultExtension) { - this.defaultExtension = defaultExtension; + Format(Set defaultExtensions) { + this.defaultExtensions = CollectionUtil.unmodifiableSet(ObjectUtils.requireNonNull(defaultExtensions)); } /** @@ -82,7 +86,7 @@ public static List names() { * @return the default extension */ @NonNull - public String getDefaultExtension() { - return defaultExtension; + public Set getDefaultExtension() { + return defaultExtensions; } } diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/io/json/MetaschemaJsonReader.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/io/json/MetaschemaJsonReader.java index a033a95d6..fd208175c 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/io/json/MetaschemaJsonReader.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/io/json/MetaschemaJsonReader.java @@ -117,7 +117,7 @@ public MetaschemaJsonReader( @NonNull JsonParser parser, @NonNull IJsonProblemHandler problemHandler) throws IOException { this.problemHandler = problemHandler; - this.objectMapper = ObjectUtils.notNull(Lazy.lazy(() -> new ObjectMapper())); + this.objectMapper = ObjectUtils.notNull(Lazy.lazy(ObjectMapper::new)); push(parser); } @@ -278,7 +278,7 @@ public Object readItemField(Object parentItem, IBoundInstanceModelFieldComplex i parentItem, instance.getDefinition(), instance.getJsonProperties(), - instance.getJsonKey(), + instance.getEffectiveJsonKey(), getProblemHandler()); } @@ -295,7 +295,7 @@ public Object readItemField(Object parentItem, IBoundInstanceModelGroupedField i return readComplexDefinitionObject( parentItem, definition, - instance.getJsonKey(), + instance.getEffectiveJsonKey(), new PropertyBodyHandler(instance.getJsonProperties()), actualProblemHandler); } @@ -333,7 +333,7 @@ public Object readItemAssembly(Object parentItem, IBoundInstanceModelGroupedAsse return readComplexDefinitionObject( parentItem, instance.getDefinition(), - instance.getJsonKey(), + instance.getEffectiveJsonKey(), new PropertyBodyHandler(instance.getJsonProperties()), new GroupedInstanceProblemHandler(instance, getProblemHandler())); } @@ -511,11 +511,11 @@ public void accept( handled = true; } - if (!(handled || problemHandler.handleUnknownProperty( + if (!handled && !problemHandler.handleUnknownProperty( definition, parentItem, propertyName, - getInstanceReader()))) { + getInstanceReader())) { if (LOGGER.isWarnEnabled()) { LOGGER.warn("Skipping unhandled JSON field '{}' {}.", propertyName, JsonUtil.toString(parser)); } @@ -747,7 +747,6 @@ public Object readItem() throws IOException { IBoundInstanceModel instance = getCollectionInfo().getInstance(); return instance.readItem(getParentObject(), getItemReader()); } - } @SuppressWarnings("unchecked") @@ -771,7 +770,7 @@ public T readProperty( T retval = null; JsonToken token; - while (!(JsonToken.END_OBJECT.equals(token = parser.currentToken()) || token == null)) { + while (!JsonToken.END_OBJECT.equals(token = parser.currentToken()) && token != null) { if (!JsonToken.FIELD_NAME.equals(token)) { throw new IOException(String.format("Expected FIELD_NAME token, found '%s'", token.toString())); } diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/io/json/MetaschemaJsonUtil.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/io/json/MetaschemaJsonUtil.java index 0d9a3ff62..dae2222fe 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/io/json/MetaschemaJsonUtil.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/io/json/MetaschemaJsonUtil.java @@ -76,7 +76,7 @@ private MetaschemaJsonUtil() { Collection flags = targetDefinition.getFlagInstances(); int flagCount = flags.size() - (jsonKey == null ? 0 : 1); - @SuppressWarnings("resource") Stream instanceStream; + Stream instanceStream; if (targetDefinition instanceof IBoundDefinitionModelAssembly) { // use all child instances instanceStream = ((IBoundDefinitionModelAssembly) targetDefinition).getModelInstances().stream() @@ -100,7 +100,7 @@ private MetaschemaJsonUtil() { if (jsonKey != null) { instanceStream = Stream.concat( - flags.stream().filter((flag) -> !jsonKey.equals(flag)), + flags.stream().filter(flag -> !jsonKey.equals(flag)), instanceStream); } else { instanceStream = Stream.concat( diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/io/xml/DefaultXmlDeserializer.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/io/xml/DefaultXmlDeserializer.java index 35c07b78b..4c4da37e9 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/io/xml/DefaultXmlDeserializer.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/io/xml/DefaultXmlDeserializer.java @@ -44,6 +44,7 @@ import java.net.URI; import javax.xml.stream.EventFilter; +import javax.xml.stream.XMLEventReader; import javax.xml.stream.XMLInputFactory; import javax.xml.stream.XMLResolver; import javax.xml.stream.XMLStreamException; @@ -99,21 +100,16 @@ private XMLInputFactory2 getXMLInputFactory() { xmlInputFactory.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, true); xmlInputFactory.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, true); xmlInputFactory.setProperty(XMLInputFactory.SUPPORT_DTD, true); - xmlInputFactory.setProperty(XMLInputFactory.RESOLVER, new XMLResolver() { - - @Override - public Object resolveEntity(String publicID, String systemID, String baseURI, String namespace) - throws XMLStreamException { - URI base = URI.create(baseURI); - URI resource = base.resolve(systemID); - try { - return resource.toURL().openStream(); - } catch (IOException ex) { - throw new XMLStreamException(ex); - } - } - - }); + xmlInputFactory.setProperty(XMLInputFactory.RESOLVER, + (XMLResolver) (publicID, systemID, baseURI, namespace) -> { + URI base = URI.create(baseURI); + URI resource = base.resolve(systemID); + try { + return resource.toURL().openStream(); + } catch (IOException ex) { + throw new XMLStreamException(ex); + } + }); } } return ObjectUtils.notNull(xmlInputFactory); @@ -153,7 +149,7 @@ protected final IDocumentNodeItem deserializeToNodeItemInternal(Reader reader, U public final CLASS deserializeToValue(Reader reader, URI documentUri) throws IOException { // doesn't auto close the underlying reader try (AutoCloser closer = new AutoCloser<>( - newXMLEventReader2(documentUri, reader), event -> event.close())) { + newXMLEventReader2(documentUri, reader), XMLEventReader::close)) { return parseXmlInternal(closer.getResource()); } catch (XMLStreamException ex) { throw new IOException("Unable to create a new XMLEventReader2 instance.", ex); diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/io/xml/MetaschemaXmlWriter.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/io/xml/MetaschemaXmlWriter.java index 1ca2f3ca9..c3d71c366 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/io/xml/MetaschemaXmlWriter.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/io/xml/MetaschemaXmlWriter.java @@ -442,7 +442,9 @@ protected AbstractItemWriter(@NonNull QName parentQName) { } /** - * @return the startElement + * Get the qualified name of the item's parent. + * + * @return the qualified name */ @NonNull protected QName getParentQName() { diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/metapath/function/Model.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/metapath/function/Model.java index 176f4b6c4..e676430a3 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/metapath/function/Model.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/metapath/function/Model.java @@ -34,11 +34,9 @@ import gov.nist.secauto.metaschema.core.metapath.item.IItem; import gov.nist.secauto.metaschema.core.metapath.item.node.IDefinitionNodeItem; import gov.nist.secauto.metaschema.core.metapath.item.node.INodeItem; -import gov.nist.secauto.metaschema.core.model.IDefinition; import gov.nist.secauto.metaschema.core.model.INamedInstance; import gov.nist.secauto.metaschema.core.model.MetaschemaModelConstants; import gov.nist.secauto.metaschema.core.util.ObjectUtils; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBinding; import java.util.List; @@ -58,7 +56,7 @@ public final class Model { .focusIndependent() .contextIndependent() .deterministic() - .returnOne() + .returnZeroOrOne() .functionHandler(Model::execute) .build(); @@ -96,15 +94,8 @@ public static ISequence execute( public static INodeItem getModel(@NonNull IDefinitionNodeItem definitionNodeItem) { INamedInstance instance = definitionNodeItem.getInstance(); - INodeItem retval = null; - if (instance instanceof IBinding) { - retval = ((IBinding) instance).getBoundNodeItem(); - } else { - IDefinition definition = definitionNodeItem.getDefinition(); - if (definition instanceof IBinding) { - retval = ((IBinding) definition).getBoundNodeItem(); - } - } - return retval; + return instance != null + ? instance.getNodeItem() + : definitionNodeItem.getDefinition().getNodeItem(); } } diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/AbstractBoundModule.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/AbstractBoundModule.java index 8929a7856..280e85d67 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/AbstractBoundModule.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/AbstractBoundModule.java @@ -43,6 +43,8 @@ import java.util.function.Function; import java.util.stream.Collectors; +import javax.xml.namespace.QName; + import edu.umd.cs.findbugs.annotations.NonNull; import nl.talsmasoftware.lazy4j.Lazy; @@ -57,9 +59,9 @@ public abstract class AbstractBoundModule @NonNull private final IBindingContext bindingContext; @NonNull - private final Lazy> assemblyDefinitions; + private final Lazy> assemblyDefinitions; @NonNull - private final Lazy> fieldDefinitions; + private final Lazy> fieldDefinitions; /** * Create a new Module instance for a given class annotated by the @@ -140,7 +142,7 @@ protected AbstractBoundModule( .requireNonNull(bindingContext.getBoundDefinitionForClass(clazz)); }) .collect(Collectors.toUnmodifiableMap( - IBoundDefinitionModelAssembly::getName, + IBoundDefinitionModelAssembly::getDefinitionQName, Function.identity())))); this.fieldDefinitions = ObjectUtils.notNull(Lazy.lazy(() -> Arrays.stream(getFieldClasses()) .map(clazz -> { @@ -149,7 +151,7 @@ protected AbstractBoundModule( .requireNonNull(bindingContext.getBoundDefinitionForClass(clazz)); }) .collect(Collectors.toUnmodifiableMap( - IBoundDefinitionModelField::getName, + IBoundDefinitionModelField::getDefinitionQName, Function.identity())))); } @@ -199,7 +201,7 @@ protected Class[] getFieldClasses() { * * @return the mapping */ - protected Map getAssemblyDefinitionMap() { + protected Map getAssemblyDefinitionMap() { return assemblyDefinitions.get(); } @@ -210,7 +212,7 @@ public Collection getAssemblyDefinitions() { } @Override - public IBoundDefinitionModelAssembly getAssemblyDefinitionByName(@NonNull String name) { + public IBoundDefinitionModelAssembly getAssemblyDefinitionByName(@NonNull QName name) { return getAssemblyDefinitionMap().get(name); } @@ -219,7 +221,7 @@ public IBoundDefinitionModelAssembly getAssemblyDefinitionByName(@NonNull String * * @return the mapping */ - protected Map getFieldDefinitionMap() { + protected Map getFieldDefinitionMap() { return fieldDefinitions.get(); } @@ -230,7 +232,7 @@ public Collection getFieldDefinitions() { } @Override - public IBoundDefinitionModelField getFieldDefinitionByName(@NonNull String name) { + public IBoundDefinitionModelField getFieldDefinitionByName(@NonNull QName name) { return getFieldDefinitionMap().get(name); } @@ -242,7 +244,7 @@ public Collection getFlagDefinitions() { } @Override - public IBoundDefinitionFlag getFlagDefinitionByName(@NonNull String name) { + public IBoundDefinitionFlag getFlagDefinitionByName(@NonNull QName name) { // Flags are always inline, so they do not have separate definitions return null; } diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundContainerFlag.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundContainerFlag.java deleted file mode 100644 index 2f488944c..000000000 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundContainerFlag.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Portions of this software was developed by employees of the National Institute - * of Standards and Technology (NIST), an agency of the Federal Government and is - * being made available as a public service. Pursuant to title 17 United States - * Code Section 105, works of NIST employees are not subject to copyright - * protection in the United States. This software may be subject to foreign - * copyright. Permission in the United States and in foreign countries, to the - * extent that NIST may hold copyright, to use, copy, modify, create derivative - * works, and distribute this software and its documentation without fee is hereby - * granted on a non-exclusive basis, provided that this notice and disclaimer - * of warranty appears in all copies. - * - * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER - * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY - * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM - * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE - * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT - * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, - * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, - * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, - * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR - * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT - * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. - */ - -package gov.nist.secauto.metaschema.databind.model; - -import java.util.Collection; - -import javax.xml.namespace.QName; - -import gov.nist.secauto.metaschema.core.model.IContainerFlag; - -public interface IBoundContainerFlag extends IContainerFlag { - @Override - IBoundInstanceFlag getFlagInstanceByName(QName name); - - @Override - Collection getFlagInstances(); - // - // @Override - // IBoundInstanceFlag getJsonKeyFlagInstance(); -} diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundContainerModelAssembly.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundContainerModelAssembly.java index 6592eb78a..abbdbe15d 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundContainerModelAssembly.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundContainerModelAssembly.java @@ -26,6 +26,8 @@ package gov.nist.secauto.metaschema.databind.model; +import gov.nist.secauto.metaschema.core.model.IContainerModelAssembly; + import java.util.Collection; import java.util.Map; @@ -33,7 +35,6 @@ import edu.umd.cs.findbugs.annotations.NonNull; import edu.umd.cs.findbugs.annotations.Nullable; -import gov.nist.secauto.metaschema.core.model.IContainerModelAssembly; public interface IBoundContainerModelAssembly extends IContainerModelAssembly { @Override diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundContainerModelChoiceGroup.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundContainerModelChoiceGroup.java index 640f6cc20..c8523e9f1 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundContainerModelChoiceGroup.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundContainerModelChoiceGroup.java @@ -26,20 +26,20 @@ package gov.nist.secauto.metaschema.databind.model; +import gov.nist.secauto.metaschema.core.model.IContainerModelGrouped; + import java.util.Collection; import javax.xml.namespace.QName; import edu.umd.cs.findbugs.annotations.NonNull; import edu.umd.cs.findbugs.annotations.Nullable; -import gov.nist.secauto.metaschema.core.model.IContainerModelGrouped; public interface IBoundContainerModelChoiceGroup extends IContainerModelGrouped { @Override - default Collection getModelInstances() { - return getNamedModelInstances(); - } + @NonNull + Collection getModelInstances(); @Override @NonNull diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundDefinition.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundDefinition.java deleted file mode 100644 index 0720957c0..000000000 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundDefinition.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Portions of this software was developed by employees of the National Institute - * of Standards and Technology (NIST), an agency of the Federal Government and is - * being made available as a public service. Pursuant to title 17 United States - * Code Section 105, works of NIST employees are not subject to copyright - * protection in the United States. This software may be subject to foreign - * copyright. Permission in the United States and in foreign countries, to the - * extent that NIST may hold copyright, to use, copy, modify, create derivative - * works, and distribute this software and its documentation without fee is hereby - * granted on a non-exclusive basis, provided that this notice and disclaimer - * of warranty appears in all copies. - * - * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER - * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY - * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM - * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE - * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT - * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, - * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, - * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, - * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR - * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT - * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. - */ - -package gov.nist.secauto.metaschema.databind.model; - -import gov.nist.secauto.metaschema.core.model.IDefinition; -import gov.nist.secauto.metaschema.databind.IBindingContext; - -import edu.umd.cs.findbugs.annotations.NonNull; - -/** - * Represents a Metaschema module definition bound to Java data. - */ -public interface IBoundDefinition extends IDefinition, IBoundModuleElement { - /** - * Get the binding context used for the definition. - * - * @return the binding context - */ - @NonNull - IBindingContext getBindingContext(); - - /** - * Get the Metaschema module containing this definition. - */ - @Override - IBoundModule getContainingModule(); -} diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundDefinitionFlag.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundDefinitionFlag.java index 01ffec7ef..c4c622ff5 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundDefinitionFlag.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundDefinitionFlag.java @@ -32,6 +32,6 @@ * Represents a flag definition/instance bound to Java field. */ public interface IBoundDefinitionFlag - extends IFlagDefinition, IBoundDefinition { + extends IFlagDefinition, IBoundModuleElement { // no additional methods } diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundDefinitionModel.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundDefinitionModel.java index 4ec46346e..e19dfebca 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundDefinitionModel.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundDefinitionModel.java @@ -26,20 +26,29 @@ package gov.nist.secauto.metaschema.databind.model; -import gov.nist.secauto.metaschema.core.model.IModelDefinition; +import gov.nist.secauto.metaschema.core.model.IFeatureContainerFlag; +import gov.nist.secauto.metaschema.databind.IBindingContext; + +import edu.umd.cs.findbugs.annotations.NonNull; /** * Represents a field or assembly instance bound to Java data. */ -// REFACTOR: rename to IBoundDefinitionModelNamed +// REFACTOR: rename to IBoundDefinitionModel public interface IBoundDefinitionModel - extends IBoundDefinition, IBoundContainerFlag, IModelDefinition { - // @NonNull - // Class getBoundClass(); + extends IBoundModuleElement, IFeatureContainerFlag { + /** + * Get the binding context used for the definition. + * + * @return the binding context + */ + @NonNull + IBindingContext getBindingContext(); @Override IBoundInstanceModelNamed getInlineInstance(); - @Override - IBoundInstanceFlag getJsonKeyFlagInstance(); + // + // @Override + // IBoundInstanceFlag getJsonKeyFlagInstance(); } diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundDefinitionModelAssembly.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundDefinitionModelAssembly.java index 58746ae9e..0dba4b3ab 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundDefinitionModelAssembly.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundDefinitionModelAssembly.java @@ -33,6 +33,7 @@ import java.io.IOException; import java.util.Map; +import java.util.function.Function; import java.util.function.Predicate; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -50,7 +51,6 @@ public interface IBoundDefinitionModelAssembly // Assembly Definition Features // ============================ - @Override @NonNull default IBoundDefinitionModelAssembly getOwningDefinition() { @@ -86,8 +86,7 @@ default Map getJsonProperties(@Nullable Predicate p.getJsonName(), (p) -> p))); + .collect(Collectors.toUnmodifiableMap(IBoundProperty::getJsonName, Function.identity()))); } @Override @@ -101,11 +100,6 @@ default void writeItem(Object item, IItemWriteHandler handler) throws IOExceptio handler.writeItemAssembly(item, this); } - @Override - default boolean canHandleJsonPropertyName(@NonNull String name) { - return name.equals(getRootJsonName()); - } - @Override default boolean canHandleXmlQName(@NonNull QName qname) { return qname.equals(getRootXmlQName()); diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundDefinitionModelComplex.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundDefinitionModelComplex.java index b629c9b44..ef39ace33 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundDefinitionModelComplex.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundDefinitionModelComplex.java @@ -27,13 +27,15 @@ package gov.nist.secauto.metaschema.databind.model; import gov.nist.secauto.metaschema.core.util.ObjectUtils; +import gov.nist.secauto.metaschema.databind.io.BindingException; import gov.nist.secauto.metaschema.databind.model.info.IFeatureComplexItemValueHandler; +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; import java.util.Map; import java.util.function.Predicate; -import javax.xml.namespace.QName; - import edu.umd.cs.findbugs.annotations.NonNull; import edu.umd.cs.findbugs.annotations.Nullable; @@ -42,10 +44,105 @@ */ public interface IBoundDefinitionModelComplex extends IBoundDefinitionModel, IFeatureComplexItemValueHandler { - @Override - @NonNull - Class getBoundClass(); @NonNull Map getJsonProperties(@Nullable Predicate flagFilter); + + @Override + default boolean isInline() { + return getBoundClass().getEnclosingClass() != null; + } + + /** + * Gets a new instance of the bound class. + * + * @param + * the type of the bound class + * @return a Java object for the class + * @throws RuntimeException + * if the instance cannot be created due to a binding error + */ + @SuppressWarnings("PMD.AvoidThrowingRawExceptionTypes") + @Override + @NonNull + default CLASS newInstance() { + Class clazz = getBoundClass(); + try { + @SuppressWarnings("unchecked") Constructor constructor + = (Constructor) clazz.getDeclaredConstructor(); + return ObjectUtils.notNull(constructor.newInstance()); + } catch (NoSuchMethodException ex) { + String msg = String.format("Class '%s' does not have a required no-arg constructor.", clazz.getName()); + throw new RuntimeException(msg, ex); + } catch (InstantiationException | IllegalAccessException | InvocationTargetException ex) { + throw new RuntimeException(ex); + } + } + + @Nullable + Method getBeforeDeserializeMethod(); + + /** + * Calls the method named "beforeDeserialize" on each class in the object's + * hierarchy if the method exists on the class. + *

+ * These methods can be used to set the initial state of the target bound object + * before data is read and applied during deserialization. + * + * @param targetObject + * the data object target to call the method(s) on + * @param parentObject + * the object target's parent object, which is used as the method + * argument + * @throws BindingException + * if an error occurs while calling the method + */ + @Override + default void callBeforeDeserialize(Object targetObject, Object parentObject) throws BindingException { + Method beforeDeserializeMethod = getBeforeDeserializeMethod(); + if (beforeDeserializeMethod != null) { + try { + beforeDeserializeMethod.invoke(targetObject, parentObject); + } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) { + throw new BindingException(ex); + } + } + } + + @Nullable + Method getAfterDeserializeMethod(); + + /** + * Calls the method named "afterDeserialize" on each class in the object's + * hierarchy if the method exists. + *

+ * These methods can be used to modify the state of the target bound object + * after data is read and applied during deserialization. + * + * @param targetObject + * the data object target to call the method(s) on + * @param parentObject + * the object target's parent object, which is used as the method + * argument + * @throws BindingException + * if an error occurs while calling the method + */ + @Override + default void callAfterDeserialize(Object targetObject, Object parentObject) throws BindingException { + Method afterDeserializeMethod = getAfterDeserializeMethod(); + if (afterDeserializeMethod != null) { + try { + afterDeserializeMethod.invoke(targetObject, parentObject); + } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) { + throw new BindingException(ex); + } + } + } + + // @Override + // public String getJsonKeyFlagName() { + // // definition items never have a JSON key + // return null; + // } + } diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundDefinitionModelFieldComplex.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundDefinitionModelFieldComplex.java index 5bd17bd3f..f8bee845e 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundDefinitionModelFieldComplex.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundDefinitionModelFieldComplex.java @@ -34,6 +34,7 @@ import java.io.IOException; import java.util.Collection; import java.util.Map; +import java.util.function.Function; import java.util.function.Predicate; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -116,7 +117,7 @@ default Map getJsonProperties(@Nullable Predicate jsonValueKeyFilter = (flag) -> !flag.equals(jsonValueKey); + Predicate jsonValueKeyFilter = flag -> !flag.equals(jsonValueKey); actualFlagFilter = actualFlagFilter == null ? jsonValueKeyFilter : actualFlagFilter.and(jsonValueKeyFilter); // ensure the field value is omitted too! fieldValue = null; @@ -144,8 +145,7 @@ default Map getJsonProperties(@Nullable Predicate p.getJsonName(), (p) -> p))); + .collect(Collectors.toUnmodifiableMap(IBoundProperty::getJsonName, Function.identity()))); } @Override @@ -159,12 +159,6 @@ default void writeItem(Object item, IItemWriteHandler handler) throws IOExceptio handler.writeItemField(item, this); } - @Override - default boolean canHandleJsonPropertyName(String name) { - // not handled, since not root - return false; - } - @Override default boolean canHandleXmlQName(QName qname) { // not handled, since not root diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundFieldValue.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundFieldValue.java index 06f17f442..25b4e017c 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundFieldValue.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundFieldValue.java @@ -26,7 +26,6 @@ package gov.nist.secauto.metaschema.databind.model; -import gov.nist.secauto.metaschema.core.util.CollectionUtil; import gov.nist.secauto.metaschema.core.util.ObjectUtils; import gov.nist.secauto.metaschema.databind.io.BindingException; import gov.nist.secauto.metaschema.databind.model.info.IFeatureScalarItemValueHandler; @@ -34,7 +33,6 @@ import gov.nist.secauto.metaschema.databind.model.info.IItemWriteHandler; import java.io.IOException; -import java.util.Collection; import javax.xml.namespace.QName; @@ -42,12 +40,6 @@ import edu.umd.cs.findbugs.annotations.Nullable; public interface IBoundFieldValue extends IFeatureScalarItemValueHandler, IBoundProperty { - - @Override - default IBoundFieldValue getInstance() { - return this; - } - @Override @Nullable Object getDefaultValue(); @@ -89,21 +81,6 @@ default Object getEffectiveDefaultValue() { return getDefaultValue(); } - @Override - default Object getValue(Object parent) { - return IBoundProperty.super.getValue(parent); - } - - @Override - default void setValue(Object parentObject, Object value) { - IBoundProperty.super.setValue(parentObject, value); - } - - @Override - default Collection getItemValues(Object value) { - return value == null ? CollectionUtil.emptyList() : CollectionUtil.singleton(value); - } - @Override default Object readItem(Object parent, IItemReadHandler handler) throws IOException { return handler.readItemFieldValue(ObjectUtils.requireNonNull(parent, "parent"), this); @@ -120,12 +97,6 @@ default void deepCopy(@NonNull Object fromInstance, @NonNull Object toInstance) setValue(toInstance, value); } - @Override - default boolean canHandleJsonPropertyName(String name) { - // REFACTOR: Is this correct? - return name.equals(getJsonValueKeyName()); - } - @Override default boolean canHandleXmlQName(QName qname) { // REFACTOR: Is this correct? diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundInstance.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundInstance.java index 91884cce0..1f99e1306 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundInstance.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundInstance.java @@ -28,6 +28,9 @@ import gov.nist.secauto.metaschema.core.model.IInstance; +import edu.umd.cs.findbugs.annotations.NonNull; +import edu.umd.cs.findbugs.annotations.Nullable; + /** * Represents a Metaschema module instance bound to Java data. */ @@ -38,15 +41,21 @@ public interface IBoundInstance extends IBoundProperty, IInstance { /** * {@inheritDoc} *

- * An instance is contained in the parent definition's module. + * Always bound to a field. */ @Override - default IBoundModule getContainingModule() { - return getContainingDefinition().getContainingModule(); + @Nullable + default Object getValue(@NonNull Object parent) { + return IBoundProperty.super.getValue(parent); } + /** + * {@inheritDoc} + *

+ * Always bound to a field. + */ @Override - default Object getValue(Object parent) { - return IBoundProperty.super.getValue(parent); + default void setValue(@NonNull Object parentObject, @Nullable Object value) { + IBoundProperty.super.setValue(parentObject, value); } } diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundInstanceFlag.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundInstanceFlag.java index f04701ccf..7f0b8daa3 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundInstanceFlag.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundInstanceFlag.java @@ -26,8 +26,8 @@ package gov.nist.secauto.metaschema.databind.model; +import gov.nist.secauto.metaschema.core.model.IFeatureDefinitionInstanceInlined; import gov.nist.secauto.metaschema.core.model.IFlagInstance; -import gov.nist.secauto.metaschema.core.util.CollectionUtil; import gov.nist.secauto.metaschema.core.util.ObjectUtils; import gov.nist.secauto.metaschema.databind.io.BindingException; import gov.nist.secauto.metaschema.databind.model.impl.InstanceFlagInline; @@ -37,12 +37,10 @@ import java.io.IOException; import java.lang.reflect.Field; -import java.util.Collection; import javax.xml.namespace.QName; import edu.umd.cs.findbugs.annotations.NonNull; -import edu.umd.cs.findbugs.annotations.Nullable; /** * Represents a flag instance bound to Java data. @@ -50,7 +48,7 @@ public interface IBoundInstanceFlag extends IFlagInstance, IBoundDefinitionFlag, IFeatureScalarItemValueHandler, - IFeatureBoundDefinitionInline { + IBoundInstance, IFeatureDefinitionInstanceInlined { /** * Create a new bound flag instance. @@ -101,12 +99,6 @@ default IBoundDefinitionModel getParentContainer() { return getContainingDefinition(); } - @Override - @NonNull - default IBoundInstanceFlag getInstance() { - return this; - } - /** * {@inheritDoc} *

@@ -114,9 +106,7 @@ default IBoundInstanceFlag getInstance() { */ @Override @NonNull - default IBoundInstanceFlag getDefinition() { - return this; - } + IBoundDefinitionFlag getDefinition(); @Override @NonNull @@ -125,32 +115,6 @@ default IBoundInstanceFlag getInlineInstance() { return this; } - @Override - default Collection getItemValues(Object value) { - return value == null ? CollectionUtil.emptyList() : CollectionUtil.singleton(value); - } - - /** - * {@inheritDoc} - *

- * Always bound to a field. - */ - @Override - @Nullable - default Object getValue(@NonNull Object parent) { - return IFeatureBoundDefinitionInline.super.getValue(parent); - } - - /** - * {@inheritDoc} - *

- * Always bound to a field. - */ - @Override - default void setValue(@NonNull Object parentObject, @Nullable Object value) { - IFeatureBoundDefinitionInline.super.setValue(parentObject, value); - } - @Override default void deepCopy(@NonNull Object fromInstance, @NonNull Object toInstance) throws BindingException { Object value = getValue(fromInstance); @@ -170,11 +134,6 @@ default void writeItem(Object item, IItemWriteHandler handler) throws IOExceptio handler.writeItemFlag(item, this); } - @Override - default boolean canHandleJsonPropertyName(@NonNull String name) { - return name.equals(getJsonName()); - } - @Override default boolean canHandleXmlQName(@NonNull QName qname) { return qname.equals(getXmlQName()); diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundInstanceModel.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundInstanceModel.java index e8a3945b9..29839a477 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundInstanceModel.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundInstanceModel.java @@ -114,23 +114,4 @@ default Collection getItemValues(Object value) { @Nullable IBoundInstanceFlag getItemJsonKey(@NonNull Object item); - /** - * {@inheritDoc} - *

- * Always bound to a field. - */ - @Override - default Object getValue(Object parent) { - return IBoundInstance.super.getValue(parent); - } - - /** - * {@inheritDoc} - *

- * Always bound to a field. - */ - @Override - default void setValue(Object parentObject, Object value) { - IBoundInstance.super.setValue(parentObject, value); - } } diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundInstanceModelAssembly.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundInstanceModelAssembly.java index 4321e0943..7a7dd64c3 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundInstanceModelAssembly.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundInstanceModelAssembly.java @@ -31,6 +31,7 @@ import gov.nist.secauto.metaschema.databind.IBindingContext; import gov.nist.secauto.metaschema.databind.io.BindingException; import gov.nist.secauto.metaschema.databind.model.impl.InstanceModelAssemblyComplex; +import gov.nist.secauto.metaschema.databind.model.info.IFeatureComplexItemValueHandler; import gov.nist.secauto.metaschema.databind.model.info.IItemReadHandler; import gov.nist.secauto.metaschema.databind.model.info.IItemWriteHandler; @@ -43,8 +44,7 @@ * Represents an assembly instance bound to Java field. */ public interface IBoundInstanceModelAssembly - extends IBoundInstanceModelNamedComplex, IAssemblyInstanceAbsolute { - + extends IBoundInstanceModelNamed, IAssemblyInstanceAbsolute, IFeatureComplexItemValueHandler { /** * Create a new bound assembly instance. * @@ -62,7 +62,10 @@ static IBoundInstanceModelAssembly newInstance( IBindingContext bindingContext = containingDefinition.getBindingContext(); IBoundDefinitionModel definition = bindingContext.getBoundDefinitionForClass(itemType); if (definition instanceof IBoundDefinitionModelAssembly) { - return new InstanceModelAssemblyComplex(field, (IBoundDefinitionModelAssembly) definition, containingDefinition); + return InstanceModelAssemblyComplex.newInstance( + field, + (IBoundDefinitionModelAssembly) definition, + containingDefinition); } throw new IllegalStateException(String.format( @@ -71,11 +74,6 @@ static IBoundInstanceModelAssembly newInstance( field.getDeclaringClass().getName())); } - @Override - default IBoundInstanceModelAssembly getInstance() { - return this; - } - @Override @NonNull IBoundDefinitionModelAssembly getDefinition(); diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundInstanceModelChoiceGroup.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundInstanceModelChoiceGroup.java index 5465937a6..1d90878e2 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundInstanceModelChoiceGroup.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundInstanceModelChoiceGroup.java @@ -60,12 +60,7 @@ public interface IBoundInstanceModelChoiceGroup static IBoundInstanceModelChoiceGroup newInstance( @NonNull Field field, @NonNull IBoundDefinitionModelAssembly containingDefinition) { - return new InstanceModelChoiceGroup(field, containingDefinition); - } - - @Override - default IBoundInstanceModelChoiceGroup getInstance() { - return this; + return InstanceModelChoiceGroup.newInstance(field, containingDefinition); } @Override @@ -150,11 +145,6 @@ default Object deepCopyItem(Object item, Object parentInstance) throws BindingEx return itemInstance.deepCopyItem(itemInstance, parentInstance); } - @Override - default boolean canHandleJsonPropertyName(@NonNull String name) { - return name.equals(getJsonName()); - } - @Override default boolean canHandleXmlQName(@NonNull QName qname) { return getGroupedModelInstance(qname) != null; diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundInstanceModelField.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundInstanceModelField.java index 2208c2a7b..d3307841f 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundInstanceModelField.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundInstanceModelField.java @@ -63,9 +63,9 @@ static IBoundInstanceModelField newInstance( IBoundInstanceModelField retval; if (definition == null) { - retval = new InstanceModelFieldScalar(field, containingDefinition); + retval = InstanceModelFieldScalar.newInstance(field, containingDefinition); } else if (definition instanceof DefinitionField) { - retval = new InstanceModelFieldComplex(field, (DefinitionField) definition, containingDefinition); + retval = InstanceModelFieldComplex.newInstance(field, (DefinitionField) definition, containingDefinition); } else { throw new IllegalStateException(String.format( "The field '%s' on class '%s' is not bound to a Metaschema field", diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundInstanceModelFieldComplex.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundInstanceModelFieldComplex.java index 4a83597f4..1616f8e41 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundInstanceModelFieldComplex.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundInstanceModelFieldComplex.java @@ -28,22 +28,14 @@ import gov.nist.secauto.metaschema.core.util.ObjectUtils; import gov.nist.secauto.metaschema.databind.io.BindingException; +import gov.nist.secauto.metaschema.databind.model.info.IFeatureComplexItemValueHandler; import gov.nist.secauto.metaschema.databind.model.info.IItemReadHandler; import gov.nist.secauto.metaschema.databind.model.info.IItemWriteHandler; import java.io.IOException; -import javax.xml.namespace.QName; - -import edu.umd.cs.findbugs.annotations.NonNull; - public interface IBoundInstanceModelFieldComplex - extends IBoundInstanceModelField, IBoundInstanceModelNamedComplex { - - @Override - default IBoundInstanceModelFieldComplex getInstance() { - return this; - } + extends IBoundInstanceModelField, IFeatureComplexItemValueHandler { @Override IBoundDefinitionModelFieldComplex getDefinition(); diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundInstanceModelFieldScalar.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundInstanceModelFieldScalar.java index ae319a54d..6d3b8f09b 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundInstanceModelFieldScalar.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundInstanceModelFieldScalar.java @@ -26,65 +26,44 @@ package gov.nist.secauto.metaschema.databind.model; +import gov.nist.secauto.metaschema.core.model.IContainerFlagSupport; +import gov.nist.secauto.metaschema.core.model.IFeatureDefinitionInstanceInlined; import gov.nist.secauto.metaschema.core.util.ObjectUtils; -import gov.nist.secauto.metaschema.databind.IBindingContext; import gov.nist.secauto.metaschema.databind.model.info.IFeatureScalarItemValueHandler; import gov.nist.secauto.metaschema.databind.model.info.IItemReadHandler; import gov.nist.secauto.metaschema.databind.model.info.IItemWriteHandler; import java.io.IOException; -import edu.umd.cs.findbugs.annotations.NonNull; - public interface IBoundInstanceModelFieldScalar extends IBoundInstanceModelField, IBoundDefinitionModelField, IFeatureScalarItemValueHandler, - IFeatureBoundDefinitionInline { + IFeatureDefinitionInstanceInlined { // integrate above - - @Override - IBoundDefinitionModelAssembly getContainingDefinition(); - @Override - default IBoundInstanceModelFieldScalar getInstance() { - return this; - } - - /** - * {@inheritDoc} - *

- * For an inline instance, this instance is the definition. - */ - @Override - default IBoundInstanceModelFieldScalar getDefinition() { - return this; + default IBoundDefinitionModelField getDefinition() { + return IFeatureDefinitionInstanceInlined.super.getDefinition(); } @Override default IBoundInstanceModelFieldScalar getInlineInstance() { - // always inline - return this; + return IFeatureDefinitionInstanceInlined.super.getInlineInstance(); } @Override - default IBindingContext getBindingContext() { - return getContainingDefinition().getBindingContext(); - } + IBoundDefinitionModelAssembly getContainingDefinition(); - /** - * {@inheritDoc} - *

- * Use the effective name of the instance. - */ @Override - @NonNull - default String getJsonName() { - return IBoundInstanceModelField.super.getJsonName(); + default IContainerFlagSupport getFlagContainer() { + return IContainerFlagSupport.empty(); } @Override - IBoundInstanceFlag getJsonKeyFlagInstance(); + default IBoundInstanceFlag getJsonKey() { + // no flags + return null; + } @Override default IBoundInstanceFlag getItemJsonKey(Object item) { @@ -110,26 +89,6 @@ default IBoundInstanceFlag getJsonValueKeyFlagInstance() { return null; } - /** - * {@inheritDoc} - *

- * Always bound to a field. - */ - @Override - default Object getValue(Object parent) { - return IBoundInstanceModelField.super.getValue(parent); - } - - /** - * {@inheritDoc} - *

- * Always bound to a field. - */ - @Override - default void setValue(Object parentObject, Object value) { - IBoundInstanceModelField.super.setValue(parentObject, value); - } - @Override default Object readItem(Object parent, IItemReadHandler handler) throws IOException { return handler.readItemField(ObjectUtils.requireNonNull(parent, "parent"), this); @@ -139,5 +98,4 @@ default Object readItem(Object parent, IItemReadHandler handler) throws IOExcept default void writeItem(Object item, IItemWriteHandler handler) throws IOException { handler.writeItemField(item, this); } - } diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundInstanceModelGroupedNamed.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundInstanceModelGroupedNamed.java index b3c824ef6..b661992ea 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundInstanceModelGroupedNamed.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundInstanceModelGroupedNamed.java @@ -26,14 +26,14 @@ package gov.nist.secauto.metaschema.databind.model; -import javax.xml.namespace.QName; - -import edu.umd.cs.findbugs.annotations.Nullable; import gov.nist.secauto.metaschema.core.model.INamedModelInstanceGrouped; import gov.nist.secauto.metaschema.core.model.JsonGroupAsBehavior; import gov.nist.secauto.metaschema.core.util.ObjectUtils; +import gov.nist.secauto.metaschema.databind.io.BindingException; import gov.nist.secauto.metaschema.databind.model.info.IFeatureComplexItemValueHandler; +import edu.umd.cs.findbugs.annotations.Nullable; + /** * Represents a model instance that is a member of a choice group instance. */ @@ -45,12 +45,44 @@ public interface IBoundInstanceModelGroupedNamed @Override IBoundDefinitionModelComplex getDefinition(); + @Override @Nullable - default IBoundInstanceFlag getJsonKey() { - String jsonKeyName = getJsonKeyFlagName(); + default IBoundInstanceFlag getEffectiveJsonKey() { return JsonGroupAsBehavior.KEYED.equals(getParentContainer().getJsonGroupAsBehavior()) - ? ObjectUtils.requireNonNull(getDefinition().getFlagInstanceByName( - new QName(getXmlNamespace(), ObjectUtils.requireNonNull(jsonKeyName)))) + ? getJsonKey() : null; } + + @Override + default IBoundInstanceFlag getJsonKey() { + String name = getParentContainer().getJsonKeyFlagInstanceName(); + return name == null + ? null + : ObjectUtils.requireNonNull(getDefinition().getFlagInstanceByName(getContainingModule().toFlagQName(name))); + } + + @Override + default IBoundDefinitionModelAssembly getContainingDefinition() { + return getParentContainer().getContainingDefinition(); + } + + @Override + default String getName() { + return getDefinition().getName(); + } + + @Override + default Object deepCopyItem(Object item, Object parentInstance) throws BindingException { + return getDefinition().deepCopyItem(item, parentInstance); + } + + @Override + default void callBeforeDeserialize(Object targetObject, Object parentObject) throws BindingException { + getDefinition().callBeforeDeserialize(targetObject, parentObject); + } + + @Override + default void callAfterDeserialize(Object targetObject, Object parentObject) throws BindingException { + getDefinition().callAfterDeserialize(targetObject, parentObject); + } } diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundInstanceModelNamed.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundInstanceModelNamed.java index 9136e50f1..343e2f41d 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundInstanceModelNamed.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundInstanceModelNamed.java @@ -26,17 +26,18 @@ package gov.nist.secauto.metaschema.databind.model; +import gov.nist.secauto.metaschema.core.model.INamedModelInstanceAbsolute; +import gov.nist.secauto.metaschema.core.model.JsonGroupAsBehavior; + import java.util.Collection; import javax.xml.namespace.QName; import edu.umd.cs.findbugs.annotations.NonNull; import edu.umd.cs.findbugs.annotations.Nullable; -import gov.nist.secauto.metaschema.core.model.INamedModelInstanceAbsolute; -import gov.nist.secauto.metaschema.core.model.JsonGroupAsBehavior; -import gov.nist.secauto.metaschema.core.util.ObjectUtils; -public interface IBoundInstanceModelNamed extends IBoundInstanceModel, INamedModelInstanceAbsolute { +public interface IBoundInstanceModelNamed + extends IBoundInstanceModel, INamedModelInstanceAbsolute { @Override @NonNull @@ -55,22 +56,21 @@ default Integer getIndex() { } @Override - default String getJsonName() { - return INamedModelInstanceAbsolute.super.getJsonName(); - } - @Nullable - default IBoundInstanceFlag getJsonKey() { - String jsonKeyName = getJsonKeyFlagName(); + default IBoundInstanceFlag getEffectiveJsonKey() { return JsonGroupAsBehavior.KEYED.equals(getJsonGroupAsBehavior()) - ? ObjectUtils.requireNonNull(getDefinition().getFlagInstanceByName( - getContainingModule().toFlagQName(ObjectUtils.requireNonNull(jsonKeyName)))) + ? getJsonKey() : null; } + @Override + default IBoundInstanceFlag getJsonKey() { + return getDefinition().getJsonKey(); + } + @Override default IBoundInstanceFlag getItemJsonKey(Object item) { - return getJsonKey(); + return getEffectiveJsonKey(); } @Override @@ -82,9 +82,4 @@ default Collection getItemValues(Object value) { default boolean canHandleXmlQName(QName qname) { return qname.equals(getXmlQName()); } - - @Override - default boolean canHandleJsonPropertyName(@NonNull String name) { - return name.equals(getJsonName()); - } } diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundInstanceModelNamedComplex.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundInstanceModelNamedComplex.java deleted file mode 100644 index f995a568d..000000000 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundInstanceModelNamedComplex.java +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Portions of this software was developed by employees of the National Institute - * of Standards and Technology (NIST), an agency of the Federal Government and is - * being made available as a public service. Pursuant to title 17 United States - * Code Section 105, works of NIST employees are not subject to copyright - * protection in the United States. This software may be subject to foreign - * copyright. Permission in the United States and in foreign countries, to the - * extent that NIST may hold copyright, to use, copy, modify, create derivative - * works, and distribute this software and its documentation without fee is hereby - * granted on a non-exclusive basis, provided that this notice and disclaimer - * of warranty appears in all copies. - * - * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER - * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY - * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM - * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE - * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT - * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, - * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, - * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, - * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR - * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT - * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. - */ - -package gov.nist.secauto.metaschema.databind.model; - -import gov.nist.secauto.metaschema.databind.model.info.IFeatureComplexItemValueHandler; - -public interface IBoundInstanceModelNamedComplex extends IBoundInstanceModelNamed, IFeatureComplexItemValueHandler { - - @Override - IBoundDefinitionModelComplex getDefinition(); -} diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundModule.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundModule.java index 6b9f07381..ddad425e9 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundModule.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundModule.java @@ -32,6 +32,8 @@ import java.net.URI; import java.util.Collection; +import javax.xml.namespace.QName; + import edu.umd.cs.findbugs.annotations.NonNull; public interface IBoundModule @@ -60,11 +62,11 @@ default URI getLocation() { // NOPMD - intentional Collection getAssemblyDefinitions(); @Override - IBoundDefinitionModelAssembly getAssemblyDefinitionByName(@NonNull String name); + IBoundDefinitionModelAssembly getAssemblyDefinitionByName(@NonNull QName name); @Override Collection getFieldDefinitions(); @Override - IBoundDefinitionModelField getFieldDefinitionByName(@NonNull String name); + IBoundDefinitionModelField getFieldDefinitionByName(@NonNull QName name); } diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundModuleElement.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundModuleElement.java index 223f01e60..65bbcd925 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundModuleElement.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundModuleElement.java @@ -37,17 +37,6 @@ * definition, instance, field value. */ public interface IBoundModuleElement extends IItemValueHandler { - /** - * Determine if the provided JSON property or YAML key name is associated with - * this instance. - * - * @param name - * the name of the property/key being parsed - * @return {@code true} if the instance will handle this name, or {@code false} - * otherwise - */ - boolean canHandleJsonPropertyName(@NonNull String name); - /** * Determine if the provided XML qualified name is associated with this * property. diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundProperty.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundProperty.java index 1cb0b7e93..ee032677c 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundProperty.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IBoundProperty.java @@ -26,43 +26,12 @@ package gov.nist.secauto.metaschema.databind.model; +import gov.nist.secauto.metaschema.core.model.IJsonNamed; import gov.nist.secauto.metaschema.databind.io.BindingException; -import java.util.Collection; - import edu.umd.cs.findbugs.annotations.NonNull; -public interface IBoundProperty extends IBoundModuleElement, IFeatureJavaField { - /** - * Get the Metaschema module instance associated with this binding. - * - * @return the instance - */ - @NonNull - IBoundProperty getInstance(); - - /** - * Get the JSON/YAML property/key name to use for serialization-related - * operations. - * - * @return the JSON name - */ - // REFACTOR: rename to getEffectiveJsonName - @NonNull - String getJsonName(); - - /** - * Get the individual item values for this property. - *

- * A property can be single- or multi-valued. This method gets each value in - * either case. - * - * @param propertyValue - * the value for the property, which can be multi-valued - * @return the item values - */ - Collection getItemValues(Object propertyValue); - +public interface IBoundProperty extends IBoundModuleElement, IFeatureJavaField, IJsonNamed { /** * Copy this instance from one parent object to another. * diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IFeatureBoundDefinitionInline.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IFeatureBoundDefinitionInline.java deleted file mode 100644 index 9f0f55962..000000000 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IFeatureBoundDefinitionInline.java +++ /dev/null @@ -1,70 +0,0 @@ -/* - * Portions of this software was developed by employees of the National Institute - * of Standards and Technology (NIST), an agency of the Federal Government and is - * being made available as a public service. Pursuant to title 17 United States - * Code Section 105, works of NIST employees are not subject to copyright - * protection in the United States. This software may be subject to foreign - * copyright. Permission in the United States and in foreign countries, to the - * extent that NIST may hold copyright, to use, copy, modify, create derivative - * works, and distribute this software and its documentation without fee is hereby - * granted on a non-exclusive basis, provided that this notice and disclaimer - * of warranty appears in all copies. - * - * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER - * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY - * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM - * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE - * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT - * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, - * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, - * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, - * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR - * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT - * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. - */ - -package gov.nist.secauto.metaschema.databind.model; - -import gov.nist.secauto.metaschema.core.model.IFeatureDefinitionInstanceInlined; - -import edu.umd.cs.findbugs.annotations.NonNull; - -// REFACTOR: Try to eliminate this interface in favor of IFeatureInlinedDefinitionInstance -public interface IFeatureBoundDefinitionInline< - DEFINITION extends IBoundDefinition, - INSTANCE extends IBoundInstanceNamed> - extends IBoundDefinition, IBoundInstanceNamed, - IFeatureDefinitionInstanceInlined { - // - // @Override - // default Object getEffectiveDefaultValue() { - // // needed to avoid conflict between IBoundInstance and INamedInstance - // return IBoundInstanceNamed.super.getDefaultValue(); - // } - - /** - * {@inheritDoc} - *

- * Get the containing module from the definition this instance is declared on. - */ - @Override - @NonNull - default IBoundModule getContainingModule() { - // this is the same as IBoundInstance, but is needed since IBoundDefinition - // and IBoundInstance both declare it - return getContainingDefinition().getContainingModule(); - } - - /** - * {@inheritDoc} - *

- * Use the effective name of the instance. - */ - @Override - default String getJsonName() { - // this is the same as INamedModelElement, but is needed since IBoundProperty - // also declares it - return getEffectiveName(); - } -} diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IGroupAs.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IGroupAs.java index 64e9d318e..2b140f364 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IGroupAs.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/IGroupAs.java @@ -29,6 +29,8 @@ import gov.nist.secauto.metaschema.core.model.JsonGroupAsBehavior; import gov.nist.secauto.metaschema.core.model.XmlGroupAsBehavior; +import javax.xml.namespace.QName; + import edu.umd.cs.findbugs.annotations.NonNull; import edu.umd.cs.findbugs.annotations.Nullable; @@ -39,12 +41,7 @@ public interface IGroupAs { @NonNull IGroupAs SINGLETON_GROUP_AS = new IGroupAs() { @Override - public String getGroupAsName() { - return null; - } - - @Override - public String getGroupAsXmlNamespace() { + public QName getGroupAsQName() { return null; } @@ -60,10 +57,19 @@ public XmlGroupAsBehavior getXmlGroupAsBehavior() { }; @Nullable - String getGroupAsName(); + QName getGroupAsQName(); + + @Nullable + default String getGroupAsName() { + QName qname = getGroupAsQName(); + return qname == null ? null : qname.getLocalPart(); + } @Nullable - String getGroupAsXmlNamespace(); + default String getGroupAsXmlNamespace() { + QName qname = getGroupAsQName(); + return qname == null ? null : qname.getNamespaceURI(); + } @NonNull JsonGroupAsBehavior getJsonGroupAsBehavior(); diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/annotations/BoundAssembly.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/annotations/BoundAssembly.java index 3e4cf04c8..24336d38b 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/annotations/BoundAssembly.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/annotations/BoundAssembly.java @@ -44,8 +44,7 @@ * Module assembly. *

* For XML serialization, the {@link #useName()} identifies the name of the - * element to use and the {@link #namespace()} identifies the namespace of this - * element. + * element to use for this element. *

* For JSON and YAML serializations, the {@link #useName()} identifies the * property/item name to use. @@ -97,17 +96,6 @@ */ int useIndex() default Integer.MIN_VALUE; - /** - * The namespace to use for associated XML elements. - *

- * If the value is "##default", then element name is derived from the namespace - * provided in the package-info. - * - * @return the namespace - */ - @NonNull - String namespace() default ModelUtil.DEFAULT_STRING_VALUE; - /** * A non-negative number that indicates the minimum occurrence of the model * instance. diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/annotations/BoundField.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/annotations/BoundField.java index 21db7dfd4..a286fe730 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/annotations/BoundField.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/annotations/BoundField.java @@ -45,8 +45,7 @@ * Module field. *

* For XML serialization, the {@link #useName()} identifies the name of the - * element to use and the {@link #namespace()} identifies the namespace of this - * element. + * element to use for this element. *

* For JSON and YAML serializations, the {@link #useName()} identifies the * property/item name to use. @@ -127,16 +126,6 @@ @NonNull String defaultValue() default ModelUtil.NULL_VALUE; - /** - * The namespace to use for associated XML elements. - *

- * If the value is "##default", then element name is derived from the namespace - * provided in the package-info. - * - * @return the namespace - */ - String namespace() default ModelUtil.DEFAULT_STRING_VALUE; - /** * If the data type allows it, determines if the field's value must be wrapped * with an XML element. diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/annotations/BoundFlag.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/annotations/BoundFlag.java index 061188c30..d7bcdc12f 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/annotations/BoundFlag.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/annotations/BoundFlag.java @@ -89,18 +89,6 @@ */ int useIndex() default Integer.MIN_VALUE; - /** - * XML target namespace of the XML Schema element. - *

- * If the value is "##default", then namespace is derived from the namespace - * provided in the package-info. If the value is "##none", the namespace will be - * {@code null}. - * - * @return the namespace - */ - @NonNull - String namespace() default ModelUtil.NO_STRING_VALUE; - /** * The default value of the flag represented as a string. *

diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/annotations/BoundGroupedAssembly.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/annotations/BoundGroupedAssembly.java index 0182629e3..972c0cfca 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/annotations/BoundGroupedAssembly.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/annotations/BoundGroupedAssembly.java @@ -42,8 +42,7 @@ * Module assembly. *

* For XML serialization, the {@link #useName()} identifies the name of the - * element to use and the {@link #namespace()} identifies the namespace of this - * element. + * element to use for this element. *

* For JSON and YAML serializations, the {@link #useName()} identifies the * property/item name to use. @@ -95,17 +94,6 @@ */ int useIndex() default Integer.MIN_VALUE; - /** - * The namespace to use for associated XML elements. - *

- * If the value is "##default", then element name is derived from the namespace - * provided in the package-info. - * - * @return the namespace - */ - @NonNull - String namespace() default ModelUtil.DEFAULT_STRING_VALUE; - /** * Get any remarks for this field. * diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/annotations/BoundGroupedField.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/annotations/BoundGroupedField.java index d0f414c9b..b66939dff 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/annotations/BoundGroupedField.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/annotations/BoundGroupedField.java @@ -43,8 +43,7 @@ * Module field. *

* For XML serialization, the {@link #useName()} identifies the name of the - * element to use and the {@link #namespace()} identifies the namespace of this - * element. + * element to use for this element. *

* For JSON and YAML serializations, the {@link #useName()} identifies the * property/item name to use. @@ -114,16 +113,6 @@ @NonNull Class> typeAdapter() default NullJavaTypeAdapter.class; - /** - * The namespace to use for associated XML elements. - *

- * If the value is "##default", then element name is derived from the namespace - * provided in the package-info. - * - * @return the namespace - */ - String namespace() default ModelUtil.DEFAULT_STRING_VALUE; - /** * Get any remarks for this field. * diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/annotations/GroupAs.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/annotations/GroupAs.java index 1978957f2..d7ddd08a2 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/annotations/GroupAs.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/annotations/GroupAs.java @@ -51,17 +51,6 @@ @NonNull String name(); - /** - * XML target namespace of the XML grouping element. - *

- * If the value is "##default", then element name is derived from the namespace - * provided in the package-info. - * - * @return the namespace - */ - @NonNull - String namespace() default ModelUtil.DEFAULT_STRING_VALUE; - /** * Describes how to handle collections in JSON/YAML. * diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/annotations/MetaschemaAssembly.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/annotations/MetaschemaAssembly.java index 848e9cdad..5781f9fe0 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/annotations/MetaschemaAssembly.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/annotations/MetaschemaAssembly.java @@ -111,17 +111,6 @@ */ int rootIndex() default Integer.MIN_VALUE; - /** - * XML target namespace of the XML element. - *

- * If the value is "##default", then namespace is derived from the namespace - * provided in the package-info. - * - * @return the namespace - */ - @NonNull - String rootNamespace() default ModelUtil.DEFAULT_STRING_VALUE; - /** * Get any remarks for this assembly. * diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/annotations/ModelUtil.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/annotations/ModelUtil.java index c3af610e3..c611b4d07 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/annotations/ModelUtil.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/annotations/ModelUtil.java @@ -30,6 +30,7 @@ import gov.nist.secauto.metaschema.core.datatype.adapter.MetaschemaDataTypeProvider; import gov.nist.secauto.metaschema.core.datatype.markup.MarkupLine; import gov.nist.secauto.metaschema.core.datatype.markup.MarkupMultiline; +import gov.nist.secauto.metaschema.core.model.IModule; import gov.nist.secauto.metaschema.core.util.ObjectUtils; import gov.nist.secauto.metaschema.databind.IBindingContext; import gov.nist.secauto.metaschema.databind.model.IGroupAs; @@ -37,7 +38,6 @@ import java.lang.annotation.Annotation; import java.lang.reflect.Field; -import java.util.function.Supplier; import edu.umd.cs.findbugs.annotations.NonNull; import edu.umd.cs.findbugs.annotations.Nullable; @@ -112,43 +112,6 @@ public static String resolveNoneOrDefault(@Nullable String value, @Nullable Stri return retval; } - @Nullable - public static String resolveOptionalNamespace( - @Nullable String annotationValue, - @NonNull Supplier defaultSupplier) { - return resolveNamespace(annotationValue, true, defaultSupplier); - } - - /** - * Resolves a provided namespace value. If the value is {@code null} or - * "##default", then the provided default value will be used instead. If the - * value is "##none" and {@code allowNone} is {@code true}, then an empty string - * value will be used. Otherwise, the value is returned. - * - * @param value - * the requested value - * @param definition - * a class with the {@link XmlSchema} annotation - * @param allowNone - * if the "##none" value is honored - * @return the resolved value or {@code null} if no namespace is defined - */ - private static String resolveNamespace( - @Nullable String value, - boolean allowNone, - @NonNull Supplier defaultSupplier) { - String retval; - if (value == null || DEFAULT_STRING_VALUE.equals(value)) { - // get namespace from the metaschema - retval = defaultSupplier.get(); - } else if (allowNone && NO_STRING_VALUE.equals(value)) { - retval = ""; // NOPMD - intentional - } else { - retval = value; - } - return retval; - } - /** * Get the processed value of a string. If the value is "##none", then the value * will be {@code null}. Otherwise the value is returned. @@ -225,9 +188,9 @@ public static Object resolveNullOrValue( @NonNull public static IGroupAs groupAs( @NonNull GroupAs groupAs, - @NonNull Supplier defaultSupplier) { + @NonNull IModule module) { return NULL_VALUE.equals(groupAs.name()) ? IGroupAs.SINGLETON_GROUP_AS - : new DefaultGroupAs(groupAs, defaultSupplier); + : new DefaultGroupAs(groupAs, module); } } diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/AbstractBoundAnnotatedJavaField.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/AbstractBoundAnnotatedJavaField.java deleted file mode 100644 index b5a38f1da..000000000 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/AbstractBoundAnnotatedJavaField.java +++ /dev/null @@ -1,78 +0,0 @@ -/* - * Portions of this software was developed by employees of the National Institute - * of Standards and Technology (NIST), an agency of the Federal Government and is - * being made available as a public service. Pursuant to title 17 United States - * Code Section 105, works of NIST employees are not subject to copyright - * protection in the United States. This software may be subject to foreign - * copyright. Permission in the United States and in foreign countries, to the - * extent that NIST may hold copyright, to use, copy, modify, create derivative - * works, and distribute this software and its documentation without fee is hereby - * granted on a non-exclusive basis, provided that this notice and disclaimer - * of warranty appears in all copies. - * - * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER - * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY - * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM - * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE - * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT - * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, - * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, - * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, - * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR - * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT - * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. - */ - -package gov.nist.secauto.metaschema.databind.model.impl; - -import gov.nist.secauto.metaschema.databind.model.IFeatureJavaField; -import gov.nist.secauto.metaschema.databind.model.annotations.ModelUtil; - -import java.lang.annotation.Annotation; -import java.lang.reflect.Field; - -import edu.umd.cs.findbugs.annotations.NonNull; - -public abstract class AbstractBoundAnnotatedJavaField implements IFeatureJavaField { - @NonNull - private final Field javaField; - @NonNull - private final A annotation; - - /** - * Construct a new binding between a Java field and a binding Java annotation. - * - * @param javaField - * the bound Java field - * @param annotationClass - * the binding annotation Java class - */ - protected AbstractBoundAnnotatedJavaField( - @NonNull Field javaField, - @NonNull Class annotationClass) { - this.javaField = javaField; - this.annotation = ModelUtil.getAnnotation(javaField, annotationClass); - } - - /** - * Get the bound Java field. - * - * @return the bound Java field - */ - @Override - @NonNull - public Field getField() { - return javaField; - } - - /** - * Get the binding Java annotation. - * - * @return the binding Java annotation - */ - @NonNull - public A getAnnotation() { - return annotation; - } -} diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/AbstractBoundDefinitionModelComplex.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/AbstractBoundDefinitionModelComplex.java index 64d613e47..830efe92a 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/AbstractBoundDefinitionModelComplex.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/AbstractBoundDefinitionModelComplex.java @@ -32,19 +32,18 @@ import gov.nist.secauto.metaschema.databind.model.IBoundDefinitionModelComplex; import gov.nist.secauto.metaschema.databind.model.IBoundInstanceFlag; import gov.nist.secauto.metaschema.databind.model.IBoundModule; -import gov.nist.secauto.metaschema.databind.model.annotations.ModelUtil; import java.lang.annotation.Annotation; -import java.lang.reflect.Constructor; -import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; +import javax.xml.namespace.QName; + import edu.umd.cs.findbugs.annotations.NonNull; import edu.umd.cs.findbugs.annotations.Nullable; import nl.talsmasoftware.lazy4j.Lazy; public abstract class AbstractBoundDefinitionModelComplex - implements IFeatureBoundContainerFlag, IBoundDefinitionModelComplex { + implements IBoundDefinitionModelComplex { @NonNull private final Class clazz; @NonNull @@ -53,6 +52,10 @@ public abstract class AbstractBoundDefinitionModelComplex private final IBindingContext bindingContext; @NonNull private final Lazy module; + @NonNull + private final Lazy qname; + @NonNull + private final Lazy definitionQName; @Nullable private final Method beforeDeserializeMethod; @Nullable @@ -60,12 +63,15 @@ public abstract class AbstractBoundDefinitionModelComplex protected AbstractBoundDefinitionModelComplex( @NonNull Class clazz, - @NonNull Class annotationClass, + @NonNull A annotation, + @NonNull Class moduleClass, @NonNull IBindingContext bindingContext) { this.clazz = clazz; - this.annotation = ModelUtil.getAnnotation(clazz, annotationClass); + this.annotation = annotation; this.bindingContext = bindingContext; - this.module = ObjectUtils.notNull(Lazy.lazy(() -> bindingContext.registerModule(getModuleClass()))); + this.module = ObjectUtils.notNull(Lazy.lazy(() -> bindingContext.registerModule(moduleClass))); + this.qname = ObjectUtils.notNull(Lazy.lazy(() -> getContainingModule().toModelQName(getEffectiveName()))); + this.definitionQName = ObjectUtils.notNull(Lazy.lazy(() -> getContainingModule().toModelQName(getName()))); this.beforeDeserializeMethod = ClassIntrospector.getMatchingMethod( clazz, "beforeDeserialize", @@ -76,9 +82,6 @@ protected AbstractBoundDefinitionModelComplex( Object.class); } - @NonNull - protected abstract Class getModuleClass(); - @Override public Class getBoundClass() { return clazz; @@ -88,11 +91,6 @@ public A getAnnotation() { return annotation; } - @Override - public boolean isInline() { - return getBoundClass().getEnclosingClass() != null; - } - @Override @NonNull public IBoundModule getContainingModule() { @@ -105,82 +103,31 @@ public IBindingContext getBindingContext() { return bindingContext; } - /** - * Gets a new instance of the bound class. - * - * @param - * the type of the bound class - * @return a Java object for the class - * @throws RuntimeException - * if the instance cannot be created due to a binding error - */ - @SuppressWarnings("PMD.AvoidThrowingRawExceptionTypes") + @SuppressWarnings("null") @Override - @NonNull - public CLASS newInstance() { - Class clazz = getBoundClass(); - try { - @SuppressWarnings("unchecked") Constructor constructor - = (Constructor) clazz.getDeclaredConstructor(); - return ObjectUtils.notNull(constructor.newInstance()); - } catch (NoSuchMethodException ex) { - String msg = String.format("Class '%s' does not have a required no-arg constructor.", clazz.getName()); - throw new RuntimeException(msg, ex); - } catch (InstantiationException | IllegalAccessException | InvocationTargetException ex) { - throw new RuntimeException(ex); - } + public final QName getXmlQName() { + return qname.get(); } - /** - * Calls the method named "beforeDeserialize" on each class in the object's - * hierarchy if the method exists on the class. - *

- * These methods can be used to set the initial state of the target bound object - * before data is read and applied during deserialization. - * - * @param targetObject - * the data object target to call the method(s) on - * @param parentObject - * the object target's parent object, which is used as the method - * argument - * @throws BindingException - * if an error occurs while calling the method - */ + @SuppressWarnings("null") @Override - public void callBeforeDeserialize(Object targetObject, Object parentObject) throws BindingException { - if (beforeDeserializeMethod != null) { - try { - beforeDeserializeMethod.invoke(targetObject, parentObject); - } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) { - throw new BindingException(ex); - } - } + public final QName getDefinitionQName() { + return definitionQName.get(); } - /** - * Calls the method named "afterDeserialize" on each class in the object's - * hierarchy if the method exists. - *

- * These methods can be used to modify the state of the target bound object - * after data is read and applied during deserialization. - * - * @param targetObject - * the data object target to call the method(s) on - * @param parentObject - * the object target's parent object, which is used as the method - * argument - * @throws BindingException - * if an error occurs while calling the method - */ @Override - public void callAfterDeserialize(Object targetObject, Object parentObject) throws BindingException { - if (afterDeserializeMethod != null) { - try { - afterDeserializeMethod.invoke(targetObject, parentObject); - } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) { - throw new BindingException(ex); - } - } + public boolean isInline() { + return getBoundClass().getEnclosingClass() != null; + } + + @Override + public Method getBeforeDeserializeMethod() { + return beforeDeserializeMethod; + } + + @Override + public Method getAfterDeserializeMethod() { + return afterDeserializeMethod; } // @Override diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/AbstractBoundInstanceField.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/AbstractBoundInstanceField.java deleted file mode 100644 index 36d3e82ea..000000000 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/AbstractBoundInstanceField.java +++ /dev/null @@ -1,140 +0,0 @@ -/* - * Portions of this software was developed by employees of the National Institute - * of Standards and Technology (NIST), an agency of the Federal Government and is - * being made available as a public service. Pursuant to title 17 United States - * Code Section 105, works of NIST employees are not subject to copyright - * protection in the United States. This software may be subject to foreign - * copyright. Permission in the United States and in foreign countries, to the - * extent that NIST may hold copyright, to use, copy, modify, create derivative - * works, and distribute this software and its documentation without fee is hereby - * granted on a non-exclusive basis, provided that this notice and disclaimer - * of warranty appears in all copies. - * - * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER - * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY - * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM - * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE - * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT - * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, - * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, - * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, - * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR - * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT - * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. - */ - -package gov.nist.secauto.metaschema.databind.model.impl; - -import gov.nist.secauto.metaschema.core.datatype.markup.MarkupLine; -import gov.nist.secauto.metaschema.core.datatype.markup.MarkupMultiline; -import gov.nist.secauto.metaschema.databind.model.IBoundDefinitionModelAssembly; -import gov.nist.secauto.metaschema.databind.model.IBoundInstanceModelField; -import gov.nist.secauto.metaschema.databind.model.IGroupAs; -import gov.nist.secauto.metaschema.databind.model.annotations.BoundField; -import gov.nist.secauto.metaschema.databind.model.annotations.GroupAs; -import gov.nist.secauto.metaschema.databind.model.annotations.ModelUtil; - -import java.lang.reflect.Field; - -import edu.umd.cs.findbugs.annotations.NonNull; -import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; - -// TODO: implement getProperties() -public abstract class AbstractBoundInstanceField - extends AbstractBoundInstanceModelJavaField - implements IBoundInstanceModelField { - @NonNull - private final IGroupAs groupAs; - - /** - * Construct a new field instance bound to a Java field. - * - * @param javaField - * the Java field bound to this instance - * @param containingDefinition - * the definition containing this instance - */ - @SuppressFBWarnings(value = "CT_CONSTRUCTOR_THROW", justification = "Use of final fields") - protected AbstractBoundInstanceField( - @NonNull Field javaField, - @NonNull IBoundDefinitionModelAssembly containingDefinition) { - super(javaField, BoundField.class, containingDefinition); - this.groupAs = ModelUtil.groupAs( - getAnnotation().groupAs(), - () -> containingDefinition.getXmlNamespace()); - if (getMaxOccurs() == -1 || getMaxOccurs() > 1) { - if (IGroupAs.SINGLETON_GROUP_AS.equals(this.groupAs)) { - throw new IllegalStateException(String.format("Field '%s' on class '%s' is missing the '%s' annotation.", - javaField.getName(), - javaField.getDeclaringClass().getName(), - GroupAs.class.getName())); // NOPMD false positive - } - } else if (!IGroupAs.SINGLETON_GROUP_AS.equals(this.groupAs)) { - // max is 1 and a groupAs is set - throw new IllegalStateException( - String.format( - "Field '%s' on class '%s' has the '%s' annotation, but maxOccurs=1. A groupAs must not be specfied.", - javaField.getName(), - javaField.getDeclaringClass().getName(), - GroupAs.class.getName())); // NOPMD false positive - } - } - - // ------------------------------------------ - // - Start annotation driven code - CPD-OFF - - // ------------------------------------------ - - @Override - public IGroupAs getGroupAs() { - return groupAs; - } - - @Override - public String getFormalName() { - return ModelUtil.resolveNoneOrValue(getAnnotation().formalName()); - } - - @Override - public MarkupLine getDescription() { - return ModelUtil.resolveToMarkupLine(getAnnotation().description()); - } - - @Override - public Integer getUseIndex() { - int value = getAnnotation().useIndex(); - return value == Integer.MIN_VALUE ? null : value; - } - - @Override - public String getXmlNamespace() { - return ModelUtil.resolveOptionalNamespace( - getAnnotation().namespace(), - () -> getContainingDefinition().getXmlNamespace()); - } - - @Override - public boolean isInXmlWrapped() { - return getAnnotation().inXmlWrapped(); - } - - @Override - public final int getMinOccurs() { - return getAnnotation().minOccurs(); - } - - @Override - public final int getMaxOccurs() { - return getAnnotation().maxOccurs(); - } - - @Override - public MarkupMultiline getRemarks() { - return ModelUtil.resolveToMarkupMultiline(getAnnotation().remarks()); - } - - // @Override - // public String getJsonKeyFlagName() { - // return IBoundInstanceModelField.super.getJsonKeyFlagName(); - // } -} diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/AbstractBoundInstanceJavaField.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/AbstractBoundInstanceJavaField.java deleted file mode 100644 index 0778d737a..000000000 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/AbstractBoundInstanceJavaField.java +++ /dev/null @@ -1,60 +0,0 @@ -/* - * Portions of this software was developed by employees of the National Institute - * of Standards and Technology (NIST), an agency of the Federal Government and is - * being made available as a public service. Pursuant to title 17 United States - * Code Section 105, works of NIST employees are not subject to copyright - * protection in the United States. This software may be subject to foreign - * copyright. Permission in the United States and in foreign countries, to the - * extent that NIST may hold copyright, to use, copy, modify, create derivative - * works, and distribute this software and its documentation without fee is hereby - * granted on a non-exclusive basis, provided that this notice and disclaimer - * of warranty appears in all copies. - * - * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER - * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY - * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM - * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE - * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT - * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, - * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, - * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, - * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR - * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT - * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. - */ - -package gov.nist.secauto.metaschema.databind.model.impl; - -import gov.nist.secauto.metaschema.databind.model.IBoundDefinitionModel; -import gov.nist.secauto.metaschema.databind.model.IBoundInstance; - -import java.lang.annotation.Annotation; -import java.lang.reflect.Field; - -import edu.umd.cs.findbugs.annotations.NonNull; - -public abstract class AbstractBoundInstanceJavaField - extends AbstractBoundAnnotatedJavaField - implements IBoundInstance { - @NonNull - private final P containingDefinition; - - protected AbstractBoundInstanceJavaField( - @NonNull Field javaField, - @NonNull Class annotationClass, - @NonNull P containingDefinition) { - super(javaField, annotationClass); - this.containingDefinition = containingDefinition; - } - - @Override - public P getContainingDefinition() { - return containingDefinition; - } - - @Override - public P getParentContainer() { - return containingDefinition; - } -} diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/AbstractBoundInstanceModelGroupedNamed.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/AbstractBoundInstanceModelGroupedNamed.java deleted file mode 100644 index 980e4b371..000000000 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/AbstractBoundInstanceModelGroupedNamed.java +++ /dev/null @@ -1,93 +0,0 @@ -/* - * Portions of this software was developed by employees of the National Institute - * of Standards and Technology (NIST), an agency of the Federal Government and is - * being made available as a public service. Pursuant to title 17 United States - * Code Section 105, works of NIST employees are not subject to copyright - * protection in the United States. This software may be subject to foreign - * copyright. Permission in the United States and in foreign countries, to the - * extent that NIST may hold copyright, to use, copy, modify, create derivative - * works, and distribute this software and its documentation without fee is hereby - * granted on a non-exclusive basis, provided that this notice and disclaimer - * of warranty appears in all copies. - * - * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER - * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY - * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM - * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE - * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT - * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, - * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, - * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, - * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR - * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT - * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. - */ - -package gov.nist.secauto.metaschema.databind.model.impl; - -import gov.nist.secauto.metaschema.databind.io.BindingException; -import gov.nist.secauto.metaschema.databind.model.IBoundDefinitionModelAssembly; -import gov.nist.secauto.metaschema.databind.model.IBoundInstanceModelChoiceGroup; -import gov.nist.secauto.metaschema.databind.model.IBoundInstanceModelGroupedNamed; - -import java.lang.annotation.Annotation; - -import edu.umd.cs.findbugs.annotations.NonNull; - -//TODO: implement getProperties() -public abstract class AbstractBoundInstanceModelGroupedNamed< - A extends Annotation> - implements IBoundInstanceModelGroupedNamed { - @NonNull - private final A annotation; - @NonNull - private final IBoundInstanceModelChoiceGroup choiceGroupInstance; - - protected AbstractBoundInstanceModelGroupedNamed( - @NonNull A annotation, - @NonNull IBoundInstanceModelChoiceGroup choiceGroupInstance) { - this.annotation = annotation; - this.choiceGroupInstance = choiceGroupInstance; - } - - public A getAnnotation() { - return annotation; - } - - @Override - public IBoundInstanceModelChoiceGroup getParentContainer() { - return choiceGroupInstance; - } - - @Override - public IBoundDefinitionModelAssembly getContainingDefinition() { - return getParentContainer().getContainingDefinition(); - } - - @Override - public String getJsonKeyFlagName() { - return getParentContainer().getJsonKeyFlagName(); - } - - @Override - public String getName() { - return getDefinition().getName(); - } - - @Override - public Object deepCopyItem(Object item, Object parentInstance) throws BindingException { - return getDefinition().deepCopyItem(item, parentInstance); - } - - @Override - public void callBeforeDeserialize(Object targetObject, Object parentObject) throws BindingException { - getDefinition().callBeforeDeserialize(targetObject, parentObject); - } - - @Override - public void callAfterDeserialize(Object targetObject, Object parentObject) throws BindingException { - getDefinition().callAfterDeserialize(targetObject, parentObject); - } - -} diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/AbstractBoundInstanceModelJavaField.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/AbstractBoundInstanceModelJavaField.java deleted file mode 100644 index f3b639c9a..000000000 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/AbstractBoundInstanceModelJavaField.java +++ /dev/null @@ -1,85 +0,0 @@ -/* - * Portions of this software was developed by employees of the National Institute - * of Standards and Technology (NIST), an agency of the Federal Government and is - * being made available as a public service. Pursuant to title 17 United States - * Code Section 105, works of NIST employees are not subject to copyright - * protection in the United States. This software may be subject to foreign - * copyright. Permission in the United States and in foreign countries, to the - * extent that NIST may hold copyright, to use, copy, modify, create derivative - * works, and distribute this software and its documentation without fee is hereby - * granted on a non-exclusive basis, provided that this notice and disclaimer - * of warranty appears in all copies. - * - * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER - * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY - * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM - * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE - * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT - * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, - * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, - * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, - * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR - * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT - * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. - */ - -package gov.nist.secauto.metaschema.databind.model.impl; - -import gov.nist.secauto.metaschema.core.util.ObjectUtils; -import gov.nist.secauto.metaschema.databind.io.BindingException; -import gov.nist.secauto.metaschema.databind.model.IBoundDefinitionModelAssembly; -import gov.nist.secauto.metaschema.databind.model.info.IModelInstanceCollectionInfo; - -import java.lang.annotation.Annotation; -import java.lang.reflect.Field; - -import edu.umd.cs.findbugs.annotations.NonNull; -import nl.talsmasoftware.lazy4j.Lazy; - -public abstract class AbstractBoundInstanceModelJavaField - extends AbstractBoundInstanceJavaField - implements IFeatureInstanceModelGroupAs { - - @NonNull - private final Lazy collectionInfo; - - public AbstractBoundInstanceModelJavaField( - @NonNull Field javaField, - @NonNull Class annotationClass, - @NonNull IBoundDefinitionModelAssembly containingDefinition) { - super(javaField, annotationClass, containingDefinition); - this.collectionInfo = ObjectUtils.notNull(Lazy.lazy(() -> IModelInstanceCollectionInfo.of(this))); - } - - /** - * Gets information about the bound property. - * - * @return the collection information for the bound property - */ - @SuppressWarnings("null") - @Override - @NonNull - public IModelInstanceCollectionInfo getCollectionInfo() { - return collectionInfo.get(); - } - // - // @Override - // public Object getValue(Object parent) { - // return IFeatureInstanceModelGroupAs.super.getValue(parent); - // } - - @Override - public void setValue(Object parentObject, Object value) { - IFeatureInstanceModelGroupAs.super.setValue(parentObject, value); - } - - @Override - public void deepCopy(@NonNull Object fromInstance, @NonNull Object toInstance) throws BindingException { - Object value = getValue(fromInstance); - if (value != null) { - value = getCollectionInfo().deepCopyItems(fromInstance, toInstance); - } - setValue(toInstance, value); - } -} diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/AssemblyModelContainerSupport.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/AssemblyModelContainerSupport.java index f3b0f09e8..650580461 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/AssemblyModelContainerSupport.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/AssemblyModelContainerSupport.java @@ -26,21 +26,6 @@ package gov.nist.secauto.metaschema.databind.model.impl; -import java.lang.reflect.Field; -import java.util.Arrays; -import java.util.Collection; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; -import java.util.Objects; -import java.util.stream.Collectors; -import java.util.stream.Stream; - -import javax.xml.namespace.QName; - -import edu.umd.cs.findbugs.annotations.NonNull; -import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; - import gov.nist.secauto.metaschema.core.model.IChoiceInstance; import gov.nist.secauto.metaschema.core.model.IContainerModelAssemblySupport; import gov.nist.secauto.metaschema.core.util.CollectionUtil; @@ -56,6 +41,21 @@ import gov.nist.secauto.metaschema.databind.model.annotations.BoundField; import gov.nist.secauto.metaschema.databind.model.annotations.Ignore; +import java.lang.reflect.Field; +import java.util.Arrays; +import java.util.Collection; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import javax.xml.namespace.QName; + +import edu.umd.cs.findbugs.annotations.NonNull; +import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; + class AssemblyModelContainerSupport implements IContainerModelAssemblySupport< IBoundInstanceModel, diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/ConstraintSupport.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/ConstraintSupport.java index fad12f9f4..eb69d743a 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/ConstraintSupport.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/ConstraintSupport.java @@ -63,19 +63,19 @@ public static void parse( // NOPMD - intentional try { Arrays.stream(valueAnnotation.lets()) .map(annotation -> ConstraintFactory.newLetExpression(annotation, source)) - .forEachOrdered(let -> set.addLetExpression(let)); + .forEachOrdered(set::addLetExpression); Arrays.stream(valueAnnotation.allowedValues()) .map(annotation -> ConstraintFactory.newAllowedValuesConstraint(annotation, source)) - .forEachOrdered(constraint -> set.addConstraint(constraint)); + .forEachOrdered(set::addConstraint); Arrays.stream(valueAnnotation.matches()) .map(annotation -> ConstraintFactory.newMatchesConstraint(annotation, source)) - .forEachOrdered(constraint -> set.addConstraint(constraint)); + .forEachOrdered(set::addConstraint); Arrays.stream(valueAnnotation.indexHasKey()) .map(annotation -> ConstraintFactory.newIndexHasKeyConstraint(annotation, source)) - .forEachOrdered(constraint -> set.addConstraint(constraint)); + .forEachOrdered(set::addConstraint); Arrays.stream(valueAnnotation.expect()) .map(annotation -> ConstraintFactory.newExpectConstraint(annotation, source)) - .forEachOrdered(constraint -> set.addConstraint(constraint)); + .forEachOrdered(set::addConstraint); } catch (MetapathException ex) { throw new MetapathException( String.format("Unable to compile a Metapath in '%s'. %s", source.getSource(), ex.getLocalizedMessage()), @@ -104,15 +104,15 @@ public static void parse( // NOPMD - intentional try { Arrays.stream(assemblyAnnotation.index()) .map(annotation -> ConstraintFactory.newIndexConstraint(annotation, source)) - .forEachOrdered(constraint -> set.addConstraint(constraint)); + .forEachOrdered(set::addConstraint); Arrays.stream(assemblyAnnotation.unique()) .map(annotation -> ConstraintFactory.newUniqueConstraint(annotation, source)) - .forEachOrdered(constraint -> set.addConstraint(constraint)); + .forEachOrdered(set::addConstraint); Arrays.stream(assemblyAnnotation.cardinality()) .map(annotation -> ConstraintFactory.newCardinalityConstraint(annotation, source)) - .forEachOrdered(constraint -> set.addConstraint(constraint)); + .forEachOrdered(set::addConstraint); } catch (MetapathException ex) { throw new MetapathException( String.format("Unable to compile a Metapath in '%s'. %s", source.getSource(), ex.getLocalizedMessage()), diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/DefaultGroupAs.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/DefaultGroupAs.java index 261869ca6..185f2dc00 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/DefaultGroupAs.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/DefaultGroupAs.java @@ -26,54 +26,42 @@ package gov.nist.secauto.metaschema.databind.model.impl; +import gov.nist.secauto.metaschema.core.model.IModule; import gov.nist.secauto.metaschema.core.model.JsonGroupAsBehavior; import gov.nist.secauto.metaschema.core.model.XmlGroupAsBehavior; import gov.nist.secauto.metaschema.databind.model.IGroupAs; import gov.nist.secauto.metaschema.databind.model.annotations.GroupAs; import gov.nist.secauto.metaschema.databind.model.annotations.ModelUtil; -import java.util.function.Supplier; +import javax.xml.namespace.QName; import edu.umd.cs.findbugs.annotations.NonNull; -import edu.umd.cs.findbugs.annotations.Nullable; import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; public class DefaultGroupAs implements IGroupAs { @NonNull - private final String name; - @Nullable - private final String namespace; + private final QName qname; @NonNull private final GroupAs annotation; @SuppressFBWarnings(value = "CT_CONSTRUCTOR_THROW", justification = "Use of final fields") public DefaultGroupAs( @NonNull GroupAs annotation, - @NonNull Supplier defaultSupplier) { + @NonNull IModule module) { this.annotation = annotation; - { - String value = ModelUtil.resolveNoneOrDefault(annotation.name(), null); - if (value == null) { - throw new IllegalStateException( - String.format("The %s#groupName value '%s' resulted in an invalid null value", - GroupAs.class.getName(), - annotation.name())); - } - this.name = value; + String value = ModelUtil.resolveNoneOrDefault(annotation.name(), null); + if (value == null) { + throw new IllegalStateException( + String.format("The %s#groupName value '%s' resulted in an invalid null value", + GroupAs.class.getName(), + annotation.name())); } - this.namespace = ModelUtil.resolveOptionalNamespace( - annotation.namespace(), - defaultSupplier); + this.qname = module.toModelQName(value); } @Override - public String getGroupAsName() { - return name; - } - - @Override - public String getGroupAsXmlNamespace() { - return namespace; + public QName getGroupAsQName() { + return qname; } @Override diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/DefinitionAssembly.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/DefinitionAssembly.java index 0e19ded94..6ae6a8e4c 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/DefinitionAssembly.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/DefinitionAssembly.java @@ -35,7 +35,6 @@ import gov.nist.secauto.metaschema.databind.IBindingContext; import gov.nist.secauto.metaschema.databind.io.BindingException; import gov.nist.secauto.metaschema.databind.model.IBoundDefinitionModelAssembly; -import gov.nist.secauto.metaschema.databind.model.IBoundInstanceFlag; import gov.nist.secauto.metaschema.databind.model.IBoundInstanceModel; import gov.nist.secauto.metaschema.databind.model.IBoundInstanceModelAssembly; import gov.nist.secauto.metaschema.databind.model.IBoundInstanceModelChoiceGroup; @@ -57,7 +56,7 @@ import nl.talsmasoftware.lazy4j.Lazy; //TODO: implement getProperties() -public class DefinitionAssembly +public final class DefinitionAssembly extends AbstractBoundDefinitionModelComplex implements IBoundDefinitionModelAssembly, IFeatureBoundContainerModelAssembly< @@ -66,6 +65,7 @@ public class DefinitionAssembly IBoundInstanceModelField, IBoundInstanceModelAssembly, IBoundInstanceModelChoiceGroup> { + @NonNull private final Lazy flagContainer; @NonNull @@ -77,25 +77,25 @@ public class DefinitionAssembly @NonNull private final Lazy> jsonProperties; - public DefinitionAssembly( + public static DefinitionAssembly newInstance( + @NonNull Class clazz, + @NonNull IBindingContext bindingContext) { + MetaschemaAssembly annotation = ModelUtil.getAnnotation(clazz, MetaschemaAssembly.class); + Class moduleClass = annotation.moduleClass(); + return new DefinitionAssembly(clazz, annotation, moduleClass, bindingContext); + } + + private DefinitionAssembly( @NonNull Class clazz, + @NonNull MetaschemaAssembly annotation, + @NonNull Class moduleClass, @NonNull IBindingContext bindingContext) { - super(clazz, MetaschemaAssembly.class, bindingContext); + super(clazz, annotation, moduleClass, bindingContext); String rootLocalName = ModelUtil.resolveNoneOrDefault(getAnnotation().rootName(), null); - this.xmlRootQName = ObjectUtils.notNull(Lazy.lazy(() -> { - QName retval = null; - if (rootLocalName != null) { - String namespace = ModelUtil.resolveOptionalNamespace( - getAnnotation().rootNamespace(), - () -> getContainingModule().getXmlNamespace().toASCIIString()); - if (namespace == null) { - namespace = getXmlNamespace(); - } - retval = new QName(namespace, rootLocalName); - } - return retval; - })); + this.xmlRootQName = ObjectUtils.notNull(Lazy.lazy(() -> rootLocalName == null + ? null + : getContainingModule().toModelQName(rootLocalName))); this.flagContainer = ObjectUtils.notNull(Lazy.lazy(() -> new FlagContainerSupport(this, null))); this.modelContainer = ObjectUtils.notNull(Lazy.lazy(() -> new AssemblyModelContainerSupport(this))); this.constraints = ObjectUtils.notNull(Lazy.lazy(() -> { @@ -124,6 +124,11 @@ protected void deepCopyItemInternal(Object fromObject, Object toObject) throws B } } + @Override + public Map getJsonProperties() { + return ObjectUtils.notNull(jsonProperties.get()); + } + // ------------------------------------------ // - Start annotation driven code - CPD-OFF - // ------------------------------------------ @@ -135,11 +140,6 @@ public FlagContainerSupport getFlagContainer() { return flagContainer.get(); } - @Override - public IBoundInstanceFlag getJsonKeyFlagInstance() { - return getFlagContainer().getJsonKeyFlagInstance(); - } - @Override @SuppressWarnings("null") @NonNull @@ -153,11 +153,6 @@ public IModelConstrained getConstraintSupport() { return ObjectUtils.notNull(constraints.get()); } - @Override - public Map getJsonProperties() { - return ObjectUtils.notNull(jsonProperties.get()); - } - @Override @Nullable public String getFormalName() { @@ -188,12 +183,6 @@ public MarkupMultiline getRemarks() { return ModelUtil.resolveToMarkupMultiline(getAnnotation().description()); } - @Override - @NonNull - protected Class getModuleClass() { - return getAnnotation().moduleClass(); - } - @Override @Nullable public QName getRootXmlQName() { diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/DefinitionField.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/DefinitionField.java index b4d7ab2f3..a66ff1bde 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/DefinitionField.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/DefinitionField.java @@ -53,11 +53,10 @@ import edu.umd.cs.findbugs.annotations.NonNull; import edu.umd.cs.findbugs.annotations.Nullable; -import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; import nl.talsmasoftware.lazy4j.Lazy; //TODO: implement getProperties() -public class DefinitionField +public final class DefinitionField extends AbstractBoundDefinitionModelComplex implements IBoundDefinitionModelFieldComplex { @NonNull @@ -84,12 +83,7 @@ private static Field getFieldValueField(Class clazz) { Field retval = null; for (Field field : fields) { - if (!field.isAnnotationPresent(BoundFieldValue.class)) { - // skip fields that aren't a field or assembly instance - continue; - } - - if (field.isAnnotationPresent(Ignore.class)) { + if (!field.isAnnotationPresent(BoundFieldValue.class) || field.isAnnotationPresent(Ignore.class)) { // skip this field, since it is ignored continue; } @@ -113,12 +107,23 @@ private static Field getFieldValueField(Class clazz) { * the Java class the definition is bound to * @param bindingContext * the Metaschema binding context managing this class + * @return the instance */ - @SuppressFBWarnings(value = "CT_CONSTRUCTOR_THROW", justification = "Use of final fields, except JSON key") - public DefinitionField( + @NonNull + public static DefinitionField newInstance( + @NonNull Class clazz, + @NonNull IBindingContext bindingContext) { + MetaschemaField annotation = ModelUtil.getAnnotation(clazz, MetaschemaField.class); + Class moduleClass = annotation.moduleClass(); + return new DefinitionField(clazz, annotation, moduleClass, bindingContext); + } + + private DefinitionField( @NonNull Class clazz, + @NonNull MetaschemaField annotation, + @NonNull Class moduleClass, @NonNull IBindingContext bindingContext) { - super(clazz, MetaschemaField.class, bindingContext); + super(clazz, annotation, moduleClass, bindingContext); Field field = getFieldValueField(getBoundClass()); if (field == null) { throw new IllegalArgumentException( @@ -126,7 +131,7 @@ public DefinitionField( clazz.getName(), BoundFieldValue.class.getName())); // NOPMD false positive } - this.fieldValue = new FieldValue(field, BoundFieldValue.class); + this.fieldValue = new FieldValue(field, BoundFieldValue.class, bindingContext); this.flagContainer = ObjectUtils.notNull(Lazy.lazy(() -> new FlagContainerSupport(this, this::handleFlagInstance))); this.constraints = ObjectUtils.notNull(Lazy.lazy(() -> { IModelConstrained retval = new AssemblyConstraintSet(); @@ -136,7 +141,7 @@ public DefinitionField( })); this.jsonProperties = ObjectUtils.notNull(Lazy.lazy(() -> { IBoundInstanceFlag jsonValueKey = getJsonValueKeyFlagInstance(); - Predicate flagFilter = jsonValueKey == null ? null : (flag) -> !flag.equals(jsonValueKey); + Predicate flagFilter = jsonValueKey == null ? null : flag -> !flag.equals(jsonValueKey); return getJsonProperties(flagFilter); })); } @@ -185,11 +190,6 @@ public FlagContainerSupport getFlagContainer() { return flagContainer.get(); } - @Override - public IBoundInstanceFlag getJsonKeyFlagInstance() { - return getFlagContainer().getJsonKeyFlagInstance(); - } - @Override @NonNull public IValueConstrained getConstraintSupport() { @@ -231,16 +231,13 @@ public MarkupMultiline getRemarks() { return ModelUtil.resolveToMarkupMultiline(getAnnotation().description()); } - @Override - @NonNull - protected Class getModuleClass() { - return getAnnotation().moduleClass(); - } - protected class FieldValue - extends AbstractBoundAnnotatedJavaField implements IBoundFieldValue { @NonNull + private final Field javaField; + @NonNull + private final BoundFieldValue annotation; + @NonNull private final IDataTypeAdapter javaTypeAdapter; @Nullable private final Object defaultValue; @@ -252,15 +249,40 @@ protected class FieldValue * the Java field the field value is bound to * @param annotationClass * the field value binding annotation Java class + * @param bindingContext + * the Metaschema binding context managing this class */ protected FieldValue( @NonNull Field javaField, - @NonNull Class annotationClass) { - super(javaField, annotationClass); + @NonNull Class annotationClass, + @NonNull IBindingContext bindingContext) { + this.javaField = javaField; + this.annotation = ModelUtil.getAnnotation(javaField, annotationClass); this.javaTypeAdapter = ModelUtil.getDataTypeAdapter( - getAnnotation().typeAdapter(), - getBindingContext()); - this.defaultValue = ModelUtil.resolveNullOrValue(getAnnotation().defaultValue(), this.javaTypeAdapter); + this.annotation.typeAdapter(), + bindingContext); + this.defaultValue = ModelUtil.resolveNullOrValue(this.annotation.defaultValue(), this.javaTypeAdapter); + } + + /** + * Get the bound Java field. + * + * @return the bound Java field + */ + @Override + @NonNull + public Field getField() { + return javaField; + } + + /** + * Get the binding Java annotation. + * + * @return the binding Java annotation + */ + @NonNull + public BoundFieldValue getAnnotation() { + return annotation; } @Override diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/FlagContainerSupport.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/FlagContainerSupport.java index d88292805..e6cdc5bea 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/FlagContainerSupport.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/FlagContainerSupport.java @@ -26,6 +26,14 @@ package gov.nist.secauto.metaschema.databind.model.impl; +import gov.nist.secauto.metaschema.core.model.IContainerFlagSupport; +import gov.nist.secauto.metaschema.core.util.CollectionUtil; +import gov.nist.secauto.metaschema.core.util.ObjectUtils; +import gov.nist.secauto.metaschema.databind.model.IBoundDefinitionModelComplex; +import gov.nist.secauto.metaschema.databind.model.IBoundInstanceFlag; +import gov.nist.secauto.metaschema.databind.model.annotations.BoundFlag; +import gov.nist.secauto.metaschema.databind.model.annotations.Ignore; + import java.lang.reflect.Field; import java.util.Collection; import java.util.Collections; @@ -44,13 +52,6 @@ import edu.umd.cs.findbugs.annotations.Nullable; import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; -import gov.nist.secauto.metaschema.core.model.IContainerFlagSupport; -import gov.nist.secauto.metaschema.core.util.CollectionUtil; -import gov.nist.secauto.metaschema.core.util.ObjectUtils; -import gov.nist.secauto.metaschema.databind.model.IBoundInstanceFlag; -import gov.nist.secauto.metaschema.databind.model.annotations.BoundFlag; -import gov.nist.secauto.metaschema.databind.model.annotations.Ignore; - public class FlagContainerSupport implements IContainerFlagSupport { @NonNull private final Map flagInstances; @@ -60,7 +61,7 @@ public class FlagContainerSupport implements IContainerFlagSupport definition, + @NonNull IBoundDefinitionModelComplex definition, @Nullable Consumer peeker) { Class clazz = definition.getBoundClass(); @@ -111,12 +112,7 @@ protected static Collection getFlagInstanceFields(Class clazz) { } for (Field field : fields) { - if (!field.isAnnotationPresent(BoundFlag.class)) { - // skip non-flag fields - continue; - } - - if (field.isAnnotationPresent(Ignore.class)) { + if (!field.isAnnotationPresent(BoundFlag.class) || field.isAnnotationPresent(Ignore.class)) { // skip this field, since it is ignored continue; } @@ -144,6 +140,7 @@ public Map getFlagInstanceMap() { return flagInstances; } + @Override public IBoundInstanceFlag getJsonKeyFlagInstance() { return jsonKeyFlag; } diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/IFeatureBoundContainerModelAssembly.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/IFeatureBoundContainerModelAssembly.java index e64c9148b..abded64be 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/IFeatureBoundContainerModelAssembly.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/IFeatureBoundContainerModelAssembly.java @@ -26,13 +26,6 @@ package gov.nist.secauto.metaschema.databind.model.impl; -import java.util.Collection; -import java.util.List; -import java.util.Map; - -import javax.xml.namespace.QName; - -import edu.umd.cs.findbugs.annotations.NonNull; import gov.nist.secauto.metaschema.core.model.IChoiceInstance; import gov.nist.secauto.metaschema.core.model.IContainerModelAssemblySupport; import gov.nist.secauto.metaschema.core.model.IFeatureContainerModelAssembly; @@ -44,6 +37,14 @@ import gov.nist.secauto.metaschema.databind.model.IBoundInstanceModelField; import gov.nist.secauto.metaschema.databind.model.IBoundInstanceModelNamed; +import java.util.Collection; +import java.util.List; +import java.util.Map; + +import javax.xml.namespace.QName; + +import edu.umd.cs.findbugs.annotations.NonNull; + public interface IFeatureBoundContainerModelAssembly< MI extends IBoundInstanceModel, NMI extends IBoundInstanceModelNamed, diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/IFeatureBoundContainerModelChoiceGroup.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/IFeatureBoundContainerModelChoiceGroup.java index b4b03d538..a707fa3a9 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/IFeatureBoundContainerModelChoiceGroup.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/IFeatureBoundContainerModelChoiceGroup.java @@ -26,20 +26,26 @@ package gov.nist.secauto.metaschema.databind.model.impl; -import java.util.Collection; - -import javax.xml.namespace.QName; - -import edu.umd.cs.findbugs.annotations.NonNull; import gov.nist.secauto.metaschema.core.model.IContainerModelSupport; +import gov.nist.secauto.metaschema.core.model.IFeatureContainerModelGrouped; import gov.nist.secauto.metaschema.databind.model.IBoundContainerModelChoiceGroup; import gov.nist.secauto.metaschema.databind.model.IBoundInstanceModelGroupedAssembly; import gov.nist.secauto.metaschema.databind.model.IBoundInstanceModelGroupedField; import gov.nist.secauto.metaschema.databind.model.IBoundInstanceModelGroupedNamed; +import java.util.Collection; + +import javax.xml.namespace.QName; + +import edu.umd.cs.findbugs.annotations.NonNull; + public interface IFeatureBoundContainerModelChoiceGroup - extends IBoundContainerModelChoiceGroup { + extends IBoundContainerModelChoiceGroup, IFeatureContainerModelGrouped< + IBoundInstanceModelGroupedNamed, + IBoundInstanceModelGroupedField, + IBoundInstanceModelGroupedAssembly> { + @Override @NonNull IContainerModelSupport< IBoundInstanceModelGroupedNamed, @@ -47,6 +53,11 @@ public interface IFeatureBoundContainerModelChoiceGroup IBoundInstanceModelGroupedField, IBoundInstanceModelGroupedAssembly> getModelContainer(); + @Override + default Collection getModelInstances() { + return getModelContainer().getModelInstances(); + } + @Override default IBoundInstanceModelGroupedNamed getNamedModelInstanceByName(QName name) { return getModelContainer().getNamedModelInstanceMap().get(name); diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/IFeatureInstanceModelGroupAs.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/IFeatureInstanceModelGroupAs.java index 1b41acae5..cc2ee1b59 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/IFeatureInstanceModelGroupAs.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/IFeatureInstanceModelGroupAs.java @@ -28,6 +28,7 @@ import gov.nist.secauto.metaschema.core.model.JsonGroupAsBehavior; import gov.nist.secauto.metaschema.core.model.XmlGroupAsBehavior; +import gov.nist.secauto.metaschema.databind.io.BindingException; import gov.nist.secauto.metaschema.databind.model.IBoundInstanceModel; import gov.nist.secauto.metaschema.databind.model.IGroupAs; @@ -56,4 +57,13 @@ default JsonGroupAsBehavior getJsonGroupAsBehavior() { default XmlGroupAsBehavior getXmlGroupAsBehavior() { return getGroupAs().getXmlGroupAsBehavior(); } + + @Override + default void deepCopy(@NonNull Object fromInstance, @NonNull Object toInstance) throws BindingException { + Object value = getValue(fromInstance); + if (value != null) { + value = getCollectionInfo().deepCopyItems(fromInstance, toInstance); + } + setValue(toInstance, value); + } } diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/InstanceFlagInline.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/InstanceFlagInline.java index 39a812c32..8f19a4afa 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/InstanceFlagInline.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/InstanceFlagInline.java @@ -29,11 +29,12 @@ import gov.nist.secauto.metaschema.core.datatype.IDataTypeAdapter; import gov.nist.secauto.metaschema.core.datatype.markup.MarkupLine; import gov.nist.secauto.metaschema.core.datatype.markup.MarkupMultiline; +import gov.nist.secauto.metaschema.core.model.AbstractInlineFlagDefinition; import gov.nist.secauto.metaschema.core.model.constraint.ISource; import gov.nist.secauto.metaschema.core.model.constraint.IValueConstrained; import gov.nist.secauto.metaschema.core.model.constraint.ValueConstraintSet; import gov.nist.secauto.metaschema.core.util.ObjectUtils; -import gov.nist.secauto.metaschema.databind.IBindingContext; +import gov.nist.secauto.metaschema.databind.model.IBoundDefinitionFlag; import gov.nist.secauto.metaschema.databind.model.IBoundDefinitionModel; import gov.nist.secauto.metaschema.databind.model.IBoundInstanceFlag; import gov.nist.secauto.metaschema.databind.model.annotations.BoundFlag; @@ -55,9 +56,14 @@ */ // TODO: implement getProperties() public class InstanceFlagInline - extends AbstractBoundInstanceJavaField + extends AbstractInlineFlagDefinition + // extends AbstractBoundInstanceJavaField implements IBoundInstanceFlag { @NonNull + private final Field javaField; + @NonNull + private final BoundFlag annotation; + @NonNull private final IDataTypeAdapter javaTypeAdapter; @Nullable private final Object defaultValue; @@ -75,7 +81,9 @@ public class InstanceFlagInline public InstanceFlagInline( @NonNull Field javaField, @NonNull IBoundDefinitionModel containingDefinition) { - super(javaField, BoundFlag.class, containingDefinition); + super(containingDefinition); + this.javaField = javaField; + this.annotation = ModelUtil.getAnnotation(javaField, BoundFlag.class); Class> adapterClass = ObjectUtils.notNull(getAnnotation().typeAdapter()); this.javaTypeAdapter = ModelUtil.getDataTypeAdapter( adapterClass, @@ -90,6 +98,26 @@ public InstanceFlagInline( })); } + /** + * Get the bound Java field. + * + * @return the bound Java field + */ + @Override + @NonNull + public Field getField() { + return javaField; + } + + /** + * Get the binding Java annotation. + * + * @return the binding Java annotation + */ + @NonNull + private BoundFlag getAnnotation() { + return annotation; + } // ------------------------------------------ // - Start annotation driven code - CPD-OFF - // ------------------------------------------ @@ -149,24 +177,12 @@ public Integer getUseIndex() { return getAnnotation().useIndex(); } - @Override - public String getXmlNamespace() { - return ModelUtil.resolveOptionalNamespace( - getAnnotation().namespace(), - () -> getContainingDefinition().getXmlNamespace()); - } - @Override @Nullable public MarkupMultiline getRemarks() { return ModelUtil.resolveToMarkupMultiline(getAnnotation().remarks()); } - @Override - public IBindingContext getBindingContext() { - return getContainingDefinition().getBindingContext(); - } - // ---------------------------------------- // - End annotation driven code - CPD-OFF - // ---------------------------------------- diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/InstanceModelAssemblyComplex.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/InstanceModelAssemblyComplex.java index 9dd778047..d7fe1f13f 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/InstanceModelAssemblyComplex.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/InstanceModelAssemblyComplex.java @@ -28,7 +28,7 @@ import gov.nist.secauto.metaschema.core.datatype.markup.MarkupLine; import gov.nist.secauto.metaschema.core.datatype.markup.MarkupMultiline; -import gov.nist.secauto.metaschema.core.model.IFeatureDefinitionReferenceInstance; +import gov.nist.secauto.metaschema.core.model.AbstractAssemblyInstance; import gov.nist.secauto.metaschema.core.util.ObjectUtils; import gov.nist.secauto.metaschema.databind.model.IBoundDefinitionModelAssembly; import gov.nist.secauto.metaschema.databind.model.IBoundInstanceFlag; @@ -38,6 +38,7 @@ import gov.nist.secauto.metaschema.databind.model.annotations.BoundAssembly; import gov.nist.secauto.metaschema.databind.model.annotations.GroupAs; import gov.nist.secauto.metaschema.databind.model.annotations.ModelUtil; +import gov.nist.secauto.metaschema.databind.model.info.IModelInstanceCollectionInfo; import java.lang.reflect.Field; import java.util.Map; @@ -52,10 +53,19 @@ * supported by a bound definition class. */ // TODO: implement getProperties() -public class InstanceModelAssemblyComplex - extends AbstractBoundInstanceModelJavaField - implements IBoundInstanceModelAssembly, - IFeatureDefinitionReferenceInstance { +public final class InstanceModelAssemblyComplex + extends AbstractAssemblyInstance< + IBoundDefinitionModelAssembly, + IBoundDefinitionModelAssembly, + IBoundInstanceModelAssembly, + IBoundDefinitionModelAssembly> + implements IBoundInstanceModelAssembly, IFeatureInstanceModelGroupAs { + @NonNull + private final Field javaField; + @NonNull + private final BoundAssembly annotation; + @NonNull + private final Lazy collectionInfo; @NonNull private final IBoundDefinitionModelAssembly definition; @NonNull @@ -73,40 +83,75 @@ public class InstanceModelAssemblyComplex * the assembly definition this instance is bound to * @param containingDefinition * the definition containing this instance + * @return the instance */ - @SuppressFBWarnings(value = "CT_CONSTRUCTOR_THROW", justification = "Use of final fields") - public InstanceModelAssemblyComplex( + @NonNull + public static InstanceModelAssemblyComplex newInstance( @NonNull Field javaField, @NonNull IBoundDefinitionModelAssembly definition, @NonNull IBoundDefinitionModelAssembly containingDefinition) { - super(javaField, BoundAssembly.class, containingDefinition); - this.definition = definition; - this.groupAs = ModelUtil.groupAs( - getAnnotation().groupAs(), - () -> containingDefinition.getXmlNamespace()); - if (getMaxOccurs() == -1 || getMaxOccurs() > 1) { - if (IGroupAs.SINGLETON_GROUP_AS.equals(this.groupAs)) { + BoundAssembly annotation = ModelUtil.getAnnotation(javaField, BoundAssembly.class); + IGroupAs groupAs = ModelUtil.groupAs(annotation.groupAs(), containingDefinition.getContainingModule()); + if (annotation.maxOccurs() == -1 || annotation.maxOccurs() > 1) { + if (IGroupAs.SINGLETON_GROUP_AS.equals(groupAs)) { throw new IllegalStateException(String.format("Field '%s' on class '%s' is missing the '%s' annotation.", - getField().getName(), + javaField.getName(), containingDefinition.getBoundClass().getName(), GroupAs.class.getName())); } - } else if (!IGroupAs.SINGLETON_GROUP_AS.equals(this.groupAs)) { + } else if (!IGroupAs.SINGLETON_GROUP_AS.equals(groupAs)) { // max is 1 and a groupAs is set throw new IllegalStateException( String.format( "Field '%s' on class '%s' has the '%s' annotation, but maxOccurs=1. A groupAs must not be specfied.", - getField().getName(), + javaField.getName(), containingDefinition.getBoundClass().getName(), GroupAs.class.getName())); } + return new InstanceModelAssemblyComplex(javaField, annotation, groupAs, definition, containingDefinition); + } + + @SuppressFBWarnings(value = "CT_CONSTRUCTOR_THROW", justification = "Use of final fields") + private InstanceModelAssemblyComplex( + @NonNull Field javaField, + @NonNull BoundAssembly annotation, + @NonNull IGroupAs groupAs, + @NonNull IBoundDefinitionModelAssembly definition, + @NonNull IBoundDefinitionModelAssembly containingDefinition) { + super(containingDefinition); + this.javaField = javaField; + this.annotation = annotation; + this.groupAs = groupAs; + this.collectionInfo = ObjectUtils.notNull(Lazy.lazy(() -> IModelInstanceCollectionInfo.of(this))); + this.definition = definition; this.jsonProperties = ObjectUtils.notNull(Lazy.lazy(() -> { - IBoundInstanceFlag jsonKey = getJsonKey(); - Predicate flagFilter = jsonKey == null ? null : (flag) -> !jsonKey.equals(flag); + IBoundInstanceFlag jsonKey = getEffectiveJsonKey(); + Predicate flagFilter = jsonKey == null ? null : flag -> !jsonKey.equals(flag); return getDefinition().getJsonProperties(flagFilter); })); } + @Override + public Field getField() { + return javaField; + } + + /** + * Get the binding Java annotation. + * + * @return the binding Java annotation + */ + @NonNull + public BoundAssembly getAnnotation() { + return annotation; + } + + @SuppressWarnings("null") + @Override + public IModelInstanceCollectionInfo getCollectionInfo() { + return collectionInfo.get(); + } + // ------------------------------------------ // - Start annotation driven code - CPD-OFF - // ------------------------------------------ @@ -148,19 +193,12 @@ public Integer getUseIndex() { } @Override - public String getXmlNamespace() { - return ModelUtil.resolveOptionalNamespace( - getAnnotation().namespace(), - () -> getContainingDefinition().getXmlNamespace()); - } - - @Override - public final int getMinOccurs() { + public int getMinOccurs() { return getAnnotation().minOccurs(); } @Override - public final int getMaxOccurs() { + public int getMaxOccurs() { return getAnnotation().maxOccurs(); } diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/InstanceModelChoiceGroup.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/InstanceModelChoiceGroup.java index 4a0eb0cea..f8c31c75e 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/InstanceModelChoiceGroup.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/InstanceModelChoiceGroup.java @@ -26,6 +26,7 @@ package gov.nist.secauto.metaschema.databind.model.impl; +import gov.nist.secauto.metaschema.core.model.AbstractChoiceGroupInstance; import gov.nist.secauto.metaschema.core.model.IContainerModelSupport; import gov.nist.secauto.metaschema.core.util.CollectionUtil; import gov.nist.secauto.metaschema.core.util.CustomCollectors; @@ -42,6 +43,7 @@ import gov.nist.secauto.metaschema.databind.model.annotations.BoundGroupedField; import gov.nist.secauto.metaschema.databind.model.annotations.GroupAs; import gov.nist.secauto.metaschema.databind.model.annotations.ModelUtil; +import gov.nist.secauto.metaschema.databind.model.info.IModelInstanceCollectionInfo; import java.lang.reflect.Field; import java.util.Arrays; @@ -49,6 +51,7 @@ import java.util.Collections; import java.util.LinkedHashMap; import java.util.Map; +import java.util.Map.Entry; import java.util.function.Function; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -63,10 +66,21 @@ /** * Implements a Metaschema module choice group instance bound to a Java field. */ -public class InstanceModelChoiceGroup - extends AbstractBoundInstanceModelJavaField +public final class InstanceModelChoiceGroup + extends AbstractChoiceGroupInstance< + IBoundDefinitionModelAssembly, + IBoundInstanceModelGroupedNamed, + IBoundInstanceModelGroupedField, + IBoundInstanceModelGroupedAssembly> + // extends AbstractBoundInstanceModelJavaField implements IBoundInstanceModelChoiceGroup, - IFeatureBoundContainerModelChoiceGroup { + IFeatureBoundContainerModelChoiceGroup, IFeatureInstanceModelGroupAs { + @NonNull + private final Field javaField; + @NonNull + private final BoundChoiceGroup annotation; + @NonNull + private final Lazy collectionInfo; @NonNull private final IGroupAs groupAs; @NonNull @@ -83,55 +97,89 @@ public class InstanceModelChoiceGroup * * @param javaField * the Java field bound to this instance - * @param containingDefinition + * @param parent * the definition containing this instance + * @return the instance */ - @SuppressFBWarnings(value = "CT_CONSTRUCTOR_THROW", justification = "Use of final fields") - public InstanceModelChoiceGroup( + @NonNull + public static InstanceModelChoiceGroup newInstance( @NonNull Field javaField, - @NonNull IBoundDefinitionModelAssembly containingDefinition) { - super(javaField, BoundChoiceGroup.class, containingDefinition); - this.groupAs = ModelUtil.groupAs( - getAnnotation().groupAs(), - () -> containingDefinition.getXmlNamespace()); - if (getMaxOccurs() == -1 || getMaxOccurs() > 1) { - if (IGroupAs.SINGLETON_GROUP_AS.equals(this.groupAs)) { + @NonNull IBoundDefinitionModelAssembly parent) { + BoundChoiceGroup annotation = ModelUtil.getAnnotation(javaField, BoundChoiceGroup.class); + IGroupAs groupAs = ModelUtil.groupAs(annotation.groupAs(), parent.getContainingModule()); + if (annotation.maxOccurs() == -1 || annotation.maxOccurs() > 1) { + if (IGroupAs.SINGLETON_GROUP_AS.equals(groupAs)) { throw new IllegalStateException(String.format("Field '%s' on class '%s' is missing the '%s' annotation.", - getField().getName(), - containingDefinition.getBoundClass().getName(), + javaField.getName(), + parent.getBoundClass().getName(), GroupAs.class.getName())); } - } else if (!IGroupAs.SINGLETON_GROUP_AS.equals(this.groupAs)) { + } else if (!IGroupAs.SINGLETON_GROUP_AS.equals(groupAs)) { // max is 1 and a groupAs is set throw new IllegalStateException( String.format( "Field '%s' on class '%s' has the '%s' annotation, but maxOccurs=1. A groupAs must not be specfied.", - getField().getName(), - containingDefinition.getBoundClass().getName(), + javaField.getName(), + parent.getBoundClass().getName(), GroupAs.class.getName())); } + return new InstanceModelChoiceGroup(javaField, annotation, groupAs, parent); + } + + @SuppressFBWarnings(value = "CT_CONSTRUCTOR_THROW", justification = "Use of final fields") + private InstanceModelChoiceGroup( + @NonNull Field javaField, + @NonNull BoundChoiceGroup annotation, + @NonNull IGroupAs groupAs, + @NonNull IBoundDefinitionModelAssembly parent) { + super(parent); + this.javaField = javaField; + this.annotation = annotation; + this.groupAs = groupAs; + this.collectionInfo = ObjectUtils.notNull(Lazy.lazy(() -> IModelInstanceCollectionInfo.of(this))); this.modelContainer = ObjectUtils.notNull(Lazy.lazy(() -> new ChoiceGroupModelContainerSupport( - getAnnotation().assemblies(), - getAnnotation().fields(), + this.annotation.assemblies(), + this.annotation.fields(), this))); this.classToInstanceMap = ObjectUtils.notNull(Lazy.lazy(() -> Collections.unmodifiableMap( getNamedModelInstances().stream() .map(instance -> instance) .collect(Collectors.toMap( - item -> (Class) item.getDefinition().getBoundClass(), + item -> item.getDefinition().getBoundClass(), CustomCollectors.identity()))))); this.qnameToInstanceMap = ObjectUtils.notNull(Lazy.lazy(() -> Collections.unmodifiableMap( getNamedModelInstances().stream() .collect(Collectors.toMap( - item -> item.getXmlQName(), + IBoundInstanceModelGroupedNamed::getXmlQName, CustomCollectors.identity()))))); this.discriminatorToInstanceMap = ObjectUtils.notNull(Lazy.lazy(() -> Collections.unmodifiableMap( getNamedModelInstances().stream() .collect(Collectors.toMap( - item -> item.getEffectiveDisciminatorValue(), + IBoundInstanceModelGroupedNamed::getEffectiveDisciminatorValue, CustomCollectors.identity()))))); } + @Override + public Field getField() { + return javaField; + } + + /** + * Get the binding Java annotation. + * + * @return the binding Java annotation + */ + @NonNull + public BoundChoiceGroup getAnnotation() { + return annotation; + } + + @SuppressWarnings("null") + @Override + public IModelInstanceCollectionInfo getCollectionInfo() { + return collectionInfo.get(); + } + // ------------------------------------------ // - Start annotation driven code - CPD-OFF - // ------------------------------------------ @@ -201,16 +249,16 @@ public ChoiceGroupModelContainerSupport getModelContainer() { @Override public IBoundDefinitionModelAssembly getOwningDefinition() { - return getContainingDefinition(); + return getParentContainer(); } @Override - public final int getMinOccurs() { + public int getMinOccurs() { return getAnnotation().minOccurs(); } @Override - public final int getMaxOccurs() { + public int getMaxOccurs() { return getAnnotation().maxOccurs(); } @@ -220,13 +268,13 @@ public String getJsonDiscriminatorProperty() { } @Override - public String getJsonKeyFlagName() { + public String getJsonKeyFlagInstanceName() { return getAnnotation().jsonKey(); } @Override public IBoundInstanceFlag getItemJsonKey(Object item) { - String jsonKeyFlagName = getJsonKeyFlagName(); + String jsonKeyFlagName = getJsonKeyFlagInstanceName(); IBoundInstanceFlag retval = null; if (jsonKeyFlagName != null) { @@ -264,7 +312,7 @@ public ChoiceGroupModelContainerSupport( container); }) .collect(Collectors.toMap( - instance -> instance.getXmlQName(), + IBoundInstanceModelGroupedAssembly::getXmlQName, Function.identity(), CustomCollectors.useLastMapper(), LinkedHashMap::new)))); @@ -274,7 +322,7 @@ public ChoiceGroupModelContainerSupport( return IBoundInstanceModelGroupedField.newInstance(instance, container); }) .collect(Collectors.toMap( - instance -> instance.getXmlQName(), + IBoundInstanceModelGroupedField::getXmlQName, Function.identity(), CustomCollectors.useLastMapper(), LinkedHashMap::new)))); @@ -282,8 +330,8 @@ public ChoiceGroupModelContainerSupport( this.assemblyInstances.entrySet().stream(), this.fieldInstances.entrySet().stream()) .collect(Collectors.toMap( - entry -> entry.getKey(), - entry -> entry.getValue(), + Entry::getKey, + Entry::getValue, CustomCollectors.useLastMapper(), LinkedHashMap::new)))); } diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/InstanceModelFieldComplex.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/InstanceModelFieldComplex.java index 9f3734450..b400efdde 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/InstanceModelFieldComplex.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/InstanceModelFieldComplex.java @@ -26,7 +26,9 @@ package gov.nist.secauto.metaschema.databind.model.impl; -import gov.nist.secauto.metaschema.core.model.IFeatureDefinitionReferenceInstance; +import gov.nist.secauto.metaschema.core.datatype.markup.MarkupLine; +import gov.nist.secauto.metaschema.core.datatype.markup.MarkupMultiline; +import gov.nist.secauto.metaschema.core.model.AbstractFieldInstance; import gov.nist.secauto.metaschema.core.util.ObjectUtils; import gov.nist.secauto.metaschema.databind.model.IBoundDefinitionModelAssembly; import gov.nist.secauto.metaschema.databind.model.IBoundDefinitionModelFieldComplex; @@ -34,7 +36,11 @@ import gov.nist.secauto.metaschema.databind.model.IBoundInstanceFlag; import gov.nist.secauto.metaschema.databind.model.IBoundInstanceModelFieldComplex; import gov.nist.secauto.metaschema.databind.model.IBoundProperty; +import gov.nist.secauto.metaschema.databind.model.IGroupAs; +import gov.nist.secauto.metaschema.databind.model.annotations.BoundField; +import gov.nist.secauto.metaschema.databind.model.annotations.GroupAs; import gov.nist.secauto.metaschema.databind.model.annotations.ModelUtil; +import gov.nist.secauto.metaschema.databind.model.info.IModelInstanceCollectionInfo; import java.lang.reflect.Field; import java.util.Map; @@ -48,10 +54,21 @@ * Implements a Metaschema module field instance bound to a Java field, * supported by a bound definition class. */ -public class InstanceModelFieldComplex - extends AbstractBoundInstanceField - implements IBoundInstanceModelFieldComplex, - IFeatureDefinitionReferenceInstance { +public final class InstanceModelFieldComplex + extends AbstractFieldInstance< + IBoundDefinitionModelAssembly, + IBoundDefinitionModelFieldComplex, + IBoundInstanceModelFieldComplex, + IBoundDefinitionModelAssembly> + implements IBoundInstanceModelFieldComplex, IFeatureInstanceModelGroupAs { + @NonNull + private final Field javaField; + @NonNull + private final BoundField annotation; + @NonNull + private final Lazy collectionInfo; + @NonNull + private final IGroupAs groupAs; @NonNull private final DefinitionField definition; @NonNull @@ -59,6 +76,50 @@ public class InstanceModelFieldComplex @NonNull private final Lazy> jsonProperties; + public static InstanceModelFieldComplex newInstance( + @NonNull Field javaField, + @NonNull DefinitionField definition, + @NonNull IBoundDefinitionModelAssembly parent) { + BoundField annotation = ModelUtil.getAnnotation(javaField, BoundField.class); + if (!annotation.inXmlWrapped()) { + if (definition.hasChildren()) { // NOPMD efficiency + throw new IllegalStateException( + String.format("Field '%s' on class '%s' is requested to be unwrapped, but it has flags preventing this.", + javaField.getName(), + parent.getBoundClass().getName())); + } + if (!definition.getJavaTypeAdapter().isUnrappedValueAllowedInXml()) { + throw new IllegalStateException( + String.format( + "Field '%s' on class '%s' is requested to be unwrapped, but its data type '%s' does not allow this.", + javaField.getName(), + parent.getBoundClass().getName(), + definition.getJavaTypeAdapter().getPreferredName())); + } + } + + IGroupAs groupAs = ModelUtil.groupAs( + annotation.groupAs(), + parent.getContainingModule()); + if (annotation.maxOccurs() == -1 || annotation.maxOccurs() > 1) { + if (IGroupAs.SINGLETON_GROUP_AS.equals(groupAs)) { + throw new IllegalStateException(String.format("Field '%s' on class '%s' is missing the '%s' annotation.", + javaField.getName(), + javaField.getDeclaringClass().getName(), + GroupAs.class.getName())); // NOPMD false positive + } + } else if (!IGroupAs.SINGLETON_GROUP_AS.equals(groupAs)) { + // max is 1 and a groupAs is set + throw new IllegalStateException( + String.format( + "Field '%s' on class '%s' has the '%s' annotation, but maxOccurs=1. A groupAs must not be specfied.", + javaField.getName(), + javaField.getDeclaringClass().getName(), + GroupAs.class.getName())); // NOPMD false positive + } + return new InstanceModelFieldComplex(javaField, annotation, groupAs, definition, parent); + } + /** * Construct a new field instance bound to a Java field, supported by a bound * definition class. @@ -67,32 +128,23 @@ public class InstanceModelFieldComplex * the Java field bound to this instance * @param definition * the assembly definition this instance is bound to - * @param containingDefinition + * @param parent * the definition containing this instance */ @SuppressFBWarnings(value = "CT_CONSTRUCTOR_THROW", justification = "Use of final fields") - public InstanceModelFieldComplex( + private InstanceModelFieldComplex( @NonNull Field javaField, + @NonNull BoundField annotation, + @NonNull IGroupAs groupAs, @NonNull DefinitionField definition, - @NonNull IBoundDefinitionModelAssembly containingDefinition) { - super(javaField, containingDefinition); + @NonNull IBoundDefinitionModelAssembly parent) { + super(parent); + this.javaField = javaField; + this.annotation = annotation; + this.collectionInfo = ObjectUtils.notNull(Lazy.lazy(() -> IModelInstanceCollectionInfo.of(this))); + this.groupAs = groupAs; this.definition = definition; - if (!isEffectiveValueWrappedInXml()) { - if (definition.hasChildren()) { // NOPMD efficiency - throw new IllegalStateException( - String.format("Field '%s' on class '%s' is requested to be unwrapped, but it has flags preventing this.", - javaField.getName(), - containingDefinition.getBoundClass().getName())); - } else if (!getDefinition().getJavaTypeAdapter().isUnrappedValueAllowedInXml()) { - throw new IllegalStateException( - String.format( - "Field '%s' on class '%s' is requested to be unwrapped, but its data type '%s' does not allow this.", - javaField.getName(), - containingDefinition.getBoundClass().getName(), - getDefinition().getJavaTypeAdapter().getPreferredName())); - } - } this.defaultValue = ObjectUtils.notNull(Lazy.lazy(() -> { Object retval = null; if (getMaxOccurs() == 1) { @@ -115,20 +167,37 @@ public InstanceModelFieldComplex( })); this.jsonProperties = ObjectUtils.notNull(Lazy.lazy(() -> { Predicate flagFilter = null; - IBoundInstanceFlag jsonKey = getJsonKey(); + IBoundInstanceFlag jsonKey = getEffectiveJsonKey(); if (jsonKey != null) { - flagFilter = (flag) -> !jsonKey.equals(flag); + flagFilter = flag -> !jsonKey.equals(flag); } return getDefinition().getJsonProperties(flagFilter); })); } - // ------------------------------------------ - // - Start annotation driven code - CPD-OFF - - // ------------------------------------------ + @Override + public Field getField() { + return javaField; + } + /** + * Get the binding Java annotation. + * + * @return the binding Java annotation + */ + @NonNull + public BoundField getAnnotation() { + return annotation; + } + + @SuppressWarnings("null") @Override - public final DefinitionField getDefinition() { + public IModelInstanceCollectionInfo getCollectionInfo() { + return collectionInfo.get(); + } + + @Override + public DefinitionField getDefinition() { return definition; } @@ -142,8 +211,53 @@ public Map getJsonProperties() { return ObjectUtils.notNull(jsonProperties.get()); } + // ------------------------------------------ + // - Start annotation driven code - CPD-OFF - + // ------------------------------------------ + + @Override + public IGroupAs getGroupAs() { + return groupAs; + } + + @Override + public String getFormalName() { + return ModelUtil.resolveNoneOrValue(getAnnotation().formalName()); + } + + @Override + public MarkupLine getDescription() { + return ModelUtil.resolveToMarkupLine(getAnnotation().description()); + } + @Override public String getUseName() { return ModelUtil.resolveNoneOrValue(getAnnotation().useName()); } + + @Override + public Integer getUseIndex() { + int value = getAnnotation().useIndex(); + return value == Integer.MIN_VALUE ? null : value; + } + + @Override + public boolean isInXmlWrapped() { + return getAnnotation().inXmlWrapped(); + } + + @Override + public int getMinOccurs() { + return getAnnotation().minOccurs(); + } + + @Override + public int getMaxOccurs() { + return getAnnotation().maxOccurs(); + } + + @Override + public MarkupMultiline getRemarks() { + return ModelUtil.resolveToMarkupMultiline(getAnnotation().remarks()); + } } diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/InstanceModelFieldScalar.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/InstanceModelFieldScalar.java index 214447623..ee2cc7b8f 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/InstanceModelFieldScalar.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/InstanceModelFieldScalar.java @@ -27,16 +27,24 @@ package gov.nist.secauto.metaschema.databind.model.impl; import gov.nist.secauto.metaschema.core.datatype.IDataTypeAdapter; -import gov.nist.secauto.metaschema.core.model.IContainerFlagSupport; +import gov.nist.secauto.metaschema.core.datatype.markup.MarkupLine; +import gov.nist.secauto.metaschema.core.datatype.markup.MarkupMultiline; +import gov.nist.secauto.metaschema.core.model.AbstractInlineFieldDefinition; import gov.nist.secauto.metaschema.core.model.constraint.ISource; import gov.nist.secauto.metaschema.core.model.constraint.IValueConstrained; import gov.nist.secauto.metaschema.core.model.constraint.ValueConstraintSet; import gov.nist.secauto.metaschema.core.util.ObjectUtils; +import gov.nist.secauto.metaschema.databind.IBindingContext; import gov.nist.secauto.metaschema.databind.model.IBoundDefinitionModelAssembly; +import gov.nist.secauto.metaschema.databind.model.IBoundDefinitionModelField; import gov.nist.secauto.metaschema.databind.model.IBoundInstanceFlag; import gov.nist.secauto.metaschema.databind.model.IBoundInstanceModelFieldScalar; +import gov.nist.secauto.metaschema.databind.model.IGroupAs; +import gov.nist.secauto.metaschema.databind.model.annotations.BoundField; +import gov.nist.secauto.metaschema.databind.model.annotations.GroupAs; import gov.nist.secauto.metaschema.databind.model.annotations.ModelUtil; import gov.nist.secauto.metaschema.databind.model.annotations.ValueConstraints; +import gov.nist.secauto.metaschema.databind.model.info.IModelInstanceCollectionInfo; import java.lang.reflect.Field; import java.util.Optional; @@ -45,10 +53,22 @@ import edu.umd.cs.findbugs.annotations.Nullable; import nl.talsmasoftware.lazy4j.Lazy; -public class InstanceModelFieldScalar - extends AbstractBoundInstanceField - implements IBoundInstanceModelFieldScalar, - IFeatureBoundContainerFlag { +public final class InstanceModelFieldScalar + extends AbstractInlineFieldDefinition< + IBoundDefinitionModelAssembly, + IBoundDefinitionModelField, + IBoundInstanceModelFieldScalar, + IBoundDefinitionModelAssembly, + IBoundInstanceFlag> + implements IBoundInstanceModelFieldScalar, IFeatureInstanceModelGroupAs { + @NonNull + private final Field javaField; + @NonNull + private final BoundField annotation; + @NonNull + private final Lazy collectionInfo; + @NonNull + private final IGroupAs groupAs; @NonNull private final IDataTypeAdapter javaTypeAdapter; @Nullable @@ -61,39 +81,93 @@ public class InstanceModelFieldScalar * * @param javaField * the Java field bound to this instance + * @param annotation + * the field binding annotation + * @param groupAs + * the grouping info for the model instance * @param containingDefinition * the definition containing this instance + * @return the instance */ - public InstanceModelFieldScalar( + @NonNull + public static InstanceModelFieldScalar newInstance( + @NonNull Field javaField, + @NonNull IBoundDefinitionModelAssembly containingDefinition) { + BoundField annotation = ModelUtil.getAnnotation(javaField, BoundField.class); + IGroupAs groupAs = ModelUtil.groupAs( + annotation.groupAs(), + containingDefinition.getContainingModule()); + + if (annotation.maxOccurs() == -1 || annotation.maxOccurs() > 1) { + if (IGroupAs.SINGLETON_GROUP_AS.equals(groupAs)) { + throw new IllegalStateException(String.format("Field '%s' on class '%s' is missing the '%s' annotation.", + javaField.getName(), + javaField.getDeclaringClass().getName(), + GroupAs.class.getName())); // NOPMD false positive + } + } else if (!IGroupAs.SINGLETON_GROUP_AS.equals(groupAs)) { + // max is 1 and a groupAs is set + throw new IllegalStateException( + String.format( + "Field '%s' on class '%s' has the '%s' annotation, but maxOccurs=1. A groupAs must not be specfied.", + javaField.getName(), + javaField.getDeclaringClass().getName(), + GroupAs.class.getName())); // NOPMD false positive + } + + return new InstanceModelFieldScalar( + javaField, + annotation, + groupAs, + containingDefinition); + } + + private InstanceModelFieldScalar( @NonNull Field javaField, + @NonNull BoundField annotation, + @NonNull IGroupAs groupAs, @NonNull IBoundDefinitionModelAssembly containingDefinition) { - super(javaField, containingDefinition); + super(containingDefinition); + this.javaField = javaField; + this.annotation = annotation; + this.collectionInfo = ObjectUtils.notNull(Lazy.lazy(() -> IModelInstanceCollectionInfo.of(this))); + this.groupAs = groupAs; this.javaTypeAdapter = ModelUtil.getDataTypeAdapter( - getAnnotation().typeAdapter(), + annotation.typeAdapter(), containingDefinition.getBindingContext()); - this.defaultValue = ModelUtil.resolveDefaultValue(getAnnotation().defaultValue(), this.javaTypeAdapter); - + this.defaultValue = ModelUtil.resolveDefaultValue(annotation.defaultValue(), this.javaTypeAdapter); this.constraints = ObjectUtils.notNull(Lazy.lazy(() -> { IValueConstrained retval = new ValueConstraintSet(); - ValueConstraints valueAnnotation = getAnnotation().valueConstraints(); + ValueConstraints valueAnnotation = annotation.valueConstraints(); ConstraintSupport.parse(valueAnnotation, ISource.modelSource(), retval); return retval; })); } @Override - public IBoundInstanceFlag getJsonKeyFlagInstance() { - // no flags - return null; + public IBindingContext getBindingContext() { + return getContainingDefinition().getBindingContext(); } - // ------------------------------------------ - // - Start annotation driven code - CPD-OFF - - // ------------------------------------------ + @Override + public Field getField() { + return javaField; + } + + /** + * Get the binding Java annotation. + * + * @return the binding Java annotation + */ + @NonNull + public BoundField getAnnotation() { + return annotation; + } + @SuppressWarnings("null") @Override - public IContainerFlagSupport getFlagContainer() { - return IContainerFlagSupport.empty(); + public IModelInstanceCollectionInfo getCollectionInfo() { + return collectionInfo.get(); } @SuppressWarnings("null") @@ -108,6 +182,56 @@ public IDataTypeAdapter getJavaTypeAdapter() { return javaTypeAdapter; } + @Override + public Object getDefaultValue() { + return defaultValue; + } + + @Override + public IGroupAs getGroupAs() { + return groupAs; + } + + // ------------------------------------------ + // - Start annotation driven code - CPD-OFF - + // ------------------------------------------ + + @Override + public String getFormalName() { + return ModelUtil.resolveNoneOrValue(getAnnotation().formalName()); + } + + @Override + public MarkupLine getDescription() { + return ModelUtil.resolveToMarkupLine(getAnnotation().description()); + } + + @Override + public Integer getUseIndex() { + int value = getAnnotation().useIndex(); + return value == Integer.MIN_VALUE ? null : value; + } + + @Override + public boolean isInXmlWrapped() { + return getAnnotation().inXmlWrapped(); + } + + @Override + public int getMinOccurs() { + return getAnnotation().minOccurs(); + } + + @Override + public int getMaxOccurs() { + return getAnnotation().maxOccurs(); + } + + @Override + public MarkupMultiline getRemarks() { + return ModelUtil.resolveToMarkupMultiline(getAnnotation().remarks()); + } + @Override public String getName() { // the name is stored as a usename to remain consistent with non-scalar valued @@ -116,11 +240,6 @@ public String getName() { Optional.ofNullable(ModelUtil.resolveNoneOrValue(getAnnotation().useName())).orElse(getField().getName())); } - @Override - public Object getDefaultValue() { - return defaultValue; - } - // ---------------------------------------- // - End annotation driven code - CPD-OFF - // ---------------------------------------- diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/InstanceModelGroupedAssembly.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/InstanceModelGroupedAssembly.java index dfdc2e2ad..e14447684 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/InstanceModelGroupedAssembly.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/InstanceModelGroupedAssembly.java @@ -28,10 +28,9 @@ import gov.nist.secauto.metaschema.core.datatype.markup.MarkupLine; import gov.nist.secauto.metaschema.core.datatype.markup.MarkupMultiline; -import gov.nist.secauto.metaschema.core.model.IFeatureDefinitionReferenceInstance; +import gov.nist.secauto.metaschema.core.model.AbstractAssemblyInstance; import gov.nist.secauto.metaschema.core.util.ObjectUtils; import gov.nist.secauto.metaschema.databind.model.IBoundDefinitionModelAssembly; -import gov.nist.secauto.metaschema.databind.model.IBoundInstanceFlag; import gov.nist.secauto.metaschema.databind.model.IBoundInstanceModelChoiceGroup; import gov.nist.secauto.metaschema.databind.model.IBoundInstanceModelGroupedAssembly; import gov.nist.secauto.metaschema.databind.model.IBoundProperty; @@ -39,7 +38,6 @@ import gov.nist.secauto.metaschema.databind.model.annotations.ModelUtil; import java.util.Map; -import java.util.function.Predicate; import edu.umd.cs.findbugs.annotations.NonNull; import nl.talsmasoftware.lazy4j.Lazy; @@ -49,9 +47,15 @@ * instance. */ public class InstanceModelGroupedAssembly - extends AbstractBoundInstanceModelGroupedNamed - implements IBoundInstanceModelGroupedAssembly, - IFeatureDefinitionReferenceInstance { + extends AbstractAssemblyInstance< + IBoundInstanceModelChoiceGroup, + IBoundDefinitionModelAssembly, + IBoundInstanceModelGroupedAssembly, + IBoundDefinitionModelAssembly> + // extends AbstractBoundInstanceModelGroupedNamed + implements IBoundInstanceModelGroupedAssembly { + @NonNull + private final BoundGroupedAssembly annotation; @NonNull private final IBoundDefinitionModelAssembly definition; @NonNull @@ -72,19 +76,29 @@ public InstanceModelGroupedAssembly( @NonNull BoundGroupedAssembly annotation, @NonNull IBoundDefinitionModelAssembly definition, @NonNull IBoundInstanceModelChoiceGroup container) { - super(annotation, container); + super(container); + this.annotation = annotation; this.definition = definition; - this.jsonProperties = ObjectUtils.notNull(Lazy.lazy(() -> { - IBoundInstanceFlag jsonKey = getJsonKey(); - Predicate flagFilter = jsonKey == null ? null : (flag) -> !jsonKey.equals(flag); - return getDefinition().getJsonProperties(flagFilter); - })); + // IBoundInstanceFlag jsonKey = getEffectiveJsonKey(); + // Predicate flagFilter = jsonKey == null ? null : (flag) -> + // !jsonKey.equals(flag); + // return getDefinition().getJsonProperties(flagFilter); + this.jsonProperties = ObjectUtils.notNull(Lazy.lazy(() -> getDefinition().getJsonProperties(null))); + } + + private BoundGroupedAssembly getAnnotation() { + return annotation; } // ------------------------------------------ // - Start annotation driven code - CPD-OFF - // ------------------------------------------ + @Override + public Class getBoundClass() { + return getAnnotation().binding(); + } + @Override public Map getJsonProperties() { return ObjectUtils.notNull(jsonProperties.get()); @@ -124,16 +138,4 @@ public String getUseName() { public Integer getUseIndex() { return ModelUtil.resolveNullOrInteger(getAnnotation().useIndex()); } - - @Override - public String getXmlNamespace() { - return ModelUtil.resolveOptionalNamespace( - getAnnotation().namespace(), - () -> getContainingDefinition().getXmlNamespace()); - } - - @Override - public Class getBoundClass() { - return getAnnotation().binding(); - } } diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/InstanceModelGroupedFieldComplex.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/InstanceModelGroupedFieldComplex.java index c58ee02f8..6f8c6dab8 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/InstanceModelGroupedFieldComplex.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/InstanceModelGroupedFieldComplex.java @@ -28,8 +28,9 @@ import gov.nist.secauto.metaschema.core.datatype.markup.MarkupLine; import gov.nist.secauto.metaschema.core.datatype.markup.MarkupMultiline; -import gov.nist.secauto.metaschema.core.model.IFeatureDefinitionReferenceInstance; +import gov.nist.secauto.metaschema.core.model.AbstractFieldInstance; import gov.nist.secauto.metaschema.core.util.ObjectUtils; +import gov.nist.secauto.metaschema.databind.model.IBoundDefinitionModelAssembly; import gov.nist.secauto.metaschema.databind.model.IBoundDefinitionModelFieldComplex; import gov.nist.secauto.metaschema.databind.model.IBoundInstanceFlag; import gov.nist.secauto.metaschema.databind.model.IBoundInstanceModelChoiceGroup; @@ -45,9 +46,16 @@ import nl.talsmasoftware.lazy4j.Lazy; public class InstanceModelGroupedFieldComplex - extends AbstractBoundInstanceModelGroupedNamed - implements IBoundInstanceModelGroupedField, - IFeatureDefinitionReferenceInstance { + extends AbstractFieldInstance< + IBoundInstanceModelChoiceGroup, + IBoundDefinitionModelFieldComplex, + IBoundInstanceModelGroupedField, + IBoundDefinitionModelAssembly> + + // extends AbstractBoundInstanceModelGroupedNamed + implements IBoundInstanceModelGroupedField { + @NonNull + private final BoundGroupedField annotation; @NonNull private final DefinitionField definition; @NonNull @@ -57,28 +65,38 @@ public InstanceModelGroupedFieldComplex( @NonNull BoundGroupedField annotation, @NonNull DefinitionField definition, @NonNull IBoundInstanceModelChoiceGroup container) { - super(annotation, container); + super(container); + this.annotation = annotation; this.definition = definition; this.jsonProperties = ObjectUtils.notNull(Lazy.lazy(() -> { Predicate flagFilter = null; - IBoundInstanceFlag jsonKey = getJsonKey(); + IBoundInstanceFlag jsonKey = getEffectiveJsonKey(); if (jsonKey != null) { - flagFilter = (flag) -> !jsonKey.equals(flag); + flagFilter = flag -> !jsonKey.equals(flag); } IBoundInstanceFlag jsonValueKey = getDefinition().getJsonValueKeyFlagInstance(); if (jsonValueKey != null) { - Predicate jsonValueKeyFilter = (flag) -> !flag.equals(jsonValueKey); + Predicate jsonValueKeyFilter = flag -> !flag.equals(jsonValueKey); flagFilter = flagFilter == null ? jsonValueKeyFilter : flagFilter.and(jsonValueKeyFilter); } return getDefinition().getJsonProperties(flagFilter); })); } + private BoundGroupedField getAnnotation() { + return annotation; + } + // ------------------------------------------ // - Start annotation driven code - CPD-OFF - // ------------------------------------------ + @Override + public Class getBoundClass() { + return getAnnotation().binding(); + } + @Override public Map getJsonProperties() { return ObjectUtils.notNull(jsonProperties.get()); @@ -119,18 +137,6 @@ public Integer getUseIndex() { return ModelUtil.resolveNullOrInteger(getAnnotation().useIndex()); } - @Override - public String getXmlNamespace() { - return ModelUtil.resolveOptionalNamespace( - getAnnotation().namespace(), - () -> getContainingDefinition().getXmlNamespace()); - } - - @Override - public Class getBoundClass() { - return getAnnotation().binding(); - } - // ---------------------------------------- // - End annotation driven code - CPD-OFF - // ---------------------------------------- diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/package-info.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/package-info.java index 1316bbc9e..9c4d1cf32 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/package-info.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/impl/package-info.java @@ -26,8 +26,8 @@ /** * Provides an implementation of the Module model - * ({@link gov.nist.secauto.metaschema.core.model}) representing module - * constructs as bound Java class annotations + * ({@link gov.nist.secauto.metaschema.core.model}) represented by bound Java + * class annotations * ({@link gov.nist.secauto.metaschema.databind.model.annotations}). */ diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/info/AbstractModelInstanceReadHandler.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/info/AbstractModelInstanceReadHandler.java index 21fc109c0..36b7e7b95 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/info/AbstractModelInstanceReadHandler.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/info/AbstractModelInstanceReadHandler.java @@ -26,10 +26,11 @@ package gov.nist.secauto.metaschema.databind.model.info; +import gov.nist.secauto.metaschema.databind.model.IBoundInstanceFlag; import gov.nist.secauto.metaschema.databind.model.IBoundInstanceModel; +import gov.nist.secauto.metaschema.databind.model.IBoundInstanceModelNamed; import edu.umd.cs.findbugs.annotations.NonNull; -import edu.umd.cs.findbugs.annotations.Nullable; public abstract class AbstractModelInstanceReadHandler implements IModelInstanceReadHandler { @NonNull @@ -65,6 +66,8 @@ public IModelInstanceCollectionInfo getCollectionInfo() { } /** + * Get the object onto which parsed data will be stored. + * * @return the parentObject */ @NonNull @@ -73,8 +76,15 @@ public Object getParentObject() { } @Override - @Nullable public String getJsonKeyFlagName() { - return getCollectionInfo().getInstance().getJsonKeyFlagName(); + IBoundInstanceModel instance = getInstance(); + String retval = null; + if (instance instanceof IBoundInstanceModelNamed) { + IBoundInstanceFlag jsonKey = ((IBoundInstanceModelNamed) instance).getEffectiveJsonKey(); + if (jsonKey != null) { + retval = jsonKey.getEffectiveName(); + } + } + return retval; } } diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/info/IFeatureComplexItemValueHandler.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/info/IFeatureComplexItemValueHandler.java index 58d6f9293..8e317dd8e 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/info/IFeatureComplexItemValueHandler.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/info/IFeatureComplexItemValueHandler.java @@ -56,6 +56,12 @@ public interface IFeatureComplexItemValueHandler extends IItemValueHandler { // @Nullable // String getJsonKeyFlagName(); + /** + * Get the mapping of JSON property names to property bindings. + * + * @return the mapping + */ + // REFACTOR: move JSON-specific methods to a binding cache implementation @NonNull Map getJsonProperties(); diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/info/IFeatureScalarItemValueHandler.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/info/IFeatureScalarItemValueHandler.java index d62b4763d..e1bd55c39 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/info/IFeatureScalarItemValueHandler.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/info/IFeatureScalarItemValueHandler.java @@ -28,17 +28,13 @@ import gov.nist.secauto.metaschema.core.datatype.IDataTypeAdapter; import gov.nist.secauto.metaschema.databind.io.BindingException; +import gov.nist.secauto.metaschema.databind.model.IValuedMutable; import edu.umd.cs.findbugs.annotations.NonNull; import edu.umd.cs.findbugs.annotations.Nullable; public interface IFeatureScalarItemValueHandler - extends IItemValueHandler { - - @Nullable - Object getValue(@NonNull Object parent); - - void setValue(@NonNull Object parent, Object value); + extends IItemValueHandler, IValuedMutable { default void setValue(@NonNull Object parent, @NonNull String text) { Object item = getValueFromString(text); diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/BindingConstraintLoader.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/BindingConstraintLoader.java index ee3d1899f..1fd75a523 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/BindingConstraintLoader.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/BindingConstraintLoader.java @@ -57,6 +57,7 @@ import gov.nist.secauto.metaschema.databind.io.IBoundLoader; import gov.nist.secauto.metaschema.databind.model.metaschema.binding.MetapathContext; import gov.nist.secauto.metaschema.databind.model.metaschema.binding.MetaschemaMetaConstraints; +import gov.nist.secauto.metaschema.databind.model.metaschema.binding.MetaschemaMetapath; import gov.nist.secauto.metaschema.databind.model.metaschema.binding.MetaschemaModuleConstraints; import gov.nist.secauto.metaschema.databind.model.metaschema.impl.ConstraintBindingSupport; @@ -250,17 +251,15 @@ private Context parseContext( List metapaths; if (parent == null) { metapaths = CollectionUtil.listOrEmpty(contextObj.getMetapaths()).stream() - .map(metapath -> metapath.getTarget()) + .map(MetaschemaMetapath::getTarget) .collect(Collectors.toList()); } else { List parentMetapaths = parent.getMetapaths().stream() .collect(Collectors.toList()); metapaths = CollectionUtil.listOrEmpty(contextObj.getMetapaths()).stream() - .map(metapath -> metapath.getTarget()) - .flatMap(childPath -> { - return parentMetapaths.stream() - .map(parentPath -> parentPath + '/' + childPath); - }) + .map(MetaschemaMetapath::getTarget) + .flatMap(childPath -> parentMetapaths.stream() + .map(parentPath -> parentPath + '/' + childPath)) .collect(Collectors.toList()); } @@ -281,8 +280,6 @@ private static class Context { @NonNull private final List metapaths; @NonNull - private final IModelConstrained constraints; - @NonNull private final List childContexts = new LinkedList<>(); @NonNull private final Lazy> targetedConstraints; @@ -291,7 +288,6 @@ public Context( @NonNull List metapaths, @NonNull IModelConstrained constraints) { this.metapaths = metapaths; - this.constraints = constraints; this.targetedConstraints = ObjectUtils.notNull(Lazy.lazy(() -> { Stream paths = getMetapaths().stream() @@ -338,20 +334,18 @@ protected MetaTargetedContraints( * @param definition * the definition to apply the constraints to. */ - @SuppressWarnings("null") protected void applyTo(@NonNull IDefinition definition) { - getAllowedValuesConstraints().forEach(constraint -> definition.addConstraint(constraint)); - getMatchesConstraints().forEach(constraint -> definition.addConstraint(constraint)); - getIndexHasKeyConstraints().forEach(constraint -> definition.addConstraint(constraint)); - getExpectConstraints().forEach(constraint -> definition.addConstraint(constraint)); + getAllowedValuesConstraints().forEach(definition::addConstraint); + getMatchesConstraints().forEach(definition::addConstraint); + getIndexHasKeyConstraints().forEach(definition::addConstraint); + getExpectConstraints().forEach(definition::addConstraint); } - @SuppressWarnings("null") protected void applyTo(@NonNull IAssemblyDefinition definition) { applyTo((IDefinition) definition); - getIndexConstraints().forEach(constraint -> definition.addConstraint(constraint)); - getUniqueConstraints().forEach(constraint -> definition.addConstraint(constraint)); - getHasCardinalityConstraints().forEach(constraint -> definition.addConstraint(constraint)); + getIndexConstraints().forEach(definition::addConstraint); + getUniqueConstraints().forEach(definition::addConstraint); + getHasCardinalityConstraints().forEach(definition::addConstraint); } @Override diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/BindingModuleLoader.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/BindingModuleLoader.java index 7b45aa481..675aecaf5 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/BindingModuleLoader.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/BindingModuleLoader.java @@ -29,6 +29,7 @@ import gov.nist.secauto.metaschema.core.configuration.IConfiguration; import gov.nist.secauto.metaschema.core.configuration.IMutableConfiguration; import gov.nist.secauto.metaschema.core.model.AbstractModuleLoader; +import gov.nist.secauto.metaschema.core.model.IMetaschemaModule; import gov.nist.secauto.metaschema.core.model.IModuleLoader; import gov.nist.secauto.metaschema.core.model.MetaschemaException; import gov.nist.secauto.metaschema.core.util.CollectionUtil; @@ -38,6 +39,7 @@ import gov.nist.secauto.metaschema.databind.io.IBoundLoader; import gov.nist.secauto.metaschema.databind.model.IBoundDefinitionModelAssembly; import gov.nist.secauto.metaschema.databind.model.metaschema.binding.METASCHEMA; +import gov.nist.secauto.metaschema.databind.model.metaschema.binding.METASCHEMA.Import; import gov.nist.secauto.metaschema.databind.model.metaschema.impl.BindingModule; import java.io.IOException; @@ -49,7 +51,7 @@ import edu.umd.cs.findbugs.annotations.NonNull; public class BindingModuleLoader - extends AbstractModuleLoader + extends AbstractModuleLoader implements IMutableConfiguration> { @NonNull private final IBoundLoader loader; @@ -78,7 +80,10 @@ public BindingModuleLoader(@NonNull List mod } @Override - protected IBindingModule newModule(URI resource, METASCHEMA binding, List importedModules) + protected IMetaschemaModule newModule( + URI resource, + METASCHEMA binding, + List importedModules) throws MetaschemaException { return new BindingModule( resource, @@ -92,7 +97,7 @@ protected IBindingModule newModule(URI resource, METASCHEMA binding, List getImports(METASCHEMA binding) { return ObjectUtils.notNull(binding.getImports().stream() - .map(imported -> imported.getHref()) + .map(Import::getHref) .collect(Collectors.toUnmodifiableList())); } diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/IBindingContainerModelAbsolute.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/IBindingContainerModelAbsolute.java deleted file mode 100644 index d77d7cbfe..000000000 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/IBindingContainerModelAbsolute.java +++ /dev/null @@ -1,60 +0,0 @@ -/* - * Portions of this software was developed by employees of the National Institute - * of Standards and Technology (NIST), an agency of the Federal Government and is - * being made available as a public service. Pursuant to title 17 United States - * Code Section 105, works of NIST employees are not subject to copyright - * protection in the United States. This software may be subject to foreign - * copyright. Permission in the United States and in foreign countries, to the - * extent that NIST may hold copyright, to use, copy, modify, create derivative - * works, and distribute this software and its documentation without fee is hereby - * granted on a non-exclusive basis, provided that this notice and disclaimer - * of warranty appears in all copies. - * - * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER - * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY - * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM - * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE - * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT - * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, - * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, - * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, - * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR - * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT - * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. - */ - -package gov.nist.secauto.metaschema.databind.model.metaschema; - -import java.util.Collection; - -import javax.xml.namespace.QName; - -import gov.nist.secauto.metaschema.core.model.IContainerModelAbsolute; - -public interface IBindingContainerModelAbsolute extends IContainerModelAbsolute { - - @Override - IBindingDefinitionAssembly getOwningDefinition(); - - @Override - Collection getModelInstances(); - - @Override - Collection getNamedModelInstances(); - - @Override - IBindingInstanceModelNamedAbsolute getNamedModelInstanceByName(QName name); - - @Override - Collection getFieldInstances(); - - @Override - IBindingInstanceModelFieldAbsolute getFieldInstanceByName(QName name); - - @Override - Collection getAssemblyInstances(); - - @Override - IBindingInstanceModelAssemblyAbsolute getAssemblyInstanceByName(QName name); -} diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/IBindingContainerModelAssembly.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/IBindingContainerModelAssembly.java deleted file mode 100644 index 47f9d2144..000000000 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/IBindingContainerModelAssembly.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Portions of this software was developed by employees of the National Institute - * of Standards and Technology (NIST), an agency of the Federal Government and is - * being made available as a public service. Pursuant to title 17 United States - * Code Section 105, works of NIST employees are not subject to copyright - * protection in the United States. This software may be subject to foreign - * copyright. Permission in the United States and in foreign countries, to the - * extent that NIST may hold copyright, to use, copy, modify, create derivative - * works, and distribute this software and its documentation without fee is hereby - * granted on a non-exclusive basis, provided that this notice and disclaimer - * of warranty appears in all copies. - * - * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER - * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY - * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM - * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE - * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT - * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, - * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, - * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, - * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR - * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT - * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. - */ - -package gov.nist.secauto.metaschema.databind.model.metaschema; - -import gov.nist.secauto.metaschema.core.model.IContainerModelAssembly; - -import java.util.List; -import java.util.Map; - -public interface IBindingContainerModelAssembly extends IContainerModelAssembly, IBindingContainerModelAbsolute { - - @Override - List getChoiceInstances(); - - @Override - IInstanceModelChoiceGroupBinding getChoiceGroupInstanceByName(String name); - - @Override - Map getChoiceGroupInstances(); -} diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/IBindingContainerModelGrouped.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/IBindingContainerModelGrouped.java deleted file mode 100644 index 1641c4e29..000000000 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/IBindingContainerModelGrouped.java +++ /dev/null @@ -1,62 +0,0 @@ -/* - * Portions of this software was developed by employees of the National Institute - * of Standards and Technology (NIST), an agency of the Federal Government and is - * being made available as a public service. Pursuant to title 17 United States - * Code Section 105, works of NIST employees are not subject to copyright - * protection in the United States. This software may be subject to foreign - * copyright. Permission in the United States and in foreign countries, to the - * extent that NIST may hold copyright, to use, copy, modify, create derivative - * works, and distribute this software and its documentation without fee is hereby - * granted on a non-exclusive basis, provided that this notice and disclaimer - * of warranty appears in all copies. - * - * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER - * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY - * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM - * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE - * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT - * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, - * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, - * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, - * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR - * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT - * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. - */ - -package gov.nist.secauto.metaschema.databind.model.metaschema; - -import java.util.Collection; - -import javax.xml.namespace.QName; - -import gov.nist.secauto.metaschema.core.model.IContainerModelGrouped; - -public interface IBindingContainerModelGrouped extends IContainerModelGrouped { - - @Override - IBindingDefinitionAssembly getOwningDefinition(); - - @Override - default Collection getModelInstances() { - return getNamedModelInstances(); - } - - @Override - Collection getNamedModelInstances(); - - @Override - IBindingInstanceModelNamedGrouped getNamedModelInstanceByName(QName name); - - @Override - Collection getFieldInstances(); - - @Override - IBindingInstanceModelFieldGrouped getFieldInstanceByName(QName name); - - @Override - Collection getAssemblyInstances(); - - @Override - IBindingInstanceModelAssemblyGrouped getAssemblyInstanceByName(QName name); -} diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/IBindingDefinition.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/IBindingDefinition.java deleted file mode 100644 index 1265b71e3..000000000 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/IBindingDefinition.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Portions of this software was developed by employees of the National Institute - * of Standards and Technology (NIST), an agency of the Federal Government and is - * being made available as a public service. Pursuant to title 17 United States - * Code Section 105, works of NIST employees are not subject to copyright - * protection in the United States. This software may be subject to foreign - * copyright. Permission in the United States and in foreign countries, to the - * extent that NIST may hold copyright, to use, copy, modify, create derivative - * works, and distribute this software and its documentation without fee is hereby - * granted on a non-exclusive basis, provided that this notice and disclaimer - * of warranty appears in all copies. - * - * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER - * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY - * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM - * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE - * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT - * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, - * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, - * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, - * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR - * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT - * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. - */ - -package gov.nist.secauto.metaschema.databind.model.metaschema; - -import gov.nist.secauto.metaschema.core.model.IDefinition; - -public interface IBindingDefinition extends IBindingModelElement, IDefinition { - @Override - IBindingModule getContainingModule(); -} diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/IBindingDefinitionAssembly.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/IBindingDefinitionAssembly.java deleted file mode 100644 index c2f90fe47..000000000 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/IBindingDefinitionAssembly.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Portions of this software was developed by employees of the National Institute - * of Standards and Technology (NIST), an agency of the Federal Government and is - * being made available as a public service. Pursuant to title 17 United States - * Code Section 105, works of NIST employees are not subject to copyright - * protection in the United States. This software may be subject to foreign - * copyright. Permission in the United States and in foreign countries, to the - * extent that NIST may hold copyright, to use, copy, modify, create derivative - * works, and distribute this software and its documentation without fee is hereby - * granted on a non-exclusive basis, provided that this notice and disclaimer - * of warranty appears in all copies. - * - * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER - * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY - * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM - * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE - * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT - * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, - * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, - * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, - * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR - * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT - * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. - */ - -package gov.nist.secauto.metaschema.databind.model.metaschema; - -import gov.nist.secauto.metaschema.core.model.IAssemblyDefinition; - -public interface IBindingDefinitionAssembly - extends IAssemblyDefinition, IBindingDefinitionModel, IBindingContainerModelAssembly { - - @Override - default IBindingDefinitionAssembly getOwningDefinition() { - return this; - } -} diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/IBindingDefinitionModelField.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/IBindingDefinitionModelField.java deleted file mode 100644 index cc9ce7433..000000000 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/IBindingDefinitionModelField.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Portions of this software was developed by employees of the National Institute - * of Standards and Technology (NIST), an agency of the Federal Government and is - * being made available as a public service. Pursuant to title 17 United States - * Code Section 105, works of NIST employees are not subject to copyright - * protection in the United States. This software may be subject to foreign - * copyright. Permission in the United States and in foreign countries, to the - * extent that NIST may hold copyright, to use, copy, modify, create derivative - * works, and distribute this software and its documentation without fee is hereby - * granted on a non-exclusive basis, provided that this notice and disclaimer - * of warranty appears in all copies. - * - * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER - * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY - * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM - * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE - * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT - * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, - * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, - * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, - * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR - * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT - * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. - */ - -package gov.nist.secauto.metaschema.databind.model.metaschema; - -import gov.nist.secauto.metaschema.core.model.IFieldDefinition; - -public interface IBindingDefinitionModelField extends IFieldDefinition, IBindingDefinitionModel { - // no additional methods -} diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/IBindingInstanceFlag.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/IBindingInstanceFlag.java deleted file mode 100644 index 6c127ea7b..000000000 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/IBindingInstanceFlag.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Portions of this software was developed by employees of the National Institute - * of Standards and Technology (NIST), an agency of the Federal Government and is - * being made available as a public service. Pursuant to title 17 United States - * Code Section 105, works of NIST employees are not subject to copyright - * protection in the United States. This software may be subject to foreign - * copyright. Permission in the United States and in foreign countries, to the - * extent that NIST may hold copyright, to use, copy, modify, create derivative - * works, and distribute this software and its documentation without fee is hereby - * granted on a non-exclusive basis, provided that this notice and disclaimer - * of warranty appears in all copies. - * - * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER - * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY - * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM - * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE - * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT - * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, - * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, - * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, - * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR - * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT - * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. - */ - -package gov.nist.secauto.metaschema.databind.model.metaschema; - -import gov.nist.secauto.metaschema.core.model.IFlagInstance; - -public interface IBindingInstanceFlag extends IFlagInstance, IBindingInstance { - // no additional methods -} diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/IBindingInstanceModel.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/IBindingInstanceModel.java deleted file mode 100644 index 7f300b290..000000000 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/IBindingInstanceModel.java +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Portions of this software was developed by employees of the National Institute - * of Standards and Technology (NIST), an agency of the Federal Government and is - * being made available as a public service. Pursuant to title 17 United States - * Code Section 105, works of NIST employees are not subject to copyright - * protection in the United States. This software may be subject to foreign - * copyright. Permission in the United States and in foreign countries, to the - * extent that NIST may hold copyright, to use, copy, modify, create derivative - * works, and distribute this software and its documentation without fee is hereby - * granted on a non-exclusive basis, provided that this notice and disclaimer - * of warranty appears in all copies. - * - * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER - * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY - * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM - * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE - * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT - * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, - * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, - * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, - * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR - * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT - * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. - */ - -package gov.nist.secauto.metaschema.databind.model.metaschema; - -import gov.nist.secauto.metaschema.core.model.IModelInstance; - -public interface IBindingInstanceModel extends IModelInstance, IBindingInstance { - - @Override - IBindingDefinitionAssembly getContainingDefinition(); -} diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/IBindingInstanceModelAssembly.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/IBindingInstanceModelAssembly.java deleted file mode 100644 index d117a0af3..000000000 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/IBindingInstanceModelAssembly.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Portions of this software was developed by employees of the National Institute - * of Standards and Technology (NIST), an agency of the Federal Government and is - * being made available as a public service. Pursuant to title 17 United States - * Code Section 105, works of NIST employees are not subject to copyright - * protection in the United States. This software may be subject to foreign - * copyright. Permission in the United States and in foreign countries, to the - * extent that NIST may hold copyright, to use, copy, modify, create derivative - * works, and distribute this software and its documentation without fee is hereby - * granted on a non-exclusive basis, provided that this notice and disclaimer - * of warranty appears in all copies. - * - * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER - * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY - * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM - * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE - * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT - * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, - * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, - * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, - * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR - * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT - * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. - */ - -package gov.nist.secauto.metaschema.databind.model.metaschema; - -import gov.nist.secauto.metaschema.core.model.IAssemblyInstance; - -public interface IBindingInstanceModelAssembly - extends IAssemblyInstance, IBindingInstanceModelNamed { - // no additional methods -} diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/IBindingInstanceModelAssemblyAbsolute.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/IBindingInstanceModelAssemblyAbsolute.java deleted file mode 100644 index 0beeec970..000000000 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/IBindingInstanceModelAssemblyAbsolute.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Portions of this software was developed by employees of the National Institute - * of Standards and Technology (NIST), an agency of the Federal Government and is - * being made available as a public service. Pursuant to title 17 United States - * Code Section 105, works of NIST employees are not subject to copyright - * protection in the United States. This software may be subject to foreign - * copyright. Permission in the United States and in foreign countries, to the - * extent that NIST may hold copyright, to use, copy, modify, create derivative - * works, and distribute this software and its documentation without fee is hereby - * granted on a non-exclusive basis, provided that this notice and disclaimer - * of warranty appears in all copies. - * - * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER - * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY - * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM - * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE - * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT - * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, - * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, - * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, - * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR - * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT - * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. - */ - -package gov.nist.secauto.metaschema.databind.model.metaschema; - -import gov.nist.secauto.metaschema.core.model.IAssemblyInstanceAbsolute; - -public interface IBindingInstanceModelAssemblyAbsolute - extends IBindingInstanceModelAssembly, IAssemblyInstanceAbsolute, IBindingInstanceModelNamedAbsolute { - // no additional methods -} diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/IBindingInstanceModelAssemblyGrouped.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/IBindingInstanceModelAssemblyGrouped.java deleted file mode 100644 index 76d3c8747..000000000 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/IBindingInstanceModelAssemblyGrouped.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Portions of this software was developed by employees of the National Institute - * of Standards and Technology (NIST), an agency of the Federal Government and is - * being made available as a public service. Pursuant to title 17 United States - * Code Section 105, works of NIST employees are not subject to copyright - * protection in the United States. This software may be subject to foreign - * copyright. Permission in the United States and in foreign countries, to the - * extent that NIST may hold copyright, to use, copy, modify, create derivative - * works, and distribute this software and its documentation without fee is hereby - * granted on a non-exclusive basis, provided that this notice and disclaimer - * of warranty appears in all copies. - * - * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER - * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY - * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM - * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE - * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT - * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, - * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, - * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, - * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR - * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT - * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. - */ - -package gov.nist.secauto.metaschema.databind.model.metaschema; - -import gov.nist.secauto.metaschema.core.model.IAssemblyInstanceGrouped; - -public interface IBindingInstanceModelAssemblyGrouped - extends IBindingInstanceModelAssembly, IAssemblyInstanceGrouped, IBindingInstanceModelNamedGrouped { - // no additional methods -} diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/IBindingInstanceModelField.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/IBindingInstanceModelField.java deleted file mode 100644 index 028c29aa0..000000000 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/IBindingInstanceModelField.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Portions of this software was developed by employees of the National Institute - * of Standards and Technology (NIST), an agency of the Federal Government and is - * being made available as a public service. Pursuant to title 17 United States - * Code Section 105, works of NIST employees are not subject to copyright - * protection in the United States. This software may be subject to foreign - * copyright. Permission in the United States and in foreign countries, to the - * extent that NIST may hold copyright, to use, copy, modify, create derivative - * works, and distribute this software and its documentation without fee is hereby - * granted on a non-exclusive basis, provided that this notice and disclaimer - * of warranty appears in all copies. - * - * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER - * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY - * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM - * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE - * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT - * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, - * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, - * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, - * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR - * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT - * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. - */ - -package gov.nist.secauto.metaschema.databind.model.metaschema; - -import gov.nist.secauto.metaschema.core.model.IFieldInstance; - -public interface IBindingInstanceModelField extends IFieldInstance, IBindingInstanceModelNamed { - // no additional methods -} diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/IBindingInstanceModelFieldAbsolute.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/IBindingInstanceModelFieldAbsolute.java deleted file mode 100644 index 9377a27c8..000000000 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/IBindingInstanceModelFieldAbsolute.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Portions of this software was developed by employees of the National Institute - * of Standards and Technology (NIST), an agency of the Federal Government and is - * being made available as a public service. Pursuant to title 17 United States - * Code Section 105, works of NIST employees are not subject to copyright - * protection in the United States. This software may be subject to foreign - * copyright. Permission in the United States and in foreign countries, to the - * extent that NIST may hold copyright, to use, copy, modify, create derivative - * works, and distribute this software and its documentation without fee is hereby - * granted on a non-exclusive basis, provided that this notice and disclaimer - * of warranty appears in all copies. - * - * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER - * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY - * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM - * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE - * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT - * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, - * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, - * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, - * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR - * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT - * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. - */ - -package gov.nist.secauto.metaschema.databind.model.metaschema; - -import gov.nist.secauto.metaschema.core.model.IFieldInstanceAbsolute; - -public interface IBindingInstanceModelFieldAbsolute - extends IBindingInstanceModelField, IFieldInstanceAbsolute, IBindingInstanceModelNamedAbsolute { - // no additional methods -} diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/IBindingInstanceModelFieldGrouped.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/IBindingInstanceModelFieldGrouped.java deleted file mode 100644 index b1885edd4..000000000 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/IBindingInstanceModelFieldGrouped.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Portions of this software was developed by employees of the National Institute - * of Standards and Technology (NIST), an agency of the Federal Government and is - * being made available as a public service. Pursuant to title 17 United States - * Code Section 105, works of NIST employees are not subject to copyright - * protection in the United States. This software may be subject to foreign - * copyright. Permission in the United States and in foreign countries, to the - * extent that NIST may hold copyright, to use, copy, modify, create derivative - * works, and distribute this software and its documentation without fee is hereby - * granted on a non-exclusive basis, provided that this notice and disclaimer - * of warranty appears in all copies. - * - * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER - * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY - * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM - * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE - * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT - * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, - * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, - * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, - * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR - * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT - * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. - */ - -package gov.nist.secauto.metaschema.databind.model.metaschema; - -import gov.nist.secauto.metaschema.core.model.IFieldInstanceGrouped; - -public interface IBindingInstanceModelFieldGrouped - extends IBindingInstanceModelField, IFieldInstanceGrouped, IBindingInstanceModelNamedGrouped { - // no additional methods -} diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/IBindingInstanceModelNamed.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/IBindingInstanceModelNamed.java deleted file mode 100644 index 0705527a7..000000000 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/IBindingInstanceModelNamed.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Portions of this software was developed by employees of the National Institute - * of Standards and Technology (NIST), an agency of the Federal Government and is - * being made available as a public service. Pursuant to title 17 United States - * Code Section 105, works of NIST employees are not subject to copyright - * protection in the United States. This software may be subject to foreign - * copyright. Permission in the United States and in foreign countries, to the - * extent that NIST may hold copyright, to use, copy, modify, create derivative - * works, and distribute this software and its documentation without fee is hereby - * granted on a non-exclusive basis, provided that this notice and disclaimer - * of warranty appears in all copies. - * - * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER - * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY - * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM - * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE - * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT - * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, - * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, - * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, - * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR - * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT - * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. - */ - -package gov.nist.secauto.metaschema.databind.model.metaschema; - -import gov.nist.secauto.metaschema.core.model.INamedModelInstance; - -public interface IBindingInstanceModelNamed extends IBindingInstanceModel, INamedModelInstance { - // no additional methods -} diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/IBindingInstanceModelNamedAbsolute.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/IBindingInstanceModelNamedAbsolute.java deleted file mode 100644 index eef878a43..000000000 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/IBindingInstanceModelNamedAbsolute.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Portions of this software was developed by employees of the National Institute - * of Standards and Technology (NIST), an agency of the Federal Government and is - * being made available as a public service. Pursuant to title 17 United States - * Code Section 105, works of NIST employees are not subject to copyright - * protection in the United States. This software may be subject to foreign - * copyright. Permission in the United States and in foreign countries, to the - * extent that NIST may hold copyright, to use, copy, modify, create derivative - * works, and distribute this software and its documentation without fee is hereby - * granted on a non-exclusive basis, provided that this notice and disclaimer - * of warranty appears in all copies. - * - * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER - * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY - * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM - * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE - * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT - * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, - * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, - * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, - * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR - * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT - * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. - */ - -package gov.nist.secauto.metaschema.databind.model.metaschema; - -import gov.nist.secauto.metaschema.core.model.INamedModelInstanceAbsolute; - -public interface IBindingInstanceModelNamedAbsolute - extends IBindingInstanceModelNamed, IBindingInstanceModelAbsolute, INamedModelInstanceAbsolute { - // no additional methods -} diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/IBindingInstanceModelNamedGrouped.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/IBindingInstanceModelNamedGrouped.java deleted file mode 100644 index 19182e389..000000000 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/IBindingInstanceModelNamedGrouped.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Portions of this software was developed by employees of the National Institute - * of Standards and Technology (NIST), an agency of the Federal Government and is - * being made available as a public service. Pursuant to title 17 United States - * Code Section 105, works of NIST employees are not subject to copyright - * protection in the United States. This software may be subject to foreign - * copyright. Permission in the United States and in foreign countries, to the - * extent that NIST may hold copyright, to use, copy, modify, create derivative - * works, and distribute this software and its documentation without fee is hereby - * granted on a non-exclusive basis, provided that this notice and disclaimer - * of warranty appears in all copies. - * - * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER - * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY - * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM - * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE - * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT - * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, - * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, - * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, - * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR - * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT - * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. - */ - -package gov.nist.secauto.metaschema.databind.model.metaschema; - -import gov.nist.secauto.metaschema.core.model.INamedModelInstanceGrouped; - -public interface IBindingInstanceModelNamedGrouped extends IBindingInstanceModelNamed, INamedModelInstanceGrouped { - @Override - IBindingDefinitionAssembly getContainingDefinition(); -} diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/IBindingModelElement.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/IBindingModelElement.java deleted file mode 100644 index 742350aff..000000000 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/IBindingModelElement.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Portions of this software was developed by employees of the National Institute - * of Standards and Technology (NIST), an agency of the Federal Government and is - * being made available as a public service. Pursuant to title 17 United States - * Code Section 105, works of NIST employees are not subject to copyright - * protection in the United States. This software may be subject to foreign - * copyright. Permission in the United States and in foreign countries, to the - * extent that NIST may hold copyright, to use, copy, modify, create derivative - * works, and distribute this software and its documentation without fee is hereby - * granted on a non-exclusive basis, provided that this notice and disclaimer - * of warranty appears in all copies. - * - * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER - * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY - * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM - * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE - * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT - * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, - * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, - * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, - * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR - * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT - * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. - */ - -package gov.nist.secauto.metaschema.databind.model.metaschema; - -import gov.nist.secauto.metaschema.core.model.IModelElement; - -public interface IBindingModelElement extends IBinding, IModelElement { - @Override - IBindingModule getContainingModule(); -} diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/IBindingModule.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/IBindingModule.java deleted file mode 100644 index ed2b1baea..000000000 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/IBindingModule.java +++ /dev/null @@ -1,43 +0,0 @@ -/* - * Portions of this software was developed by employees of the National Institute - * of Standards and Technology (NIST), an agency of the Federal Government and is - * being made available as a public service. Pursuant to title 17 United States - * Code Section 105, works of NIST employees are not subject to copyright - * protection in the United States. This software may be subject to foreign - * copyright. Permission in the United States and in foreign countries, to the - * extent that NIST may hold copyright, to use, copy, modify, create derivative - * works, and distribute this software and its documentation without fee is hereby - * granted on a non-exclusive basis, provided that this notice and disclaimer - * of warranty appears in all copies. - * - * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER - * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY - * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM - * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE - * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT - * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, - * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, - * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, - * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR - * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT - * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. - */ - -package gov.nist.secauto.metaschema.databind.model.metaschema; - -import gov.nist.secauto.metaschema.core.metapath.item.node.IDocumentNodeItem; -import gov.nist.secauto.metaschema.core.model.IModuleExtended; - -public interface IBindingModule - extends IModuleExtended< - IBindingModule, - IBindingDefinitionModel, - IBindingDefinitionFlag, - IBindingDefinitionModelField, - IBindingDefinitionAssembly>, - IBinding { - - @Override - IDocumentNodeItem getBoundNodeItem(); -} diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/IInstanceModelChoiceBinding.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/IInstanceModelChoiceBinding.java deleted file mode 100644 index 7eeb41687..000000000 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/IInstanceModelChoiceBinding.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Portions of this software was developed by employees of the National Institute - * of Standards and Technology (NIST), an agency of the Federal Government and is - * being made available as a public service. Pursuant to title 17 United States - * Code Section 105, works of NIST employees are not subject to copyright - * protection in the United States. This software may be subject to foreign - * copyright. Permission in the United States and in foreign countries, to the - * extent that NIST may hold copyright, to use, copy, modify, create derivative - * works, and distribute this software and its documentation without fee is hereby - * granted on a non-exclusive basis, provided that this notice and disclaimer - * of warranty appears in all copies. - * - * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER - * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY - * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM - * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE - * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT - * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, - * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, - * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, - * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR - * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT - * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. - */ - -package gov.nist.secauto.metaschema.databind.model.metaschema; - -import gov.nist.secauto.metaschema.core.model.IChoiceInstance; - -public interface IInstanceModelChoiceBinding - extends IBindingInstanceModelAbsolute, IChoiceInstance, IBindingContainerModelAbsolute { - - @Override - IBindingDefinitionAssembly getOwningDefinition(); - - @Override - IBindingDefinitionAssembly getContainingDefinition(); -} diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/IInstanceModelChoiceGroupBinding.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/IInstanceModelChoiceGroupBinding.java deleted file mode 100644 index e441b0776..000000000 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/IInstanceModelChoiceGroupBinding.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Portions of this software was developed by employees of the National Institute - * of Standards and Technology (NIST), an agency of the Federal Government and is - * being made available as a public service. Pursuant to title 17 United States - * Code Section 105, works of NIST employees are not subject to copyright - * protection in the United States. This software may be subject to foreign - * copyright. Permission in the United States and in foreign countries, to the - * extent that NIST may hold copyright, to use, copy, modify, create derivative - * works, and distribute this software and its documentation without fee is hereby - * granted on a non-exclusive basis, provided that this notice and disclaimer - * of warranty appears in all copies. - * - * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER - * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY - * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM - * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE - * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT - * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, - * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, - * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, - * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR - * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT - * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. - */ - -package gov.nist.secauto.metaschema.databind.model.metaschema; - -import gov.nist.secauto.metaschema.core.model.IChoiceGroupInstance; - -public interface IInstanceModelChoiceGroupBinding extends IBindingInstanceModelAbsolute, IChoiceGroupInstance { - - @Override - IBindingDefinitionAssembly getOwningDefinition(); - - @Override - IBindingDefinitionAssembly getContainingDefinition(); -} diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/binding/AssemblyConstraints.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/binding/AssemblyConstraints.java index 47cf9db51..f51a776c3 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/binding/AssemblyConstraints.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/binding/AssemblyConstraints.java @@ -55,7 +55,7 @@ public class AssemblyConstraints implements IModelConstraintsBase { formalName = "Constraint Let Expression", useName = "let", maxOccurs = -1, - groupAs = @GroupAs(name = "lets", namespace = "##default", inJson = JsonGroupAsBehavior.LIST)) + groupAs = @GroupAs(name = "lets", inJson = JsonGroupAsBehavior.LIST)) private List _lets; @BoundChoiceGroup( @@ -77,7 +77,7 @@ public class AssemblyConstraints implements IModelConstraintsBase { @BoundGroupedAssembly(formalName = "Targeted Cardinality Constraint", useName = "has-cardinality", binding = TargetedHasCardinalityConstraint.class) }, - groupAs = @GroupAs(name = "rules", namespace = "##default", inJson = JsonGroupAsBehavior.LIST)) + groupAs = @GroupAs(name = "rules", inJson = JsonGroupAsBehavior.LIST)) private List _rules; @Override diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/binding/AssemblyModel.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/binding/AssemblyModel.java index c1f4df539..7e793c778 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/binding/AssemblyModel.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/binding/AssemblyModel.java @@ -77,7 +77,7 @@ public class AssemblyModel { @BoundGroupedAssembly(formalName = "Choice Grouping", useName = "choice-group", binding = ChoiceGroup.class) }, groupAs = @gov.nist.secauto.metaschema.databind.model.annotations.GroupAs(name = "instances", - namespace = "##default", inJson = JsonGroupAsBehavior.LIST)) + inJson = JsonGroupAsBehavior.LIST)) private List _instances; @BoundAssembly( @@ -159,7 +159,7 @@ public static class ChoiceGroup { binding = DefineField.class) }, groupAs = @gov.nist.secauto.metaschema.databind.model.annotations.GroupAs(name = "choices", - namespace = "##default", inJson = JsonGroupAsBehavior.LIST)) + inJson = JsonGroupAsBehavior.LIST)) private List _choices; @BoundField( @@ -271,7 +271,7 @@ public static class Assembly { useName = "prop", maxOccurs = -1, groupAs = @gov.nist.secauto.metaschema.databind.model.annotations.GroupAs(name = "props", - namespace = "##default", inJson = JsonGroupAsBehavior.LIST)) + inJson = JsonGroupAsBehavior.LIST)) private List _props; @BoundField( @@ -440,7 +440,7 @@ public static class DefineAssembly { useName = "prop", maxOccurs = -1, groupAs = @gov.nist.secauto.metaschema.databind.model.annotations.GroupAs(name = "props", - namespace = "##default", inJson = JsonGroupAsBehavior.LIST)) + inJson = JsonGroupAsBehavior.LIST)) private List _props; @BoundField( @@ -457,7 +457,7 @@ public static class DefineAssembly { @BoundGroupedAssembly(formalName = "Flag Reference", useName = "flag", binding = FlagReference.class) }, groupAs = @gov.nist.secauto.metaschema.databind.model.annotations.GroupAs(name = "flags", - namespace = "##default", inJson = JsonGroupAsBehavior.LIST)) + inJson = JsonGroupAsBehavior.LIST)) private List _flags; @BoundAssembly( @@ -479,7 +479,7 @@ public static class DefineAssembly { useName = "example", maxOccurs = -1, groupAs = @gov.nist.secauto.metaschema.databind.model.annotations.GroupAs(name = "examples", - namespace = "##default", inJson = JsonGroupAsBehavior.LIST)) + inJson = JsonGroupAsBehavior.LIST)) private List _examples; public String getName() { @@ -701,7 +701,7 @@ public static class Field { useName = "prop", maxOccurs = -1, groupAs = @gov.nist.secauto.metaschema.databind.model.annotations.GroupAs(name = "props", - namespace = "##default", inJson = JsonGroupAsBehavior.LIST)) + inJson = JsonGroupAsBehavior.LIST)) private List _props; @BoundField( @@ -920,7 +920,7 @@ public static class DefineField { useName = "prop", maxOccurs = -1, groupAs = @gov.nist.secauto.metaschema.databind.model.annotations.GroupAs(name = "props", - namespace = "##default", inJson = JsonGroupAsBehavior.LIST)) + inJson = JsonGroupAsBehavior.LIST)) private List _props; @BoundField( @@ -948,7 +948,7 @@ public static class DefineField { @BoundGroupedAssembly(formalName = "Flag Reference", useName = "flag", binding = FlagReference.class) }, groupAs = @gov.nist.secauto.metaschema.databind.model.annotations.GroupAs(name = "flags", - namespace = "##default", inJson = JsonGroupAsBehavior.LIST)) + inJson = JsonGroupAsBehavior.LIST)) private List _flags; @BoundAssembly( @@ -966,7 +966,7 @@ public static class DefineField { useName = "example", maxOccurs = -1, groupAs = @gov.nist.secauto.metaschema.databind.model.annotations.GroupAs(name = "examples", - namespace = "##default", inJson = JsonGroupAsBehavior.LIST)) + inJson = JsonGroupAsBehavior.LIST)) private List _examples; public String getName() { @@ -1170,7 +1170,7 @@ public static class Choice { binding = InlineDefineField.class) }, groupAs = @gov.nist.secauto.metaschema.databind.model.annotations.GroupAs(name = "choices", - namespace = "##default", inJson = JsonGroupAsBehavior.LIST)) + inJson = JsonGroupAsBehavior.LIST)) private List _choices; @BoundAssembly( diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/binding/AssemblyReference.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/binding/AssemblyReference.java index 5b1acc190..870118793 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/binding/AssemblyReference.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/binding/AssemblyReference.java @@ -41,13 +41,13 @@ import gov.nist.secauto.metaschema.databind.model.annotations.Matches; import gov.nist.secauto.metaschema.databind.model.annotations.MetaschemaAssembly; import gov.nist.secauto.metaschema.databind.model.annotations.ValueConstraints; -import java.lang.Override; -import java.lang.String; + +import org.apache.commons.lang3.builder.ReflectionToStringBuilder; +import org.apache.commons.lang3.builder.ToStringStyle; + import java.math.BigInteger; import java.util.LinkedList; import java.util.List; -import org.apache.commons.lang3.builder.ReflectionToStringBuilder; -import org.apache.commons.lang3.builder.ToStringStyle; @SuppressWarnings({ "PMD.DataClass", @@ -110,7 +110,7 @@ public class AssemblyReference { formalName = "Property", useName = "prop", maxOccurs = -1, - groupAs = @gov.nist.secauto.metaschema.databind.model.annotations.GroupAs(name = "props", namespace = "##default", + groupAs = @gov.nist.secauto.metaschema.databind.model.annotations.GroupAs(name = "props", inJson = JsonGroupAsBehavior.LIST)) private List _props; diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/binding/FieldConstraints.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/binding/FieldConstraints.java index 469a616e9..a4d5f1f9d 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/binding/FieldConstraints.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/binding/FieldConstraints.java @@ -54,7 +54,7 @@ public class FieldConstraints implements IValueConstraintsBase { formalName = "Constraint Let Expression", useName = "let", maxOccurs = -1, - groupAs = @GroupAs(name = "lets", namespace = "##default", inJson = JsonGroupAsBehavior.LIST)) + groupAs = @GroupAs(name = "lets", inJson = JsonGroupAsBehavior.LIST)) private List _lets; @BoundChoiceGroup( @@ -70,7 +70,7 @@ public class FieldConstraints implements IValueConstraintsBase { @BoundGroupedAssembly(formalName = "Value Matches Constraint", useName = "matches", binding = TargetedMatchesConstraint.class) }, - groupAs = @GroupAs(name = "rules", namespace = "##default", inJson = JsonGroupAsBehavior.LIST)) + groupAs = @GroupAs(name = "rules", inJson = JsonGroupAsBehavior.LIST)) private List _rules; @Override diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/binding/FieldReference.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/binding/FieldReference.java index 0cbff61d7..a5688ec66 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/binding/FieldReference.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/binding/FieldReference.java @@ -43,13 +43,13 @@ import gov.nist.secauto.metaschema.databind.model.annotations.Matches; import gov.nist.secauto.metaschema.databind.model.annotations.MetaschemaAssembly; import gov.nist.secauto.metaschema.databind.model.annotations.ValueConstraints; -import java.lang.Override; -import java.lang.String; + +import org.apache.commons.lang3.builder.ReflectionToStringBuilder; +import org.apache.commons.lang3.builder.ToStringStyle; + import java.math.BigInteger; import java.util.LinkedList; import java.util.List; -import org.apache.commons.lang3.builder.ReflectionToStringBuilder; -import org.apache.commons.lang3.builder.ToStringStyle; @SuppressWarnings({ "PMD.DataClass", @@ -131,7 +131,7 @@ public class FieldReference { formalName = "Property", useName = "prop", maxOccurs = -1, - groupAs = @gov.nist.secauto.metaschema.databind.model.annotations.GroupAs(name = "props", namespace = "##default", + groupAs = @gov.nist.secauto.metaschema.databind.model.annotations.GroupAs(name = "props", inJson = JsonGroupAsBehavior.LIST)) private List _props; diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/binding/FlagAllowedValues.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/binding/FlagAllowedValues.java index c1091c614..85fb6111c 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/binding/FlagAllowedValues.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/binding/FlagAllowedValues.java @@ -122,7 +122,7 @@ public class FlagAllowedValues implements IConstraintBase { formalName = "Property", useName = "prop", maxOccurs = -1, - groupAs = @GroupAs(name = "props", namespace = "##default", inJson = JsonGroupAsBehavior.LIST)) + groupAs = @GroupAs(name = "props", inJson = JsonGroupAsBehavior.LIST)) private List _props; @BoundField( @@ -130,7 +130,7 @@ public class FlagAllowedValues implements IConstraintBase { useName = "enum", minOccurs = 1, maxOccurs = -1, - groupAs = @GroupAs(name = "enums", namespace = "##default", inJson = JsonGroupAsBehavior.LIST)) + groupAs = @GroupAs(name = "enums", inJson = JsonGroupAsBehavior.LIST)) private List _enums; @BoundField( diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/binding/FlagConstraints.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/binding/FlagConstraints.java index 6f6db71a9..3ed9b9082 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/binding/FlagConstraints.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/binding/FlagConstraints.java @@ -55,7 +55,7 @@ public class FlagConstraints implements IValueConstraintsBase { formalName = "Constraint Let Expression", useName = "let", maxOccurs = -1, - groupAs = @GroupAs(name = "lets", namespace = "##default", inJson = JsonGroupAsBehavior.LIST)) + groupAs = @GroupAs(name = "lets", inJson = JsonGroupAsBehavior.LIST)) private List _lets; @BoundChoiceGroup( @@ -71,7 +71,7 @@ public class FlagConstraints implements IValueConstraintsBase { @BoundGroupedAssembly(formalName = "Value Matches Constraint", useName = "matches", binding = FlagMatches.class) }, - groupAs = @GroupAs(name = "rules", namespace = "##default", inJson = JsonGroupAsBehavior.LIST)) + groupAs = @GroupAs(name = "rules", inJson = JsonGroupAsBehavior.LIST)) private List _rules; @Override diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/binding/FlagExpect.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/binding/FlagExpect.java index f609191c7..3b2c87921 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/binding/FlagExpect.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/binding/FlagExpect.java @@ -104,7 +104,7 @@ public class FlagExpect implements IConstraintBase { formalName = "Property", useName = "prop", maxOccurs = -1, - groupAs = @GroupAs(name = "props", namespace = "##default", inJson = JsonGroupAsBehavior.LIST)) + groupAs = @GroupAs(name = "props", inJson = JsonGroupAsBehavior.LIST)) private List _props; @BoundField( diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/binding/FlagIndexHasKey.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/binding/FlagIndexHasKey.java index 53fb36ed1..7707bf98c 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/binding/FlagIndexHasKey.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/binding/FlagIndexHasKey.java @@ -103,7 +103,7 @@ public class FlagIndexHasKey implements IConstraintBase { formalName = "Property", useName = "prop", maxOccurs = -1, - groupAs = @GroupAs(name = "props", namespace = "##default", inJson = JsonGroupAsBehavior.LIST)) + groupAs = @GroupAs(name = "props", inJson = JsonGroupAsBehavior.LIST)) private List _props; @BoundAssembly( @@ -111,7 +111,7 @@ public class FlagIndexHasKey implements IConstraintBase { useName = "key-field", minOccurs = 1, maxOccurs = -1, - groupAs = @GroupAs(name = "key-fields", namespace = "##default", inJson = JsonGroupAsBehavior.LIST)) + groupAs = @GroupAs(name = "key-fields", inJson = JsonGroupAsBehavior.LIST)) private List _keyFields; @BoundField( diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/binding/FlagMatches.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/binding/FlagMatches.java index dedba477e..52efdaddd 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/binding/FlagMatches.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/binding/FlagMatches.java @@ -127,7 +127,7 @@ public class FlagMatches implements IConstraintBase { formalName = "Property", useName = "prop", maxOccurs = -1, - groupAs = @GroupAs(name = "props", namespace = "##default", inJson = JsonGroupAsBehavior.LIST)) + groupAs = @GroupAs(name = "props", inJson = JsonGroupAsBehavior.LIST)) private List _props; @BoundField( diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/binding/FlagReference.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/binding/FlagReference.java index e0f529c33..8e507ccac 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/binding/FlagReference.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/binding/FlagReference.java @@ -42,13 +42,13 @@ import gov.nist.secauto.metaschema.databind.model.annotations.GroupAs; import gov.nist.secauto.metaschema.databind.model.annotations.MetaschemaAssembly; import gov.nist.secauto.metaschema.databind.model.annotations.ValueConstraints; -import java.lang.Override; -import java.lang.String; + +import org.apache.commons.lang3.builder.ReflectionToStringBuilder; +import org.apache.commons.lang3.builder.ToStringStyle; + import java.math.BigInteger; import java.util.LinkedList; import java.util.List; -import org.apache.commons.lang3.builder.ReflectionToStringBuilder; -import org.apache.commons.lang3.builder.ToStringStyle; @SuppressWarnings({ "PMD.DataClass", @@ -110,7 +110,7 @@ public class FlagReference { formalName = "Property", useName = "prop", maxOccurs = -1, - groupAs = @GroupAs(name = "props", namespace = "##default", inJson = JsonGroupAsBehavior.LIST)) + groupAs = @GroupAs(name = "props", inJson = JsonGroupAsBehavior.LIST)) private List _props; @BoundField( diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/binding/InlineDefineAssembly.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/binding/InlineDefineAssembly.java index 8be25ca5f..c6dad361f 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/binding/InlineDefineAssembly.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/binding/InlineDefineAssembly.java @@ -43,14 +43,13 @@ import gov.nist.secauto.metaschema.databind.model.annotations.Matches; import gov.nist.secauto.metaschema.databind.model.annotations.MetaschemaAssembly; import gov.nist.secauto.metaschema.databind.model.annotations.ValueConstraints; -import java.lang.Object; -import java.lang.Override; -import java.lang.String; + +import org.apache.commons.lang3.builder.ReflectionToStringBuilder; +import org.apache.commons.lang3.builder.ToStringStyle; + import java.math.BigInteger; import java.util.LinkedList; import java.util.List; -import org.apache.commons.lang3.builder.ReflectionToStringBuilder; -import org.apache.commons.lang3.builder.ToStringStyle; @SuppressWarnings({ "PMD.DataClass", @@ -113,7 +112,7 @@ public class InlineDefineAssembly { formalName = "Property", useName = "prop", maxOccurs = -1, - groupAs = @gov.nist.secauto.metaschema.databind.model.annotations.GroupAs(name = "props", namespace = "##default", + groupAs = @gov.nist.secauto.metaschema.databind.model.annotations.GroupAs(name = "props", inJson = JsonGroupAsBehavior.LIST)) private List _props; @@ -135,7 +134,7 @@ public class InlineDefineAssembly { binding = InlineDefineFlag.class), @BoundGroupedAssembly(formalName = "Flag Reference", useName = "flag", binding = FlagReference.class) }, - groupAs = @gov.nist.secauto.metaschema.databind.model.annotations.GroupAs(name = "flags", namespace = "##default", + groupAs = @gov.nist.secauto.metaschema.databind.model.annotations.GroupAs(name = "flags", inJson = JsonGroupAsBehavior.LIST)) private List _flags; @@ -158,7 +157,7 @@ public class InlineDefineAssembly { useName = "example", maxOccurs = -1, groupAs = @gov.nist.secauto.metaschema.databind.model.annotations.GroupAs(name = "examples", - namespace = "##default", inJson = JsonGroupAsBehavior.LIST)) + inJson = JsonGroupAsBehavior.LIST)) private List _examples; public String getName() { diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/binding/InlineDefineField.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/binding/InlineDefineField.java index 5ed0f442d..b13ae5daf 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/binding/InlineDefineField.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/binding/InlineDefineField.java @@ -45,14 +45,13 @@ import gov.nist.secauto.metaschema.databind.model.annotations.Matches; import gov.nist.secauto.metaschema.databind.model.annotations.MetaschemaAssembly; import gov.nist.secauto.metaschema.databind.model.annotations.ValueConstraints; -import java.lang.Object; -import java.lang.Override; -import java.lang.String; + +import org.apache.commons.lang3.builder.ReflectionToStringBuilder; +import org.apache.commons.lang3.builder.ToStringStyle; + import java.math.BigInteger; import java.util.LinkedList; import java.util.List; -import org.apache.commons.lang3.builder.ReflectionToStringBuilder; -import org.apache.commons.lang3.builder.ToStringStyle; @SuppressWarnings({ "PMD.DataClass", @@ -161,7 +160,7 @@ public class InlineDefineField { formalName = "Property", useName = "prop", maxOccurs = -1, - groupAs = @gov.nist.secauto.metaschema.databind.model.annotations.GroupAs(name = "props", namespace = "##default", + groupAs = @gov.nist.secauto.metaschema.databind.model.annotations.GroupAs(name = "props", inJson = JsonGroupAsBehavior.LIST)) private List _props; @@ -194,7 +193,7 @@ public class InlineDefineField { binding = InlineDefineFlag.class), @BoundGroupedAssembly(formalName = "Flag Reference", useName = "flag", binding = FlagReference.class) }, - groupAs = @gov.nist.secauto.metaschema.databind.model.annotations.GroupAs(name = "flags", namespace = "##default", + groupAs = @gov.nist.secauto.metaschema.databind.model.annotations.GroupAs(name = "flags", inJson = JsonGroupAsBehavior.LIST)) private List _flags; @@ -213,7 +212,7 @@ public class InlineDefineField { useName = "example", maxOccurs = -1, groupAs = @gov.nist.secauto.metaschema.databind.model.annotations.GroupAs(name = "examples", - namespace = "##default", inJson = JsonGroupAsBehavior.LIST)) + inJson = JsonGroupAsBehavior.LIST)) private List _examples; public String getName() { diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/binding/InlineDefineFlag.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/binding/InlineDefineFlag.java index 048be4ea8..b14dab385 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/binding/InlineDefineFlag.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/binding/InlineDefineFlag.java @@ -42,13 +42,13 @@ import gov.nist.secauto.metaschema.databind.model.annotations.GroupAs; import gov.nist.secauto.metaschema.databind.model.annotations.MetaschemaAssembly; import gov.nist.secauto.metaschema.databind.model.annotations.ValueConstraints; -import java.lang.Override; -import java.lang.String; + +import org.apache.commons.lang3.builder.ReflectionToStringBuilder; +import org.apache.commons.lang3.builder.ToStringStyle; + import java.math.BigInteger; import java.util.LinkedList; import java.util.List; -import org.apache.commons.lang3.builder.ReflectionToStringBuilder; -import org.apache.commons.lang3.builder.ToStringStyle; @SuppressWarnings({ "PMD.DataClass", @@ -135,7 +135,7 @@ public class InlineDefineFlag { formalName = "Property", useName = "prop", maxOccurs = -1, - groupAs = @GroupAs(name = "props", namespace = "##default", inJson = JsonGroupAsBehavior.LIST)) + groupAs = @GroupAs(name = "props", inJson = JsonGroupAsBehavior.LIST)) private List _props; @BoundAssembly( @@ -152,7 +152,7 @@ public class InlineDefineFlag { formalName = "Example", useName = "example", maxOccurs = -1, - groupAs = @GroupAs(name = "examples", namespace = "##default", inJson = JsonGroupAsBehavior.LIST)) + groupAs = @GroupAs(name = "examples", inJson = JsonGroupAsBehavior.LIST)) private List _examples; public String getName() { diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/binding/METASCHEMA.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/binding/METASCHEMA.java index 94f036f94..4a2e711e4 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/binding/METASCHEMA.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/binding/METASCHEMA.java @@ -49,15 +49,14 @@ import gov.nist.secauto.metaschema.databind.model.annotations.MetaschemaAssembly; import gov.nist.secauto.metaschema.databind.model.annotations.MetaschemaField; import gov.nist.secauto.metaschema.databind.model.annotations.ValueConstraints; -import java.lang.Object; -import java.lang.Override; -import java.lang.String; + +import org.apache.commons.lang3.builder.ReflectionToStringBuilder; +import org.apache.commons.lang3.builder.ToStringStyle; + import java.math.BigInteger; import java.net.URI; import java.util.LinkedList; import java.util.List; -import org.apache.commons.lang3.builder.ReflectionToStringBuilder; -import org.apache.commons.lang3.builder.ToStringStyle; /** * A declaration of the Metaschema module. @@ -139,7 +138,7 @@ public class METASCHEMA { description = "Imports a set of Metaschema modules contained in another resource. Imports support the reuse of common information structures.", useName = "import", maxOccurs = -1, - groupAs = @GroupAs(name = "imports", namespace = "##default", inJson = JsonGroupAsBehavior.LIST)) + groupAs = @GroupAs(name = "imports", inJson = JsonGroupAsBehavior.LIST)) private List _imports; @BoundChoiceGroup( @@ -153,7 +152,7 @@ public class METASCHEMA { @BoundGroupedAssembly(formalName = "Global Flag Definition", useName = "define-flag", binding = DefineFlag.class) }, - groupAs = @GroupAs(name = "definitions", namespace = "##default", inJson = JsonGroupAsBehavior.LIST)) + groupAs = @GroupAs(name = "definitions", inJson = JsonGroupAsBehavior.LIST)) private List _definitions; public String getAbstract() { @@ -346,7 +345,7 @@ public static class DefineField { formalName = "Property", useName = "prop", maxOccurs = -1, - groupAs = @GroupAs(name = "props", namespace = "##default", inJson = JsonGroupAsBehavior.LIST)) + groupAs = @GroupAs(name = "props", inJson = JsonGroupAsBehavior.LIST)) private List _props; @BoundField( @@ -379,7 +378,7 @@ public static class DefineField { binding = InlineDefineFlag.class), @BoundGroupedAssembly(formalName = "Flag Reference", useName = "flag", binding = FlagReference.class) }, - groupAs = @GroupAs(name = "flags", namespace = "##default", inJson = JsonGroupAsBehavior.LIST)) + groupAs = @GroupAs(name = "flags", inJson = JsonGroupAsBehavior.LIST)) private List _flags; @BoundAssembly( @@ -396,7 +395,7 @@ public static class DefineField { formalName = "Example", useName = "example", maxOccurs = -1, - groupAs = @GroupAs(name = "examples", namespace = "##default", inJson = JsonGroupAsBehavior.LIST)) + groupAs = @GroupAs(name = "examples", inJson = JsonGroupAsBehavior.LIST)) private List _examples; public String getName() { @@ -682,7 +681,7 @@ public static class DefineFlag { formalName = "Property", useName = "prop", maxOccurs = -1, - groupAs = @GroupAs(name = "props", namespace = "##default", inJson = JsonGroupAsBehavior.LIST)) + groupAs = @GroupAs(name = "props", inJson = JsonGroupAsBehavior.LIST)) private List _props; @BoundField( @@ -705,7 +704,7 @@ public static class DefineFlag { formalName = "Example", useName = "example", maxOccurs = -1, - groupAs = @GroupAs(name = "examples", namespace = "##default", inJson = JsonGroupAsBehavior.LIST)) + groupAs = @GroupAs(name = "examples", inJson = JsonGroupAsBehavior.LIST)) private List _examples; public String getName() { @@ -970,7 +969,7 @@ public static class DefineAssembly { formalName = "Property", useName = "prop", maxOccurs = -1, - groupAs = @GroupAs(name = "props", namespace = "##default", inJson = JsonGroupAsBehavior.LIST)) + groupAs = @GroupAs(name = "props", inJson = JsonGroupAsBehavior.LIST)) private List _props; @BoundField( @@ -999,7 +998,7 @@ public static class DefineAssembly { binding = InlineDefineFlag.class), @BoundGroupedAssembly(formalName = "Flag Reference", useName = "flag", binding = FlagReference.class) }, - groupAs = @GroupAs(name = "flags", namespace = "##default", inJson = JsonGroupAsBehavior.LIST)) + groupAs = @GroupAs(name = "flags", inJson = JsonGroupAsBehavior.LIST)) private List _flags; @BoundAssembly( @@ -1020,7 +1019,7 @@ public static class DefineAssembly { formalName = "Example", useName = "example", maxOccurs = -1, - groupAs = @GroupAs(name = "examples", namespace = "##default", inJson = JsonGroupAsBehavior.LIST)) + groupAs = @GroupAs(name = "examples", inJson = JsonGroupAsBehavior.LIST)) private List _examples; public String getName() { diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/binding/MetapathContext.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/binding/MetapathContext.java index 6edea95c9..98ea9635a 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/binding/MetapathContext.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/binding/MetapathContext.java @@ -52,7 +52,7 @@ public class MetapathContext { useName = "metapath", minOccurs = 1, maxOccurs = -1, - groupAs = @GroupAs(name = "metapaths", namespace = "##default", inJson = JsonGroupAsBehavior.LIST)) + groupAs = @GroupAs(name = "metapaths", inJson = JsonGroupAsBehavior.LIST)) private List _metapaths; @BoundAssembly( @@ -62,7 +62,7 @@ public class MetapathContext { @BoundAssembly( useName = "metapath-context", maxOccurs = -1, - groupAs = @GroupAs(name = "metapath-contexts", namespace = "##default", inJson = JsonGroupAsBehavior.LIST)) + groupAs = @GroupAs(name = "metapath-contexts", inJson = JsonGroupAsBehavior.LIST)) private List _metapathContexts; @BoundField( diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/binding/MetaschemaMetaConstraints.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/binding/MetaschemaMetaConstraints.java index 364eaaa60..057591ef7 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/binding/MetaschemaMetaConstraints.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/binding/MetaschemaMetaConstraints.java @@ -63,7 +63,7 @@ public class MetaschemaMetaConstraints { description = "Declares a set of Metaschema constraints from an out-of-line resource to import, supporting composition of constraint sets.", useName = "import", maxOccurs = -1, - groupAs = @GroupAs(name = "imports", namespace = "##default", inJson = JsonGroupAsBehavior.LIST)) + groupAs = @GroupAs(name = "imports", inJson = JsonGroupAsBehavior.LIST)) private List _imports; @BoundAssembly( @@ -74,7 +74,7 @@ public class MetaschemaMetaConstraints { useName = "metapath-context", minOccurs = 1, maxOccurs = -1, - groupAs = @GroupAs(name = "metapath-contexts", namespace = "##default", inJson = JsonGroupAsBehavior.LIST)) + groupAs = @GroupAs(name = "metapath-contexts", inJson = JsonGroupAsBehavior.LIST)) private List _metapathContexts; public List getImports() { diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/binding/MetaschemaModuleConstraints.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/binding/MetaschemaModuleConstraints.java index e40f725aa..1fb17e0fd 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/binding/MetaschemaModuleConstraints.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/binding/MetaschemaModuleConstraints.java @@ -83,14 +83,14 @@ public class MetaschemaModuleConstraints { description = "Declares a set of Metaschema constraints from an out-of-line resource to import, supporting composition of constraint sets.", useName = "import", maxOccurs = -1, - groupAs = @GroupAs(name = "imports", namespace = "##default", inJson = JsonGroupAsBehavior.LIST)) + groupAs = @GroupAs(name = "imports", inJson = JsonGroupAsBehavior.LIST)) private List _imports; @BoundAssembly( useName = "scope", minOccurs = 1, maxOccurs = -1, - groupAs = @GroupAs(name = "scopes", namespace = "##default", inJson = JsonGroupAsBehavior.LIST)) + groupAs = @GroupAs(name = "scopes", inJson = JsonGroupAsBehavior.LIST)) private List _scopes; public String getName() { @@ -208,7 +208,7 @@ public static class Scope { @BoundGroupedAssembly(useName = "field", binding = Field.class), @BoundGroupedAssembly(useName = "flag", binding = Flag.class) }, - groupAs = @GroupAs(name = "constraints", namespace = "##default", inJson = JsonGroupAsBehavior.LIST)) + groupAs = @GroupAs(name = "constraints", inJson = JsonGroupAsBehavior.LIST)) private List _constraints; @BoundField( @@ -278,7 +278,7 @@ public static class Flag implements IValueConstraintsBase { @BoundGroupedAssembly(formalName = "Value Matches Constraint", useName = "matches", binding = FlagMatches.class) }, - groupAs = @GroupAs(name = "rules", namespace = "##default", inJson = JsonGroupAsBehavior.LIST)) + groupAs = @GroupAs(name = "rules", inJson = JsonGroupAsBehavior.LIST)) private List _rules; @Override @@ -333,7 +333,7 @@ public static class Field implements IValueConstraintsBase { @BoundGroupedAssembly(formalName = "Value Matches Constraint", useName = "matches", binding = TargetedMatchesConstraint.class) }, - groupAs = @GroupAs(name = "rules", namespace = "##default", inJson = JsonGroupAsBehavior.LIST)) + groupAs = @GroupAs(name = "rules", inJson = JsonGroupAsBehavior.LIST)) private List _rules; public String getTarget() { @@ -394,7 +394,7 @@ public static class Assembly implements IModelConstraintsBase { @BoundGroupedAssembly(formalName = "Targeted Cardinality Constraint", useName = "has-cardinality", binding = TargetedHasCardinalityConstraint.class) }, - groupAs = @GroupAs(name = "rules", namespace = "##default", inJson = JsonGroupAsBehavior.LIST)) + groupAs = @GroupAs(name = "rules", inJson = JsonGroupAsBehavior.LIST)) private List _rules; public String getTarget() { diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/binding/TargetedAllowedValuesConstraint.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/binding/TargetedAllowedValuesConstraint.java index 971151767..edf02a210 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/binding/TargetedAllowedValuesConstraint.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/binding/TargetedAllowedValuesConstraint.java @@ -130,7 +130,7 @@ public class TargetedAllowedValuesConstraint implements ITargetedConstraintBase formalName = "Property", useName = "prop", maxOccurs = -1, - groupAs = @GroupAs(name = "props", namespace = "##default", inJson = JsonGroupAsBehavior.LIST)) + groupAs = @GroupAs(name = "props", inJson = JsonGroupAsBehavior.LIST)) private List _props; @BoundField( @@ -138,7 +138,7 @@ public class TargetedAllowedValuesConstraint implements ITargetedConstraintBase useName = "enum", minOccurs = 1, maxOccurs = -1, - groupAs = @GroupAs(name = "enums", namespace = "##default", inJson = JsonGroupAsBehavior.LIST)) + groupAs = @GroupAs(name = "enums", inJson = JsonGroupAsBehavior.LIST)) private List _enums; @BoundField( diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/binding/TargetedExpectConstraint.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/binding/TargetedExpectConstraint.java index ddd306fa6..de17f639e 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/binding/TargetedExpectConstraint.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/binding/TargetedExpectConstraint.java @@ -111,7 +111,7 @@ public class TargetedExpectConstraint implements ITargetedConstraintBase { formalName = "Property", useName = "prop", maxOccurs = -1, - groupAs = @GroupAs(name = "props", namespace = "##default", inJson = JsonGroupAsBehavior.LIST)) + groupAs = @GroupAs(name = "props", inJson = JsonGroupAsBehavior.LIST)) private List _props; @BoundField( diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/binding/TargetedHasCardinalityConstraint.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/binding/TargetedHasCardinalityConstraint.java index c20099b25..b6a09bf8f 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/binding/TargetedHasCardinalityConstraint.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/binding/TargetedHasCardinalityConstraint.java @@ -121,7 +121,7 @@ public class TargetedHasCardinalityConstraint implements ITargetedConstraintBase formalName = "Property", useName = "prop", maxOccurs = -1, - groupAs = @GroupAs(name = "props", namespace = "##default", inJson = JsonGroupAsBehavior.LIST)) + groupAs = @GroupAs(name = "props", inJson = JsonGroupAsBehavior.LIST)) private List _props; @BoundField( diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/binding/TargetedIndexConstraint.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/binding/TargetedIndexConstraint.java index 58d63835c..6051f9e97 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/binding/TargetedIndexConstraint.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/binding/TargetedIndexConstraint.java @@ -111,7 +111,7 @@ public class TargetedIndexConstraint implements ITargetedConstraintBase { formalName = "Property", useName = "prop", maxOccurs = -1, - groupAs = @GroupAs(name = "props", namespace = "##default", inJson = JsonGroupAsBehavior.LIST)) + groupAs = @GroupAs(name = "props", inJson = JsonGroupAsBehavior.LIST)) private List _props; @BoundAssembly( @@ -119,7 +119,7 @@ public class TargetedIndexConstraint implements ITargetedConstraintBase { useName = "key-field", minOccurs = 1, maxOccurs = -1, - groupAs = @GroupAs(name = "key-fields", namespace = "##default", inJson = JsonGroupAsBehavior.LIST)) + groupAs = @GroupAs(name = "key-fields", inJson = JsonGroupAsBehavior.LIST)) private List _keyFields; @BoundField( diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/binding/TargetedIndexHasKeyConstraint.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/binding/TargetedIndexHasKeyConstraint.java index 8dd650d3d..d9aedbb56 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/binding/TargetedIndexHasKeyConstraint.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/binding/TargetedIndexHasKeyConstraint.java @@ -111,7 +111,7 @@ public class TargetedIndexHasKeyConstraint implements ITargetedConstraintBase { formalName = "Property", useName = "prop", maxOccurs = -1, - groupAs = @GroupAs(name = "props", namespace = "##default", inJson = JsonGroupAsBehavior.LIST)) + groupAs = @GroupAs(name = "props", inJson = JsonGroupAsBehavior.LIST)) private List _props; @BoundAssembly( @@ -119,7 +119,7 @@ public class TargetedIndexHasKeyConstraint implements ITargetedConstraintBase { useName = "key-field", minOccurs = 1, maxOccurs = -1, - groupAs = @GroupAs(name = "key-fields", namespace = "##default", inJson = JsonGroupAsBehavior.LIST)) + groupAs = @GroupAs(name = "key-fields", inJson = JsonGroupAsBehavior.LIST)) private List _keyFields; @BoundField( diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/binding/TargetedIsUniqueConstraint.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/binding/TargetedIsUniqueConstraint.java index c4cc2e4b9..e8eedd6d6 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/binding/TargetedIsUniqueConstraint.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/binding/TargetedIsUniqueConstraint.java @@ -104,7 +104,7 @@ public class TargetedIsUniqueConstraint implements ITargetedConstraintBase { formalName = "Property", useName = "prop", maxOccurs = -1, - groupAs = @GroupAs(name = "props", namespace = "##default", inJson = JsonGroupAsBehavior.LIST)) + groupAs = @GroupAs(name = "props", inJson = JsonGroupAsBehavior.LIST)) private List _props; @BoundAssembly( @@ -112,7 +112,7 @@ public class TargetedIsUniqueConstraint implements ITargetedConstraintBase { useName = "key-field", minOccurs = 1, maxOccurs = -1, - groupAs = @GroupAs(name = "key-fields", namespace = "##default", inJson = JsonGroupAsBehavior.LIST)) + groupAs = @GroupAs(name = "key-fields", inJson = JsonGroupAsBehavior.LIST)) private List _keyFields; @BoundField( diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/binding/TargetedMatchesConstraint.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/binding/TargetedMatchesConstraint.java index 3eaa1a743..25ce66f60 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/binding/TargetedMatchesConstraint.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/binding/TargetedMatchesConstraint.java @@ -134,7 +134,7 @@ public class TargetedMatchesConstraint implements ITargetedConstraintBase { formalName = "Property", useName = "prop", maxOccurs = -1, - groupAs = @GroupAs(name = "props", namespace = "##default", inJson = JsonGroupAsBehavior.LIST)) + groupAs = @GroupAs(name = "props", inJson = JsonGroupAsBehavior.LIST)) private List _props; @BoundField( diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/binding/package-info.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/binding/package-info.java index f0b9eef72..93c990d9a 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/binding/package-info.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/binding/package-info.java @@ -24,6 +24,11 @@ * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. */ + +/** + * A Metaschema module represented as a set of Metaschema module bindings. + */ + @gov.nist.secauto.metaschema.databind.model.annotations.MetaschemaPackage(moduleClass = { MetaschemaModelModule.class }) @gov.nist.secauto.metaschema.databind.model.annotations.XmlSchema( namespace = "http://csrc.nist.gov/ns/oscal/metaschema/1.0", diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/AbstractBindingModelContainerSupport.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/AbstractBindingModelContainerSupport.java index 2675858b7..c44e029e5 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/AbstractBindingModelContainerSupport.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/AbstractBindingModelContainerSupport.java @@ -26,38 +26,39 @@ package gov.nist.secauto.metaschema.databind.model.metaschema.impl; +import gov.nist.secauto.metaschema.core.model.IAssemblyDefinition; +import gov.nist.secauto.metaschema.core.model.IAssemblyInstanceAbsolute; +import gov.nist.secauto.metaschema.core.model.IContainerModelAbsolute; +import gov.nist.secauto.metaschema.core.model.IContainerModelSupport; +import gov.nist.secauto.metaschema.core.model.IFieldDefinition; +import gov.nist.secauto.metaschema.core.model.IFieldInstanceAbsolute; +import gov.nist.secauto.metaschema.core.model.IModelInstanceAbsolute; +import gov.nist.secauto.metaschema.core.model.IModule; +import gov.nist.secauto.metaschema.core.model.INamedModelInstanceAbsolute; +import gov.nist.secauto.metaschema.core.util.ObjectUtils; +import gov.nist.secauto.metaschema.databind.model.IBoundInstanceModelGroupedAssembly; +import gov.nist.secauto.metaschema.databind.model.metaschema.binding.AssemblyReference; +import gov.nist.secauto.metaschema.databind.model.metaschema.binding.FieldReference; + import java.util.List; import java.util.Map; import javax.xml.namespace.QName; import edu.umd.cs.findbugs.annotations.NonNull; -import gov.nist.secauto.metaschema.core.model.IContainerModelSupport; -import gov.nist.secauto.metaschema.core.util.ObjectUtils; -import gov.nist.secauto.metaschema.databind.model.IBoundInstanceModelGroupedAssembly; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingContainerModelAbsolute; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingDefinitionAssembly; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingDefinitionModelField; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingInstanceModelAbsolute; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingInstanceModelAssemblyAbsolute; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingInstanceModelFieldAbsolute; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingInstanceModelNamedAbsolute; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingModule; -import gov.nist.secauto.metaschema.databind.model.metaschema.binding.AssemblyReference; -import gov.nist.secauto.metaschema.databind.model.metaschema.binding.FieldReference; public abstract class AbstractBindingModelContainerSupport implements IContainerModelSupport< - IBindingInstanceModelAbsolute, - IBindingInstanceModelNamedAbsolute, - IBindingInstanceModelFieldAbsolute, - IBindingInstanceModelAssemblyAbsolute> { + IModelInstanceAbsolute, + INamedModelInstanceAbsolute, + IFieldInstanceAbsolute, + IAssemblyInstanceAbsolute> { protected static void addInstance( - @NonNull IBindingInstanceModelAssemblyAbsolute assembly, - @NonNull List modelInstances, - @NonNull Map namedModelInstances, - @NonNull Map assemblyInstances) { + @NonNull IAssemblyInstanceAbsolute assembly, + @NonNull List modelInstances, + @NonNull Map namedModelInstances, + @NonNull Map assemblyInstances) { QName effectiveName = assembly.getXmlQName(); modelInstances.add(assembly); namedModelInstances.put(effectiveName, assembly); @@ -65,10 +66,10 @@ protected static void addInstance( } protected static void addInstance( - @NonNull IBindingInstanceModelFieldAbsolute field, - @NonNull List modelInstances, - @NonNull Map namedModelInstances, - @NonNull Map fieldInstances) { + @NonNull IFieldInstanceAbsolute field, + @NonNull List modelInstances, + @NonNull Map namedModelInstances, + @NonNull Map fieldInstances) { QName effectiveName = field.getXmlQName(); modelInstances.add(field); namedModelInstances.put(effectiveName, field); @@ -76,16 +77,17 @@ protected static void addInstance( } @NonNull - protected static IBindingInstanceModelAssemblyAbsolute newInstance( + protected static IAssemblyInstanceAbsolute newInstance( @NonNull AssemblyReference obj, @NonNull IBoundInstanceModelGroupedAssembly objInstance, int position, - @NonNull IBindingContainerModelAbsolute parent) { - IBindingDefinitionAssembly owningDefinition = parent.getOwningDefinition(); - IBindingModule module = owningDefinition.getContainingModule(); + @NonNull IContainerModelAbsolute parent) { + IAssemblyDefinition owningDefinition = parent.getOwningDefinition(); + IModule module = owningDefinition.getContainingModule(); String name = ObjectUtils.requireNonNull(obj.getRef()); - IBindingDefinitionAssembly definition = module.getScopedAssemblyDefinitionByName(name); + IAssemblyDefinition definition = module.getScopedAssemblyDefinitionByName( + module.toModelQName(name)); if (definition == null) { throw new IllegalStateException( @@ -98,16 +100,17 @@ protected static IBindingInstanceModelAssemblyAbsolute newInstance( } @NonNull - protected static IBindingInstanceModelFieldAbsolute newInstance( + protected static IFieldInstanceAbsolute newInstance( @NonNull FieldReference obj, @NonNull IBoundInstanceModelGroupedAssembly objInstance, int position, - @NonNull IBindingContainerModelAbsolute parent) { - IBindingDefinitionAssembly owningDefinition = parent.getOwningDefinition(); - IBindingModule module = owningDefinition.getContainingModule(); + @NonNull IContainerModelAbsolute parent) { + IAssemblyDefinition owningDefinition = parent.getOwningDefinition(); + IModule module = owningDefinition.getContainingModule(); String name = ObjectUtils.requireNonNull(obj.getRef()); - IBindingDefinitionModelField definition = module.getScopedFieldDefinitionByName(name); + IFieldDefinition definition = module.getScopedFieldDefinitionByName( + module.toModelQName(name)); if (definition == null) { throw new IllegalStateException( String.format("Unable to resolve field reference '%s' in definition '%s' in module '%s'", diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/AbstractGroupedAssemblyInstanceNodeItem.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/AbstractGroupedAssemblyInstanceNodeItem.java deleted file mode 100644 index 621e2e9b9..000000000 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/AbstractGroupedAssemblyInstanceNodeItem.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Portions of this software was developed by employees of the National Institute - * of Standards and Technology (NIST), an agency of the Federal Government and is - * being made available as a public service. Pursuant to title 17 United States - * Code Section 105, works of NIST employees are not subject to copyright - * protection in the United States. This software may be subject to foreign - * copyright. Permission in the United States and in foreign countries, to the - * extent that NIST may hold copyright, to use, copy, modify, create derivative - * works, and distribute this software and its documentation without fee is hereby - * granted on a non-exclusive basis, provided that this notice and disclaimer - * of warranty appears in all copies. - * - * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER - * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY - * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM - * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE - * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT - * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, - * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, - * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, - * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR - * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT - * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. - */ - -package gov.nist.secauto.metaschema.databind.model.metaschema.impl; - -// REFACTOR: Check if this is used, delete? -// public abstract class AbstractGroupedAssemblyInstanceNodeItem -// extends AbstractGroupedModelInstanceNodeItem -// implements IAssemblyInstanceGroupedNodeItem, IFeatureModelContainerItem { -// protected AbstractGroupedAssemblyInstanceNodeItem( -// @NonNull IAssemblyInstanceGrouped instance, -// int position, -// @NonNull INodeItemGenerator generator) { -// super(instance, position); -// this.model = -// ObjectUtils.notNull(Lazy.lazy(generator.newDataModelSupplier(this))); -// } -// -// @NonNull -// private final Lazy model; -// -// @SuppressWarnings("null") -// @Override -// public ModelContainer getModel() { -// return model.get(); -// } -// } diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/AbstractGroupedModelInstanceNodeItem.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/AbstractGroupedModelInstanceNodeItem.java deleted file mode 100644 index 2ce1c6a01..000000000 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/AbstractGroupedModelInstanceNodeItem.java +++ /dev/null @@ -1,71 +0,0 @@ -/* - * Portions of this software was developed by employees of the National Institute - * of Standards and Technology (NIST), an agency of the Federal Government and is - * being made available as a public service. Pursuant to title 17 United States - * Code Section 105, works of NIST employees are not subject to copyright - * protection in the United States. This software may be subject to foreign - * copyright. Permission in the United States and in foreign countries, to the - * extent that NIST may hold copyright, to use, copy, modify, create derivative - * works, and distribute this software and its documentation without fee is hereby - * granted on a non-exclusive basis, provided that this notice and disclaimer - * of warranty appears in all copies. - * - * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER - * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY - * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM - * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE - * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT - * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, - * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, - * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, - * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR - * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT - * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. - */ - -package gov.nist.secauto.metaschema.databind.model.metaschema.impl; - -// REFACTOR: Check if this is used, delete? -// public abstract class AbstractGroupedModelInstanceNodeItem< -// D extends IModelDefinition, -// I extends INamedModelInstance, -// T> -// implements IModelNodeItem { -// private final int position; -// @NonNull -// private final I instance; -// @NonNull -// private final T value; -// -// protected AbstractGroupedModelInstanceNodeItem( -// @NonNull I instance, -// int position, -// @NonNull T value) { -// this.position = position; -// this.instance = instance; -// this.value = value; -// } -// -// @Override -// public int getPosition() { -// return position; -// } -// -// @SuppressWarnings("unchecked") -// @Override -// public D getDefinition() { -// return (D) getInstance().getDefinition(); -// } -// -// @Override -// public I getInstance() { -// return instance; -// } -// -// @Override -// @NonNull -// public Object getValue() { -// return value; -// } -// } diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/AbstractInstanceModel.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/AbstractInstanceModel.java deleted file mode 100644 index 59ac912aa..000000000 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/AbstractInstanceModel.java +++ /dev/null @@ -1,64 +0,0 @@ -/* - * Portions of this software was developed by employees of the National Institute - * of Standards and Technology (NIST), an agency of the Federal Government and is - * being made available as a public service. Pursuant to title 17 United States - * Code Section 105, works of NIST employees are not subject to copyright - * protection in the United States. This software may be subject to foreign - * copyright. Permission in the United States and in foreign countries, to the - * extent that NIST may hold copyright, to use, copy, modify, create derivative - * works, and distribute this software and its documentation without fee is hereby - * granted on a non-exclusive basis, provided that this notice and disclaimer - * of warranty appears in all copies. - * - * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER - * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY - * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM - * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE - * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT - * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, - * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, - * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, - * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR - * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT - * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. - */ - -package gov.nist.secauto.metaschema.databind.model.metaschema.impl; - -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingContainerModelAbsolute; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingDefinitionAssembly; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingInstanceModelAbsolute; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingModule; - -import edu.umd.cs.findbugs.annotations.NonNull; - -public abstract class AbstractInstanceModel - extends AbstractBinding - implements IBindingInstanceModelAbsolute, - IFeatureValueless { - @NonNull - private final PARENT parent; - - protected AbstractInstanceModel( - @NonNull BINDING binding, - @NonNull PARENT parent) { - super(binding); - this.parent = parent; - } - - @Override - public PARENT getParentContainer() { - return parent; - } - - @Override - public IBindingDefinitionAssembly getContainingDefinition() { - return getParentContainer().getOwningDefinition(); - } - - @Override - public IBindingModule getContainingModule() { - return getContainingDefinition().getContainingModule(); - } -} diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/AbstractInstanceModelGrouped.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/AbstractInstanceModelGrouped.java deleted file mode 100644 index f905dca4f..000000000 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/AbstractInstanceModelGrouped.java +++ /dev/null @@ -1,104 +0,0 @@ -/* - * Portions of this software was developed by employees of the National Institute - * of Standards and Technology (NIST), an agency of the Federal Government and is - * being made available as a public service. Pursuant to title 17 United States - * Code Section 105, works of NIST employees are not subject to copyright - * protection in the United States. This software may be subject to foreign - * copyright. Permission in the United States and in foreign countries, to the - * extent that NIST may hold copyright, to use, copy, modify, create derivative - * works, and distribute this software and its documentation without fee is hereby - * granted on a non-exclusive basis, provided that this notice and disclaimer - * of warranty appears in all copies. - * - * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER - * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY - * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM - * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE - * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT - * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, - * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, - * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, - * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR - * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT - * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. - */ - -package gov.nist.secauto.metaschema.databind.model.metaschema.impl; - -import gov.nist.secauto.metaschema.core.metapath.item.node.IAssemblyNodeItem; -import gov.nist.secauto.metaschema.core.metapath.item.node.INodeItem; -import gov.nist.secauto.metaschema.core.model.IAttributable; -import gov.nist.secauto.metaschema.core.util.ObjectUtils; -import gov.nist.secauto.metaschema.databind.model.IBoundInstanceModelGroupedAssembly; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingDefinitionAssembly; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingInstanceModelNamedGrouped; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingModule; -import gov.nist.secauto.metaschema.databind.model.metaschema.IInstanceModelChoiceGroupBinding; -import gov.nist.secauto.metaschema.databind.model.metaschema.binding.Property; - -import java.util.List; -import java.util.Map; -import java.util.Set; - -import edu.umd.cs.findbugs.annotations.NonNull; -import nl.talsmasoftware.lazy4j.Lazy; - -public abstract class AbstractInstanceModelGrouped< - BINDING> - extends AbstractBinding - implements IBindingInstanceModelNamedGrouped, - IFeatureValueless { - @NonNull - private final IInstanceModelChoiceGroupBinding parent; - @NonNull - private final Map> properties; - @NonNull - private final Lazy boundNodeItem; - - protected AbstractInstanceModelGrouped( - @NonNull BINDING binding, - @NonNull IBoundInstanceModelGroupedAssembly bindingInstance, - int position, - @NonNull IInstanceModelChoiceGroupBinding parent, - @NonNull List properties) { - super(binding); - this.parent = parent; - this.properties = ModelSupport.parseProperties(properties); - this.boundNodeItem = ObjectUtils.notNull( - Lazy.lazy(() -> (IAssemblyNodeItem) getContainingDefinition().getBoundNodeItem() - .getModelItemsByName(bindingInstance.getXmlQName()) - .get(position))); - } - - @Override - public IInstanceModelChoiceGroupBinding getParentContainer() { - return parent; - } - - @Override - public Map> getProperties() { - return properties; - } - - @SuppressWarnings("null") - @Override - public INodeItem getBoundNodeItem() { - return boundNodeItem.get(); - } - - @Override - public IBindingDefinitionAssembly getContainingDefinition() { - return getParentContainer().getOwningDefinition(); - } - - @Override - public String getJsonKeyFlagName() { - return getParentContainer().getJsonKeyFlagName(); - } - - @Override - public IBindingModule getContainingModule() { - return getContainingDefinition().getContainingModule(); - } -} diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/AbstractInstanceModelNamed.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/AbstractInstanceModelNamed.java deleted file mode 100644 index 95c2bd3c8..000000000 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/AbstractInstanceModelNamed.java +++ /dev/null @@ -1,118 +0,0 @@ -/* - * Portions of this software was developed by employees of the National Institute - * of Standards and Technology (NIST), an agency of the Federal Government and is - * being made available as a public service. Pursuant to title 17 United States - * Code Section 105, works of NIST employees are not subject to copyright - * protection in the United States. This software may be subject to foreign - * copyright. Permission in the United States and in foreign countries, to the - * extent that NIST may hold copyright, to use, copy, modify, create derivative - * works, and distribute this software and its documentation without fee is hereby - * granted on a non-exclusive basis, provided that this notice and disclaimer - * of warranty appears in all copies. - * - * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER - * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY - * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM - * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE - * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT - * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, - * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, - * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, - * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR - * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT - * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. - */ - -package gov.nist.secauto.metaschema.databind.model.metaschema.impl; - -import gov.nist.secauto.metaschema.core.metapath.item.node.IAssemblyNodeItem; -import gov.nist.secauto.metaschema.core.metapath.item.node.INodeItem; -import gov.nist.secauto.metaschema.core.model.IAttributable; -import gov.nist.secauto.metaschema.core.util.ObjectUtils; -import gov.nist.secauto.metaschema.databind.model.IBoundInstanceModelGroupedAssembly; -import gov.nist.secauto.metaschema.databind.model.IGroupAs; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingContainerModelAbsolute; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingInstanceModelNamedAbsolute; -import gov.nist.secauto.metaschema.databind.model.metaschema.binding.GroupAs; -import gov.nist.secauto.metaschema.databind.model.metaschema.binding.Property; - -import java.util.List; -import java.util.Map; -import java.util.Set; - -import edu.umd.cs.findbugs.annotations.NonNull; -import edu.umd.cs.findbugs.annotations.Nullable; -import nl.talsmasoftware.lazy4j.Lazy; - -/** - * Implements a named model instance associated with a Java object binding. - * - * @param - * the Java type of the associated object binding - * @param - * the Java type of the parent container - */ -public abstract class AbstractInstanceModelNamed< - BINDING, - PARENT extends IBindingContainerModelAbsolute> - extends AbstractInstanceModel - implements IBindingInstanceModelNamedAbsolute, - IFeatureInstanceModelGroupAs { - @NonNull - private final Map> properties; - @NonNull - private final IGroupAs groupAs; - @NonNull - private final Lazy boundNodeItem; - - /** - * Construct a new bound named instance. - * - * @param binding - * the instance object bound to a Java class - * @param bindingInstance - * the Metaschema module instance for the bound object - * @param position - * the zero-based position of this bound object relative to its bound - * object siblings - * @param parent - * the container containing this binding - * @param properties - * the collection of properties associated with this instance, which - * may be empty - * @param groupAs - * the instance grouping information - */ - protected AbstractInstanceModelNamed( - @NonNull BINDING binding, - @NonNull IBoundInstanceModelGroupedAssembly bindingInstance, - int position, - @NonNull PARENT parent, - @NonNull List properties, - @Nullable GroupAs groupAs) { - super(binding, parent); - this.properties = ModelSupport.parseProperties(properties); - this.groupAs = ModelSupport.groupAs(groupAs, parent.getOwningDefinition().getXmlNamespace()); - this.boundNodeItem = ObjectUtils.notNull( - Lazy.lazy(() -> (IAssemblyNodeItem) getContainingDefinition().getBoundNodeItem() - .getModelItemsByName(bindingInstance.getXmlQName()) - .get(position))); - } - - @Override - public Map> getProperties() { - return properties; - } - - @Override - public IGroupAs getGroupAs() { - return groupAs; - } - - @SuppressWarnings("null") - @Override - public INodeItem getBoundNodeItem() { - return boundNodeItem.get(); - } -} diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/AbstractInstanceModelNamedInline.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/AbstractInstanceModelNamedInline.java deleted file mode 100644 index 8d378865e..000000000 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/AbstractInstanceModelNamedInline.java +++ /dev/null @@ -1,71 +0,0 @@ -/* - * Portions of this software was developed by employees of the National Institute - * of Standards and Technology (NIST), an agency of the Federal Government and is - * being made available as a public service. Pursuant to title 17 United States - * Code Section 105, works of NIST employees are not subject to copyright - * protection in the United States. This software may be subject to foreign - * copyright. Permission in the United States and in foreign countries, to the - * extent that NIST may hold copyright, to use, copy, modify, create derivative - * works, and distribute this software and its documentation without fee is hereby - * granted on a non-exclusive basis, provided that this notice and disclaimer - * of warranty appears in all copies. - * - * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER - * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY - * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM - * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE - * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT - * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, - * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, - * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, - * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR - * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT - * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. - */ - -package gov.nist.secauto.metaschema.databind.model.metaschema.impl; - -import gov.nist.secauto.metaschema.databind.model.IBoundInstanceModelGroupedAssembly; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingContainerModelAbsolute; -import gov.nist.secauto.metaschema.databind.model.metaschema.binding.GroupAs; -import gov.nist.secauto.metaschema.databind.model.metaschema.binding.Property; - -import java.util.List; - -import edu.umd.cs.findbugs.annotations.NonNull; -import edu.umd.cs.findbugs.annotations.Nullable; - -public abstract class AbstractInstanceModelNamedInline< - BINDING, - PARENT extends IBindingContainerModelAbsolute> - extends AbstractInstanceModelNamed { - - /** - * Construct a new bound named instance that represents an inline definition. - * - * @param binding - * the instance object bound to a Java class - * @param bindingInstance - * the Metaschema module instance for the bound object - * @param position - * the zero-based position of this bound object relative to its bound - * object siblings - * @param parent - * the container containing this binding - * @param properties - * the collection of properties associated with this instance, which - * may be empty - * @param groupAs - * the instance grouping information - */ - protected AbstractInstanceModelNamedInline( - @NonNull BINDING binding, - @NonNull IBoundInstanceModelGroupedAssembly bindingInstance, - int position, - @NonNull PARENT parent, - @NonNull List properties, - @Nullable GroupAs groupAs) { - super(binding, bindingInstance, position, parent, properties, groupAs); - } -} diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/AbstractInstanceModelNamedReference.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/AbstractInstanceModelNamedReference.java deleted file mode 100644 index 94dbd12e2..000000000 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/AbstractInstanceModelNamedReference.java +++ /dev/null @@ -1,107 +0,0 @@ -/* - * Portions of this software was developed by employees of the National Institute - * of Standards and Technology (NIST), an agency of the Federal Government and is - * being made available as a public service. Pursuant to title 17 United States - * Code Section 105, works of NIST employees are not subject to copyright - * protection in the United States. This software may be subject to foreign - * copyright. Permission in the United States and in foreign countries, to the - * extent that NIST may hold copyright, to use, copy, modify, create derivative - * works, and distribute this software and its documentation without fee is hereby - * granted on a non-exclusive basis, provided that this notice and disclaimer - * of warranty appears in all copies. - * - * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER - * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY - * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM - * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE - * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT - * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, - * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, - * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, - * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR - * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT - * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. - */ - -package gov.nist.secauto.metaschema.databind.model.metaschema.impl; - -import gov.nist.secauto.metaschema.databind.model.IBoundInstanceModelGroupedAssembly; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingContainerModelAbsolute; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingDefinitionModel; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingInstanceFlag; -import gov.nist.secauto.metaschema.databind.model.metaschema.binding.GroupAs; -import gov.nist.secauto.metaschema.databind.model.metaschema.binding.Property; - -import java.util.List; - -import edu.umd.cs.findbugs.annotations.NonNull; -import edu.umd.cs.findbugs.annotations.Nullable; - -/** - * Implements a named model instance, referencing a global definition, - * associated with a Java object binding. - * - * @param - * the Java type of the associated object binding - * @param - * the Java type of the referenced definition - * @param - * the Java type of the parent container - */ -public abstract class AbstractInstanceModelNamedReference< - BINDING, - DEF extends IBindingDefinitionModel, - PARENT extends IBindingContainerModelAbsolute> - extends AbstractInstanceModelNamed { - @NonNull - private final DEF definition; - - /** - * Construct a new bound named instance that references a global definition. - * - * @param binding - * the instance object bound to a Java class - * @param bindingInstance - * the Metaschema module instance for the bound object - * @param position - * the zero-based position of this bound object relative to its bound - * object siblings - * @param definition - * the referenced global definition - * @param parent - * the container containing this binding - * @param properties - * the collection of properties associated with this instance, which - * may be empty - * @param groupAs - * the instance grouping information - */ - protected AbstractInstanceModelNamedReference( - @NonNull BINDING binding, - @NonNull IBoundInstanceModelGroupedAssembly bindingInstance, - int position, - @NonNull DEF definition, - @NonNull PARENT parent, - @NonNull List properties, - @Nullable GroupAs groupAs) { - super(binding, bindingInstance, position, parent, properties, groupAs); - this.definition = definition; - } - - @Override - public DEF getDefinition() { - return definition; - } - - @Override - public String getName() { - return getDefinition().getName(); - } - - @Override - public String getJsonKeyFlagName() { - IBindingInstanceFlag instance = getDefinition().getJsonKeyFlagInstance(); - return instance == null ? null : instance.getName(); - } -} diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/AssemblyModelContainerSupport.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/AssemblyModelContainerSupport.java index 6584aac0e..9fd089a1a 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/AssemblyModelContainerSupport.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/AssemblyModelContainerSupport.java @@ -26,31 +26,20 @@ package gov.nist.secauto.metaschema.databind.model.metaschema.impl; -import java.util.LinkedHashMap; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; - -import javax.xml.namespace.QName; - -import edu.umd.cs.findbugs.annotations.NonNull; -import edu.umd.cs.findbugs.annotations.Nullable; -import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; - import gov.nist.secauto.metaschema.core.metapath.item.node.INodeItemFactory; +import gov.nist.secauto.metaschema.core.model.IAssemblyDefinition; +import gov.nist.secauto.metaschema.core.model.IAssemblyInstanceAbsolute; +import gov.nist.secauto.metaschema.core.model.IChoiceGroupInstance; +import gov.nist.secauto.metaschema.core.model.IChoiceInstance; import gov.nist.secauto.metaschema.core.model.IContainerModelAssemblySupport; +import gov.nist.secauto.metaschema.core.model.IFieldInstanceAbsolute; +import gov.nist.secauto.metaschema.core.model.IModelInstanceAbsolute; +import gov.nist.secauto.metaschema.core.model.INamedModelInstanceAbsolute; import gov.nist.secauto.metaschema.core.util.CollectionUtil; import gov.nist.secauto.metaschema.core.util.ObjectUtils; import gov.nist.secauto.metaschema.databind.model.IBoundInstanceModelAssembly; import gov.nist.secauto.metaschema.databind.model.IBoundInstanceModelChoiceGroup; import gov.nist.secauto.metaschema.databind.model.IBoundInstanceModelGroupedAssembly; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingDefinitionAssembly; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingInstanceModelAbsolute; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingInstanceModelAssemblyAbsolute; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingInstanceModelFieldAbsolute; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingInstanceModelNamedAbsolute; -import gov.nist.secauto.metaschema.databind.model.metaschema.IInstanceModelChoiceBinding; -import gov.nist.secauto.metaschema.databind.model.metaschema.IInstanceModelChoiceGroupBinding; import gov.nist.secauto.metaschema.databind.model.metaschema.binding.AssemblyModel; import gov.nist.secauto.metaschema.databind.model.metaschema.binding.AssemblyModel.Choice; import gov.nist.secauto.metaschema.databind.model.metaschema.binding.AssemblyModel.ChoiceGroup; @@ -59,40 +48,51 @@ import gov.nist.secauto.metaschema.databind.model.metaschema.binding.InlineDefineAssembly; import gov.nist.secauto.metaschema.databind.model.metaschema.binding.InlineDefineField; +import java.util.LinkedHashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; + +import javax.xml.namespace.QName; + +import edu.umd.cs.findbugs.annotations.NonNull; +import edu.umd.cs.findbugs.annotations.Nullable; +import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; + class AssemblyModelContainerSupport extends AbstractBindingModelContainerSupport implements IContainerModelAssemblySupport< - IBindingInstanceModelAbsolute, - IBindingInstanceModelNamedAbsolute, - IBindingInstanceModelFieldAbsolute, - IBindingInstanceModelAssemblyAbsolute, - IInstanceModelChoiceBinding, - IInstanceModelChoiceGroupBinding> { + IModelInstanceAbsolute, + INamedModelInstanceAbsolute, + IFieldInstanceAbsolute, + IAssemblyInstanceAbsolute, + IChoiceInstance, + IChoiceGroupInstance> { @NonNull - private final List modelInstances; + private final List modelInstances; @NonNull - private final Map namedModelInstances; + private final Map namedModelInstances; @NonNull - private final Map fieldInstances; + private final Map fieldInstances; @NonNull - private final Map assemblyInstances; + private final Map assemblyInstances; @NonNull - private final List choiceInstances; + private final List choiceInstances; @NonNull - private final Map choiceGroupInstances; + private final Map choiceGroupInstances; @SuppressWarnings("PMD.ShortMethodName") @SuppressFBWarnings(value = "CT_CONSTRUCTOR_THROW", justification = "Use of final fields") public static IContainerModelAssemblySupport< - IBindingInstanceModelAbsolute, - IBindingInstanceModelNamedAbsolute, - IBindingInstanceModelFieldAbsolute, - IBindingInstanceModelAssemblyAbsolute, - IInstanceModelChoiceBinding, - IInstanceModelChoiceGroupBinding> of( + IModelInstanceAbsolute, + INamedModelInstanceAbsolute, + IFieldInstanceAbsolute, + IAssemblyInstanceAbsolute, + IChoiceInstance, + IChoiceGroupInstance> of( @Nullable AssemblyModel binding, @NonNull IBoundInstanceModelAssembly bindingInstance, - @NonNull IBindingDefinitionAssembly parent, + @NonNull IAssemblyDefinition parent, @NonNull INodeItemFactory nodeItemFactory) { List instances; return binding == null || (instances = binding.getInstances()) == null || instances.isEmpty() @@ -121,16 +121,16 @@ IInstanceModelChoiceGroupBinding> of( protected AssemblyModelContainerSupport( @NonNull AssemblyModel model, @NonNull IBoundInstanceModelAssembly modelInstance, - @NonNull IBindingDefinitionAssembly parent, + @NonNull IAssemblyDefinition parent, @NonNull INodeItemFactory nodeItemFactory) { // create temporary collections to store the child binding objects - final List modelInstances = new LinkedList<>(); - final Map namedModelInstances = new LinkedHashMap<>(); - final Map fieldInstances = new LinkedHashMap<>(); - final Map assemblyInstances = new LinkedHashMap<>(); - final List choiceInstances = new LinkedList<>(); - final Map choiceGroupInstances = new LinkedHashMap<>(); + final List modelInstances = new LinkedList<>(); + final Map namedModelInstances = new LinkedHashMap<>(); + final Map fieldInstances = new LinkedHashMap<>(); + final Map assemblyInstances = new LinkedHashMap<>(); + final List choiceInstances = new LinkedList<>(); + final Map choiceGroupInstances = new LinkedHashMap<>(); // create counters to track child positions int assemblyReferencePosition = 0; @@ -148,14 +148,14 @@ protected AssemblyModelContainerSupport( = (IBoundInstanceModelGroupedAssembly) instance.getItemInstance(obj); if (obj instanceof AssemblyReference) { - IBindingInstanceModelAssemblyAbsolute assembly = newInstance( + IAssemblyInstanceAbsolute assembly = newInstance( (AssemblyReference) obj, objInstance, assemblyReferencePosition++, parent); addInstance(assembly, modelInstances, namedModelInstances, assemblyInstances); } else if (obj instanceof InlineDefineAssembly) { - IBindingInstanceModelAssemblyAbsolute assembly = new InstanceModelAssemblyInline( + IAssemblyInstanceAbsolute assembly = new InstanceModelAssemblyInline( (InlineDefineAssembly) obj, objInstance, assemblyInlineDefinitionPosition++, @@ -163,21 +163,21 @@ protected AssemblyModelContainerSupport( nodeItemFactory); addInstance(assembly, modelInstances, namedModelInstances, assemblyInstances); } else if (obj instanceof FieldReference) { - IBindingInstanceModelFieldAbsolute field = newInstance( + IFieldInstanceAbsolute field = newInstance( (FieldReference) obj, objInstance, fieldReferencePosition++, parent); addInstance(field, modelInstances, namedModelInstances, fieldInstances); } else if (obj instanceof InlineDefineField) { - IBindingInstanceModelFieldAbsolute field = new InstanceModelFieldInline( + IFieldInstanceAbsolute field = new InstanceModelFieldInline( (InlineDefineField) obj, objInstance, fieldInlineDefinitionPosition++, parent); addInstance(field, modelInstances, namedModelInstances, fieldInstances); } else if (obj instanceof AssemblyModel.Choice) { - IInstanceModelChoiceBinding choice = new InstanceModelChoice( + IChoiceInstance choice = new InstanceModelChoice( (Choice) obj, objInstance, choicePosition++, @@ -186,7 +186,7 @@ protected AssemblyModelContainerSupport( modelInstances.add(choice); choiceInstances.add(choice); } else if (obj instanceof AssemblyModel.ChoiceGroup) { - IInstanceModelChoiceGroupBinding choiceGroup = new InstanceModelChoiceGroup( + IChoiceGroupInstance choiceGroup = new InstanceModelChoiceGroup( (ChoiceGroup) obj, objInstance, choiceGroupPosition++, @@ -220,32 +220,32 @@ protected AssemblyModelContainerSupport( } @Override - public List getModelInstances() { + public List getModelInstances() { return modelInstances; } @Override - public Map getNamedModelInstanceMap() { + public Map getNamedModelInstanceMap() { return namedModelInstances; } @Override - public Map getFieldInstanceMap() { + public Map getFieldInstanceMap() { return fieldInstances; } @Override - public Map getAssemblyInstanceMap() { + public Map getAssemblyInstanceMap() { return assemblyInstances; } @Override - public List getChoiceInstances() { + public List getChoiceInstances() { return choiceInstances; } @Override - public Map getChoiceGroupInstanceMap() { + public Map getChoiceGroupInstanceMap() { return choiceGroupInstances; } } diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/BindingModule.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/BindingModule.java index dbf625f31..3c9b5cbb4 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/BindingModule.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/BindingModule.java @@ -31,17 +31,16 @@ import gov.nist.secauto.metaschema.core.metapath.item.node.IDocumentNodeItem; import gov.nist.secauto.metaschema.core.metapath.item.node.INodeItemFactory; import gov.nist.secauto.metaschema.core.model.AbstractModule; -import gov.nist.secauto.metaschema.core.model.IDefinition; +import gov.nist.secauto.metaschema.core.model.IAssemblyDefinition; +import gov.nist.secauto.metaschema.core.model.IFieldDefinition; +import gov.nist.secauto.metaschema.core.model.IFlagDefinition; +import gov.nist.secauto.metaschema.core.model.IMetaschemaModule; +import gov.nist.secauto.metaschema.core.model.IModelDefinition; import gov.nist.secauto.metaschema.core.model.MetaschemaException; import gov.nist.secauto.metaschema.core.util.ObjectUtils; import gov.nist.secauto.metaschema.databind.model.IBoundDefinitionModelAssembly; import gov.nist.secauto.metaschema.databind.model.IBoundInstanceModelChoiceGroup; import gov.nist.secauto.metaschema.databind.model.IBoundInstanceModelGroupedAssembly; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingDefinitionAssembly; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingDefinitionFlag; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingDefinitionModel; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingDefinitionModelField; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingModule; import gov.nist.secauto.metaschema.databind.model.metaschema.binding.METASCHEMA; import java.net.URI; @@ -52,18 +51,20 @@ import java.util.stream.Collectors; import java.util.stream.Stream; +import javax.xml.namespace.QName; + import edu.umd.cs.findbugs.annotations.NonNull; import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; import nl.talsmasoftware.lazy4j.Lazy; public class BindingModule extends AbstractModule< - IBindingModule, - IBindingDefinitionModel, - IBindingDefinitionFlag, - IBindingDefinitionModelField, - IBindingDefinitionAssembly> - implements IBindingModule { + IMetaschemaModule, + IModelDefinition, + IFlagDefinition, + IFieldDefinition, + IAssemblyDefinition> + implements IMetaschemaModule { @NonNull private final URI location; @NonNull @@ -71,15 +72,13 @@ public class BindingModule @NonNull private final Lazy nodeItem; @NonNull - private final Map definitions; - @NonNull - private final Map flagDefinitions; + private final Map flagDefinitions; @NonNull - private final Map fieldDefinitions; + private final Map fieldDefinitions; @NonNull - private final Map assemblyDefinitions; + private final Map assemblyDefinitions; @NonNull - private final Map rootAssemblyDefinitions; + private final Map rootAssemblyDefinitions; /** * Constructs a new Metaschema instance. @@ -101,11 +100,10 @@ public BindingModule( // NOPMD - unavoidable @NonNull URI resource, @NonNull IBoundDefinitionModelAssembly rootDefinition, @NonNull METASCHEMA binding, - @NonNull List importedModules) throws MetaschemaException { + @NonNull List importedModules) throws MetaschemaException { super(importedModules); this.location = ObjectUtils.requireNonNull(resource, "resource"); this.binding = binding; - this.definitions = new LinkedHashMap<>(); this.flagDefinitions = new LinkedHashMap<>(); this.fieldDefinitions = new LinkedHashMap<>(); this.assemblyDefinitions = new LinkedHashMap<>(); @@ -124,35 +122,32 @@ public BindingModule( // NOPMD - unavoidable = (IBoundInstanceModelGroupedAssembly) instance.getItemInstance(obj); if (obj instanceof METASCHEMA.DefineAssembly) { - IBindingDefinitionAssembly definition = new DefinitionAssemblyGlobal( + IAssemblyDefinition definition = new DefinitionAssemblyGlobal( (METASCHEMA.DefineAssembly) obj, objInstance, globalAssemblyPosition++, this, nodeItemFactory); - String name = definition.getName(); - definitions.put(name, definition); + QName name = definition.getDefinitionQName(); assemblyDefinitions.put(name, definition); if (definition.isRoot()) { rootAssemblyDefinitions.put(name, definition); } } else if (obj instanceof METASCHEMA.DefineField) { - IBindingDefinitionModelField definition = new DefinitionFieldGlobal( + IFieldDefinition definition = new DefinitionFieldGlobal( (METASCHEMA.DefineField) obj, objInstance, globalFieldPosition++, this); - String name = definition.getName(); - definitions.put(name, definition); + QName name = definition.getDefinitionQName(); fieldDefinitions.put(name, definition); } else if (obj instanceof METASCHEMA.DefineFlag) { - IBindingDefinitionFlag definition = new DefinitionFlagGlobal( + IFlagDefinition definition = new DefinitionFlagGlobal( (METASCHEMA.DefineFlag) obj, objInstance, globalFlagPosition++, this); - String name = definition.getName(); - definitions.put(name, definition); + QName name = definition.getDefinitionQName(); flagDefinitions.put(name, definition); } else { throw new IllegalStateException( @@ -161,19 +156,17 @@ public BindingModule( // NOPMD - unavoidable resource.toASCIIString())); } } - this.nodeItem = ObjectUtils.notNull(Lazy.lazy(() -> { - return nodeItemFactory.newDocumentNodeItem(rootDefinition, resource, binding); - })); + this.nodeItem + = ObjectUtils.notNull(Lazy.lazy(() -> nodeItemFactory.newDocumentNodeItem(rootDefinition, resource, binding))); } - @Override @NonNull public METASCHEMA getBinding() { return binding; } @Override - public IDocumentNodeItem getBoundNodeItem() { + public IDocumentNodeItem getNodeItem() { return ObjectUtils.notNull(nodeItem.get()); } @@ -234,69 +227,64 @@ public URI getJsonBaseUri() { return retval; } - @NonNull - public Collection getDefinitions() { - return ObjectUtils.notNull(definitions.values()); - } - - private Map getAssemblyDefinitionMap() { + private Map getAssemblyDefinitionMap() { return assemblyDefinitions; } @Override - public Collection getAssemblyDefinitions() { + public Collection getAssemblyDefinitions() { return ObjectUtils.notNull(getAssemblyDefinitionMap().values()); } @Override - public IBindingDefinitionAssembly getAssemblyDefinitionByName(@NonNull String name) { + public IAssemblyDefinition getAssemblyDefinitionByName(@NonNull QName name) { return getAssemblyDefinitionMap().get(name); } - private Map getFieldDefinitionMap() { + private Map getFieldDefinitionMap() { return fieldDefinitions; } @SuppressWarnings("null") @Override - public Collection getFieldDefinitions() { + public Collection getFieldDefinitions() { return getFieldDefinitionMap().values(); } @Override - public IBindingDefinitionModelField getFieldDefinitionByName(@NonNull String name) { + public IFieldDefinition getFieldDefinitionByName(@NonNull QName name) { return getFieldDefinitionMap().get(name); } @SuppressWarnings("null") @Override - public List getAssemblyAndFieldDefinitions() { + public List getAssemblyAndFieldDefinitions() { return Stream.concat(getAssemblyDefinitions().stream(), getFieldDefinitions().stream()) .collect(Collectors.toList()); } - private Map getFlagDefinitionMap() { + private Map getFlagDefinitionMap() { return flagDefinitions; } @SuppressWarnings("null") @Override - public Collection getFlagDefinitions() { + public Collection getFlagDefinitions() { return getFlagDefinitionMap().values(); } @Override - public IBindingDefinitionFlag getFlagDefinitionByName(@NonNull String name) { + public IFlagDefinition getFlagDefinitionByName(@NonNull QName name) { return getFlagDefinitionMap().get(name); } - private Map getRootAssemblyDefinitionMap() { + private Map getRootAssemblyDefinitionMap() { return rootAssemblyDefinitions; } @SuppressWarnings("null") @Override - public Collection getRootAssemblyDefinitions() { + public Collection getRootAssemblyDefinitions() { return getRootAssemblyDefinitionMap().values(); } } diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/ChoiceGroupModelContainerSupport.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/ChoiceGroupModelContainerSupport.java index ac214e16a..9fa52a738 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/ChoiceGroupModelContainerSupport.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/ChoiceGroupModelContainerSupport.java @@ -26,6 +26,21 @@ package gov.nist.secauto.metaschema.databind.model.metaschema.impl; +import gov.nist.secauto.metaschema.core.metapath.item.node.INodeItemFactory; +import gov.nist.secauto.metaschema.core.model.IAssemblyDefinition; +import gov.nist.secauto.metaschema.core.model.IAssemblyInstanceGrouped; +import gov.nist.secauto.metaschema.core.model.IChoiceGroupInstance; +import gov.nist.secauto.metaschema.core.model.IContainerModelSupport; +import gov.nist.secauto.metaschema.core.model.IFieldDefinition; +import gov.nist.secauto.metaschema.core.model.IFieldInstanceGrouped; +import gov.nist.secauto.metaschema.core.model.IModule; +import gov.nist.secauto.metaschema.core.model.INamedModelInstanceGrouped; +import gov.nist.secauto.metaschema.core.util.CollectionUtil; +import gov.nist.secauto.metaschema.core.util.ObjectUtils; +import gov.nist.secauto.metaschema.databind.model.IBoundInstanceModelChoiceGroup; +import gov.nist.secauto.metaschema.databind.model.IBoundInstanceModelGroupedAssembly; +import gov.nist.secauto.metaschema.databind.model.metaschema.binding.AssemblyModel; + import java.util.Collection; import java.util.LinkedHashMap; import java.util.List; @@ -37,43 +52,28 @@ import edu.umd.cs.findbugs.annotations.Nullable; import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; -import gov.nist.secauto.metaschema.core.metapath.item.node.INodeItemFactory; -import gov.nist.secauto.metaschema.core.model.IContainerModelSupport; -import gov.nist.secauto.metaschema.core.util.CollectionUtil; -import gov.nist.secauto.metaschema.core.util.ObjectUtils; -import gov.nist.secauto.metaschema.databind.model.IBoundInstanceModelChoiceGroup; -import gov.nist.secauto.metaschema.databind.model.IBoundInstanceModelGroupedAssembly; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingDefinitionAssembly; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingDefinitionModelField; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingInstanceModelAssemblyGrouped; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingInstanceModelFieldGrouped; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingInstanceModelNamedGrouped; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingModule; -import gov.nist.secauto.metaschema.databind.model.metaschema.IInstanceModelChoiceGroupBinding; -import gov.nist.secauto.metaschema.databind.model.metaschema.binding.AssemblyModel; - class ChoiceGroupModelContainerSupport implements IContainerModelSupport< - IBindingInstanceModelNamedGrouped, - IBindingInstanceModelNamedGrouped, - IBindingInstanceModelFieldGrouped, - IBindingInstanceModelAssemblyGrouped> { + INamedModelInstanceGrouped, + INamedModelInstanceGrouped, + IFieldInstanceGrouped, + IAssemblyInstanceGrouped> { @NonNull - private final Map namedModelInstances; + private final Map namedModelInstances; @NonNull - private final Map fieldInstances; + private final Map fieldInstances; @NonNull - private final Map assemblyInstances; + private final Map assemblyInstances; @SuppressWarnings("PMD.ShortMethodName") public static IContainerModelSupport< - IBindingInstanceModelNamedGrouped, - IBindingInstanceModelNamedGrouped, - IBindingInstanceModelFieldGrouped, - IBindingInstanceModelAssemblyGrouped> of( + INamedModelInstanceGrouped, + INamedModelInstanceGrouped, + IFieldInstanceGrouped, + IAssemblyInstanceGrouped> of( @Nullable AssemblyModel.ChoiceGroup binding, @NonNull IBoundInstanceModelGroupedAssembly bindingInstance, - @NonNull IInstanceModelChoiceGroupBinding parent, + @NonNull IChoiceGroupInstance parent, @NonNull INodeItemFactory nodeItemFactory) { List instances; return binding == null || (instances = binding.getChoices()) == null || instances.isEmpty() @@ -102,13 +102,13 @@ IBindingInstanceModelAssemblyGrouped> of( public ChoiceGroupModelContainerSupport( @NonNull AssemblyModel.ChoiceGroup binding, @NonNull IBoundInstanceModelGroupedAssembly bindingInstance, - @NonNull IInstanceModelChoiceGroupBinding parent, + @NonNull IChoiceGroupInstance parent, @NonNull INodeItemFactory nodeItemFactory) { // create temporary collections to store the child binding objects - final Map namedModelInstances = new LinkedHashMap<>(); - final Map fieldInstances = new LinkedHashMap<>(); - final Map assemblyInstances = new LinkedHashMap<>(); + final Map namedModelInstances = new LinkedHashMap<>(); + final Map fieldInstances = new LinkedHashMap<>(); + final Map assemblyInstances = new LinkedHashMap<>(); // create counters to track child positions int assemblyReferencePosition = 0; @@ -124,14 +124,14 @@ public ChoiceGroupModelContainerSupport( = (IBoundInstanceModelGroupedAssembly) instance.getItemInstance(obj); if (obj instanceof AssemblyModel.ChoiceGroup.Assembly) { - IBindingInstanceModelAssemblyGrouped assembly = newInstance( + IAssemblyInstanceGrouped assembly = newInstance( (AssemblyModel.ChoiceGroup.Assembly) obj, objInstance, assemblyReferencePosition++, parent); addInstance(assembly, namedModelInstances, assemblyInstances); } else if (obj instanceof AssemblyModel.ChoiceGroup.DefineAssembly) { - IBindingInstanceModelAssemblyGrouped assembly = new InstanceModelGroupedAssemblyInline( + IAssemblyInstanceGrouped assembly = new InstanceModelGroupedAssemblyInline( (AssemblyModel.ChoiceGroup.DefineAssembly) obj, objInstance, assemblyInlineDefinitionPosition++, @@ -139,14 +139,14 @@ public ChoiceGroupModelContainerSupport( nodeItemFactory); addInstance(assembly, namedModelInstances, assemblyInstances); } else if (obj instanceof AssemblyModel.ChoiceGroup.Field) { - IBindingInstanceModelFieldGrouped field = newInstance( + IFieldInstanceGrouped field = newInstance( (AssemblyModel.ChoiceGroup.Field) obj, objInstance, fieldReferencePosition++, parent); addInstance(field, namedModelInstances, fieldInstances); } else if (obj instanceof AssemblyModel.ChoiceGroup.DefineField) { - IBindingInstanceModelFieldGrouped field = new InstanceModelGroupedFieldInline( + IFieldInstanceGrouped field = new InstanceModelGroupedFieldInline( (AssemblyModel.ChoiceGroup.DefineField) obj, objInstance, fieldInlineDefinitionPosition++, @@ -170,34 +170,34 @@ public ChoiceGroupModelContainerSupport( } protected static void addInstance( - @NonNull IBindingInstanceModelAssemblyGrouped assembly, - @NonNull Map namedModelInstances, - @NonNull Map assemblyInstances) { + @NonNull IAssemblyInstanceGrouped assembly, + @NonNull Map namedModelInstances, + @NonNull Map assemblyInstances) { QName effectiveName = assembly.getXmlQName(); namedModelInstances.put(effectiveName, assembly); assemblyInstances.put(effectiveName, assembly); } protected static void addInstance( - @NonNull IBindingInstanceModelFieldGrouped field, - @NonNull Map namedModelInstances, - @NonNull Map fieldInstances) { + @NonNull IFieldInstanceGrouped field, + @NonNull Map namedModelInstances, + @NonNull Map fieldInstances) { QName effectiveName = field.getXmlQName(); namedModelInstances.put(effectiveName, field); fieldInstances.put(effectiveName, field); } @NonNull - protected static IBindingInstanceModelAssemblyGrouped newInstance( + protected static IAssemblyInstanceGrouped newInstance( @NonNull AssemblyModel.ChoiceGroup.Assembly obj, @NonNull IBoundInstanceModelGroupedAssembly objInstance, int position, - @NonNull IInstanceModelChoiceGroupBinding parent) { - IBindingDefinitionAssembly owningDefinition = parent.getOwningDefinition(); - IBindingModule module = owningDefinition.getContainingModule(); + @NonNull IChoiceGroupInstance parent) { + IAssemblyDefinition owningDefinition = parent.getOwningDefinition(); + IModule module = owningDefinition.getContainingModule(); - String name = ObjectUtils.requireNonNull(obj.getRef()); - IBindingDefinitionAssembly definition = module.getScopedAssemblyDefinitionByName(name); + QName name = parent.getContainingModule().toModelQName(ObjectUtils.requireNonNull(obj.getRef())); + IAssemblyDefinition definition = module.getScopedAssemblyDefinitionByName(name); if (definition == null) { throw new IllegalStateException( @@ -210,16 +210,16 @@ protected static IBindingInstanceModelAssemblyGrouped newInstance( } @NonNull - protected static IBindingInstanceModelFieldGrouped newInstance( + protected static IFieldInstanceGrouped newInstance( @NonNull AssemblyModel.ChoiceGroup.Field obj, @NonNull IBoundInstanceModelGroupedAssembly objInstance, int position, - @NonNull IInstanceModelChoiceGroupBinding parent) { - IBindingDefinitionAssembly owningDefinition = parent.getOwningDefinition(); - IBindingModule module = owningDefinition.getContainingModule(); + @NonNull IChoiceGroupInstance parent) { + IAssemblyDefinition owningDefinition = parent.getOwningDefinition(); + IModule module = owningDefinition.getContainingModule(); - String name = ObjectUtils.requireNonNull(obj.getRef()); - IBindingDefinitionModelField definition = module.getScopedFieldDefinitionByName(name); + QName name = parent.getContainingModule().toModelQName(ObjectUtils.requireNonNull(obj.getRef())); + IFieldDefinition definition = module.getScopedFieldDefinitionByName(name); if (definition == null) { throw new IllegalStateException( String.format("Unable to resolve field reference '%s' in definition '%s' in module '%s'", @@ -232,22 +232,22 @@ protected static IBindingInstanceModelFieldGrouped newInstance( @SuppressWarnings("null") @Override - public Collection getModelInstances() { + public Collection getModelInstances() { return namedModelInstances.values(); } @Override - public Map getNamedModelInstanceMap() { + public Map getNamedModelInstanceMap() { return namedModelInstances; } @Override - public Map getFieldInstanceMap() { + public Map getFieldInstanceMap() { return fieldInstances; } @Override - public Map getAssemblyInstanceMap() { + public Map getAssemblyInstanceMap() { return assemblyInstances; } } diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/ChoiceModelContainerSupport.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/ChoiceModelContainerSupport.java index 2becf1dc6..cdb9bd0f8 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/ChoiceModelContainerSupport.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/ChoiceModelContainerSupport.java @@ -26,54 +26,54 @@ package gov.nist.secauto.metaschema.databind.model.metaschema.impl; -import java.util.LinkedHashMap; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; - -import javax.xml.namespace.QName; - -import edu.umd.cs.findbugs.annotations.NonNull; -import edu.umd.cs.findbugs.annotations.Nullable; -import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; - import gov.nist.secauto.metaschema.core.metapath.item.node.INodeItemFactory; +import gov.nist.secauto.metaschema.core.model.IAssemblyInstanceAbsolute; +import gov.nist.secauto.metaschema.core.model.IChoiceInstance; import gov.nist.secauto.metaschema.core.model.IContainerModelSupport; +import gov.nist.secauto.metaschema.core.model.IFieldInstanceAbsolute; +import gov.nist.secauto.metaschema.core.model.IModelInstanceAbsolute; +import gov.nist.secauto.metaschema.core.model.INamedModelInstanceAbsolute; import gov.nist.secauto.metaschema.core.util.CollectionUtil; import gov.nist.secauto.metaschema.core.util.ObjectUtils; import gov.nist.secauto.metaschema.databind.model.IBoundInstanceModelChoiceGroup; import gov.nist.secauto.metaschema.databind.model.IBoundInstanceModelGroupedAssembly; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingInstanceModelAbsolute; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingInstanceModelAssemblyAbsolute; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingInstanceModelFieldAbsolute; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingInstanceModelNamedAbsolute; -import gov.nist.secauto.metaschema.databind.model.metaschema.IInstanceModelChoiceBinding; import gov.nist.secauto.metaschema.databind.model.metaschema.binding.AssemblyModel.Choice; import gov.nist.secauto.metaschema.databind.model.metaschema.binding.AssemblyReference; import gov.nist.secauto.metaschema.databind.model.metaschema.binding.FieldReference; import gov.nist.secauto.metaschema.databind.model.metaschema.binding.InlineDefineAssembly; import gov.nist.secauto.metaschema.databind.model.metaschema.binding.InlineDefineField; +import java.util.LinkedHashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; + +import javax.xml.namespace.QName; + +import edu.umd.cs.findbugs.annotations.NonNull; +import edu.umd.cs.findbugs.annotations.Nullable; +import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; + class ChoiceModelContainerSupport extends AbstractBindingModelContainerSupport { @NonNull - private final List modelInstances; + private final List modelInstances; @NonNull - private final Map namedModelInstances; + private final Map namedModelInstances; @NonNull - private final Map fieldInstances; + private final Map fieldInstances; @NonNull - private final Map assemblyInstances; + private final Map assemblyInstances; @SuppressWarnings("PMD.ShortMethodName") public static IContainerModelSupport< - IBindingInstanceModelAbsolute, - IBindingInstanceModelNamedAbsolute, - IBindingInstanceModelFieldAbsolute, - IBindingInstanceModelAssemblyAbsolute> of( + IModelInstanceAbsolute, + INamedModelInstanceAbsolute, + IFieldInstanceAbsolute, + IAssemblyInstanceAbsolute> of( @Nullable Choice binding, @NonNull IBoundInstanceModelGroupedAssembly bindingInstance, - @NonNull IInstanceModelChoiceBinding parent, + @NonNull IChoiceInstance parent, @NonNull INodeItemFactory nodeItemFactory) { List instances; return binding == null || (instances = binding.getChoices()) == null || instances.isEmpty() @@ -102,14 +102,14 @@ IBindingInstanceModelAssemblyAbsolute> of( public ChoiceModelContainerSupport( @NonNull Choice binding, @NonNull IBoundInstanceModelGroupedAssembly bindingInstance, - @NonNull IInstanceModelChoiceBinding parent, + @NonNull IChoiceInstance parent, @NonNull INodeItemFactory nodeItemFactory) { // create temporary collections to store the child binding objects - final List modelInstances = new LinkedList<>(); - final Map namedModelInstances = new LinkedHashMap<>(); - final Map fieldInstances = new LinkedHashMap<>(); - final Map assemblyInstances = new LinkedHashMap<>(); + final List modelInstances = new LinkedList<>(); + final Map namedModelInstances = new LinkedHashMap<>(); + final Map fieldInstances = new LinkedHashMap<>(); + final Map assemblyInstances = new LinkedHashMap<>(); // create counters to track child positions int assemblyReferencePosition = 0; @@ -125,14 +125,14 @@ public ChoiceModelContainerSupport( = (IBoundInstanceModelGroupedAssembly) instance.getItemInstance(obj); if (obj instanceof AssemblyReference) { - IBindingInstanceModelAssemblyAbsolute assembly = newInstance( + IAssemblyInstanceAbsolute assembly = newInstance( (AssemblyReference) obj, objInstance, assemblyReferencePosition++, parent); addInstance(assembly, modelInstances, namedModelInstances, assemblyInstances); } else if (obj instanceof InlineDefineAssembly) { - IBindingInstanceModelAssemblyAbsolute assembly = new InstanceModelAssemblyInline( + IAssemblyInstanceAbsolute assembly = new InstanceModelAssemblyInline( (InlineDefineAssembly) obj, objInstance, assemblyInlineDefinitionPosition++, @@ -140,14 +140,14 @@ public ChoiceModelContainerSupport( nodeItemFactory); addInstance(assembly, modelInstances, namedModelInstances, assemblyInstances); } else if (obj instanceof FieldReference) { - IBindingInstanceModelFieldAbsolute field = newInstance( + IFieldInstanceAbsolute field = newInstance( (FieldReference) obj, objInstance, fieldReferencePosition++, parent); addInstance(field, modelInstances, namedModelInstances, fieldInstances); } else if (obj instanceof InlineDefineField) { - IBindingInstanceModelFieldAbsolute field = new InstanceModelFieldInline( + IFieldInstanceAbsolute field = new InstanceModelFieldInline( (InlineDefineField) obj, objInstance, fieldInlineDefinitionPosition++, @@ -173,22 +173,22 @@ public ChoiceModelContainerSupport( } @Override - public List getModelInstances() { + public List getModelInstances() { return modelInstances; } @Override - public Map getNamedModelInstanceMap() { + public Map getNamedModelInstanceMap() { return namedModelInstances; } @Override - public Map getFieldInstanceMap() { + public Map getFieldInstanceMap() { return fieldInstances; } @Override - public Map getAssemblyInstanceMap() { + public Map getAssemblyInstanceMap() { return assemblyInstances; } } diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/DefinitionAssemblyGlobal.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/DefinitionAssemblyGlobal.java index da31161bc..c17f462d5 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/DefinitionAssemblyGlobal.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/DefinitionAssemblyGlobal.java @@ -29,27 +29,26 @@ import gov.nist.secauto.metaschema.core.datatype.markup.MarkupLine; import gov.nist.secauto.metaschema.core.datatype.markup.MarkupMultiline; import gov.nist.secauto.metaschema.core.metapath.item.node.IAssemblyNodeItem; -import gov.nist.secauto.metaschema.core.metapath.item.node.INodeItem; import gov.nist.secauto.metaschema.core.metapath.item.node.INodeItemFactory; -import gov.nist.secauto.metaschema.core.model.IAssemblyDefinition; +import gov.nist.secauto.metaschema.core.model.AbstractGlobalAssemblyDefinition; +import gov.nist.secauto.metaschema.core.model.IAssemblyInstance; +import gov.nist.secauto.metaschema.core.model.IAssemblyInstanceAbsolute; import gov.nist.secauto.metaschema.core.model.IAttributable; +import gov.nist.secauto.metaschema.core.model.IChoiceGroupInstance; +import gov.nist.secauto.metaschema.core.model.IChoiceInstance; import gov.nist.secauto.metaschema.core.model.IContainerFlagSupport; import gov.nist.secauto.metaschema.core.model.IContainerModelAssemblySupport; +import gov.nist.secauto.metaschema.core.model.IFieldInstanceAbsolute; +import gov.nist.secauto.metaschema.core.model.IFlagInstance; +import gov.nist.secauto.metaschema.core.model.IMetaschemaModule; +import gov.nist.secauto.metaschema.core.model.IModelInstanceAbsolute; +import gov.nist.secauto.metaschema.core.model.INamedModelInstanceAbsolute; import gov.nist.secauto.metaschema.core.model.ModuleScopeEnum; import gov.nist.secauto.metaschema.core.model.constraint.AssemblyConstraintSet; import gov.nist.secauto.metaschema.core.model.constraint.IModelConstrained; import gov.nist.secauto.metaschema.core.model.constraint.ISource; import gov.nist.secauto.metaschema.core.util.ObjectUtils; import gov.nist.secauto.metaschema.databind.model.IBoundInstanceModelGroupedAssembly; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingDefinitionAssembly; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingInstanceFlag; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingInstanceModelAbsolute; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingInstanceModelAssemblyAbsolute; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingInstanceModelFieldAbsolute; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingInstanceModelNamedAbsolute; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingModule; -import gov.nist.secauto.metaschema.databind.model.metaschema.IInstanceModelChoiceBinding; -import gov.nist.secauto.metaschema.databind.model.metaschema.IInstanceModelChoiceGroupBinding; import gov.nist.secauto.metaschema.databind.model.metaschema.binding.AssemblyConstraints; import gov.nist.secauto.metaschema.databind.model.metaschema.binding.JsonKey; import gov.nist.secauto.metaschema.databind.model.metaschema.binding.METASCHEMA; @@ -61,23 +60,30 @@ import nl.talsmasoftware.lazy4j.Lazy; public class DefinitionAssemblyGlobal - extends AbstractDefinition - implements IBindingDefinitionAssembly, - IFeatureBindingContainerFlag, - IFeatureBindingContainerModelAssembly { - + extends AbstractGlobalAssemblyDefinition< + IMetaschemaModule, + IAssemblyInstance, + IFlagInstance, + IModelInstanceAbsolute, + INamedModelInstanceAbsolute, + IFieldInstanceAbsolute, + IAssemblyInstanceAbsolute, + IChoiceInstance, + IChoiceGroupInstance> { + @NonNull + private final METASCHEMA.DefineAssembly binding; @NonNull private final Map> properties; @NonNull - private final Lazy> flagContainer; + private final Lazy> flagContainer; @NonNull private final Lazy> modelContainer; + IModelInstanceAbsolute, + INamedModelInstanceAbsolute, + IFieldInstanceAbsolute, + IAssemblyInstanceAbsolute, + IChoiceInstance, + IChoiceGroupInstance>> modelContainer; @NonNull private final Lazy modelConstraints; @NonNull @@ -103,18 +109,23 @@ public DefinitionAssemblyGlobal( @NonNull METASCHEMA.DefineAssembly binding, @NonNull IBoundInstanceModelGroupedAssembly bindingInstance, int position, - @NonNull IBindingModule module, + @NonNull IMetaschemaModule module, @NonNull INodeItemFactory nodeItemFactory) { - super(binding, module); + super(module); + this.binding = binding; this.properties = ModelSupport.parseProperties(ObjectUtils.requireNonNull(binding.getProps())); - this.flagContainer = ObjectUtils.notNull(Lazy.lazy(() -> FlagContainerSupport.of( - binding.getFlags(), - bindingInstance, - this))); + this.flagContainer = ObjectUtils.notNull(Lazy.lazy(() -> { + JsonKey jsonKey = getBinding().getJsonKey(); + return FlagContainerSupport.newFlagContainer( + binding.getFlags(), + bindingInstance, + this, + jsonKey == null ? null : jsonKey.getFlagRef()); + })); this.modelContainer = ObjectUtils.notNull(Lazy.lazy(() -> AssemblyModelContainerSupport.of( binding.getModel(), ObjectUtils - .requireNonNull(bindingInstance.getDefinition().getAssemblyInstanceByName(IAssemblyDefinition.MODEL_QNAME)), + .requireNonNull(bindingInstance.getDefinition().getAssemblyInstanceByName(MODEL_QNAME)), this, nodeItemFactory))); this.modelConstraints = ObjectUtils.notNull(Lazy.lazy(() -> @@ -127,26 +138,30 @@ public DefinitionAssemblyGlobal( } return retval; })); - this.boundNodeItem = ObjectUtils.notNull(Lazy.lazy(() -> (IAssemblyNodeItem) + this.boundNodeItem = ObjectUtils.notNull(Lazy.lazy(() -> ObjectUtils.requireNonNull(ModelSupport.toNodeItem( + module, + bindingInstance.getXmlQName(), + position)))); + } - getContainingModule().getBoundNodeItem() - .getModelItemsByName(bindingInstance.getXmlQName()) - .get(position))); + @NonNull + protected METASCHEMA.DefineAssembly getBinding() { + return binding; } @Override - public IContainerFlagSupport getFlagContainer() { + public IContainerFlagSupport getFlagContainer() { return ObjectUtils.notNull(flagContainer.get()); } @Override public IContainerModelAssemblySupport< - IBindingInstanceModelAbsolute, - IBindingInstanceModelNamedAbsolute, - IBindingInstanceModelFieldAbsolute, - IBindingInstanceModelAssemblyAbsolute, - IInstanceModelChoiceBinding, - IInstanceModelChoiceGroupBinding> getModelContainer() { + IModelInstanceAbsolute, + INamedModelInstanceAbsolute, + IFieldInstanceAbsolute, + IAssemblyInstanceAbsolute, + IChoiceInstance, + IChoiceGroupInstance> getModelContainer() { return ObjectUtils.notNull(modelContainer.get()); } @@ -155,9 +170,8 @@ public IModelConstrained getConstraintSupport() { return ObjectUtils.notNull(modelConstraints.get()); } - @SuppressWarnings("null") @Override - public INodeItem getBoundNodeItem() { + public IAssemblyNodeItem getNodeItem() { return boundNodeItem.get(); } @@ -166,6 +180,10 @@ public Map> getProperties() { return properties; } + // --------------------------------------- + // - Start binding driven code - CPD-OFF - + // --------------------------------------- + @Override public String getFormalName() { return getBinding().getFormalName(); @@ -206,12 +224,6 @@ public MarkupMultiline getRemarks() { return ModelSupport.remarks(getBinding().getRemarks()); } - @Override - public String getJsonKeyFlagName() { - JsonKey jsonKey = getBinding().getJsonKey(); - return jsonKey == null ? null : jsonKey.getFlagRef(); - } - @Override public boolean isRoot() { return getRootName() != null || getRootIndex() != null; diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/DefinitionFieldGlobal.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/DefinitionFieldGlobal.java index 7911f1fcb..0d17f5acb 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/DefinitionFieldGlobal.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/DefinitionFieldGlobal.java @@ -30,19 +30,18 @@ import gov.nist.secauto.metaschema.core.datatype.markup.MarkupLine; import gov.nist.secauto.metaschema.core.datatype.markup.MarkupMultiline; import gov.nist.secauto.metaschema.core.metapath.item.node.IAssemblyNodeItem; -import gov.nist.secauto.metaschema.core.metapath.item.node.INodeItem; +import gov.nist.secauto.metaschema.core.model.AbstractGlobalFieldDefinition; import gov.nist.secauto.metaschema.core.model.IAttributable; import gov.nist.secauto.metaschema.core.model.IContainerFlagSupport; +import gov.nist.secauto.metaschema.core.model.IFieldInstance; import gov.nist.secauto.metaschema.core.model.IFlagInstance; +import gov.nist.secauto.metaschema.core.model.IMetaschemaModule; import gov.nist.secauto.metaschema.core.model.ModuleScopeEnum; import gov.nist.secauto.metaschema.core.model.constraint.ISource; import gov.nist.secauto.metaschema.core.model.constraint.IValueConstrained; import gov.nist.secauto.metaschema.core.model.constraint.ValueConstraintSet; import gov.nist.secauto.metaschema.core.util.ObjectUtils; import gov.nist.secauto.metaschema.databind.model.IBoundInstanceModelGroupedAssembly; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingDefinitionModelField; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingInstanceFlag; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingModule; import gov.nist.secauto.metaschema.databind.model.metaschema.binding.FieldConstraints; import gov.nist.secauto.metaschema.databind.model.metaschema.binding.JsonKey; import gov.nist.secauto.metaschema.databind.model.metaschema.binding.JsonValueKeyFlag; @@ -51,16 +50,14 @@ import java.util.Map; import java.util.Set; -import javax.xml.namespace.QName; - import edu.umd.cs.findbugs.annotations.NonNull; import edu.umd.cs.findbugs.annotations.Nullable; import nl.talsmasoftware.lazy4j.Lazy; public class DefinitionFieldGlobal - extends AbstractDefinition - implements IBindingDefinitionModelField, - IFeatureBindingContainerFlag { + extends AbstractGlobalFieldDefinition { + @NonNull + private final METASCHEMA.DefineField binding; @NonNull private final Map> properties; @NonNull @@ -68,7 +65,7 @@ public class DefinitionFieldGlobal @Nullable private final Object defaultValue; @NonNull - private final Lazy> flagContainer; + private final Lazy> flagContainer; @NonNull private final Lazy valueConstraints; @NonNull @@ -78,31 +75,41 @@ public DefinitionFieldGlobal( @NonNull METASCHEMA.DefineField binding, @NonNull IBoundInstanceModelGroupedAssembly bindingInstance, int position, - @NonNull IBindingModule module) { - super(binding, module); - this.properties = ModelSupport.parseProperties(ObjectUtils.requireNonNull(getBinding().getProps())); - this.javaTypeAdapter = ModelSupport.dataType(getBinding().getAsType()); - this.defaultValue = ModelSupport.defaultValue(getBinding().getDefault(), this.javaTypeAdapter); - this.flagContainer = ObjectUtils.notNull(Lazy.lazy(() -> FlagContainerSupport.of( - binding.getFlags(), - bindingInstance, - this))); + @NonNull IMetaschemaModule module) { + super(module); + this.binding = binding; + this.properties = ModelSupport.parseProperties(ObjectUtils.requireNonNull(binding.getProps())); + this.javaTypeAdapter = ModelSupport.dataType(binding.getAsType()); + this.defaultValue = ModelSupport.defaultValue(binding.getDefault(), this.javaTypeAdapter); + this.flagContainer = ObjectUtils.notNull(Lazy.lazy(() -> { + JsonKey jsonKey = binding.getJsonKey(); + return FlagContainerSupport.newFlagContainer( + binding.getFlags(), + bindingInstance, + this, + jsonKey == null ? null : jsonKey.getFlagRef()); + })); this.valueConstraints = ObjectUtils.notNull(Lazy.lazy(() -> { IValueConstrained retval = new ValueConstraintSet(); - FieldConstraints constraints = getBinding().getConstraint(); + FieldConstraints constraints = binding.getConstraint(); if (constraints != null) { ConstraintBindingSupport.parse(retval, constraints, ISource.modelSource(module.getLocation())); } return retval; })); - this.boundNodeItem = ObjectUtils.notNull( - Lazy.lazy(() -> (IAssemblyNodeItem) getContainingModule().getBoundNodeItem() - .getModelItemsByName(bindingInstance.getXmlQName()) - .get(position))); + this.boundNodeItem = ObjectUtils.notNull(Lazy.lazy(() -> ObjectUtils.requireNonNull(ModelSupport.toNodeItem( + module, + bindingInstance.getXmlQName(), + position)))); + } + + @NonNull + protected METASCHEMA.DefineField getBinding() { + return binding; } @Override - public IContainerFlagSupport getFlagContainer() { + public IContainerFlagSupport getFlagContainer() { return ObjectUtils.notNull(flagContainer.get()); } @@ -126,6 +133,15 @@ public Object getDefaultValue() { return defaultValue; } + @Override + public IAssemblyNodeItem getNodeItem() { + return boundNodeItem.get(); + } + + // --------------------------------------- + // - Start binding driven code - CPD-OFF - + // --------------------------------------- + @Override public String getFormalName() { return getBinding().getFormalName(); @@ -166,12 +182,6 @@ public MarkupMultiline getRemarks() { return ModelSupport.remarks(getBinding().getRemarks()); } - @Override - public String getJsonKeyFlagName() { - JsonKey jsonKey = getBinding().getJsonKey(); - return jsonKey == null ? null : jsonKey.getFlagRef(); - } - @Override public IFlagInstance getJsonValueKeyFlagInstance() { JsonValueKeyFlag obj = getBinding().getJsonValueKeyFlag(); @@ -186,9 +196,4 @@ public String getJsonValueKeyName() { return getBinding().getJsonValueKey(); } - @SuppressWarnings("null") - @Override - public INodeItem getBoundNodeItem() { - return boundNodeItem.get(); - } } diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/DefinitionFlagGlobal.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/DefinitionFlagGlobal.java index e506fe03d..1b272180b 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/DefinitionFlagGlobal.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/DefinitionFlagGlobal.java @@ -31,15 +31,16 @@ import gov.nist.secauto.metaschema.core.datatype.markup.MarkupMultiline; import gov.nist.secauto.metaschema.core.metapath.item.node.IAssemblyNodeItem; import gov.nist.secauto.metaschema.core.metapath.item.node.INodeItem; +import gov.nist.secauto.metaschema.core.model.AbstractGlobalFlagDefinition; import gov.nist.secauto.metaschema.core.model.IAttributable; +import gov.nist.secauto.metaschema.core.model.IFlagInstance; +import gov.nist.secauto.metaschema.core.model.IMetaschemaModule; import gov.nist.secauto.metaschema.core.model.ModuleScopeEnum; import gov.nist.secauto.metaschema.core.model.constraint.ISource; import gov.nist.secauto.metaschema.core.model.constraint.IValueConstrained; import gov.nist.secauto.metaschema.core.model.constraint.ValueConstraintSet; import gov.nist.secauto.metaschema.core.util.ObjectUtils; import gov.nist.secauto.metaschema.databind.model.IBoundInstanceModelGroupedAssembly; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingDefinitionFlag; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingModule; import gov.nist.secauto.metaschema.databind.model.metaschema.binding.FlagConstraints; import gov.nist.secauto.metaschema.databind.model.metaschema.binding.METASCHEMA; @@ -51,8 +52,9 @@ import nl.talsmasoftware.lazy4j.Lazy; public class DefinitionFlagGlobal - extends AbstractDefinition - implements IBindingDefinitionFlag { + extends AbstractGlobalFlagDefinition { + @NonNull + private final METASCHEMA.DefineFlag binding; @NonNull private final Map> properties; @NonNull @@ -82,24 +84,29 @@ public DefinitionFlagGlobal( @NonNull METASCHEMA.DefineFlag binding, @NonNull IBoundInstanceModelGroupedAssembly bindingInstance, int position, - @NonNull IBindingModule module) { - super(binding, module); - this.properties = ModelSupport.parseProperties(ObjectUtils.requireNonNull(getBinding().getProps())); - this.javaTypeAdapter = ModelSupport.dataType(getBinding().getAsType()); - this.defaultValue = ModelSupport.defaultValue(getBinding().getDefault(), this.javaTypeAdapter); + @NonNull IMetaschemaModule module) { + super(module); + this.binding = binding; + this.properties = ModelSupport.parseProperties(ObjectUtils.requireNonNull(binding.getProps())); + this.javaTypeAdapter = ModelSupport.dataType(binding.getAsType()); + this.defaultValue = ModelSupport.defaultValue(binding.getDefault(), this.javaTypeAdapter); this.valueConstraints = ObjectUtils.notNull(Lazy.lazy(() -> { IValueConstrained retval = new ValueConstraintSet(); - FlagConstraints constraints = getBinding().getConstraint(); + FlagConstraints constraints = binding.getConstraint(); if (constraints != null) { ConstraintBindingSupport.parse(retval, constraints, ISource.modelSource(module.getLocation())); } return retval; })); - this.boundNodeItem = ObjectUtils.notNull( - Lazy.lazy(() -> (IAssemblyNodeItem) getContainingModule().getBoundNodeItem() - .getModelItemsByName(bindingInstance.getXmlQName()) - .get(position))); - // new NodeItem(bindingInstance, position, generator) + this.boundNodeItem = ObjectUtils.notNull(Lazy.lazy(() -> ObjectUtils.requireNonNull(ModelSupport.toNodeItem( + module, + bindingInstance.getXmlQName(), + position)))); + } + + @NonNull + protected METASCHEMA.DefineFlag getBinding() { + return binding; } @Override @@ -162,34 +169,8 @@ public MarkupMultiline getRemarks() { return ModelSupport.remarks(getBinding().getRemarks()); } - @SuppressWarnings("null") @Override - public INodeItem getBoundNodeItem() { + public INodeItem getNodeItem() { return boundNodeItem.get(); } - - // private class NodeItem - // extends AbstractGroupedAssemblyInstanceNodeItem { - // private NodeItem( - // @NonNull IAssemblyInstanceGrouped instance, - // int position, - // @NonNull INodeItemGenerator generator) { - // super(instance, position, generator); - // } - // - // @Override - // public IAssemblyNodeItem getParentNodeItem() { - // return getContainingModule().getBoundNodeItem(); - // } - // - // @Override - // public IAssemblyNodeItem getParentContentNodeItem() { - // return getParentNodeItem(); - // } - // - // @Override - // public METASCHEMA.DefineFlag getValue() { - // return getBinding(); - // } - // } } diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/FlagContainerSupport.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/FlagContainerSupport.java index 653b6f51e..84b5c6a89 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/FlagContainerSupport.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/FlagContainerSupport.java @@ -26,59 +26,42 @@ package gov.nist.secauto.metaschema.databind.model.metaschema.impl; -import java.util.LinkedHashMap; -import java.util.List; -import java.util.Map; - -import javax.xml.namespace.QName; - -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; - -import edu.umd.cs.findbugs.annotations.NonNull; -import edu.umd.cs.findbugs.annotations.Nullable; -import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; - import gov.nist.secauto.metaschema.core.model.IContainerFlagSupport; +import gov.nist.secauto.metaschema.core.model.IFlagContainerBuilder; +import gov.nist.secauto.metaschema.core.model.IFlagDefinition; +import gov.nist.secauto.metaschema.core.model.IFlagInstance; import gov.nist.secauto.metaschema.core.model.IModelDefinition; import gov.nist.secauto.metaschema.core.model.IModule; -import gov.nist.secauto.metaschema.core.util.CollectionUtil; import gov.nist.secauto.metaschema.core.util.ObjectUtils; import gov.nist.secauto.metaschema.databind.model.IBoundInstanceModelChoiceGroup; import gov.nist.secauto.metaschema.databind.model.IBoundInstanceModelGroupedAssembly; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingDefinitionFlag; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingDefinitionModel; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingInstanceFlag; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingModule; import gov.nist.secauto.metaschema.databind.model.metaschema.binding.FlagReference; import gov.nist.secauto.metaschema.databind.model.metaschema.binding.InlineDefineFlag; -public class FlagContainerSupport implements IContainerFlagSupport { - private static final Logger LOGGER = LogManager.getLogger(FlagContainerSupport.class); - @NonNull - private final Map flagInstances; +import java.util.List; - @SuppressWarnings("PMD.ShortMethodName") - public static IContainerFlagSupport of( - @Nullable List flags, - @NonNull IBoundInstanceModelGroupedAssembly bindingInstance, - @NonNull IBindingDefinitionModel parent) { - return flags == null || flags.isEmpty() - ? IContainerFlagSupport.empty() - : new FlagContainerSupport( - flags, - bindingInstance, - parent); - } +import javax.xml.namespace.QName; + +import edu.umd.cs.findbugs.annotations.NonNull; +import edu.umd.cs.findbugs.annotations.Nullable; +import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; - @SuppressWarnings("PMD.UseConcurrentHashMap") +public final class FlagContainerSupport { @SuppressFBWarnings(value = "CT_CONSTRUCTOR_THROW", justification = "Use of final fields") - public FlagContainerSupport( - @NonNull List flags, + @NonNull + public static IContainerFlagSupport newFlagContainer( + @Nullable List flags, @NonNull IBoundInstanceModelGroupedAssembly bindingInstance, - @NonNull IBindingDefinitionModel parent) { + @NonNull IModelDefinition parent, + @Nullable String jsonKeyName) { + if (flags == null || flags.isEmpty()) { + return IContainerFlagSupport.empty(); + } + // create temporary collections to store the child binding objects - final Map flagInstances = new LinkedHashMap<>(); + IFlagContainerBuilder builder = jsonKeyName == null + ? IContainerFlagSupport.builder() + : IContainerFlagSupport.builder(parent.getContainingModule().toFlagQName(jsonKeyName)); // create counter to track child positions int flagReferencePosition = 0; @@ -90,9 +73,9 @@ public FlagContainerSupport( IBoundInstanceModelGroupedAssembly objInstance = (IBoundInstanceModelGroupedAssembly) instance.getItemInstance(obj); - IBindingInstanceFlag flag; + IFlagInstance flag; if (obj instanceof InlineDefineFlag) { - flag = newFlagInstance( + flag = new InstanceFlagInline( (InlineDefineFlag) obj, objInstance, flagInlineDefinitionPosition++, @@ -107,56 +90,34 @@ public FlagContainerSupport( throw new UnsupportedOperationException(String.format("Unknown flag instance class: %s", obj.getClass())); } - String key = flag.getEffectiveName(); - flagInstances.merge(flag.getXmlQName(), flag, (v1, v2) -> { - if (LOGGER.isErrorEnabled()) { - IModelDefinition owningDefinition = v1.getContainingDefinition(); - IModule module = owningDefinition.getContainingModule(); - LOGGER.error( - String.format( - "Unexpected duplicate flag instance name '%s' in definition '%s' in module name '%s' at '%s'", - key, - owningDefinition.getName(), - module.getShortName(), - module.getLocation())); - } - return ObjectUtils.notNull(v2); - }); + builder.flag(flag); } - this.flagInstances = flagInstances.isEmpty() - ? CollectionUtil.emptyMap() - : CollectionUtil.unmodifiableMap(flagInstances); - } - - @Override - public Map getFlagInstanceMap() { - return flagInstances; + return builder.build(); } - protected static final IBindingInstanceFlag newFlagInstance( - @NonNull InlineDefineFlag obj, - @NonNull IBoundInstanceModelGroupedAssembly objInstance, - int position, - @NonNull IBindingDefinitionModel parent) { - return new InstanceFlagInline(obj, objInstance, position, parent); - } - - protected static final IBindingInstanceFlag newFlagInstance( + @NonNull + private static IFlagInstance newFlagInstance( @NonNull FlagReference obj, @NonNull IBoundInstanceModelGroupedAssembly objInstance, int position, - @NonNull IBindingDefinitionModel parent) { - String flagName = ObjectUtils.requireNonNull(obj.getRef()); - IBindingModule module = parent.getContainingModule(); - IBindingDefinitionFlag definition = module.getScopedFlagDefinitionByName(flagName); + @NonNull IModelDefinition parent) { + IModule module = parent.getContainingModule(); + + QName qname = module.toFlagQName(ObjectUtils.requireNonNull(obj.getRef())); + IFlagDefinition definition = module.getScopedFlagDefinitionByName(qname); if (definition == null) { throw new IllegalStateException( String.format("Unable to resolve flag reference '%s' in definition '%s' in module '%s'", - flagName, + qname, parent.getName(), module.getShortName())); } return new InstanceFlagReference(obj, objInstance, position, definition, parent); } + + private FlagContainerSupport() { + // disable construction + } + } diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/GroupAsImpl.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/GroupAsImpl.java index d3ce622cc..0538373bb 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/GroupAsImpl.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/GroupAsImpl.java @@ -26,40 +26,34 @@ package gov.nist.secauto.metaschema.databind.model.metaschema.impl; +import gov.nist.secauto.metaschema.core.model.IModule; import gov.nist.secauto.metaschema.core.model.JsonGroupAsBehavior; import gov.nist.secauto.metaschema.core.model.XmlGroupAsBehavior; import gov.nist.secauto.metaschema.core.util.ObjectUtils; import gov.nist.secauto.metaschema.databind.model.IGroupAs; import gov.nist.secauto.metaschema.databind.model.metaschema.binding.GroupAs; +import javax.xml.namespace.QName; + import edu.umd.cs.findbugs.annotations.NonNull; -import edu.umd.cs.findbugs.annotations.Nullable; class GroupAsImpl implements IGroupAs { @NonNull - private final String name; - @Nullable - private final String namespace; + private final QName qname; @NonNull private final JsonGroupAsBehavior jsonBehavior; @NonNull private final XmlGroupAsBehavior xmlBehavior; - public GroupAsImpl(@NonNull GroupAs groupAs, @Nullable String namespace) { - this.name = ObjectUtils.requireNonNull(groupAs.getName()); - this.namespace = namespace; + public GroupAsImpl(@NonNull GroupAs groupAs, @NonNull IModule module) { + this.qname = module.toModelQName(ObjectUtils.requireNonNull(groupAs.getName())); this.jsonBehavior = ModelSupport.groupAsJsonBehavior(groupAs.getInJson()); this.xmlBehavior = ModelSupport.groupAsXmlBehavior(groupAs.getInXml()); } @Override - public String getGroupAsName() { - return name; - } - - @Override - public String getGroupAsXmlNamespace() { - return namespace; + public QName getGroupAsQName() { + return qname; } @Override diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/IFeatureBindingContainerModel.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/IFeatureBindingContainerModel.java index 63be50118..cf44f4ada 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/IFeatureBindingContainerModel.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/IFeatureBindingContainerModel.java @@ -26,68 +26,69 @@ package gov.nist.secauto.metaschema.databind.model.metaschema.impl; +import gov.nist.secauto.metaschema.core.model.IAssemblyInstanceAbsolute; +import gov.nist.secauto.metaschema.core.model.IContainerModelAbsolute; +import gov.nist.secauto.metaschema.core.model.IContainerModelSupport; +import gov.nist.secauto.metaschema.core.model.IFeatureContainerModel; +import gov.nist.secauto.metaschema.core.model.IFieldInstanceAbsolute; +import gov.nist.secauto.metaschema.core.model.IModelInstanceAbsolute; +import gov.nist.secauto.metaschema.core.model.INamedModelInstanceAbsolute; + import java.util.Collection; import javax.xml.namespace.QName; import edu.umd.cs.findbugs.annotations.NonNull; -import gov.nist.secauto.metaschema.core.model.IContainerModelSupport; -import gov.nist.secauto.metaschema.core.model.IFeatureContainerModel; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingContainerModelAbsolute; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingInstanceModelAbsolute; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingInstanceModelAssemblyAbsolute; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingInstanceModelFieldAbsolute; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingInstanceModelNamedAbsolute; public interface IFeatureBindingContainerModel - extends IBindingContainerModelAbsolute, IFeatureContainerModel< - IBindingInstanceModelAbsolute, - IBindingInstanceModelNamedAbsolute, - IBindingInstanceModelFieldAbsolute, - IBindingInstanceModelAssemblyAbsolute> { + extends IContainerModelAbsolute, IFeatureContainerModel< + IModelInstanceAbsolute, + INamedModelInstanceAbsolute, + IFieldInstanceAbsolute, + IAssemblyInstanceAbsolute> { @Override @NonNull IContainerModelSupport< - IBindingInstanceModelAbsolute, - IBindingInstanceModelNamedAbsolute, - IBindingInstanceModelFieldAbsolute, - IBindingInstanceModelAssemblyAbsolute> getModelContainer(); + IModelInstanceAbsolute, + INamedModelInstanceAbsolute, + IFieldInstanceAbsolute, + IAssemblyInstanceAbsolute> getModelContainer(); @Override - default Collection getModelInstances() { + default Collection getModelInstances() { return getModelContainer().getModelInstances(); } @Override - default IBindingInstanceModelNamedAbsolute getNamedModelInstanceByName(QName name) { + default INamedModelInstanceAbsolute getNamedModelInstanceByName(QName name) { return getModelContainer().getNamedModelInstanceMap().get(name); } @SuppressWarnings("null") @Override - default Collection getNamedModelInstances() { + default Collection getNamedModelInstances() { return getModelContainer().getNamedModelInstanceMap().values(); } @Override - default IBindingInstanceModelFieldAbsolute getFieldInstanceByName(QName name) { + default IFieldInstanceAbsolute getFieldInstanceByName(QName name) { return getModelContainer().getFieldInstanceMap().get(name); } @SuppressWarnings("null") @Override - default Collection getFieldInstances() { + default Collection getFieldInstances() { return getModelContainer().getFieldInstanceMap().values(); } @Override - default IBindingInstanceModelAssemblyAbsolute getAssemblyInstanceByName(QName name) { + default IAssemblyInstanceAbsolute getAssemblyInstanceByName(QName name) { return getModelContainer().getAssemblyInstanceMap().get(name); } @SuppressWarnings("null") @Override - default Collection getAssemblyInstances() { + default Collection getAssemblyInstances() { return getModelContainer().getAssemblyInstanceMap().values(); } } diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/IFeatureBindingContainerModelAssembly.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/IFeatureBindingContainerModelAssembly.java index 96a682e3d..cb2074d2e 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/IFeatureBindingContainerModelAssembly.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/IFeatureBindingContainerModelAssembly.java @@ -26,6 +26,15 @@ package gov.nist.secauto.metaschema.databind.model.metaschema.impl; +import gov.nist.secauto.metaschema.core.model.IAssemblyInstanceAbsolute; +import gov.nist.secauto.metaschema.core.model.IChoiceGroupInstance; +import gov.nist.secauto.metaschema.core.model.IChoiceInstance; +import gov.nist.secauto.metaschema.core.model.IContainerModelAssemblySupport; +import gov.nist.secauto.metaschema.core.model.IFeatureContainerModelAssembly; +import gov.nist.secauto.metaschema.core.model.IFieldInstanceAbsolute; +import gov.nist.secauto.metaschema.core.model.IModelInstanceAbsolute; +import gov.nist.secauto.metaschema.core.model.INamedModelInstanceAbsolute; + import java.util.Collection; import java.util.List; import java.util.Map; @@ -33,86 +42,76 @@ import javax.xml.namespace.QName; import edu.umd.cs.findbugs.annotations.NonNull; -import gov.nist.secauto.metaschema.core.model.IContainerModelAssemblySupport; -import gov.nist.secauto.metaschema.core.model.IFeatureContainerModelAssembly; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingContainerModelAssembly; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingInstanceModelAbsolute; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingInstanceModelAssemblyAbsolute; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingInstanceModelFieldAbsolute; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingInstanceModelNamedAbsolute; -import gov.nist.secauto.metaschema.databind.model.metaschema.IInstanceModelChoiceBinding; -import gov.nist.secauto.metaschema.databind.model.metaschema.IInstanceModelChoiceGroupBinding; public interface IFeatureBindingContainerModelAssembly - extends IBindingContainerModelAssembly, - IFeatureBindingContainerModel, + extends IFeatureBindingContainerModel, IFeatureContainerModelAssembly< - IBindingInstanceModelAbsolute, - IBindingInstanceModelNamedAbsolute, - IBindingInstanceModelFieldAbsolute, - IBindingInstanceModelAssemblyAbsolute, - IInstanceModelChoiceBinding, - IInstanceModelChoiceGroupBinding> { + IModelInstanceAbsolute, + INamedModelInstanceAbsolute, + IFieldInstanceAbsolute, + IAssemblyInstanceAbsolute, + IChoiceInstance, + IChoiceGroupInstance> { @Override @NonNull IContainerModelAssemblySupport< - IBindingInstanceModelAbsolute, - IBindingInstanceModelNamedAbsolute, - IBindingInstanceModelFieldAbsolute, - IBindingInstanceModelAssemblyAbsolute, - IInstanceModelChoiceBinding, - IInstanceModelChoiceGroupBinding> getModelContainer(); + IModelInstanceAbsolute, + INamedModelInstanceAbsolute, + IFieldInstanceAbsolute, + IAssemblyInstanceAbsolute, + IChoiceInstance, + IChoiceGroupInstance> getModelContainer(); @Override - default Collection getModelInstances() { + default Collection getModelInstances() { return getModelContainer().getModelInstances(); } @Override - default IBindingInstanceModelNamedAbsolute getNamedModelInstanceByName(QName name) { + default INamedModelInstanceAbsolute getNamedModelInstanceByName(QName name) { return getModelContainer().getNamedModelInstanceMap().get(name); } @SuppressWarnings("null") @Override - default Collection getNamedModelInstances() { + default Collection getNamedModelInstances() { return getModelContainer().getNamedModelInstanceMap().values(); } @Override - default IBindingInstanceModelFieldAbsolute getFieldInstanceByName(QName name) { + default IFieldInstanceAbsolute getFieldInstanceByName(QName name) { return getModelContainer().getFieldInstanceMap().get(name); } @SuppressWarnings("null") @Override - default Collection getFieldInstances() { + default Collection getFieldInstances() { return getModelContainer().getFieldInstanceMap().values(); } @Override - default IBindingInstanceModelAssemblyAbsolute getAssemblyInstanceByName(QName name) { + default IAssemblyInstanceAbsolute getAssemblyInstanceByName(QName name) { return getModelContainer().getAssemblyInstanceMap().get(name); } @SuppressWarnings("null") @Override - default Collection getAssemblyInstances() { + default Collection getAssemblyInstances() { return getModelContainer().getAssemblyInstanceMap().values(); } @Override - default List getChoiceInstances() { + default List getChoiceInstances() { return getModelContainer().getChoiceInstances(); } @Override - default IInstanceModelChoiceGroupBinding getChoiceGroupInstanceByName(String name) { + default IChoiceGroupInstance getChoiceGroupInstanceByName(String name) { return getModelContainer().getChoiceGroupInstanceMap().get(name); } @Override - default Map getChoiceGroupInstances() { + default Map getChoiceGroupInstances() { return getModelContainer().getChoiceGroupInstanceMap(); } } diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/IFeatureInstanceModelGroupAs.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/IFeatureInstanceModelGroupAs.java index b724b7825..3e98330c2 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/IFeatureInstanceModelGroupAs.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/IFeatureInstanceModelGroupAs.java @@ -26,14 +26,14 @@ package gov.nist.secauto.metaschema.databind.model.metaschema.impl; +import gov.nist.secauto.metaschema.core.model.IModelInstanceAbsolute; import gov.nist.secauto.metaschema.core.model.JsonGroupAsBehavior; import gov.nist.secauto.metaschema.core.model.XmlGroupAsBehavior; import gov.nist.secauto.metaschema.databind.model.IGroupAs; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingInstanceModelAbsolute; import edu.umd.cs.findbugs.annotations.NonNull; -public interface IFeatureInstanceModelGroupAs extends IBindingInstanceModelAbsolute { +public interface IFeatureInstanceModelGroupAs extends IModelInstanceAbsolute { @NonNull IGroupAs getGroupAs(); diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/InstanceFlagInline.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/InstanceFlagInline.java index afac75db4..7eef0377f 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/InstanceFlagInline.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/InstanceFlagInline.java @@ -30,17 +30,17 @@ import gov.nist.secauto.metaschema.core.datatype.markup.MarkupLine; import gov.nist.secauto.metaschema.core.datatype.markup.MarkupMultiline; import gov.nist.secauto.metaschema.core.metapath.item.node.IAssemblyNodeItem; -import gov.nist.secauto.metaschema.core.metapath.item.node.INodeItem; +import gov.nist.secauto.metaschema.core.model.AbstractInlineFlagDefinition; import gov.nist.secauto.metaschema.core.model.IAttributable; -import gov.nist.secauto.metaschema.core.model.IFeatureDefinitionInstanceInlined; +import gov.nist.secauto.metaschema.core.model.IFeatureValueless; +import gov.nist.secauto.metaschema.core.model.IFlagDefinition; +import gov.nist.secauto.metaschema.core.model.IFlagInstance; +import gov.nist.secauto.metaschema.core.model.IModelDefinition; import gov.nist.secauto.metaschema.core.model.constraint.ISource; import gov.nist.secauto.metaschema.core.model.constraint.IValueConstrained; import gov.nist.secauto.metaschema.core.model.constraint.ValueConstraintSet; import gov.nist.secauto.metaschema.core.util.ObjectUtils; import gov.nist.secauto.metaschema.databind.model.IBoundInstanceModelGroupedAssembly; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingDefinitionFlag; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingDefinitionModel; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingInstanceFlag; import gov.nist.secauto.metaschema.databind.model.metaschema.binding.FlagConstraints; import gov.nist.secauto.metaschema.databind.model.metaschema.binding.InlineDefineFlag; @@ -52,9 +52,13 @@ import nl.talsmasoftware.lazy4j.Lazy; public class InstanceFlagInline - extends AbstractInstance - implements IBindingInstanceFlag, IBindingDefinitionFlag, - IFeatureDefinitionInstanceInlined { + extends AbstractInlineFlagDefinition< + IModelDefinition, + IFlagDefinition, + IFlagInstance> + implements IFeatureValueless { + @NonNull + private final InlineDefineFlag binding; @NonNull private final Map> properties; @NonNull @@ -70,34 +74,39 @@ public InstanceFlagInline( @NonNull InlineDefineFlag binding, @NonNull IBoundInstanceModelGroupedAssembly bindingInstance, int position, - @NonNull IBindingDefinitionModel parent) { - super(binding, parent); - this.properties = ModelSupport.parseProperties(ObjectUtils.requireNonNull(getBinding().getProps())); - this.javaTypeAdapter = ModelSupport.dataType(getBinding().getAsType()); - this.defaultValue = ModelSupport.defaultValue(getBinding().getDefault(), this.javaTypeAdapter); + @NonNull IModelDefinition parent) { + super(parent); + this.binding = binding; + this.properties = ModelSupport.parseProperties(ObjectUtils.requireNonNull(binding.getProps())); + this.javaTypeAdapter = ModelSupport.dataType(binding.getAsType()); + this.defaultValue = ModelSupport.defaultValue(binding.getDefault(), this.javaTypeAdapter); this.valueConstraints = ObjectUtils.notNull(Lazy.lazy(() -> { IValueConstrained retval = new ValueConstraintSet(); - FlagConstraints constraints = getBinding().getConstraint(); + FlagConstraints constraints = binding.getConstraint(); if (constraints != null) { ConstraintBindingSupport.parse(retval, constraints, ISource.modelSource()); } return retval; })); this.boundNodeItem = ObjectUtils.notNull( - Lazy.lazy(() -> (IAssemblyNodeItem) getContainingModule().getBoundNodeItem() + Lazy.lazy(() -> (IAssemblyNodeItem) ObjectUtils.notNull(getContainingModule().getNodeItem()) .getModelItemsByName(bindingInstance.getXmlQName()) .get(position))); } + @NonNull + protected InlineDefineFlag getBinding() { + return binding; + } + @SuppressWarnings("null") @Override public IValueConstrained getConstraintSupport() { return valueConstraints.get(); } - @SuppressWarnings("null") @Override - public INodeItem getBoundNodeItem() { + public IAssemblyNodeItem getNodeItem() { return boundNodeItem.get(); } @@ -116,16 +125,6 @@ public Object getDefaultValue() { return defaultValue; } - @Override - public IBindingInstanceFlag getInlineInstance() { - return this; - } - - @Override - public IBindingDefinitionFlag getDefinition() { - return this; - } - @Override public String getName() { return ObjectUtils.notNull(getBinding().getName()); diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/InstanceFlagReference.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/InstanceFlagReference.java index 7710abebe..e63d35245 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/InstanceFlagReference.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/InstanceFlagReference.java @@ -29,14 +29,14 @@ import gov.nist.secauto.metaschema.core.datatype.markup.MarkupLine; import gov.nist.secauto.metaschema.core.datatype.markup.MarkupMultiline; import gov.nist.secauto.metaschema.core.metapath.item.node.IAssemblyNodeItem; -import gov.nist.secauto.metaschema.core.metapath.item.node.INodeItem; +import gov.nist.secauto.metaschema.core.model.AbstractFlagInstance; import gov.nist.secauto.metaschema.core.model.IAttributable; -import gov.nist.secauto.metaschema.core.model.IFeatureDefinitionReferenceInstance; +import gov.nist.secauto.metaschema.core.model.IFeatureValueless; +import gov.nist.secauto.metaschema.core.model.IFlagDefinition; +import gov.nist.secauto.metaschema.core.model.IFlagInstance; +import gov.nist.secauto.metaschema.core.model.IModelDefinition; import gov.nist.secauto.metaschema.core.util.ObjectUtils; import gov.nist.secauto.metaschema.databind.model.IBoundInstanceModelGroupedAssembly; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingDefinitionFlag; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingDefinitionModel; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingInstanceFlag; import gov.nist.secauto.metaschema.databind.model.metaschema.binding.FlagReference; import java.util.Map; @@ -47,11 +47,14 @@ import nl.talsmasoftware.lazy4j.Lazy; public class InstanceFlagReference - extends AbstractInstance - implements IBindingInstanceFlag, - IFeatureDefinitionReferenceInstance { + extends AbstractFlagInstance< + IModelDefinition, + IFlagDefinition, IFlagInstance> + implements IFeatureValueless { @NonNull - private final IBindingDefinitionFlag definition; + private final FlagReference binding; + @NonNull + private final IFlagDefinition definition; @NonNull private final Map> properties; @Nullable @@ -60,29 +63,34 @@ public class InstanceFlagReference private final Lazy boundNodeItem; public InstanceFlagReference( - @NonNull FlagReference obj, + @NonNull FlagReference binding, @NonNull IBoundInstanceModelGroupedAssembly bindingInstance, int position, - @NonNull IBindingDefinitionFlag definition, - @NonNull IBindingDefinitionModel parent) { - super(obj, parent); + @NonNull IFlagDefinition definition, + @NonNull IModelDefinition parent) { + super(parent); + this.binding = binding; this.definition = definition; - this.properties = ModelSupport.parseProperties(ObjectUtils.requireNonNull(getBinding().getProps())); - this.defaultValue = ModelSupport.defaultValue(getBinding().getDefault(), definition.getJavaTypeAdapter()); + this.properties = ModelSupport.parseProperties(ObjectUtils.requireNonNull(binding.getProps())); + this.defaultValue = ModelSupport.defaultValue(binding.getDefault(), definition.getJavaTypeAdapter()); this.boundNodeItem = ObjectUtils.notNull( - Lazy.lazy(() -> (IAssemblyNodeItem) getContainingModule().getBoundNodeItem() + Lazy.lazy(() -> (IAssemblyNodeItem) ObjectUtils.notNull(getContainingModule().getNodeItem()) .getModelItemsByName(bindingInstance.getXmlQName()) .get(position))); } - @SuppressWarnings("null") + @NonNull + protected FlagReference getBinding() { + return binding; + } + @Override - public INodeItem getBoundNodeItem() { + public IAssemblyNodeItem getNodeItem() { return boundNodeItem.get(); } @Override - public IBindingDefinitionFlag getDefinition() { + public IFlagDefinition getDefinition() { return definition; } diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/InstanceModelAssemblyInline.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/InstanceModelAssemblyInline.java index 9cc18c5c9..357f25762 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/InstanceModelAssemblyInline.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/InstanceModelAssemblyInline.java @@ -28,53 +28,71 @@ import gov.nist.secauto.metaschema.core.datatype.markup.MarkupLine; import gov.nist.secauto.metaschema.core.datatype.markup.MarkupMultiline; +import gov.nist.secauto.metaschema.core.metapath.item.node.IAssemblyNodeItem; import gov.nist.secauto.metaschema.core.metapath.item.node.INodeItemFactory; +import gov.nist.secauto.metaschema.core.model.AbstractInlineAssemblyDefinition; import gov.nist.secauto.metaschema.core.model.IAssemblyDefinition; +import gov.nist.secauto.metaschema.core.model.IAssemblyInstanceAbsolute; +import gov.nist.secauto.metaschema.core.model.IAttributable; +import gov.nist.secauto.metaschema.core.model.IChoiceGroupInstance; +import gov.nist.secauto.metaschema.core.model.IChoiceInstance; import gov.nist.secauto.metaschema.core.model.IContainerFlagSupport; +import gov.nist.secauto.metaschema.core.model.IContainerModelAbsolute; import gov.nist.secauto.metaschema.core.model.IContainerModelAssemblySupport; -import gov.nist.secauto.metaschema.core.model.IFeatureDefinitionInstanceInlined; -import gov.nist.secauto.metaschema.core.model.IGroupable; +import gov.nist.secauto.metaschema.core.model.IFieldInstanceAbsolute; +import gov.nist.secauto.metaschema.core.model.IFlagInstance; +import gov.nist.secauto.metaschema.core.model.IModelInstanceAbsolute; +import gov.nist.secauto.metaschema.core.model.INamedModelInstanceAbsolute; import gov.nist.secauto.metaschema.core.model.constraint.AssemblyConstraintSet; import gov.nist.secauto.metaschema.core.model.constraint.IModelConstrained; import gov.nist.secauto.metaschema.core.model.constraint.ISource; import gov.nist.secauto.metaschema.core.util.ObjectUtils; import gov.nist.secauto.metaschema.databind.model.IBoundInstanceModelGroupedAssembly; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingContainerModelAbsolute; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingDefinitionAssembly; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingInstanceFlag; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingInstanceModelAbsolute; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingInstanceModelAssemblyAbsolute; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingInstanceModelFieldAbsolute; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingInstanceModelNamedAbsolute; -import gov.nist.secauto.metaschema.databind.model.metaschema.IInstanceModelChoiceBinding; -import gov.nist.secauto.metaschema.databind.model.metaschema.IInstanceModelChoiceGroupBinding; +import gov.nist.secauto.metaschema.databind.model.IGroupAs; import gov.nist.secauto.metaschema.databind.model.metaschema.binding.AssemblyConstraints; import gov.nist.secauto.metaschema.databind.model.metaschema.binding.InlineDefineAssembly; import gov.nist.secauto.metaschema.databind.model.metaschema.binding.JsonKey; import java.math.BigInteger; +import java.util.Map; +import java.util.Set; import edu.umd.cs.findbugs.annotations.NonNull; import nl.talsmasoftware.lazy4j.Lazy; public class InstanceModelAssemblyInline - extends AbstractInstanceModelNamedInline< - InlineDefineAssembly, - IBindingContainerModelAbsolute> - implements IBindingInstanceModelAssemblyAbsolute, IBindingDefinitionAssembly, - IFeatureDefinitionInstanceInlined, - IFeatureBindingContainerFlag, + extends AbstractInlineAssemblyDefinition< + IContainerModelAbsolute, + IAssemblyDefinition, + IAssemblyInstanceAbsolute, + IAssemblyDefinition, + IFlagInstance, + IModelInstanceAbsolute, + INamedModelInstanceAbsolute, + IFieldInstanceAbsolute, + IAssemblyInstanceAbsolute, + IChoiceInstance, + IChoiceGroupInstance> + implements IAssemblyInstanceAbsolute, IFeatureInstanceModelGroupAs, IFeatureBindingContainerModelAssembly { @NonNull - private final Lazy> flagContainer; + private final InlineDefineAssembly binding; + @NonNull + private final Map> properties; + @NonNull + private final IGroupAs groupAs; + @NonNull + private final Lazy boundNodeItem; + @NonNull + private final Lazy> flagContainer; @NonNull private final Lazy> modelContainer; + IModelInstanceAbsolute, + INamedModelInstanceAbsolute, + IFieldInstanceAbsolute, + IAssemblyInstanceAbsolute, + IChoiceInstance, + IChoiceGroupInstance>> modelContainer; @NonNull private final Lazy modelConstraints; @@ -97,22 +115,28 @@ public InstanceModelAssemblyInline( @NonNull InlineDefineAssembly binding, @NonNull IBoundInstanceModelGroupedAssembly bindingInstance, int position, - @NonNull IBindingContainerModelAbsolute parent, + @NonNull IContainerModelAbsolute parent, @NonNull INodeItemFactory nodeItemFactory) { - super(binding, - bindingInstance, - position, - parent, - ObjectUtils.requireNonNull(binding.getProps()), - binding.getGroupAs()); - this.flagContainer = ObjectUtils.notNull(Lazy.lazy(() -> FlagContainerSupport.of( - binding.getFlags(), - bindingInstance, - this))); + super(parent); + this.binding = binding; + this.properties = ModelSupport.parseProperties(ObjectUtils.requireNonNull(binding.getProps())); + this.groupAs = ModelSupport.groupAs(binding.getGroupAs(), parent.getOwningDefinition().getContainingModule()); + this.boundNodeItem = ObjectUtils.notNull( + Lazy.lazy(() -> (IAssemblyNodeItem) ObjectUtils.notNull(getContainingDefinition().getNodeItem()) + .getModelItemsByName(bindingInstance.getXmlQName()) + .get(position))); + this.flagContainer = ObjectUtils.notNull(Lazy.lazy(() -> { + JsonKey jsonKey = getBinding().getJsonKey(); + return FlagContainerSupport.newFlagContainer( + binding.getFlags(), + bindingInstance, + this, + jsonKey == null ? null : jsonKey.getFlagRef()); + })); this.modelContainer = ObjectUtils.notNull(Lazy.lazy(() -> AssemblyModelContainerSupport.of( binding.getModel(), ObjectUtils - .requireNonNull(bindingInstance.getDefinition().getAssemblyInstanceByName(IAssemblyDefinition.MODEL_QNAME)), + .requireNonNull(bindingInstance.getDefinition().getAssemblyInstanceByName(MODEL_QNAME)), this, nodeItemFactory))); this.modelConstraints = ObjectUtils.notNull(Lazy.lazy(() -> { @@ -128,20 +152,41 @@ public InstanceModelAssemblyInline( })); } + @NonNull + protected InlineDefineAssembly getBinding() { + getContainingDefinition(); + return binding; + } + + @Override + public Map> getProperties() { + return properties; + } + + @Override + public IGroupAs getGroupAs() { + return groupAs; + } + + @Override + public IAssemblyNodeItem getNodeItem() { + return boundNodeItem.get(); + } + @SuppressWarnings("null") @Override - public IContainerFlagSupport getFlagContainer() { + public IContainerFlagSupport getFlagContainer() { return flagContainer.get(); } @Override public IContainerModelAssemblySupport< - IBindingInstanceModelAbsolute, - IBindingInstanceModelNamedAbsolute, - IBindingInstanceModelFieldAbsolute, - IBindingInstanceModelAssemblyAbsolute, - IInstanceModelChoiceBinding, - IInstanceModelChoiceGroupBinding> getModelContainer() { + IModelInstanceAbsolute, + INamedModelInstanceAbsolute, + IFieldInstanceAbsolute, + IAssemblyInstanceAbsolute, + IChoiceInstance, + IChoiceGroupInstance> getModelContainer() { return ObjectUtils.notNull(modelContainer.get()); } @@ -150,15 +195,9 @@ public IModelConstrained getConstraintSupport() { return ObjectUtils.notNull(modelConstraints.get()); } - @Override - public InstanceModelAssemblyInline getDefinition() { - return this; - } - - @Override - public IBindingInstanceModelAssemblyAbsolute getInlineInstance() { - return this; - } + // --------------------------------------- + // - Start binding driven code - CPD-OFF - + // --------------------------------------- @Override public String getName() { @@ -185,23 +224,15 @@ public MarkupMultiline getRemarks() { return ModelSupport.remarks(getBinding().getRemarks()); } - @Override - public String getJsonKeyFlagName() { - JsonKey jsonKey = getBinding().getJsonKey(); - return jsonKey == null ? null : jsonKey.getFlagRef(); - } - @Override public int getMinOccurs() { BigInteger min = getBinding().getMinOccurs(); - return min == null ? IGroupable.DEFAULT_GROUP_AS_MIN_OCCURS : min.intValueExact(); + return min == null ? DEFAULT_GROUP_AS_MIN_OCCURS : min.intValueExact(); } @Override public int getMaxOccurs() { String max = getBinding().getMaxOccurs(); - return max == null - ? IGroupable.DEFAULT_GROUP_AS_MIN_OCCURS - : ModelSupport.maxOccurs(max); + return max == null ? DEFAULT_GROUP_AS_MAX_OCCURS : ModelSupport.maxOccurs(max); } } diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/InstanceModelAssemblyReference.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/InstanceModelAssemblyReference.java index 710edcb55..62980c39d 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/InstanceModelAssemblyReference.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/InstanceModelAssemblyReference.java @@ -28,26 +28,42 @@ import gov.nist.secauto.metaschema.core.datatype.markup.MarkupLine; import gov.nist.secauto.metaschema.core.datatype.markup.MarkupMultiline; -import gov.nist.secauto.metaschema.core.model.IFeatureDefinitionReferenceInstance; -import gov.nist.secauto.metaschema.core.model.IGroupable; +import gov.nist.secauto.metaschema.core.metapath.item.node.IAssemblyNodeItem; +import gov.nist.secauto.metaschema.core.model.AbstractAssemblyInstance; +import gov.nist.secauto.metaschema.core.model.IAssemblyDefinition; +import gov.nist.secauto.metaschema.core.model.IAssemblyInstanceAbsolute; +import gov.nist.secauto.metaschema.core.model.IAttributable; +import gov.nist.secauto.metaschema.core.model.IContainerModelAbsolute; import gov.nist.secauto.metaschema.core.util.ObjectUtils; import gov.nist.secauto.metaschema.databind.model.IBoundInstanceModelGroupedAssembly; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingContainerModelAbsolute; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingDefinitionAssembly; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingInstanceModelAssemblyAbsolute; +import gov.nist.secauto.metaschema.databind.model.IGroupAs; import gov.nist.secauto.metaschema.databind.model.metaschema.binding.AssemblyReference; import java.math.BigInteger; +import java.util.Map; +import java.util.Set; import edu.umd.cs.findbugs.annotations.NonNull; +import nl.talsmasoftware.lazy4j.Lazy; public class InstanceModelAssemblyReference - extends AbstractInstanceModelNamedReference< - AssemblyReference, - IBindingDefinitionAssembly, - IBindingContainerModelAbsolute> - implements IBindingInstanceModelAssemblyAbsolute, - IFeatureDefinitionReferenceInstance { + extends AbstractAssemblyInstance< + IContainerModelAbsolute, + IAssemblyDefinition, + IAssemblyInstanceAbsolute, + IAssemblyDefinition> + implements IAssemblyInstanceAbsolute, IFeatureInstanceModelGroupAs { + @NonNull + private final AssemblyReference binding; + @NonNull + private final IAssemblyDefinition definition; + @NonNull + private final Map> properties; + @NonNull + private final IGroupAs groupAs; + @NonNull + private final Lazy boundNodeItem; + /** * Construct a new assembly reference. * @@ -67,15 +83,51 @@ public InstanceModelAssemblyReference( @NonNull AssemblyReference binding, @NonNull IBoundInstanceModelGroupedAssembly bindingInstance, int position, - @NonNull IBindingDefinitionAssembly definition, - @NonNull IBindingContainerModelAbsolute parent) { - super(binding, - bindingInstance, - position, - definition, - parent, - ObjectUtils.requireNonNull(binding.getProps()), - binding.getGroupAs()); + @NonNull IAssemblyDefinition definition, + @NonNull IContainerModelAbsolute parent) { + super(parent); + this.binding = binding; + this.definition = definition; + this.properties = ModelSupport.parseProperties(ObjectUtils.requireNonNull(binding.getProps())); + this.groupAs = ModelSupport.groupAs(binding.getGroupAs(), parent.getOwningDefinition().getContainingModule()); + this.boundNodeItem = ObjectUtils.notNull( + Lazy.lazy(() -> (IAssemblyNodeItem) ObjectUtils.notNull(getContainingDefinition().getNodeItem()) + .getModelItemsByName(bindingInstance.getXmlQName()) + .get(position))); + } + + @Override + public IAssemblyDefinition getDefinition() { + return definition; + } + + @NonNull + protected AssemblyReference getBinding() { + return binding; + } + + @Override + public Map> getProperties() { + return properties; + } + + @Override + public IGroupAs getGroupAs() { + return groupAs; + } + + @Override + public IAssemblyNodeItem getNodeItem() { + return boundNodeItem.get(); + } + + // --------------------------------------- + // - Start binding driven code - CPD-OFF - + // --------------------------------------- + + @Override + public String getName() { + return getDefinition().getName(); } @Override @@ -111,14 +163,12 @@ public MarkupMultiline getRemarks() { @Override public int getMinOccurs() { BigInteger min = getBinding().getMinOccurs(); - return min == null ? IGroupable.DEFAULT_GROUP_AS_MIN_OCCURS : min.intValueExact(); + return min == null ? DEFAULT_GROUP_AS_MIN_OCCURS : min.intValueExact(); } @Override public int getMaxOccurs() { String max = getBinding().getMaxOccurs(); - return max == null - ? IGroupable.DEFAULT_GROUP_AS_MIN_OCCURS - : ModelSupport.maxOccurs(max); + return max == null ? DEFAULT_GROUP_AS_MAX_OCCURS : ModelSupport.maxOccurs(max); } } diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/InstanceModelChoice.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/InstanceModelChoice.java index 387e0388c..1c17ec0d9 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/InstanceModelChoice.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/InstanceModelChoice.java @@ -28,32 +28,37 @@ import gov.nist.secauto.metaschema.core.datatype.markup.MarkupMultiline; import gov.nist.secauto.metaschema.core.metapath.item.node.IAssemblyNodeItem; -import gov.nist.secauto.metaschema.core.metapath.item.node.INodeItem; import gov.nist.secauto.metaschema.core.metapath.item.node.INodeItemFactory; +import gov.nist.secauto.metaschema.core.model.AbstractChoiceInstance; +import gov.nist.secauto.metaschema.core.model.IAssemblyDefinition; +import gov.nist.secauto.metaschema.core.model.IAssemblyInstanceAbsolute; import gov.nist.secauto.metaschema.core.model.IContainerModelSupport; +import gov.nist.secauto.metaschema.core.model.IFieldInstanceAbsolute; +import gov.nist.secauto.metaschema.core.model.IModelInstanceAbsolute; +import gov.nist.secauto.metaschema.core.model.INamedModelInstanceAbsolute; import gov.nist.secauto.metaschema.core.util.ObjectUtils; import gov.nist.secauto.metaschema.databind.model.IBoundInstanceModelGroupedAssembly; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingDefinitionAssembly; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingInstanceModelAbsolute; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingInstanceModelAssemblyAbsolute; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingInstanceModelFieldAbsolute; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingInstanceModelNamedAbsolute; -import gov.nist.secauto.metaschema.databind.model.metaschema.IInstanceModelChoiceBinding; +import gov.nist.secauto.metaschema.databind.model.metaschema.binding.AssemblyModel; import gov.nist.secauto.metaschema.databind.model.metaschema.binding.AssemblyModel.Choice; import edu.umd.cs.findbugs.annotations.NonNull; import nl.talsmasoftware.lazy4j.Lazy; public class InstanceModelChoice - extends AbstractInstanceModel - implements IInstanceModelChoiceBinding, - IFeatureBindingContainerModel { + extends AbstractChoiceInstance< + IAssemblyDefinition, + IModelInstanceAbsolute, + INamedModelInstanceAbsolute, + IFieldInstanceAbsolute, + IAssemblyInstanceAbsolute> { + @NonNull + private final AssemblyModel.Choice binding; @NonNull private final Lazy> modelContainer; + IModelInstanceAbsolute, + INamedModelInstanceAbsolute, + IFieldInstanceAbsolute, + IAssemblyInstanceAbsolute>> modelContainer; @NonNull private final Lazy boundNodeItem; @@ -61,45 +66,45 @@ public InstanceModelChoice( @NonNull Choice binding, @NonNull IBoundInstanceModelGroupedAssembly bindingInstance, int position, - @NonNull IBindingDefinitionAssembly parent, + @NonNull IAssemblyDefinition parent, @NonNull INodeItemFactory nodeItemFactory) { - super(binding, parent); + super(parent); + this.binding = binding; this.modelContainer = ObjectUtils.notNull(Lazy.lazy(() -> ChoiceModelContainerSupport.of( binding, bindingInstance, this, nodeItemFactory))); this.boundNodeItem = ObjectUtils.notNull( - Lazy.lazy(() -> (IAssemblyNodeItem) getContainingDefinition().getBoundNodeItem() + Lazy.lazy(() -> (IAssemblyNodeItem) ObjectUtils.notNull(getContainingDefinition().getNodeItem()) .getModelItemsByName(bindingInstance.getXmlQName()) .get(position))); } + @NonNull + protected AssemblyModel.Choice getBinding() { + return binding; + } + @Override public IContainerModelSupport< - IBindingInstanceModelAbsolute, - IBindingInstanceModelNamedAbsolute, - IBindingInstanceModelFieldAbsolute, - IBindingInstanceModelAssemblyAbsolute> getModelContainer() { + IModelInstanceAbsolute, + INamedModelInstanceAbsolute, + IFieldInstanceAbsolute, + IAssemblyInstanceAbsolute> getModelContainer() { return ObjectUtils.notNull(modelContainer.get()); } - @SuppressWarnings("null") @Override - public INodeItem getBoundNodeItem() { + public IAssemblyNodeItem getNodeItem() { return boundNodeItem.get(); } @Override - public IBindingDefinitionAssembly getOwningDefinition() { + public IAssemblyDefinition getOwningDefinition() { return getContainingDefinition(); } - @Override - public String getJsonKeyFlagName() { - return null; - } - @Override public MarkupMultiline getRemarks() { // no remarks diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/InstanceModelChoiceGroup.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/InstanceModelChoiceGroup.java index 6d4a6dd1d..647cfd946 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/InstanceModelChoiceGroup.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/InstanceModelChoiceGroup.java @@ -27,20 +27,16 @@ package gov.nist.secauto.metaschema.databind.model.metaschema.impl; import gov.nist.secauto.metaschema.core.metapath.item.node.IAssemblyNodeItem; -import gov.nist.secauto.metaschema.core.metapath.item.node.INodeItem; import gov.nist.secauto.metaschema.core.metapath.item.node.INodeItemFactory; -import gov.nist.secauto.metaschema.core.model.IChoiceGroupInstance; +import gov.nist.secauto.metaschema.core.model.AbstractChoiceGroupInstance; +import gov.nist.secauto.metaschema.core.model.IAssemblyDefinition; +import gov.nist.secauto.metaschema.core.model.IAssemblyInstanceGrouped; import gov.nist.secauto.metaschema.core.model.IContainerModelSupport; -import gov.nist.secauto.metaschema.core.model.IFeatureContainerModelGrouped; -import gov.nist.secauto.metaschema.core.model.IGroupable; +import gov.nist.secauto.metaschema.core.model.IFieldInstanceGrouped; +import gov.nist.secauto.metaschema.core.model.INamedModelInstanceGrouped; import gov.nist.secauto.metaschema.core.util.ObjectUtils; import gov.nist.secauto.metaschema.databind.model.IBoundInstanceModelGroupedAssembly; import gov.nist.secauto.metaschema.databind.model.IGroupAs; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingDefinitionAssembly; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingInstanceModelAssemblyGrouped; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingInstanceModelFieldGrouped; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingInstanceModelNamedGrouped; -import gov.nist.secauto.metaschema.databind.model.metaschema.IInstanceModelChoiceGroupBinding; import gov.nist.secauto.metaschema.databind.model.metaschema.binding.AssemblyModel; import gov.nist.secauto.metaschema.databind.model.metaschema.binding.JsonKey; @@ -50,21 +46,22 @@ import nl.talsmasoftware.lazy4j.Lazy; public class InstanceModelChoiceGroup - extends AbstractInstanceModel - implements IInstanceModelChoiceGroupBinding, - IFeatureContainerModelGrouped< - IBindingInstanceModelNamedGrouped, - IBindingInstanceModelFieldGrouped, - IBindingInstanceModelAssemblyGrouped>, - IFeatureInstanceModelGroupAs { + extends AbstractChoiceGroupInstance< + IAssemblyDefinition, + INamedModelInstanceGrouped, + IFieldInstanceGrouped, + IAssemblyInstanceGrouped> + implements IFeatureInstanceModelGroupAs { + @NonNull + private final AssemblyModel.ChoiceGroup binding; @NonNull private final IGroupAs groupAs; @NonNull private final Lazy> modelContainer; + INamedModelInstanceGrouped, + INamedModelInstanceGrouped, + IFieldInstanceGrouped, + IAssemblyInstanceGrouped>> modelContainer; @NonNull private final Lazy boundNodeItem; @@ -72,27 +69,33 @@ public InstanceModelChoiceGroup( @NonNull AssemblyModel.ChoiceGroup binding, @NonNull IBoundInstanceModelGroupedAssembly bindingInstance, int position, - @NonNull IBindingDefinitionAssembly parent, + @NonNull IAssemblyDefinition parent, @NonNull INodeItemFactory nodeItemFactory) { - super(binding, parent); - this.groupAs = ModelSupport.groupAs(binding.getGroupAs(), parent.getXmlNamespace()); + super(parent); + this.binding = binding; + this.groupAs = ModelSupport.groupAs(binding.getGroupAs(), parent.getContainingModule()); this.modelContainer = ObjectUtils.notNull(Lazy.lazy(() -> ChoiceGroupModelContainerSupport.of( binding, bindingInstance, this, nodeItemFactory))); this.boundNodeItem = ObjectUtils.notNull( - Lazy.lazy(() -> (IAssemblyNodeItem) getContainingDefinition().getBoundNodeItem() + Lazy.lazy(() -> (IAssemblyNodeItem) ObjectUtils.notNull(getContainingDefinition().getNodeItem()) .getModelItemsByName(bindingInstance.getXmlQName()) .get(position))); } + @NonNull + protected AssemblyModel.ChoiceGroup getBinding() { + return binding; + } + @Override public IContainerModelSupport< - IBindingInstanceModelNamedGrouped, - IBindingInstanceModelNamedGrouped, - IBindingInstanceModelFieldGrouped, - IBindingInstanceModelAssemblyGrouped> getModelContainer() { + INamedModelInstanceGrouped, + INamedModelInstanceGrouped, + IFieldInstanceGrouped, + IAssemblyInstanceGrouped> getModelContainer() { return ObjectUtils.notNull(modelContainer.get()); } @@ -101,39 +104,40 @@ public IGroupAs getGroupAs() { return groupAs; } - @SuppressWarnings("null") @Override - public INodeItem getBoundNodeItem() { + public IAssemblyNodeItem getNodeItem() { return boundNodeItem.get(); } + // --------------------------------------- + // - Start binding driven code - CPD-OFF - + // --------------------------------------- + @Override public int getMinOccurs() { BigInteger min = getBinding().getMinOccurs(); - return min == null ? IGroupable.DEFAULT_GROUP_AS_MIN_OCCURS : min.intValueExact(); + return min == null ? DEFAULT_GROUP_AS_MIN_OCCURS : min.intValueExact(); } @Override public int getMaxOccurs() { String max = getBinding().getMaxOccurs(); - return max == null - ? IGroupable.DEFAULT_GROUP_AS_MIN_OCCURS - : ModelSupport.maxOccurs(max); + return max == null ? DEFAULT_GROUP_AS_MAX_OCCURS : ModelSupport.maxOccurs(max); } @Override - public IBindingDefinitionAssembly getOwningDefinition() { - return getContainingDefinition(); + public IAssemblyDefinition getOwningDefinition() { + return getParentContainer(); } @Override public String getJsonDiscriminatorProperty() { String discriminator = getBinding().getDiscriminator(); - return discriminator == null ? IChoiceGroupInstance.DEFAULT_JSON_DISCRIMINATOR_PROPERTY_NAME : discriminator; + return discriminator == null ? DEFAULT_JSON_DISCRIMINATOR_PROPERTY_NAME : discriminator; } @Override - public String getJsonKeyFlagName() { + public String getJsonKeyFlagInstanceName() { JsonKey jsonKey = getBinding().getJsonKey(); return jsonKey == null ? null : jsonKey.getFlagRef(); } diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/InstanceModelFieldInline.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/InstanceModelFieldInline.java index 3de41f937..6275dc1fd 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/InstanceModelFieldInline.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/InstanceModelFieldInline.java @@ -29,25 +29,29 @@ import gov.nist.secauto.metaschema.core.datatype.IDataTypeAdapter; import gov.nist.secauto.metaschema.core.datatype.markup.MarkupLine; import gov.nist.secauto.metaschema.core.datatype.markup.MarkupMultiline; +import gov.nist.secauto.metaschema.core.metapath.item.node.IAssemblyNodeItem; +import gov.nist.secauto.metaschema.core.model.AbstractInlineFieldDefinition; +import gov.nist.secauto.metaschema.core.model.IAssemblyDefinition; +import gov.nist.secauto.metaschema.core.model.IAttributable; import gov.nist.secauto.metaschema.core.model.IContainerFlagSupport; -import gov.nist.secauto.metaschema.core.model.IFeatureDefinitionInstanceInlined; +import gov.nist.secauto.metaschema.core.model.IContainerModelAbsolute; +import gov.nist.secauto.metaschema.core.model.IFieldDefinition; +import gov.nist.secauto.metaschema.core.model.IFieldInstanceAbsolute; import gov.nist.secauto.metaschema.core.model.IFlagInstance; -import gov.nist.secauto.metaschema.core.model.IGroupable; import gov.nist.secauto.metaschema.core.model.constraint.ISource; import gov.nist.secauto.metaschema.core.model.constraint.IValueConstrained; import gov.nist.secauto.metaschema.core.model.constraint.ValueConstraintSet; import gov.nist.secauto.metaschema.core.util.ObjectUtils; import gov.nist.secauto.metaschema.databind.model.IBoundInstanceModelGroupedAssembly; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingContainerModelAbsolute; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingDefinitionModelField; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingInstanceFlag; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingInstanceModelFieldAbsolute; +import gov.nist.secauto.metaschema.databind.model.IGroupAs; import gov.nist.secauto.metaschema.databind.model.metaschema.binding.FieldConstraints; import gov.nist.secauto.metaschema.databind.model.metaschema.binding.InlineDefineField; import gov.nist.secauto.metaschema.databind.model.metaschema.binding.JsonKey; import gov.nist.secauto.metaschema.databind.model.metaschema.binding.JsonValueKeyFlag; import java.math.BigInteger; +import java.util.Map; +import java.util.Set; import javax.xml.namespace.QName; @@ -56,17 +60,27 @@ import nl.talsmasoftware.lazy4j.Lazy; public class InstanceModelFieldInline - extends AbstractInstanceModelNamedInline - implements IBindingInstanceModelFieldAbsolute, IBindingDefinitionModelField, - IFeatureDefinitionInstanceInlined, - IFeatureBindingContainerFlag { + extends AbstractInlineFieldDefinition< + IContainerModelAbsolute, + IFieldDefinition, + IFieldInstanceAbsolute, + IAssemblyDefinition, + IFlagInstance> + implements IFieldInstanceAbsolute, IFeatureInstanceModelGroupAs { + @NonNull + private final InlineDefineField binding; + @NonNull + private final Map> properties; + @NonNull + private final IGroupAs groupAs; + @NonNull + private final Lazy boundNodeItem; @NonNull private final IDataTypeAdapter javaTypeAdapter; @Nullable private final Object defaultValue; @NonNull - private final Lazy> flagContainer; + private final Lazy> flagContainer; @NonNull private final Lazy valueConstraints; @@ -74,22 +88,28 @@ public InstanceModelFieldInline( @NonNull InlineDefineField binding, @NonNull IBoundInstanceModelGroupedAssembly bindingInstance, int position, - @NonNull IBindingContainerModelAbsolute parent) { - super(binding, - bindingInstance, - position, - parent, - ObjectUtils.requireNonNull(binding.getProps()), - binding.getGroupAs()); - this.javaTypeAdapter = ModelSupport.dataType(getBinding().getAsType()); - this.defaultValue = ModelSupport.defaultValue(getBinding().getDefault(), this.javaTypeAdapter); - this.flagContainer = ObjectUtils.notNull(Lazy.lazy(() -> FlagContainerSupport.of( - binding.getFlags(), - bindingInstance, - this))); + @NonNull IContainerModelAbsolute parent) { + super(parent); + this.binding = binding; + this.properties = ModelSupport.parseProperties(ObjectUtils.requireNonNull(binding.getProps())); + this.groupAs = ModelSupport.groupAs(binding.getGroupAs(), parent.getOwningDefinition().getContainingModule()); + this.boundNodeItem = ObjectUtils.notNull( + Lazy.lazy(() -> (IAssemblyNodeItem) ObjectUtils.notNull(getContainingDefinition().getNodeItem()) + .getModelItemsByName(bindingInstance.getXmlQName()) + .get(position))); + this.javaTypeAdapter = ModelSupport.dataType(binding.getAsType()); + this.defaultValue = ModelSupport.defaultValue(binding.getDefault(), this.javaTypeAdapter); + this.flagContainer = ObjectUtils.notNull(Lazy.lazy(() -> { + JsonKey jsonKey = binding.getJsonKey(); + return FlagContainerSupport.newFlagContainer( + binding.getFlags(), + bindingInstance, + this, + jsonKey == null ? null : jsonKey.getFlagRef()); + })); this.valueConstraints = ObjectUtils.notNull(Lazy.lazy(() -> { IValueConstrained retval = new ValueConstraintSet(); - FieldConstraints constraints = getBinding().getConstraint(); + FieldConstraints constraints = binding.getConstraint(); if (constraints != null) { ConstraintBindingSupport.parse( retval, @@ -100,13 +120,23 @@ public InstanceModelFieldInline( })); } + @NonNull + protected InlineDefineField getBinding() { + return binding; + } + @Override - public boolean isInXmlWrapped() { - return ModelSupport.fieldInXml(getBinding().getInXml()); + public Map> getProperties() { + return properties; } @Override - public IContainerFlagSupport getFlagContainer() { + public IGroupAs getGroupAs() { + return groupAs; + } + + @Override + public IContainerFlagSupport getFlagContainer() { return ObjectUtils.notNull(flagContainer.get()); } @@ -121,18 +151,22 @@ public IDataTypeAdapter getJavaTypeAdapter() { } @Override - public InstanceModelFieldInline getDefinition() { - return this; + public Object getDefaultValue() { + return defaultValue; } @Override - public IBindingInstanceModelFieldAbsolute getInlineInstance() { - return this; + public IAssemblyNodeItem getNodeItem() { + return boundNodeItem.get(); } + // --------------------------------------- + // - Start binding driven code - CPD-OFF - + // --------------------------------------- + @Override - public Object getDefaultValue() { - return defaultValue; + public boolean isInXmlWrapped() { + return ModelSupport.fieldInXml(getBinding().getInXml()); } @Override @@ -160,24 +194,16 @@ public MarkupMultiline getRemarks() { return ModelSupport.remarks(getBinding().getRemarks()); } - @Override - public String getJsonKeyFlagName() { - JsonKey jsonKey = getBinding().getJsonKey(); - return jsonKey == null ? null : jsonKey.getFlagRef(); - } - @Override public int getMinOccurs() { BigInteger min = getBinding().getMinOccurs(); - return min == null ? IGroupable.DEFAULT_GROUP_AS_MIN_OCCURS : min.intValueExact(); + return min == null ? DEFAULT_GROUP_AS_MIN_OCCURS : min.intValueExact(); } @Override public int getMaxOccurs() { String max = getBinding().getMaxOccurs(); - return max == null - ? IGroupable.DEFAULT_GROUP_AS_MIN_OCCURS - : ModelSupport.maxOccurs(max); + return max == null ? DEFAULT_GROUP_AS_MAX_OCCURS : ModelSupport.maxOccurs(max); } @Override diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/InstanceModelFieldReference.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/InstanceModelFieldReference.java index c16c9c76e..88d6ac709 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/InstanceModelFieldReference.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/InstanceModelFieldReference.java @@ -28,27 +28,43 @@ import gov.nist.secauto.metaschema.core.datatype.markup.MarkupLine; import gov.nist.secauto.metaschema.core.datatype.markup.MarkupMultiline; -import gov.nist.secauto.metaschema.core.model.IFeatureDefinitionReferenceInstance; -import gov.nist.secauto.metaschema.core.model.IGroupable; +import gov.nist.secauto.metaschema.core.metapath.item.node.IAssemblyNodeItem; +import gov.nist.secauto.metaschema.core.model.AbstractFieldInstance; +import gov.nist.secauto.metaschema.core.model.IAssemblyDefinition; +import gov.nist.secauto.metaschema.core.model.IAttributable; +import gov.nist.secauto.metaschema.core.model.IContainerModelAbsolute; +import gov.nist.secauto.metaschema.core.model.IFieldDefinition; +import gov.nist.secauto.metaschema.core.model.IFieldInstanceAbsolute; import gov.nist.secauto.metaschema.core.util.ObjectUtils; import gov.nist.secauto.metaschema.databind.model.IBoundInstanceModelGroupedAssembly; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingContainerModelAbsolute; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingDefinitionModelField; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingInstanceModelFieldAbsolute; +import gov.nist.secauto.metaschema.databind.model.IGroupAs; import gov.nist.secauto.metaschema.databind.model.metaschema.binding.FieldReference; import java.math.BigInteger; +import java.util.Map; +import java.util.Set; import edu.umd.cs.findbugs.annotations.NonNull; import edu.umd.cs.findbugs.annotations.Nullable; +import nl.talsmasoftware.lazy4j.Lazy; public class InstanceModelFieldReference - extends AbstractInstanceModelNamedReference< - FieldReference, - IBindingDefinitionModelField, - IBindingContainerModelAbsolute> - implements IBindingInstanceModelFieldAbsolute, - IFeatureDefinitionReferenceInstance { + extends AbstractFieldInstance< + IContainerModelAbsolute, + IFieldDefinition, + IFieldInstanceAbsolute, + IAssemblyDefinition> + implements IFieldInstanceAbsolute, IFeatureInstanceModelGroupAs { + @NonNull + private final FieldReference binding; + @NonNull + private final IFieldDefinition definition; + @NonNull + private final Map> properties; + @NonNull + private final IGroupAs groupAs; + @NonNull + private final Lazy boundNodeItem; @Nullable private final Object defaultValue; @@ -56,19 +72,49 @@ public InstanceModelFieldReference( @NonNull FieldReference binding, @NonNull IBoundInstanceModelGroupedAssembly bindingInstance, int position, - @NonNull IBindingDefinitionModelField definition, - @NonNull IBindingContainerModelAbsolute parent) { - super( - binding, - bindingInstance, - position, - definition, - parent, - ObjectUtils.requireNonNull(binding.getProps()), - binding.getGroupAs()); - this.defaultValue = ModelSupport.defaultValue(getBinding().getDefault(), definition.getJavaTypeAdapter()); + @NonNull IFieldDefinition definition, + @NonNull IContainerModelAbsolute parent) { + super(parent); + this.binding = binding; + this.definition = definition; + this.properties = ModelSupport.parseProperties(ObjectUtils.requireNonNull(binding.getProps())); + this.groupAs = ModelSupport.groupAs(binding.getGroupAs(), parent.getOwningDefinition().getContainingModule()); + this.boundNodeItem = ObjectUtils.notNull( + Lazy.lazy(() -> (IAssemblyNodeItem) ObjectUtils.notNull(getContainingDefinition().getNodeItem()) + .getModelItemsByName(bindingInstance.getXmlQName()) + .get(position))); + this.defaultValue = ModelSupport.defaultValue(binding.getDefault(), definition.getJavaTypeAdapter()); } + @Override + public IFieldDefinition getDefinition() { + return definition; + } + + @NonNull + protected FieldReference getBinding() { + return binding; + } + + @Override + public Map> getProperties() { + return properties; + } + + @Override + public IGroupAs getGroupAs() { + return groupAs; + } + + @Override + public IAssemblyNodeItem getNodeItem() { + return boundNodeItem.get(); + } + + // --------------------------------------- + // - Start binding driven code - CPD-OFF - + // --------------------------------------- + @Override public boolean isInXmlWrapped() { return ModelSupport.fieldInXml(getBinding().getInXml()); @@ -117,14 +163,12 @@ public MarkupMultiline getRemarks() { @Override public int getMinOccurs() { BigInteger min = getBinding().getMinOccurs(); - return min == null ? IGroupable.DEFAULT_GROUP_AS_MIN_OCCURS : min.intValueExact(); + return min == null ? DEFAULT_GROUP_AS_MIN_OCCURS : min.intValueExact(); } @Override public int getMaxOccurs() { String max = getBinding().getMaxOccurs(); - return max == null - ? IGroupable.DEFAULT_GROUP_AS_MIN_OCCURS - : ModelSupport.maxOccurs(max); + return max == null ? DEFAULT_GROUP_AS_MAX_OCCURS : ModelSupport.maxOccurs(max); } } diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/InstanceModelGroupedAssemblyInline.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/InstanceModelGroupedAssemblyInline.java index 034d116cd..67404960c 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/InstanceModelGroupedAssemblyInline.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/InstanceModelGroupedAssemblyInline.java @@ -29,49 +29,62 @@ import gov.nist.secauto.metaschema.core.datatype.markup.MarkupLine; import gov.nist.secauto.metaschema.core.datatype.markup.MarkupMultiline; import gov.nist.secauto.metaschema.core.metapath.item.node.IAssemblyNodeItem; -import gov.nist.secauto.metaschema.core.metapath.item.node.INodeItem; import gov.nist.secauto.metaschema.core.metapath.item.node.INodeItemFactory; +import gov.nist.secauto.metaschema.core.model.AbstractInlineAssemblyDefinition; import gov.nist.secauto.metaschema.core.model.IAssemblyDefinition; +import gov.nist.secauto.metaschema.core.model.IAssemblyInstanceAbsolute; import gov.nist.secauto.metaschema.core.model.IAssemblyInstanceGrouped; +import gov.nist.secauto.metaschema.core.model.IAttributable; +import gov.nist.secauto.metaschema.core.model.IChoiceGroupInstance; +import gov.nist.secauto.metaschema.core.model.IChoiceInstance; import gov.nist.secauto.metaschema.core.model.IContainerFlagSupport; import gov.nist.secauto.metaschema.core.model.IContainerModelAssemblySupport; -import gov.nist.secauto.metaschema.core.model.IFeatureDefinitionInstanceInlined; +import gov.nist.secauto.metaschema.core.model.IFieldInstanceAbsolute; +import gov.nist.secauto.metaschema.core.model.IFlagInstance; +import gov.nist.secauto.metaschema.core.model.IModelInstanceAbsolute; +import gov.nist.secauto.metaschema.core.model.INamedModelInstanceAbsolute; import gov.nist.secauto.metaschema.core.model.constraint.AssemblyConstraintSet; import gov.nist.secauto.metaschema.core.model.constraint.IModelConstrained; import gov.nist.secauto.metaschema.core.model.constraint.ISource; import gov.nist.secauto.metaschema.core.util.ObjectUtils; import gov.nist.secauto.metaschema.databind.model.IBoundInstanceModelGroupedAssembly; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingDefinitionAssembly; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingInstanceFlag; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingInstanceModelAbsolute; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingInstanceModelAssemblyAbsolute; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingInstanceModelAssemblyGrouped; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingInstanceModelFieldAbsolute; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingInstanceModelNamedAbsolute; -import gov.nist.secauto.metaschema.databind.model.metaschema.IInstanceModelChoiceBinding; -import gov.nist.secauto.metaschema.databind.model.metaschema.IInstanceModelChoiceGroupBinding; import gov.nist.secauto.metaschema.databind.model.metaschema.binding.AssemblyConstraints; import gov.nist.secauto.metaschema.databind.model.metaschema.binding.AssemblyModel; +import java.util.Map; +import java.util.Set; + import edu.umd.cs.findbugs.annotations.NonNull; import nl.talsmasoftware.lazy4j.Lazy; public class InstanceModelGroupedAssemblyInline - extends AbstractInstanceModelGrouped - implements IBindingInstanceModelAssemblyGrouped, IBindingDefinitionAssembly, - IFeatureDefinitionInstanceInlined, - IFeatureBindingContainerModelAssembly, - IFeatureBindingContainerFlag { + extends AbstractInlineAssemblyDefinition< + IChoiceGroupInstance, + IAssemblyDefinition, + IAssemblyInstanceGrouped, + IAssemblyDefinition, + IFlagInstance, + IModelInstanceAbsolute, + INamedModelInstanceAbsolute, + IFieldInstanceAbsolute, + IAssemblyInstanceAbsolute, + IChoiceInstance, + IChoiceGroupInstance> + implements IAssemblyInstanceGrouped, IFeatureBindingContainerModelAssembly { + @NonNull + private final AssemblyModel.ChoiceGroup.DefineAssembly binding; @NonNull - private final Lazy> flagContainer; + private final Map> properties; + @NonNull + private final Lazy> flagContainer; @NonNull private final Lazy> modelContainer; + IModelInstanceAbsolute, + INamedModelInstanceAbsolute, + IFieldInstanceAbsolute, + IAssemblyInstanceAbsolute, + IChoiceInstance, + IChoiceGroupInstance>> modelContainer; @NonNull private final Lazy modelConstraints; @NonNull @@ -81,22 +94,25 @@ public InstanceModelGroupedAssemblyInline( @NonNull AssemblyModel.ChoiceGroup.DefineAssembly binding, @NonNull IBoundInstanceModelGroupedAssembly bindingInstance, int position, - @NonNull IInstanceModelChoiceGroupBinding parent, + @NonNull IChoiceGroupInstance parent, @NonNull INodeItemFactory nodeItemFactory) { - super(binding, bindingInstance, position, parent, ObjectUtils.requireNonNull(binding.getProps())); - this.flagContainer = ObjectUtils.notNull(Lazy.lazy(() -> FlagContainerSupport.of( + super(parent); + this.binding = binding; + this.properties = ModelSupport.parseProperties(ObjectUtils.requireNonNull(binding.getProps())); + this.flagContainer = ObjectUtils.notNull(Lazy.lazy(() -> FlagContainerSupport.newFlagContainer( binding.getFlags(), bindingInstance, - this))); + this, + getParentContainer().getJsonKeyFlagInstanceName()))); this.modelContainer = ObjectUtils.notNull(Lazy.lazy(() -> AssemblyModelContainerSupport.of( binding.getModel(), ObjectUtils - .requireNonNull(bindingInstance.getDefinition().getAssemblyInstanceByName(IAssemblyDefinition.MODEL_QNAME)), + .requireNonNull(bindingInstance.getDefinition().getAssemblyInstanceByName(MODEL_QNAME)), this, nodeItemFactory))); this.modelConstraints = ObjectUtils.notNull(Lazy.lazy(() -> { IModelConstrained retval = new AssemblyConstraintSet(); - AssemblyConstraints constraints = getBinding().getConstraint(); + AssemblyConstraints constraints = binding.getConstraint(); if (constraints != null) { ConstraintBindingSupport.parse( retval, @@ -106,24 +122,34 @@ public InstanceModelGroupedAssemblyInline( return retval; })); this.boundNodeItem = ObjectUtils.notNull( - Lazy.lazy(() -> (IAssemblyNodeItem) getContainingDefinition().getBoundNodeItem() + Lazy.lazy(() -> (IAssemblyNodeItem) ObjectUtils.notNull(getContainingDefinition().getNodeItem()) .getModelItemsByName(bindingInstance.getXmlQName()) .get(position))); } + @NonNull + protected AssemblyModel.ChoiceGroup.DefineAssembly getBinding() { + return binding; + } + @Override - public IContainerFlagSupport getFlagContainer() { + public Map> getProperties() { + return properties; + } + + @Override + public IContainerFlagSupport getFlagContainer() { return ObjectUtils.notNull(flagContainer.get()); } @Override public IContainerModelAssemblySupport< - IBindingInstanceModelAbsolute, - IBindingInstanceModelNamedAbsolute, - IBindingInstanceModelFieldAbsolute, - IBindingInstanceModelAssemblyAbsolute, - IInstanceModelChoiceBinding, - IInstanceModelChoiceGroupBinding> getModelContainer() { + IModelInstanceAbsolute, + INamedModelInstanceAbsolute, + IFieldInstanceAbsolute, + IAssemblyInstanceAbsolute, + IChoiceInstance, + IChoiceGroupInstance> getModelContainer() { return ObjectUtils.notNull(modelContainer.get()); } @@ -132,21 +158,14 @@ public IModelConstrained getConstraintSupport() { return ObjectUtils.notNull(modelConstraints.get()); } - @SuppressWarnings("null") @Override - public INodeItem getBoundNodeItem() { + public IAssemblyNodeItem getNodeItem() { return boundNodeItem.get(); } - @Override - public IBindingDefinitionAssembly getDefinition() { - return this; - } - - @Override - public IAssemblyInstanceGrouped getInlineInstance() { - return this; - } + // --------------------------------------- + // - Start binding driven code - CPD-OFF - + // --------------------------------------- @Override public String getName() { diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/InstanceModelGroupedAssemblyReference.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/InstanceModelGroupedAssemblyReference.java index 44f70a156..f86357e7b 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/InstanceModelGroupedAssemblyReference.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/InstanceModelGroupedAssemblyReference.java @@ -28,43 +28,83 @@ import gov.nist.secauto.metaschema.core.datatype.markup.MarkupLine; import gov.nist.secauto.metaschema.core.datatype.markup.MarkupMultiline; -import gov.nist.secauto.metaschema.core.model.IFeatureDefinitionReferenceInstance; +import gov.nist.secauto.metaschema.core.metapath.item.node.IAssemblyNodeItem; +import gov.nist.secauto.metaschema.core.model.AbstractAssemblyInstance; +import gov.nist.secauto.metaschema.core.model.IAssemblyDefinition; +import gov.nist.secauto.metaschema.core.model.IAssemblyInstanceGrouped; +import gov.nist.secauto.metaschema.core.model.IAttributable; +import gov.nist.secauto.metaschema.core.model.IChoiceGroupInstance; import gov.nist.secauto.metaschema.core.util.ObjectUtils; import gov.nist.secauto.metaschema.databind.model.IBoundInstanceModelGroupedAssembly; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingDefinitionAssembly; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingInstanceModelAssemblyGrouped; -import gov.nist.secauto.metaschema.databind.model.metaschema.IInstanceModelChoiceGroupBinding; import gov.nist.secauto.metaschema.databind.model.metaschema.binding.AssemblyModel; +import java.util.Map; +import java.util.Set; + import edu.umd.cs.findbugs.annotations.NonNull; +import nl.talsmasoftware.lazy4j.Lazy; public class InstanceModelGroupedAssemblyReference - extends AbstractInstanceModelGrouped - implements IBindingInstanceModelAssemblyGrouped, - IFeatureDefinitionReferenceInstance { + extends AbstractAssemblyInstance< + IChoiceGroupInstance, + IAssemblyDefinition, + IAssemblyInstanceGrouped, + IAssemblyDefinition> + implements IAssemblyInstanceGrouped { + @NonNull + private final AssemblyModel.ChoiceGroup.Assembly binding; + @NonNull + private final IAssemblyDefinition definition; + @NonNull + private final Map> properties; @NonNull - private final IBindingDefinitionAssembly definition; + private final Lazy boundNodeItem; public InstanceModelGroupedAssemblyReference( @NonNull AssemblyModel.ChoiceGroup.Assembly binding, @NonNull IBoundInstanceModelGroupedAssembly bindingInstance, int position, - @NonNull IBindingDefinitionAssembly definition, - @NonNull IInstanceModelChoiceGroupBinding parent) { - super(binding, bindingInstance, position, parent, ObjectUtils.requireNonNull(binding.getProps())); + @NonNull IAssemblyDefinition definition, + @NonNull IChoiceGroupInstance parent) { + super(parent); + this.binding = binding; this.definition = definition; + this.properties = ModelSupport.parseProperties(ObjectUtils.requireNonNull(binding.getProps())); + this.boundNodeItem = ObjectUtils.notNull( + Lazy.lazy(() -> (IAssemblyNodeItem) ObjectUtils.notNull(getContainingDefinition().getNodeItem()) + .getModelItemsByName(bindingInstance.getXmlQName()) + .get(position))); } @Override - public IBindingDefinitionAssembly getDefinition() { + public IAssemblyDefinition getDefinition() { return definition; } + @NonNull + protected AssemblyModel.ChoiceGroup.Assembly getBinding() { + return binding; + } + + @Override + public Map> getProperties() { + return properties; + } + + @Override + public IAssemblyNodeItem getNodeItem() { + return boundNodeItem.get(); + } + @Override public String getName() { return getDefinition().getName(); } + // --------------------------------------- + // - Start binding driven code - CPD-OFF - + // --------------------------------------- + @Override public String getUseName() { return ModelSupport.useName(getBinding().getUseName()); diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/InstanceModelGroupedFieldInline.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/InstanceModelGroupedFieldInline.java index 237dce80b..54e7220b7 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/InstanceModelGroupedFieldInline.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/InstanceModelGroupedFieldInline.java @@ -26,15 +26,16 @@ package gov.nist.secauto.metaschema.databind.model.metaschema.impl; -import javax.xml.namespace.QName; - -import edu.umd.cs.findbugs.annotations.NonNull; -import edu.umd.cs.findbugs.annotations.Nullable; import gov.nist.secauto.metaschema.core.datatype.IDataTypeAdapter; import gov.nist.secauto.metaschema.core.datatype.markup.MarkupLine; import gov.nist.secauto.metaschema.core.datatype.markup.MarkupMultiline; +import gov.nist.secauto.metaschema.core.metapath.item.node.IFieldNodeItem; +import gov.nist.secauto.metaschema.core.model.AbstractInlineFieldDefinition; +import gov.nist.secauto.metaschema.core.model.IAssemblyDefinition; +import gov.nist.secauto.metaschema.core.model.IAttributable; +import gov.nist.secauto.metaschema.core.model.IChoiceGroupInstance; import gov.nist.secauto.metaschema.core.model.IContainerFlagSupport; -import gov.nist.secauto.metaschema.core.model.IFeatureDefinitionInstanceInlined; +import gov.nist.secauto.metaschema.core.model.IFieldDefinition; import gov.nist.secauto.metaschema.core.model.IFieldInstanceGrouped; import gov.nist.secauto.metaschema.core.model.IFlagInstance; import gov.nist.secauto.metaschema.core.model.constraint.ISource; @@ -42,44 +43,60 @@ import gov.nist.secauto.metaschema.core.model.constraint.ValueConstraintSet; import gov.nist.secauto.metaschema.core.util.ObjectUtils; import gov.nist.secauto.metaschema.databind.model.IBoundInstanceModelGroupedAssembly; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingDefinitionModelField; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingInstanceFlag; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingInstanceModelFieldGrouped; -import gov.nist.secauto.metaschema.databind.model.metaschema.IInstanceModelChoiceGroupBinding; import gov.nist.secauto.metaschema.databind.model.metaschema.binding.AssemblyModel; import gov.nist.secauto.metaschema.databind.model.metaschema.binding.FieldConstraints; import gov.nist.secauto.metaschema.databind.model.metaschema.binding.JsonValueKeyFlag; + +import java.util.Map; +import java.util.Set; + +import javax.xml.namespace.QName; + +import edu.umd.cs.findbugs.annotations.NonNull; +import edu.umd.cs.findbugs.annotations.Nullable; import nl.talsmasoftware.lazy4j.Lazy; public class InstanceModelGroupedFieldInline - extends AbstractInstanceModelGrouped - implements IBindingInstanceModelFieldGrouped, IBindingDefinitionModelField, - IFeatureDefinitionInstanceInlined, - IFeatureBindingContainerFlag { + extends AbstractInlineFieldDefinition< + IChoiceGroupInstance, + IFieldDefinition, + IFieldInstanceGrouped, + IAssemblyDefinition, + IFlagInstance> + implements IFieldInstanceGrouped { + @NonNull + private final AssemblyModel.ChoiceGroup.DefineField binding; + @NonNull + private final Map> properties; @NonNull private final IDataTypeAdapter javaTypeAdapter; @Nullable private final Object defaultValue; @NonNull - private final Lazy> flagContainer; + private final Lazy> flagContainer; @NonNull private final Lazy valueConstraints; + @NonNull + private final Lazy boundNodeItem; public InstanceModelGroupedFieldInline( @NonNull AssemblyModel.ChoiceGroup.DefineField binding, @NonNull IBoundInstanceModelGroupedAssembly bindingInstance, int position, - @NonNull IInstanceModelChoiceGroupBinding parent) { - super(binding, bindingInstance, position, parent, ObjectUtils.requireNonNull(binding.getProps())); - this.javaTypeAdapter = ModelSupport.dataType(getBinding().getAsType()); - this.defaultValue = ModelSupport.defaultValue(getBinding().getDefault(), this.javaTypeAdapter); - this.flagContainer = ObjectUtils.notNull(Lazy.lazy(() -> FlagContainerSupport.of( + @NonNull IChoiceGroupInstance parent) { + super(parent); + this.binding = binding; + this.properties = ModelSupport.parseProperties(ObjectUtils.requireNonNull(binding.getProps())); + this.javaTypeAdapter = ModelSupport.dataType(binding.getAsType()); + this.defaultValue = ModelSupport.defaultValue(binding.getDefault(), this.javaTypeAdapter); + this.flagContainer = ObjectUtils.notNull(Lazy.lazy(() -> FlagContainerSupport.newFlagContainer( binding.getFlags(), bindingInstance, - this))); + this, + getParentContainer().getJsonKeyFlagInstanceName()))); this.valueConstraints = ObjectUtils.notNull(Lazy.lazy(() -> { IValueConstrained retval = new ValueConstraintSet(); - FieldConstraints constraints = getBinding().getConstraint(); + FieldConstraints constraints = binding.getConstraint(); if (constraints != null) { ConstraintBindingSupport.parse( retval, @@ -88,31 +105,40 @@ public InstanceModelGroupedFieldInline( } return retval; })); + this.boundNodeItem = ObjectUtils.notNull( + Lazy.lazy(() -> (IFieldNodeItem) ObjectUtils.notNull(getContainingDefinition().getNodeItem()) + .getModelItemsByName(bindingInstance.getXmlQName()) + .get(position))); + } + + @NonNull + protected AssemblyModel.ChoiceGroup.DefineField getBinding() { + return binding; } @Override - public IContainerFlagSupport getFlagContainer() { - return ObjectUtils.notNull(flagContainer.get()); + public Map> getProperties() { + return properties; } @Override - public IValueConstrained getConstraintSupport() { - return ObjectUtils.notNull(valueConstraints.get()); + public IContainerFlagSupport getFlagContainer() { + return ObjectUtils.notNull(flagContainer.get()); } @Override - public IDataTypeAdapter getJavaTypeAdapter() { - return javaTypeAdapter; + public IValueConstrained getConstraintSupport() { + return ObjectUtils.notNull(valueConstraints.get()); } @Override - public IBindingDefinitionModelField getDefinition() { - return this; + public IFieldNodeItem getNodeItem() { + return boundNodeItem.get(); } @Override - public IFieldInstanceGrouped getInlineInstance() { - return this; + public IDataTypeAdapter getJavaTypeAdapter() { + return javaTypeAdapter; } @Override @@ -120,6 +146,10 @@ public Object getDefaultValue() { return defaultValue; } + // --------------------------------------- + // - Start binding driven code - CPD-OFF - + // --------------------------------------- + @Override public String getName() { return ObjectUtils.notNull(getBinding().getName()); diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/InstanceModelGroupedFieldReference.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/InstanceModelGroupedFieldReference.java index 5ab347f65..20b4560cb 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/InstanceModelGroupedFieldReference.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/InstanceModelGroupedFieldReference.java @@ -28,43 +28,84 @@ import gov.nist.secauto.metaschema.core.datatype.markup.MarkupLine; import gov.nist.secauto.metaschema.core.datatype.markup.MarkupMultiline; -import gov.nist.secauto.metaschema.core.model.IFeatureDefinitionReferenceInstance; +import gov.nist.secauto.metaschema.core.metapath.item.node.IAssemblyNodeItem; +import gov.nist.secauto.metaschema.core.model.AbstractFieldInstance; +import gov.nist.secauto.metaschema.core.model.IAssemblyDefinition; +import gov.nist.secauto.metaschema.core.model.IAttributable; +import gov.nist.secauto.metaschema.core.model.IChoiceGroupInstance; +import gov.nist.secauto.metaschema.core.model.IFieldDefinition; +import gov.nist.secauto.metaschema.core.model.IFieldInstanceGrouped; import gov.nist.secauto.metaschema.core.util.ObjectUtils; import gov.nist.secauto.metaschema.databind.model.IBoundInstanceModelGroupedAssembly; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingDefinitionModelField; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingInstanceModelFieldGrouped; -import gov.nist.secauto.metaschema.databind.model.metaschema.IInstanceModelChoiceGroupBinding; import gov.nist.secauto.metaschema.databind.model.metaschema.binding.AssemblyModel; +import java.util.Map; +import java.util.Set; + import edu.umd.cs.findbugs.annotations.NonNull; +import nl.talsmasoftware.lazy4j.Lazy; public class InstanceModelGroupedFieldReference - extends AbstractInstanceModelGrouped - implements IBindingInstanceModelFieldGrouped, - IFeatureDefinitionReferenceInstance { + extends AbstractFieldInstance< + IChoiceGroupInstance, + IFieldDefinition, + IFieldInstanceGrouped, + IAssemblyDefinition> + implements IFieldInstanceGrouped { + @NonNull + private final AssemblyModel.ChoiceGroup.Field binding; + @NonNull + private final IFieldDefinition definition; + @NonNull + private final Map> properties; @NonNull - private final IBindingDefinitionModelField definition; + private final Lazy boundNodeItem; protected InstanceModelGroupedFieldReference( @NonNull AssemblyModel.ChoiceGroup.Field binding, @NonNull IBoundInstanceModelGroupedAssembly bindingInstance, int position, - @NonNull IBindingDefinitionModelField definition, - @NonNull IInstanceModelChoiceGroupBinding parent) { - super(binding, bindingInstance, position, parent, ObjectUtils.requireNonNull(binding.getProps())); + @NonNull IFieldDefinition definition, + @NonNull IChoiceGroupInstance parent) { + super(parent); + this.binding = binding; this.definition = definition; + this.properties = ModelSupport.parseProperties(ObjectUtils.requireNonNull(binding.getProps())); + this.boundNodeItem = ObjectUtils.notNull( + Lazy.lazy(() -> (IAssemblyNodeItem) ObjectUtils.notNull(getContainingDefinition().getNodeItem()) + .getModelItemsByName(bindingInstance.getXmlQName()) + .get(position))); } @Override - public IBindingDefinitionModelField getDefinition() { + public IFieldDefinition getDefinition() { return definition; } + @NonNull + protected AssemblyModel.ChoiceGroup.Field getBinding() { + return binding; + } + + @Override + public Map> getProperties() { + return properties; + } + + @Override + public IAssemblyNodeItem getNodeItem() { + return boundNodeItem.get(); + } + @Override public String getName() { return getDefinition().getName(); } + // --------------------------------------- + // - Start binding driven code - CPD-OFF - + // --------------------------------------- + @Override public String getUseName() { return ModelSupport.useName(getBinding().getUseName()); diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/ModelSupport.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/ModelSupport.java index caee807b3..e3e277784 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/ModelSupport.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/impl/ModelSupport.java @@ -30,9 +30,12 @@ import gov.nist.secauto.metaschema.core.datatype.IDataTypeAdapter; import gov.nist.secauto.metaschema.core.datatype.adapter.MetaschemaDataTypeProvider; import gov.nist.secauto.metaschema.core.datatype.markup.MarkupMultiline; +import gov.nist.secauto.metaschema.core.metapath.item.node.IAssemblyNodeItem; +import gov.nist.secauto.metaschema.core.metapath.item.node.IDocumentNodeItem; import gov.nist.secauto.metaschema.core.model.IAttributable; import gov.nist.secauto.metaschema.core.model.IFieldInstance; import gov.nist.secauto.metaschema.core.model.IGroupable; +import gov.nist.secauto.metaschema.core.model.IModule; import gov.nist.secauto.metaschema.core.model.JsonGroupAsBehavior; import gov.nist.secauto.metaschema.core.model.ModuleScopeEnum; import gov.nist.secauto.metaschema.core.model.XmlGroupAsBehavior; @@ -54,6 +57,8 @@ import java.util.Set; import java.util.stream.Collectors; +import javax.xml.namespace.QName; + import edu.umd.cs.findbugs.annotations.NonNull; import edu.umd.cs.findbugs.annotations.Nullable; @@ -67,14 +72,14 @@ public static Map> parseProperties(@NonNull List< return CollectionUtil.unmodifiableMap(ObjectUtils.notNull(props.stream() .collect( Collectors.groupingBy( - (prop) -> { + prop -> { String name = ObjectUtils.requireNonNull(prop.getName()); URI namespace = prop.getNamespace(); return namespace == null ? IAttributable.key(name) : IAttributable.key(name, ObjectUtils.notNull(namespace.toASCIIString())); }, Collectors.mapping( - (prop) -> ObjectUtils.requireNonNull(prop.getValue()), + prop -> ObjectUtils.requireNonNull(prop.getValue()), Collectors.toCollection(LinkedHashSet::new)))))); } @@ -183,10 +188,10 @@ public static boolean fieldInXml(@Nullable String inXml) { @NonNull public static IGroupAs groupAs( @Nullable GroupAs groupAs, - @Nullable String groupAsNamespace) { + @NonNull IModule module) { return groupAs == null ? IGroupAs.SINGLETON_GROUP_AS - : new GroupAsImpl(groupAs, groupAsNamespace); + : new GroupAsImpl(groupAs, module); } @NonNull @@ -229,4 +234,14 @@ public static XmlGroupAsBehavior groupAsXmlBehavior(@Nullable String inXml) { } return retval; } + + @SuppressWarnings("unchecked") + @Nullable + public static NODE toNodeItem( + @NonNull IModule module, + @NonNull QName qname, + int position) { + IDocumentNodeItem moduleNodeItem = module.getNodeItem(); + return moduleNodeItem == null ? null : (NODE) moduleNodeItem.getModelItemsByName(qname).get(position); + } } diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/IBindingDefinitionFlag.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/package-info.java similarity index 87% rename from databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/IBindingDefinitionFlag.java rename to databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/package-info.java index ee6f5e956..bcb6f2a2a 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/IBindingDefinitionFlag.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/metaschema/package-info.java @@ -24,10 +24,11 @@ * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. */ -package gov.nist.secauto.metaschema.databind.model.metaschema; - -import gov.nist.secauto.metaschema.core.model.IFlagDefinition; +/** + * Provides an implementation of the Module model + * ({@link gov.nist.secauto.metaschema.core.model}) represented by a + * module-specific set of bound Java classes + * ({@link gov.nist.secauto.metaschema.databind.model.metaschema.binding}). + */ -public interface IBindingDefinitionFlag extends IFlagDefinition, IBindingDefinition { - // no additional methods -} +package gov.nist.secauto.metaschema.databind.model.metaschema; diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/package-info.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/package-info.java index 4cabc3660..b92bac6ff 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/package-info.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/model/package-info.java @@ -26,9 +26,9 @@ /** * Provides an overlay of the Metaschema module model - * ({@link gov.nist.secauto.metaschema.core.model}) representing module - * constructs as bound Java class annotations - * ({@link gov.nist.secauto.metaschema.databind.model.annotations}). + * ({@link gov.nist.secauto.metaschema.core.model}) using bound Java class + * annotations ({@link gov.nist.secauto.metaschema.databind.model.annotations}) + * to represent module constructs annotated on Java classes. */ package gov.nist.secauto.metaschema.databind.model; diff --git a/databind/src/main/java/gov/nist/secauto/metaschema/databind/package-info.java b/databind/src/main/java/gov/nist/secauto/metaschema/databind/package-info.java index 64de47654..ee1782849 100644 --- a/databind/src/main/java/gov/nist/secauto/metaschema/databind/package-info.java +++ b/databind/src/main/java/gov/nist/secauto/metaschema/databind/package-info.java @@ -24,7 +24,23 @@ * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. */ /** - * The core implementation of the metaschema binding framework. + * Support for parsing Metaschema module-based data using Java class bindings. + *

+ * Two methods are supported + *

    + *
  1. A Java annotation based approach + * ({@link gov.nist.secauto.metaschema.databind.model}) using annotations + * ({@link gov.nist.secauto.metaschema.databind.model.annotations}). The + * {@link gov.nist.secauto.metaschema.databind.DefaultBindingContext} is used to + * load a bound Java class.
  2. + *
  3. A metaschema-specific binding, based on the first method, that is capable + * of representing a Metaschema module + * ({@link gov.nist.secauto.metaschema.databind.model.metaschema.binding}). The + * {@link gov.nist.secauto.metaschema.databind.model.metaschema.BindingConstraintLoader} + * can be used to load any Metaschema module using this method. Once loaded, the + * module can be registered with the binding context using + * {@link gov.nist.secauto.metaschema.databind.IBindingContext#registerModule(gov.nist.secauto.metaschema.core.model.IModule, java.nio.file.Path)}. + *
*/ package gov.nist.secauto.metaschema.databind; diff --git a/databind/src/test/java/gov/nist/secauto/metaschema/databind/DefaultBindingContextTest.java b/databind/src/test/java/gov/nist/secauto/metaschema/databind/DefaultBindingContextTest.java index 431600079..66d952708 100644 --- a/databind/src/test/java/gov/nist/secauto/metaschema/databind/DefaultBindingContextTest.java +++ b/databind/src/test/java/gov/nist/secauto/metaschema/databind/DefaultBindingContextTest.java @@ -48,6 +48,8 @@ import java.nio.file.Paths; import java.util.List; +import javax.xml.namespace.QName; + class DefaultBindingContextTest { @Test @@ -61,7 +63,8 @@ void testConstraints() throws MetaschemaException, IOException { // NOPMD - inte IBindingContext bindingContext = new DefaultBindingContext(CollectionUtil.singletonList(postProcessor)); IBoundModule module = bindingContext.registerModule(TestMetaschema.class); - IAssemblyDefinition root = module.getExportedAssemblyDefinitionByName("root"); + IAssemblyDefinition root + = module.getExportedAssemblyDefinitionByName(new QName("https://csrc.nist.gov/ns/test/xml", "root")); assertNotNull(root, "root not found"); List constraints = root.getConstraints(); @@ -79,7 +82,8 @@ void testConstraintsUsingBinding() throws MetaschemaException, IOException { // IBindingContext bindingContext = new DefaultBindingContext(CollectionUtil.singletonList(postProcessor)); IBoundModule module = bindingContext.registerModule(TestMetaschema.class); - IAssemblyDefinition root = module.getExportedAssemblyDefinitionByName("root"); + IAssemblyDefinition root + = module.getExportedAssemblyDefinitionByName(new QName("https://csrc.nist.gov/ns/test/xml", "root")); assertNotNull(root, "root not found"); List constraints = root.getConstraints(); diff --git a/databind/src/test/java/gov/nist/secauto/metaschema/databind/codegen/AbstractMetaschemaTest.java b/databind/src/test/java/gov/nist/secauto/metaschema/databind/codegen/AbstractMetaschemaTest.java index c83c80fa9..3b77b3e82 100644 --- a/databind/src/test/java/gov/nist/secauto/metaschema/databind/codegen/AbstractMetaschemaTest.java +++ b/databind/src/test/java/gov/nist/secauto/metaschema/databind/codegen/AbstractMetaschemaTest.java @@ -28,6 +28,7 @@ import static org.junit.jupiter.api.Assertions.assertAll; +import gov.nist.secauto.metaschema.core.model.IMetaschemaModule; import gov.nist.secauto.metaschema.core.model.MetaschemaException; import gov.nist.secauto.metaschema.core.util.ObjectUtils; import gov.nist.secauto.metaschema.databind.DefaultBindingContext; @@ -37,7 +38,6 @@ import gov.nist.secauto.metaschema.databind.io.Format; import gov.nist.secauto.metaschema.databind.io.IDeserializer; import gov.nist.secauto.metaschema.databind.model.metaschema.BindingModuleLoader; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingModule; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -62,14 +62,14 @@ abstract class AbstractMetaschemaTest { Path generationDir = ObjectUtils.notNull(Paths.get("target/generated-test-sources/metaschema")); @NonNull - private static IBindingModule loadModule(@NonNull Path moduleFile) throws MetaschemaException, IOException { + private static IMetaschemaModule loadModule(@NonNull Path moduleFile) throws MetaschemaException, IOException { return LOADER.load(moduleFile); } public static Class compileModule(@NonNull Path moduleFile, @Nullable Path bindingFile, @NonNull String rootClassName, @NonNull Path classDir) throws IOException, ClassNotFoundException, MetaschemaException { - IBindingModule module = loadModule(moduleFile); + IMetaschemaModule module = loadModule(moduleFile); DefaultBindingConfiguration bindingConfiguration = new DefaultBindingConfiguration(); if (bindingFile != null && Files.exists(bindingFile) && Files.isRegularFile(bindingFile)) { @@ -93,8 +93,7 @@ private static Object read( throws IOException { IDeserializer deserializer = context.newDeserializer(format, rootClass); LOGGER.info("Reading content: {}", file); - Object value = deserializer.deserialize(file); - return value; + return deserializer.deserialize(file); } private static void write( diff --git a/databind/src/test/java/gov/nist/secauto/metaschema/databind/codegen/GenerationTest.java b/databind/src/test/java/gov/nist/secauto/metaschema/databind/codegen/GenerationTest.java index a46737557..13b42fb2b 100644 --- a/databind/src/test/java/gov/nist/secauto/metaschema/databind/codegen/GenerationTest.java +++ b/databind/src/test/java/gov/nist/secauto/metaschema/databind/codegen/GenerationTest.java @@ -29,12 +29,12 @@ import static org.junit.jupiter.api.Assertions.assertAll; import static org.junit.jupiter.api.Assertions.assertNotNull; +import gov.nist.secauto.metaschema.core.model.IMetaschemaModule; import gov.nist.secauto.metaschema.core.model.MetaschemaException; import gov.nist.secauto.metaschema.core.util.ObjectUtils; import gov.nist.secauto.metaschema.databind.IBindingContext; import gov.nist.secauto.metaschema.databind.io.DeserializationFeature; import gov.nist.secauto.metaschema.databind.model.metaschema.BindingModuleLoader; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingModule; import org.junit.jupiter.api.Test; @@ -48,7 +48,7 @@ public class GenerationTest { void testOscalBindingModuleLoader() throws MetaschemaException, IOException { BindingModuleLoader loader = new BindingModuleLoader(); loader.set(DeserializationFeature.DESERIALIZE_XML_ALLOW_ENTITY_RESOLUTION, true); - IBindingModule module = loader.load(ObjectUtils.notNull(URI.create( + IMetaschemaModule module = loader.load(ObjectUtils.notNull(URI.create( "https://raw.githubusercontent.com/usnistgov/OSCAL/main/src/metaschema/oscal_complete_metaschema.xml"))); IBindingContext context = IBindingContext.instance().registerModule(module, Paths.get("target/oscal-classes")); assertAll( diff --git a/databind/src/test/java/gov/nist/secauto/metaschema/databind/codegen/MetaschemaModuleMetaschemaTest.java b/databind/src/test/java/gov/nist/secauto/metaschema/databind/codegen/MetaschemaModuleMetaschemaTest.java index ba661572e..14260382a 100644 --- a/databind/src/test/java/gov/nist/secauto/metaschema/databind/codegen/MetaschemaModuleMetaschemaTest.java +++ b/databind/src/test/java/gov/nist/secauto/metaschema/databind/codegen/MetaschemaModuleMetaschemaTest.java @@ -28,8 +28,8 @@ import static org.junit.jupiter.api.Assertions.assertNotNull; +import gov.nist.secauto.metaschema.core.model.IMetaschemaModule; import gov.nist.secauto.metaschema.core.model.MetaschemaException; -import gov.nist.secauto.metaschema.core.model.xml.IXmlModule; import gov.nist.secauto.metaschema.core.model.xml.ModuleLoader; import gov.nist.secauto.metaschema.core.util.ObjectUtils; import gov.nist.secauto.metaschema.databind.IBindingContext; @@ -39,7 +39,6 @@ import gov.nist.secauto.metaschema.databind.io.IDeserializer; import gov.nist.secauto.metaschema.databind.io.ISerializer; import gov.nist.secauto.metaschema.databind.model.metaschema.BindingModuleLoader; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingModule; import gov.nist.secauto.metaschema.databind.model.metaschema.binding.METASCHEMA; import org.junit.jupiter.api.Test; @@ -112,7 +111,7 @@ void testReadMetaschemaAsXml() throws IOException { @Test void testModuleLoader() throws MetaschemaException, IOException { BindingModuleLoader loader = new BindingModuleLoader(); - IBindingModule module = loader.load(METASCHEMA_FILE); + IMetaschemaModule module = loader.load(METASCHEMA_FILE); assertNotNull(module); } @@ -120,7 +119,7 @@ void testModuleLoader() throws MetaschemaException, IOException { void testOscalBindingModuleLoader() throws MetaschemaException, IOException { BindingModuleLoader loader = new BindingModuleLoader(); loader.set(DeserializationFeature.DESERIALIZE_XML_ALLOW_ENTITY_RESOLUTION, true); - IBindingModule module = loader.load(ObjectUtils.notNull(URI.create( + IMetaschemaModule module = loader.load(ObjectUtils.notNull(URI.create( "https://raw.githubusercontent.com/usnistgov/OSCAL/main/src/metaschema/oscal_complete_metaschema.xml"))); assertNotNull(module); } @@ -130,7 +129,7 @@ void testOscalXmlModuleLoader() throws MetaschemaException, IOException { ModuleLoader loader = new ModuleLoader(); // loader.set(DeserializationFeature.DESERIALIZE_XML_ALLOW_ENTITY_RESOLUTION, // true); - IXmlModule module = loader.load(ObjectUtils.notNull(URI.create( + IMetaschemaModule module = loader.load(ObjectUtils.notNull(URI.create( "https://raw.githubusercontent.com/usnistgov/OSCAL/main/src/metaschema/oscal_complete_metaschema.xml"))); assertNotNull(module); } diff --git a/databind/src/test/java/gov/nist/secauto/metaschema/databind/io/json/JsonParserTest.java b/databind/src/test/java/gov/nist/secauto/metaschema/databind/io/json/JsonParserTest.java index 74687c984..12a42a603 100644 --- a/databind/src/test/java/gov/nist/secauto/metaschema/databind/io/json/JsonParserTest.java +++ b/databind/src/test/java/gov/nist/secauto/metaschema/databind/io/json/JsonParserTest.java @@ -28,20 +28,17 @@ import static org.junit.jupiter.api.Assertions.assertNotNull; +import gov.nist.secauto.metaschema.core.model.IMetaschemaModule; import gov.nist.secauto.metaschema.core.model.MetaschemaException; -import gov.nist.secauto.metaschema.core.model.xml.IXmlModule; import gov.nist.secauto.metaschema.core.model.xml.ModuleLoader; import gov.nist.secauto.metaschema.databind.IBindingContext; import gov.nist.secauto.metaschema.databind.io.DeserializationFeature; import gov.nist.secauto.metaschema.databind.io.IBoundLoader; import gov.nist.secauto.metaschema.databind.model.AbstractBoundModelTestSupport; -import gov.nist.secauto.metaschema.databind.model.metaschema.BindingModuleLoader; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingModule; import org.junit.jupiter.api.Test; import java.io.IOException; -import java.net.URI; import java.nio.file.Paths; class JsonParserTest @@ -49,7 +46,7 @@ class JsonParserTest @Test void testIssue308Regression() throws IOException, MetaschemaException { ModuleLoader moduleLoader = new ModuleLoader(); - IXmlModule module + IMetaschemaModule module = moduleLoader.load(Paths.get("src/test/resources/metaschema/308-choice-regression/metaschema.xml")); IBindingContext context = IBindingContext.instance(); diff --git a/databind/src/test/java/gov/nist/secauto/metaschema/databind/model/DefaultAssemblyClassBindingTest.java b/databind/src/test/java/gov/nist/secauto/metaschema/databind/model/DefaultAssemblyClassBindingTest.java index 72d19e0d9..4be2540a5 100644 --- a/databind/src/test/java/gov/nist/secauto/metaschema/databind/model/DefaultAssemblyClassBindingTest.java +++ b/databind/src/test/java/gov/nist/secauto/metaschema/databind/model/DefaultAssemblyClassBindingTest.java @@ -31,6 +31,7 @@ import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.core.JsonParser; +import gov.nist.secauto.metaschema.core.model.IModule; import gov.nist.secauto.metaschema.databind.io.json.MetaschemaJsonReader; import org.junit.jupiter.api.Test; @@ -61,7 +62,7 @@ void testMinimalJsonParse() throws JsonParseException, IOException { @Test void testModule() { IBoundDefinitionModelAssembly definition = getRootAssemblyClassBinding(); - IBoundModule module = definition.getContainingModule(); + IModule module = definition.getContainingModule(); assertNotNull(module, "metaschema was null"); } diff --git a/databind/src/test/java/gov/nist/secauto/metaschema/databind/model/JsonKeyTest.java b/databind/src/test/java/gov/nist/secauto/metaschema/databind/model/JsonKeyTest.java index 685a51ccc..e0aae3b2e 100644 --- a/databind/src/test/java/gov/nist/secauto/metaschema/databind/model/JsonKeyTest.java +++ b/databind/src/test/java/gov/nist/secauto/metaschema/databind/model/JsonKeyTest.java @@ -28,11 +28,11 @@ import static org.junit.jupiter.api.Assertions.assertNotNull; +import gov.nist.secauto.metaschema.core.model.IMetaschemaModule; import gov.nist.secauto.metaschema.core.model.MetaschemaException; import gov.nist.secauto.metaschema.core.util.ObjectUtils; import gov.nist.secauto.metaschema.databind.IBindingContext; import gov.nist.secauto.metaschema.databind.model.metaschema.BindingModuleLoader; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingModule; import org.junit.jupiter.api.Test; @@ -51,7 +51,7 @@ class JsonKeyTest @Test void testJsonKey() throws IOException, MetaschemaException { - IBindingModule module = new BindingModuleLoader().load(ObjectUtils.requireNonNull( + IMetaschemaModule module = new BindingModuleLoader().load(ObjectUtils.requireNonNull( Paths.get("src/test/resources/metaschema/json-key/metaschema.xml"))); IBindingContext bindingContext = IBindingContext.instance(); diff --git a/databind/src/test/java/gov/nist/secauto/metaschema/databind/model/test/SimpleAssembly.java b/databind/src/test/java/gov/nist/secauto/metaschema/databind/model/test/SimpleAssembly.java index 985a4485f..b9cc11213 100644 --- a/databind/src/test/java/gov/nist/secauto/metaschema/databind/model/test/SimpleAssembly.java +++ b/databind/src/test/java/gov/nist/secauto/metaschema/databind/model/test/SimpleAssembly.java @@ -33,8 +33,7 @@ import java.math.BigInteger; @SuppressWarnings("PMD") -@MetaschemaAssembly(name = "simple-assembly", rootName = "test", moduleClass = TestMetaschema.class, - rootNamespace = "http://example.com/ns") +@MetaschemaAssembly(name = "simple-assembly", rootName = "test", moduleClass = TestMetaschema.class) public class SimpleAssembly { @BoundFlag(name = "id") private String _id; diff --git a/databind/src/test/java/gov/nist/secauto/metaschema/databind/testing/model/ModelTestBase.java b/databind/src/test/java/gov/nist/secauto/metaschema/databind/testing/model/ModelTestBase.java index 4128e3722..d10e4a6e9 100644 --- a/databind/src/test/java/gov/nist/secauto/metaschema/databind/testing/model/ModelTestBase.java +++ b/databind/src/test/java/gov/nist/secauto/metaschema/databind/testing/model/ModelTestBase.java @@ -32,6 +32,8 @@ import static org.junit.jupiter.api.Assertions.assertTrue; import gov.nist.secauto.metaschema.core.datatype.IDataTypeAdapter; +import gov.nist.secauto.metaschema.core.datatype.markup.MarkupLine; +import gov.nist.secauto.metaschema.core.datatype.markup.MarkupMultiline; import gov.nist.secauto.metaschema.databind.IBindingContext; import gov.nist.secauto.metaschema.databind.model.IBoundDefinitionModelAssembly; import gov.nist.secauto.metaschema.databind.model.IBoundInstanceFlag; @@ -69,11 +71,11 @@ public static void assertAssemblyDefinition( "formalName"), () -> assertEquals( ModelUtil.resolveNoneOrValue(annotation.description()), - Optional.ofNullable(assembly.getDescription()).map(value -> value.toMarkdown()).orElse(null), + Optional.ofNullable(assembly.getDescription()).map(MarkupLine::toMarkdown).orElse(null), "description"), () -> assertEquals( ModelUtil.resolveNoneOrValue(annotation.remarks()), - Optional.ofNullable(assembly.getRemarks()).map(value -> value.toMarkdown()).orElse(null), + Optional.ofNullable(assembly.getRemarks()).map(MarkupMultiline::toMarkdown).orElse(null), "remarks"), () -> { String rootName = ModelUtil.resolveNoneOrValue(annotation.rootName()); @@ -84,9 +86,7 @@ public static void assertAssemblyDefinition( assembly.getRootName(), "rootName"), () -> assertEquals( - ModelUtil.resolveOptionalNamespace( - annotation.rootNamespace(), - () -> assembly.getContainingModule().getXmlNamespace().toASCIIString()), + assembly.getContainingModule().getXmlNamespace().toASCIIString(), assembly.getRootXmlQName().getNamespaceURI(), "rootNamespace"), () -> assertTrue(true)); @@ -138,11 +138,11 @@ public static void assertFlagInstance( "formalName"), () -> assertEquals( ModelUtil.resolveNoneOrValue(annotation.description()), - Optional.ofNullable(flag.getDescription()).map(value -> value.toMarkdown()).orElse(null), + Optional.ofNullable(flag.getDescription()).map(MarkupLine::toMarkdown).orElse(null), "description"), () -> assertEquals( ModelUtil.resolveNoneOrValue(annotation.remarks()), - Optional.ofNullable(flag.getRemarks()).map(value -> value.toMarkdown()).orElse(null), + Optional.ofNullable(flag.getRemarks()).map(MarkupMultiline::toMarkdown).orElse(null), "remarks")); } @@ -185,9 +185,7 @@ public static void assertFieldInstance( field.getDefaultValue(), "defaultValue"), () -> assertEquals( - ModelUtil.resolveOptionalNamespace( - annotation.namespace(), - () -> field.getContainingModule().getXmlNamespace().toASCIIString()), + field.getContainingModule().getXmlNamespace().toASCIIString(), field.getXmlNamespace(), "namespace"), () -> assertEquals( @@ -208,11 +206,11 @@ public static void assertFieldInstance( "formalName"), () -> assertEquals( ModelUtil.resolveNoneOrValue(annotation.description()), - Optional.ofNullable(field.getDescription()).map(value -> value.toMarkdown()).orElse(null), + Optional.ofNullable(field.getDescription()).map(MarkupLine::toMarkdown).orElse(null), "description"), () -> assertEquals( ModelUtil.resolveNoneOrValue(annotation.remarks()), - Optional.ofNullable(field.getRemarks()).map(value -> value.toMarkdown()).orElse(null), + Optional.ofNullable(field.getRemarks()).map(MarkupMultiline::toMarkdown).orElse(null), "remarks")); // groupAs } diff --git a/metaschema-cli/src/test/java/gov/nist/secauto/metaschema/cli/CLITest.java b/metaschema-cli/src/test/java/gov/nist/secauto/metaschema/cli/CLITest.java index 97c44158f..554a01be1 100644 --- a/metaschema-cli/src/test/java/gov/nist/secauto/metaschema/cli/CLITest.java +++ b/metaschema-cli/src/test/java/gov/nist/secauto/metaschema/cli/CLITest.java @@ -47,7 +47,7 @@ * Unit test for simple CLI. */ public class CLITest { - private static final ExitCode NO_EXPECTION_CLASS = null; + private static final ExitCode NO_EXCEPTION_CLASS = null; void evaluateResult(@NonNull ExitStatus status, @NonNull ExitCode expectedCode) { status.generateMessage(true); @@ -65,7 +65,6 @@ void evaluateResult(@NonNull ExitStatus status, @NonNull ExitCode expectedCode, } private static Stream providesValues() { - ExitCode NO_EXCEPTION_CLASS = null; List values = new LinkedList<>() { { add(Arguments.of(new String[] {}, ExitCode.INVALID_COMMAND, NO_EXCEPTION_CLASS)); diff --git a/schemagen/src/main/java/gov/nist/secauto/metaschema/schemagen/ModuleIndex.java b/schemagen/src/main/java/gov/nist/secauto/metaschema/schemagen/ModuleIndex.java index 3a6cb48cc..5a28346fe 100644 --- a/schemagen/src/main/java/gov/nist/secauto/metaschema/schemagen/ModuleIndex.java +++ b/schemagen/src/main/java/gov/nist/secauto/metaschema/schemagen/ModuleIndex.java @@ -269,7 +269,7 @@ public boolean isChoiceSibling() { public boolean isUsedAsJsonKey() { return references.stream() .anyMatch(ref -> ref instanceof INamedModelInstance - && ((INamedModelInstance) ref).getJsonKeyFlagName() != null); + && ((INamedModelInstance) ref).hasJsonKey()); } public boolean isUsedWithoutJsonKey() { @@ -277,12 +277,12 @@ public boolean isUsedWithoutJsonKey() { || references.isEmpty() || references.stream() .anyMatch(ref -> ref instanceof INamedModelInstance - && ((INamedModelInstance) ref).getJsonKeyFlagName() == null); + && !((INamedModelInstance) ref).hasJsonKey()); } public boolean isChoiceGroupMember() { return references.stream() - .anyMatch(ref -> ref instanceof INamedModelInstanceGrouped); + .anyMatch(INamedModelInstanceGrouped.class::isInstance); } } } diff --git a/schemagen/src/main/java/gov/nist/secauto/metaschema/schemagen/json/impl/FieldDefinitionJsonSchema.java b/schemagen/src/main/java/gov/nist/secauto/metaschema/schemagen/json/impl/FieldDefinitionJsonSchema.java index 4ecbc7759..b660f3bd0 100644 --- a/schemagen/src/main/java/gov/nist/secauto/metaschema/schemagen/json/impl/FieldDefinitionJsonSchema.java +++ b/schemagen/src/main/java/gov/nist/secauto/metaschema/schemagen/json/impl/FieldDefinitionJsonSchema.java @@ -69,9 +69,9 @@ protected void generateBody( Collection flags = definition.getFlagInstances(); String discriminatorProperty = getDiscriminatorProperty(); - IFlagInstance jsonKeyFlag = definition.getJsonKeyFlagInstance(); + IFlagInstance jsonKeyFlag = definition.getJsonKey(); if (discriminatorProperty == null - && (flags.isEmpty() || (jsonKeyFlag != null && flags.size() == 1))) { // NOPMD readability + && (flags.isEmpty() || jsonKeyFlag != null && flags.size() == 1)) { // NOPMD readability // field is a simple data type value if there are no flags or if the only flag // is a JSON key IDataTypeJsonSchema schema = state.getDataTypeSchemaForDefinition(definition); diff --git a/schemagen/src/main/java/gov/nist/secauto/metaschema/schemagen/json/impl/builder/AbstractCollectionBuilder.java b/schemagen/src/main/java/gov/nist/secauto/metaschema/schemagen/json/impl/builder/AbstractCollectionBuilder.java index f1e859b97..118e4bf24 100644 --- a/schemagen/src/main/java/gov/nist/secauto/metaschema/schemagen/json/impl/builder/AbstractCollectionBuilder.java +++ b/schemagen/src/main/java/gov/nist/secauto/metaschema/schemagen/json/impl/builder/AbstractCollectionBuilder.java @@ -144,18 +144,7 @@ private abstract static class Type implements IMo protected Type(@NonNull T instance) { this.namedModelInstance = instance; - String jsonKeyFlagName = instance.getJsonKeyFlagName(); - IFlagInstance jsonKey = null; - if (jsonKeyFlagName != null) { - IModelDefinition definition = instance.getDefinition(); - jsonKey = definition.getFlagInstanceByName( - definition.getContainingModule().toFlagQName(jsonKeyFlagName)); - - if (jsonKey == null) { - throw new IllegalStateException(String.format("No JSON key flag named '%s.", jsonKeyFlagName)); - } - } - this.jsonKeyFlag = jsonKey; + this.jsonKeyFlag = instance.getEffectiveJsonKey(); if (instance instanceof INamedModelInstanceGrouped) { INamedModelInstanceGrouped grouped = (INamedModelInstanceGrouped) instance; @@ -166,7 +155,11 @@ protected Type(@NonNull T instance) { this.discriminatorValue = null; } this.key - = IKey.of(instance.getDefinition(), jsonKeyFlagName, this.discriminatorProperty, this.discriminatorValue); + = IKey.of( + instance.getDefinition(), + jsonKeyFlag == null ? null : jsonKeyFlag.getName(), + this.discriminatorProperty, + this.discriminatorValue); } @NonNull diff --git a/schemagen/src/test/java/gov/nist/secauto/metaschema/schemagen/JsonSuiteTest.java b/schemagen/src/test/java/gov/nist/secauto/metaschema/schemagen/JsonSuiteTest.java index 2d27f243a..cfd08e8e4 100644 --- a/schemagen/src/test/java/gov/nist/secauto/metaschema/schemagen/JsonSuiteTest.java +++ b/schemagen/src/test/java/gov/nist/secauto/metaschema/schemagen/JsonSuiteTest.java @@ -34,7 +34,6 @@ import gov.nist.secauto.metaschema.core.model.validation.JsonSchemaContentValidator; import gov.nist.secauto.metaschema.databind.io.Format; import gov.nist.secauto.metaschema.databind.model.metaschema.BindingModuleLoader; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingModule; import gov.nist.secauto.metaschema.schemagen.json.JsonSchemaGenerator; import org.junit.jupiter.api.Disabled; @@ -135,7 +134,7 @@ void testOscalComplete() throws IOException, MetaschemaException { // NOPMD - de BindingModuleLoader loader = new BindingModuleLoader(); loader.allowEntityResolution(); - IBindingModule module = loader.load(new URL( + IModule module = loader.load(new URL( "https://raw.githubusercontent.com/usnistgov/OSCAL/develop/src/metaschema/oscal_complete_metaschema.xml")); ISchemaGenerator schemaGenerator = new JsonSchemaGenerator(); IMutableConfiguration> features @@ -156,7 +155,7 @@ void testTestMetaschema() throws IOException, MetaschemaException { // NOPMD - d BindingModuleLoader loader = new BindingModuleLoader(); loader.allowEntityResolution(); - IBindingModule module = loader.load(new URL( + IModule module = loader.load(new URL( "https://raw.githubusercontent.com/usnistgov/metaschema/71233f4eb6854e820c7949144e86afa4d7981b22/test-suite/metaschema-xspec/json-schema-gen/json-value-testing-mini-metaschema.xml")); ISchemaGenerator schemaGenerator = new JsonSchemaGenerator(); IMutableConfiguration> features = new DefaultConfiguration<>(); diff --git a/schemagen/src/test/java/gov/nist/secauto/metaschema/schemagen/MetaschemaModuleTest.java b/schemagen/src/test/java/gov/nist/secauto/metaschema/schemagen/MetaschemaModuleTest.java index 5f57d26c1..4fae0da85 100644 --- a/schemagen/src/test/java/gov/nist/secauto/metaschema/schemagen/MetaschemaModuleTest.java +++ b/schemagen/src/test/java/gov/nist/secauto/metaschema/schemagen/MetaschemaModuleTest.java @@ -28,10 +28,10 @@ import gov.nist.secauto.metaschema.core.configuration.DefaultConfiguration; import gov.nist.secauto.metaschema.core.configuration.IMutableConfiguration; +import gov.nist.secauto.metaschema.core.model.IModule; import gov.nist.secauto.metaschema.core.model.MetaschemaException; import gov.nist.secauto.metaschema.core.util.ObjectUtils; import gov.nist.secauto.metaschema.databind.model.metaschema.BindingModuleLoader; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingModule; import gov.nist.secauto.metaschema.schemagen.json.JsonSchemaGenerator; import gov.nist.secauto.metaschema.schemagen.xml.XmlSchemaGenerator; @@ -41,7 +41,6 @@ import java.io.Writer; import java.nio.charset.StandardCharsets; import java.nio.file.Files; -import java.nio.file.OpenOption; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.StandardOpenOption; @@ -57,7 +56,7 @@ class MetaschemaModuleTest { void testGenerateMetaschemaModuleJson() throws MetaschemaException, IOException { BindingModuleLoader loader = new BindingModuleLoader(); - IBindingModule module = loader.load(METASCHEMA_FILE); + IModule module = loader.load(METASCHEMA_FILE); IMutableConfiguration> features = new DefaultConfiguration<>(); @@ -67,11 +66,7 @@ void testGenerateMetaschemaModuleJson() throws MetaschemaException, IOException try (Writer writer = Files.newBufferedWriter( Path.of("target/metaschema-schema.json"), StandardCharsets.UTF_8, - new OpenOption[] { - StandardOpenOption.CREATE, - StandardOpenOption.WRITE, - StandardOpenOption.TRUNCATE_EXISTING - })) { + StandardOpenOption.CREATE, StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING)) { assert writer != null; ISchemaGenerator schemaGenerator = new JsonSchemaGenerator(); schemaGenerator.generateFromModule(module, writer, features); @@ -82,7 +77,7 @@ void testGenerateMetaschemaModuleJson() throws MetaschemaException, IOException void testGenerateMetaschemaModuleXml() throws MetaschemaException, IOException { BindingModuleLoader loader = new BindingModuleLoader(); - IBindingModule module = loader.load(METASCHEMA_FILE); + IModule module = loader.load(METASCHEMA_FILE); IMutableConfiguration> features = new DefaultConfiguration<>(); @@ -92,11 +87,7 @@ void testGenerateMetaschemaModuleXml() throws MetaschemaException, IOException { try (Writer writer = Files.newBufferedWriter( Path.of("target/metaschema-schema.xsd"), StandardCharsets.UTF_8, - new OpenOption[] { - StandardOpenOption.CREATE, - StandardOpenOption.WRITE, - StandardOpenOption.TRUNCATE_EXISTING - })) { + StandardOpenOption.CREATE, StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING)) { assert writer != null; ISchemaGenerator schemaGenerator = new XmlSchemaGenerator(); schemaGenerator.generateFromModule(module, writer, features); diff --git a/schemagen/src/test/java/gov/nist/secauto/metaschema/schemagen/XmlSuiteTest.java b/schemagen/src/test/java/gov/nist/secauto/metaschema/schemagen/XmlSuiteTest.java index ac7b439ec..580793793 100644 --- a/schemagen/src/test/java/gov/nist/secauto/metaschema/schemagen/XmlSuiteTest.java +++ b/schemagen/src/test/java/gov/nist/secauto/metaschema/schemagen/XmlSuiteTest.java @@ -36,7 +36,6 @@ import gov.nist.secauto.metaschema.core.model.validation.XmlSchemaContentValidator; import gov.nist.secauto.metaschema.databind.io.Format; import gov.nist.secauto.metaschema.databind.model.metaschema.BindingModuleLoader; -import gov.nist.secauto.metaschema.databind.model.metaschema.IBindingModule; import gov.nist.secauto.metaschema.schemagen.xml.XmlSchemaGenerator; import org.jdom2.Document; @@ -176,7 +175,7 @@ void testliboscalJavaIssue181() throws IOException, MetaschemaException, XMLStre BindingModuleLoader loader = new BindingModuleLoader(); loader.allowEntityResolution(); - IBindingModule module = loader.load(new URL( + IModule module = loader.load(new URL( // "https://raw.githubusercontent.com/usnistgov/OSCAL/develop/src/metaschema/oscal_complete_metaschema.xml")); "https://raw.githubusercontent.com/usnistgov/OSCAL/v1.1.1/src/metaschema/oscal_catalog_metaschema.xml")); ISchemaGenerator schemaGenerator = new XmlSchemaGenerator(); @@ -213,7 +212,7 @@ void testLiboscalJavaIssue181() throws IOException, MetaschemaException, XMLStre BindingModuleLoader loader = new BindingModuleLoader(); loader.allowEntityResolution(); - IBindingModule module = loader.load(new URL( + IModule module = loader.load(new URL( "https://raw.githubusercontent.com/usnistgov/OSCAL/v1.1.1/src/metaschema/oscal_catalog_metaschema.xml")); ISchemaGenerator schemaGenerator = new XmlSchemaGenerator(); IMutableConfiguration> features = new DefaultConfiguration<>(); diff --git a/src/site/site.xml b/src/site/site.xml index 643b3c61b..f5dc38049 100644 --- a/src/site/site.xml +++ b/src/site/site.xml @@ -12,7 +12,7 @@ - + io.github.devacfr.maven.skins @@ -115,5 +115,6 @@ +
\ No newline at end of file From 0a9e90134318bd4c0ddf0b02cb5dccfca512cf99 Mon Sep 17 00:00:00 2001 From: David Waltermire Date: Sun, 26 May 2024 19:39:41 -0400 Subject: [PATCH 05/13] Add support for Metapath arrays * Completed code to support Metapath array construction. * Implemented array postfix and unary lookups. * Cleaned up ISequence implementations, moving getFirstItem to be a method member. Added a utility function to convert a sequence into an array member. * Added support for the following Metapath functions: - array:get - array:put - array:append - array:subarray - array:remove - array:insert-before - array:join - array:head - array:tail - array:reverse - array:flatten * Fixed spotbugs null check warnings * Cleaned up some Javadocs. * Incorporated changes based on CodeRabbit review. * normalized line endings --- .gitattributes | 9 + .../cli/processor/CLIProcessor.java | 38 +- .../command/AbstractParentCommand.java | 4 +- core/src/main/antlr4/Metapath10.g4 | 19 +- .../core/datatype/DataTypeService.java | 76 +-- .../markup/flexmark/MarkupXmlEventWriter.java | 1 + .../flexmark/MarkupXmlStreamWriter.java | 2 + .../{ => impl}/AbstractMarkupWriter.java | 12 +- .../AbstractCodedMetapathException.java | 4 +- .../metapath/DynamicMetapathException.java | 2 +- .../core/metapath/ICollectionValue.java | 48 ++ .../metaschema/core/metapath/ISequence.java | 604 ++++++++++++------ .../core/metapath/IStringValued.java | 39 ++ .../core/metapath/MetapathConstants.java | 6 +- .../core/metapath/MetapathExpression.java | 6 +- .../core/metapath/StaticContext.java | 3 + .../metapath/StaticMetapathException.java | 2 +- .../core/metapath/TypeMetapathException.java | 2 +- .../metapath/antlr/AbstractAstVisitor.java | 249 ++++++-- .../metapath/cst/AbstractCSTVisitorBase.java | 28 +- .../core/metapath/cst/AbstractExpression.java | 6 +- .../cst/AbstractExpressionVisitor.java | 25 + .../cst/AbstractFilterExpression.java | 2 +- .../core/metapath/cst/AbstractLookup.java | 180 ++++++ .../core/metapath/cst/ArraySequence.java | 79 +++ .../core/metapath/cst/ArraySquare.java | 64 ++ .../core/metapath/cst/BuildCSTVisitor.java | 347 +++++----- .../core/metapath/cst/CSTPrinter.java | 25 + .../metaschema/core/metapath/cst/Except.java | 2 +- .../metaschema/core/metapath/cst/For.java | 2 +- .../metapath/cst/FunctionCallAccessor.java | 88 +++ .../core/metapath/cst/IExpressionVisitor.java | 55 ++ .../core/metapath/cst/Intersect.java | 2 +- .../core/metapath/cst/Metapath.java | 2 +- .../core/metapath/cst/PostfixLookup.java | 80 +++ .../metapath/cst/PredicateExpression.java | 2 +- .../metaschema/core/metapath/cst/Range.java | 2 +- .../core/metapath/cst/SimpleMap.java | 2 +- .../core/metapath/cst/StringConcat.java | 2 +- .../core/metapath/cst/UnaryLookup.java | 64 ++ .../metaschema/core/metapath/cst/Union.java | 7 +- .../cst/path/AbstractPathExpression.java | 8 +- .../core/metapath/cst/path/Axis.java | 2 +- .../core/metapath/cst/path/Flag.java | 2 +- .../core/metapath/cst/path/ModelInstance.java | 2 +- .../core/metapath/cst/path/NameTest.java | 2 +- .../metapath/cst/path/RootSlashOnlyPath.java | 2 +- .../core/metapath/cst/path/RootSlashPath.java | 5 +- .../core/metapath/cst/path/Wildcard.java | 2 +- .../metapath/function/AbstractFunction.java | 22 +- .../function/ArithmeticFunctionException.java | 2 +- .../function/ComparisonFunctions.java | 4 +- .../function/DateTimeFunctionException.java | 2 +- .../metapath/function/DefaultFunction.java | 72 +-- .../function/DocumentFunctionException.java | 2 +- .../metapath/function/FunctionService.java | 18 +- .../core/metapath/function/FunctionUtils.java | 74 +-- .../core/metapath/function/IFunction.java | 24 +- .../core/metapath/function/ISequenceType.java | 6 + .../InvalidArgumentFunctionException.java | 2 +- .../InvalidTypeFunctionException.java | 2 +- .../metapath/function/SequenceTypeImpl.java | 4 +- .../function/UriFunctionException.java | 2 +- .../function/library/ArrayAppend.java | 100 +++ .../function/library/ArrayFlatten.java | 95 +++ .../metapath/function/library/ArrayGet.java | 109 ++++ .../metapath/function/library/ArrayHead.java | 86 +++ .../function/library/ArrayInsertBefore.java | 117 ++++ .../metapath/function/library/ArrayJoin.java | 89 +++ .../metapath/function/library/ArrayPut.java | 127 ++++ .../function/library/ArrayRemove.java | 121 ++++ .../function/library/ArrayReverse.java | 90 +++ .../metapath/function/library/ArraySize.java | 77 +++ .../function/library/ArraySubarray.java | 194 ++++++ .../metapath/function/library/ArrayTail.java | 84 +++ .../function/library/CastFunction.java | 2 +- .../library/DefaultFunctionLibrary.java | 32 + .../core/metapath/function/library/FnAbs.java | 5 +- .../core/metapath/function/library/FnAvg.java | 2 +- .../metapath/function/library/FnBaseUri.java | 2 +- .../metapath/function/library/FnBoolean.java | 3 +- .../metapath/function/library/FnCeiling.java | 2 +- .../metapath/function/library/FnCompare.java | 8 +- .../metapath/function/library/FnConcat.java | 3 +- .../metapath/function/library/FnData.java | 66 +- .../core/metapath/function/library/FnDoc.java | 2 +- .../function/library/FnDocumentUri.java | 28 +- .../function/library/FnInsertBefore.java | 7 +- .../metapath/function/library/FnMinMax.java | 4 +- .../metapath/function/library/FnPath.java | 2 +- .../metapath/function/library/FnRemove.java | 6 +- .../function/library/FnResolveUri.java | 6 +- .../metapath/function/library/FnReverse.java | 2 +- .../metapath/function/library/FnRound.java | 9 +- .../function/library/FnStartsWith.java | 8 +- .../core/metapath/function/library/FnSum.java | 12 +- .../metapath/function/library/FnTail.java | 2 +- .../function/library/MpRecurseDepth.java | 8 +- .../function/library/NumericFunction.java | 2 +- .../core/metapath/impl/AbstractArrayItem.java | 105 +++ .../AbstractSequence.java} | 75 +-- .../core/metapath/impl/ArrayItemN.java | 56 ++ .../metapath/impl/ImmutableCollections.java | 170 +++++ .../SequenceN.java} | 60 +- .../SingletonSequence.java} | 51 +- .../StreamSequence.java} | 59 +- .../metaschema/core/metapath/item/IItem.java | 17 +- .../metapath/item/atomic/IAnyAtomicItem.java | 4 +- .../metapath/item/atomic/IDecimalItem.java | 3 +- .../metapath/item/atomic/IIntegerItem.java | 14 + .../item/function/ArrayException.java | 101 +++ .../metapath/item/function/IArrayItem.java | 573 +++++++++++++++++ .../item/node/DefaultNodeItemFactory.java | 67 +- .../metaschema/core/model/IGroupable.java | 2 +- .../DefaultConstraintValidator.java | 12 +- ...CollectingConstraintValidationHandler.java | 4 +- .../model/xml/impl/XmlbeansMarkupVisitor.java | 2 +- .../core/metapath/ISequenceTest.java | 55 ++ .../core/metapath/MetapathExpressionTest.java | 2 +- .../metaschema/core/metapath/TestUtils.java | 23 + .../metapath/cst/BuildCstVisitorTest.java | 13 +- .../metapath/cst/PredicateExpressionTest.java | 8 +- .../function/library/ArrayAppendTest.java | 65 ++ .../function/library/ArrayFlattenTest.java | 68 ++ .../function/library/ArrayGetTest.java | 65 ++ .../function/library/ArrayHeadTest.java | 71 ++ .../library/ArrayInsertBeforeTest.java | 68 ++ .../function/library/ArrayJoinTest.java | 74 +++ .../function/library/ArrayPutTest.java | 69 ++ .../function/library/ArrayRemoveTest.java | 74 +++ .../function/library/ArrayReverseTest.java | 72 +++ .../function/library/ArraySizeTest.java | 71 ++ .../function/library/ArraySubarrayTest.java | 79 +++ .../function/library/ArrayTailTest.java | 70 ++ .../function/library/FunctionTestBase.java | 4 +- .../item/function/IArrayItemTest.java | 167 +++++ .../model/xml/MetaConstraintLoaderTest.java | 2 +- core/src/test/resources/log4j2-test.xml | 2 +- .../codegen/impl/AnnotationGenerator.java | 2 +- .../databind/metapath/function/Model.java | 2 +- .../services/javax.xml.bind.JAXBContext | 2 +- ...it.jupiter.api.extension.Extension-removed | 1 - .../ValidateContentUsingModuleCommand.java | 2 +- .../xml-outline.html | 100 +-- .../src/it/generate-sources/verify.groovy | 2 +- .../maven/plugin/AbstractMetaschemaMojo.java | 20 +- .../maven/plugin/GenerateSchemaMojo.java | 29 +- .../maven/plugin/GenerateSourcesMojo.java | 44 +- pom.xml | 15 + 149 files changed, 5592 insertions(+), 1089 deletions(-) create mode 100644 .gitattributes rename core/src/main/java/gov/nist/secauto/metaschema/core/datatype/markup/flexmark/{ => impl}/AbstractMarkupWriter.java (98%) create mode 100644 core/src/main/java/gov/nist/secauto/metaschema/core/metapath/ICollectionValue.java create mode 100644 core/src/main/java/gov/nist/secauto/metaschema/core/metapath/IStringValued.java create mode 100644 core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/AbstractLookup.java create mode 100644 core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/ArraySequence.java create mode 100644 core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/ArraySquare.java create mode 100644 core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/FunctionCallAccessor.java create mode 100644 core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/PostfixLookup.java create mode 100644 core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/UnaryLookup.java create mode 100644 core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/ArrayAppend.java create mode 100644 core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/ArrayFlatten.java create mode 100644 core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/ArrayGet.java create mode 100644 core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/ArrayHead.java create mode 100644 core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/ArrayInsertBefore.java create mode 100644 core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/ArrayJoin.java create mode 100644 core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/ArrayPut.java create mode 100644 core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/ArrayRemove.java create mode 100644 core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/ArrayReverse.java create mode 100644 core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/ArraySize.java create mode 100644 core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/ArraySubarray.java create mode 100644 core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/ArrayTail.java create mode 100644 core/src/main/java/gov/nist/secauto/metaschema/core/metapath/impl/AbstractArrayItem.java rename core/src/main/java/gov/nist/secauto/metaschema/core/metapath/{SingletonSequenceImpl.java => impl/AbstractSequence.java} (66%) create mode 100644 core/src/main/java/gov/nist/secauto/metaschema/core/metapath/impl/ArrayItemN.java create mode 100644 core/src/main/java/gov/nist/secauto/metaschema/core/metapath/impl/ImmutableCollections.java rename core/src/main/java/gov/nist/secauto/metaschema/core/metapath/{ListSequenceImpl.java => impl/SequenceN.java} (64%) rename core/src/main/java/gov/nist/secauto/metaschema/core/metapath/{EmptyListImpl.java => impl/SingletonSequence.java} (73%) rename core/src/main/java/gov/nist/secauto/metaschema/core/metapath/{StreamSequenceImpl.java => impl/StreamSequence.java} (68%) create mode 100644 core/src/main/java/gov/nist/secauto/metaschema/core/metapath/item/function/ArrayException.java create mode 100644 core/src/main/java/gov/nist/secauto/metaschema/core/metapath/item/function/IArrayItem.java create mode 100644 core/src/test/java/gov/nist/secauto/metaschema/core/metapath/ISequenceTest.java create mode 100644 core/src/test/java/gov/nist/secauto/metaschema/core/metapath/function/library/ArrayAppendTest.java create mode 100644 core/src/test/java/gov/nist/secauto/metaschema/core/metapath/function/library/ArrayFlattenTest.java create mode 100644 core/src/test/java/gov/nist/secauto/metaschema/core/metapath/function/library/ArrayGetTest.java create mode 100644 core/src/test/java/gov/nist/secauto/metaschema/core/metapath/function/library/ArrayHeadTest.java create mode 100644 core/src/test/java/gov/nist/secauto/metaschema/core/metapath/function/library/ArrayInsertBeforeTest.java create mode 100644 core/src/test/java/gov/nist/secauto/metaschema/core/metapath/function/library/ArrayJoinTest.java create mode 100644 core/src/test/java/gov/nist/secauto/metaschema/core/metapath/function/library/ArrayPutTest.java create mode 100644 core/src/test/java/gov/nist/secauto/metaschema/core/metapath/function/library/ArrayRemoveTest.java create mode 100644 core/src/test/java/gov/nist/secauto/metaschema/core/metapath/function/library/ArrayReverseTest.java create mode 100644 core/src/test/java/gov/nist/secauto/metaschema/core/metapath/function/library/ArraySizeTest.java create mode 100644 core/src/test/java/gov/nist/secauto/metaschema/core/metapath/function/library/ArraySubarrayTest.java create mode 100644 core/src/test/java/gov/nist/secauto/metaschema/core/metapath/function/library/ArrayTailTest.java create mode 100644 core/src/test/java/gov/nist/secauto/metaschema/core/metapath/item/function/IArrayItemTest.java delete mode 100644 databind/src/test/resources/META-INF/services/org.junit.jupiter.api.extension.Extension-removed diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 000000000..6d826dcd1 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,9 @@ +# To list all file extensions: +# git ls-files | awk -F . {'print $NF'}|sort -u +# +# Set the default behavior, in case people don't have core.autocrlf set. +* text=auto + +# files that are binary +*.png +*.jpg diff --git a/cli-processor/src/main/java/gov/nist/secauto/metaschema/cli/processor/CLIProcessor.java b/cli-processor/src/main/java/gov/nist/secauto/metaschema/cli/processor/CLIProcessor.java index 95389d82c..36a318b26 100644 --- a/cli-processor/src/main/java/gov/nist/secauto/metaschema/cli/processor/CLIProcessor.java +++ b/cli-processor/src/main/java/gov/nist/secauto/metaschema/cli/processor/CLIProcessor.java @@ -196,7 +196,7 @@ private ExitStatus parseCommand(String... args) { return status; } - protected List getTopLevelCommands() { + protected final List getTopLevelCommands() { List retval = Collections.unmodifiableList(commands); assert retval != null; return retval; @@ -221,7 +221,7 @@ public static void handleQuiet() { protected void showVersion() { @SuppressWarnings("resource") PrintStream out = AnsiConsole.out(); // NOPMD - not owner - getVersionInfos().stream().forEach((info) -> { + getVersionInfos().stream().forEach(info -> { out.println(ansi() .bold().a(info.getName()).boldOff() .a(" ") @@ -265,35 +265,31 @@ public CallingContext(@NonNull List args) { boolean endArgs = false; for (String arg : args) { - if (endArgs) { + if (endArgs || arg.startsWith("-")) { extraArgs.add(arg); + } else if ("--".equals(arg)) { + endArgs = true; } else { - if (arg.startsWith("-")) { + ICommand command; + if (calledCommands.isEmpty()) { + command = topLevelCommandMap.get(arg); + } else { + command = calledCommands.getLast(); + command = command.getSubCommandByName(arg); + } + + if (command == null) { extraArgs.add(arg); - } else if ("--".equals(arg)) { endArgs = true; } else { - ICommand command; - if (calledCommands.isEmpty()) { - command = topLevelCommandMap.get(arg); - } else { - command = calledCommands.getLast(); - command = command.getSubCommandByName(arg); - } - - if (command == null) { - extraArgs.add(arg); - endArgs = true; - } else { - calledCommands.add(command); - } + calledCommands.add(command); } } } if (LOGGER.isDebugEnabled()) { String commandChain = calledCommands.stream() - .map(command -> command.getName()) + .map(ICommand::getName) .collect(Collectors.joining(" -> ")); LOGGER.debug("Processing command chain: {}", commandChain); } @@ -548,7 +544,7 @@ protected String buildHelpCliSyntax() { // output required options getOptionsList().stream() - .filter(option -> option.isRequired()) + .filter(Option::isRequired) .forEach(option -> { builder .append(' ') diff --git a/cli-processor/src/main/java/gov/nist/secauto/metaschema/cli/processor/command/AbstractParentCommand.java b/cli-processor/src/main/java/gov/nist/secauto/metaschema/cli/processor/command/AbstractParentCommand.java index dc3f72b9a..0416006e3 100644 --- a/cli-processor/src/main/java/gov/nist/secauto/metaschema/cli/processor/command/AbstractParentCommand.java +++ b/cli-processor/src/main/java/gov/nist/secauto/metaschema/cli/processor/command/AbstractParentCommand.java @@ -51,7 +51,7 @@ protected AbstractParentCommand(boolean subCommandRequired) { this.subCommandRequired = subCommandRequired; } - protected void addCommandHandler(ICommand handler) { + protected final void addCommandHandler(ICommand handler) { String commandName = handler.getName(); this.commandToSubcommandHandlerMap.put(commandName, handler); } @@ -87,7 +87,7 @@ protected ExitStatus executeCommand( status = ExitCode.INVALID_COMMAND .exitMessage("Please use one of the following sub-commands: " + getSubCommands().stream() - .map(command -> command.getName()) + .map(ICommand::getName) .collect(Collectors.joining(", "))); } else { status = ExitCode.OK.exit(); diff --git a/core/src/main/antlr4/Metapath10.g4 b/core/src/main/antlr4/Metapath10.g4 index 79e006469..acea52be3 100644 --- a/core/src/main/antlr4/Metapath10.g4 +++ b/core/src/main/antlr4/Metapath10.g4 @@ -13,7 +13,7 @@ metapath : expr EOF ; // param : DOLLAR eqname typedeclaration? ; // functionbody : enclosedexpr ; // [5] -// enclosedexpr : OC expr? CC ; +enclosedexpr : OC expr? CC ; expr : exprsingle ( COMMA exprsingle)* ; exprsingle : forexpr | letexpr | quantifiedexpr | ifexpr | orexpr ; forexpr : simpleforclause KW_RETURN exprsingle ; @@ -70,19 +70,18 @@ abbrevreversestep : DD ; nodetest : nametest ; nametest : eqname | wildcard ; wildcard : STAR | NCName CS | SC NCName | BracedURILiteral STAR ; -// postfixexpr : primaryexpr (predicate | argumentlist | lookup)* ; -postfixexpr : primaryexpr (predicate)* ; +postfixexpr : primaryexpr (predicate | argumentlist | lookup)* ; // [50] argumentlist : OP (argument ( COMMA argument)*)? CP ; predicatelist : predicate* ; predicate : OB expr CB ; -// lookup : QM keyspecifier ; -// keyspecifier : NCName | IntegerLiteral | parenthesizedexpr | STAR ; +lookup : QM keyspecifier ; +keyspecifier : NCName | IntegerLiteral | parenthesizedexpr | STAR ; // [55] //arrowfunctionspecifier : eqname | varref | parenthesizedexpr ; arrowfunctionspecifier : eqname; // primaryexpr : literal | varref | parenthesizedexpr | contextitemexpr | functioncall | functionitemexpr | mapconstructor | arrayconstructor | unarylookup ; -primaryexpr : literal | varref | parenthesizedexpr | contextitemexpr | functioncall ; +primaryexpr : literal | varref | parenthesizedexpr | contextitemexpr | functioncall | arrayconstructor | unarylookup; literal : numericliteral | StringLiteral ; numericliteral : IntegerLiteral | DecimalLiteral | DoubleLiteral ; varref : DOLLAR varname ; @@ -103,11 +102,11 @@ argument : exprsingle ; // mapconstructorentry : mapkeyexpr COLON mapvalueexpr ; // mapkeyexpr : exprsingle ; // mapvalueexpr : exprsingle ; -// arrayconstructor : squarearrayconstructor | curlyarrayconstructor ; -// squarearrayconstructor : OB (exprsingle ( COMMA exprsingle)*)? CB ; +arrayconstructor : squarearrayconstructor | curlyarrayconstructor ; +squarearrayconstructor : OB (exprsingle ( COMMA exprsingle)*)? CB ; // [75] -// curlyarrayconstructor : KW_ARRAY enclosedexpr ; -// unarylookup : QM keyspecifier ; +curlyarrayconstructor : KW_ARRAY enclosedexpr ; +unarylookup : QM keyspecifier ; // singletype : simpletypename QM? ; // typedeclaration : KW_AS sequencetype ; // sequencetype : KW_EMPTY_SEQUENCE OP CP | itemtype occurrenceindicator? ; diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/DataTypeService.java b/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/DataTypeService.java index 84db86536..305b0cdaf 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/DataTypeService.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/DataTypeService.java @@ -51,8 +51,8 @@ public final class DataTypeService { private static final Logger LOGGER = LogManager.getLogger(DataTypeService.class); private static final Lazy INSTANCE = Lazy.lazy(() -> new DataTypeService()); - private Map> libraryByName; - private Map>, IDataTypeAdapter> libraryByClass; + private final Map> libraryByName; + private final Map>, IDataTypeAdapter> libraryByClass; /** * Get the singleton service instance, which will be lazy constructed on first @@ -67,45 +67,7 @@ public static DataTypeService getInstance() { } private DataTypeService() { - load(); - } - - /** - * Lookup a specific {@link IDataTypeAdapter} instance by its name. - * - * @param name - * the data type name of data type adapter to get the instance for - * @return the instance or {@code null} if the instance is unknown to the type - * system - */ - @Nullable - public IDataTypeAdapter getJavaTypeAdapterByName(@NonNull String name) { - return libraryByName.get(name); - } - /** - * Lookup a specific {@link IDataTypeAdapter} instance by its class. - * - * @param clazz - * the adapter class to get the instance for - * @param - * the type of the requested adapter - * @return the instance or {@code null} if the instance is unknown to the type - * system - */ - @SuppressWarnings("unchecked") - @Nullable - public > TYPE getJavaTypeAdapterByClass(@NonNull Class clazz) { - return (TYPE) libraryByClass.get(clazz); - } - - /** - * Load available data types registered with the {@link IDataTypeProvider} SPI. - * - * @throws IllegalStateException - * if there are two adapters with the same name - */ - private void load() { ServiceLoader loader = ServiceLoader.load(IDataTypeProvider.class); List> dataTypes = loader.stream() .map(Provider::get) @@ -142,10 +104,36 @@ private void load() { return v1; }, ConcurrentHashMap::new)); + this.libraryByName = libraryByName; + this.libraryByClass = libraryByClass; + } + + /** + * Lookup a specific {@link IDataTypeAdapter} instance by its name. + * + * @param name + * the data type name of data type adapter to get the instance for + * @return the instance or {@code null} if the instance is unknown to the type + * system + */ + @Nullable + public IDataTypeAdapter getJavaTypeAdapterByName(@NonNull String name) { + return libraryByName.get(name); + } - synchronized (this) { - this.libraryByName = libraryByName; - this.libraryByClass = libraryByClass; - } + /** + * Lookup a specific {@link IDataTypeAdapter} instance by its class. + * + * @param clazz + * the adapter class to get the instance for + * @param + * the type of the requested adapter + * @return the instance or {@code null} if the instance is unknown to the type + * system + */ + @SuppressWarnings("unchecked") + @Nullable + public > TYPE getJavaTypeAdapterByClass(@NonNull Class clazz) { + return (TYPE) libraryByClass.get(clazz); } } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/markup/flexmark/MarkupXmlEventWriter.java b/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/markup/flexmark/MarkupXmlEventWriter.java index 16806f8e2..9c5dbd9d7 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/markup/flexmark/MarkupXmlEventWriter.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/markup/flexmark/MarkupXmlEventWriter.java @@ -28,6 +28,7 @@ import com.vladsch.flexmark.parser.ListOptions; +import gov.nist.secauto.metaschema.core.datatype.markup.flexmark.impl.AbstractMarkupWriter; import gov.nist.secauto.metaschema.core.util.CollectionUtil; import gov.nist.secauto.metaschema.core.util.ObjectUtils; diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/markup/flexmark/MarkupXmlStreamWriter.java b/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/markup/flexmark/MarkupXmlStreamWriter.java index 3f79e7f4f..6c9d526cc 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/markup/flexmark/MarkupXmlStreamWriter.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/markup/flexmark/MarkupXmlStreamWriter.java @@ -28,6 +28,8 @@ import com.vladsch.flexmark.parser.ListOptions; +import gov.nist.secauto.metaschema.core.datatype.markup.flexmark.impl.AbstractMarkupWriter; + import java.util.Map; import javax.xml.namespace.QName; diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/markup/flexmark/AbstractMarkupWriter.java b/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/markup/flexmark/impl/AbstractMarkupWriter.java similarity index 98% rename from core/src/main/java/gov/nist/secauto/metaschema/core/datatype/markup/flexmark/AbstractMarkupWriter.java rename to core/src/main/java/gov/nist/secauto/metaschema/core/datatype/markup/flexmark/impl/AbstractMarkupWriter.java index 056edf3ca..49ef7c5a9 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/markup/flexmark/AbstractMarkupWriter.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/datatype/markup/flexmark/impl/AbstractMarkupWriter.java @@ -24,7 +24,7 @@ * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. */ -package gov.nist.secauto.metaschema.core.datatype.markup.flexmark; // NOPMD AST processor has many members +package gov.nist.secauto.metaschema.core.datatype.markup.flexmark.impl; // NOPMD AST processor has many members import com.vladsch.flexmark.ast.AutoLink; import com.vladsch.flexmark.ast.BlockQuote; @@ -63,7 +63,9 @@ import com.vladsch.flexmark.util.sequence.BasedSequence; import com.vladsch.flexmark.util.sequence.Escaping; +import gov.nist.secauto.metaschema.core.datatype.markup.flexmark.IMarkupWriter; import gov.nist.secauto.metaschema.core.datatype.markup.flexmark.HtmlQuoteTagExtension.DoubleQuoteNode; +import gov.nist.secauto.metaschema.core.datatype.markup.flexmark.IMarkupWriter.ChildHandler; import gov.nist.secauto.metaschema.core.datatype.markup.flexmark.InsertAnchorExtension.InsertAnchorNode; import gov.nist.secauto.metaschema.core.util.CollectionUtil; import gov.nist.secauto.metaschema.core.util.ObjectUtils; @@ -111,9 +113,11 @@ public abstract class AbstractMarkupWriter // NOPMD not // normal cases // ENTITY_MAP.put("&", "&"); /* - * ENTITY_MAP.put("‘", "‘"); ENTITY_MAP.put("’", "’"); ENTITY_MAP.put("…", "…"); - * ENTITY_MAP.put("—", "—"); ENTITY_MAP.put("–", "–"); ENTITY_MAP.put("“", "“"); - * ENTITY_MAP.put("”", "”"); ENTITY_MAP.put("«", "«"); ENTITY_MAP.put("»", "»"); + * ENTITY_MAP.put("‘", "‘"); ENTITY_MAP.put("’", "’"); + * ENTITY_MAP.put("…", "…"); ENTITY_MAP.put("—", "—"); + * ENTITY_MAP.put("–", "–"); ENTITY_MAP.put("“", "“"); + * ENTITY_MAP.put("”", "”"); ENTITY_MAP.put("«", "«"); + * ENTITY_MAP.put("»", "»"); */ } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/AbstractCodedMetapathException.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/AbstractCodedMetapathException.java index 64b1b3399..5b822836e 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/AbstractCodedMetapathException.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/AbstractCodedMetapathException.java @@ -97,7 +97,7 @@ public String getMessage() { * * @return the error code value */ - protected int getCode() { + public int getCode() { return code; } @@ -106,7 +106,7 @@ protected int getCode() { * * @return the error code family */ - protected abstract String getCodePrefix(); + public abstract String getCodePrefix(); /** * Get a combination of the error code family and value. diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/DynamicMetapathException.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/DynamicMetapathException.java index df5f8fcc0..2b432fad5 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/DynamicMetapathException.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/DynamicMetapathException.java @@ -105,7 +105,7 @@ public DynamicMetapathException(int code, Throwable cause) { } @Override - protected String getCodePrefix() { + public String getCodePrefix() { return "MPDY"; } } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/ICollectionValue.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/ICollectionValue.java new file mode 100644 index 000000000..1293ee9aa --- /dev/null +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/ICollectionValue.java @@ -0,0 +1,48 @@ +/* + * Portions of this software was developed by employees of the National Institute + * of Standards and Technology (NIST), an agency of the Federal Government and is + * being made available as a public service. Pursuant to title 17 United States + * Code Section 105, works of NIST employees are not subject to copyright + * protection in the United States. This software may be subject to foreign + * copyright. Permission in the United States and in foreign countries, to the + * extent that NIST may hold copyright, to use, copy, modify, create derivative + * works, and distribute this software and its documentation without fee is hereby + * granted on a non-exclusive basis, provided that this notice and disclaimer + * of warranty appears in all copies. + * + * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER + * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY + * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM + * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE + * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT + * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, + * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, + * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, + * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR + * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT + * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. + */ + +package gov.nist.secauto.metaschema.core.metapath; + +import gov.nist.secauto.metaschema.core.metapath.item.IItem; +import gov.nist.secauto.metaschema.core.util.ObjectUtils; + +import java.util.stream.Stream; + +import edu.umd.cs.findbugs.annotations.NonNull; + +public interface ICollectionValue { + // TODO: rename to toSequence and resolve conflicting methods? + @NonNull + ISequence asSequence(); + + @NonNull + static Stream normalizeAsItems(@NonNull ICollectionValue value) { + return value instanceof IItem ? ObjectUtils.notNull(Stream.of((IItem) value)) : value.asSequence().stream(); + } + + @NonNull + Stream flatten(); +} diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/ISequence.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/ISequence.java index 229b65f6e..b93b84050 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/ISequence.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/ISequence.java @@ -26,20 +26,22 @@ package gov.nist.secauto.metaschema.core.metapath; +import gov.nist.secauto.metaschema.core.metapath.impl.AbstractSequence; +import gov.nist.secauto.metaschema.core.metapath.impl.SequenceN; +import gov.nist.secauto.metaschema.core.metapath.impl.SingletonSequence; +import gov.nist.secauto.metaschema.core.metapath.impl.StreamSequence; import gov.nist.secauto.metaschema.core.metapath.item.IItem; +import gov.nist.secauto.metaschema.core.metapath.item.function.IArrayItem; import gov.nist.secauto.metaschema.core.util.ObjectUtils; import java.util.ArrayList; -import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.Iterator; import java.util.List; -import java.util.ListIterator; import java.util.Set; import java.util.function.BiConsumer; import java.util.function.BinaryOperator; -import java.util.function.Consumer; import java.util.function.Function; import java.util.function.Supplier; import java.util.stream.Collector; @@ -54,125 +56,118 @@ * Items is a sequence are typically ordered based on their position in the * original node graph based on a depth first ordering. * - * @param + * @param * the Java type of the items in a sequence */ -public interface ISequence extends List { - @SuppressWarnings("rawtypes") - ISequence EMPTY = new EmptyListImpl<>(); - +@SuppressWarnings("PMD.ShortMethodName") +public interface ISequence extends List, IStringValued, ICollectionValue { /** * Get an empty sequence. * - * @param + * @param * the item type * @return the empty sequence */ - @SuppressWarnings({ "unchecked", "null" }) + @SuppressWarnings("null") @NonNull - static ISequence empty() { - return EMPTY; + static ISequence empty() { + return AbstractSequence.empty(); + } + + @Override + default Iterator iterator() { + return getValue().listIterator(); } /** - * Construct a new sequence containing the provided {@code item}. - *

- * If the item is {@code null} and empty sequence will be created. + * Get the items in this sequence as a {@link List}. * - * @param - * the type of items contained in the sequence. - * @param item - * the item to add to the sequence - * @return the new sequence + * @return a list containing all the items of the sequence */ @NonNull - static ISequence of( // NOPMD - intentional - @Nullable ITEM_TYPE item) { - ISequence retval; - if (item == null) { - retval = empty(); - } else { - retval = new SingletonSequenceImpl<>(item); - } - return retval; - } + List getValue(); /** - * Construct a new sequence containing the provided {@code items}. + * Get the items in this sequence as a {@link Stream}. * - * @param - * the type of items contained in the sequence. - * @param items - * the items to add to the sequence - * @return the new sequence + * @return a stream containing all the items of the sequence */ - @SafeVarargs + @Override @NonNull - static ISequence of( // NOPMD - intentional - @NonNull ITEM_TYPE... items) { - return of(ObjectUtils.notNull(Arrays.asList(items))); - } + Stream stream(); /** - * Construct a new sequence containing the provided {@code items}. + * Retrieves the first item in a sequence. If the sequence is empty, a + * {@code null} result is returned. If requireSingleton is {@code true} and the + * sequence contains more than one item, a {@link TypeMetapathException} is + * thrown. * - * @param - * the type of items contained in the sequence. + * @param + * the item type to return derived from the provided sequence * @param items - * the items to add to the sequence - * @return the new sequence + * the sequence to retrieve the first item from + * @param requireSingleton + * if {@code true} then a {@link TypeMetapathException} is thrown if + * the sequence contains more than one item + * @return {@code null} if the sequence is empty, or the item otherwise + * @throws TypeMetapathException + * if the sequence contains more than one item and requireSingleton is + * {@code true} */ - @NonNull - static ISequence of( // NOPMD - intentional - @NonNull List items) { - ISequence retval; - if (items.isEmpty()) { - retval = empty(); - } else if (items.size() == 1) { - retval = new SingletonSequenceImpl<>(ObjectUtils.notNull(items.iterator().next())); - } else { - retval = new ListSequenceImpl<>(items); - } - return retval; + static T getFirstItem(@NonNull ISequence items, boolean requireSingleton) { + return getFirstItem(items.stream(), requireSingleton); } /** - * Construct a new sequence containing the provided {@code items}. + * Retrieves the first item in a sequence. If the sequence is empty, a + * {@code null} result is returned. If requireSingleton is {@code true} and the + * sequence contains more than one item, a {@link TypeMetapathException} is + * thrown. * - * @param - * the type of items contained in the sequence. + * @param + * the item type to return derived from the provided sequence * @param items - * the items to add to the sequence - * @return the new sequence + * the sequence to retrieve the first item from + * @param requireSingleton + * if {@code true} then a {@link TypeMetapathException} is thrown if + * the sequence contains more than one item + * @return {@code null} if the sequence is empty, or the item otherwise + * @throws TypeMetapathException + * if the sequence contains more than one item and requireSingleton is + * {@code true} */ - // TODO: remove null check on callers - @NonNull - static ISequence of( // NOPMD - intentional - Stream items) { - return items == null ? empty() : new StreamSequenceImpl<>(items); + static T getFirstItem(@NonNull Stream items, boolean requireSingleton) { + return items.limit(2) + .reduce((t, u) -> { + if (requireSingleton) { + throw new InvalidTypeMetapathException( + null, + String.format("sequence expected to contain only one item, but found multiple")); + } + return t; + }).orElse(null); } - @Override - default Iterator iterator() { - return asList().listIterator(); + @Nullable + default ITEM getFirstItem(boolean requireSingleton) { + return getFirstItem(this, requireSingleton); } - /** - * Get the items in this sequence as a {@link List}. - * - * @return a list containing all the items of the sequence - */ - @NonNull - List asList(); - - /** - * Get the items in this sequence as a {@link Stream}. - * - * @return a stream containing all the items of the sequence - */ - // TODO: rename to "stream" @NonNull - Stream asStream(); + default ICollectionValue toArrayMember() { + ICollectionValue retval; + switch (size()) { + case 0: + retval = ISequence.empty(); + break; + case 1: + retval = ObjectUtils.notNull(stream().findFirst().get()); + break; + default: + retval = this; + } + return retval; + } /** * Get a stream guaranteed to be backed by a list. @@ -180,46 +175,16 @@ default Iterator iterator() { * @return the stream */ @NonNull - default Stream safeStream() { - return ObjectUtils.notNull(asList().stream()); + default Stream safeStream() { + return ObjectUtils.notNull(getValue().stream()); } - /** - * This optional operation ensures that a list is used to back this sequence. - *

- * If a stream is currently backing this sequence, the stream will be collected - * into a list. This ensures the sequence can be visited multiple times. - * - * @return the resulting sequence - */ - @NonNull - ISequence collect(); - - /** - * Determine if this sequence is empty. - * - * @return {@code true} if the sequence contains no items, or {@code false} - * otherwise - */ - @Override - boolean isEmpty(); - - /** - * Get the count of items in this sequence. - * - * @return the count of items - */ + @SuppressWarnings("null") @Override - int size(); - - /** - * Iterate over each item in the sequence using the provided {@code action}. - * - * @param action - * code to execute for each item - */ - @Override - void forEach(Consumer action); + default Stream flatten() { + // TODO: Is a safe stream needed here? + return safeStream(); + } /** * A {@link Collector} implementation to generates a sequence from a stream of @@ -253,7 +218,7 @@ public BinaryOperator> combiner() { @Override public Function, ISequence> finisher() { - return list -> of(ObjectUtils.notNull(list)); + return list -> ofCollection(ObjectUtils.notNull(list)); } @Override @@ -263,6 +228,11 @@ public Set characteristics() { }; } + @Override + default ISequence asSequence() { + return this; + } + /** * Apply the provided {@code mapFunction} to each item in the sequence. * @@ -284,103 +254,349 @@ static ISequence map( .collect(toSequence()); } - @Override - default boolean contains(Object obj) { - return asList().contains(obj); - } - - @Override - default Object[] toArray() { - return asList().toArray(); - } - - @Override - default T[] toArray(T[] array) { - return asList().toArray(array); - } - - @Override - default boolean add(ITEM_TYPE item) { - throw new UnsupportedOperationException("object is immutable"); - } - - @Override - default void add(int index, ITEM_TYPE element) { - throw new UnsupportedOperationException("object is immutable"); - } - - @Override - default boolean addAll(Collection collection) { - throw new UnsupportedOperationException("object is immutable"); + /** + * Returns an unmodifiable sequence containing the provided {@code item}. + *

+ * If the item is {@code null} and empty sequence will be created. + * + * @param + * the type of items contained in the sequence. + * @param item + * the item to add to the sequence + * @return the new sequence + */ + @NonNull + static ISequence of( // NOPMD - intentional + @Nullable T item) { + return item == null ? empty() : new SingletonSequence<>(item); } - @Override - default boolean addAll(int index, Collection collection) { - throw new UnsupportedOperationException("object is immutable"); + /** + * Returns an unmodifiable sequence containing the provided {@code items}. + * + * @param + * the type of items contained in the sequence. + * @param items + * the items to add to the sequence + * @return the new sequence + */ + @NonNull + static ISequence ofCollection( // NOPMD - intentional + @NonNull List items) { + ISequence retval; + if (items.isEmpty()) { + retval = empty(); + } else if (items.size() == 1) { + retval = new SingletonSequence<>(ObjectUtils.notNull(items.iterator().next())); + } else { + retval = new SequenceN<>(items); + } + return retval; } - @Override - default boolean remove(Object obj) { - throw new UnsupportedOperationException("object is immutable"); + /** + * Returns an unmodifiable sequence containing the provided {@code items}. + * + * @param + * the type of items contained in the sequence. + * @param items + * the items to add to the sequence + * @return the new sequence + */ + // TODO: remove null check on callers + @NonNull + static ISequence of( // NOPMD - intentional + Stream items) { + return items == null ? empty() : new StreamSequence<>(items); } - @Override - default ITEM_TYPE remove(int index) { - throw new UnsupportedOperationException("object is immutable"); + /** + * Returns an unmodifiable sequence containing zero elements. + * + * @param + * the item type + * @return an empty {@code ISequence} + */ + @NonNull + static ISequence of() { + return empty(); } - @Override - default boolean containsAll(Collection collection) { - return asList().containsAll(collection); + /** + * Returns an unmodifiable sequence containing two items. + * + * @param + * the {@code ISequence}'s item type + * @param e1 + * the first item + * @param e2 + * the second item + * @return an {@code ISequence} containing the specified items + * @throws NullPointerException + * if an item is {@code null} + */ + @NonNull + static ISequence of(T e1, T e2) { + return new SequenceN<>(e1, e2); } - @Override - default boolean removeAll(Collection collection) { - throw new UnsupportedOperationException("object is immutable"); + /** + * Returns an unmodifiable sequence containing three elements. + * + * @param + * the {@code ISequence}'s item type + * @param e1 + * the first item + * @param e2 + * the second item + * @param e3 + * the third item + * @return an {@code ISequence} containing the specified items + * @throws NullPointerException + * if an item is {@code null} + */ + @NonNull + static ISequence of(T e1, T e2, T e3) { + return new SequenceN<>(e1, e2, e3); } - @Override - default boolean retainAll(Collection collection) { - throw new UnsupportedOperationException("object is immutable"); + /** + * Returns an unmodifiable sequence containing four items. + * + * @param + * the {@code ISequence}'s item type + * @param e1 + * the first item + * @param e2 + * the second item + * @param e3 + * the third item + * @param e4 + * the fourth item + * @return an {@code ISequence} containing the specified items + * @throws NullPointerException + * if an item is {@code null} + */ + @NonNull + static ISequence of(T e1, T e2, T e3, T e4) { + return new SequenceN<>(e1, e2, e3, e4); } - @Override - default void clear() { - throw new UnsupportedOperationException("object is immutable"); + /** + * Returns an unmodifiable sequence containing five items. + * + * @param + * the {@code ISequence}'s item type + * @param e1 + * the first item + * @param e2 + * the second item + * @param e3 + * the third item + * @param e4 + * the fourth item + * @param e5 + * the fifth item + * @return an {@code ISequence} containing the specified items + * @throws NullPointerException + * if an item is {@code null} + */ + @NonNull + static ISequence of(T e1, T e2, T e3, T e4, T e5) { + return new SequenceN<>(e1, e2, e3, e4, e5); } - @Override - default ITEM_TYPE get(int index) { - return asList().get(index); + /** + * Returns an unmodifiable sequence containing six items. + * + * @param + * the {@code ISequence}'s item type + * @param e1 + * the first item + * @param e2 + * the second item + * @param e3 + * the third item + * @param e4 + * the fourth item + * @param e5 + * the fifth item + * @param e6 + * the sixth item + * @return an {@code ISequence} containing the specified items + * @throws NullPointerException + * if an item is {@code null} + */ + @NonNull + static ISequence of(T e1, T e2, T e3, T e4, T e5, T e6) { + return new SequenceN<>(e1, e2, e3, e4, e5, e6); } - @Override - default ITEM_TYPE set(int index, ITEM_TYPE element) { - throw new UnsupportedOperationException("object is immutable"); + /** + * Returns an unmodifiable sequence containing seven items. + * + * @param + * the {@code ISequence}'s item type + * @param e1 + * the first item + * @param e2 + * the second item + * @param e3 + * the third item + * @param e4 + * the fourth item + * @param e5 + * the fifth item + * @param e6 + * the sixth item + * @param e7 + * the seventh item + * @return an {@code ISequence} containing the specified items + * @throws NullPointerException + * if an item is {@code null} + */ + @NonNull + static ISequence of(T e1, T e2, T e3, T e4, T e5, T e6, T e7) { + return new SequenceN<>(e1, e2, e3, e4, e5, e6, e7); } - @Override - default int indexOf(Object obj) { - return asList().indexOf(obj); + /** + * Returns an unmodifiable sequence containing eight items. + * + * @param + * the {@code ISequence}'s item type + * @param e1 + * the first item + * @param e2 + * the second item + * @param e3 + * the third item + * @param e4 + * the fourth item + * @param e5 + * the fifth item + * @param e6 + * the sixth item + * @param e7 + * the seventh item + * @param e8 + * the eighth item + * @return an {@code ISequence} containing the specified items + * @throws NullPointerException + * if an item is {@code null} + */ + @NonNull + static ISequence of(T e1, T e2, T e3, T e4, T e5, T e6, T e7, T e8) { + return new SequenceN<>(e1, e2, e3, e4, e5, e6, e7, e8); } - @Override - default int lastIndexOf(Object obj) { - return asList().lastIndexOf(obj); + /** + * Returns an unmodifiable sequence containing nine items. + * + * @param + * the {@code ISequence}'s item type + * @param e1 + * the first item + * @param e2 + * the second item + * @param e3 + * the third item + * @param e4 + * the fourth item + * @param e5 + * the fifth item + * @param e6 + * the sixth item + * @param e7 + * the seventh item + * @param e8 + * the eighth item + * @param e9 + * the ninth item + * @return an {@code ISequence} containing the specified items + * @throws NullPointerException + * if an item is {@code null} + */ + @NonNull + static ISequence of(T e1, T e2, T e3, T e4, T e5, T e6, T e7, T e8, T e9) { + return new SequenceN<>(e1, e2, e3, e4, e5, e6, e7, e8, e9); } - @Override - default ListIterator listIterator() { - return asList().listIterator(); + /** + * Returns an unmodifiable sequence containing ten items. + * + * @param + * the {@code ISequence}'s item type + * @param e1 + * the first item + * @param e2 + * the second item + * @param e3 + * the third item + * @param e4 + * the fourth item + * @param e5 + * the fifth item + * @param e6 + * the sixth item + * @param e7 + * the seventh item + * @param e8 + * the eighth item + * @param e9 + * the ninth item + * @param e10 + * the tenth item + * @return an {@code IArrayItem} containing the specified items + * @throws NullPointerException + * if an item is {@code null} + */ + @NonNull + static ISequence of(T e1, T e2, T e3, T e4, T e5, T e6, T e7, T e8, T e9, T e10) { + return new SequenceN<>(e1, e2, e3, e4, e5, e6, e7, e8, e9, e10); } - @Override - default ListIterator listIterator(int index) { - return asList().listIterator(index); + /** + * Returns an unmodifiable sequence containing an arbitrary number of items. + * + * @param + * the {@code ISequence}'s item type + * @param items + * the items to be contained in the list + * @return an {@code ISequence} containing the specified items + * @throws NullPointerException + * if an item is {@code null} or if the array is {@code null} + */ + @SafeVarargs + @NonNull + static ISequence of(@NonNull T... items) { + return items.length == 0 ? empty() : new SequenceN<>(items); } - @Override - default List subList(int fromIndex, int toIndex) { - return asList().subList(fromIndex, toIndex); + /** + * Returns an unmodifiable sequence containing the items of the given + * Collection, in its iteration order. The given Collection must not be null, + * and it must not contain any null items. If the given Collection is + * subsequently modified, the returned array item will not reflect such + * modifications. + * + * @param + * the {@code ISequence}'s item type + * @param collection + * a {@code Collection} from which items are drawn, must be non-null + * @return an {@code ISequence} containing the items of the given + * {@code Collection} + * @throws NullPointerException + * if collection is null, or if it contains any nulls + * @since 10 + */ + @SuppressWarnings("unchecked") + @NonNull + static ISequence copyOf(Collection collection) { + return collection instanceof IArrayItem + ? (ISequence) collection + : collection.isEmpty() + ? empty() + : new SequenceN<>(new ArrayList<>(collection)); } } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/IStringValued.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/IStringValued.java new file mode 100644 index 000000000..ec9b682db --- /dev/null +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/IStringValued.java @@ -0,0 +1,39 @@ +/* + * Portions of this software was developed by employees of the National Institute + * of Standards and Technology (NIST), an agency of the Federal Government and is + * being made available as a public service. Pursuant to title 17 United States + * Code Section 105, works of NIST employees are not subject to copyright + * protection in the United States. This software may be subject to foreign + * copyright. Permission in the United States and in foreign countries, to the + * extent that NIST may hold copyright, to use, copy, modify, create derivative + * works, and distribute this software and its documentation without fee is hereby + * granted on a non-exclusive basis, provided that this notice and disclaimer + * of warranty appears in all copies. + * + * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER + * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY + * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM + * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE + * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT + * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, + * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, + * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, + * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR + * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT + * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. + */ + +package gov.nist.secauto.metaschema.core.metapath; + +import edu.umd.cs.findbugs.annotations.NonNull; + +public interface IStringValued { + /** + * Get the string value. + * + * @return the string value + */ + @NonNull + String asString(); +} diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/MetapathConstants.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/MetapathConstants.java index 86b73dd12..115c0f63a 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/MetapathConstants.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/MetapathConstants.java @@ -52,6 +52,9 @@ public final class MetapathConstants { public static final URI NS_METAPATH_FUNCTIONS_MATH = ObjectUtils.requireNonNull( URI.create("http://csrc.nist.gov/ns/metaschema/metapath-functions/math")); @NonNull + public static final URI NS_METAPATH_FUNCTIONS_ARRAY = ObjectUtils.requireNonNull( + URI.create("http://csrc.nist.gov/ns/metaschema/metapath-functions/array")); + @NonNull public static final URI NS_METAPATH_FUNCTIONS_EXTENDED = NS_METAPATH; @NonNull @@ -62,9 +65,10 @@ public final class MetapathConstants { public static final String PREFIX_XPATH_FUNCTIONS = "mp"; @NonNull public static final String PREFIX_XPATH_FUNCTIONS_MATH = "math"; + @NonNull + public static final String PREFIX_XPATH_FUNCTIONS_ARRAY = "array"; private MetapathConstants() { // disable construction } - } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/MetapathExpression.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/MetapathExpression.java index 2d09b2fbe..1e8b7ca17 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/MetapathExpression.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/MetapathExpression.java @@ -346,7 +346,7 @@ protected T toResultType(@NonNull ISequence sequence, @NonNull ResultType result = FnBoolean.fnBoolean(sequence).toBoolean(); break; case NODE: - result = FunctionUtils.getFirstItem(sequence, true); + result = sequence.getFirstItem(true); break; case NUMBER: INumericItem numeric = FunctionUtils.toNumeric(sequence, true); @@ -356,8 +356,8 @@ protected T toResultType(@NonNull ISequence sequence, @NonNull ResultType result = sequence; break; case STRING: - IItem item = FunctionUtils.getFirstItem(sequence, true); - result = item == null ? "" : FnData.fnDataItem(item).asString(); + IAnyAtomicItem item = FnData.fnData(sequence).getFirstItem(true); + result = item == null ? "" : item.asString(); break; default: throw new InvalidTypeMetapathException(null, String.format("unsupported result type '%s'", resultType.name())); diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/StaticContext.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/StaticContext.java index 18f07fb07..1859059a7 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/StaticContext.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/StaticContext.java @@ -62,6 +62,9 @@ public final class StaticContext { knownNamespaces.put( MetapathConstants.PREFIX_XPATH_FUNCTIONS_MATH, MetapathConstants.NS_METAPATH_FUNCTIONS_MATH); + knownNamespaces.put( + MetapathConstants.PREFIX_XPATH_FUNCTIONS_ARRAY, + MetapathConstants.NS_METAPATH_FUNCTIONS_ARRAY); WELL_KNOWN_NAMESPACES = CollectionUtil.unmodifiableMap(knownNamespaces); } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/StaticMetapathException.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/StaticMetapathException.java index eea69c7a7..0331a3cff 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/StaticMetapathException.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/StaticMetapathException.java @@ -129,7 +129,7 @@ public StaticMetapathException(int code, Throwable cause) { } @Override - protected String getCodePrefix() { + public String getCodePrefix() { return "MPST"; } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/TypeMetapathException.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/TypeMetapathException.java index 89de22791..9066b5976 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/TypeMetapathException.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/TypeMetapathException.java @@ -106,7 +106,7 @@ public TypeMetapathException(int code, Throwable cause) { } @Override - protected String getCodePrefix() { + public String getCodePrefix() { return "MPTY"; } } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/antlr/AbstractAstVisitor.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/antlr/AbstractAstVisitor.java index b1e15f4e1..b00d13711 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/antlr/AbstractAstVisitor.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/antlr/AbstractAstVisitor.java @@ -32,11 +32,14 @@ import gov.nist.secauto.metaschema.core.metapath.antlr.Metapath10.AndexprContext; import gov.nist.secauto.metaschema.core.metapath.antlr.Metapath10.ArgumentContext; import gov.nist.secauto.metaschema.core.metapath.antlr.Metapath10.ArgumentlistContext; +import gov.nist.secauto.metaschema.core.metapath.antlr.Metapath10.ArrayconstructorContext; import gov.nist.secauto.metaschema.core.metapath.antlr.Metapath10.ArrowexprContext; import gov.nist.secauto.metaschema.core.metapath.antlr.Metapath10.ArrowfunctionspecifierContext; import gov.nist.secauto.metaschema.core.metapath.antlr.Metapath10.AxisstepContext; import gov.nist.secauto.metaschema.core.metapath.antlr.Metapath10.ComparisonexprContext; import gov.nist.secauto.metaschema.core.metapath.antlr.Metapath10.ContextitemexprContext; +import gov.nist.secauto.metaschema.core.metapath.antlr.Metapath10.CurlyarrayconstructorContext; +import gov.nist.secauto.metaschema.core.metapath.antlr.Metapath10.EnclosedexprContext; import gov.nist.secauto.metaschema.core.metapath.antlr.Metapath10.EqnameContext; import gov.nist.secauto.metaschema.core.metapath.antlr.Metapath10.ExprContext; import gov.nist.secauto.metaschema.core.metapath.antlr.Metapath10.ExprsingleContext; @@ -47,8 +50,10 @@ import gov.nist.secauto.metaschema.core.metapath.antlr.Metapath10.GeneralcompContext; import gov.nist.secauto.metaschema.core.metapath.antlr.Metapath10.IfexprContext; import gov.nist.secauto.metaschema.core.metapath.antlr.Metapath10.IntersectexceptexprContext; +import gov.nist.secauto.metaschema.core.metapath.antlr.Metapath10.KeyspecifierContext; import gov.nist.secauto.metaschema.core.metapath.antlr.Metapath10.LetexprContext; import gov.nist.secauto.metaschema.core.metapath.antlr.Metapath10.LiteralContext; +import gov.nist.secauto.metaschema.core.metapath.antlr.Metapath10.LookupContext; import gov.nist.secauto.metaschema.core.metapath.antlr.Metapath10.MetapathContext; import gov.nist.secauto.metaschema.core.metapath.antlr.Metapath10.MultiplicativeexprContext; import gov.nist.secauto.metaschema.core.metapath.antlr.Metapath10.NametestContext; @@ -71,9 +76,11 @@ import gov.nist.secauto.metaschema.core.metapath.antlr.Metapath10.SimpleletbindingContext; import gov.nist.secauto.metaschema.core.metapath.antlr.Metapath10.SimpleletclauseContext; import gov.nist.secauto.metaschema.core.metapath.antlr.Metapath10.SimplemapexprContext; +import gov.nist.secauto.metaschema.core.metapath.antlr.Metapath10.SquarearrayconstructorContext; import gov.nist.secauto.metaschema.core.metapath.antlr.Metapath10.StepexprContext; import gov.nist.secauto.metaschema.core.metapath.antlr.Metapath10.StringconcatexprContext; import gov.nist.secauto.metaschema.core.metapath.antlr.Metapath10.UnaryexprContext; +import gov.nist.secauto.metaschema.core.metapath.antlr.Metapath10.UnarylookupContext; import gov.nist.secauto.metaschema.core.metapath.antlr.Metapath10.UnionexprContext; import gov.nist.secauto.metaschema.core.metapath.antlr.Metapath10.ValuecompContext; import gov.nist.secauto.metaschema.core.metapath.antlr.Metapath10.ValueexprContext; @@ -138,8 +145,9 @@ protected R delegateToChild(@NonNull T ctx) { throw new IllegalStateException("a single child expression was expected"); } - /* ============================================================ - * Expressions - https://www.w3.org/TR/xpath-31/#id-expressions + /* + * ============================================================ Expressions - + * https://www.w3.org/TR/xpath-31/#id-expressions * ============================================================ */ @@ -169,7 +177,8 @@ public R visitExprsingle(ExprsingleContext ctx) { assert ctx != null; return delegateToChild(ctx); } - /* ============================================================================ + /* + * ============================================================================ * Primary Expressions - https://www.w3.org/TR/xpath-31/#id-primary-expressions * ============================================================================ */ @@ -180,8 +189,9 @@ public R visitPrimaryexpr(PrimaryexprContext ctx) { return delegateToChild(ctx); } - /* ================================================================= - * Literal Expressions - https://www.w3.org/TR/xpath-31/#id-literals + /* + * ================================================================= Literal + * Expressions - https://www.w3.org/TR/xpath-31/#id-literals * ================================================================= */ @@ -215,8 +225,9 @@ public R visitNumericliteral(NumericliteralContext ctx) { return handle(ctx, (context) -> handleNumericLiteral(ctx)); } - /* ================================================================== - * Variable References - https://www.w3.org/TR/xpath-31/#id-variables + /* + * ================================================================== Variable + * References - https://www.w3.org/TR/xpath-31/#id-variables * ================================================================== */ @@ -241,9 +252,12 @@ public R visitVarname(VarnameContext ctx) { return delegateToChild(ctx); } - /* ================================================================================= - * Parenthesized Expressions - https://www.w3.org/TR/xpath-31/#id-paren-expressions - * ================================================================================= + /* + * ============================================================================= + * ==== Parenthesized Expressions - + * https://www.w3.org/TR/xpath-31/#id-paren-expressions + * ============================================================================= + * ==== */ /** @@ -262,9 +276,12 @@ public R visitParenthesizedexpr(ParenthesizedexprContext ctx) { return expr == null ? handleEmptyParenthesizedexpr(ctx) : visit(expr); } - /* ===================================================================================== - * Context Item Expression - https://www.w3.org/TR/xpath-31/#id-context-item-expression - * ===================================================================================== + /* + * ============================================================================= + * ======== Context Item Expression - + * https://www.w3.org/TR/xpath-31/#id-context-item-expression + * ============================================================================= + * ======== */ /** @@ -282,10 +299,9 @@ public R visitContextitemexpr(ContextitemexprContext ctx) { return handle(ctx, (context) -> handleContextitemexpr(ctx)); } - /* ========================================================================= - * Static Function Calls - https://www.w3.org/TR/xpath-31/#id-function-calls - * ========================================================================= - */ + // ========================================================================= + // Static Function Calls - https://www.w3.org/TR/xpath-31/#id-function-calls + // ========================================================================= /** * Handle the provided expression. @@ -314,7 +330,18 @@ public R visitArgument(ArgumentContext ctx) { throw new IllegalStateException(); } - /* ========================================================================= + // ======================================================================= + // Enclosed Expressions - https://www.w3.org/TR/xpath-31/#id-enclosed-expr + // ======================================================================= + + @Override + public R visitEnclosedexpr(EnclosedexprContext ctx) { + ExprContext expr = ctx.expr(); + return expr == null ? null : expr.accept(this); + } + + /* + * ========================================================================= * Filter Expressions - https://www.w3.org/TR/xpath-31/#id-filter-expression * ========================================================================= */ @@ -334,14 +361,39 @@ public R visitPostfixexpr(PostfixexprContext ctx) { return handle(ctx, (context) -> handlePostfixexpr(ctx)); } + /** + * Handle the provided expression. + * + * @param ctx + * the provided expression context + * @return the result + */ + protected abstract R handlePredicate(@NonNull PredicateContext ctx); + @Override public R visitPredicate(PredicateContext ctx) { - // should never be called, since this is handled by the parent expression - throw new IllegalStateException(); + assert ctx != null; + return handlePredicate(ctx); + } + + /** + * Handle the provided expression. + * + * @param ctx + * the provided expression context + * @return the result + */ + protected abstract R handleLookup(@NonNull LookupContext ctx); + + @Override + public R visitLookup(LookupContext ctx) { + assert ctx != null; + return handleLookup(ctx); } - /* ====================================================================== - * Path Expressions - https://www.w3.org/TR/xpath-31/#id-path-expressions + /* + * ====================================================================== Path + * Expressions - https://www.w3.org/TR/xpath-31/#id-path-expressions * ====================================================================== */ @@ -360,9 +412,12 @@ public R visitPathexpr(PathexprContext ctx) { return handle(ctx, (context) -> handlePathexpr(ctx)); } - /* ======================================================================================= - * RelativePath Expressions - https://www.w3.org/TR/xpath-31/#id-relative-path-expressions - * ======================================================================================= + /* + * ============================================================================= + * ========== RelativePath Expressions - + * https://www.w3.org/TR/xpath-31/#id-relative-path-expressions + * ============================================================================= + * ========== */ /** @@ -380,8 +435,9 @@ public R visitRelativepathexpr(RelativepathexprContext ctx) { return handle(ctx, (context) -> handleRelativepathexpr(ctx)); } - /* ================================================ - * Steps - https://www.w3.org/TR/xpath-31/#id-steps + /* + * ================================================ Steps - + * https://www.w3.org/TR/xpath-31/#id-steps * ================================================ */ @@ -423,7 +479,8 @@ public R visitReversestep(ReversestepContext ctx) { return handle(ctx, (context) -> handleReversestep(ctx)); } - /* ====================================================================== + /* + * ====================================================================== * Predicates within Steps - https://www.w3.org/TR/xpath-31/#id-predicate * ====================================================================== */ @@ -449,8 +506,9 @@ public R visitPredicatelist(PredicatelistContext ctx) { throw new IllegalStateException(); } - /* =========================================== - * Axes - https://www.w3.org/TR/xpath-31/#axes + /* + * =========================================== Axes - + * https://www.w3.org/TR/xpath-31/#axes * =========================================== */ @@ -466,8 +524,9 @@ public R visitReverseaxis(ReverseaxisContext ctx) { throw new IllegalStateException(); } - /* ======================================================= - * Node Tests - https://www.w3.org/TR/xpath-31/#node-tests + /* + * ======================================================= Node Tests - + * https://www.w3.org/TR/xpath-31/#node-tests * ======================================================= */ @@ -504,8 +563,9 @@ public R visitWildcard(WildcardContext ctx) { return handleWildcard(ctx); } - /* =========================================================== - * Abbreviated Syntax - https://www.w3.org/TR/xpath-31/#abbrev + /* + * =========================================================== Abbreviated + * Syntax - https://www.w3.org/TR/xpath-31/#abbrev * =========================================================== */ @@ -539,7 +599,8 @@ public R visitAbbrevreversestep(AbbrevreversestepContext ctx) { return handleAbbrevreversestep(ctx); } - /* ====================================================================== + /* + * ====================================================================== * Constructing Sequences - https://www.w3.org/TR/xpath-31/#construct_seq * ====================================================================== */ @@ -559,7 +620,8 @@ public R visitRangeexpr(RangeexprContext ctx) { return handle(ctx, (context) -> handleRangeexpr(ctx)); } - /* ======================================================================== + /* + * ======================================================================== * Combining Node Sequences - https://www.w3.org/TR/xpath-31/#combining_seq * ======================================================================== */ @@ -594,7 +656,8 @@ public R visitIntersectexceptexpr(IntersectexceptexprContext ctx) { return handle(ctx, (context) -> handleIntersectexceptexpr(ctx)); } - /* ====================================================================== + /* + * ====================================================================== * Arithmetic Expressions - https://www.w3.org/TR/xpath-31/#id-arithmetic * ====================================================================== */ @@ -650,9 +713,12 @@ public R visitValueexpr(ValueexprContext ctx) { return delegateToChild(ctx); } - /* ======================================================================================== - * String Concatenation Expressions - https://www.w3.org/TR/xpath-31/#id-string-concat-expr - * ======================================================================================== + /* + * ============================================================================= + * =========== String Concatenation Expressions - + * https://www.w3.org/TR/xpath-31/#id-string-concat-expr + * ============================================================================= + * =========== */ /** @@ -670,7 +736,8 @@ public R visitStringconcatexpr(StringconcatexprContext ctx) { return handle(ctx, (context) -> handleStringconcatexpr(ctx)); } - /* ======================================================================= + /* + * ======================================================================= * Comparison Expressions - https://www.w3.org/TR/xpath-31/#id-comparisons * ======================================================================= */ @@ -702,7 +769,8 @@ public R visitGeneralcomp(GeneralcompContext ctx) { throw new IllegalStateException(); } - /* ============================================================================ + /* + * ============================================================================ * Logical Expressions - https://www.w3.org/TR/xpath-31/#id-logical-expressions * ============================================================================ */ @@ -737,10 +805,9 @@ public R visitAndexpr(AndexprContext ctx) { return handle(ctx, (context) -> handleAndexpr(ctx)); } - /* ==================================================================== - * For Expressions - https://www.w3.org/TR/xpath-31/#id-for-expressions - * ==================================================================== - */ + // ==================================================================== + // For Expressions - https://www.w3.org/TR/xpath-31/#id-for-expressions + // ==================================================================== /** * Handle the provided expression. @@ -769,10 +836,9 @@ public R visitSimpleforbinding(SimpleforbindingContext ctx) { throw new IllegalStateException(); } - /* ==================================================================== - * Let Expressions - https://www.w3.org/TR/xpath-31/#id-let-expressions - * ==================================================================== - */ + // ==================================================================== + // Let Expressions - https://www.w3.org/TR/xpath-31/#id-let-expressions + // ==================================================================== /** * Handle the provided expression. @@ -801,10 +867,70 @@ public R visitSimpleletbinding(SimpleletbindingContext ctx) { throw new IllegalStateException(); } - /* ========================================================================= - * Conditional Expressions - https://www.w3.org/TR/xpath-31/#id-conditionals - * ========================================================================= + // ============================================================== + // Array Constructors - https://www.w3.org/TR/xpath-31/#id-arrays + // ============================================================== + + @Override + public R visitArrayconstructor(ArrayconstructorContext ctx) { + assert ctx != null; + return delegateToChild(ctx); + } + + /** + * Handle the provided expression. + * + * @param ctx + * the provided expression context + * @return the result + */ + protected abstract R handleArrayConstructor(@NonNull SquarearrayconstructorContext ctx); + + @Override + public R visitSquarearrayconstructor(SquarearrayconstructorContext ctx) { + assert ctx != null; + return handleArrayConstructor(ctx); + } + + /** + * Handle the provided expression. + * + * @param ctx + * the provided expression context + * @return the result + */ + protected abstract R handleArrayConstructor(@NonNull CurlyarrayconstructorContext ctx); + + @Override + public R visitCurlyarrayconstructor(CurlyarrayconstructorContext ctx) { + assert ctx != null; + return handleArrayConstructor(ctx); + } + + @Override + public R visitKeyspecifier(KeyspecifierContext ctx) { + // should never be called, since this is handled by the parent expression + throw new IllegalStateException(); + } + + /** + * Handle the provided expression. + * + * @param ctx + * the provided expression context + * @return the result */ + protected abstract R handleUnarylookup(@NonNull UnarylookupContext ctx); + + @Override + public R visitUnarylookup(UnarylookupContext ctx) { + assert ctx != null; + return handleUnarylookup(ctx); + } + + // ========================================================================= + // Conditional Expressions - https://www.w3.org/TR/xpath-31/#id-conditionals + // ========================================================================= /** * Handle the provided expression. @@ -821,9 +947,12 @@ public R visitIfexpr(IfexprContext ctx) { return handle(ctx, (context) -> handleIfexpr(ctx)); } - /* ================================================================================== - * Quantified Expressions - https://www.w3.org/TR/xpath-31/#id-quantified-expressions - * ================================================================================== + /* + * ============================================================================= + * ===== Quantified Expressions - + * https://www.w3.org/TR/xpath-31/#id-quantified-expressions + * ============================================================================= + * ===== */ /** @@ -841,7 +970,8 @@ public R visitQuantifiedexpr(QuantifiedexprContext ctx) { return handleQuantifiedexpr(ctx); } - /* ========================================================================= + /* + * ========================================================================= * Simple map operator (!) - https://www.w3.org/TR/xpath-31/#id-map-operator * ========================================================================= */ @@ -861,8 +991,9 @@ public R visitSimplemapexpr(SimplemapexprContext ctx) { return handle(ctx, (context) -> handleSimplemapexpr(ctx)); } - /* ======================================================================= - * Arrow operator (=>) - https://www.w3.org/TR/xpath-31/#id-arrow-operator + /* + * ======================================================================= Arrow + * operator (=>) - https://www.w3.org/TR/xpath-31/#id-arrow-operator * ======================================================================= */ diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/AbstractCSTVisitorBase.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/AbstractCSTVisitorBase.java index 637d24caf..cf9dd621d 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/AbstractCSTVisitorBase.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/AbstractCSTVisitorBase.java @@ -47,6 +47,7 @@ import javax.xml.namespace.QName; import edu.umd.cs.findbugs.annotations.NonNull; +import edu.umd.cs.findbugs.annotations.Nullable; public abstract class AbstractCSTVisitorBase extends AbstractAstVisitor { @@ -130,6 +131,28 @@ public IExpression visit(ParseTree tree) { return super.visit(tree); } + @Nullable + protected IExpression + nAiryToCollection( + @NonNull CONTEXT context, + int startIndex, + int step, + @NonNull BiFunction parser, + @NonNull Function, IExpression> supplier) { + int numChildren = context.getChildCount(); + + IExpression retval = null; + if (startIndex < numChildren) { + List children = new ArrayList<>((numChildren - startIndex) / step); + for (int idx = startIndex; idx < numChildren; idx += step) { + EXPRESSION result = parser.apply(context, idx); + children.add(result); + } + retval = supplier.apply(children); + } + return retval; + } + /** * Parse the provided context as an n-ary phrase, which will be one of the * following. @@ -272,12 +295,11 @@ protected IExpression handleGroupedNAiry( IExpression retval = null; if (numChildren > 0) { ParseTree leftTree = context.getChild(startingIndex); - IExpression result = ObjectUtils.notNull(leftTree.accept(this)); + retval = ObjectUtils.notNull(leftTree.accept(this)); for (int i = startingIndex + 1; i < numChildren; i = i + step) { - result = parser.apply(context, i, result); + retval = parser.apply(context, i, retval); } - retval = result; } return retval; } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/AbstractExpression.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/AbstractExpression.java index 967b413c2..b60654f2f 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/AbstractExpression.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/AbstractExpression.java @@ -28,9 +28,7 @@ import gov.nist.secauto.metaschema.core.metapath.ISequence; import gov.nist.secauto.metaschema.core.metapath.TypeMetapathException; -import gov.nist.secauto.metaschema.core.metapath.function.FunctionUtils; import gov.nist.secauto.metaschema.core.metapath.function.library.FnData; -import gov.nist.secauto.metaschema.core.metapath.item.IItem; import gov.nist.secauto.metaschema.core.metapath.item.atomic.IAnyAtomicItem; import edu.umd.cs.findbugs.annotations.NonNull; @@ -54,9 +52,7 @@ public abstract class AbstractExpression implements IExpression { @Nullable public static IAnyAtomicItem getFirstDataItem(@NonNull ISequence sequence, boolean requireSingleton) { - IItem item = FunctionUtils.getFirstItem(sequence, requireSingleton); - - return item == null ? null : FnData.fnDataItem(item); + return FnData.fnData(sequence).getFirstItem(requireSingleton); } @Override diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/AbstractExpressionVisitor.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/AbstractExpressionVisitor.java index 92c0b5e2a..9708e7542 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/AbstractExpressionVisitor.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/AbstractExpressionVisitor.java @@ -352,4 +352,29 @@ public RESULT visitFor(For expr, CONTEXT context) { public RESULT visitSimpleMap(SimpleMap expr, CONTEXT context) { return visitChildren(expr, context); } + + @Override + public RESULT visitArray(ArraySequence expr, CONTEXT context) { + return visitChildren(expr, context); + } + + @Override + public RESULT visitArray(ArraySquare expr, CONTEXT context) { + return visitChildren(expr, context); + } + + @Override + public RESULT visitPostfixLookup(PostfixLookup expr, CONTEXT context) { + return visitChildren(expr, context); + } + + @Override + public RESULT visitFunctionCallAccessor(FunctionCallAccessor expr, CONTEXT context) { + return visitChildren(expr, context); + } + + @Override + public RESULT visitUnaryLookup(UnaryLookup expr, CONTEXT context) { + return defaultResult(); + } } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/AbstractFilterExpression.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/AbstractFilterExpression.java index 0408ab3ed..5b20b87ff 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/AbstractFilterExpression.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/AbstractFilterExpression.java @@ -71,7 +71,7 @@ public ISequence accept( ISequence left = getLeft().accept(dynamicContext, focus); ISequence right = getRight().accept(dynamicContext, focus); - List rightList = right.asList(); + List rightList = right.getValue(); return applyFilterTo(left, rightList); } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/AbstractLookup.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/AbstractLookup.java new file mode 100644 index 000000000..e61f89fed --- /dev/null +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/AbstractLookup.java @@ -0,0 +1,180 @@ +/* + * Portions of this software was developed by employees of the National Institute + * of Standards and Technology (NIST), an agency of the Federal Government and is + * being made available as a public service. Pursuant to title 17 United States + * Code Section 105, works of NIST employees are not subject to copyright + * protection in the United States. This software may be subject to foreign + * copyright. Permission in the United States and in foreign countries, to the + * extent that NIST may hold copyright, to use, copy, modify, create derivative + * works, and distribute this software and its documentation without fee is hereby + * granted on a non-exclusive basis, provided that this notice and disclaimer + * of warranty appears in all copies. + * + * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER + * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY + * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM + * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE + * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT + * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, + * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, + * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, + * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR + * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT + * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. + */ + +package gov.nist.secauto.metaschema.core.metapath.cst; + +import gov.nist.secauto.metaschema.core.metapath.DynamicContext; +import gov.nist.secauto.metaschema.core.metapath.ICollectionValue; +import gov.nist.secauto.metaschema.core.metapath.ISequence; +import gov.nist.secauto.metaschema.core.metapath.InvalidTypeMetapathException; +import gov.nist.secauto.metaschema.core.metapath.function.library.FnData; +import gov.nist.secauto.metaschema.core.metapath.item.IItem; +import gov.nist.secauto.metaschema.core.metapath.item.atomic.IAnyAtomicItem; +import gov.nist.secauto.metaschema.core.metapath.item.atomic.IIntegerItem; +import gov.nist.secauto.metaschema.core.metapath.item.function.ArrayException; +import gov.nist.secauto.metaschema.core.metapath.item.function.IArrayItem; + +import java.util.stream.Stream; + +import edu.umd.cs.findbugs.annotations.NonNull; + +public abstract class AbstractLookup implements IExpression { + @NonNull + private final IKeySpecifier keySpecifier; + + protected AbstractLookup(@NonNull IKeySpecifier keySpecifier) { + this.keySpecifier = keySpecifier; + } + + @NonNull + public IKeySpecifier getKeySpecifier() { + return keySpecifier; + } + + protected interface IKeySpecifier { + + default Stream lookup( + @NonNull IItem item, + @NonNull DynamicContext dynamicContext, + @NonNull ISequence focus) { + Stream result; + if (item instanceof IArrayItem) { + result = lookupInArray((IArrayItem) item, dynamicContext, focus); + } else { + throw new InvalidTypeMetapathException(item, + String.format("Item type '%s' is not an array or map.", item.getClass().getName())); + } + return result; + } + + Stream lookupInArray( + @NonNull IArrayItem item, + @NonNull DynamicContext dynamicContext, + @NonNull ISequence focus); + } + + protected static class NCNameKeySpecifier implements IKeySpecifier { + @NonNull + private final String name; + + public NCNameKeySpecifier(String name) { + this.name = name; + } + + public String getName() { + return name; + } + + @Override + public Stream lookupInArray( + IArrayItem item, + DynamicContext dynamicContext, + ISequence focus) { + throw new InvalidTypeMetapathException(item, + String.format("The key name-based lookup '%s' is not appropriate for an array.", getName())); + } + } + + protected static class IntegerLiteralKeySpecifier implements IKeySpecifier { + private final int index; + + public IntegerLiteralKeySpecifier(IIntegerItem literal) { + index = literal.asInteger().intValueExact() - 1; + } + + @Override + public Stream lookupInArray( + IArrayItem item, + DynamicContext dynamicContext, + ISequence focus) { + try { + return Stream.of(item.get(index)); + } catch (IndexOutOfBoundsException ex) { + throw new ArrayException( + ArrayException.INDEX_OUT_OF_BOUNDS, + String.format("The index %d is outside the range of values for the array size '%d'.", + index + 1, + item.size()), + ex); + } + } + } + + protected static class WildcardKeySpecifier implements IKeySpecifier { + + @Override + public Stream lookupInArray( + IArrayItem item, + DynamicContext dynamicContext, + ISequence focus) { + return item.stream(); + } + } + + public static class ParenthesizedExprKeySpecifier implements IKeySpecifier { + @NonNull + private final IExpression keyExpression; + + public ParenthesizedExprKeySpecifier(@NonNull IExpression keyExpression) { + this.keyExpression = keyExpression; + } + + public IExpression getKeyExpression() { + return keyExpression; + } + + @Override + public Stream lookupInArray( + IArrayItem item, + DynamicContext dynamicContext, + ISequence focus) { + ISequence keys = FnData.fnData(getKeyExpression().accept(dynamicContext, focus)); + + return keys.stream() + .map(key -> { + if (key instanceof IIntegerItem) { + int index = ((IIntegerItem) key).asInteger().intValueExact() - 1; + try { + return item.get(index); + } catch (IndexOutOfBoundsException ex) { + throw new ArrayException( + ArrayException.INDEX_OUT_OF_BOUNDS, + String.format("The index %d is outside the range of values for the array size '%d'.", + index + 1, + item.size()), + ex); + } + } + throw new InvalidTypeMetapathException(item, + String.format("The key '%s' of type '%s' is not appropriate for an array lookup.", + key.asString(), + key.getClass().getName())); + + }); + } + } + +} diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/ArraySequence.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/ArraySequence.java new file mode 100644 index 000000000..cdf18b221 --- /dev/null +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/ArraySequence.java @@ -0,0 +1,79 @@ +/* + * Portions of this software was developed by employees of the National Institute + * of Standards and Technology (NIST), an agency of the Federal Government and is + * being made available as a public service. Pursuant to title 17 United States + * Code Section 105, works of NIST employees are not subject to copyright + * protection in the United States. This software may be subject to foreign + * copyright. Permission in the United States and in foreign countries, to the + * extent that NIST may hold copyright, to use, copy, modify, create derivative + * works, and distribute this software and its documentation without fee is hereby + * granted on a non-exclusive basis, provided that this notice and disclaimer + * of warranty appears in all copies. + * + * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER + * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY + * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM + * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE + * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT + * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, + * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, + * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, + * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR + * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT + * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. + */ + +package gov.nist.secauto.metaschema.core.metapath.cst; + +import gov.nist.secauto.metaschema.core.metapath.DynamicContext; +import gov.nist.secauto.metaschema.core.metapath.ISequence; +import gov.nist.secauto.metaschema.core.metapath.item.function.IArrayItem; + +import java.util.List; + +import edu.umd.cs.findbugs.annotations.Nullable; + +public class ArraySequence implements IExpression { + @Nullable + private final IExpression expr; + + public ArraySequence(@Nullable IExpression expr) { + this.expr = expr; + } + + @SuppressWarnings("rawtypes") + @Override + public Class getBaseResultType() { + return IArrayItem.class; + } + + @SuppressWarnings("rawtypes") + @Override + public Class getStaticResultType() { + return IArrayItem.class; + } + + @SuppressWarnings("null") + @Override + public List getChildren() { + return List.of(expr); + } + + @Override + public ISequence> accept(DynamicContext dynamicContext, ISequence focus) { + ISequence> retval; + if (expr != null) { + IArrayItem array = IArrayItem.ofCollection(expr.accept(dynamicContext, focus)); + retval = ISequence.of(array); + } else { + retval = ISequence.of(); + } + return retval; + } + + @Override + public RESULT accept(IExpressionVisitor visitor, CONTEXT context) { + return visitor.visitArray(this, context); + } +} diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/ArraySquare.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/ArraySquare.java new file mode 100644 index 000000000..35dd54ef0 --- /dev/null +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/ArraySquare.java @@ -0,0 +1,64 @@ +/* + * Portions of this software was developed by employees of the National Institute + * of Standards and Technology (NIST), an agency of the Federal Government and is + * being made available as a public service. Pursuant to title 17 United States + * Code Section 105, works of NIST employees are not subject to copyright + * protection in the United States. This software may be subject to foreign + * copyright. Permission in the United States and in foreign countries, to the + * extent that NIST may hold copyright, to use, copy, modify, create derivative + * works, and distribute this software and its documentation without fee is hereby + * granted on a non-exclusive basis, provided that this notice and disclaimer + * of warranty appears in all copies. + * + * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER + * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY + * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM + * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE + * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT + * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, + * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, + * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, + * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR + * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT + * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. + */ + +package gov.nist.secauto.metaschema.core.metapath.cst; + +import gov.nist.secauto.metaschema.core.metapath.DynamicContext; +import gov.nist.secauto.metaschema.core.metapath.ISequence; +import gov.nist.secauto.metaschema.core.metapath.item.IItem; +import gov.nist.secauto.metaschema.core.metapath.item.function.IArrayItem; + +import java.util.List; + +import edu.umd.cs.findbugs.annotations.NonNull; + +public class ArraySquare implements IExpression { + @NonNull + private final List children; + + public ArraySquare(@NonNull List children) { + this.children = children; + } + + @Override + public List getChildren() { + return children; + } + + @Override + public ISequence accept(DynamicContext dynamicContext, ISequence focus) { + return ISequence.of(getChildren().stream() + .map(expr -> expr.accept(dynamicContext, focus)) + .map(ISequence::toArrayMember) + .collect(IArrayItem.toArrayItem())); + } + + @Override + public RESULT accept(IExpressionVisitor visitor, CONTEXT context) { + return visitor.visitArray(this, context); + } + +} diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/BuildCSTVisitor.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/BuildCSTVisitor.java index 471d2acd1..2f3f254dd 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/BuildCSTVisitor.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/BuildCSTVisitor.java @@ -38,6 +38,7 @@ import gov.nist.secauto.metaschema.core.metapath.antlr.Metapath10.AxisstepContext; import gov.nist.secauto.metaschema.core.metapath.antlr.Metapath10.ComparisonexprContext; import gov.nist.secauto.metaschema.core.metapath.antlr.Metapath10.ContextitemexprContext; +import gov.nist.secauto.metaschema.core.metapath.antlr.Metapath10.CurlyarrayconstructorContext; import gov.nist.secauto.metaschema.core.metapath.antlr.Metapath10.EqnameContext; import gov.nist.secauto.metaschema.core.metapath.antlr.Metapath10.ExprContext; import gov.nist.secauto.metaschema.core.metapath.antlr.Metapath10.ExprsingleContext; @@ -47,8 +48,10 @@ import gov.nist.secauto.metaschema.core.metapath.antlr.Metapath10.GeneralcompContext; import gov.nist.secauto.metaschema.core.metapath.antlr.Metapath10.IfexprContext; import gov.nist.secauto.metaschema.core.metapath.antlr.Metapath10.IntersectexceptexprContext; +import gov.nist.secauto.metaschema.core.metapath.antlr.Metapath10.KeyspecifierContext; import gov.nist.secauto.metaschema.core.metapath.antlr.Metapath10.LetexprContext; import gov.nist.secauto.metaschema.core.metapath.antlr.Metapath10.LiteralContext; +import gov.nist.secauto.metaschema.core.metapath.antlr.Metapath10.LookupContext; import gov.nist.secauto.metaschema.core.metapath.antlr.Metapath10.MultiplicativeexprContext; import gov.nist.secauto.metaschema.core.metapath.antlr.Metapath10.NametestContext; import gov.nist.secauto.metaschema.core.metapath.antlr.Metapath10.NodetestContext; @@ -67,14 +70,17 @@ import gov.nist.secauto.metaschema.core.metapath.antlr.Metapath10.SimpleletbindingContext; import gov.nist.secauto.metaschema.core.metapath.antlr.Metapath10.SimpleletclauseContext; import gov.nist.secauto.metaschema.core.metapath.antlr.Metapath10.SimplemapexprContext; +import gov.nist.secauto.metaschema.core.metapath.antlr.Metapath10.SquarearrayconstructorContext; import gov.nist.secauto.metaschema.core.metapath.antlr.Metapath10.StringconcatexprContext; import gov.nist.secauto.metaschema.core.metapath.antlr.Metapath10.UnaryexprContext; +import gov.nist.secauto.metaschema.core.metapath.antlr.Metapath10.UnarylookupContext; import gov.nist.secauto.metaschema.core.metapath.antlr.Metapath10.UnionexprContext; import gov.nist.secauto.metaschema.core.metapath.antlr.Metapath10.ValuecompContext; import gov.nist.secauto.metaschema.core.metapath.antlr.Metapath10.VarnameContext; import gov.nist.secauto.metaschema.core.metapath.antlr.Metapath10.VarrefContext; import gov.nist.secauto.metaschema.core.metapath.antlr.Metapath10.WildcardContext; import gov.nist.secauto.metaschema.core.metapath.antlr.Metapath10Lexer; +import gov.nist.secauto.metaschema.core.metapath.cst.AbstractLookup.IKeySpecifier; import gov.nist.secauto.metaschema.core.metapath.cst.comparison.GeneralComparison; import gov.nist.secauto.metaschema.core.metapath.cst.comparison.ValueComparison; import gov.nist.secauto.metaschema.core.metapath.cst.math.Addition; @@ -97,6 +103,7 @@ import gov.nist.secauto.metaschema.core.metapath.cst.path.Step; import gov.nist.secauto.metaschema.core.metapath.cst.path.Wildcard; import gov.nist.secauto.metaschema.core.metapath.function.ComparisonFunctions; +import gov.nist.secauto.metaschema.core.metapath.item.atomic.IIntegerItem; import gov.nist.secauto.metaschema.core.metapath.item.node.IDefinitionNodeItem; import gov.nist.secauto.metaschema.core.util.CollectionUtil; import gov.nist.secauto.metaschema.core.util.ObjectUtils; @@ -138,11 +145,10 @@ public BuildCSTVisitor(@NonNull StaticContext context) { this.context = context; } - /* - * ============================================================ Expressions - - * https://www.w3.org/TR/xpath-31/#id-expressions - * ============================================================ - */ + // ============================================================ + // Expressions - https://www.w3.org/TR/xpath-31/#id-expressions + // ============================================================ + @NonNull protected StaticContext getContext() { return context; @@ -156,11 +162,9 @@ protected IExpression handleExpr(ExprContext ctx) { }); } - /* - * ================================================================= Literal - * Expressions - https://www.w3.org/TR/xpath-31/#id-literals - * ================================================================= - */ + // ================================================================= + // Literal Expressions - https://www.w3.org/TR/xpath-31/#id-literals + // ================================================================= @Override protected IExpression handleStringLiteral(LiteralContext ctx) { @@ -187,11 +191,9 @@ protected IExpression handleNumericLiteral(NumericliteralContext ctx) { return retval; } - /* - * ================================================================== Variable - * References - https://www.w3.org/TR/xpath-31/#id-variables - * ================================================================== - */ + // ================================================================== + // Variable References - https://www.w3.org/TR/xpath-31/#id-variables + // ================================================================== @Override protected IExpression handleVarref(VarrefContext ctx) { @@ -201,11 +203,9 @@ protected IExpression handleVarref(VarrefContext ctx) { getContext().getVariablePrefixResolver())); } - /* - * ==================================================================== For - * Expressions - https://www.w3.org/TR/xpath-31/#id-for-expressions - * ==================================================================== - */ + // ==================================================================== + // For Expressions - https://www.w3.org/TR/xpath-31/#id-for-expressions + // ==================================================================== @SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops") @Override @@ -238,11 +238,9 @@ protected IExpression handleForexpr(ForexprContext ctx) { return retval; } - /* - * ==================================================================== Let - * Expressions - https://www.w3.org/TR/xpath-31/#id-let-expressions - * ==================================================================== - */ + // ==================================================================== + // Let Expressions - https://www.w3.org/TR/xpath-31/#id-let-expressions + // ==================================================================== @Override protected IExpression handleLet(LetexprContext context) { @@ -267,13 +265,66 @@ protected IExpression handleLet(LetexprContext context) { return retval; } - /* - * ============================================================================= - * ===== Quantified Expressions - - * https://www.w3.org/TR/xpath-31/#id-quantified-expressions - * ============================================================================= - * ===== - */ + // ============================================================== + // Array Constructors - https://www.w3.org/TR/xpath-31/#id-arrays + // ============================================================== + + @Override + protected IExpression handleArrayConstructor(SquarearrayconstructorContext context) { + if (context.getChildCount() == 2) { + // empty + return new ArraySquare(CollectionUtil.emptyList()); + } + + return nAiryToCollection(context, 1, 2, + (ctx, idx) -> { + int pos = (idx - 1) / 2; + ParseTree tree = ctx.exprsingle(pos); + return visit(tree); + }, + children -> { + assert children != null; + return new ArraySquare(children); + }); + } + + @Override + protected IExpression handleArrayConstructor(CurlyarrayconstructorContext ctx) { + return new ArraySequence(visit(ctx.enclosedexpr())); + } + + // =============================================== + // Unary Lookup - + // https://www.w3.org/TR/xpath-31/#id-unary-lookup + // =============================================== + + @Override + protected IExpression handleUnarylookup(UnarylookupContext ctx) { + KeyspecifierContext specifier = ctx.keyspecifier(); + + IKeySpecifier keySpecifier; + if (specifier.parenthesizedexpr() != null) { + keySpecifier + = new UnaryLookup.ParenthesizedExprKeySpecifier( + ObjectUtils.requireNonNull(specifier.parenthesizedexpr().accept(this))); + } else if (specifier.NCName() != null) { + keySpecifier + = new UnaryLookup.NCNameKeySpecifier(ObjectUtils.requireNonNull(specifier.NCName().getText())); + } else if (specifier.IntegerLiteral() != null) { + keySpecifier = new UnaryLookup.IntegerLiteralKeySpecifier( + IIntegerItem.valueOf(ObjectUtils.requireNonNull(specifier.IntegerLiteral().getText()))); + } else if (specifier.STAR() != null) { + keySpecifier = new UnaryLookup.WildcardKeySpecifier(); + } else { + throw new UnsupportedOperationException("unknown key specifier"); + } + return new UnaryLookup(keySpecifier); + } + + // ========================================================= + // Quantified Expressions - + // https://www.w3.org/TR/xpath-31/#id-quantified-expressions + // ========================================================= @Override protected IExpression handleQuantifiedexpr(QuantifiedexprContext ctx) { @@ -310,11 +361,9 @@ protected IExpression handleQuantifiedexpr(QuantifiedexprContext ctx) { return new Quantified(quantifier, vars, satisfies); } - /* - * ======================================================================= Arrow - * operator (=>) - https://www.w3.org/TR/xpath-31/#id-arrow-operator - * ======================================================================= - */ + // ======================================================================= + // Arrow operator (=>) - https://www.w3.org/TR/xpath-31/#id-arrow-operator + // ======================================================================= @Override protected IExpression handleArrowexpr(ArrowexprContext context) { @@ -342,37 +391,29 @@ protected IExpression handleArrowexpr(ArrowexprContext context) { }); } - /* - * ============================================================================= - * ==== Parenthesized Expressions - - * https://www.w3.org/TR/xpath-31/#id-paren-expressions - * ============================================================================= - * ==== - */ + // ==================================================== + // Parenthesized Expressions - + // https://www.w3.org/TR/xpath-31/#id-paren-expressions + // ==================================================== @Override protected IExpression handleEmptyParenthesizedexpr(ParenthesizedexprContext ctx) { return EmptySequence.instance(); } - /* - * ============================================================================= - * ======== Context Item Expression - - * https://www.w3.org/TR/xpath-31/#id-context-item-expression - * ============================================================================= - * ======== - */ + // ========================================================== + // Context Item Expression - + // https://www.w3.org/TR/xpath-31/#id-context-item-expression + // ========================================================== @Override protected IExpression handleContextitemexpr(ContextitemexprContext ctx) { return ContextItem.instance(); } - /* - * ========================================================================= - * Static Function Calls - https://www.w3.org/TR/xpath-31/#id-function-calls - * ========================================================================= - */ + // ========================================================================= + // Static Function Calls - https://www.w3.org/TR/xpath-31/#id-function-calls + // ========================================================================= /** * Parse a list of arguments. @@ -412,11 +453,9 @@ protected IExpression handleFunctioncall(FunctioncallContext ctx) { .collect(Collectors.toUnmodifiableList()))); } - /* - * ========================================================================= - * Filter Expressions - https://www.w3.org/TR/xpath-31/#id-filter-expression - * ========================================================================= - */ + // ========================================================================= + // Filter Expressions - https://www.w3.org/TR/xpath-31/#id-filter-expression + // ========================================================================= /** * Parse a predicate AST. @@ -466,23 +505,65 @@ protected List parsePredicates(@NonNull ParseTree context, int star } @Override - protected IExpression handlePostfixexpr(PostfixexprContext ctx) { - int numChildren = ctx.getChildCount(); - ParseTree primaryTree = ctx.getChild(0); - IExpression retval = ObjectUtils.notNull(primaryTree.accept(this)); + protected IExpression handlePostfixexpr(PostfixexprContext context) { + return handleGroupedNAiry( + context, + 0, + 1, + (ctx, idx, left) -> { + ParseTree tree = ctx.getChild(idx); + IExpression result; + if (tree instanceof ArgumentlistContext) { + // map or array access using function call syntax + result = new FunctionCallAccessor( + left, + parseArgumentList((ArgumentlistContext) tree).findFirst().get()); + } else if (tree instanceof PredicateContext) { + result = new PredicateExpression( + left, + CollectionUtil.singletonList(parsePredicate((PredicateContext) tree))); + } else if (tree instanceof LookupContext) { + KeyspecifierContext specifier = ((LookupContext) tree).keyspecifier(); + + IKeySpecifier keySpecifier; + if (specifier.parenthesizedexpr() != null) { + keySpecifier + = new PostfixLookup.ParenthesizedExprKeySpecifier( + ObjectUtils.requireNonNull(specifier.parenthesizedexpr().accept(this))); + } else if (specifier.NCName() != null) { + keySpecifier + = new PostfixLookup.NCNameKeySpecifier(ObjectUtils.requireNonNull(specifier.NCName().getText())); + } else if (specifier.IntegerLiteral() != null) { + keySpecifier = new PostfixLookup.IntegerLiteralKeySpecifier( + IIntegerItem.valueOf(ObjectUtils.requireNonNull(specifier.IntegerLiteral().getText()))); + } else if (specifier.STAR() != null) { + keySpecifier = new PostfixLookup.WildcardKeySpecifier(); + } else { + throw new UnsupportedOperationException("unknown key specifier"); + } + result = new PostfixLookup(left, keySpecifier); + } else { + result = visit(tree); + } + return result; + }); + } - List predicates = numChildren > 1 ? parsePredicates(ctx, 1) : CollectionUtil.emptyList(); + // ====================================================================== + // Path Expressions - https://www.w3.org/TR/xpath-31/#id-path-expressions + // ====================================================================== - if (!predicates.isEmpty()) { - retval = new PredicateExpression(retval, predicates); - } - return retval; + @Override + protected IExpression handlePredicate(PredicateContext ctx) { + parsePredicate(ctx); + return null; + } + + @Override + protected IExpression handleLookup(LookupContext ctx) { + // TODO Auto-generated method stub + return null; } - /* - * ====================================================================== Path - * Expressions - https://www.w3.org/TR/xpath-31/#id-path-expressions - * ====================================================================== - */ @Override protected IExpression handlePathexpr(PathexprContext ctx) { @@ -516,13 +597,10 @@ protected IExpression handlePathexpr(PathexprContext ctx) { return retval; } - /* - * ============================================================================= - * ========== RelativePath Expressions - - * https://www.w3.org/TR/xpath-31/#id-relative-path-expressions - * ============================================================================= - * ========== - */ + // ============================================================ + // RelativePath Expressions - + // https://www.w3.org/TR/xpath-31/#id-relative-path-expressions + // ============================================================ @Override protected IExpression handleRelativepathexpr(RelativepathexprContext context) { @@ -549,11 +627,9 @@ protected IExpression handleRelativepathexpr(RelativepathexprContext context) { }); } - /* - * ================================================ Steps - - * https://www.w3.org/TR/xpath-31/#id-steps - * ================================================ - */ + // ================================================ + // Steps - https://www.w3.org/TR/xpath-31/#id-steps + // ================================================ @Override protected IExpression handleForwardstep(ForwardstepContext ctx) { @@ -616,17 +692,9 @@ protected IExpression handleReversestep(ReversestepContext ctx) { return new Step(axis, parseNodeTest(ctx.nodetest(), false)); } - /* - * ======================================================= Node Tests - - * https://www.w3.org/TR/xpath-31/#node-tests - * ======================================================= - */ - - /* - * ======================================================= Node Tests - - * https://www.w3.org/TR/xpath-31/#node-tests - * ======================================================= - */ + // ======================================================= + // Node Tests - https://www.w3.org/TR/xpath-31/#node-tests + // ======================================================= protected INodeTestExpression parseNodeTest(NodetestContext ctx, boolean flag) { // TODO: implement kind test @@ -674,11 +742,9 @@ protected Wildcard handleWildcard(WildcardContext ctx) { return new Wildcard(matcher); } - /* - * ====================================================================== - * Predicates within Steps - https://www.w3.org/TR/xpath-31/#id-predicate - * ====================================================================== - */ + // ====================================================================== + // Predicates within Steps - https://www.w3.org/TR/xpath-31/#id-predicate + // ====================================================================== @Override protected IExpression handleAxisstep(AxisstepContext ctx) { @@ -691,11 +757,9 @@ protected IExpression handleAxisstep(AxisstepContext ctx) { return predicates.isEmpty() ? step : new PredicateExpression(step, predicates); } - /* - * =========================================================== Abbreviated - * Syntax - https://www.w3.org/TR/xpath-31/#abbrev - * =========================================================== - */ + // =========================================================== + // Abbreviated Syntax - https://www.w3.org/TR/xpath-31/#abbrev + // =========================================================== @Override protected IExpression handleAbbrevforwardstep(AbbrevforwardstepContext ctx) { @@ -716,11 +780,9 @@ protected IExpression handleAbbrevreversestep(AbbrevreversestepContext ctx) { return Axis.PARENT; } - /* - * ====================================================================== - * Constructing Sequences - https://www.w3.org/TR/xpath-31/#construct_seq - * ====================================================================== - */ + // ====================================================================== + // Constructing Sequences - https://www.w3.org/TR/xpath-31/#construct_seq + // ====================================================================== @Override protected IExpression handleRangeexpr(RangeexprContext ctx) { @@ -732,11 +794,9 @@ protected IExpression handleRangeexpr(RangeexprContext ctx) { return new Range(left, right); } - /* - * ======================================================================== - * Combining Node Sequences - https://www.w3.org/TR/xpath-31/#combining_seq - * ======================================================================== - */ + // ======================================================================== + // Combining Node Sequences - https://www.w3.org/TR/xpath-31/#combining_seq + // ======================================================================== @Override protected IExpression handleUnionexpr(UnionexprContext ctx) { @@ -771,11 +831,9 @@ protected IExpression handleIntersectexceptexpr(IntersectexceptexprContext conte }); } - /* - * ====================================================================== - * Arithmetic Expressions - https://www.w3.org/TR/xpath-31/#id-arithmetic - * ====================================================================== - */ + // ====================================================================== + // Arithmetic Expressions - https://www.w3.org/TR/xpath-31/#id-arithmetic + // ====================================================================== @Override protected IExpression handleAdditiveexpr(AdditiveexprContext context) { @@ -863,13 +921,10 @@ protected IExpression handleUnaryexpr(UnaryexprContext ctx) { return retval; } - /* - * ============================================================================= - * =========== String Concatenation Expressions - - * https://www.w3.org/TR/xpath-31/#id-string-concat-expr - * ============================================================================= - * =========== - */ + // ===================================================== + // String Concatenation Expressions - + // https://www.w3.org/TR/xpath-31/#id-string-concat-expr + // ===================================================== @Override protected IExpression handleStringconcatexpr(StringconcatexprContext ctx) { @@ -879,11 +934,9 @@ protected IExpression handleStringconcatexpr(StringconcatexprContext ctx) { }); } - /* - * ======================================================================= - * Comparison Expressions - https://www.w3.org/TR/xpath-31/#id-comparisons - * ======================================================================= - */ + // ======================================================================= + // Comparison Expressions - https://www.w3.org/TR/xpath-31/#id-comparisons + // ======================================================================= @Override protected IExpression handleComparisonexpr(ComparisonexprContext ctx) { // NOPMD - ok @@ -956,11 +1009,9 @@ protected IExpression handleComparisonexpr(ComparisonexprContext ctx) { // NOPMD return retval; } - /* - * ============================================================================ - * Logical Expressions - https://www.w3.org/TR/xpath-31/#id-logical-expressions - * ============================================================================ - */ + // ============================================================================ + // Logical Expressions - https://www.w3.org/TR/xpath-31/#id-logical-expressions + // ============================================================================ @Override protected IExpression handleOrexpr(OrexprContext ctx) { @@ -978,11 +1029,9 @@ protected IExpression handleAndexpr(AndexprContext ctx) { }); } - /* - * ========================================================================= - * Conditional Expressions - https://www.w3.org/TR/xpath-31/#id-conditionals - * ========================================================================= - */ + // ========================================================================= + // Conditional Expressions - https://www.w3.org/TR/xpath-31/#id-conditionals + // ========================================================================= @Override protected IExpression handleIfexpr(IfexprContext ctx) { @@ -993,11 +1042,9 @@ protected IExpression handleIfexpr(IfexprContext ctx) { return new If(testExpr, thenExpr, elseExpr); } - /* - * ========================================================================= - * Simple map operator (!) - https://www.w3.org/TR/xpath-31/#id-map-operator - * ========================================================================= - */ + // ========================================================================= + // Simple map operator (!) - https://www.w3.org/TR/xpath-31/#id-map-operator + // ========================================================================= @Override protected IExpression handleSimplemapexpr(SimplemapexprContext context) { diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/CSTPrinter.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/CSTPrinter.java index 635edacb7..44a842c47 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/CSTPrinter.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/CSTPrinter.java @@ -336,6 +336,31 @@ public String visitSimpleMap(SimpleMap expr, State context) { return appendNode(expr, super.visitSimpleMap(expr, context), context); } + @Override + public String visitArray(ArraySequence expr, State context) { + return appendNode(expr, super.visitArray(expr, context), context); + } + + @Override + public String visitArray(ArraySquare expr, State context) { + return appendNode(expr, super.visitArray(expr, context), context); + } + + @Override + public String visitPostfixLookup(PostfixLookup expr, State context) { + return appendNode(expr, super.visitPostfixLookup(expr, context), context); + } + + @Override + public String visitFunctionCallAccessor(FunctionCallAccessor expr, State context) { + return appendNode(expr, super.visitFunctionCallAccessor(expr, context), context); + } + + @Override + public String visitUnaryLookup(UnaryLookup expr, State context) { + return appendNode(expr, super.visitUnaryLookup(expr, context), context); + } + } static class State { diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/Except.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/Except.java index cacef3b31..8070ad825 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/Except.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/Except.java @@ -55,7 +55,7 @@ public Except(@NonNull IExpression left, @NonNull IExpression right) { @Override protected ISequence applyFilterTo(@NonNull ISequence result, @NonNull List items) { - return ISequence.of(result.asStream() + return ISequence.of(result.stream() .filter(item -> !items.contains(item))); } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/For.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/For.java index 186aed9da..cc87e3219 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/For.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/For.java @@ -82,6 +82,6 @@ public ISequence accept(DynamicContext dynamicContext, ISequenc subDynamicContext.bindVariableValue(variable.getName(), ISequence.of(item)); retval.addAll(getReturnExpression().accept(subDynamicContext, focus)); } - return ISequence.of(retval); + return ISequence.ofCollection(retval); } } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/FunctionCallAccessor.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/FunctionCallAccessor.java new file mode 100644 index 000000000..39b2213b7 --- /dev/null +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/FunctionCallAccessor.java @@ -0,0 +1,88 @@ +/* + * Portions of this software was developed by employees of the National Institute + * of Standards and Technology (NIST), an agency of the Federal Government and is + * being made available as a public service. Pursuant to title 17 United States + * Code Section 105, works of NIST employees are not subject to copyright + * protection in the United States. This software may be subject to foreign + * copyright. Permission in the United States and in foreign countries, to the + * extent that NIST may hold copyright, to use, copy, modify, create derivative + * works, and distribute this software and its documentation without fee is hereby + * granted on a non-exclusive basis, provided that this notice and disclaimer + * of warranty appears in all copies. + * + * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER + * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY + * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM + * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE + * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT + * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, + * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, + * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, + * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR + * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT + * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. + */ + +package gov.nist.secauto.metaschema.core.metapath.cst; + +import gov.nist.secauto.metaschema.core.metapath.DynamicContext; +import gov.nist.secauto.metaschema.core.metapath.ISequence; +import gov.nist.secauto.metaschema.core.metapath.function.library.ArrayGet; +import gov.nist.secauto.metaschema.core.metapath.item.IItem; +import gov.nist.secauto.metaschema.core.util.ObjectUtils; + +import java.util.List; + +import edu.umd.cs.findbugs.annotations.NonNull; + +public class FunctionCallAccessor implements IExpression { + @NonNull + private final IExpression base; + @NonNull + private final IExpression argument; + + public FunctionCallAccessor(@NonNull IExpression base, @NonNull IExpression argument) { + this.base = base; + this.argument = argument; + } + + /** + * Get the base sub-expression. + * + * @return the sub-expression + */ + @NonNull + public IExpression getBase() { + return base; + } + + /** + * Retrieve the argument to use for the lookup. + * + * @return the argument + */ + @NonNull + public IExpression getArgument() { + return argument; + } + + @SuppressWarnings("null") + @Override + public List getChildren() { + return List.of(getBase(), getArgument()); + } + + @Override + public ISequence accept(DynamicContext dynamicContext, ISequence focus) { + ISequence collection = getBase().accept(dynamicContext, focus); + ISequence key = getArgument().accept(dynamicContext, focus); + + return ArrayGet.SIGNATURE.execute(ObjectUtils.notNull(List.of(collection, key)), dynamicContext, focus); + } + + @Override + public RESULT accept(@NonNull IExpressionVisitor visitor, CONTEXT context) { + return visitor.visitFunctionCallAccessor(this, context); + } +} diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/IExpressionVisitor.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/IExpressionVisitor.java index d0642f479..633120ab1 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/IExpressionVisitor.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/IExpressionVisitor.java @@ -510,4 +510,59 @@ public interface IExpressionVisitor { * @return the visitation result or {@code null} if no result was produced */ RESULT visitSimpleMap(@NonNull SimpleMap expr, @NonNull CONTEXT context); + + /** + * Visit the CST node. + * + * @param expr + * the CST node to visit + * @param context + * the processing context + * @return the visitation result or {@code null} if no result was produced + */ + RESULT visitArray(@NonNull ArraySequence expr, @NonNull CONTEXT context); + + /** + * Visit the CST node. + * + * @param expr + * the CST node to visit + * @param context + * the processing context + * @return the visitation result or {@code null} if no result was produced + */ + RESULT visitArray(@NonNull ArraySquare expr, @NonNull CONTEXT context); + + /** + * Visit the CST node. + * + * @param expr + * the CST node to visit + * @param context + * the processing context + * @return the visitation result or {@code null} if no result was produced + */ + RESULT visitPostfixLookup(@NonNull PostfixLookup expr, @NonNull CONTEXT context); + + /** + * Visit the CST node. + * + * @param expr + * the CST node to visit + * @param context + * the processing context + * @return the visitation result or {@code null} if no result was produced + */ + RESULT visitFunctionCallAccessor(@NonNull FunctionCallAccessor expr, @NonNull CONTEXT context); + + /** + * Visit the CST node. + * + * @param expr + * the CST node to visit + * @param context + * the processing context + * @return the visitation result or {@code null} if no result was produced + */ + RESULT visitUnaryLookup(@NonNull UnaryLookup expr, @NonNull CONTEXT context); } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/Intersect.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/Intersect.java index 04999c6de..3a51e26d4 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/Intersect.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/Intersect.java @@ -56,7 +56,7 @@ public Intersect(@NonNull IExpression left, @NonNull IExpression right) { @Override protected ISequence applyFilterTo(@NonNull ISequence result, @NonNull List items) { - return ISequence.of(result.asStream() + return ISequence.of(result.stream() .distinct() .filter(items::contains)); } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/Metapath.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/Metapath.java index 8abb5a942..f77150838 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/Metapath.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/Metapath.java @@ -68,7 +68,7 @@ public ISequence accept(DynamicContext dynamicContext, ISequence focus) { Stream retval = ObjectUtils.notNull(getChildren().stream() .flatMap(child -> { ISequence result = child.accept(dynamicContext, focus); - return result.asStream(); + return result.stream(); })); return ISequence.of(retval); } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/PostfixLookup.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/PostfixLookup.java new file mode 100644 index 000000000..1e0108676 --- /dev/null +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/PostfixLookup.java @@ -0,0 +1,80 @@ +/* + * Portions of this software was developed by employees of the National Institute + * of Standards and Technology (NIST), an agency of the Federal Government and is + * being made available as a public service. Pursuant to title 17 United States + * Code Section 105, works of NIST employees are not subject to copyright + * protection in the United States. This software may be subject to foreign + * copyright. Permission in the United States and in foreign countries, to the + * extent that NIST may hold copyright, to use, copy, modify, create derivative + * works, and distribute this software and its documentation without fee is hereby + * granted on a non-exclusive basis, provided that this notice and disclaimer + * of warranty appears in all copies. + * + * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER + * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY + * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM + * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE + * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT + * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, + * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, + * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, + * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR + * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT + * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. + */ + +package gov.nist.secauto.metaschema.core.metapath.cst; + +import gov.nist.secauto.metaschema.core.metapath.DynamicContext; +import gov.nist.secauto.metaschema.core.metapath.ICollectionValue; +import gov.nist.secauto.metaschema.core.metapath.ISequence; +import gov.nist.secauto.metaschema.core.metapath.item.IItem; + +import java.util.List; + +import edu.umd.cs.findbugs.annotations.NonNull; + +public class PostfixLookup + extends AbstractLookup { + + @NonNull + private final IExpression base; + + public PostfixLookup(@NonNull IExpression base, @NonNull IKeySpecifier keySpecifier) { + super(keySpecifier); + this.base = base; + } + + /** + * Get the base sub-expression. + * + * @return the sub-expression + */ + @NonNull + public IExpression getBase() { + return base; + } + + @SuppressWarnings("null") + @Override + public List getChildren() { + return List.of(getBase()); + } + + @Override + public ISequence accept(DynamicContext dynamicContext, ISequence focus) { + ISequence base = getBase().accept(dynamicContext, focus); + + IKeySpecifier specifier = getKeySpecifier(); + + return ISequence.of(base.stream() + .flatMap(item -> specifier.lookup(item, dynamicContext, focus)) + .flatMap(ICollectionValue::normalizeAsItems)); + } + + @Override + public RESULT accept(@NonNull IExpressionVisitor visitor, CONTEXT context) { + return visitor.visitPostfixLookup(this, context); + } +} diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/PredicateExpression.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/PredicateExpression.java index 8e33e838b..cca0a474c 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/PredicateExpression.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/PredicateExpression.java @@ -99,7 +99,7 @@ public List getChildren() { AtomicInteger index = new AtomicInteger(); Stream stream = ObjectUtils.notNull( - retval.asStream().map(item -> { + retval.stream().map(item -> { // build a positional index of the items return Map.entry(BigInteger.valueOf(index.incrementAndGet()), item); }).filter(entry -> { diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/Range.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/Range.java index a0914210e..f89d13286 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/Range.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/Range.java @@ -84,7 +84,7 @@ public ISequence accept(DynamicContext dynamicContext, ISequence accept(DynamicContext dynamicContext, ISequence focus) { IExpression right = getRight(); return ObjectUtils.notNull(leftResult.stream() - .flatMap(item -> right.accept(dynamicContext, ISequence.of(item)).asStream()) + .flatMap(item -> right.accept(dynamicContext, ISequence.of(item)).stream()) .collect(ISequence.toSequence())); } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/StringConcat.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/StringConcat.java index 2f7c36f27..757163ee9 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/StringConcat.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/StringConcat.java @@ -71,7 +71,7 @@ public ISequence accept(DynamicContext dynamicContext, ISequence focus) { StringBuilder builder = new StringBuilder(); for (IExpression child : getChildren()) { ISequence result = child.accept(dynamicContext, focus); - FnData.fnData(result).asStream() + FnData.fnData(result).stream() .forEachOrdered(item -> { builder.append(item.asString()); }); diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/UnaryLookup.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/UnaryLookup.java new file mode 100644 index 000000000..2e2bb0d07 --- /dev/null +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/UnaryLookup.java @@ -0,0 +1,64 @@ +/* + * Portions of this software was developed by employees of the National Institute + * of Standards and Technology (NIST), an agency of the Federal Government and is + * being made available as a public service. Pursuant to title 17 United States + * Code Section 105, works of NIST employees are not subject to copyright + * protection in the United States. This software may be subject to foreign + * copyright. Permission in the United States and in foreign countries, to the + * extent that NIST may hold copyright, to use, copy, modify, create derivative + * works, and distribute this software and its documentation without fee is hereby + * granted on a non-exclusive basis, provided that this notice and disclaimer + * of warranty appears in all copies. + * + * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER + * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY + * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM + * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE + * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT + * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, + * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, + * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, + * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR + * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT + * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. + */ + +package gov.nist.secauto.metaschema.core.metapath.cst; + +import gov.nist.secauto.metaschema.core.metapath.DynamicContext; +import gov.nist.secauto.metaschema.core.metapath.ICollectionValue; +import gov.nist.secauto.metaschema.core.metapath.ISequence; +import gov.nist.secauto.metaschema.core.metapath.item.IItem; + +import java.util.List; + +import edu.umd.cs.findbugs.annotations.NonNull; + +public class UnaryLookup + extends AbstractLookup { + + public UnaryLookup(@NonNull IKeySpecifier keySpecifier) { + super(keySpecifier); + } + + @SuppressWarnings("null") + @Override + public List getChildren() { + return List.of(); + } + + @Override + public ISequence accept(DynamicContext dynamicContext, ISequence focus) { + IKeySpecifier specifier = getKeySpecifier(); + + return ISequence.of(focus.stream() + .flatMap(item -> specifier.lookup(item, dynamicContext, focus)) + .flatMap(ICollectionValue::normalizeAsItems)); + } + + @Override + public RESULT accept(@NonNull IExpressionVisitor visitor, CONTEXT context) { + return visitor.visitUnaryLookup(this, context); + } +} diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/Union.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/Union.java index 7eaacd691..ab8be293f 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/Union.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/Union.java @@ -70,11 +70,14 @@ public RESULT accept(IExpressionVisitor visit @Override public ISequence accept(DynamicContext dynamicContext, ISequence focus) { - focus.collect(); + // ensure the sequence is backed by a list + focus.getValue(); + + // now process the union @NonNull Stream retval = ObjectUtils.notNull(getChildren().stream() .flatMap(child -> { ISequence result = child.accept(dynamicContext, focus); - return result.asStream(); + return result.stream(); }).distinct()); return ISequence.of(retval); } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/path/AbstractPathExpression.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/path/AbstractPathExpression.java index a381d9419..e0354e852 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/path/AbstractPathExpression.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/path/AbstractPathExpression.java @@ -68,14 +68,14 @@ protected Stream searchExpression( @NonNull IExpression expression, @NonNull DynamicContext dynamicContext, @NonNull ISequence outerFocus) { - - outerFocus.collect(); + // ensure the sequence is backed by a list + outerFocus.getValue(); // check the current focus @SuppressWarnings("unchecked") Stream nodeMatches - = (Stream) expression.accept(dynamicContext, outerFocus).asStream(); + = (Stream) expression.accept(dynamicContext, outerFocus).stream(); - Stream childMatches = outerFocus.asStream() + Stream childMatches = outerFocus.stream() .map(ItemUtils::checkItemIsNodeItemForStep) .flatMap(focusedNode -> { diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/path/Axis.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/path/Axis.java index 8965ee515..d9f61f15c 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/path/Axis.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/path/Axis.java @@ -98,7 +98,7 @@ public ISequence accept( if (outerFocus.isEmpty()) { retval = ISequence.empty(); } else { - retval = ISequence.of(outerFocus.asStream() + retval = ISequence.of(outerFocus.stream() .map(ItemUtils::checkItemIsNodeItemForStep) .flatMap(item -> { assert item != null; diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/path/Flag.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/path/Flag.java index 9dd6b5fea..e04f6a13d 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/path/Flag.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/path/Flag.java @@ -69,7 +69,7 @@ public RESULT accept(IExpressionVisitor visit public ISequence accept( DynamicContext dynamicContext, ISequence focus) { - return ISequence.of(focus.asStream() + return ISequence.of(focus.stream() .map(ItemUtils::checkItemIsNodeItemForStep) .flatMap(item -> { assert item != null; diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/path/ModelInstance.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/path/ModelInstance.java index 4b17abc81..c1ee8298a 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/path/ModelInstance.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/path/ModelInstance.java @@ -71,7 +71,7 @@ public RESULT accept(IExpressionVisitor visit public ISequence> accept( DynamicContext dynamicContext, ISequence focus) { - return ISequence.of(focus.asStream() + return ISequence.of(focus.stream() .map(ItemUtils::checkItemIsNodeItemForStep) .flatMap(item -> { assert item != null; diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/path/NameTest.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/path/NameTest.java index d23d39582..884d7491f 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/path/NameTest.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/path/NameTest.java @@ -77,7 +77,7 @@ public RESULT accept(IExpressionVisitor visit public ISequence accept( DynamicContext dynamicContext, ISequence focus) { - return ISequence.of(focus.asStream() + return ISequence.of(focus.stream() .map(ItemUtils::checkItemIsNodeItemForStep) .filter(this::match)); } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/path/RootSlashOnlyPath.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/path/RootSlashOnlyPath.java index a83c31a4b..aa0d0688e 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/path/RootSlashOnlyPath.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/path/RootSlashOnlyPath.java @@ -60,7 +60,7 @@ public ISequence accept( DynamicContext dynamicContext, ISequence focus) { - return ObjectUtils.notNull(focus.asStream() + return ObjectUtils.notNull(focus.stream() .map(ItemUtils::checkItemIsNodeItemForStep) .map(item -> Axis.ANCESTOR_OR_SELF.execute(ObjectUtils.notNull(item)).findFirst().get()) .collect(ISequence.toSequence())); diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/path/RootSlashPath.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/path/RootSlashPath.java index df4a9a98d..4d0cec884 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/path/RootSlashPath.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/path/RootSlashPath.java @@ -59,9 +59,10 @@ public ISequence accept( DynamicContext dynamicContext, ISequence focus) { - ISequence roots = ObjectUtils.notNull(focus.asStream() + ISequence roots = ObjectUtils.notNull(focus.stream() .map(ItemUtils::checkItemIsNodeItemForStep) - .map(item -> Axis.ANCESTOR_OR_SELF.execute(ObjectUtils.notNull(item)).findFirst().get()) + // the previous checks for a null instance + .flatMap(item -> Axis.ANCESTOR_OR_SELF.execute(ObjectUtils.notNull(item)).limit(1)) .collect(ISequence.toSequence())); return getExpression().accept(dynamicContext, roots); diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/path/Wildcard.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/path/Wildcard.java index 12174dfa8..81a7a6ab6 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/path/Wildcard.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/cst/path/Wildcard.java @@ -60,7 +60,7 @@ public RESULT accept(IExpressionVisitor visit @Override public ISequence accept( DynamicContext dynamicContext, ISequence focus) { - Stream nodes = focus.asStream().map(ItemUtils::checkItemIsNodeItemForStep); + Stream nodes = focus.stream().map(ItemUtils::checkItemIsNodeItemForStep); if (matcher != null) { Predicate> test = matcher; nodes = nodes.filter(item -> { diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/AbstractFunction.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/AbstractFunction.java index d639a5ec1..b9237cdff 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/AbstractFunction.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/AbstractFunction.java @@ -28,13 +28,13 @@ import java.util.List; +import javax.xml.namespace.QName; + import edu.umd.cs.findbugs.annotations.NonNull; abstract class AbstractFunction implements IFunction { @NonNull - private final String name; - @NonNull - private final String namespace; + private final QName qname; @NonNull private final List arguments; @@ -42,19 +42,19 @@ protected AbstractFunction( @NonNull String name, @NonNull String namespace, @NonNull List arguments) { - this.name = name; - this.namespace = namespace; - this.arguments = arguments; + this(new QName(namespace, name), arguments); } - @Override - public String getName() { - return name; + protected AbstractFunction( + @NonNull QName qname, + @NonNull List arguments) { + this.qname = qname; + this.arguments = arguments; } @Override - public String getNamespace() { - return namespace; + public QName getQName() { + return qname; } @Override diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/ArithmeticFunctionException.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/ArithmeticFunctionException.java index 0e153d284..cfde0d34c 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/ArithmeticFunctionException.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/ArithmeticFunctionException.java @@ -96,7 +96,7 @@ public ArithmeticFunctionException(int code, Throwable cause) { } @Override - protected String getCodePrefix() { + public String getCodePrefix() { return "FOAR"; } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/ComparisonFunctions.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/ComparisonFunctions.java index dc2c82e91..81182def0 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/ComparisonFunctions.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/ComparisonFunctions.java @@ -117,9 +117,9 @@ public static IBooleanItem generalCompairison( // NOPMD - acceptable complexity @NonNull ISequence rightItems) { IBooleanItem retval = IBooleanItem.FALSE; - for (IAnyAtomicItem left : leftItems.asList()) { + for (IAnyAtomicItem left : leftItems.getValue()) { assert left != null; - for (IAnyAtomicItem right : rightItems.asList()) { + for (IAnyAtomicItem right : rightItems.getValue()) { assert right != null; IAnyAtomicItem leftCast; IAnyAtomicItem rightCast; diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/DateTimeFunctionException.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/DateTimeFunctionException.java index a67b5f508..38c007e78 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/DateTimeFunctionException.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/DateTimeFunctionException.java @@ -102,7 +102,7 @@ public DateTimeFunctionException(int code, Throwable cause) { } @Override - protected String getCodePrefix() { + public String getCodePrefix() { return "FODT"; } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/DefaultFunction.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/DefaultFunction.java index 2540ce4e5..27690d589 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/DefaultFunction.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/DefaultFunction.java @@ -35,6 +35,7 @@ import gov.nist.secauto.metaschema.core.metapath.item.atomic.IAnyAtomicItem; import gov.nist.secauto.metaschema.core.metapath.item.atomic.IAnyUriItem; import gov.nist.secauto.metaschema.core.metapath.item.atomic.IStringItem; +import gov.nist.secauto.metaschema.core.util.ObjectUtils; import java.util.ArrayList; import java.util.Collections; @@ -43,7 +44,7 @@ import java.util.List; import java.util.Objects; import java.util.Set; -import java.util.stream.Collectors; +import java.util.stream.Stream; import edu.umd.cs.findbugs.annotations.NonNull; import edu.umd.cs.findbugs.annotations.Nullable; @@ -206,7 +207,7 @@ public static List> convertArguments( String.format("a sequence of one expected, but found '%d'", size)); } - IItem item = FunctionUtils.getFirstItem(parameter, true); + IItem item = parameter.getFirstItem(true); parameter = item == null ? ISequence.empty() : ISequence.of(item); break; } @@ -217,7 +218,7 @@ public static List> convertArguments( String.format("a sequence of zero or one expected, but found '%d'", size)); } - IItem item = FunctionUtils.getFirstItem(parameter, false); + IItem item = parameter.getFirstItem(false); parameter = item == null ? ISequence.empty() : ISequence.of(item); break; } @@ -246,7 +247,7 @@ public static List> convertArguments( parameter = convertSequence(argument, parameter); // check resulting values - for (IItem item : parameter.asList()) { + for (IItem item : parameter.getValue()) { Class itemClass = item.getClass(); if (!argumentClass.isAssignableFrom(itemClass)) { throw new InvalidTypeMetapathException( @@ -280,35 +281,34 @@ protected static ISequence convertSequence(@NonNull IArgument argument, @NonN ISequenceType requiredSequenceType = argument.getSequenceType(); Class requiredSequenceTypeClass = requiredSequenceType.getType(); - List result = new ArrayList<>(sequence.size()); + Stream stream = sequence.safeStream(); - boolean atomize = IAnyAtomicItem.class.isAssignableFrom(requiredSequenceTypeClass); + if (IAnyAtomicItem.class.isAssignableFrom(requiredSequenceTypeClass)) { + Stream atomicStream = stream.flatMap(item -> FnData.atomize(item)); - for (IItem item : sequence.asList()) { - assert item != null; - if (atomize) { - item = FnData.fnDataItem(item); // NOPMD - intentional - - // if (IUntypedAtomicItem.class.isInstance(item)) { // NOPMD - // // TODO: apply cast to atomic type - // } + // if (IUntypedAtomicItem.class.isInstance(item)) { // NOPMD + // // TODO: apply cast to atomic type + // } + if (IStringItem.class.equals(requiredSequenceTypeClass)) { // promote URIs to strings if a string is required - if (IStringItem.class.equals(requiredSequenceTypeClass) && IAnyUriItem.class.isInstance(item)) { - item = IStringItem.cast((IAnyUriItem) item); // NOPMD - intentional - } + atomicStream = atomicStream.map(item -> IAnyUriItem.class.isInstance(item) ? IStringItem.cast(item) : item); } - // item = requiredSequenceType. + stream = atomicStream; + } + + stream = stream.peek(item -> { if (!requiredSequenceTypeClass.isInstance(item)) { throw new InvalidTypeMetapathException( item, - String.format("The type '%s' is not a subtype of '%s'", item.getClass().getName(), + String.format("The type '%s' is not a subtype of '%s'", + item.getClass().getName(), requiredSequenceTypeClass.getName())); } - result.add(item); - } - retval = ISequence.of(result); + }); + + retval = ISequence.of(stream); } return retval; } @@ -321,7 +321,7 @@ public ISequence execute( try { List> convertedArguments = convertArguments(this, arguments); - IItem contextItem = isFocusDepenent() ? FunctionUtils.requireFirstItem(focus, true) : null; + IItem contextItem = isFocusDepenent() ? ObjectUtils.requireNonNull(focus.getFirstItem(true)) : null; CallingContext callingContext = null; ISequence result = null; @@ -381,32 +381,6 @@ public String toString() { return toSignature(); } - @Override - public String toSignature() { - StringBuilder builder = new StringBuilder() - .append("Q{") - .append(getNamespace()) - .append('}') - .append(getName()) // name - .append('('); // arguments - - List arguments = getArguments(); - if (arguments.isEmpty()) { - builder.append("()"); - } else { - builder.append(arguments.stream().map(IArgument::toSignature).collect(Collectors.joining(","))); - - if (isArityUnbounded()) { - builder.append(", ..."); - } - } - - builder.append(") as ") - .append(getResult().toSignature());// return type - - return builder.toString(); - } - public final class CallingContext { @Nullable private final IItem contextItem; diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/DocumentFunctionException.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/DocumentFunctionException.java index cef0f0aa3..9f35470c0 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/DocumentFunctionException.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/DocumentFunctionException.java @@ -104,7 +104,7 @@ public DocumentFunctionException(int code, Throwable cause) { } @Override - protected String getCodePrefix() { + public String getCodePrefix() { return "FODC"; } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/FunctionService.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/FunctionService.java index 22b456138..219268974 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/FunctionService.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/FunctionService.java @@ -42,7 +42,7 @@ public final class FunctionService { @NonNull private final ServiceLoader loader; @NonNull - private IFunctionLibrary library; + private final IFunctionLibrary library; /** * Get the singleton instance of the function service. @@ -59,16 +59,6 @@ public static FunctionService getInstance() { @SuppressWarnings("null") public FunctionService() { this.loader = ServiceLoader.load(IFunctionLibrary.class); - this.library = load(); - } - - /** - * Load all known functions registered with this function service. - * - * @return the function library - */ - @NonNull - public IFunctionLibrary load() { ServiceLoader loader = getLoader(); FunctionLibrary functionLibrary = new FunctionLibrary(); @@ -76,11 +66,7 @@ public IFunctionLibrary load() { .map(Provider::get) .flatMap(IFunctionLibrary::getFunctionsAsStream) .forEachOrdered(function -> functionLibrary.registerFunction(ObjectUtils.notNull(function))); - - synchronized (this) { - this.library = functionLibrary; - } - return functionLibrary; + this.library = functionLibrary; } /** diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/FunctionUtils.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/FunctionUtils.java index 567f95257..73d7dca75 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/FunctionUtils.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/FunctionUtils.java @@ -112,74 +112,6 @@ public static long asLong(@NonNull BigInteger value) { return value.longValueExact(); } - /** - * Retrieves the first item in a sequence. If the sequence is empty, a - * {@link TypeMetapathException} exception is thrown. If requireSingleton is - * {@code true} and the sequence contains more than one item, a - * {@link TypeMetapathException} is thrown. - * - * @param - * the item type to return derived from the provided sequence - * @param sequence - * the sequence to retrieve the first item from - * @param requireSingleton - * if {@code true} then a {@link TypeMetapathException} is thrown if - * the sequence contains more than one item - * @return {@code null} if the sequence is empty, or the item otherwise - * @throws TypeMetapathException - * if the sequence is empty, or contains more than one item and - * requireSingleton is {@code true} - */ - @NonNull - public static ITEM requireFirstItem(@NonNull ISequence sequence, - boolean requireSingleton) { - if (sequence.isEmpty()) { - throw new InvalidTypeMetapathException( - null, - "Expected a non-empty sequence, but sequence was empty."); - } - List items = sequence.asList(); - if (requireSingleton && items.size() != 1) { - throw new InvalidTypeMetapathException( - null, - String.format("sequence expected to contain one item, but found '%d'", items.size())); - } - return ObjectUtils.notNull(items.iterator().next()); - } - - /** - * Retrieves the first item in a sequence. If the sequence is empty, a - * {@code null} result is returned. If requireSingleton is {@code true} and the - * sequence contains more than one item, a {@link TypeMetapathException} is - * thrown. - * - * @param - * the item type to return derived from the provided sequence - * @param sequence - * the sequence to retrieve the first item from - * @param requireSingleton - * if {@code true} then a {@link TypeMetapathException} is thrown if - * the sequence contains more than one item - * @return {@code null} if the sequence is empty, or the item otherwise - * @throws TypeMetapathException - * if the sequence contains more than one item and requireSingleton is - * {@code true} - */ - @Nullable - public static ITEM getFirstItem(@NonNull ISequence sequence, boolean requireSingleton) { - @Nullable ITEM retval = null; - if (!sequence.isEmpty()) { - List items = sequence.asList(); - if (requireSingleton && items.size() != 1) { - throw new InvalidTypeMetapathException( - null, - String.format("sequence expected to contain one item, but found '%d'", items.size())); - } - retval = items.iterator().next(); - } - return retval; - } - /** * Gets the first item of the provided sequence as a {@link INumericItem} value. * If the sequence is empty, then a {@code null} value is returned. @@ -198,7 +130,7 @@ public static ITEM getFirstItem(@NonNull ISequence se */ @Nullable public static INumericItem toNumeric(@NonNull ISequence sequence, boolean requireSingleton) { - IItem item = getFirstItem(sequence, requireSingleton); + IItem item = sequence.getFirstItem(requireSingleton); return item == null ? null : toNumeric(item); } @@ -215,7 +147,7 @@ public static INumericItem toNumeric(@NonNull ISequence sequence, boolean req @NonNull public static INumericItem toNumeric(@NonNull IItem item) { // atomize - IAnyAtomicItem atomicItem = FnData.fnDataItem(item); + IAnyAtomicItem atomicItem = ISequence.getFirstItem(FnData.atomize(item), true); return toNumeric(atomicItem); } @@ -229,7 +161,7 @@ public static INumericItem toNumeric(@NonNull IItem item) { * if the item cannot be cast to a numeric value */ @NonNull - public static INumericItem toNumeric(@NonNull IAnyAtomicItem item) { + public static INumericItem toNumeric(@Nullable IAnyAtomicItem item) { try { return IDecimalItem.cast(item); } catch (InvalidValueForCastFunctionException ex) { diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/IFunction.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/IFunction.java index ec45095dd..224fce0f5 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/IFunction.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/IFunction.java @@ -39,6 +39,7 @@ import java.util.List; import java.util.Objects; import java.util.Set; +import java.util.stream.Collectors; import javax.xml.namespace.QName; @@ -91,7 +92,9 @@ enum FunctionProperty { * @return the function's name */ @NonNull - String getName(); + default String getName() { + return ObjectUtils.notNull(getQName().getLocalPart()); + } /** * Retrieve the namespace of the function. @@ -99,7 +102,9 @@ enum FunctionProperty { * @return the function's namespace */ @NonNull - String getNamespace(); + default String getNamespace() { + return ObjectUtils.notNull(getQName().getNamespaceURI()); + } /** * Retrieve the namespace qualified name of the function. @@ -107,9 +112,7 @@ enum FunctionProperty { * @return the namespace qualified name */ @NonNull - default QName getQName() { - return new QName(getNamespace(), getName()); - } + QName getQName(); /** * Retrieve the set of assigned function properties. @@ -223,7 +226,16 @@ ISequence execute( * * @return the signature */ - String toSignature(); + @NonNull + default String toSignature() { + return ObjectUtils.notNull(String.format("Q{%s}%s(%s) as %s", + getNamespace(), + getName(), + getArguments().isEmpty() ? "()" + : getArguments().stream().map(IArgument::toSignature).collect(Collectors.joining(",")) + + (isArityUnbounded() ? ", ..." : ""), + getResult().toSignature())); + } /** * Construct a new function signature builder. diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/ISequenceType.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/ISequenceType.java index dbb26a80e..8c54d5765 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/ISequenceType.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/ISequenceType.java @@ -54,6 +54,11 @@ public String toSignature() { } }; + @NonNull + static ISequenceType of(@NonNull Class type, @NonNull Occurrence occurrence) { + return new SequenceTypeImpl(type, occurrence); + } + /** * Determine if the sequence is empty (if it holds any data) or not. * @@ -81,5 +86,6 @@ public String toSignature() { * * @return the signature */ + @NonNull String toSignature(); } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/InvalidArgumentFunctionException.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/InvalidArgumentFunctionException.java index b3504b28a..37b62a3b7 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/InvalidArgumentFunctionException.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/InvalidArgumentFunctionException.java @@ -103,7 +103,7 @@ public InvalidArgumentFunctionException(int code, Throwable cause) { } @Override - protected String getCodePrefix() { + public String getCodePrefix() { return "FORG"; } } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/InvalidTypeFunctionException.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/InvalidTypeFunctionException.java index 9fe744ca9..05b78569a 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/InvalidTypeFunctionException.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/InvalidTypeFunctionException.java @@ -95,7 +95,7 @@ private static String generateMessage(@NonNull IItem item) { } @Override - protected String getCodePrefix() { + public String getCodePrefix() { return "FOTY"; } } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/SequenceTypeImpl.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/SequenceTypeImpl.java index 2db8d2ce8..80d779f03 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/SequenceTypeImpl.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/SequenceTypeImpl.java @@ -30,11 +30,13 @@ import java.util.Objects; +import edu.umd.cs.findbugs.annotations.NonNull; + class SequenceTypeImpl implements ISequenceType { private final Class type; private final Occurrence occurrence; - public SequenceTypeImpl(Class type, Occurrence occurrence) { + public SequenceTypeImpl(@NonNull Class type, @NonNull Occurrence occurrence) { Objects.requireNonNull(type, "type"); Objects.requireNonNull(occurrence, "occurrence"); this.type = type; diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/UriFunctionException.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/UriFunctionException.java index f30461b7e..d5b3d9a64 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/UriFunctionException.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/UriFunctionException.java @@ -98,7 +98,7 @@ public UriFunctionException(int code, Throwable cause) { } @Override - protected String getCodePrefix() { + public String getCodePrefix() { return "FONS"; } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/ArrayAppend.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/ArrayAppend.java new file mode 100644 index 000000000..e1ad07b69 --- /dev/null +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/ArrayAppend.java @@ -0,0 +1,100 @@ +/* + * Portions of this software was developed by employees of the National Institute + * of Standards and Technology (NIST), an agency of the Federal Government and is + * being made available as a public service. Pursuant to title 17 United States + * Code Section 105, works of NIST employees are not subject to copyright + * protection in the United States. This software may be subject to foreign + * copyright. Permission in the United States and in foreign countries, to the + * extent that NIST may hold copyright, to use, copy, modify, create derivative + * works, and distribute this software and its documentation without fee is hereby + * granted on a non-exclusive basis, provided that this notice and disclaimer + * of warranty appears in all copies. + * + * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER + * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY + * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM + * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE + * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT + * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, + * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, + * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, + * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR + * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT + * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. + */ + +package gov.nist.secauto.metaschema.core.metapath.function.library; + +import gov.nist.secauto.metaschema.core.metapath.DynamicContext; +import gov.nist.secauto.metaschema.core.metapath.ICollectionValue; +import gov.nist.secauto.metaschema.core.metapath.ISequence; +import gov.nist.secauto.metaschema.core.metapath.MetapathConstants; +import gov.nist.secauto.metaschema.core.metapath.function.FunctionUtils; +import gov.nist.secauto.metaschema.core.metapath.function.IArgument; +import gov.nist.secauto.metaschema.core.metapath.function.IFunction; +import gov.nist.secauto.metaschema.core.metapath.item.IItem; +import gov.nist.secauto.metaschema.core.metapath.item.function.IArrayItem; +import gov.nist.secauto.metaschema.core.util.ObjectUtils; + +import java.util.ArrayList; +import java.util.List; + +import edu.umd.cs.findbugs.annotations.NonNull; + +public class ArrayAppend { + @NonNull + public static final IFunction SIGNATURE = IFunction.builder() + .name("append") + .namespace(MetapathConstants.NS_METAPATH_FUNCTIONS_ARRAY) + .argument(IArgument.builder() + .name("array") + .type(IArrayItem.class) + .one() + .build()) + .argument(IArgument.builder() + .name("appendage") + .type(IItem.class) + .zeroOrMore() + .build()) + .returnType(IArrayItem.class) + .returnOne() + .functionHandler(ArrayAppend::execute) + .build(); + + @SuppressWarnings("unused") + @NonNull + private static ISequence> execute(@NonNull IFunction function, + @NonNull List> arguments, + @NonNull DynamicContext dynamicContext, + IItem focus) { + IArrayItem array = FunctionUtils.asType(ObjectUtils.requireNonNull( + arguments.get(0).getFirstItem(true))); + @SuppressWarnings("unchecked") T appendage = (T) arguments.get(1).toArrayMember(); + + return ISequence.of(append(array, appendage)); + } + + /** + * An implementation of XPath 3.1 array:append. + * + * @param + * the type of items in the given Metapath array + * @param array + * the target Metapath array + * @param appendage + * the Metapath item to append to the identified array + * @return a new array containing the modification + */ + @NonNull + public static IArrayItem append( + @NonNull IArrayItem array, + @NonNull T appendage) { + + List copy = new ArrayList<>(array); + copy.add(appendage); + + return IArrayItem.ofCollection(copy); + } +} diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/ArrayFlatten.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/ArrayFlatten.java new file mode 100644 index 000000000..0ced0bf9c --- /dev/null +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/ArrayFlatten.java @@ -0,0 +1,95 @@ +/* + * Portions of this software was developed by employees of the National Institute + * of Standards and Technology (NIST), an agency of the Federal Government and is + * being made available as a public service. Pursuant to title 17 United States + * Code Section 105, works of NIST employees are not subject to copyright + * protection in the United States. This software may be subject to foreign + * copyright. Permission in the United States and in foreign countries, to the + * extent that NIST may hold copyright, to use, copy, modify, create derivative + * works, and distribute this software and its documentation without fee is hereby + * granted on a non-exclusive basis, provided that this notice and disclaimer + * of warranty appears in all copies. + * + * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER + * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY + * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM + * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE + * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT + * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, + * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, + * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, + * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR + * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT + * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. + */ + +package gov.nist.secauto.metaschema.core.metapath.function.library; + +import gov.nist.secauto.metaschema.core.metapath.DynamicContext; +import gov.nist.secauto.metaschema.core.metapath.ISequence; +import gov.nist.secauto.metaschema.core.metapath.MetapathConstants; +import gov.nist.secauto.metaschema.core.metapath.function.IArgument; +import gov.nist.secauto.metaschema.core.metapath.function.IFunction; +import gov.nist.secauto.metaschema.core.metapath.item.IItem; +import gov.nist.secauto.metaschema.core.metapath.item.function.IArrayItem; +import gov.nist.secauto.metaschema.core.util.ObjectUtils; + +import java.util.List; +import java.util.stream.Stream; + +import edu.umd.cs.findbugs.annotations.NonNull; + +public class ArrayFlatten { + @NonNull + static final IFunction SIGNATURE = IFunction.builder() + .name("flatten") + .namespace(MetapathConstants.NS_METAPATH_FUNCTIONS_ARRAY) + .argument(IArgument.builder() + .name("input") + .type(IItem.class) + .zeroOrMore() + .build()) + .returnType(IItem.class) + .returnZeroOrMore() + .functionHandler(ArrayFlatten::execute) + .build(); + + @SuppressWarnings("unused") + @NonNull + private static ISequence execute(@NonNull IFunction function, + @NonNull List> arguments, + @NonNull DynamicContext dynamicContext, + IItem focus) { + ISequence input = ObjectUtils.requireNonNull(arguments.get(0)); + + return ISequence.of(flatten(input)); + } + + /** + * An implementation of XPath 3.1 array:flatten. + * + * @param input + * the items to flatten + * @return the flattened items + */ + @SuppressWarnings("null") + @NonNull + public static Stream flatten(@NonNull List input) { + return input.stream() + .flatMap(ArrayFlatten::flatten); + } + + @SuppressWarnings("null") + @NonNull + public static Stream flatten(@NonNull IItem item) { + return item instanceof IArrayItem + // flatten the array members + ? ((IArrayItem) item).stream() + .flatMap(member -> member.asSequence().stream() + .flatMap(ArrayFlatten::flatten)) + // use the item + : ObjectUtils.notNull(Stream.of(item)); + } +} diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/ArrayGet.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/ArrayGet.java new file mode 100644 index 000000000..680b3e6df --- /dev/null +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/ArrayGet.java @@ -0,0 +1,109 @@ +/* + * Portions of this software was developed by employees of the National Institute + * of Standards and Technology (NIST), an agency of the Federal Government and is + * being made available as a public service. Pursuant to title 17 United States + * Code Section 105, works of NIST employees are not subject to copyright + * protection in the United States. This software may be subject to foreign + * copyright. Permission in the United States and in foreign countries, to the + * extent that NIST may hold copyright, to use, copy, modify, create derivative + * works, and distribute this software and its documentation without fee is hereby + * granted on a non-exclusive basis, provided that this notice and disclaimer + * of warranty appears in all copies. + * + * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER + * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY + * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM + * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE + * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT + * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, + * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, + * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, + * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR + * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT + * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. + */ + +package gov.nist.secauto.metaschema.core.metapath.function.library; + +import gov.nist.secauto.metaschema.core.metapath.DynamicContext; +import gov.nist.secauto.metaschema.core.metapath.ICollectionValue; +import gov.nist.secauto.metaschema.core.metapath.ISequence; +import gov.nist.secauto.metaschema.core.metapath.MetapathConstants; +import gov.nist.secauto.metaschema.core.metapath.function.FunctionUtils; +import gov.nist.secauto.metaschema.core.metapath.function.IArgument; +import gov.nist.secauto.metaschema.core.metapath.function.IFunction; +import gov.nist.secauto.metaschema.core.metapath.item.IItem; +import gov.nist.secauto.metaschema.core.metapath.item.atomic.IIntegerItem; +import gov.nist.secauto.metaschema.core.metapath.item.function.ArrayException; +import gov.nist.secauto.metaschema.core.metapath.item.function.IArrayItem; +import gov.nist.secauto.metaschema.core.util.ObjectUtils; + +import java.util.List; + +import edu.umd.cs.findbugs.annotations.NonNull; + +public class ArrayGet { + @NonNull + public static final IFunction SIGNATURE = IFunction.builder() + .name("get") + .namespace(MetapathConstants.NS_METAPATH_FUNCTIONS_ARRAY) + .argument(IArgument.builder() + .name("array") + .type(IArrayItem.class) + .one() + .build()) + .argument(IArgument.builder() + .name("position") + .type(IIntegerItem.class) + .one() + .build()) + .returnType(IItem.class) + .returnZeroOrOne() + .functionHandler(ArrayGet::execute) + .build(); + + @SuppressWarnings("unused") + @NonNull + private static ISequence execute(@NonNull IFunction function, + @NonNull List> arguments, + @NonNull DynamicContext dynamicContext, + IItem focus) { + IArrayItem array = FunctionUtils.asType(ObjectUtils.requireNonNull(arguments.get(0).getFirstItem(true))); + IIntegerItem position = FunctionUtils.asType(ObjectUtils.requireNonNull(arguments.get(1).getFirstItem(true))); + + return get(array, position).asSequence(); + } + + /** + * An implementation of XPath 3.1 array:get. + * + * @param + * the type of items in the given Metapath array + * @param target + * the array of Metapath items that is the target of retrieval + * @param positionItem + * the integer position of the item to retrieve + * @return the retrieved item + * @throws ArrayException + * if the position is not in the range of 1 to array:size + */ + @NonNull + public static T get( + @NonNull List target, + @NonNull IIntegerItem positionItem) { + int position = positionItem.asInteger().intValue(); + + try { + return ObjectUtils.requireNonNull(target.get(position - 1)); + } catch (IndexOutOfBoundsException ex) { + throw new ArrayException( + ArrayException.INDEX_OUT_OF_BOUNDS, + String.format("The index %d is outside the range of values for the array size '%d'.", + position, + target.size()), + ex); + } + } +} diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/ArrayHead.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/ArrayHead.java new file mode 100644 index 000000000..af5a4ba9a --- /dev/null +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/ArrayHead.java @@ -0,0 +1,86 @@ +/* + * Portions of this software was developed by employees of the National Institute + * of Standards and Technology (NIST), an agency of the Federal Government and is + * being made available as a public service. Pursuant to title 17 United States + * Code Section 105, works of NIST employees are not subject to copyright + * protection in the United States. This software may be subject to foreign + * copyright. Permission in the United States and in foreign countries, to the + * extent that NIST may hold copyright, to use, copy, modify, create derivative + * works, and distribute this software and its documentation without fee is hereby + * granted on a non-exclusive basis, provided that this notice and disclaimer + * of warranty appears in all copies. + * + * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER + * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY + * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM + * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE + * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT + * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, + * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, + * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, + * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR + * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT + * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. + */ + +package gov.nist.secauto.metaschema.core.metapath.function.library; + +import gov.nist.secauto.metaschema.core.metapath.DynamicContext; +import gov.nist.secauto.metaschema.core.metapath.ICollectionValue; +import gov.nist.secauto.metaschema.core.metapath.ISequence; +import gov.nist.secauto.metaschema.core.metapath.MetapathConstants; +import gov.nist.secauto.metaschema.core.metapath.function.FunctionUtils; +import gov.nist.secauto.metaschema.core.metapath.function.IArgument; +import gov.nist.secauto.metaschema.core.metapath.function.IFunction; +import gov.nist.secauto.metaschema.core.metapath.item.IItem; +import gov.nist.secauto.metaschema.core.metapath.item.function.IArrayItem; +import gov.nist.secauto.metaschema.core.util.ObjectUtils; + +import java.util.List; + +import edu.umd.cs.findbugs.annotations.NonNull; +import edu.umd.cs.findbugs.annotations.Nullable; + +public class ArrayHead { + @NonNull + static final IFunction SIGNATURE = IFunction.builder() + .name("head") + .namespace(MetapathConstants.NS_METAPATH_FUNCTIONS_ARRAY) + .argument(IArgument.builder() + .name("array") + .type(IArrayItem.class) + .one() + .build()) + .returnType(IItem.class) + .returnZeroOrOne() + .functionHandler(ArrayHead::execute) + .build(); + + @SuppressWarnings("unused") + @NonNull + private static ISequence execute(@NonNull IFunction function, + @NonNull List> arguments, + @NonNull DynamicContext dynamicContext, + IItem focus) { + IArrayItem array = FunctionUtils.asType(ObjectUtils.requireNonNull(arguments.get(0).getFirstItem(true))); + + ICollectionValue result = head(array); + return result == null ? ISequence.empty() : result.asSequence(); + } + + /** + * An implementation of XPath 3.1 array:head. + * + * @param + * the type of items in the given Metapath array + * @param array + * the array to get the head item from + * @return the head item + */ + @Nullable + public static T head(@NonNull IArrayItem array) { + return array.isEmpty() ? null : array.get(0); + } +} diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/ArrayInsertBefore.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/ArrayInsertBefore.java new file mode 100644 index 000000000..a2f3cea92 --- /dev/null +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/ArrayInsertBefore.java @@ -0,0 +1,117 @@ +/* + * Portions of this software was developed by employees of the National Institute + * of Standards and Technology (NIST), an agency of the Federal Government and is + * being made available as a public service. Pursuant to title 17 United States + * Code Section 105, works of NIST employees are not subject to copyright + * protection in the United States. This software may be subject to foreign + * copyright. Permission in the United States and in foreign countries, to the + * extent that NIST may hold copyright, to use, copy, modify, create derivative + * works, and distribute this software and its documentation without fee is hereby + * granted on a non-exclusive basis, provided that this notice and disclaimer + * of warranty appears in all copies. + * + * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER + * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY + * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM + * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE + * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT + * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, + * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, + * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, + * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR + * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT + * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. + */ + +package gov.nist.secauto.metaschema.core.metapath.function.library; + +import gov.nist.secauto.metaschema.core.metapath.DynamicContext; +import gov.nist.secauto.metaschema.core.metapath.ICollectionValue; +import gov.nist.secauto.metaschema.core.metapath.ISequence; +import gov.nist.secauto.metaschema.core.metapath.MetapathConstants; +import gov.nist.secauto.metaschema.core.metapath.function.FunctionUtils; +import gov.nist.secauto.metaschema.core.metapath.function.IArgument; +import gov.nist.secauto.metaschema.core.metapath.function.IFunction; +import gov.nist.secauto.metaschema.core.metapath.item.IItem; +import gov.nist.secauto.metaschema.core.metapath.item.atomic.IIntegerItem; +import gov.nist.secauto.metaschema.core.metapath.item.function.ArrayException; +import gov.nist.secauto.metaschema.core.metapath.item.function.IArrayItem; +import gov.nist.secauto.metaschema.core.util.ObjectUtils; + +import java.util.List; + +import edu.umd.cs.findbugs.annotations.NonNull; + +public class ArrayInsertBefore { + @NonNull + public static final IFunction SIGNATURE = IFunction.builder() + .name("insert-before") + .namespace(MetapathConstants.NS_METAPATH_FUNCTIONS_ARRAY) + .argument(IArgument.builder() + .name("array") + .type(IArrayItem.class) + .one() + .build()) + .argument(IArgument.builder() + .name("position") + .type(IIntegerItem.class) + .one() + .build()) + .argument(IArgument.builder() + .name("member") + .type(IItem.class) + .zeroOrMore() + .build()) + .returnType(IArrayItem.class) + .returnOne() + .functionHandler(ArrayInsertBefore::execute) + .build(); + + @SuppressWarnings("unused") + @NonNull + private static ISequence> execute(@NonNull IFunction function, + @NonNull List> arguments, + @NonNull DynamicContext dynamicContext, + IItem focus) { + IArrayItem array = FunctionUtils.asType(ObjectUtils.requireNonNull( + arguments.get(0).getFirstItem(true))); + IIntegerItem position = FunctionUtils.asType(ObjectUtils.requireNonNull(arguments.get(1).getFirstItem(true))); + @SuppressWarnings("unchecked") T member = (T) ObjectUtils.requireNonNull(arguments.get(2)).toArrayMember(); + + return ISequence.of(insertBefore(array, position, member)); + } + + /** + * An implementation of XPath 3.1 array:insert-before. + * + * @param + * the type of items in the given Metapath array + * @param array + * the target Metapath array + * @param positionItem + * the integer position of the item to insert before + * @param member + * the Metapath item to insert into the identified array + * @return a new array containing the modification + * @throws ArrayException + * if the position is not in the range of 1 to array:size + */ + @NonNull + public static IArrayItem insertBefore( + @NonNull IArrayItem array, + @NonNull IIntegerItem positionItem, + @NonNull T member) { + return insertBefore(array, positionItem.asInteger().intValueExact(), member); + } + + @NonNull + public static IArrayItem insertBefore(@NonNull IArrayItem array, int position, + @NonNull T member) { + return ArrayJoin.join(ObjectUtils.notNull(List.of( + ArraySubarray.subarray(array, 1, position - 1), + IArrayItem.of(member), + ArraySubarray.subarray(array, position)))); + } +} diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/ArrayJoin.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/ArrayJoin.java new file mode 100644 index 000000000..ffe8ced15 --- /dev/null +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/ArrayJoin.java @@ -0,0 +1,89 @@ +/* + * Portions of this software was developed by employees of the National Institute + * of Standards and Technology (NIST), an agency of the Federal Government and is + * being made available as a public service. Pursuant to title 17 United States + * Code Section 105, works of NIST employees are not subject to copyright + * protection in the United States. This software may be subject to foreign + * copyright. Permission in the United States and in foreign countries, to the + * extent that NIST may hold copyright, to use, copy, modify, create derivative + * works, and distribute this software and its documentation without fee is hereby + * granted on a non-exclusive basis, provided that this notice and disclaimer + * of warranty appears in all copies. + * + * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER + * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY + * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM + * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE + * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT + * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, + * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, + * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, + * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR + * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT + * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. + */ + +package gov.nist.secauto.metaschema.core.metapath.function.library; + +import gov.nist.secauto.metaschema.core.metapath.DynamicContext; +import gov.nist.secauto.metaschema.core.metapath.ICollectionValue; +import gov.nist.secauto.metaschema.core.metapath.ISequence; +import gov.nist.secauto.metaschema.core.metapath.MetapathConstants; +import gov.nist.secauto.metaschema.core.metapath.function.FunctionUtils; +import gov.nist.secauto.metaschema.core.metapath.function.IArgument; +import gov.nist.secauto.metaschema.core.metapath.function.IFunction; +import gov.nist.secauto.metaschema.core.metapath.item.IItem; +import gov.nist.secauto.metaschema.core.metapath.item.function.IArrayItem; +import gov.nist.secauto.metaschema.core.util.ObjectUtils; + +import java.util.Collection; +import java.util.List; +import java.util.stream.Collectors; + +import edu.umd.cs.findbugs.annotations.NonNull; + +public class ArrayJoin { + @NonNull + public static final IFunction SIGNATURE = IFunction.builder() + .name("join") + .namespace(MetapathConstants.NS_METAPATH_FUNCTIONS_ARRAY) + .argument(IArgument.builder() + .name("array") + .type(IArrayItem.class) + .zeroOrMore() + .build()) + .returnType(IItem.class) + .returnZeroOrOne() + .functionHandler(ArrayJoin::execute) + .build(); + + @SuppressWarnings("unused") + @NonNull + private static ISequence> execute(@NonNull IFunction function, + @NonNull List> arguments, + @NonNull DynamicContext dynamicContext, + IItem focus) { + ISequence> arrays = FunctionUtils.asType(ObjectUtils.requireNonNull(arguments.get(0))); + + return join(arrays).asSequence(); + } + + /** + * An implementation of XPath 3.1 array:join. + * + * @param + * the type of items in the given Metapath array + * @param arrays + * the arrays to join + * @return a new combined array + */ + @NonNull + public static IArrayItem join( + @NonNull Collection> arrays) { + return IArrayItem.ofCollection(ObjectUtils.notNull(arrays.stream() + .flatMap(array -> array.stream()) + .collect(Collectors.toList()))); + } +} diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/ArrayPut.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/ArrayPut.java new file mode 100644 index 000000000..cea736e1e --- /dev/null +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/ArrayPut.java @@ -0,0 +1,127 @@ +/* + * Portions of this software was developed by employees of the National Institute + * of Standards and Technology (NIST), an agency of the Federal Government and is + * being made available as a public service. Pursuant to title 17 United States + * Code Section 105, works of NIST employees are not subject to copyright + * protection in the United States. This software may be subject to foreign + * copyright. Permission in the United States and in foreign countries, to the + * extent that NIST may hold copyright, to use, copy, modify, create derivative + * works, and distribute this software and its documentation without fee is hereby + * granted on a non-exclusive basis, provided that this notice and disclaimer + * of warranty appears in all copies. + * + * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER + * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY + * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM + * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE + * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT + * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, + * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, + * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, + * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR + * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT + * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. + */ + +package gov.nist.secauto.metaschema.core.metapath.function.library; + +import gov.nist.secauto.metaschema.core.metapath.DynamicContext; +import gov.nist.secauto.metaschema.core.metapath.ICollectionValue; +import gov.nist.secauto.metaschema.core.metapath.ISequence; +import gov.nist.secauto.metaschema.core.metapath.MetapathConstants; +import gov.nist.secauto.metaschema.core.metapath.function.FunctionUtils; +import gov.nist.secauto.metaschema.core.metapath.function.IArgument; +import gov.nist.secauto.metaschema.core.metapath.function.IFunction; +import gov.nist.secauto.metaschema.core.metapath.item.IItem; +import gov.nist.secauto.metaschema.core.metapath.item.atomic.IIntegerItem; +import gov.nist.secauto.metaschema.core.metapath.item.function.ArrayException; +import gov.nist.secauto.metaschema.core.metapath.item.function.IArrayItem; +import gov.nist.secauto.metaschema.core.util.ObjectUtils; + +import java.util.ArrayList; +import java.util.List; + +import edu.umd.cs.findbugs.annotations.NonNull; + +public class ArrayPut { + @NonNull + public static final IFunction SIGNATURE = IFunction.builder() + .name("put") + .namespace(MetapathConstants.NS_METAPATH_FUNCTIONS_ARRAY) + .argument(IArgument.builder() + .name("array") + .type(IArrayItem.class) + .one() + .build()) + .argument(IArgument.builder() + .name("position") + .type(IIntegerItem.class) + .one() + .build()) + .argument(IArgument.builder() + .name("member") + .type(IItem.class) + .zeroOrMore() + .build()) + .returnType(IArrayItem.class) + .returnOne() + .functionHandler(ArrayPut::execute) + .build(); + + @SuppressWarnings("unused") + @NonNull + private static ISequence> execute(@NonNull IFunction function, + @NonNull List> arguments, + @NonNull DynamicContext dynamicContext, + IItem focus) { + IArrayItem array = FunctionUtils.asType(ObjectUtils.requireNonNull( + arguments.get(0).getFirstItem(true))); + IIntegerItem position = FunctionUtils.asType(ObjectUtils.requireNonNull(arguments.get(1).getFirstItem(true))); + @SuppressWarnings("unchecked") T member = (T) arguments.get(2).toArrayMember(); + + return put(array, position, member).asSequence(); + } + + /** + * An implementation of XPath 3.1 array:put. + * + * @param + * the type of items in the given Metapath array + * @param array + * the target Metapath array + * @param positionItem + * the integer position of the item to replace + * @param member + * the Metapath item to replace the identified array member with + * @return a new array containing the modification + * @throws ArrayException + * if the position is not in the range of 1 to array:size + */ + @NonNull + public static IArrayItem put( + @NonNull IArrayItem array, + @NonNull IIntegerItem positionItem, + @NonNull T member) { + return put(array, positionItem.asInteger().intValueExact(), member); + } + + @NonNull + public static IArrayItem put(@NonNull IArrayItem array, int position, + @NonNull T member) { + List copy = new ArrayList<>(array); + try { + copy.set(position - 1, member); + } catch (IndexOutOfBoundsException ex) { + throw new ArrayException( + ArrayException.INDEX_OUT_OF_BOUNDS, + String.format("The position %d is outside the range of values for the array of size '%d'.", + position, + copy.size()), + ex); + } + + return IArrayItem.ofCollection(copy); + } +} diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/ArrayRemove.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/ArrayRemove.java new file mode 100644 index 000000000..6cb62fcce --- /dev/null +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/ArrayRemove.java @@ -0,0 +1,121 @@ +/* + * Portions of this software was developed by employees of the National Institute + * of Standards and Technology (NIST), an agency of the Federal Government and is + * being made available as a public service. Pursuant to title 17 United States + * Code Section 105, works of NIST employees are not subject to copyright + * protection in the United States. This software may be subject to foreign + * copyright. Permission in the United States and in foreign countries, to the + * extent that NIST may hold copyright, to use, copy, modify, create derivative + * works, and distribute this software and its documentation without fee is hereby + * granted on a non-exclusive basis, provided that this notice and disclaimer + * of warranty appears in all copies. + * + * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER + * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY + * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM + * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE + * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT + * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, + * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, + * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, + * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR + * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT + * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. + */ + +package gov.nist.secauto.metaschema.core.metapath.function.library; + +import gov.nist.secauto.metaschema.core.metapath.DynamicContext; +import gov.nist.secauto.metaschema.core.metapath.ISequence; +import gov.nist.secauto.metaschema.core.metapath.MetapathConstants; +import gov.nist.secauto.metaschema.core.metapath.function.FunctionUtils; +import gov.nist.secauto.metaschema.core.metapath.function.IArgument; +import gov.nist.secauto.metaschema.core.metapath.function.IFunction; +import gov.nist.secauto.metaschema.core.metapath.item.IItem; +import gov.nist.secauto.metaschema.core.metapath.item.atomic.IIntegerItem; +import gov.nist.secauto.metaschema.core.metapath.item.function.ArrayException; +import gov.nist.secauto.metaschema.core.metapath.item.function.IArrayItem; +import gov.nist.secauto.metaschema.core.util.ObjectUtils; + +import java.util.Collection; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; +import java.util.stream.IntStream; + +import edu.umd.cs.findbugs.annotations.NonNull; + +public class ArrayRemove { + @NonNull + public static final IFunction SIGNATURE = IFunction.builder() + .name("remove") + .namespace(MetapathConstants.NS_METAPATH_FUNCTIONS_ARRAY) + .argument(IArgument.builder() + .name("array") + .type(IArrayItem.class) + .one() + .build()) + .argument(IArgument.builder() + .name("positions") + .type(IIntegerItem.class) + .zeroOrMore() + .build()) + .returnType(IArrayItem.class) + .returnOne() + .functionHandler(ArrayRemove::execute) + .build(); + + @SuppressWarnings("unused") + @NonNull + private static ISequence> execute(@NonNull IFunction function, + @NonNull List> arguments, + @NonNull DynamicContext dynamicContext, + IItem focus) { + IArrayItem array = FunctionUtils.asType(ObjectUtils.requireNonNull( + arguments.get(0).getFirstItem(true))); + ISequence positions = FunctionUtils.asType(ObjectUtils.requireNonNull(arguments.get(1))); + + return ISequence.of(removeItems(array, positions)); + } + + /** + * An implementation of XPath 3.1 array:remove. + * + * @param + * the type of items in the given Metapath array + * @param array + * the target Metapath array + * @param positions + * the integer position of the items to remove + * @return a new array containing the modification + * @throws ArrayException + * if the position is not in the range of 1 to array:size + */ + @NonNull + public static IArrayItem removeItems( + @NonNull IArrayItem array, + @NonNull Collection positions) { + return remove( + array, + ObjectUtils.notNull(positions.stream() + .map(position -> position.asInteger().intValueExact()) + .collect(Collectors.toSet()))); + } + + @NonNull + public static IArrayItem remove( + @NonNull IArrayItem array, + @NonNull Collection positions) { + Set positionSet = positions instanceof Set ? (Set) positions : new HashSet(positions); + + List removed = ObjectUtils.notNull(IntStream.range(1, array.size() + 1) + .filter(index -> !positionSet.contains(index)) + .mapToObj(index -> array.get(index - 1)) + .collect(Collectors.toList())); + + return IArrayItem.ofCollection(removed); + } +} diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/ArrayReverse.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/ArrayReverse.java new file mode 100644 index 000000000..b83e4a6a0 --- /dev/null +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/ArrayReverse.java @@ -0,0 +1,90 @@ +/* + * Portions of this software was developed by employees of the National Institute + * of Standards and Technology (NIST), an agency of the Federal Government and is + * being made available as a public service. Pursuant to title 17 United States + * Code Section 105, works of NIST employees are not subject to copyright + * protection in the United States. This software may be subject to foreign + * copyright. Permission in the United States and in foreign countries, to the + * extent that NIST may hold copyright, to use, copy, modify, create derivative + * works, and distribute this software and its documentation without fee is hereby + * granted on a non-exclusive basis, provided that this notice and disclaimer + * of warranty appears in all copies. + * + * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER + * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY + * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM + * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE + * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT + * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, + * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, + * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, + * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR + * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT + * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. + */ + +package gov.nist.secauto.metaschema.core.metapath.function.library; + +import gov.nist.secauto.metaschema.core.metapath.DynamicContext; +import gov.nist.secauto.metaschema.core.metapath.ICollectionValue; +import gov.nist.secauto.metaschema.core.metapath.ISequence; +import gov.nist.secauto.metaschema.core.metapath.MetapathConstants; +import gov.nist.secauto.metaschema.core.metapath.function.FunctionUtils; +import gov.nist.secauto.metaschema.core.metapath.function.IArgument; +import gov.nist.secauto.metaschema.core.metapath.function.IFunction; +import gov.nist.secauto.metaschema.core.metapath.item.IItem; +import gov.nist.secauto.metaschema.core.metapath.item.function.IArrayItem; +import gov.nist.secauto.metaschema.core.util.ObjectUtils; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +import edu.umd.cs.findbugs.annotations.NonNull; + +public class ArrayReverse { + @NonNull + public static final IFunction SIGNATURE = IFunction.builder() + .name("reverse") + .namespace(MetapathConstants.NS_METAPATH_FUNCTIONS_ARRAY) + .argument(IArgument.builder() + .name("array") + .type(IArrayItem.class) + .one() + .build()) + .returnType(IArrayItem.class) + .returnOne() + .functionHandler(ArrayReverse::execute) + .build(); + + @SuppressWarnings("unused") + @NonNull + private static ISequence> execute(@NonNull IFunction function, + @NonNull List> arguments, + @NonNull DynamicContext dynamicContext, + IItem focus) { + IArrayItem array = FunctionUtils.asType(ObjectUtils.requireNonNull( + arguments.get(0).getFirstItem(true))); + + return ISequence.of(reverse(array)); + } + + /** + * An implementation of XPath 3.1 array:reverse. + * + * @param + * the type of items in the given Metapath array + * @param array + * the target Metapath array + * @return a new array containing the modification + */ + @NonNull + public static IArrayItem reverse( + @NonNull IArrayItem array) { + List copy = new ArrayList<>(array); + Collections.reverse(copy); + return IArrayItem.ofCollection(copy); + } +} diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/ArraySize.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/ArraySize.java new file mode 100644 index 000000000..cad553484 --- /dev/null +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/ArraySize.java @@ -0,0 +1,77 @@ +/* + * Portions of this software was developed by employees of the National Institute + * of Standards and Technology (NIST), an agency of the Federal Government and is + * being made available as a public service. Pursuant to title 17 United States + * Code Section 105, works of NIST employees are not subject to copyright + * protection in the United States. This software may be subject to foreign + * copyright. Permission in the United States and in foreign countries, to the + * extent that NIST may hold copyright, to use, copy, modify, create derivative + * works, and distribute this software and its documentation without fee is hereby + * granted on a non-exclusive basis, provided that this notice and disclaimer + * of warranty appears in all copies. + * + * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER + * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY + * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM + * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE + * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT + * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, + * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, + * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, + * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR + * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT + * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. + */ + +package gov.nist.secauto.metaschema.core.metapath.function.library; + +import gov.nist.secauto.metaschema.core.metapath.DynamicContext; +import gov.nist.secauto.metaschema.core.metapath.ISequence; +import gov.nist.secauto.metaschema.core.metapath.MetapathConstants; +import gov.nist.secauto.metaschema.core.metapath.function.FunctionUtils; +import gov.nist.secauto.metaschema.core.metapath.function.IArgument; +import gov.nist.secauto.metaschema.core.metapath.function.IFunction; +import gov.nist.secauto.metaschema.core.metapath.item.IItem; +import gov.nist.secauto.metaschema.core.metapath.item.atomic.IIntegerItem; +import gov.nist.secauto.metaschema.core.metapath.item.function.IArrayItem; +import gov.nist.secauto.metaschema.core.util.ObjectUtils; + +import java.util.List; + +import edu.umd.cs.findbugs.annotations.NonNull; + +public class ArraySize { + @NonNull + static final IFunction SIGNATURE = IFunction.builder() + .name("size") + .namespace(MetapathConstants.NS_METAPATH_FUNCTIONS_ARRAY) + .argument(IArgument.builder() + .name("array") + .type(IArrayItem.class) + .one() + .build()) + .returnType(IIntegerItem.class) + .returnOne() + .functionHandler(ArraySize::execute) + .build(); + + /** + * An implementation of XPath 3.1 array:size. + * + * @param array + * the arrays to join + * @return a new combined array + */ + @SuppressWarnings("unused") + @NonNull + private static ISequence execute(@NonNull IFunction function, + @NonNull List> arguments, + @NonNull DynamicContext dynamicContext, + IItem focus) { + IArrayItem array = FunctionUtils.asType(ObjectUtils.requireNonNull(arguments.get(0).getFirstItem(true))); + + return ISequence.of(IIntegerItem.valueOf(array.size())); + } +} diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/ArraySubarray.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/ArraySubarray.java new file mode 100644 index 000000000..e8b3326e3 --- /dev/null +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/ArraySubarray.java @@ -0,0 +1,194 @@ +/* + * Portions of this software was developed by employees of the National Institute + * of Standards and Technology (NIST), an agency of the Federal Government and is + * being made available as a public service. Pursuant to title 17 United States + * Code Section 105, works of NIST employees are not subject to copyright + * protection in the United States. This software may be subject to foreign + * copyright. Permission in the United States and in foreign countries, to the + * extent that NIST may hold copyright, to use, copy, modify, create derivative + * works, and distribute this software and its documentation without fee is hereby + * granted on a non-exclusive basis, provided that this notice and disclaimer + * of warranty appears in all copies. + * + * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER + * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY + * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM + * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE + * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT + * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, + * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, + * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, + * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR + * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT + * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. + */ + +package gov.nist.secauto.metaschema.core.metapath.function.library; + +import gov.nist.secauto.metaschema.core.metapath.DynamicContext; +import gov.nist.secauto.metaschema.core.metapath.ICollectionValue; +import gov.nist.secauto.metaschema.core.metapath.ISequence; +import gov.nist.secauto.metaschema.core.metapath.MetapathConstants; +import gov.nist.secauto.metaschema.core.metapath.function.FunctionUtils; +import gov.nist.secauto.metaschema.core.metapath.function.IArgument; +import gov.nist.secauto.metaschema.core.metapath.function.IFunction; +import gov.nist.secauto.metaschema.core.metapath.item.IItem; +import gov.nist.secauto.metaschema.core.metapath.item.atomic.IIntegerItem; +import gov.nist.secauto.metaschema.core.metapath.item.function.ArrayException; +import gov.nist.secauto.metaschema.core.metapath.item.function.IArrayItem; +import gov.nist.secauto.metaschema.core.util.ObjectUtils; + +import java.util.ArrayList; +import java.util.List; + +import edu.umd.cs.findbugs.annotations.NonNull; + +public class ArraySubarray { + @NonNull + public static final IFunction SIGNATURE_TWO_ARG = IFunction.builder() + .name("subarray") + .namespace(MetapathConstants.NS_METAPATH_FUNCTIONS_ARRAY) + .argument(IArgument.builder() + .name("array") + .type(IArrayItem.class) + .one() + .build()) + .argument(IArgument.builder() + .name("start") + .type(IIntegerItem.class) + .one() + .build()) + .returnType(IArrayItem.class) + .returnOne() + .functionHandler(ArraySubarray::executeTwoArg) + .build(); + @NonNull + public static final IFunction SIGNATURE_THREE_ARG = IFunction.builder() + .name("subarray") + .namespace(MetapathConstants.NS_METAPATH_FUNCTIONS_ARRAY) + .argument(IArgument.builder() + .name("array") + .type(IArrayItem.class) + .one() + .build()) + .argument(IArgument.builder() + .name("start") + .type(IIntegerItem.class) + .one() + .build()) + .argument(IArgument.builder() + .name("length") + .type(IIntegerItem.class) + .one() + .build()) + .returnType(IArrayItem.class) + .returnOne() + .functionHandler(ArraySubarray::executeThreeArg) + .build(); + + @SuppressWarnings("unused") + @NonNull + private static ISequence> executeTwoArg(@NonNull IFunction function, + @NonNull List> arguments, + @NonNull DynamicContext dynamicContext, + IItem focus) { + IArrayItem array = FunctionUtils.asType(ObjectUtils.requireNonNull( + arguments.get(0).getFirstItem(true))); + IIntegerItem start = FunctionUtils.asType(ObjectUtils.requireNonNull(arguments.get(1).getFirstItem(true))); + + return ISequence.of(subarray(array, start)); + } + + @SuppressWarnings("unused") + @NonNull + private static ISequence> executeThreeArg(@NonNull IFunction function, + @NonNull List> arguments, + @NonNull DynamicContext dynamicContext, + IItem focus) { + IArrayItem array = FunctionUtils.asType(ObjectUtils.requireNonNull( + arguments.get(0).getFirstItem(true))); + IIntegerItem start = FunctionUtils.asType(ObjectUtils.requireNonNull(arguments.get(1).getFirstItem(true))); + IIntegerItem length = FunctionUtils.asType(ObjectUtils.requireNonNull(arguments.get(2).getFirstItem(true))); + + return ISequence.of(subarray(array, start, length)); + } + + /** + * An implementation of XPath 3.1 array:subarray. + * + * @param + * the type of items in the given Metapath array + * @param array + * the target Metapath array + * @param startItem + * the integer position of the item to start with (inclusive) + * @return a new array consisting of the items in the identified range + * @throws ArrayException + * if the position is not in the range of 1 to array:size + */ + @SuppressWarnings("PMD.OnlyOneReturn") + @NonNull + public static IArrayItem subarray( + @NonNull IArrayItem array, + @NonNull IIntegerItem startItem) { + return subarray(array, startItem.asInteger().intValueExact()); + } + + /** + * An implementation of XPath 3.1 array:subarray. + * + * @param + * the type of items in the given Metapath array + * @param array + * the target Metapath array + * @param startItem + * the integer position of the item to start with (inclusive) + * @param lengthItem + * the integer count of items to include starting with the item at the + * start position + * @return a new array consisting of the items in the identified range + * @throws ArrayException + * if the length is negative or the position is not in the range of 1 + * to array:size + */ + @SuppressWarnings("PMD.OnlyOneReturn") + @NonNull + public static IArrayItem subarray( + @NonNull IArrayItem array, + @NonNull IIntegerItem startItem, + @NonNull IIntegerItem lengthItem) { + return subarray(array, startItem.asInteger().intValueExact(), lengthItem.asInteger().intValueExact()); + } + + @NonNull + public static IArrayItem subarray(@NonNull IArrayItem array, int start) { + return subarray(array, start, array.size() - start + 1); + } + + @NonNull + public static IArrayItem subarray(@NonNull IArrayItem array, int start, + int length) { + if (length < 0) { + throw new ArrayException( + ArrayException.NEGATIVE_ARRAY_LENGTH, String.format("The length '%d' is negative.", length)); + } + + List copy; + try { + copy = array.subList(start - 1, start - 1 + length); + } catch (IndexOutOfBoundsException ex) { + throw new ArrayException( + ArrayException.INDEX_OUT_OF_BOUNDS, + String.format("The start + length (%d + %d) exceeds the array length '%d'.", + start, + length, + array.size()), + ex); + } + + return IArrayItem.ofCollection(new ArrayList<>(copy)); + } +} diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/ArrayTail.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/ArrayTail.java new file mode 100644 index 000000000..6f5d1fe53 --- /dev/null +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/ArrayTail.java @@ -0,0 +1,84 @@ +/* + * Portions of this software was developed by employees of the National Institute + * of Standards and Technology (NIST), an agency of the Federal Government and is + * being made available as a public service. Pursuant to title 17 United States + * Code Section 105, works of NIST employees are not subject to copyright + * protection in the United States. This software may be subject to foreign + * copyright. Permission in the United States and in foreign countries, to the + * extent that NIST may hold copyright, to use, copy, modify, create derivative + * works, and distribute this software and its documentation without fee is hereby + * granted on a non-exclusive basis, provided that this notice and disclaimer + * of warranty appears in all copies. + * + * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER + * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY + * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM + * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE + * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT + * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, + * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, + * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, + * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR + * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT + * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. + */ + +package gov.nist.secauto.metaschema.core.metapath.function.library; + +import gov.nist.secauto.metaschema.core.metapath.DynamicContext; +import gov.nist.secauto.metaschema.core.metapath.ISequence; +import gov.nist.secauto.metaschema.core.metapath.MetapathConstants; +import gov.nist.secauto.metaschema.core.metapath.function.FunctionUtils; +import gov.nist.secauto.metaschema.core.metapath.function.IArgument; +import gov.nist.secauto.metaschema.core.metapath.function.IFunction; +import gov.nist.secauto.metaschema.core.metapath.item.IItem; +import gov.nist.secauto.metaschema.core.metapath.item.function.IArrayItem; +import gov.nist.secauto.metaschema.core.util.ObjectUtils; + +import java.util.List; + +import edu.umd.cs.findbugs.annotations.NonNull; +import edu.umd.cs.findbugs.annotations.Nullable; + +public class ArrayTail { + @NonNull + static final IFunction SIGNATURE = IFunction.builder() + .name("tail") + .namespace(MetapathConstants.NS_METAPATH_FUNCTIONS_ARRAY) + .argument(IArgument.builder() + .name("array") + .type(IArrayItem.class) + .one() + .build()) + .returnType(IItem.class) + .returnZeroOrOne() + .functionHandler(ArrayTail::execute) + .build(); + + @SuppressWarnings("unused") + @NonNull + private static ISequence> execute(@NonNull IFunction function, + @NonNull List> arguments, + @NonNull DynamicContext dynamicContext, + IItem focus) { + IArrayItem array = FunctionUtils.asType(ObjectUtils.requireNonNull(arguments.get(0).getFirstItem(true))); + + return ISequence.of(tail(array)); + } + + /** + * An implementation of XPath 3.1 array:tail. + * + * @param + * the type of items in the given Metapath array + * @param array + * the array to get the tail items from + * @return the tail items + */ + @Nullable + public static IArrayItem tail(@NonNull IArrayItem array) { + return ArraySubarray.subarray(array, 2); + } +} diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/CastFunction.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/CastFunction.java index f6c45a1c8..6af3c8230 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/CastFunction.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/CastFunction.java @@ -105,7 +105,7 @@ public ISequence execute(@NonNull IFunction function, ISequence arg = FunctionUtils.asType( ObjectUtils.notNull(arguments.get(0))); - IAnyAtomicItem item = FunctionUtils.getFirstItem(arg, true); + IAnyAtomicItem item = arg.getFirstItem(true); if (item == null) { return ISequence.empty(); // NOPMD - readability } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/DefaultFunctionLibrary.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/DefaultFunctionLibrary.java index 271813910..13ea87d4e 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/DefaultFunctionLibrary.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/DefaultFunctionLibrary.java @@ -193,6 +193,38 @@ public DefaultFunctionLibrary() { // NOPMD - intentional // https://www.w3.org/TR/xpath-functions-31/#func-years-from-duration // P2: https://www.w3.org/TR/xpath-functions-31/#func-zero-or-one + // https://www.w3.org/TR/xpath-functions-31/#func-array-get + registerFunction(ArrayGet.SIGNATURE); + // https://www.w3.org/TR/xpath-functions-31/#func-array-size + registerFunction(ArraySize.SIGNATURE); + // https://www.w3.org/TR/xpath-functions-31/#func-array-put + registerFunction(ArrayPut.SIGNATURE); + // https://www.w3.org/TR/xpath-functions-31/#func-array-append + registerFunction(ArrayAppend.SIGNATURE); + // https://www.w3.org/TR/xpath-functions-31/#func-array-subarray + registerFunction(ArraySubarray.SIGNATURE_TWO_ARG); + registerFunction(ArraySubarray.SIGNATURE_THREE_ARG); + // https://www.w3.org/TR/xpath-functions-31/#func-array-remove + registerFunction(ArrayRemove.SIGNATURE); + // https://www.w3.org/TR/xpath-functions-31/#func-array-insert-before + registerFunction(ArrayInsertBefore.SIGNATURE); + // https://www.w3.org/TR/xpath-functions-31/#func-array-head + registerFunction(ArrayHead.SIGNATURE); + // https://www.w3.org/TR/xpath-functions-31/#func-array-tail + registerFunction(ArrayTail.SIGNATURE); + // https://www.w3.org/TR/xpath-functions-31/#func-array-reverse + registerFunction(ArrayReverse.SIGNATURE); + // https://www.w3.org/TR/xpath-functions-31/#func-array-join + registerFunction(ArrayJoin.SIGNATURE); + // P3: https://www.w3.org/TR/xpath-functions-31/#func-array-for-each + // P3: https://www.w3.org/TR/xpath-functions-31/#func-array-filter + // P3: https://www.w3.org/TR/xpath-functions-31/#func-array-fold-left + // P3: https://www.w3.org/TR/xpath-functions-31/#func-array-fold-right + // P3: https://www.w3.org/TR/xpath-functions-31/#func-array-for-each-pair + // P3: https://www.w3.org/TR/xpath-functions-31/#func-array-sort + // https://www.w3.org/TR/xpath-functions-31/#func-array-flatten + registerFunction(ArrayFlatten.SIGNATURE); + // xpath casting functions registerFunction( CastFunction.signature(MetapathConstants.NS_XML_SCHEMA, "boolean", IBooleanItem.class, IBooleanItem::cast)); diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/FnAbs.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/FnAbs.java index 0d0a999f8..98c99d588 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/FnAbs.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/FnAbs.java @@ -76,10 +76,9 @@ private static ISequence execute( @NonNull List> arguments, @NonNull DynamicContext dynamicContext, IItem focus) { - ISequence sequence = FunctionUtils.asType( - ObjectUtils.requireNonNull(arguments.get(0))); + ISequence sequence = FunctionUtils.asType(ObjectUtils.requireNonNull(arguments.get(0))); - INumericItem item = FunctionUtils.getFirstItem(sequence, true); + INumericItem item = sequence.getFirstItem(true); if (item == null) { return ISequence.empty(); // NOPMD - readability } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/FnAvg.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/FnAvg.java index 07fb5b0a0..a392a53de 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/FnAvg.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/FnAvg.java @@ -93,7 +93,7 @@ private static ISequence execute( ISequence sequence = FunctionUtils.asType( ObjectUtils.requireNonNull(arguments.get(0))); - List items = sequence.asList(); + List items = sequence.getValue(); return ISequence.of(average(items)); } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/FnBaseUri.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/FnBaseUri.java index 91486eda6..e193bbe9c 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/FnBaseUri.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/FnBaseUri.java @@ -101,7 +101,7 @@ private static ISequence executeOneArg(@NonNull IFunction function, ISequence arg = FunctionUtils.asType(ObjectUtils.requireNonNull(arguments.get(0))); - INodeItem item = FunctionUtils.getFirstItem(arg, true); + INodeItem item = arg.getFirstItem(true); return ISequence.of(fnBaseUri(item)); } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/FnBoolean.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/FnBoolean.java index 4d044095a..bafdf43b4 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/FnBoolean.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/FnBoolean.java @@ -29,7 +29,6 @@ import gov.nist.secauto.metaschema.core.metapath.DynamicContext; import gov.nist.secauto.metaschema.core.metapath.ISequence; import gov.nist.secauto.metaschema.core.metapath.MetapathConstants; -import gov.nist.secauto.metaschema.core.metapath.function.FunctionUtils; import gov.nist.secauto.metaschema.core.metapath.function.IArgument; import gov.nist.secauto.metaschema.core.metapath.function.IFunction; import gov.nist.secauto.metaschema.core.metapath.function.InvalidArgumentFunctionException; @@ -109,7 +108,7 @@ public static IBooleanItem fnBoolean(@NonNull ISequence sequence) { */ public static boolean fnBooleanAsPrimitive(@NonNull ISequence sequence) { boolean retval = false; - IItem first = FunctionUtils.getFirstItem(sequence, false); + IItem first = sequence.getFirstItem(false); if (first != null) { if (first instanceof INodeItem) { retval = true; diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/FnCeiling.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/FnCeiling.java index 215ede52b..7d847da3e 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/FnCeiling.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/FnCeiling.java @@ -79,7 +79,7 @@ private static ISequence execute( ISequence sequence = FunctionUtils.asType( ObjectUtils.requireNonNull(arguments.get(0))); - INumericItem item = FunctionUtils.getFirstItem(sequence, true); + INumericItem item = sequence.getFirstItem(true); if (item == null) { return ISequence.empty(); // NOPMD - readability } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/FnCompare.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/FnCompare.java index ad5fc26b6..f6fbf7516 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/FnCompare.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/FnCompare.java @@ -35,7 +35,6 @@ import gov.nist.secauto.metaschema.core.metapath.item.IItem; import gov.nist.secauto.metaschema.core.metapath.item.atomic.IIntegerItem; import gov.nist.secauto.metaschema.core.metapath.item.atomic.IStringItem; -import gov.nist.secauto.metaschema.core.util.ObjectUtils; import java.util.List; @@ -80,11 +79,8 @@ private static ISequence execute( @NonNull List> arguments, @NonNull DynamicContext dynamicContext, IItem focus) { - IStringItem comparand1 = FunctionUtils.getFirstItem( - FunctionUtils.asType(ObjectUtils.requireNonNull(arguments.get(0))), true); - - IStringItem comparand2 = FunctionUtils.getFirstItem( - FunctionUtils.asType(ObjectUtils.requireNonNull(arguments.get(1))), true); + IStringItem comparand1 = FunctionUtils.asTypeOrNull(arguments.get(0).getFirstItem(true)); + IStringItem comparand2 = FunctionUtils.asTypeOrNull(arguments.get(1).getFirstItem(true)); ISequence retval; if (comparand1 == null || comparand2 == null) { diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/FnConcat.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/FnConcat.java index 56eaf357f..42594b5f9 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/FnConcat.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/FnConcat.java @@ -29,7 +29,6 @@ import gov.nist.secauto.metaschema.core.metapath.DynamicContext; import gov.nist.secauto.metaschema.core.metapath.ISequence; import gov.nist.secauto.metaschema.core.metapath.MetapathConstants; -import gov.nist.secauto.metaschema.core.metapath.function.FunctionUtils; import gov.nist.secauto.metaschema.core.metapath.function.IArgument; import gov.nist.secauto.metaschema.core.metapath.function.IFunction; import gov.nist.secauto.metaschema.core.metapath.item.IItem; @@ -87,7 +86,7 @@ private static ISequence execute( return ISequence.of(concat(ObjectUtils.notNull(arguments.stream() .map(arg -> { assert arg != null; - return (IAnyAtomicItem) FunctionUtils.getFirstItem(arg, true); + return (IAnyAtomicItem) arg.getFirstItem(true); })))); } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/FnData.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/FnData.java index f860e9bf2..3ebb645e5 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/FnData.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/FnData.java @@ -36,6 +36,7 @@ import gov.nist.secauto.metaschema.core.metapath.item.IItem; import gov.nist.secauto.metaschema.core.metapath.item.atomic.IAnyAtomicItem; import gov.nist.secauto.metaschema.core.metapath.item.atomic.IAtomicValuedItem; +import gov.nist.secauto.metaschema.core.metapath.item.function.IArrayItem; import gov.nist.secauto.metaschema.core.metapath.item.node.INodeItem; import gov.nist.secauto.metaschema.core.util.ObjectUtils; @@ -126,11 +127,9 @@ private static ISequence executeOneArg( */ @SuppressWarnings("null") @NonNull - public static ISequence fnData(@NonNull ISequence sequence) { - @NonNull Stream stream = sequence.asStream(); - return ISequence.of(stream.flatMap(x -> { - return Stream.of(fnDataItem(x)); - })); + public static ISequence fnData(@NonNull ISequence sequence) { + return ISequence.of(sequence.stream() + .flatMap(FnData::atomize)); } /** @@ -143,15 +142,62 @@ public static ISequence fnData(@NonNull ISequence sequence) { * @return the atomized result */ @NonNull - public static IAnyAtomicItem fnDataItem(@NonNull IItem item) { + public static IAnyAtomicItem fnDataItem(@NonNull INodeItem item) { IAnyAtomicItem retval = null; - if (item instanceof IAnyAtomicItem) { - retval = (IAnyAtomicItem) item; - } else if (item instanceof IAtomicValuedItem) { + if (item instanceof IAtomicValuedItem) { retval = ((IAtomicValuedItem) item).toAtomicItem(); } - if (retval == null) { + if (retval != null) { + return retval; + } + throw new InvalidTypeFunctionException(InvalidTypeFunctionException.NODE_HAS_NO_TYPED_VALUE, item); + } + + /** + * An implementation of + * item + * atomization. + * + * @param item + * the item to atomize + * @return the atomized result + */ + @NonNull + public static Stream fnDataItem(@NonNull IArrayItem item) { + return ObjectUtils.notNull(item.stream().flatMap(member -> { + Stream result; + if (member instanceof IItem) { + result = atomize((IItem) member); + } else if (member instanceof ISequence) { + result = ((ISequence) member).stream() + .flatMap(FnData::atomize); + } else { + throw new UnsupportedOperationException("array member not an item or sequence."); + } + return result; + })); + } + + /** + * An implementation of + * item + * atomization. + * + * @param item + * the item to atomize + * @return the atomized result + */ + @NonNull + public static Stream atomize(@NonNull IItem item) { + Stream retval; + if (item instanceof IAnyAtomicItem) { + retval = ObjectUtils.notNull(Stream.of((IAnyAtomicItem) item)); + } else if (item instanceof IAtomicValuedItem) { + retval = ObjectUtils.notNull(Stream.of(((IAtomicValuedItem) item).toAtomicItem())); + } else if (item instanceof IArrayItem) { + retval = fnDataItem((IArrayItem) item); + } else { throw new InvalidTypeFunctionException(InvalidTypeFunctionException.NODE_HAS_NO_TYPED_VALUE, item); } return retval; diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/FnDoc.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/FnDoc.java index efb74b3a0..049e4b58e 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/FnDoc.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/FnDoc.java @@ -76,7 +76,7 @@ private static ISequence execute(@NonNull IFunction function, IItem focus) { ISequence arg = FunctionUtils.asType(ObjectUtils.requireNonNull(arguments.get(0))); - IStringItem item = FunctionUtils.getFirstItem(arg, true); + IStringItem item = arg.getFirstItem(true); return item == null ? ISequence.empty() : ISequence.of(fnDoc(item, dynamicContext)); } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/FnDocumentUri.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/FnDocumentUri.java index 2e02b919e..64c8f602f 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/FnDocumentUri.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/FnDocumentUri.java @@ -88,14 +88,9 @@ private static ISequence executeNoArg(@NonNull IFunction function, INodeItem item = FunctionUtils.requireTypeOrNull(INodeItem.class, focus); - ISequence retval; - if (item instanceof IDocumentNodeItem) { - IAnyUriItem uri = fnDocumentUri((IDocumentNodeItem) item); - retval = ISequence.of(uri); - } else { - retval = ISequence.empty(); - } - return retval; + return item instanceof IDocumentNodeItem + ? ISequence.of(fnDocumentUri((IDocumentNodeItem) item)) + : ISequence.empty(); } @SuppressWarnings("unused") @@ -107,18 +102,11 @@ private static ISequence executeOneArg(@NonNull IFunction function, ISequence arg = FunctionUtils.asType(ObjectUtils.requireNonNull(arguments.get(0))); - INodeItem item = FunctionUtils.getFirstItem(arg, true); - - ISequence retval; - if (item == null) { - retval = ISequence.empty(); - } else if (item instanceof IDocumentNodeItem) { - IAnyUriItem uri = fnDocumentUri((IDocumentNodeItem) item); - retval = ISequence.of(uri); - } else { - retval = ISequence.empty(); - } - return retval; + INodeItem item = arg.getFirstItem(true); + + return item instanceof IDocumentNodeItem + ? ISequence.of(fnDocumentUri((IDocumentNodeItem) item)) + : ISequence.empty(); } /** diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/FnInsertBefore.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/FnInsertBefore.java index 0189f99a0..879a37f27 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/FnInsertBefore.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/FnInsertBefore.java @@ -84,11 +84,10 @@ private static ISequence execute(@NonNull IFunction function, @NonNull DynamicContext dynamicContext, IItem focus) { ISequence target = FunctionUtils.asType(ObjectUtils.requireNonNull(arguments.get(0))); - IIntegerItem position - = ObjectUtils.requireNonNull( - FunctionUtils.getFirstItem(FunctionUtils.asType(ObjectUtils.requireNonNull(arguments.get(1))), true)); + + IIntegerItem position = FunctionUtils.asType(ObjectUtils.requireNonNull(arguments.get(1).getFirstItem(true))); ISequence inserts = FunctionUtils.asType(ObjectUtils.requireNonNull(arguments.get(2))); - return ISequence.of(fnInsertBefore(target, position, inserts)); + return ISequence.ofCollection(fnInsertBefore(target, position, inserts)); } /** diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/FnMinMax.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/FnMinMax.java index 12c6043a2..8953bcad3 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/FnMinMax.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/FnMinMax.java @@ -101,7 +101,7 @@ private static ISequence executeMin( ISequence sequence = FunctionUtils.asType( ObjectUtils.requireNonNull(arguments.get(0))); - return ISequence.of(min(sequence.asList())); + return ISequence.of(min(sequence.getValue())); } @SuppressWarnings("unused") @@ -114,7 +114,7 @@ private static ISequence executeMax( ISequence sequence = FunctionUtils.asType( ObjectUtils.requireNonNull(arguments.get(0))); - return ISequence.of(max(sequence.asList())); + return ISequence.of(max(sequence.getValue())); } /** diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/FnPath.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/FnPath.java index 1242f9bbc..bd06bdd36 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/FnPath.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/FnPath.java @@ -121,7 +121,7 @@ private static ISequence executeOneArg(@NonNull IFunction function, */ @NonNull public static ISequence fnPath(@NonNull ISequence sequence) { - IItem item = FunctionUtils.getFirstItem(sequence, true); + IItem item = sequence.getFirstItem(true); ISequence retval; if (item == null) { diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/FnRemove.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/FnRemove.java index 9568d11d3..73f666bf2 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/FnRemove.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/FnRemove.java @@ -79,10 +79,8 @@ private static ISequence execute(@NonNull IFunction function, @NonNull DynamicContext dynamicContext, IItem focus) { ISequence target = FunctionUtils.asType(ObjectUtils.requireNonNull(arguments.get(0))); - IIntegerItem position - = ObjectUtils.requireNonNull( - FunctionUtils.getFirstItem(FunctionUtils.asType(ObjectUtils.requireNonNull(arguments.get(1))), true)); - return ISequence.of(fnRemove(target, position)); + IIntegerItem position = FunctionUtils.asType(ObjectUtils.requireNonNull(arguments.get(1).getFirstItem(true))); + return ISequence.ofCollection(fnRemove(target, position)); } /** diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/FnResolveUri.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/FnResolveUri.java index fbfe91ec3..210932977 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/FnResolveUri.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/FnResolveUri.java @@ -102,7 +102,7 @@ private static ISequence executeOneArg( return ISequence.empty(); // NOPMD - readability } - IStringItem relativeString = FunctionUtils.getFirstItem(relativeSequence, true); + IStringItem relativeString = relativeSequence.getFirstItem(true); IAnyUriItem resolvedUri = null; if (relativeString != null) { resolvedUri = fnResolveUri(relativeString, null, dynamicContext); @@ -143,7 +143,7 @@ private static ISequence executeTwoArg( } ISequence baseSequence = FunctionUtils.asType(ObjectUtils.requireNonNull(arguments.get(1))); - IStringItem baseString = FunctionUtils.getFirstItem(baseSequence, true); + IStringItem baseString = baseSequence.getFirstItem(true); if (baseString == null) { throw new InvalidArgumentFunctionException( @@ -152,7 +152,7 @@ private static ISequence executeTwoArg( } IAnyUriItem baseUri = IAnyUriItem.cast(baseString); - IStringItem relativeString = FunctionUtils.getFirstItem(relativeSequence, true); + IStringItem relativeString = relativeSequence.getFirstItem(true); IAnyUriItem resolvedUri = null; if (relativeString != null) { diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/FnReverse.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/FnReverse.java index 5c6b39fa4..8fac330e3 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/FnReverse.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/FnReverse.java @@ -74,7 +74,7 @@ private static ISequence execute(@NonNull IFunction function, @NonNull DynamicContext dynamicContext, IItem focus) { ISequence target = FunctionUtils.asType(ObjectUtils.requireNonNull(arguments.get(0))); - return ISequence.of(fnReverse(target)); + return ISequence.ofCollection(fnReverse(target)); } /** diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/FnRound.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/FnRound.java index 5edef5cf5..7b39a3643 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/FnRound.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/FnRound.java @@ -102,7 +102,7 @@ private static ISequence executeOneArg( ISequence sequence = FunctionUtils.asType( ObjectUtils.requireNonNull(arguments.get(0))); - INumericItem item = FunctionUtils.getFirstItem(sequence, true); + INumericItem item = sequence.getFirstItem(true); if (item == null) { return ISequence.empty(); // NOPMD - readability } @@ -120,15 +120,12 @@ private static ISequence executeTwoArg( ISequence sequence = FunctionUtils.asType( ObjectUtils.requireNonNull(arguments.get(0))); - INumericItem item = FunctionUtils.getFirstItem(sequence, true); + INumericItem item = sequence.getFirstItem(true); if (item == null) { return ISequence.empty(); // NOPMD - readability } - IIntegerItem precision = FunctionUtils.asType( - FunctionUtils.requireFirstItem( - ObjectUtils.requireNonNull(arguments.get(1)), - true)); + IIntegerItem precision = FunctionUtils.asType(ObjectUtils.requireNonNull(arguments.get(1).getFirstItem(true))); return ISequence.of(item.round(precision)); } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/FnStartsWith.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/FnStartsWith.java index 33320a005..b07098495 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/FnStartsWith.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/FnStartsWith.java @@ -35,7 +35,6 @@ import gov.nist.secauto.metaschema.core.metapath.item.IItem; import gov.nist.secauto.metaschema.core.metapath.item.atomic.IBooleanItem; import gov.nist.secauto.metaschema.core.metapath.item.atomic.IStringItem; -import gov.nist.secauto.metaschema.core.util.ObjectUtils; import java.util.List; @@ -68,10 +67,9 @@ private static ISequence execute(@NonNull IFunction function, @NonNull List> arguments, @NonNull DynamicContext dynamicContext, IItem focus) { - IStringItem arg1 = FunctionUtils.getFirstItem( - FunctionUtils.asType(ObjectUtils.requireNonNull(arguments.get(0))), true); - IStringItem arg2 = FunctionUtils.getFirstItem( - FunctionUtils.asType(ObjectUtils.requireNonNull(arguments.get(1))), true); + IStringItem arg1 = FunctionUtils.asTypeOrNull(arguments.get(0).getFirstItem(true)); + + IStringItem arg2 = FunctionUtils.asTypeOrNull(arguments.get(1).getFirstItem(true)); return ISequence.of(fnStartsWith(arg1, arg2)); } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/FnSum.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/FnSum.java index 538969662..2275c7ee4 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/FnSum.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/FnSum.java @@ -105,7 +105,7 @@ private static ISequence executeOneArg( ISequence sequence = FunctionUtils.asType( ObjectUtils.requireNonNull(arguments.get(0))); - return ISequence.of(sum(sequence.asList(), IIntegerItem.ZERO)); + return ISequence.of(sum(sequence.getValue(), IIntegerItem.ZERO)); } @SuppressWarnings("unused") @@ -115,15 +115,11 @@ private static ISequence executeTwoArg( @NonNull List> arguments, @NonNull DynamicContext dynamicContext, IItem focus) { - ISequence sequence = FunctionUtils.asType( - ObjectUtils.requireNonNull(arguments.get(0))); + ISequence sequence = FunctionUtils.asType(ObjectUtils.requireNonNull(arguments.get(0))); - IAnyAtomicItem zero = FunctionUtils.getFirstItem( - FunctionUtils.asType( - ObjectUtils.requireNonNull(arguments.get(1))), - true); + IAnyAtomicItem zero = FunctionUtils.asTypeOrNull(arguments.get(1).getFirstItem(true)); - return ISequence.of(sum(sequence.asList(), zero)); + return ISequence.of(sum(sequence.getValue(), zero)); } /** diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/FnTail.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/FnTail.java index 1f07291d6..0ecb66551 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/FnTail.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/FnTail.java @@ -73,7 +73,7 @@ private static ISequence execute(@NonNull IFunction function, @NonNull DynamicContext dynamicContext, IItem focus) { ISequence items = FunctionUtils.asType(ObjectUtils.requireNonNull(arguments.get(0))); - return ISequence.of(fnTail(items)); + return ISequence.ofCollection(fnTail(items)); } /** diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/MpRecurseDepth.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/MpRecurseDepth.java index 5e3e83139..d8bfdd423 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/MpRecurseDepth.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/MpRecurseDepth.java @@ -105,7 +105,7 @@ private static ISequence executeOneArg( ISequence initalContext = ISequence.of(FunctionUtils.requireType(INodeItem.class, focus)); ISequence arg = FunctionUtils.asType(ObjectUtils.requireNonNull(arguments.get(0))); - IStringItem recursionPath = FunctionUtils.requireFirstItem(arg, true); + IStringItem recursionPath = ObjectUtils.requireNonNull(arg.getFirstItem(true)); return recurseDepth(initalContext, recursionPath, dynamicContext); } @@ -121,7 +121,7 @@ private static ISequence executeTwoArg( ISequence initalContext = FunctionUtils.asType(ObjectUtils.requireNonNull(arguments.get(0))); ISequence arg = FunctionUtils.asType(ObjectUtils.requireNonNull(arguments.get(1))); - IStringItem recursionPath = FunctionUtils.requireFirstItem(arg, true); + IStringItem recursionPath = ObjectUtils.requireNonNull(arg.getFirstItem(true)); return recurseDepth(initalContext, recursionPath, dynamicContext); } @@ -162,12 +162,12 @@ public static ISequence recurseDepth( @NonNull MetapathExpression recursionMetapath, @NonNull DynamicContext dynamicContext) { - return ISequence.of(ObjectUtils.notNull(initialContext.asStream() + return ISequence.of(ObjectUtils.notNull(initialContext.stream() .flatMap(item -> { @NonNull ISequence metapathResult = recursionMetapath.evaluate(item, dynamicContext); ISequence result = recurseDepth(metapathResult, recursionMetapath, dynamicContext); - return ObjectUtils.notNull(Stream.concat(result.asStream(), Stream.of(item))); + return ObjectUtils.notNull(Stream.concat(result.stream(), Stream.of(item))); }))); } } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/NumericFunction.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/NumericFunction.java index 09244ee9f..ddbb03df6 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/NumericFunction.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/function/library/NumericFunction.java @@ -103,7 +103,7 @@ public ISequence execute(@NonNull IFunction function, return ISequence.empty(); // NOPMD - readability } - INumericItem item = FunctionUtils.getFirstItem(sequence, true); + INumericItem item = sequence.getFirstItem(true); if (item == null) { return ISequence.empty(); // NOPMD - readability } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/impl/AbstractArrayItem.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/impl/AbstractArrayItem.java new file mode 100644 index 000000000..dd6dd1b64 --- /dev/null +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/impl/AbstractArrayItem.java @@ -0,0 +1,105 @@ +/* + * Portions of this software was developed by employees of the National Institute + * of Standards and Technology (NIST), an agency of the Federal Government and is + * being made available as a public service. Pursuant to title 17 United States + * Code Section 105, works of NIST employees are not subject to copyright + * protection in the United States. This software may be subject to foreign + * copyright. Permission in the United States and in foreign countries, to the + * extent that NIST may hold copyright, to use, copy, modify, create derivative + * works, and distribute this software and its documentation without fee is hereby + * granted on a non-exclusive basis, provided that this notice and disclaimer + * of warranty appears in all copies. + * + * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER + * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY + * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM + * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE + * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT + * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, + * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, + * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, + * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR + * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT + * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. + */ + +package gov.nist.secauto.metaschema.core.metapath.impl; + +import gov.nist.secauto.metaschema.core.metapath.DynamicContext; +import gov.nist.secauto.metaschema.core.metapath.ICollectionValue; +import gov.nist.secauto.metaschema.core.metapath.ISequence; +import gov.nist.secauto.metaschema.core.metapath.function.FunctionUtils; +import gov.nist.secauto.metaschema.core.metapath.function.IArgument; +import gov.nist.secauto.metaschema.core.metapath.function.ISequenceType; +import gov.nist.secauto.metaschema.core.metapath.function.Occurrence; +import gov.nist.secauto.metaschema.core.metapath.item.atomic.IAnyAtomicItem; +import gov.nist.secauto.metaschema.core.metapath.item.atomic.IIntegerItem; +import gov.nist.secauto.metaschema.core.metapath.item.function.IArrayItem; +import gov.nist.secauto.metaschema.core.util.ObjectUtils; + +import java.util.EnumSet; +import java.util.List; +import java.util.Objects; +import java.util.Set; + +import javax.xml.namespace.QName; + +import edu.umd.cs.findbugs.annotations.NonNull; + +public abstract class AbstractArrayItem + extends ImmutableCollections.AbstractImmutableDelegatedCollection + implements IArrayItem { + @NonNull + public static final QName QNAME = new QName("array"); + @NonNull + public static final Set PROPERTIES = ObjectUtils.notNull( + EnumSet.of(FunctionProperty.DETERMINISTIC)); + @NonNull + public static final List ARGUMENTS = ObjectUtils.notNull(List.of( + IArgument.builder().name("position").type(IIntegerItem.class).one().build())); + @NonNull + public static final ISequenceType RESULT = ISequenceType.of(IAnyAtomicItem.class, Occurrence.ZERO_OR_ONE); + + @SuppressWarnings({ "rawtypes", "unchecked" }) + @NonNull + private static final IArrayItem EMPTY = new ArrayItemN<>(); + + @SuppressWarnings("unchecked") + @NonNull + public static IArrayItem empty() { + return (IArrayItem) EMPTY; + } + + @Override + public ISequence execute(List> arguments, DynamicContext dynamicContext, + ISequence focus) { + ISequence arg = FunctionUtils.asType( + ObjectUtils.notNull(arguments.get(0))); + + IIntegerItem position = arg.getFirstItem(true); + if (position == null) { + return ISequence.empty(); // NOPMD - readability + } + + int index = position.asInteger().intValueExact() - 1; + ICollectionValue result = getValue().get(index); + return result.asSequence(); + } + + @Override + public int hashCode() { + return Objects.hash(getValue()); + } + + @Override + public boolean equals(Object other) { + return other == this + || other instanceof IArrayItem && getValue().equals(((IArrayItem) other).getValue()); + } + + @Override + public String asString() { + return ObjectUtils.notNull(toString()); + } +} diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/SingletonSequenceImpl.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/impl/AbstractSequence.java similarity index 66% rename from core/src/main/java/gov/nist/secauto/metaschema/core/metapath/SingletonSequenceImpl.java rename to core/src/main/java/gov/nist/secauto/metaschema/core/metapath/impl/AbstractSequence.java index 2f5244aba..2d2a42783 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/SingletonSequenceImpl.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/impl/AbstractSequence.java @@ -24,76 +24,51 @@ * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. */ -package gov.nist.secauto.metaschema.core.metapath; +package gov.nist.secauto.metaschema.core.metapath.impl; +import gov.nist.secauto.metaschema.core.metapath.ISequence; import gov.nist.secauto.metaschema.core.metapath.item.IItem; +import gov.nist.secauto.metaschema.core.util.ObjectUtils; -import java.util.List; -import java.util.function.Consumer; -import java.util.stream.Stream; +import java.util.stream.Collectors; import edu.umd.cs.findbugs.annotations.NonNull; -class SingletonSequenceImpl implements ISequence { - @NonNull - private final ITEM_TYPE item; - - public SingletonSequenceImpl(@NonNull ITEM_TYPE item) { - this.item = item; - } +public abstract class AbstractSequence + extends ImmutableCollections.AbstractImmutableDelegatedCollection + implements ISequence { + @SuppressWarnings({ "rawtypes", "unchecked" }) @NonNull - protected ITEM_TYPE getItem() { - return item; - } + private static final ISequence EMPTY = new SequenceN<>(); - @SuppressWarnings("null") - @Override - public List asList() { - return List.of(item); - } - - @SuppressWarnings("null") - @Override - public Stream asStream() { - return Stream.of(item); - } - - @Override - public boolean isEmpty() { - return false; - } - - @Override - public String toString() { - return asList().toString(); + @SuppressWarnings("unchecked") + public static ISequence empty() { + return (ISequence) EMPTY; } @Override - public int size() { - return 1; - } - - @Override - public ISequence collect() { - return this; + public boolean equals(Object other) { + // must either be the same instance or a sequence that has the same list + // contents + return other == this + || other instanceof ISequence && getValue().equals(((ISequence) other).getValue()); } @Override - public void forEach(Consumer action) { - action.accept(item); + public int hashCode() { + return getValue().hashCode(); } @Override - public boolean equals(Object other) { - // must either be the same instance or a sequence that has the same list - // contents - return other == this - || other instanceof ISequence && asList().equals(((ISequence) other).asList()); + public String asString() { + return ObjectUtils.notNull(toString()); } @Override - public int hashCode() { - return asList().hashCode(); + public String toString() { + return safeStream() + .map(Object::toString) + .collect(Collectors.joining(",", "(", ")")); } } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/impl/ArrayItemN.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/impl/ArrayItemN.java new file mode 100644 index 000000000..a700c5669 --- /dev/null +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/impl/ArrayItemN.java @@ -0,0 +1,56 @@ +/* + * Portions of this software was developed by employees of the National Institute + * of Standards and Technology (NIST), an agency of the Federal Government and is + * being made available as a public service. Pursuant to title 17 United States + * Code Section 105, works of NIST employees are not subject to copyright + * protection in the United States. This software may be subject to foreign + * copyright. Permission in the United States and in foreign countries, to the + * extent that NIST may hold copyright, to use, copy, modify, create derivative + * works, and distribute this software and its documentation without fee is hereby + * granted on a non-exclusive basis, provided that this notice and disclaimer + * of warranty appears in all copies. + * + * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER + * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY + * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM + * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE + * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT + * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, + * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, + * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, + * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR + * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT + * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. + */ + +package gov.nist.secauto.metaschema.core.metapath.impl; + +import gov.nist.secauto.metaschema.core.metapath.ICollectionValue; +import gov.nist.secauto.metaschema.core.util.CollectionUtil; +import gov.nist.secauto.metaschema.core.util.ObjectUtils; + +import java.util.List; + +import edu.umd.cs.findbugs.annotations.NonNull; + +public class ArrayItemN + extends AbstractArrayItem { + @NonNull + private final List items; + + @SafeVarargs + public ArrayItemN(@NonNull ITEM... items) { + this(ObjectUtils.notNull(List.of(items))); + } + + public ArrayItemN(@NonNull List items) { + this.items = CollectionUtil.unmodifiableList(items); + } + + @Override + public List getValue() { + return items; + } + +} diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/impl/ImmutableCollections.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/impl/ImmutableCollections.java new file mode 100644 index 000000000..4102d577d --- /dev/null +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/impl/ImmutableCollections.java @@ -0,0 +1,170 @@ +/* + * Portions of this software was developed by employees of the National Institute + * of Standards and Technology (NIST), an agency of the Federal Government and is + * being made available as a public service. Pursuant to title 17 United States + * Code Section 105, works of NIST employees are not subject to copyright + * protection in the United States. This software may be subject to foreign + * copyright. Permission in the United States and in foreign countries, to the + * extent that NIST may hold copyright, to use, copy, modify, create derivative + * works, and distribute this software and its documentation without fee is hereby + * granted on a non-exclusive basis, provided that this notice and disclaimer + * of warranty appears in all copies. + * + * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER + * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY + * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM + * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE + * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT + * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, + * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, + * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, + * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR + * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT + * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. + */ + +package gov.nist.secauto.metaschema.core.metapath.impl; + +import gov.nist.secauto.metaschema.core.util.ObjectUtils; + +import java.util.AbstractCollection; +import java.util.Collection; +import java.util.Iterator; +import java.util.List; +import java.util.ListIterator; +import java.util.function.Predicate; +import java.util.stream.Stream; + +import edu.umd.cs.findbugs.annotations.NonNull; + +/** + * This implementation is inspired by the similar implementation provided by the + * JDK. + */ +public class ImmutableCollections { + protected static UnsupportedOperationException unsupported() { + return new UnsupportedOperationException("method not supported"); + } + + public static abstract class AbstractImmutableCollection + extends AbstractCollection { + + @Override + public final boolean add(T e) { + throw unsupported(); + } + + @Override + public final boolean addAll(Collection c) { + throw unsupported(); + } + + @Override + public final void clear() { + throw unsupported(); + } + + @Override + public final boolean remove(Object o) { + throw unsupported(); + } + + @Override + public final boolean removeAll(Collection c) { + throw unsupported(); + } + + @Override + public final boolean removeIf(Predicate filter) { + throw unsupported(); + } + + @Override + public final boolean retainAll(Collection c) { + throw unsupported(); + } + } + + public static abstract class AbstractImmutableList + extends AbstractImmutableCollection + implements List { + + @Override + public boolean addAll(int index, Collection c) { + throw unsupported(); + } + + @Override + public T set(int index, T element) { + throw unsupported(); + } + + @Override + public void add(int index, T element) { + throw unsupported(); + } + + @Override + public T remove(int index) { + throw unsupported(); + } + } + + public static abstract class AbstractImmutableDelegatedCollection + extends AbstractImmutableList { + + @NonNull + public abstract List getValue(); + + @Override + public T get(int index) { + return getValue().get(index); + } + + @Override + public int indexOf(Object obj) { + return getValue().indexOf(obj); + } + + @Override + public Iterator iterator() { + return getValue().iterator(); + } + + @Override + public int lastIndexOf(Object obj) { + return getValue().lastIndexOf(obj); + } + + @Override + public ListIterator listIterator() { + return getValue().listIterator(); + } + + @Override + public ListIterator listIterator(int index) { + return getValue().listIterator(index); + } + + @Override + public int size() { + return getValue().size(); + } + + @Override + public List subList(int fromIndex, int toIndex) { + return getValue().subList(fromIndex, toIndex); + } + + @Override + public Stream stream() { + return ObjectUtils.notNull(getValue().stream()); + } + + @Override + public String toString() { + return getValue().toString(); + } + } +} diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/ListSequenceImpl.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/impl/SequenceN.java similarity index 64% rename from core/src/main/java/gov/nist/secauto/metaschema/core/metapath/ListSequenceImpl.java rename to core/src/main/java/gov/nist/secauto/metaschema/core/metapath/impl/SequenceN.java index 3bf294a4e..618c0a50b 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/ListSequenceImpl.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/impl/SequenceN.java @@ -24,7 +24,7 @@ * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. */ -package gov.nist.secauto.metaschema.core.metapath; +package gov.nist.secauto.metaschema.core.metapath.impl; import gov.nist.secauto.metaschema.core.metapath.item.IItem; import gov.nist.secauto.metaschema.core.util.CollectionUtil; @@ -33,68 +33,34 @@ import java.util.ArrayList; import java.util.Collection; import java.util.List; -import java.util.function.Consumer; -import java.util.stream.Stream; import edu.umd.cs.findbugs.annotations.NonNull; -class ListSequenceImpl implements ISequence { +public class SequenceN + extends AbstractSequence { @NonNull - private final List items; + private final List items; - public ListSequenceImpl(@NonNull Collection items) { + public SequenceN(@NonNull Collection items) { this(new ArrayList<>(items), false); } - public ListSequenceImpl(@NonNull List items, boolean copy) { + public SequenceN(@NonNull List items, boolean copy) { this.items = CollectionUtil.unmodifiableList(copy ? new ArrayList<>(items) : items); } - @Override - public List asList() { - return items; - } - - @Override - public Stream asStream() { - return ObjectUtils.notNull(items.stream()); + @SafeVarargs + public SequenceN(@NonNull ITEM... items) { + this(CollectionUtil.unmodifiableList(ObjectUtils.notNull(List.of(items)))); } - @Override - public boolean isEmpty() { - return items.isEmpty(); + public SequenceN(@NonNull List items) { + this.items = items; } @Override - public String toString() { - return asList().toString(); - } - - @Override - public int size() { - return items.size(); - } - - @Override - public ISequence collect() { - return this; - } - - @Override - public void forEach(Consumer action) { - items.forEach(action); - } - - @Override - public boolean equals(Object other) { - // must either be the same instance or a sequence that has the same list - // contents - return other == this - || other instanceof ISequence && asList().equals(((ISequence) other).asList()); + public List getValue() { + return items; } - @Override - public int hashCode() { - return asList().hashCode(); - } } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/EmptyListImpl.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/impl/SingletonSequence.java similarity index 73% rename from core/src/main/java/gov/nist/secauto/metaschema/core/metapath/EmptyListImpl.java rename to core/src/main/java/gov/nist/secauto/metaschema/core/metapath/impl/SingletonSequence.java index 56e2bfbe4..4c4356188 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/EmptyListImpl.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/impl/SingletonSequence.java @@ -24,58 +24,55 @@ * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. */ -package gov.nist.secauto.metaschema.core.metapath; +package gov.nist.secauto.metaschema.core.metapath.impl; import gov.nist.secauto.metaschema.core.metapath.item.IItem; +import gov.nist.secauto.metaschema.core.util.CollectionUtil; -import java.util.Collections; import java.util.List; import java.util.function.Consumer; import java.util.stream.Stream; -class EmptyListImpl implements ISequence { +import edu.umd.cs.findbugs.annotations.NonNull; - @SuppressWarnings("null") - @Override - public List asList() { - return Collections.emptyList(); - } +public class SingletonSequence + extends AbstractSequence { + @NonNull + private final ITEM item; - @SuppressWarnings("null") - @Override - public Stream asStream() { - return Stream.empty(); + public SingletonSequence(@NonNull ITEM item) { + this.item = item; } - @Override - public boolean isEmpty() { - return true; + @NonNull + protected ITEM getItem() { + return item; } + @SuppressWarnings("null") @Override - public int size() { - return 0; + public List getValue() { + return CollectionUtil.singletonList(item); } @Override - public ISequence collect() { - return this; + public boolean isEmpty() { + return false; } @Override - public void forEach(Consumer action) { - // do nothing + public int size() { + return 1; } + @SuppressWarnings("null") @Override - public boolean equals(Object other) { - // must either be the same instance or a sequence that is empty - return other == this - || other instanceof ISequence && ((ISequence) other).isEmpty(); + public Stream stream() { + return Stream.of(item); } @Override - public int hashCode() { - return 1; + public void forEach(Consumer action) { + action.accept(item); } } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/StreamSequenceImpl.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/impl/StreamSequence.java similarity index 68% rename from core/src/main/java/gov/nist/secauto/metaschema/core/metapath/StreamSequenceImpl.java rename to core/src/main/java/gov/nist/secauto/metaschema/core/metapath/impl/StreamSequence.java index 184cf4acb..d4c840bfd 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/StreamSequenceImpl.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/impl/StreamSequence.java @@ -24,7 +24,7 @@ * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. */ -package gov.nist.secauto.metaschema.core.metapath; +package gov.nist.secauto.metaschema.core.metapath.impl; import gov.nist.secauto.metaschema.core.metapath.item.IItem; import gov.nist.secauto.metaschema.core.util.ObjectUtils; @@ -37,25 +37,22 @@ import edu.umd.cs.findbugs.annotations.NonNull; -class StreamSequenceImpl implements ISequence { - private Stream stream; - private List list; +public class StreamSequence + extends AbstractSequence { - public StreamSequenceImpl(@NonNull Stream stream) { + private Stream stream; + private List list; + + public StreamSequence(@NonNull Stream stream) { Objects.requireNonNull(stream, "stream"); this.stream = stream; } @Override - public boolean isEmpty() { - return asList().isEmpty(); - } - - @Override - public List asList() { + public List getValue() { synchronized (this) { if (list == null) { - list = asStream().collect(Collectors.toUnmodifiableList()); + list = stream().collect(Collectors.toUnmodifiableList()); } assert list != null; return list; @@ -63,8 +60,9 @@ public List asList() { } @Override - public Stream asStream() { - @NonNull Stream retval; + public Stream stream() { + @NonNull Stream retval; + // Ensure thread safety and prevent multiple consumptions of the stream synchronized (this) { if (list == null) { if (stream == null) { @@ -81,36 +79,7 @@ public Stream asStream() { } @Override - public void forEach(Consumer action) { - asStream().forEachOrdered(action); - } - - @Override - public String toString() { - return asList().toString(); - } - - @Override - public int size() { - return asList().size(); - } - - @Override - public ISequence collect() { - asList(); - return this; - } - - @Override - public boolean equals(Object other) { - // must either be the same instance or a sequence that has the same list - // contents - return other == this - || other instanceof ISequence && asList().equals(((ISequence) other).asList()); - } - - @Override - public int hashCode() { - return asList().hashCode(); + public void forEach(Consumer action) { + stream().forEachOrdered(action); } } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/item/IItem.java b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/item/IItem.java index b1ea60637..60144f278 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/item/IItem.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/metapath/item/IItem.java @@ -27,8 +27,12 @@ package gov.nist.secauto.metaschema.core.metapath.item; import gov.nist.secauto.metaschema.core.datatype.IDataTypeAdapter; +import gov.nist.secauto.metaschema.core.metapath.ICollectionValue; +import gov.nist.secauto.metaschema.core.metapath.ISequence; -public interface IItem { +import java.util.stream.Stream; + +public interface IItem extends ICollectionValue { /** * Get the item's "wrapped" value. This "wrapped" value may be: *