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
  • Field Details

    • REFERENCE_ATTRIBUTE

      private static final QName REFERENCE_ATTRIBUTE
      Name of the attribute that indicates that an entity property is only the reference to an existing entity.
    • KEY_ATTRIBUTE

      private static final QName 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

      private static final Consumer<Object> NOOP_CONSUMER
    • context

      private final org.fastnate.generator.context.GeneratorContext context
      The generation context.
    • entityRegistration

      private final EntityRegistration entityRegistration
      Contains all entities, that have a unique property.
  • 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

    • 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

      private static boolean isEntityReference(StartElement element)
    • nextEvent

      private static XMLEvent nextEvent(XMLEventReader reader) throws XMLStreamException
      Throws:
      XMLStreamException
    • nextEventIsStartElement

      private static boolean nextEventIsStartElement(XMLEventReader reader) throws XMLStreamException
      Throws:
      XMLStreamException
    • readCharacters

      private static String readCharacters(XMLEventReader reader, String endElement) throws XMLStreamException
      Throws:
      XMLStreamException
    • skipWhitespaces

      private static XMLEvent skipWhitespaces(XMLEventReader reader) throws XMLStreamException
      Throws:
      XMLStreamException
    • wrapRuntimeException

      private static DataImportException wrapRuntimeException(RuntimeException e, DataFile file, XMLEvent nextEvent)
    • wrapStreamException

      private static DataImportException wrapStreamException(XMLStreamException e, DataFile file)
    • 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,?>> 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