initBindingMetadata();
}
private void initBindingMetadata()
{
final Element element = property().element();
final PropertyDef property = property().definition();
try
{
final XmlElementBinding xmlElementBindingAnnotation = property.getAnnotation( XmlElementBinding.class );
final XmlNamespaceResolver xmlNamespaceResolver = ( (XmlResource) element.resource() ).getXmlNamespaceResolver();
final SortedSet<ElementType> possible = this.possibleTypesService.types();
this.modelElementTypes = possible.toArray( new ElementType[ possible.size() ] );
if( xmlElementBindingAnnotation == null )
{
final XmlBinding xmlBindingAnnotation = property.getAnnotation( XmlBinding.class );
if( xmlBindingAnnotation != null && possible.size() == 1 )
{
final String path = xmlBindingAnnotation.path();
final int slashIndex = path.lastIndexOf( '/' );
if( slashIndex == -1 )
{
this.xmlElementNames = new QName[] { createQualifiedName( path, xmlNamespaceResolver ) };
}
else if( slashIndex > 0 && slashIndex < path.length() - 1 )
{
this.path = new XmlPath( path.substring( 0, slashIndex ), xmlNamespaceResolver );
this.xmlElementNames = new QName[] { createQualifiedName( path.substring( slashIndex + 1 ), xmlNamespaceResolver ) };
}
}
if( this.xmlElementNames == null )
{
this.path = new XmlPath( property.name(), ( (XmlResource) element.resource() ).getXmlNamespaceResolver() );
this.xmlElementNames = new QName[ this.modelElementTypes.length ];
for( int i = 0; i < this.modelElementTypes.length; i++ )
{
this.xmlElementNames[ i ] = createDefaultElementName( this.modelElementTypes[ i ], xmlNamespaceResolver );
}
}
}
else
{
if( xmlElementBindingAnnotation.path().length() > 0 )
{
this.path = new XmlPath( xmlElementBindingAnnotation.path(), xmlNamespaceResolver );
}
final XmlElementBinding.Mapping[] mappings = xmlElementBindingAnnotation.mappings();
this.xmlElementNames = new QName[ this.modelElementTypes.length ];
for( int i = 0; i < this.modelElementTypes.length; i++ )
{
final ElementType type = this.modelElementTypes[ i ];
for( XmlElementBinding.Mapping mapping : mappings )
{
if( mapping.type() == type.getModelElementClass() )
{
final String mappingElementName = mapping.element().trim();
if( mappingElementName.length() == 0 )
{
throw new RuntimeException( mustSpecifyElementNameMsg.text() );
}
this.xmlElementNames[ i ] = createQualifiedName( mappingElementName, xmlNamespaceResolver );
break;
}
}
if( this.xmlElementNames[ i ] == null )
{
this.xmlElementNames[ i ] = createDefaultElementName( type, xmlNamespaceResolver );
}
}
}
}
catch( Exception e )
{
final String msg = failure.format( element.type().getSimpleName(), property.name(), e.getMessage() );
throw new RuntimeException( msg, e );
}
}