public class XmlDataImporter extends PropertyDataImporter
<ArbitraryRoot>
<EntityName primitivePropertyName="... value ...">
<anotherPrimitivePropertyName>... value ...</anotherPrimitivePropertyName>
<entityProperty>
<ReferencedEntityName>
... properties ...
</ReferencedEntityName>
</entityProperty>
<anotherEntityProperty>
<ReferencedEntityName reference="true">
... unique properties necessary to identify the entity in the EntityRegistration
...
</ReferencedEntityName>
</anotherEntityProperty>
<embeddedProperty>
... properties ...
</embeddedProperty>
</EntityName>
<EntityName>...</EntityName>
...
</ArbitraryRoot>
Modifier and Type | Field and Description |
---|---|
private GeneratorContext |
context
The generation context.
|
private EntityRegistration |
entityRegistration
Contains all entities, that have a unique property.
|
private static QName |
KEY_ATTRIBUTE
Name of the attribute that contains the key for a map entry (the value is always the content of the element).
|
private static Consumer<Object> |
NOOP_CONSUMER |
private static QName |
REFERENCE_ATTRIBUTE
Name of the attribute that indicates that an entity property is only the reference to an existing entity.
|
Constructor and Description |
---|
XmlDataImporter()
Creates a new instance of
XmlDataImporter . |
XmlDataImporter(GeneratorContext context,
EntityRegistration entityRegistration) |
Modifier and Type | Method and Description |
---|---|
private static void |
check(boolean condition,
XMLEvent event,
String errorMessage,
Object... parameters) |
private static void |
checkEndElement(XMLEvent element,
String expectedName) |
private static <T> T |
checkExists(T value,
XMLEvent event,
String errorMessage,
Object... parameters) |
private <T> T |
convert(XMLEvent element,
String propertyName,
String content,
Class<T> type) |
protected <E> void |
importAttribute(XMLEventReader reader,
StartElement element,
E entity,
Map<String,? extends Property<? super E,?>> properties,
Attribute attribute)
Imports an attribute from XML.
|
protected <E> void |
importElement(XMLEventReader reader,
StartElement entityElement,
E entity,
Map<String,? extends Property<? super E,?>> properties,
StartElement propertyElement)
Imports an element from XML for an entity.
|
protected <E,T> void |
importEmbeddedProperty(XMLEventReader reader,
StartElement propertyElement,
E entity,
EmbeddedProperty<? super E,T> property)
Imports a property which contains an
embedded object from XML. |
protected <E> void |
importEntity(XMLEventReader reader,
StartElement element,
EntityClass<E> classDescription,
boolean reference,
Consumer<E> onImport)
Imports an entity and invokes a function as soon the entity is imported.
|
protected <E,T> void |
importEntityProperty(XMLEventReader reader,
E entity,
EntityProperty<E,T> property)
Imports a property which contains an
entity from XML. |
List<Object> |
importFile(DataFile file)
Imports all entites found in the given XML file.
|
protected <E,T> void |
importPluralProperty(XMLEventReader reader,
E entity,
PluralProperty<? super E,?,T> property)
Imports a
Collection or Map property from XML. |
protected <E,T> void |
importPrimitiveProperty(XMLEventReader reader,
E entity,
PrimitiveProperty<E,T> property)
Imports the given property from XML.
|
protected <E,T> void |
importPrimitiveProperty(XMLEventReader reader,
E entity,
PrimitiveProperty<E,T> property,
T value)
Imports the given value for the given property from XML.
|
protected <E> void |
importProperties(XMLEventReader reader,
StartElement element,
Map<String,? extends Property<? super E,?>> properties,
E entity)
Imports the given set of properties from the XML file for the given entity.
|
protected <E,T> void |
importProperty(XMLEventReader reader,
StartElement element,
Property<? super E,T> property,
E entity)
Imports one property from the XML file.
|
private static boolean |
isEntityReference(StartElement element) |
private static XMLEvent |
nextEvent(XMLEventReader reader) |
private static boolean |
nextEventIsStartElement(XMLEventReader reader) |
private static String |
readCharacters(XMLEventReader reader,
String endElement) |
private static XMLEvent |
skipWhitespaces(XMLEventReader reader) |
private static DataImportException |
wrapRuntimeException(RuntimeException e,
DataFile file,
XMLEvent nextEvent) |
private static DataImportException |
wrapStreamException(XMLStreamException e,
DataFile file) |
addConverter, findConverter
private static final QName REFERENCE_ATTRIBUTE
private static final QName KEY_ATTRIBUTE
private final GeneratorContext context
private final EntityRegistration entityRegistration
public XmlDataImporter()
XmlDataImporter
.
Uses its own GeneratorContext
and EntityRegistration
, so only use as standalone importer.@ConstructorProperties(value={"context","entityRegistration"}) @Generated(value="lombok") public XmlDataImporter(GeneratorContext context, EntityRegistration entityRegistration)
private static void check(boolean condition, XMLEvent event, String errorMessage, Object... parameters) throws XMLStreamException
XMLStreamException
private static void checkEndElement(XMLEvent element, String expectedName) throws XMLStreamException
XMLStreamException
private static <T> T checkExists(T value, XMLEvent event, String errorMessage, Object... parameters) throws XMLStreamException
XMLStreamException
private static boolean isEntityReference(StartElement element)
private static XMLEvent nextEvent(XMLEventReader reader) throws XMLStreamException
XMLStreamException
private static boolean nextEventIsStartElement(XMLEventReader reader) throws XMLStreamException
XMLStreamException
private static String readCharacters(XMLEventReader reader, String endElement) throws XMLStreamException
XMLStreamException
private static XMLEvent skipWhitespaces(XMLEventReader reader) throws XMLStreamException
XMLStreamException
private static DataImportException wrapRuntimeException(RuntimeException e, DataFile file, XMLEvent nextEvent)
private static DataImportException wrapStreamException(XMLStreamException e, DataFile file)
private <T> T convert(XMLEvent element, String propertyName, String content, Class<T> type) throws XMLStreamException
XMLStreamException
protected <E> void importAttribute(XMLEventReader reader, StartElement element, E entity, Map<String,? extends Property<? super E,?>> properties, Attribute attribute) throws XMLStreamException
E
- the type of the imported entityreader
- the XML streamelement
- the element of the entityentity
- the imported entityproperties
- all properties known for the entity typeattribute
- the imported attributeXMLStreamException
- if the XML is invalidprotected <E> void importElement(XMLEventReader reader, StartElement entityElement, E entity, Map<String,? extends Property<? super E,?>> properties, StartElement propertyElement) throws XMLStreamException
E
- the type of the imported entityreader
- the XML streamentityElement
- the element of the entityentity
- the imported entityproperties
- all properties known for the entity typepropertyElement
- the imported elementXMLStreamException
- if the XML is invalidprotected <E,T> void importEmbeddedProperty(XMLEventReader reader, StartElement propertyElement, E entity, EmbeddedProperty<? super E,T> property) throws XMLStreamException
embedded object
from XML.E
- the type of the current entityT
- the type of the embedded objectreader
- the XML streampropertyElement
- the current element that started the propertyentity
- the imported entityproperty
- the imported property of the entityXMLStreamException
- if the XML is invalidprotected <E> void importEntity(XMLEventReader reader, StartElement element, EntityClass<E> classDescription, boolean reference, Consumer<E> onImport) throws XMLStreamException
reference
, the given handler is invoked as soon the entity is
registered
.E
- the type of the imported entityreader
- the XML streamelement
- the element that started the importclassDescription
- the description of the type of the entityreference
- indicates that the imported entity is just a reference to an existing entityonImport
- the handler to call as soon as the entity is foundXMLStreamException
- if there is an error during importprotected <E,T> void importEntityProperty(XMLEventReader reader, E entity, EntityProperty<E,T> property) throws XMLStreamException
entity
from XML.E
- the type of the current entityT
- the type of the property (the target entity)reader
- the XML streamentity
- the imported entityproperty
- the imported property of the entityXMLStreamException
- if the XML is invalidpublic List<Object> importFile(DataFile file) throws IOException, DataImportException
file
- the fileIOException
- if there was a problem when accessing the fileDataImportException
- if the XML or its contents was invalidprotected <E,T> void importPluralProperty(XMLEventReader reader, E entity, PluralProperty<? super E,?,T> property) throws XMLStreamException
Collection
or Map
property from XML.E
- the type of the current entityT
- the type of the embedded objectreader
- the XML streamentity
- the imported entityproperty
- the imported property of the entityXMLStreamException
- if the XML is invalidprotected <E,T> void importPrimitiveProperty(XMLEventReader reader, E entity, PrimitiveProperty<E,T> property) throws XMLStreamException
importPrimitiveProperty(XMLEventReader, Object, PrimitiveProperty, Object)
.reader
- the XML streamentity
- the imported entityproperty
- the imported propertyXMLStreamException
- if the XML is invalidprotected <E,T> void importPrimitiveProperty(XMLEventReader reader, E entity, PrimitiveProperty<E,T> property, T value)
reader
- the XML streamentity
- the imported entityproperty
- the imported propertyvalue
- the imported valueprotected <E> void importProperties(XMLEventReader reader, StartElement element, Map<String,? extends Property<? super E,?>> properties, E entity) throws XMLStreamException
E
- the type of the entityreader
- the XML streamelement
- the element of the entityproperties
- all properties known for the entity typeentity
- the entity to importXMLStreamException
- if the XML is invalidprotected <E,T> void importProperty(XMLEventReader reader, StartElement element, Property<? super E,T> property, E entity) throws XMLStreamException
E
- the type of the imported entityreader
- the XML streamelement
- the element that triggered the import of the propertyproperty
- the imported propertyentity
- the imported entityXMLStreamException
- if the XML is invalidCopyright © 2022 fastnate.org. All rights reserved.