Package org.fastnate.data.xml
Class XmlDataImporter
java.lang.Object
org.fastnate.data.properties.PropertyDataImporter
org.fastnate.data.xml.XmlDataImporter
Imports entities from a set of XML files in the following format:
<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>
- Author:
- Tobias Liefke
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final org.fastnate.generator.context.GeneratorContextThe generation context.private final EntityRegistrationContains all entities, that have a unique property.private static final QNameName of the attribute that contains the key for a map entry (the value is always the content of the element).private static final QNameName of the attribute that indicates that an entity property is only the reference to an existing entity. -
Constructor Summary
ConstructorsConstructorDescriptionCreates a new instance ofXmlDataImporter.XmlDataImporter(org.fastnate.generator.context.GeneratorContext context, EntityRegistration entityRegistration) Creates a newXmlDataImporterinstance. -
Method Summary
Modifier and TypeMethodDescriptionprivate static voidprivate static voidcheckEndElement(XMLEvent element, String expectedName) private static <T> TcheckExists(T value, XMLEvent event, String errorMessage, Object... parameters) private <T> Tprotected <E> voidimportAttribute(XMLEventReader reader, StartElement element, E entity, Map<String, ? extends org.fastnate.generator.context.Property<? super E, ?>> properties, Attribute attribute) Imports an attribute from XML.protected <E> voidimportElement(XMLEventReader reader, StartElement entityElement, E entity, Map<String, ? extends org.fastnate.generator.context.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, org.fastnate.generator.context.EmbeddedProperty<? super E, T> property) Imports a property which contains anembedded objectfrom XML.protected <E> voidimportEntity(XMLEventReader reader, StartElement element, org.fastnate.generator.context.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, org.fastnate.generator.context.EntityProperty<E, T> property) Imports a property which contains anentityfrom XML.importFile(DataFile file) Imports all entites found in the given XML file.protected <E,T> void importPluralProperty(XMLEventReader reader, E entity, org.fastnate.generator.context.PluralProperty<? super E, ?, T> property) Imports aCollectionorMapproperty from XML.protected <E,T> void importPrimitiveProperty(XMLEventReader reader, E entity, org.fastnate.generator.context.PrimitiveProperty<E, T> property) Imports the given property from XML.protected <E,T> void importPrimitiveProperty(XMLEventReader reader, E entity, org.fastnate.generator.context.PrimitiveProperty<E, T> property, T value) Imports the given value for the given property from XML.protected <E> voidimportProperties(XMLEventReader reader, StartElement element, Map<String, ? extends org.fastnate.generator.context.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, org.fastnate.generator.context.Property<? super E, T> property, E entity) Imports one property from the XML file.private static booleanisEntityReference(StartElement element) private static XMLEventnextEvent(XMLEventReader reader) private static booleanprivate static StringreadCharacters(XMLEventReader reader, String endElement) private static XMLEventskipWhitespaces(XMLEventReader reader) private static DataImportExceptionwrapRuntimeException(RuntimeException e, DataFile file, XMLEvent nextEvent) private static DataImportExceptionMethods inherited from class org.fastnate.data.properties.PropertyDataImporter
addConverter, findConverter
-
Field Details
-
REFERENCE_ATTRIBUTE
Name of the attribute that indicates that an entity property is only the reference to an existing entity. -
KEY_ATTRIBUTE
Name of the attribute that contains the key for a map entry (the value is always the content of the element). -
NOOP_CONSUMER
-
context
private final org.fastnate.generator.context.GeneratorContext contextThe generation context. -
entityRegistration
Contains all entities, that have a unique property.
-
-
Constructor Details
-
XmlDataImporter
public XmlDataImporter()Creates a new instance ofXmlDataImporter. Uses its ownGeneratorContextandEntityRegistration, so only use as standalone importer. -
XmlDataImporter
public XmlDataImporter(org.fastnate.generator.context.GeneratorContext context, EntityRegistration entityRegistration) Creates a newXmlDataImporterinstance.- Parameters:
context- The generation context.entityRegistration- Contains all entities, that have a unique property.
-
-
Method Details
-
check
private static void check(boolean condition, XMLEvent event, String errorMessage, Object... parameters) throws XMLStreamException - Throws:
XMLStreamException
-
checkEndElement
private static void checkEndElement(XMLEvent element, String expectedName) throws XMLStreamException - Throws:
XMLStreamException
-
checkExists
private static <T> T checkExists(T value, XMLEvent event, String errorMessage, Object... parameters) throws XMLStreamException - Throws:
XMLStreamException
-
isEntityReference
-
nextEvent
- Throws:
XMLStreamException
-
nextEventIsStartElement
- Throws:
XMLStreamException
-
readCharacters
private static String readCharacters(XMLEventReader reader, String endElement) throws XMLStreamException - Throws:
XMLStreamException
-
skipWhitespaces
- Throws:
XMLStreamException
-
wrapRuntimeException
private static DataImportException wrapRuntimeException(RuntimeException e, DataFile file, XMLEvent nextEvent) -
wrapStreamException
-
convert
private <T> T convert(XMLEvent element, String propertyName, String content, Class<T> type) throws XMLStreamException - Throws:
XMLStreamException
-
importAttribute
protected <E> void importAttribute(XMLEventReader reader, StartElement element, E entity, Map<String, ? extends org.fastnate.generator.context.Property<? super E, throws XMLStreamException?>> properties, Attribute attribute) Imports an attribute from XML.- Type Parameters:
E- the type of the imported entity- Parameters:
reader- the XML streamelement- the element of the entityentity- the imported entityproperties- all properties known for the entity typeattribute- the imported attribute- Throws:
XMLStreamException- if the XML is invalid
-
importElement
protected <E> void importElement(XMLEventReader reader, StartElement entityElement, E entity, Map<String, ? extends org.fastnate.generator.context.Property<? super E, throws XMLStreamException?>> properties, StartElement propertyElement) Imports an element from XML for an entity.- Type Parameters:
E- the type of the imported entity- Parameters:
reader- the XML streamentityElement- the element of the entityentity- the imported entityproperties- all properties known for the entity typepropertyElement- the imported element- Throws:
XMLStreamException- if the XML is invalid
-
importEmbeddedProperty
protected <E,T> void importEmbeddedProperty(XMLEventReader reader, StartElement propertyElement, E entity, org.fastnate.generator.context.EmbeddedProperty<? super E, T> property) throws XMLStreamExceptionImports a property which contains anembedded objectfrom XML.- Type Parameters:
E- the type of the current entityT- the type of the embedded object- Parameters:
reader- the XML streampropertyElement- the current element that started the propertyentity- the imported entityproperty- the imported property of the entity- Throws:
XMLStreamException- if the XML is invalid
-
importEntity
protected <E> void importEntity(XMLEventReader reader, StartElement element, org.fastnate.generator.context.EntityClass<E> classDescription, boolean reference, Consumer<E> onImport) throws XMLStreamException Imports an entity and invokes a function as soon the entity is imported. If the entity is only areference, the given handler is invoked as soon the entity isregistered.- Type Parameters:
E- the type of the imported entity- Parameters:
reader- 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 found- Throws:
XMLStreamException- if there is an error during import
-
importEntityProperty
protected <E,T> void importEntityProperty(XMLEventReader reader, E entity, org.fastnate.generator.context.EntityProperty<E, T> property) throws XMLStreamExceptionImports a property which contains anentityfrom XML.- Type Parameters:
E- the type of the current entityT- the type of the property (the target entity)- Parameters:
reader- the XML streamentity- the imported entityproperty- the imported property of the entity- Throws:
XMLStreamException- if the XML is invalid
-
importFile
Imports all entites found in the given XML file. The root element in the XML is ignored (it's only used as a container).- Parameters:
file- the file- Returns:
- the imported entities
- Throws:
IOException- if there was a problem when accessing the fileDataImportException- if the XML or its contents was invalid
-
importPluralProperty
protected <E,T> void importPluralProperty(XMLEventReader reader, E entity, org.fastnate.generator.context.PluralProperty<? super E, ?, throws XMLStreamExceptionT> property) Imports aCollectionorMapproperty from XML.- Type Parameters:
E- the type of the current entityT- the type of the embedded object- Parameters:
reader- the XML streamentity- the imported entityproperty- the imported property of the entity- Throws:
XMLStreamException- if the XML is invalid
-
importPrimitiveProperty
protected <E,T> void importPrimitiveProperty(XMLEventReader reader, E entity, org.fastnate.generator.context.PrimitiveProperty<E, T> property) throws XMLStreamExceptionImports the given property from XML. The default implementation reads and converts the value and callsimportPrimitiveProperty(XMLEventReader, Object, PrimitiveProperty, Object).- Parameters:
reader- the XML streamentity- the imported entityproperty- the imported property- Throws:
XMLStreamException- if the XML is invalid
-
importPrimitiveProperty
protected <E,T> void importPrimitiveProperty(XMLEventReader reader, E entity, org.fastnate.generator.context.PrimitiveProperty<E, T> property, T value) Imports the given value for the given property from XML.- Parameters:
reader- the XML streamentity- the imported entityproperty- the imported propertyvalue- the imported value
-
importProperties
protected <E> void importProperties(XMLEventReader reader, StartElement element, Map<String, ? extends org.fastnate.generator.context.Property<? super E, throws XMLStreamException?>> properties, E entity) Imports the given set of properties from the XML file for the given entity.- Type Parameters:
E- the type of the entity- Parameters:
reader- the XML streamelement- the element of the entityproperties- all properties known for the entity typeentity- the entity to import- Throws:
XMLStreamException- if the XML is invalid
-
importProperty
protected <E,T> void importProperty(XMLEventReader reader, StartElement element, org.fastnate.generator.context.Property<? super E, T> property, E entity) throws XMLStreamExceptionImports one property from the XML file.- Type Parameters:
E- the type of the imported entity- Parameters:
reader- the XML streamelement- the element that triggered the import of the propertyproperty- the imported propertyentity- the imported entity- Throws:
XMLStreamException- if the XML is invalid
-