FileDocCategorySizeDatePackage
Mp4Tag.javaAPI DocJaudiotagger 2.0.423974Wed Dec 07 10:26:04 GMT 2011org.jaudiotagger.tag.mp4

Mp4Tag

public class Mp4Tag extends org.jaudiotagger.audio.generic.AbstractTag
A Logical representation of Mp4Tag, i.e the meta information stored in an Mp4 file underneath the moov.udt.meta.ilst atom.

Fields Summary
private static final EnumMap
tagFieldToMp4Field
Constructors Summary
Methods Summary
public TagFieldcreateArtworkField(byte[] data)
Create artwork field

param
data raw image data
return
throws
org.jaudiotagger.tag.FieldDataInvalidException

        return new Mp4TagCoverField(data);
    
public TagFieldcreateField(org.jaudiotagger.tag.images.Artwork artwork)
Create artwork field

return

        return new Mp4TagCoverField(artwork.getBinaryData());
    
public TagFieldcreateField(FieldKey genericKey, java.lang.String value)
Create Tag Field using generic key

This should use the correct subclass for the key

param
genericKey
param
value
return
throws
KeyNotFoundException
throws
FieldDataInvalidException

        if (value == null)
        {
            throw new IllegalArgumentException(ErrorMessage.GENERAL_INVALID_NULL_ARGUMENT.getMsg());
        }
        if (genericKey == null)
        {
            throw new KeyNotFoundException();
        }

        //Special handling for these number fields
        if(
                (genericKey== FieldKey.TRACK)||
                (genericKey== FieldKey.TRACK_TOTAL)||
                (genericKey== FieldKey.DISC_NO)||
                (genericKey== FieldKey.DISC_TOTAL)
            )
        {
            try
            {
                int number = Integer.parseInt(value);
                if(genericKey== FieldKey.TRACK)
                {
                    return new Mp4TrackField(number);
                }
                else if(genericKey== FieldKey.TRACK_TOTAL)
                {
                    return new Mp4TrackField(0,number);
                }
                else if(genericKey== FieldKey.DISC_NO)
                {
                    return new Mp4DiscNoField(number);
                }
                else if(genericKey== FieldKey.DISC_TOTAL)
                {
                    return new Mp4DiscNoField(0,number);
                }
            }
            catch(NumberFormatException nfe)
            {
                //If not number we want to convert to an expected exception (which is not a RuntimeException)
                //so can be handled properly by calling program
                throw new FieldDataInvalidException("Value "+value + " is not a number as required",nfe);
            }
        }

        //Default for all other fields
        return createField(tagFieldToMp4Field.get(genericKey), value);

    
public TagFieldcreateField(Mp4FieldKey mp4FieldKey, java.lang.String value)
Create Tag Field using mp4 key

Uses the correct subclass for the key

param
mp4FieldKey
param
value
return
throws
KeyNotFoundException
throws
FieldDataInvalidException

        if (value == null)
        {
            throw new IllegalArgumentException(ErrorMessage.GENERAL_INVALID_NULL_ARGUMENT.getMsg());
        }
        if (mp4FieldKey == null)
        {
            throw new KeyNotFoundException();
        }

        //This is boolean stored as 1, but calling program might setField as 'true' so we handle this
        //case internally
        if(mp4FieldKey==Mp4FieldKey.COMPILATION)
        {
            if(value.equals("true"))
            {
                value= Mp4TagByteField.TRUE_VALUE;
            }
            return new Mp4TagByteField(mp4FieldKey, value, mp4FieldKey.getFieldLength());
        }
        else if(mp4FieldKey==Mp4FieldKey.GENRE)
        {
            return createGenreField(value);
        }
        else if(mp4FieldKey.getSubClassFieldType()==Mp4TagFieldSubType.DISC_NO)
        {
            return new Mp4DiscNoField(value);
        }
        else if(mp4FieldKey.getSubClassFieldType()==Mp4TagFieldSubType.TRACK_NO)
        {
            return new Mp4TrackField(value);
        }
        else if(mp4FieldKey.getSubClassFieldType()==Mp4TagFieldSubType.BYTE)
        {
            return new Mp4TagByteField(mp4FieldKey, value, mp4FieldKey.getFieldLength());
        }
        else if(mp4FieldKey.getSubClassFieldType()==Mp4TagFieldSubType.NUMBER)
        {
            return new Mp4TagTextNumberField(mp4FieldKey.getFieldName(), value);
        }
        else if(mp4FieldKey.getSubClassFieldType()==Mp4TagFieldSubType.REVERSE_DNS)
        {
            return new Mp4TagReverseDnsField(mp4FieldKey, value);
        }
        else if(mp4FieldKey.getSubClassFieldType()==Mp4TagFieldSubType.ARTWORK)
        {
            throw new UnsupportedOperationException(ErrorMessage.ARTWORK_CANNOT_BE_CREATED_WITH_THIS_METHOD.getMsg());
        }
        else if(mp4FieldKey.getSubClassFieldType()==Mp4TagFieldSubType.TEXT)
        {
            return new Mp4TagTextField(mp4FieldKey.getFieldName(), value);
        }
        else if(mp4FieldKey.getSubClassFieldType()==Mp4TagFieldSubType.UNKNOWN)
        {
            throw new UnsupportedOperationException(ErrorMessage.DO_NOT_KNOW_HOW_TO_CREATE_THIS_ATOM_TYPE.getMsg(mp4FieldKey.getFieldName()));
        }
        else
        {
            throw new UnsupportedOperationException(ErrorMessage.DO_NOT_KNOW_HOW_TO_CREATE_THIS_ATOM_TYPE.getMsg(mp4FieldKey.getFieldName()));
        }
    
