Coverage Report - org.jaudiotagger.tag.mp4.field.Mp4GenreField
 
Classes in this File Line Coverage Branch Coverage Complexity
Mp4GenreField
82%
33/40
71%
10/14
4.5
 
 1  
 package org.jaudiotagger.tag.mp4.field;
 2  
 
 3  
 import org.jaudiotagger.audio.mp4.atom.Mp4BoxHeader;
 4  
 import org.jaudiotagger.logging.ErrorMessage;
 5  
 import org.jaudiotagger.tag.mp4.Mp4FieldKey;
 6  
 import org.jaudiotagger.tag.mp4.atom.Mp4DataBox;
 7  
 import org.jaudiotagger.tag.reference.GenreTypes;
 8  
 
 9  
 import java.io.UnsupportedEncodingException;
 10  
 import java.nio.ByteBuffer;
 11  
 import java.util.ArrayList;
 12  
 
 13  
 /**
 14  
  * Represents the Genre field , when user has selected from the set list of genres
 15  
  * <p/>
 16  
  * <p>This class allows you to retrieve either the internal genreid, or the display value
 17  
  */
 18  
 public class Mp4GenreField extends Mp4TagTextNumberField
 19  
 {
 20  
     public Mp4GenreField(String id, ByteBuffer data) throws UnsupportedEncodingException
 21  
     {
 22  91
         super(id, data);
 23  91
     }
 24  
 
 25  
     /**
 26  
      * Precheck to see if the value is a valid genre or whether you should use a custom genre.
 27  
      *
 28  
      * @param genreId
 29  
      * @return
 30  
      */
 31  
     public static boolean isValidGenre(String genreId)
 32  
     {
 33  
         //Is it an id (within old id3 range)      
 34  
         try
 35  
         {
 36  62
             short genreVal = Short.parseShort(genreId);
 37  28
             if ((genreVal - 1) <= GenreTypes.getMaxStandardGenreId())
 38  
             {
 39  28
                 return true;
 40  
             }
 41  
         }
 42  34
         catch (NumberFormatException nfe)
 43  
         {
 44  
             //Do Nothing test as String instead
 45  0
         }
 46  
 
 47  
         //Is it the String value ?
 48  34
         Integer id3GenreId = GenreTypes.getInstanceOf().getIdForValue(genreId);
 49  34
         if (id3GenreId != null)
 50  
         {
 51  16
             if (id3GenreId <= GenreTypes.getMaxStandardGenreId())
 52  
             {
 53  8
                 return true;
 54  
             }
 55  
         }
 56  26
         return false;
 57  
     }
 58  
 
 59  
     /**
 60  
      * Construct genre, if cant find match just default to first genre
 61  
      *
 62  
      * @param genreId key into ID3v1 list (offset by one) or String value in ID3list
 63  
      */
 64  
     public Mp4GenreField(String genreId)
 65  
     {
 66  36
         super(Mp4FieldKey.GENRE.getFieldName(), genreId);
 67  
 
 68  
         //Is it an id
 69  
         try
 70  
         {
 71  36
             short genreVal = Short.parseShort(genreId);
 72  28
             if ((genreVal - 1) <= GenreTypes.getMaxStandardGenreId())
 73  
             {
 74  28
                 numbers = new ArrayList<Short>();
 75  28
                 numbers.add(genreVal);
 76  28
                 return;
 77  
             }
 78  
             //Default
 79  0
             numbers = new ArrayList<Short>();
 80  0
             numbers.add((short) (1));
 81  0
             return;
 82  
         }
 83  8
         catch (NumberFormatException nfe)
 84  
         {
 85  
             //Do Nothing test as String instead
 86  
         }
 87  
 
 88  
         //Is it the String value ?
 89  8
         Integer id3GenreId = GenreTypes.getInstanceOf().getIdForValue(genreId);
 90  8
         if (id3GenreId != null)
 91  
         {
 92  8
             if (id3GenreId <= GenreTypes.getMaxStandardGenreId())
 93  
             {
 94  8
                 numbers = new ArrayList<Short>();
 95  8
                 numbers.add((short) (id3GenreId + 1));
 96  8
                 return;
 97  
             }
 98  
         }
 99  0
         numbers = new ArrayList<Short>();
 100  0
         numbers.add((short) (1));
 101  0
     }
 102  
 
 103  
     protected void build(ByteBuffer data) throws UnsupportedEncodingException
 104  
     {
 105  
         //Data actually contains a 'Data' Box so process data using this
 106  91
         Mp4BoxHeader header = new Mp4BoxHeader(data);
 107  91
         Mp4DataBox databox = new Mp4DataBox(header, data);
 108  91
         dataSize = header.getDataLength();
 109  91
         numbers = databox.getNumbers();
 110  
 
 111  91
         int genreId = numbers.get(0);
 112  
         //Get value, we have to adjust index by one because iTunes labels from one instead of zero
 113  91
         content = GenreTypes.getInstanceOf().getValueForId(genreId - 1);
 114  
 
 115  
         //Some apps set genre to invalid value, we dont disguise this by setting content to empty string we leave
 116  
         //as null so apps can handle if they wish, but we do display a warning to make them aware.
 117  91
         if (content == null)
 118  
         {
 119  1
             logger.warning(ErrorMessage.MP4_GENRE_OUT_OF_RANGE.getMsg(genreId));
 120  
         }
 121  91
     }
 122  
 }