Class AbstractDataTypeAdapter<TYPE,ITEM_TYPE extends IAnyAtomicItem>
- java.lang.Object
-
- gov.nist.secauto.metaschema.core.datatype.AbstractDataTypeAdapter<TYPE,ITEM_TYPE>
-
- Type Parameters:
TYPE
- the raw Java type this adapter supportsITEM_TYPE
- the metapath item type corresponding to the raw Java type supported by the adapter
- All Implemented Interfaces:
IDataTypeAdapter<TYPE>
- Direct Known Subclasses:
AbstractCustomJavaDataTypeAdapter
,AbstractIntegerAdapter
,AbstractStringAdapter
,Base64Adapter
,BooleanAdapter
,DateTimeWithTZAdapter
,DateWithTZAdapter
,DayTimeAdapter
,DecimalAdapter
,IPv4AddressAdapter
,IPv6AddressAdapter
,UriAdapter
,UriReferenceAdapter
,UuidAdapter
,YearMonthAdapter
public abstract class AbstractDataTypeAdapter<TYPE,ITEM_TYPE extends IAnyAtomicItem> extends Object implements IDataTypeAdapter<TYPE>
Provides a basic Java type adapter implementation. This implementation should be the parent class of all Java type adapter implementations.
-
-
Field Summary
Fields Modifier and Type Field Description static String
DEFAULT_JSON_FIELD_NAME
-
Constructor Summary
Constructors Modifier Constructor Description protected
AbstractDataTypeAdapter(Class<TYPE> clazz)
Construct a new Java type adapter for a provided class.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description String
asString(Object value)
Gets the value as a string suitable for writing as text.boolean
canHandleQName(QName nextQName)
Determines if adapter can parse the next element.ITEM_TYPE
cast(IAnyAtomicItem item)
Cast the provided item to an item of this type, if possible.protected ITEM_TYPE
castInternal(IAnyAtomicItem item)
Attempt to cast the provided item to this adapter's item type.String
getDefaultJsonValueKey()
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.abstract Class<ITEM_TYPE>
getItemClass()
Get the java type of the associated item.Class<TYPE>
getJavaClass()
Get the Java class supported by this adapter.boolean
isParsingStartElement()
Indicates if the adapter will parse theXMLStreamConstants.START_ELEMENT
before parsing the value data.boolean
isUnrappedValueAllowedInXml()
Determines if the data type's value is allowed to be unwrapped in XML when the value is a field value.boolean
isXmlMixed()
Determines if the datatype uses mixed text and element content in XML.abstract ITEM_TYPE
newItem(Object value)
Construct a new item of this type using the provided value.TYPE
parse(com.fasterxml.jackson.core.JsonParser parser)
This default implementation will parse the value as a string and delegate to the string-based parsing method.TYPE
parse(org.codehaus.stax2.XMLEventReader2 eventReader)
This method is expected to parse content starting at the next event.TYPE
toValue(Object value)
Casts the provided value to the type associated with this adapter.void
writeJsonValue(Object value, com.fasterxml.jackson.core.JsonGenerator generator)
Writes the provided Java class instance as a JSON/YAML field value.void
writeXmlValue(Object value, QName parentName, org.codehaus.stax2.XMLStreamWriter2 writer)
Writes the provided Java class instance data as XML.void
writeXmlValue(Object value, StartElement parent, org.codehaus.stax2.evt.XMLEventFactory2 eventFactory, XMLEventWriter eventWriter)
Writes the provided Java class instance data as XML.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface gov.nist.secauto.metaschema.core.datatype.IDataTypeAdapter
copy, getJsonRawType, getNames, getPreferredName, isAtomic, parse, parseAndSupply, parseAndSupply, parseAndSupply
-
-
-
-
Field Detail
-
DEFAULT_JSON_FIELD_NAME
public static final String DEFAULT_JSON_FIELD_NAME
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
AbstractDataTypeAdapter
protected AbstractDataTypeAdapter(@NonNull Class<TYPE> clazz)
Construct a new Java type adapter for a provided class.- Parameters:
clazz
- the Java type this adapter supports
-
-
Method Detail
-
toValue
public TYPE toValue(Object value)
Description copied from interface:IDataTypeAdapter
Casts the provided value to the type associated with this adapter.- Specified by:
toValue
in interfaceIDataTypeAdapter<TYPE>
- Parameters:
value
- a value of the provided type- Returns:
- the typed value
-
getJavaClass
public Class<TYPE> getJavaClass()
Description copied from interface:IDataTypeAdapter
Get the Java class supported by this adapter.- Specified by:
getJavaClass
in interfaceIDataTypeAdapter<TYPE>
- Returns:
- the Java class
-
isParsingStartElement
public boolean isParsingStartElement()
Description copied from interface:IDataTypeAdapter
Indicates if the adapter will parse theXMLStreamConstants.START_ELEMENT
before parsing the value data.- Specified by:
isParsingStartElement
in interfaceIDataTypeAdapter<TYPE>
- Returns:
true
if the adapter requires the start element for parsing, orfalse
otherwise
-
canHandleQName
public boolean canHandleQName(QName nextQName)
Description copied from interface:IDataTypeAdapter
Determines if adapter can parse the next element. The next element'sQName
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.
- Specified by:
canHandleQName
in interfaceIDataTypeAdapter<TYPE>
- Parameters:
nextQName
- the next element's namespace-qualified name- Returns:
true
if the adapter will parse the element, orfalse
otherwise
-
getDefaultJsonValueKey
public String getDefaultJsonValueKey()
Description copied from interface:IDataTypeAdapter
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.- Specified by:
getDefaultJsonValueKey
in interfaceIDataTypeAdapter<TYPE>
- Returns:
- the default field name to use
-
isUnrappedValueAllowedInXml
public boolean isUnrappedValueAllowedInXml()
Description copied from interface:IDataTypeAdapter
Determines if the data type's value is allowed to be unwrapped in XML when the value is a field value.- Specified by:
isUnrappedValueAllowedInXml
in interfaceIDataTypeAdapter<TYPE>
- Returns:
true
if allowed, orfalse
otherwise.
-
isXmlMixed
public boolean isXmlMixed()
Description copied from interface:IDataTypeAdapter
Determines if the datatype uses mixed text and element content in XML.- Specified by:
isXmlMixed
in interfaceIDataTypeAdapter<TYPE>
- Returns:
true
if the datatype uses mixed text and element content in XML, orfalse
otherwise
-
parse
public TYPE parse(org.codehaus.stax2.XMLEventReader2 eventReader) throws IOException
Description copied from interface:IDataTypeAdapter
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 anyXMLStreamConstants.END_ELEMENT
that corresponds to anXMLStreamConstants.START_ELEMENT
parsed by this adapter.If
IDataTypeAdapter.isParsingStartElement()
returnstrue
, then the first event to parse will be theXMLStreamConstants.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 correspondingXMLStreamConstants.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.
- Specified by:
parse
in interfaceIDataTypeAdapter<TYPE>
- Parameters:
eventReader
- the XML parser used to read the parsed value- Returns:
- the parsed value
- Throws:
IOException
- if a parsing error occurs
-
parse
public TYPE parse(com.fasterxml.jackson.core.JsonParser parser) throws IOException
This default implementation will parse the value as a string and delegate to the string-based parsing method.- Specified by:
parse
in interfaceIDataTypeAdapter<TYPE>
- Parameters:
parser
- the JSON parser used to read the parsed value- Returns:
- the parsed value
- Throws:
IOException
- if a parsing error occurs
-
asString
public String asString(Object value)
Description copied from interface:IDataTypeAdapter
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 anUnsupportedOperationException
when this is called.- Specified by:
asString
in interfaceIDataTypeAdapter<TYPE>
- Parameters:
value
- the data to formatted as a string- Returns:
- a string
-
writeXmlValue
public void writeXmlValue(Object value, StartElement parent, org.codehaus.stax2.evt.XMLEventFactory2 eventFactory, XMLEventWriter eventWriter) throws IOException, XMLStreamException
Description copied from interface:IDataTypeAdapter
Writes the provided Java class instance data as XML. The parent element information is provided as aStartElement
event, which allows namespace information to be obtained from the parent element using theStartElement.getName()
andStartElement.getNamespaceContext()
methods, which can be used when writing the provided instance value.- Specified by:
writeXmlValue
in interfaceIDataTypeAdapter<TYPE>
- Parameters:
value
- theField
instance value to writeparent
- theStartElement
XML event that is the parent of the data to writeeventFactory
- the XML event factory used to generate XML writing eventseventWriter
- the XML writer used to output XML as events- Throws:
IOException
- if an unexpected error occurred while writing to the output streamXMLStreamException
- if an unexpected error occurred while processing the XML output
-
writeXmlValue
public void writeXmlValue(Object value, QName parentName, org.codehaus.stax2.XMLStreamWriter2 writer) throws XMLStreamException
Description copied from interface:IDataTypeAdapter
Writes the provided Java class instance data as XML. The parent element information is provided as an XMLQName
, which allows namespace information to be obtained from the parent element. Additional namespace information can be gathered using theXMLStreamWriter.getNamespaceContext()
method, which can be used when writing the provided instance value.- Specified by:
writeXmlValue
in interfaceIDataTypeAdapter<TYPE>
- Parameters:
value
- theField
instance value to writeparentName
- the qualified name of the XML data's parent elementwriter
- the XML writer used to output the XML data- Throws:
XMLStreamException
- if an unexpected error occurred while processing the XML output
-
writeJsonValue
public void writeJsonValue(Object value, com.fasterxml.jackson.core.JsonGenerator generator) throws IOException
Description copied from interface:IDataTypeAdapter
Writes the provided Java class instance as a JSON/YAML field value.- Specified by:
writeJsonValue
in interfaceIDataTypeAdapter<TYPE>
- Parameters:
value
- theField
instance value to writegenerator
- the JSON/YAML writer used to output the JSON/YAML data- Throws:
IOException
- if an unexpected error occurred while writing the JSON/YAML output
-
getItemClass
public abstract Class<ITEM_TYPE> getItemClass()
Description copied from interface:IDataTypeAdapter
Get the java type of the associated item.- Specified by:
getItemClass
in interfaceIDataTypeAdapter<TYPE>
- Returns:
- the java associated item type
-
newItem
public abstract ITEM_TYPE newItem(Object value)
Description copied from interface:IDataTypeAdapter
Construct a new item of this type using the provided value.- Specified by:
newItem
in interfaceIDataTypeAdapter<TYPE>
- Parameters:
value
- the item's value- Returns:
- a new item
-
cast
public ITEM_TYPE cast(IAnyAtomicItem item)
Description copied from interface:IDataTypeAdapter
Cast the provided item to an item of this type, if possible.- Specified by:
cast
in interfaceIDataTypeAdapter<TYPE>
- Parameters:
item
- the atomic item to cast- Returns:
- an atomic item of this type
-
castInternal
@NonNull protected ITEM_TYPE castInternal(@NonNull IAnyAtomicItem item)
Attempt to cast the provided item to this adapter's item type.The default implementation of this will attempt to parse the provided item as a string using the
IDataTypeAdapter.parse(String)
method. If this behavior is undesirable, then a subclass should override this method.- Parameters:
item
- the item to cast- Returns:
- the item casted to this adapter's item type
- Throws:
InvalidValueForCastFunctionException
- if the casting of the item is not possible because the item represents an invalid value for this adapter's item type
-
-