private TagFieldcreateGenreField(java.lang.String content)
Create genre field

If the content can be parsed to one of the known values use the genre field otherwise use the custom field.

param
content
return

        if (content == null)
        {
            throw new IllegalArgumentException(ErrorMessage.GENERAL_INVALID_NULL_ARGUMENT.getMsg());
        }

        //Always write as text
        if(TagOptionSingleton.getInstance().isWriteMp4GenresAsText())
        {
            return new Mp4TagTextField(GENRE_CUSTOM.getFieldName(), content);
        }

        if (Mp4GenreField.isValidGenre(content))
        {
            return new Mp4GenreField(content);
        }
        else
        {
            return new Mp4TagTextField(GENRE_CUSTOM.getFieldName(), content);
        }
    
public voiddeleteField(FieldKey genericKey)
Delete fields with this generic key

param
genericKey

        if (genericKey == null)
        {
            throw new KeyNotFoundException();
        }
        super.deleteField(tagFieldToMp4Field.get(genericKey).getFieldName());
    
public voiddeleteField(Mp4FieldKey mp4Key)
Delete fields with this mp4key

param
mp4Key
throws
org.jaudiotagger.tag.KeyNotFoundException

        if (mp4Key == null)
        {
            throw new KeyNotFoundException();
        }
        super.deleteField(mp4Key.getFieldName());
    
public java.util.Listget(Mp4FieldKey mp4FieldKey)
Retrieve the values that exists for this mp4keyId (this is the internalid actually used)

param
mp4FieldKey
throws
org.jaudiotagger.tag.KeyNotFoundException
return

        if (mp4FieldKey == null)
        {
            throw new KeyNotFoundException();
        }
        return super.getFields(mp4FieldKey.getFieldName());
    
public java.util.ListgetArtworkList()

        List<TagField> coverartList = get(Mp4FieldKey.ARTWORK);
        List<Artwork> artworkList = new ArrayList<Artwork>(coverartList.size());

        for(TagField next:coverartList)
        {
            Mp4TagCoverField mp4CoverArt = (Mp4TagCoverField)next;
            Artwork artwork = ArtworkFactory.getNew();
            artwork.setBinaryData(mp4CoverArt.getData());
            artwork.setMimeType(Mp4TagCoverField.getMimeTypeForImageType(mp4CoverArt.getFieldType()));
            artworkList.add(artwork);
        }
        return artworkList;
    
public java.util.ListgetFields(FieldKey genericKey)
Maps the generic key to the mp4 key and return the list of values for this field

param
genericKey

        if (genericKey == null)
        {
            throw new KeyNotFoundException();
        }
        return super.getFields(tagFieldToMp4Field.get(genericKey).getFieldName());
    
public java.lang.StringgetFirst(Mp4FieldKey mp4Key)
Retrieve the first value that exists for this mp4key

param
mp4Key
return
throws
org.jaudiotagger.tag.KeyNotFoundException

        if (mp4Key == null)
        {
            throw new KeyNotFoundException();
        }
        return super.getFirst(mp4Key.getFieldName());
    
public Mp4TagFieldgetFirstField(FieldKey genericKey)

        if (genericKey == null)
        {
            throw new KeyNotFoundException();
        }
        return (Mp4TagField)super.getFirstField(tagFieldToMp4Field.get(genericKey).getFieldName());
    
public Mp4TagFieldgetFirstField(Mp4FieldKey mp4Key)

        if (mp4Key == null)
        {
            throw new KeyNotFoundException();
        }
        return (Mp4TagField) super.getFirstField(mp4Key.getFieldName());
    
public java.lang.StringgetValue(FieldKey genericKey, int index)
Retrieve the indexed value that exists for this generic key

