| 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 | |
|
| 15 | |
|
| 16 | |
|
| 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 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
public static boolean isValidGenre(String genreId) |
| 32 | |
{ |
| 33 | |
|
| 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 | |
|
| 45 | 0 | } |
| 46 | |
|
| 47 | |
|
| 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 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
public Mp4GenreField(String genreId) |
| 65 | |
{ |
| 66 | 36 | super(Mp4FieldKey.GENRE.getFieldName(), genreId); |
| 67 | |
|
| 68 | |
|
| 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 | |
|
| 79 | 0 | numbers = new ArrayList<Short>(); |
| 80 | 0 | numbers.add((short) (1)); |
| 81 | 0 | return; |
| 82 | |
} |
| 83 | 8 | catch (NumberFormatException nfe) |
| 84 | |
{ |
| 85 | |
|
| 86 | |
} |
| 87 | |
|
| 88 | |
|
| 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 | |
|
| 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 | |
|
| 113 | 91 | content = GenreTypes.getInstanceOf().getValueForId(genreId - 1); |
| 114 | |
|
| 115 | |
|
| 116 | |
|
| 117 | 91 | if (content == null) |
| 118 | |
{ |
| 119 | 1 | logger.warning(ErrorMessage.MP4_GENRE_OUT_OF_RANGE.getMsg(genreId)); |
| 120 | |
} |
| 121 | 91 | } |
| 122 | |
} |