FileDocCategorySizeDatePackage
TagConverter.javaAPI DocJaudiotagger 2.0.48583Wed Mar 30 16:11:52 BST 2011org.jaudiotagger.audio.asf.util

TagConverter

public final class TagConverter extends Object
This class provides functionality to convert {@link org.jaudiotagger.audio.asf.data.AsfHeader}objects into {@link org.jaudiotagger.tag.Tag}objects.
author
Christian Laireiter (liree)

Fields Summary
Constructors Summary
private TagConverter()
Hidden utility class constructor.

        // Nothing to do.
    
Methods Summary
public static voidassignCommonTagValues(org.jaudiotagger.tag.Tag tag, MetadataContainer description)
This method assigns those tags of tag which are defined to be common by jaudiotagger.

param
tag The tag from which the values are gathered.
Assigned values are:
param
description The extended content description which should receive the values.
Warning: the common values will be replaced.

        assert description.getContainerType() == ContainerType.EXTENDED_CONTENT;
        MetadataDescriptor tmp;
        if (!Utils.isBlank(tag.getFirst(FieldKey.ALBUM))) {
            tmp = new MetadataDescriptor(description.getContainerType(),
                    AsfFieldKey.ALBUM.getFieldName(),
                    MetadataDescriptor.TYPE_STRING);
            tmp.setStringValue(tag.getFirst(FieldKey.ALBUM));
            description.removeDescriptorsByName(tmp.getName());
            description.addDescriptor(tmp);
        } else {
            description.removeDescriptorsByName(AsfFieldKey.ALBUM
                    .getFieldName());
        }
        if (!Utils.isBlank(tag.getFirst(FieldKey.TRACK))) {
            tmp = new MetadataDescriptor(description.getContainerType(),
                    AsfFieldKey.TRACK.getFieldName(),
                    MetadataDescriptor.TYPE_STRING);
            tmp.setStringValue(tag.getFirst(FieldKey.TRACK));
            description.removeDescriptorsByName(tmp.getName());
            description.addDescriptor(tmp);
        } else {
            description.removeDescriptorsByName(AsfFieldKey.TRACK
                    .getFieldName());
        }
        if (!Utils.isBlank(tag.getFirst(FieldKey.YEAR))) {
            tmp = new MetadataDescriptor(description.getContainerType(),
                    AsfFieldKey.YEAR.getFieldName(),
                    MetadataDescriptor.TYPE_STRING);
            tmp.setStringValue(tag.getFirst(FieldKey.YEAR));
            description.removeDescriptorsByName(tmp.getName());
            description.addDescriptor(tmp);
        } else {
            description
                    .removeDescriptorsByName(AsfFieldKey.YEAR.getFieldName());
        }
        if (!Utils.isBlank(tag.getFirst(FieldKey.GENRE))) {
            // Write Genre String value
            tmp = new MetadataDescriptor(description.getContainerType(),
                    AsfFieldKey.GENRE.getFieldName(),
                    MetadataDescriptor.TYPE_STRING);
            tmp.setStringValue(tag.getFirst(FieldKey.GENRE));
            description.removeDescriptorsByName(tmp.getName());
            description.addDescriptor(tmp);
            Integer genreNum = GenreTypes.getInstanceOf().getIdForName(
                    tag.getFirst(FieldKey.GENRE));
            // ..and if it is one of the standard genre types used the id as
            // well
            if (genreNum != null) {
                tmp = new MetadataDescriptor(description.getContainerType(),
                        AsfFieldKey.GENRE_ID.getFieldName(),
                        MetadataDescriptor.TYPE_STRING);
                tmp.setStringValue("(" + genreNum + ")");
                description.removeDescriptorsByName(tmp.getName());
                description.addDescriptor(tmp);
            } else {
                description.removeDescriptorsByName(AsfFieldKey.GENRE_ID
                        .getFieldName());
            }
        } else {
            description.removeDescriptorsByName(AsfFieldKey.GENRE
                    .getFieldName());
            description.removeDescriptorsByName(AsfFieldKey.GENRE_ID
                    .getFieldName());
        }
    
public static AsfTagcreateTagOf(AsfHeader source)
This method creates a {@link Tag}and fills it with the contents of the given {@link AsfHeader}.

param
source The ASF header which contains the information.
return
A Tag with all its values.

        // TODO do we need to copy here.
        AsfTag result = new AsfTag(true);
        for (int i = 0; i < ContainerType.values().length; i++) {
            MetadataContainer current = source
                    .findMetadataContainer(ContainerType.values()[i]);
            if (current != null) {
                List<MetadataDescriptor> descriptors = current.getDescriptors();
                for (MetadataDescriptor descriptor : descriptors) {
                    AsfTagField toAdd;
                    if (descriptor.getType() == MetadataDescriptor.TYPE_BINARY) {
                        if (descriptor.getName().equals(
                                AsfFieldKey.COVER_ART.getFieldName())) {
                            toAdd = new AsfTagCoverField(descriptor);
                        } else if (descriptor.getName().equals(
                                AsfFieldKey.BANNER_IMAGE.getFieldName())) {
                            toAdd = new AsfTagBannerField(descriptor);
                        } else {
                            toAdd = new AsfTagField(descriptor);
                        }
                    } else {
                        toAdd = new AsfTagTextField(descriptor);
                    }
                    result.addField(toAdd);
                }
            }
        }
        return result;
    
public static MetadataContainer[]distributeMetadata(AsfTag tag)
This method distributes the tags fields among the {@linkplain ContainerType#getOrdered()} {@linkplain MetadataContainer containers}.

param
tag the tag with the fields to distribute.
return
distribution

        final Iterator<AsfTagField> asfFields = tag.getAsfFields();
        final MetadataContainer[] createContainers = MetadataContainerFactory
                .getInstance().createContainers(ContainerType.getOrdered());
        boolean assigned;
        AsfTagField current;
        while (asfFields.hasNext()) {
            current = asfFields.next();
            assigned = false;
            for (int i = 0; !assigned && i < createContainers.length; i++) {
                if (ContainerType.areInCorrectOrder(createContainers[i]
                        .getContainerType(), AsfFieldKey.getAsfFieldKey(
                        current.getId()).getHighestContainer())) {
                    if (createContainers[i].isAddSupported(current
                            .getDescriptor())) {
                        createContainers[i].addDescriptor(current
                                .getDescriptor());
                        assigned = true;
                    }
                }
            }
            assert assigned;
        }
        return createContainers;