param
genericKey
return

        if (genericKey == null)
        {
            throw new KeyNotFoundException();
        }

        if(genericKey== FieldKey.GENRE)
        {
            List<TagField> genres = getFields(GENRE.getFieldName());
            if (genres.size() == 0)
            {
                genres = getFields(GENRE_CUSTOM.getFieldName());
            }
            if(genres.size()>index)
            {
                return ((TagTextField)genres.get(index)).getContent();
            }
            else
            {
                return "";
            }

        }
        else if(genericKey== FieldKey.TRACK)
        {
            List<TagField> list = get(tagFieldToMp4Field.get(genericKey));
            if(list.size()>index)
            {
                Mp4TrackField trackField = (Mp4TrackField)list.get(index);
                if(trackField.getTrackNo()>0)
                {
                    return String.valueOf(trackField.getTrackNo());
                }
            }
        }
        else if(genericKey== FieldKey.TRACK_TOTAL)
        {
            List<TagField> list = get(tagFieldToMp4Field.get(genericKey));
            if(list.size()>index)
            {
                Mp4TrackField trackField = (Mp4TrackField)list.get(index);
                if(trackField.getTrackTotal()>0)
                {
                    return String.valueOf(trackField.getTrackTotal());
                }
            }
        }
        else if(genericKey== FieldKey.DISC_NO)
        {
            List<TagField> list = get(tagFieldToMp4Field.get(genericKey));
            if(list.size()>index)
            {
                Mp4DiscNoField discField = (Mp4DiscNoField)list.get(index);
                if(discField.getDiscNo()>0)
                {
                     return String.valueOf(discField.getDiscNo());
                }

            }
        }
        else if(genericKey== FieldKey.DISC_TOTAL)
        {
            List<TagField> list = get(tagFieldToMp4Field.get(genericKey));
            if(list.size()>index)
            {
                Mp4DiscNoField discField = (Mp4DiscNoField)list.get(index);
                if(discField.getDiscTotal()>0)
                {
                     return String.valueOf(discField.getDiscTotal());
                }
            }
        }
        else
        {
            return super.getItem(tagFieldToMp4Field.get(genericKey).getFieldName(), index);
        }
        return "";
    
public booleanhasField(FieldKey genericKey)

param
genericKey
return

        Mp4FieldKey mp4FieldKey = tagFieldToMp4Field.get(genericKey);
        return getFields(mp4FieldKey.getFieldName()).size() != 0;
    
public booleanhasField(Mp4FieldKey mp4FieldKey)

param
mp4FieldKey
return

        return getFields(mp4FieldKey.getFieldName()).size() != 0;
    
protected booleanisAllowedEncoding(java.lang.String enc)

        return enc.equals(Mp4BoxHeader.CHARSET_UTF_8);
    
public voidsetField(TagField field)
Set field, special handling for track and disc because they hold two fields

param
field

        if (field == null)
        {
            return;
        }

        if(field.getId().equals(TRACK.getFieldName()))
        {
            List<TagField> list = fields.get(field.getId());
            if(list==null||list.size()==0)
            {
                 super.setField(field);
            }
            else
            {
                Mp4TrackField existingTrackField = (Mp4TrackField)list.get(0);
                Mp4TrackField newTrackField      = (Mp4TrackField)field;
                Short trackNo    = existingTrackField.getTrackNo();
                Short trackTotal = existingTrackField.getTrackTotal();
                if(newTrackField.getTrackNo()>0 )
                {
                    trackNo = newTrackField.getTrackNo();
                }
                if(newTrackField.getTrackTotal()>0 )
                {
                    trackTotal = newTrackField.getTrackTotal();
                }

                Mp4TrackField mergedTrackField = new Mp4TrackField(trackNo,trackTotal);
                super.setField(mergedTrackField);
            }
        }
        else if(field.getId().equals(DISCNUMBER.getFieldName()))
        {
            List<TagField> list = fields.get(field.getId());
            if(list==null||list.size()==0)
            {
                 super.setField(field);
            }
            else
            {
                Mp4DiscNoField existingDiscNoField = (Mp4DiscNoField)list.get(0);
                Mp4DiscNoField newDiscNoField      = (Mp4DiscNoField)field;
                Short discNo    = existingDiscNoField.getDiscNo();
                Short discTotal = existingDiscNoField.getDiscTotal();
                if(newDiscNoField.getDiscNo()>0 )
                {
                    discNo = newDiscNoField.getDiscNo();
                }
                if(newDiscNoField.getDiscTotal()>0 )
                {
                    discTotal = newDiscNoField.getDiscTotal();
                }

                Mp4DiscNoField mergedDiscNoField = new Mp4DiscNoField(discNo,discTotal);
                super.setField(mergedDiscNoField);
            }
        }
        else
        {
            super.setField(field);
        }
    
public java.lang.StringtoString()

        return "Mpeg4 " + super.toString();