Coverage Report - org.jaudiotagger.tag.mp4.field.Mp4GenreField
 
Classes in this File Line Coverage Branch Coverage Complexity
Mp4GenreField
82%
33/40
71%
10/14
0
 
 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  35
         super(id, data);
 23  35
     }
 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  17
             short genreVal = Short.parseShort(genreId);
 37  7
             if ((genreVal - 1) <= GenreTypes.getMaxStandardGenreId())
 38  
             {
 39  7
                 return true;
 40  
             }
 41  
         }
 42  10
         catch (NumberFormatException nfe)
 43  
         {
 44  
             //Do Nothing test as String instead
 45  0
         }
 46  
 
 47  
         //Is it the String value ?
 48  10
         Integer id3GenreId = GenreTypes.getInstanceOf().getIdForValue(genreId);
 49  10
         if (id3GenreId != null)
 50  
         {
 51  4
             if (id3GenreId <= GenreTypes.getMaxStandardGenreId())
 52  
             {
 53  2
                 return true;
 54  
             }
 55  
         }
 56  8
         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  9
         super(Mp4FieldKey.GENRE.getFieldName(), genreId);
 67  
 
 68  
         //Is it an id
 69  
         try
 70  
         {
 71  9
             short genreVal = Short.parseShort(genreId);
 72  7
             if ((genreVal - 1) <= GenreTypes.getMaxStandardGenreId())
 73  
             {
 74  7
                 numbers = new ArrayList<Short>();
 75  7
                 numbers.add(genreVal);
 76  7
                 return;
 77  
             }
 78  
             //Default
 79  0
             numbers = new ArrayList<Short>();
 80  0
             numbers.add((short) (1));
 81  0
             return;
 82  
         }
 83  2
         catch (NumberFormatException nfe)
 84  
         {
 85  
             //Do Nothing test as String instead
 86  
         }
 87  
 
 88  
         //Is it the String value ?
 89  2
         Integer id3GenreId = GenreTypes.getInstanceOf().getIdForValue(genreId);
 90  2
         if (id3GenreId != null)
 91  
         {
 92  2
             if (id3GenreId.intValue() <= GenreTypes.getMaxStandardGenreId())
 93  
             {
 94  2
                 numbers = new ArrayList<Short>();
 95  2
                 numbers.add((short) (id3GenreId + 1));
 96  2
                 return;
 97  
             }
 98  
         }
 99  0
         numbers = new ArrayList<Short>();
 100  0
         numbers.add((short) (1));
 101  0
         return;
 102  
     }
 103  
 
 104  
     protected void build(ByteBuffer data) throws UnsupportedEncodingException
 105  
     {
 106  
         //Data actually contains a 'Data' Box so process data using this
 107  35
         Mp4BoxHeader header = new Mp4BoxHeader(data);
 108  35
         Mp4DataBox databox = new Mp4DataBox(header, data);
 109  35
         dataSize = header.getDataLength();
 110  35
         numbers = databox.getNumbers();
 111  
 
 112  35
         int genreId = numbers.get(0);
 113  
         //Get value, we have to adjust index by one because iTunes labels from one instead of zero
 114  35
         content = GenreTypes.getInstanceOf().getValueForId(genreId - 1);
 115  
 
 116  
         //Some apps set genre to invalid value, we dont disguise this by setting content to empty string we leave
 117  
         //as null so apps can handle if they wish, but we do display a warning to make them aware.
 118  35
         if (content == null)
 119  
         {
 120  1
             logger.warning(ErrorMessage.MP4_GENRE_OUT_OF_RANGE.getMsg(genreId));
 121  
         }
 122  35
     }
 123  
 }