Class PuzzlePiece

  • All Implemented Interfaces:
    Comparable<PuzzlePiece>, PuzzleComponent, QNamed, QNamedPuzzleComponent

    public class PuzzlePiece
    extends Object
    implements Comparable<PuzzlePiece>, QNamedPuzzleComponent
    One of the following RelaxNG definitions of an Element, Attribute, Value or Datatype.

    Each PuzzlePiece encapsulates one MSV Expression. Two PuzzlePiece can share the same MSV Expression (RelaxNG pattern: <element><choice><name>aName</name><name>anotherName</name></choice></element>)


    • hashCode uses the hashCode from the encapsulated Expressions. So two Definitions (rarely) can have the same hashCode. Equals uses Name _and_ hashCode.
    • Sorting is done by ns:local tag names as first key and hashCode as second key.
    • All returned PuzzlePieceSet objects are immutable to protect them against naive usage in velocity templates
    • Method Detail

      • equals

        public boolean equals​(Object b)
        Uses the name and the wrapped MSV Expression to test for equality.
        equals in class Object
        b - Another object
        Whether both objects equal
      • hashCode

        public int hashCode()
        Uses the wrapped MSV Expression for the hashCode. MSV Expressions are numbered consecutively by a distinct Hash Code.
        hashCode in class Object
      • compareTo

        public int compareTo​(PuzzlePiece o)
        Uses the ns:local name of the wrapped MSV Expression as first key and the hashCode as second key. If o1.equals(o2) this method will return 0 (since both must share the same Expression and name).
        Specified by:
        compareTo in interface Comparable<PuzzlePiece>
        o - Other Object
      • contentEquals

        protected boolean contentEquals​(PuzzlePiece other)
      • getQName

        public String getQName()
        Gets the ns:local tag name of this PuzzlePiece
        Specified by:
        getQName in interface QNamed
        The tag name
      • getLocalName

        public String getLocalName()
        Description copied from interface: QNamed
        Get only localname
        Specified by:
        getLocalName in interface QNamed
      • getNamespace

        public String getNamespace()
        Description copied from interface: QNamed
        Get only namespace
        Specified by:
        getNamespace in interface QNamed
      • toString

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

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

        toString in class Object
      • getType

        public MSVExpressionType getType()
        Gets the type of this (ELEMENT, ATTRIBUTE, DATA, VALUE)
        Specified by:
        getType in interface PuzzleComponent
        The ExpressionType of this PuzzlePiece
      • getExpression

        public com.sun.msv.grammar.Expression getExpression()
        Gets the wrapped Expression
        The Expression wrapped by this.
      • canHaveText

        public boolean canHaveText()
        Determines whether this Element can have a text node as child
        Specified by:
        canHaveText in interface PuzzleComponent
        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
        child - PuzzleComponent child
        True if child is defined as Singleton, falso otherwise.
      • withMultiples

        public PuzzlePieceSet withMultiples()
        Gets the List of Definitions which share the same tag name, but are defined multiple times in the schema. The list is type specific, i.e. an ATTRIBUTE can never be a multiple of an ELEMENT.
        The list of Definitions which share the same tag name.
      • getMultipleNumber

        public int getMultipleNumber()
        Gets the index of 'this' in the List of Definitions returned by withMultiples()
        Index of this PuzzlePiece object in the PuzzlePieceSet returned by withMultiples()
      • getParents

        public PuzzlePieceSet getParents()
        Gets the Parents which can contain this PuzzlePiece as a child
        Specified by:
        getParents in interface PuzzleComponent
        The parent Definitions
      • getChildElements

        public PuzzlePieceSet getChildElements()
        Gets the child elements of this PuzzlePiece. Please note that only Definitions of type ELEMENT can have child elements.
        Specified by:
        getChildElements in interface PuzzleComponent
        The child Definitions of type ELEMENT
      • isMandatory

        public boolean isMandatory​(QNamedPuzzleComponent child)
        Description copied from interface: QNamedPuzzleComponent
        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 QNamedPuzzleComponent
        child - The child Definition(s) of type ELEMENT or ATTRIBUTE
        true if child is a defined child of this and if it's mandatory. False otherwise.
      • getAttributes

        public PuzzlePieceSet getAttributes()
        Gets the Attributes of this PuzzlePiece. Please note that only Definitions of type ELEMENT can have attributes.
        Specified by:
        getAttributes in interface PuzzleComponent
        The child Definitions of type ATTRIBUTE
      • getValues

        public PuzzlePieceSet getValues()
        Gets the defined constant values. Please note that only Definitions of type ATTRIBUTE can have values.
        Specified by:
        getValues in interface PuzzleComponent
        The constant values
      • getDatatypes

        public PuzzlePieceSet getDatatypes()
        Gets the defined datatypes. Please note that only Definitions of type ATTRIBUTE can have datatypes.
        Specified by:
        getDatatypes in interface PuzzleComponent
        The datatypes
      • extractPuzzlePieces

        public static void extractPuzzlePieces​(com.sun.msv.grammar.Expression root,
                                               PuzzlePieceSet newElementSet,
                                               PuzzlePieceSet newAttributeSet,
                                               String schemaFileName)
        Creates all PuzzlePiece objects from MSV root tree.

        The PuzzlePiece objects are all made immutable to protect them against changes by naive template usage. Note that the Sets of all elements/attributes can only be made immutable by the caller after this method run.

        root - MSV root Expression
        newElementSet - empty Set. Will be filled with Definitions of Type.ELEMENT
        newAttributeSet - empty Set. Will be filled with Definitions of Type.ATTRIBUTE