Skip to content

Commit

Permalink
Added support for QName for Metaschema data types, which are used in …
Browse files Browse the repository at this point in the history
…function signatures now instead of the Java class name.
  • Loading branch information
david-waltermire committed Jun 7, 2024
1 parent 95a1e4e commit 1fbc504
Show file tree
Hide file tree
Showing 37 changed files with 438 additions and 171 deletions.
4 changes: 2 additions & 2 deletions cli-processor/src/main/resources/log4j2.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
<!DOCTYPE Configuration>
<Configuration verbose="true">
<Appenders>
<Console name="console-trace" target="SYSTEM_ERR" immediateFlush="true">
<Console name="console-trace" target="SYSTEM_OUT" immediateFlush="true">
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" charset="UTF-8" />
<ThresholdFilter level="INFO" onMatch="DENY" onMismatch="ACCEPT" />
</Console>
<Console name="console-info" target="SYSTEM_ERR" immediateFlush="true">
<Console name="console-info" target="SYSTEM_OUT" immediateFlush="true">
<PatternLayout pattern="%m%n" charset="UTF-8" />
<Filters>
<ThresholdFilter level="ERROR" onMatch="DENY" onMismatch="ACCEPT" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,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;
import nl.talsmasoftware.lazy4j.Lazy;
Expand All @@ -51,8 +53,9 @@ public final class DataTypeService {
private static final Logger LOGGER = LogManager.getLogger(DataTypeService.class);
private static final Lazy<DataTypeService> INSTANCE = Lazy.lazy(() -> new DataTypeService());

private final Map<String, IDataTypeAdapter<?>> libraryByName;
private final Map<Class<? extends IDataTypeAdapter<?>>, IDataTypeAdapter<?>> libraryByClass;
private final Map<String, IDataTypeAdapter<?>> typeByName;
private final Map<QName, IDataTypeAdapter<?>> typeByQName;
private final Map<Class<? extends IDataTypeAdapter<?>>, IDataTypeAdapter<?>> typeByClass;

/**
* Get the singleton service instance, which will be lazy constructed on first
Expand All @@ -74,12 +77,29 @@ private DataTypeService() {
.flatMap(provider -> provider.getJavaTypeAdapters().stream())
.collect(Collectors.toList());

Map<String, IDataTypeAdapter<?>> libraryByName = dataTypes.stream()
Map<String, IDataTypeAdapter<?>> typeByName = dataTypes.stream()
.flatMap(dataType -> dataType.getNames().stream()
.map(qname -> Map.entry(qname.getLocalPart(), dataType)))
.collect(CustomCollectors.toMap(
Map.Entry::getKey,
Map.Entry::getValue,
(key, v1, v2) -> {
if (LOGGER.isWarnEnabled()) {
LOGGER.warn("Data types '{}' and '{}' have duplicate name '{}'. Using the first.",
v1.getClass().getName(),
v2.getClass().getName(),
key);
}
return v1;
},
ConcurrentHashMap::new));

Map<QName, IDataTypeAdapter<?>> typeByQName = dataTypes.stream()
.flatMap(dataType -> dataType.getNames().stream()
.map(name -> Map.entry(name, dataType)))
.map(qname -> Map.entry(qname, dataType)))
.collect(CustomCollectors.toMap(
Map.Entry::getKey,
(entry) -> entry.getValue(),
Map.Entry::getValue,
(key, v1, v2) -> {
if (LOGGER.isWarnEnabled()) {
LOGGER.warn("Data types '{}' and '{}' have duplicate name '{}'. Using the first.",
Expand All @@ -92,7 +112,7 @@ private DataTypeService() {
ConcurrentHashMap::new));

@SuppressWarnings({ "unchecked", "null" }) Map<Class<? extends IDataTypeAdapter<?>>,
IDataTypeAdapter<?>> libraryByClass = dataTypes.stream()
IDataTypeAdapter<?>> typeByClass = dataTypes.stream()
.collect(CustomCollectors.toMap(
dataType -> (Class<? extends IDataTypeAdapter<?>>) dataType.getClass(),
Function.identity(),
Expand All @@ -104,21 +124,35 @@ private DataTypeService() {
return v1;
},
ConcurrentHashMap::new));
this.libraryByName = libraryByName;
this.libraryByClass = libraryByClass;
this.typeByName = typeByName;
this.typeByQName = typeByQName;
this.typeByClass = typeByClass;
}

/**
* Lookup a specific {@link IDataTypeAdapter} instance by its name.
*
* @param qname
* the qualified 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<?> getJavaTypeAdapterByQName(@NonNull QName qname) {
return typeByQName.get(qname);
}

/**
* Lookup a specific {@link IDataTypeAdapter} instance by its name.
*
* @param name
* the data type name of data type adapter to get the instance for
* the 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);
return typeByName.get(name);
}

/**
Expand All @@ -134,6 +168,6 @@ public IDataTypeAdapter<?> getJavaTypeAdapterByName(@NonNull String name) {
@SuppressWarnings("unchecked")
@Nullable
public <TYPE extends IDataTypeAdapter<?>> TYPE getJavaTypeAdapterByClass(@NonNull Class<TYPE> clazz) {
return (TYPE) libraryByClass.get(clazz);
return (TYPE) typeByClass.get(clazz);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,25 +63,25 @@ public interface IDataTypeAdapter<TYPE> {
* @return the name
*/
@NonNull
List<String> getNames();

/**
* The JSON primative type of the data type.
*
* @return the JSON data type
*/
JsonFormatTypes getJsonRawType();
List<QName> getNames();

/**
* Get the most preferred name for this data type.
*
* @return the name
*/
@NonNull
default String getPreferredName() {
default QName getPreferredName() {
return ObjectUtils.notNull(getNames().iterator().next());
}

/**
* The JSON primative type of the data type.
*
* @return the JSON data type
*/
JsonFormatTypes getJsonRawType();

/**
* Get the Java class supported by this adapter.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,30 +29,33 @@
import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonFormatTypes;

import gov.nist.secauto.metaschema.core.datatype.AbstractDataTypeAdapter;
import gov.nist.secauto.metaschema.core.metapath.MetapathConstants;
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 javax.xml.namespace.QName;

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

public class Base64Adapter
extends AbstractDataTypeAdapter<ByteBuffer, IBase64BinaryItem> {
@NonNull
private static final List<String> NAMES = ObjectUtils.notNull(
private static final List<QName> NAMES = ObjectUtils.notNull(
List.of(
"base64",
new QName(MetapathConstants.NS_METAPATH.toASCIIString(), "base64"),
// for backwards compatibility with original type name
"base64Binary"));
new QName(MetapathConstants.NS_METAPATH.toASCIIString(), "base64Binary")));

Base64Adapter() {
super(ByteBuffer.class);
}

@Override
public List<String> getNames() {
public List<QName> getNames() {
return NAMES;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonFormatTypes;

import gov.nist.secauto.metaschema.core.datatype.AbstractDataTypeAdapter;
import gov.nist.secauto.metaschema.core.metapath.MetapathConstants;
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;
Expand All @@ -41,20 +42,22 @@
import java.io.IOException;
import java.util.List;

import javax.xml.namespace.QName;

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

public class BooleanAdapter
extends AbstractDataTypeAdapter<Boolean, IBooleanItem> {
@NonNull
private static final List<String> NAMES = ObjectUtils.notNull(
List.of("boolean"));
private static final List<QName> NAMES = ObjectUtils.notNull(
List.of(new QName(MetapathConstants.NS_METAPATH.toASCIIString(), "boolean")));

BooleanAdapter() {
super(Boolean.class);
}

@Override
public List<String> getNames() {
public List<QName> getNames() {
return NAMES;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

import gov.nist.secauto.metaschema.core.datatype.AbstractCustomJavaDataTypeAdapter;
import gov.nist.secauto.metaschema.core.datatype.object.Date;
import gov.nist.secauto.metaschema.core.metapath.MetapathConstants;
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;
Expand All @@ -48,13 +49,15 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.xml.namespace.QName;

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

public class DateAdapter
extends AbstractCustomJavaDataTypeAdapter<Date, IDateItem> {
@NonNull
private static final List<String> NAMES = ObjectUtils.notNull(
List.of("date"));
private static final List<QName> NAMES = ObjectUtils.notNull(
List.of(new QName(MetapathConstants.NS_METAPATH.toASCIIString(), "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]))"
Expand All @@ -68,7 +71,7 @@ public class DateAdapter
}

@Override
public List<String> getNames() {
public List<QName> getNames() {
return NAMES;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

import gov.nist.secauto.metaschema.core.datatype.AbstractCustomJavaDataTypeAdapter;
import gov.nist.secauto.metaschema.core.datatype.object.DateTime;
import gov.nist.secauto.metaschema.core.metapath.MetapathConstants;
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;
Expand All @@ -44,23 +45,25 @@
import java.time.format.DateTimeParseException;
import java.util.List;

import javax.xml.namespace.QName;

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

public class DateTimeAdapter
extends AbstractCustomJavaDataTypeAdapter<DateTime, IDateTimeItem> {
@NonNull
private static final List<String> NAMES = ObjectUtils.notNull(
private static final List<QName> NAMES = ObjectUtils.notNull(
List.of(
"date-time",
new QName(MetapathConstants.NS_METAPATH.toASCIIString(), "date-time"),
// for backwards compatibility with original type name
"dateTime"));
new QName(MetapathConstants.NS_METAPATH.toASCIIString(), "dateTime")));

DateTimeAdapter() {
super(DateTime.class);
}

@Override
public List<String> getNames() {
public List<QName> getNames() {
return NAMES;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,30 +29,33 @@
import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonFormatTypes;

import gov.nist.secauto.metaschema.core.datatype.AbstractDataTypeAdapter;
import gov.nist.secauto.metaschema.core.metapath.MetapathConstants;
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 javax.xml.namespace.QName;

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

public class DateTimeWithTZAdapter
extends AbstractDataTypeAdapter<ZonedDateTime, IDateTimeItem> {
@NonNull
private static final List<String> NAMES = ObjectUtils.notNull(
private static final List<QName> NAMES = ObjectUtils.notNull(
List.of(
"date-time-with-timezone",
new QName(MetapathConstants.NS_METAPATH.toASCIIString(), "date-time-with-timezone"),
// for backwards compatibility with original type name
"dateTime-with-timezone"));
new QName(MetapathConstants.NS_METAPATH.toASCIIString(), "dateTime-with-timezone")));

DateTimeWithTZAdapter() {
super(ZonedDateTime.class);
}

@Override
public List<String> getNames() {
public List<QName> getNames() {
return NAMES;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import com.fasterxml.jackson.databind.jsonFormatVisitors.JsonFormatTypes;

import gov.nist.secauto.metaschema.core.datatype.AbstractDataTypeAdapter;
import gov.nist.secauto.metaschema.core.metapath.MetapathConstants;
import gov.nist.secauto.metaschema.core.metapath.item.atomic.IDateItem;
import gov.nist.secauto.metaschema.core.util.ObjectUtils;

Expand All @@ -38,14 +39,16 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.xml.namespace.QName;

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

public class DateWithTZAdapter
extends AbstractDataTypeAdapter<ZonedDateTime, IDateItem> {
@NonNull
private static final List<String> NAMES = ObjectUtils.notNull(
private static final List<QName> NAMES = ObjectUtils.notNull(
List.of(
"date-with-timezone"));
new QName(MetapathConstants.NS_METAPATH.toASCIIString(), "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]))"
Expand All @@ -59,7 +62,7 @@ public class DateWithTZAdapter
}

@Override
public List<String> getNames() {
public List<QName> getNames() {
return NAMES;
}

Expand Down
Loading

0 comments on commit 1fbc504

Please sign in to comment.