Class PuzzlePieceSet

java.lang.Object
schema2template.grammar.PuzzlePieceSet
All Implemented Interfaces:
Iterable<PuzzlePiece>, Collection<PuzzlePiece>, PuzzleComponent

public class PuzzlePieceSet extends Object implements PuzzleComponent, Collection<PuzzlePiece>
Collection Class for RelaxNG definitions of an Element, Attribute, Value or Datatype.

Conventions:

  • PuzzlePiece sorting is done by ns:local tag names as first key and hashCode as second key (see class PuzzlePiece).
  • Since it is a Collection, PuzzlePieceSet is not meant to be used in a Collection. So equals(o) and hashCode() are not overwritten
  • All returned PuzzlePieceSet objects are immutable to protect them against naive usage in velocity templates
  • Constructor Details

    • PuzzlePieceSet

      public PuzzlePieceSet()
    • PuzzlePieceSet

      public PuzzlePieceSet(Collection<PuzzlePiece> c)
  • Method Details

    • equals

      public boolean equals(Object o)
      Specified by:
      equals in interface Collection<PuzzlePiece>
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Specified by:
      hashCode in interface Collection<PuzzlePiece>
      Overrides:
      hashCode in class Object
    • makeImmutable

      public void makeImmutable()
      Make PuzzlePieceSet immutable. Cannot be undone.

      Template Usage: Not for use in templates as all PuzzlePieceSet already have been made immutable.

    • withoutMultiples

      public PuzzlePieceSet withoutMultiples()
      Returns new PuzzlePieceSet containing the elements of this PuzzlePieceSet, but restricted to one PuzzlePiece per Name.

      Template Usage: #foreach ($element in $elements.withoutMultiples())

      Returns:
      new PuzzlePieceSet
    • without

      public PuzzlePieceSet without(PuzzleComponent removeAll)
      Returns new PuzzlePieceSet containing the elements of this PuzzlePieceSet, but without the elements of the parameter removeAll

      Template Usage: #set ($non_base_attributes = $element.getAttributes().without($baseclass.getAttributes())

      Parameters:
      removeAll - PuzzleComponent which (or which elements) should be removed from the new PuzzlePieceSet
      Returns:
      new PuzzlePieceSet
    • byParent

      public PuzzlePieceSet byParent(PuzzleComponent parents)
      Returns new PuzzlePieceSet containing the elements of this PuzzlePieceSet, but only those which have at least one element from the PuzzleComponent parameter as one of their parent Definitions.

      Template Usage: Imagine we have one attribute name and we're not interested in the differences between Definitions sharing the same name. We're now printing the resulting allowed attribute values for each parent element name:
      #set ( $oneOrMoreAttributes = $model.getAttribute($atttributeName) )
      ## we want to write information about only _one_ parent per Name...
      #foreach ($parent in $oneOrMoreAttributes.getParents().withoutMultiples())
      - Allowed Values for Parent Element $parent :
      ## but we want the attribute values displayed which are allowed in _all_ parents with the same Name...
      #foreach ($value in $oneOrMoreAttributes.byParent($parent.withMultiples()).getValues())
      -- "$value"
      #end
      #end

      Parameters:
      parents -
      Returns:
      new PuzzlePieceSet
    • containsName

      public boolean containsName(String aDefinitionName)
      Check whether this List contains an Element by this Name
      Parameters:
      aDefinitionName -
      Returns:
      True if an element by this name exists
    • containsName

      public boolean containsName(PuzzleComponent aNamed)
      Check whether this List contains an Element by this Name
      Parameters:
      aNamed -
      Returns:
      True if an element by this name exists
    • getQName

      public String getQName()
      Gets the ns:local tag name of the Definitions - provided that this PuzzlePieceSet is not empty and all Definitions share the same tag name. Throws Exception otherwise.
      Specified by:
      getQName in interface PuzzleComponent
      Returns:
      The tag name
    • getType

      public MSVExpressionType getType()
      Gets the type of the Definitions - provided that this PuzzlePieceSet is not empty and all Definitions have the same type and name. Throws Exception otherwise.
      Specified by:
      getType in interface PuzzleComponent
      Returns:
      The type of this PuzzleComponent
    • canHaveText

      public boolean canHaveText()
      Determines whether the Definitions can have text - provided that this PuzzlePieceSet is not empty and all Definitions have the same type and name. Throws Exception otherwise.
      Specified by:
      canHaveText in interface PuzzleComponent
      Returns:
      True if a text node is allowed, false otherwise
    • isSingleton

      public boolean isSingleton(PuzzleComponent child)
      Description copied from interface: PuzzleComponent
      Determines whether the child PuzzlePiece(s) is/are singleton(s)

      Convention: If child is a collection this method returns false if one child element is no singleton. If this is a collection this method returns false if child is no singleton for one element of this.

      Specified by:
      isSingleton in interface PuzzleComponent
      Parameters:
      child - PuzzleComponent child
      Returns:
      True if child is defined as Singleton, falso otherwise.
    • getLocalName

      public String getLocalName()
      Description copied from interface: PuzzleComponent
      Get only localname
      Specified by:
      getLocalName in interface PuzzleComponent
      Returns:
      localname
    • getNamespacePrefix

      public String getNamespacePrefix()
      Description copied from interface: PuzzleComponent
      Get only namespace prefix of the qualified name
      Specified by:
      getNamespacePrefix in interface PuzzleComponent
      Returns:
      namespacePrefix
    • toString

      public String toString()
      Returns String representation (convenient method for getQName())

      Template Usage: Just use $aDefinitionSet as you would use a string variable

      Overrides:
      toString in class Object
    • add

      public boolean add(PuzzlePiece e)
      Specified by:
      add in interface Collection<PuzzlePiece>
    • addAll

      public boolean addAll(Collection<? extends PuzzlePiece> c)
      Specified by:
      addAll in interface Collection<PuzzlePiece>
    • clear

      public void clear()
      Specified by:
      clear in interface Collection<PuzzlePiece>
    • contains

      public boolean contains(Object o)
      Specified by:
      contains in interface Collection<PuzzlePiece>
    • containsAll

      public boolean containsAll(Collection<?> c)
      Specified by:
      containsAll in interface Collection<PuzzlePiece>
    • isEmpty

      public boolean isEmpty()
      Specified by:
      isEmpty in interface Collection<PuzzlePiece>
    • iterator

      public Iterator<PuzzlePiece> iterator()
      Specified by:
      iterator in interface Collection<PuzzlePiece>
      Specified by:
      iterator in interface Iterable<PuzzlePiece>
    • remove

      public boolean remove(Object o)
      Specified by:
      remove in interface Collection<PuzzlePiece>
    • removeAll

      public boolean removeAll(Collection<?> c)
      Specified by:
      removeAll in interface Collection<PuzzlePiece>
    • retainAll

      public boolean retainAll(Collection<?> c)
      Specified by:
      retainAll in interface Collection<PuzzlePiece>
    • size

      public int size()
      Specified by:
      size in interface Collection<PuzzlePiece>
    • toArray

      public Object[] toArray()
      Specified by:
      toArray in interface Collection<PuzzlePiece>
    • toArray

      public <T> T[] toArray(T[] a)
      Specified by:
      toArray in interface Collection<PuzzlePiece>
    • getChildElements

      public PuzzlePieceSet getChildElements()
      Description copied from interface: PuzzleComponent
      ELEMENT PuzzlePiece only: Get all child element Definitions
      Specified by:
      getChildElements in interface PuzzleComponent
      Returns:
      The child Definitions of this PuzzleComponent
    • isMandatory

      public boolean isMandatory(PuzzleComponent child)
      Description copied from interface: PuzzleComponent
      ELEMENT Definition only: Determine solely by child type and name whether child is mandatory.

      Here's why we're not using the child Definition object(s) for this: An element often has a mandatory attribute, but two (or more) different content definitions for this attribute. This is done by defining this attribute twice and creating a CHOICE between both Definitions. If you'd ask whether one of these definitions is mandatory, you'd always get false as answer as you have the choice between the two definitions. Mostly this is not the answer you're looking for.

      Contract: If 'this' is a Collection, mandatory means mandatory for one member of 'this'.

      Specified by:
      isMandatory in interface PuzzleComponent
      Parameters:
      child - The child Definition(s) of type ELEMENT or ATTRIBUTE
      Returns:
      true if child is a defined child of this and if it's mandatory. False otherwise.
    • getCollection

      public Collection<PuzzlePiece> getCollection()
      Description copied from interface: PuzzleComponent
      Method to treat NamedDefined as a Collection of PuzzlePiece
      Specified by:
      getCollection in interface PuzzleComponent
      Returns:
      Collection of PuzzlePiece objects
    • getAttributes

      public PuzzlePieceSet getAttributes()
      Description copied from interface: PuzzleComponent
      ELEMENT PuzzlePiece only: Get all attribute Definitions
      Specified by:
      getAttributes in interface PuzzleComponent
      Returns:
      The attribute Definitions of this PuzzleComponent
    • getDatatypes

      public PuzzlePieceSet getDatatypes()
      Description copied from interface: PuzzleComponent
      ATTRIBUTE PuzzlePiece only: Get all datatype Definitions
      Specified by:
      getDatatypes in interface PuzzleComponent
      Returns:
      The datatype Definitions of this PuzzleComponent
    • getParents

      public PuzzlePieceSet getParents()
      Description copied from interface: PuzzleComponent
      Get all parent Definitions
      Specified by:
      getParents in interface PuzzleComponent
      Returns:
      The parent Definitions of this PuzzleComponent
    • getValues

      public PuzzlePieceSet getValues()
      Description copied from interface: PuzzleComponent
      ATTRIBUTE PuzzlePiece only: Get all value Definitions
      Specified by:
      getValues in interface PuzzleComponent
      Returns:
      The constant value Definitions of this PuzzleComponent