Class XmlDataImporter


public class XmlDataImporter extends PropertyDataImporter
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
  • Constructor Details

    • XmlDataImporter

      public XmlDataImporter()
      Creates a new instance of XmlDataImporter. Uses its own GeneratorContext and EntityRegistration, so only use as standalone importer.
    • XmlDataImporter

      public XmlDataImporter(org.fastnate.generator.context.GeneratorContext context, EntityRegistration entityRegistration)
      Creates a new XmlDataImporter instance.
      Parameters:
      context - The generation context.
      entityRegistration - Contains all entities, that have a unique property.
  • Method Details

    • importAttribute

      protected <E> void importAttribute(XMLEventReader reader, StartElement element, E entity, Map<String,? extends org.fastnate.generator.context.Property<? super E,?>> properties, Attribute attribute) throws XMLStreamException
      Imports an attribute from XML.
      Type Parameters:
      E - the type of the imported entity
      Parameters:
      reader - the XML stream
      element - the element of the entity
      entity - the imported entity
      properties - all properties known for the entity type
      attribute - 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,?>> properties, StartElement propertyElement) throws XMLStreamException
      Imports an element from XML for an entity.
      Type Parameters:
      E - the type of the imported entity
      Parameters:
      reader - the XML stream
      entityElement - the element of the entity
      entity - the imported entity
      properties - all properties known for the entity type
      propertyElement - 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 XMLStreamException
      Imports a property which contains an embedded object from XML.
      Type Parameters:
      E - the type of the current entity
      T - the type of the embedded object
      Parameters:
      reader - the XML stream
      propertyElement - the current element that started the property
      entity - the imported entity
      property - 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 a reference, the given handler is invoked as soon the entity is registered.
      Type Parameters:
      E - the type of the imported entity
      Parameters:
      reader - the XML stream
      element - the element that started the import
      classDescription - the description of the type of the entity
      reference - indicates that the imported entity is just a reference to an existing entity
      onImport - 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 XMLStreamException
      Imports a property which contains an entity from XML.
      Type Parameters:
      E - the type of the current entity
      T - the type of the property (the target entity)
      Parameters:
      reader - the XML stream
      entity - the imported entity
      property - the imported property of the entity
      Throws:
      XMLStreamException - if the XML is invalid
    • importFile

      public List<Object> importFile(DataFile file) throws IOException, DataImportException
      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 file
      DataImportException - 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,?,T> property) throws XMLStreamException
      Imports a Collection or Map property from XML.
      Type Parameters:
      E - the type of the current entity
      T - the type of the embedded object
      Parameters:
      reader - the XML stream
      entity - the imported entity
      property - 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 XMLStreamException
      Imports the given property from XML. The default implementation reads and converts the value and calls importPrimitiveProperty(XMLEventReader, Object, PrimitiveProperty, Object).
      Parameters:
      reader - the XML stream
      entity - the imported entity
      property - 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 stream
      entity - the imported entity
      property - the imported property
      value - the imported value
    • importProperties

      protected <E> void importProperties(XMLEventReader reader, StartElement element, Map<String,? extends org.fastnate.generator.context.Property<? super E,?>> properties, E entity) throws XMLStreamException
      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 stream
      element - the element of the entity
      properties - all properties known for the entity type
      entity - 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 XMLStreamException
      Imports one property from the XML file.
      Type Parameters:
      E - the type of the imported entity
      Parameters:
      reader - the XML stream
      element - the element that triggered the import of the property
      property - the imported property
      entity - the imported entity
      Throws:
      XMLStreamException - if the XML is invalid