~ubuntu-branches/ubuntu/utopic/libjaudiotagger-java/utopic

« back to all changes in this revision

Viewing changes to src/org/jaudiotagger/tag/vorbiscomment/VorbisCommentTag.java

  • Committer: Bazaar Package Importer
  • Author(s): Damien Raude-Morvan
  • Date: 2011-04-28 23:52:43 UTC
  • mfrom: (3.1.4 sid)
  • Revision ID: james.westby@ubuntu.com-20110428235243-pzalvw6lncis3ukf
Tags: 2.0.3-1
* d/control: Drop Depends on default-jre per Debian Java Policy as its
  a library package.
* d/watch: Fix to directly monitor SVN tags.
* Switch to 3.0 (quilt) format.
* Bump Standards-Version to 3.9.2 (no changes needed).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Entagged Audio Tag library
3
 
 * Copyright (c) 2003-2005 Rapha�l Slinckx <raphael@slinckx.net>
4
 
 * 
5
 
 * This library is free software; you can redistribute it and/or
6
 
 * modify it under the terms of the GNU Lesser General Public
7
 
 * License as published by the Free Software Foundation; either
8
 
 * version 2.1 of the License, or (at your option) any later version.
9
 
 *  
10
 
 * This library is distributed in the hope that it will be useful,
11
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
 
 * Lesser General Public License for more details.
14
 
 * 
15
 
 * You should have received a copy of the GNU Lesser General Public
16
 
 * License along with this library; if not, write to the Free Software
17
 
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
 
 */
19
 
package org.jaudiotagger.tag.vorbiscomment;
20
 
 
21
 
import org.jaudiotagger.audio.generic.AbstractTag;
22
 
import org.jaudiotagger.audio.ogg.util.VorbisHeader;
23
 
import org.jaudiotagger.logging.ErrorMessage;
24
 
import org.jaudiotagger.tag.FieldDataInvalidException;
25
 
import org.jaudiotagger.tag.FieldKey;
26
 
import org.jaudiotagger.tag.KeyNotFoundException;
27
 
import org.jaudiotagger.tag.TagField;
28
 
import org.jaudiotagger.tag.datatype.Artwork;
29
 
import static org.jaudiotagger.tag.vorbiscomment.VorbisCommentFieldKey.ALBUM;
30
 
import static org.jaudiotagger.tag.vorbiscomment.VorbisCommentFieldKey.VENDOR;
31
 
import org.jaudiotagger.tag.vorbiscomment.util.Base64Coder;
32
 
 
33
 
import java.util.ArrayList;
34
 
import java.util.EnumMap;
35
 
import java.util.List;
36
 
 
37
 
/**
38
 
 * This is the logical representation of  Vorbis Comment Data
39
 
 */
40
 
public class VorbisCommentTag extends AbstractTag
41
 
{
42
 
    private static EnumMap<FieldKey, VorbisCommentFieldKey> tagFieldToOggField = new EnumMap<FieldKey, VorbisCommentFieldKey>(FieldKey.class);
43
 
 
44
 
    static
45
 
    {
46
 
        tagFieldToOggField.put(FieldKey.ARTIST, VorbisCommentFieldKey.ARTIST);
47
 
        tagFieldToOggField.put(FieldKey.ALBUM, VorbisCommentFieldKey.ALBUM);
48
 
        tagFieldToOggField.put(FieldKey.TITLE, VorbisCommentFieldKey.TITLE);
49
 
        tagFieldToOggField.put(FieldKey.TRACK, VorbisCommentFieldKey.TRACKNUMBER);
50
 
        tagFieldToOggField.put(FieldKey.YEAR, VorbisCommentFieldKey.DATE);
51
 
        tagFieldToOggField.put(FieldKey.GENRE, VorbisCommentFieldKey.GENRE);
52
 
        tagFieldToOggField.put(FieldKey.COMMENT, VorbisCommentFieldKey.COMMENT);
53
 
        tagFieldToOggField.put(FieldKey.ALBUM_ARTIST, VorbisCommentFieldKey.ALBUMARTIST);
54
 
        tagFieldToOggField.put(FieldKey.COMPOSER, VorbisCommentFieldKey.COMPOSER);
55
 
        tagFieldToOggField.put(FieldKey.GROUPING, VorbisCommentFieldKey.GROUPING);
56
 
        tagFieldToOggField.put(FieldKey.DISC_NO, VorbisCommentFieldKey.DISCNUMBER);
57
 
        tagFieldToOggField.put(FieldKey.BPM, VorbisCommentFieldKey.BPM);
58
 
        tagFieldToOggField.put(FieldKey.MUSICBRAINZ_ARTISTID, VorbisCommentFieldKey.MUSICBRAINZ_ARTISTID);
59
 
        tagFieldToOggField.put(FieldKey.MUSICBRAINZ_RELEASEID, VorbisCommentFieldKey.MUSICBRAINZ_ALBUMID);
60
 
        tagFieldToOggField.put(FieldKey.MUSICBRAINZ_RELEASEARTISTID, VorbisCommentFieldKey.MUSICBRAINZ_ALBUMARTISTID);
61
 
        tagFieldToOggField.put(FieldKey.MUSICBRAINZ_TRACK_ID, VorbisCommentFieldKey.MUSICBRAINZ_TRACKID);
62
 
        tagFieldToOggField.put(FieldKey.MUSICBRAINZ_DISC_ID, VorbisCommentFieldKey.MUSICBRAINZ_DISCID);
63
 
        tagFieldToOggField.put(FieldKey.MUSICIP_ID, VorbisCommentFieldKey.MUSICIP_PUID);
64
 
        tagFieldToOggField.put(FieldKey.AMAZON_ID, VorbisCommentFieldKey.ASIN);
65
 
        tagFieldToOggField.put(FieldKey.MUSICBRAINZ_RELEASE_STATUS, VorbisCommentFieldKey.MUSICBRAINZ_ALBUMSTATUS);
66
 
        tagFieldToOggField.put(FieldKey.MUSICBRAINZ_RELEASE_TYPE, VorbisCommentFieldKey.MUSICBRAINZ_ALBUMTYPE);
67
 
        tagFieldToOggField.put(FieldKey.MUSICBRAINZ_RELEASE_COUNTRY, VorbisCommentFieldKey.RELEASECOUNTRY);
68
 
        tagFieldToOggField.put(FieldKey.LYRICS, VorbisCommentFieldKey.LYRICS);
69
 
        tagFieldToOggField.put(FieldKey.IS_COMPILATION, VorbisCommentFieldKey.COMPILATION);
70
 
        tagFieldToOggField.put(FieldKey.ARTIST_SORT, VorbisCommentFieldKey.ARTISTSORT);
71
 
        tagFieldToOggField.put(FieldKey.ALBUM_ARTIST_SORT, VorbisCommentFieldKey.ALBUMARTISTSORT);
72
 
        tagFieldToOggField.put(FieldKey.ALBUM_SORT, VorbisCommentFieldKey.ALBUMSORT);
73
 
        tagFieldToOggField.put(FieldKey.TITLE_SORT, VorbisCommentFieldKey.TITLESORT);
74
 
        tagFieldToOggField.put(FieldKey.COMPOSER_SORT, VorbisCommentFieldKey.COMPOSERSORT);
75
 
        tagFieldToOggField.put(FieldKey.ENCODER, VorbisCommentFieldKey.VENDOR);     //Known as vendor in VorbisComment
76
 
        tagFieldToOggField.put(FieldKey.ISRC, VorbisCommentFieldKey.ISRC);
77
 
        tagFieldToOggField.put(FieldKey.BARCODE, VorbisCommentFieldKey.BARCODE);
78
 
        tagFieldToOggField.put(FieldKey.CATALOG_NO, VorbisCommentFieldKey.CATALOGNUMBER);
79
 
        tagFieldToOggField.put(FieldKey.RECORD_LABEL, VorbisCommentFieldKey.LABEL);
80
 
        tagFieldToOggField.put(FieldKey.LYRICIST, VorbisCommentFieldKey.LYRICIST);
81
 
        tagFieldToOggField.put(FieldKey.CONDUCTOR, VorbisCommentFieldKey.CONDUCTOR);
82
 
        tagFieldToOggField.put(FieldKey.REMIXER, VorbisCommentFieldKey.REMIXER);
83
 
        tagFieldToOggField.put(FieldKey.MOOD, VorbisCommentFieldKey.MOOD);
84
 
        tagFieldToOggField.put(FieldKey.MEDIA, VorbisCommentFieldKey.MEDIA);
85
 
        tagFieldToOggField.put(FieldKey.URL_DISCOGS_ARTIST_SITE, VorbisCommentFieldKey.URL_DISCOGS_ARTIST_SITE);
86
 
        tagFieldToOggField.put(FieldKey.URL_DISCOGS_RELEASE_SITE, VorbisCommentFieldKey.URL_DISCOGS_RELEASE_SITE);
87
 
        tagFieldToOggField.put(FieldKey.URL_OFFICIAL_ARTIST_SITE, VorbisCommentFieldKey.URL_OFFICIAL_ARTIST_SITE);
88
 
        tagFieldToOggField.put(FieldKey.URL_OFFICIAL_RELEASE_SITE, VorbisCommentFieldKey.URL_OFFICIAL_RELEASE_SITE);
89
 
        tagFieldToOggField.put(FieldKey.URL_WIKIPEDIA_ARTIST_SITE, VorbisCommentFieldKey.URL_WIKIPEDIA_ARTIST_SITE);
90
 
        tagFieldToOggField.put(FieldKey.URL_WIKIPEDIA_RELEASE_SITE, VorbisCommentFieldKey.URL_WIKIPEDIA_RELEASE_SITE);
91
 
        tagFieldToOggField.put(FieldKey.LANGUAGE, VorbisCommentFieldKey.LANGUAGE);
92
 
        tagFieldToOggField.put(FieldKey.KEY, VorbisCommentFieldKey.KEY);
93
 
        tagFieldToOggField.put(FieldKey.URL_LYRICS_SITE, VorbisCommentFieldKey.URL_LYRICS_SITE);
94
 
        tagFieldToOggField.put(FieldKey.TRACK_TOTAL, VorbisCommentFieldKey.TRACKTOTAL);
95
 
        tagFieldToOggField.put(FieldKey.DISC_TOTAL, VorbisCommentFieldKey.DISCTOTAL);
96
 
 
97
 
    }
98
 
 
99
 
    //This is the vendor string that will be written if no other is supplied. Should be the name of the software
100
 
    //that actually encoded the file in the first place.
101
 
    public static final String DEFAULT_VENDOR = "jaudiotagger";
102
 
 
103
 
    /**
104
 
     * Only used within Package, hidden because it doesnt set Vendor
105
 
     * which should be done when created by end user
106
 
     */
107
 
    VorbisCommentTag()
108
 
    {
109
 
 
110
 
    }
111
 
 
112
 
    /**
113
 
     * Use to construct a new tag properly initialized
114
 
     *
115
 
     * @return
116
 
     */
117
 
    public static VorbisCommentTag createNewTag()
118
 
    {
119
 
        VorbisCommentTag tag = new VorbisCommentTag();
120
 
        tag.setVendor(DEFAULT_VENDOR);
121
 
        return tag;
122
 
    }
123
 
 
124
 
    private String getAlbumId()
125
 
    {
126
 
        return ALBUM.name();
127
 
    }
128
 
 
129
 
    /**
130
 
     * @return the vendor, generically known as the encoder
131
 
     */
132
 
    public String getVendor()
133
 
    {
134
 
        return getFirst(VENDOR.name());
135
 
    }
136
 
 
137
 
    /**
138
 
     * Set the vendor, known as the encoder  generally
139
 
     * <p/>
140
 
     * We dont want this to be blank, when written to file this field is written to a different location
141
 
     * to all other fields but user of library can just reat it as another field
142
 
     *
143
 
     * @param vendor
144
 
     */
145
 
    public void setVendor(String vendor)
146
 
    {
147
 
        if (vendor == null)
148
 
        {
149
 
            vendor = DEFAULT_VENDOR;
150
 
        }
151
 
        super.setField(new VorbisCommentTagField(VENDOR.name(), vendor));
152
 
    }
153
 
 
154
 
    protected boolean isAllowedEncoding(String enc)
155
 
    {
156
 
        return enc.equals(VorbisHeader.CHARSET_UTF_8);
157
 
    }
158
 
 
159
 
    public String toString()
160
 
    {
161
 
        return "OGG " + super.toString();
162
 
    }
163
 
 
164
 
    /**
165
 
     * Create Tag Field using generic key
166
 
     */
167
 
    @Override
168
 
    public TagField createField(FieldKey genericKey, String value) throws KeyNotFoundException,FieldDataInvalidException
169
 
    {
170
 
        if (genericKey == null)
171
 
        {
172
 
            throw new KeyNotFoundException();
173
 
        }
174
 
        return createTagField(tagFieldToOggField.get(genericKey), value);
175
 
    }
176
 
 
177
 
    /**
178
 
     * Create Tag Field using ogg key
179
 
     *
180
 
     * @param vorbisCommentFieldKey
181
 
     * @param value
182
 
     * @return
183
 
     * @throws org.jaudiotagger.tag.KeyNotFoundException
184
 
     * @throws org.jaudiotagger.tag.FieldDataInvalidException
185
 
     */
186
 
    public TagField createTagField(VorbisCommentFieldKey vorbisCommentFieldKey, String value) throws KeyNotFoundException,FieldDataInvalidException
187
 
    {
188
 
        if (value == null)
189
 
        {
190
 
            throw new IllegalArgumentException(ErrorMessage.GENERAL_INVALID_NULL_ARGUMENT.getMsg());
191
 
        }
192
 
        if (vorbisCommentFieldKey == null)
193
 
        {
194
 
            throw new KeyNotFoundException();
195
 
        }
196
 
 
197
 
        return new VorbisCommentTagField(vorbisCommentFieldKey.name(), value);
198
 
    }
199
 
 
200
 
    /**
201
 
     * Create Tag Field using ogg key
202
 
     * <p/>
203
 
     * This method is provided to allow you to create key of any value because VorbisComment allows
204
 
     * arbitary keys.
205
 
     *
206
 
     * @param vorbisCommentFieldKey
207
 
     * @param value
208
 
     * @return
209
 
     */
210
 
    public TagField createTagField(String vorbisCommentFieldKey, String value)
211
 
    {
212
 
        if (value == null)
213
 
        {
214
 
            throw new IllegalArgumentException(ErrorMessage.GENERAL_INVALID_NULL_ARGUMENT.getMsg());
215
 
        }
216
 
        return new VorbisCommentTagField(vorbisCommentFieldKey, value);
217
 
    }
218
 
 
219
 
    /**
220
 
     * Maps the generic key to the ogg key and return the list of values for this field
221
 
     *
222
 
     * @param genericKey
223
 
     */
224
 
    @Override
225
 
    public List<TagField> getFields(FieldKey genericKey) throws KeyNotFoundException
226
 
    {
227
 
        VorbisCommentFieldKey vorbisCommentFieldKey = tagFieldToOggField.get(genericKey);
228
 
        if (vorbisCommentFieldKey == null)
229
 
        {
230
 
            throw new KeyNotFoundException();
231
 
        }
232
 
        return super.get(vorbisCommentFieldKey.name());
233
 
    }
234
 
 
235
 
    /**
236
 
     * Retrieve the first value that exists for this vorbis comment key
237
 
     *
238
 
     * @param vorbisCommentKey
239
 
     * @return
240
 
     * @throws org.jaudiotagger.tag.KeyNotFoundException
241
 
     */
242
 
    public List<TagField> get(VorbisCommentFieldKey vorbisCommentKey) throws KeyNotFoundException
243
 
    {
244
 
        if (vorbisCommentKey == null)
245
 
        {
246
 
            throw new KeyNotFoundException();
247
 
        }
248
 
        return super.get(vorbisCommentKey.name());
249
 
    }
250
 
 
251
 
    /**
252
 
     * Retrieve the first value that exists for this generic key
253
 
     *
254
 
     * @param genericKey
255
 
     * @return
256
 
     */
257
 
    public String getFirst(FieldKey genericKey) throws KeyNotFoundException
258
 
    {
259
 
        VorbisCommentFieldKey vorbisCommentFieldKey = tagFieldToOggField.get(genericKey);
260
 
        if (vorbisCommentFieldKey == null)
261
 
        {
262
 
            throw new KeyNotFoundException();
263
 
        }
264
 
        return super.getFirst(vorbisCommentFieldKey.name());
265
 
    }
266
 
 
267
 
    /**
268
 
     * Retrieve the first value that exists for this vorbis comment key
269
 
     *
270
 
     * @param vorbisCommentKey
271
 
     * @return
272
 
     * @throws org.jaudiotagger.tag.KeyNotFoundException
273
 
     */
274
 
    public String getFirst(VorbisCommentFieldKey vorbisCommentKey) throws KeyNotFoundException
275
 
    {
276
 
        if (vorbisCommentKey == null)
277
 
        {
278
 
            throw new KeyNotFoundException();
279
 
        }
280
 
        return super.getFirst(vorbisCommentKey.name());
281
 
    }
282
 
 
283
 
    /**
284
 
     * Delete fields with this generic key
285
 
     *
286
 
     * @param genericKey
287
 
     */
288
 
    public void deleteField(FieldKey genericKey) throws KeyNotFoundException
289
 
    {
290
 
        if (genericKey == null)
291
 
        {
292
 
            throw new KeyNotFoundException();
293
 
        }
294
 
        VorbisCommentFieldKey vorbisCommentFieldKey = tagFieldToOggField.get(genericKey);
295
 
        deleteTagField(vorbisCommentFieldKey);
296
 
    }
297
 
 
298
 
    /**
299
 
     * Delete fields with this vorbisCommentFieldKey
300
 
     *
301
 
     * @param vorbisCommentFieldKey
302
 
     * @throws org.jaudiotagger.tag.KeyNotFoundException
303
 
     */
304
 
    public void deleteTagField(VorbisCommentFieldKey vorbisCommentFieldKey) throws KeyNotFoundException
305
 
    {
306
 
        if (vorbisCommentFieldKey == null)
307
 
        {
308
 
            throw new KeyNotFoundException();
309
 
        }
310
 
        super.deleteField(vorbisCommentFieldKey.name());
311
 
    }
312
 
 
313
 
    /**
314
 
     * Create artwork field
315
 
     * <p/>
316
 
     * Actually create two fields , the data field and the mimetype
317
 
     *
318
 
     * @param data     raw image data
319
 
     * @param mimeType mimeType of data
320
 
     *                 <p/>
321
 
     *                 TODO could possibly work out mimetype from data, but unlike mp4 there is nothing to restrict to only png
322
 
     *                 or jpeg images
323
 
     * @return
324
 
     */
325
 
    public void setArtworkField(byte[] data, String mimeType)
326
 
    {
327
 
        char[] testdata = Base64Coder.encode(data);
328
 
        String base64image = new String(testdata);
329
 
        VorbisCommentTagField dataField = new VorbisCommentTagField(VorbisCommentFieldKey.COVERART.name(), base64image);
330
 
        VorbisCommentTagField mimeField = new VorbisCommentTagField(VorbisCommentFieldKey.COVERARTMIME.name(), mimeType);
331
 
 
332
 
        setField(dataField);
333
 
        setField(mimeField);
334
 
 
335
 
    }
336
 
 
337
 
    /**
338
 
     * Retrieve artwork raw data
339
 
     *
340
 
     * @return
341
 
     */
342
 
    public byte[] getArtworkBinaryData()
343
 
    {
344
 
        String base64data = this.getFirst(VorbisCommentFieldKey.COVERART);
345
 
        byte[] rawdata = Base64Coder.decode(base64data.toCharArray());
346
 
        return rawdata;
347
 
    }
348
 
 
349
 
    /**
350
 
     * @return mimetype
351
 
     */
352
 
    public String getArtworkMimeType()
353
 
    {
354
 
        return this.getFirst(VorbisCommentFieldKey.COVERARTMIME);
355
 
    }
356
 
 
357
 
    /**
358
 
     * Is this tag empty
359
 
     * <p/>
360
 
     * <p>Overridden because check for size of one because there is always a vendor tag unless just
361
 
     * created an empty vorbis tag as part of flac tag in which case size could be zero
362
 
     *
363
 
     * @see org.jaudiotagger.tag.Tag#isEmpty()
364
 
     */
365
 
    public boolean isEmpty()
366
 
    {
367
 
        return fields.size() <= 1;
368
 
    }
369
 
 
370
 
    /**
371
 
     * Add Field
372
 
     * <p/>
373
 
     * <p>Overidden because there can only be one vendor set
374
 
     *
375
 
     * @param field
376
 
     */
377
 
    public void addField(TagField field)
378
 
    {
379
 
        if (field.getId().equals(VorbisCommentFieldKey.VENDOR.name()))
380
 
        {
381
 
            super.setField(field);
382
 
        }
383
 
        else
384
 
        {
385
 
            super.addField(field);
386
 
        }
387
 
    }
388
 
 
389
 
     public TagField getFirstField(FieldKey genericKey) throws KeyNotFoundException
390
 
    {
391
 
        if (genericKey == null)
392
 
        {
393
 
            throw new KeyNotFoundException();
394
 
        }
395
 
        return getFirstField(tagFieldToOggField.get(genericKey).name());
396
 
    }
397
 
 
398
 
    public List<Artwork> getArtworkList()
399
 
    {
400
 
        List<Artwork>  artworkList  = new ArrayList<Artwork>(1);
401
 
 
402
 
        if(getArtworkBinaryData()!=null & getArtworkBinaryData().length>0)
403
 
        {
404
 
            Artwork artwork=new Artwork();
405
 
            artwork.setMimeType(getArtworkMimeType());
406
 
            artwork.setBinaryData(getArtworkBinaryData());
407
 
            artworkList.add(artwork);
408
 
        }
409
 
        return artworkList;
410
 
    }
411
 
 
412
 
    /**
413
 
     * Create artwork field
414
 
     *
415
 
     * Not supported because reuire two fields to be created use
416
 
     * @return
417
 
     */
418
 
    public TagField createField(Artwork artwork) throws FieldDataInvalidException
419
 
    {
420
 
        throw new UnsupportedOperationException("Please use setField instead");
421
 
    }
422
 
 
423
 
    /**
424
 
     * Create artwork field
425
 
     *
426
 
     * Actually sets two fields
427
 
     *
428
 
     * @return
429
 
     */
430
 
    @Override
431
 
    public void setField(Artwork artwork) throws FieldDataInvalidException
432
 
    {
433
 
        char[] testdata = Base64Coder.encode(artwork.getBinaryData());
434
 
                String base64image = new String(testdata);
435
 
            TagField imageTagField  = createTagField(VorbisCommentFieldKey.COVERART, base64image);
436
 
                TagField imageTypeField = createTagField(VorbisCommentFieldKey.COVERARTMIME, artwork.getMimeType());
437
 
 
438
 
        this.setField(imageTagField);
439
 
        this.setField(imageTypeField);
440
 
    }
441
 
 
442
 
     /**
443
 
     * Delete all instance of artwork Field
444
 
     *
445
 
     * @throws KeyNotFoundException
446
 
     */
447
 
    public void deleteArtworkField() throws KeyNotFoundException
448
 
    {
449
 
        this.deleteTagField(VorbisCommentFieldKey.COVERART);
450
 
        this.deleteTagField(VorbisCommentFieldKey.COVERARTMIME);
451
 
    }
452
 
}
453
 
 
 
1
/*
 
2
 * Entagged Audio Tag library
 
3
 * Copyright (c) 2003-2005 Rapha�l Slinckx <raphael@slinckx.net>
 
4
 * 
 
5
 * This library is free software; you can redistribute it and/or
 
6
 * modify it under the terms of the GNU Lesser General Public
 
7
 * License as published by the Free Software Foundation; either
 
8
 * version 2.1 of the License, or (at your option) any later version.
 
9
 *  
 
10
 * This library is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
13
 * Lesser General Public License for more details.
 
14
 * 
 
15
 * You should have received a copy of the GNU Lesser General Public
 
16
 * License along with this library; if not, write to the Free Software
 
17
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
18
 */
 
19
package org.jaudiotagger.tag.vorbiscomment;
 
20
 
 
21
import org.jaudiotagger.audio.flac.metadatablock.MetadataBlockDataPicture;
 
22
import org.jaudiotagger.audio.generic.AbstractTag;
 
23
import org.jaudiotagger.audio.generic.Utils;
 
24
import org.jaudiotagger.audio.ogg.util.VorbisHeader;
 
25
import org.jaudiotagger.logging.ErrorMessage;
 
26
import org.jaudiotagger.tag.*;
 
27
import org.jaudiotagger.tag.datatype.Artwork;
 
28
 
 
29
import static org.jaudiotagger.tag.vorbiscomment.VorbisCommentFieldKey.VENDOR;
 
30
 
 
31
import org.jaudiotagger.tag.id3.valuepair.TextEncoding;
 
32
import org.jaudiotagger.tag.reference.Tagger;
 
33
import org.jaudiotagger.tag.vorbiscomment.util.Base64Coder;
 
34
 
 
35
import java.awt.image.BufferedImage;
 
36
import java.io.IOException;
 
37
import java.io.UnsupportedEncodingException;
 
38
import java.nio.ByteBuffer;
 
39
import java.util.ArrayList;
 
40
import java.util.EnumMap;
 
41
import java.util.EnumSet;
 
42
import java.util.List;
 
43
 
 
44
/**
 
45
 * This is the logical representation of  Vorbis Comment Data
 
46
 */
 
47
public class VorbisCommentTag extends AbstractTag
 
48
{
 
49
    private static EnumMap<FieldKey, VorbisCommentFieldKey> tagFieldToOggField = new EnumMap<FieldKey, VorbisCommentFieldKey>(FieldKey.class);
 
50
 
 
51
    static
 
52
    {
 
53
        tagFieldToOggField.put(FieldKey.ALBUM, VorbisCommentFieldKey.ALBUM);
 
54
        tagFieldToOggField.put(FieldKey.ALBUM_ARTIST, VorbisCommentFieldKey.ALBUMARTIST);
 
55
        tagFieldToOggField.put(FieldKey.ALBUM_ARTIST_SORT, VorbisCommentFieldKey.ALBUMARTISTSORT);
 
56
        tagFieldToOggField.put(FieldKey.ALBUM_SORT, VorbisCommentFieldKey.ALBUMSORT);
 
57
        tagFieldToOggField.put(FieldKey.ARTIST, VorbisCommentFieldKey.ARTIST);
 
58
        tagFieldToOggField.put(FieldKey.AMAZON_ID, VorbisCommentFieldKey.ASIN);
 
59
        tagFieldToOggField.put(FieldKey.ARTIST_SORT, VorbisCommentFieldKey.ARTISTSORT);
 
60
        tagFieldToOggField.put(FieldKey.BARCODE, VorbisCommentFieldKey.BARCODE);
 
61
        tagFieldToOggField.put(FieldKey.BPM, VorbisCommentFieldKey.BPM);
 
62
        tagFieldToOggField.put(FieldKey.CATALOG_NO, VorbisCommentFieldKey.CATALOGNUMBER);
 
63
        tagFieldToOggField.put(FieldKey.COMMENT, VorbisCommentFieldKey.COMMENT);
 
64
        tagFieldToOggField.put(FieldKey.COMPOSER, VorbisCommentFieldKey.COMPOSER);
 
65
        tagFieldToOggField.put(FieldKey.COMPOSER_SORT, VorbisCommentFieldKey.COMPOSERSORT);
 
66
        tagFieldToOggField.put(FieldKey.CONDUCTOR, VorbisCommentFieldKey.CONDUCTOR);
 
67
        tagFieldToOggField.put(FieldKey.COVER_ART, VorbisCommentFieldKey.METADATA_BLOCK_PICTURE);
 
68
        tagFieldToOggField.put(FieldKey.CUSTOM1, VorbisCommentFieldKey.CUSTOM1);
 
69
        tagFieldToOggField.put(FieldKey.CUSTOM2, VorbisCommentFieldKey.CUSTOM2);
 
70
        tagFieldToOggField.put(FieldKey.CUSTOM3, VorbisCommentFieldKey.CUSTOM3);
 
71
        tagFieldToOggField.put(FieldKey.CUSTOM4, VorbisCommentFieldKey.CUSTOM4);
 
72
        tagFieldToOggField.put(FieldKey.CUSTOM5, VorbisCommentFieldKey.CUSTOM5);                                        
 
73
        tagFieldToOggField.put(FieldKey.DISC_NO, VorbisCommentFieldKey.DISCNUMBER);
 
74
        tagFieldToOggField.put(FieldKey.DISC_TOTAL, VorbisCommentFieldKey.DISCTOTAL);
 
75
        tagFieldToOggField.put(FieldKey.ENCODER, VorbisCommentFieldKey.VENDOR);     //Known as vendor in VorbisComment
 
76
        tagFieldToOggField.put(FieldKey.FBPM, VorbisCommentFieldKey.FBPM);
 
77
        tagFieldToOggField.put(FieldKey.GENRE, VorbisCommentFieldKey.GENRE);
 
78
        tagFieldToOggField.put(FieldKey.GROUPING, VorbisCommentFieldKey.GROUPING);
 
79
        tagFieldToOggField.put(FieldKey.ISRC, VorbisCommentFieldKey.ISRC);
 
80
        tagFieldToOggField.put(FieldKey.IS_COMPILATION, VorbisCommentFieldKey.COMPILATION);
 
81
        tagFieldToOggField.put(FieldKey.KEY, VorbisCommentFieldKey.KEY);
 
82
        tagFieldToOggField.put(FieldKey.LANGUAGE, VorbisCommentFieldKey.LANGUAGE);
 
83
        tagFieldToOggField.put(FieldKey.LYRICIST, VorbisCommentFieldKey.LYRICIST);
 
84
        tagFieldToOggField.put(FieldKey.LYRICS, VorbisCommentFieldKey.LYRICS);
 
85
        tagFieldToOggField.put(FieldKey.MEDIA, VorbisCommentFieldKey.MEDIA);
 
86
        tagFieldToOggField.put(FieldKey.MOOD, VorbisCommentFieldKey.MOOD);
 
87
        tagFieldToOggField.put(FieldKey.MUSICBRAINZ_ARTISTID, VorbisCommentFieldKey.MUSICBRAINZ_ARTISTID);
 
88
        tagFieldToOggField.put(FieldKey.MUSICBRAINZ_DISC_ID, VorbisCommentFieldKey.MUSICBRAINZ_DISCID);
 
89
        tagFieldToOggField.put(FieldKey.MUSICBRAINZ_RELEASEARTISTID, VorbisCommentFieldKey.MUSICBRAINZ_ALBUMARTISTID);
 
90
        tagFieldToOggField.put(FieldKey.MUSICBRAINZ_RELEASEID, VorbisCommentFieldKey.MUSICBRAINZ_ALBUMID);
 
91
        tagFieldToOggField.put(FieldKey.MUSICBRAINZ_RELEASE_GROUP_ID, VorbisCommentFieldKey.MUSICBRAINZ_RELEASEGROUPID);
 
92
        tagFieldToOggField.put(FieldKey.MUSICBRAINZ_RELEASE_COUNTRY, VorbisCommentFieldKey.RELEASECOUNTRY);
 
93
        tagFieldToOggField.put(FieldKey.MUSICBRAINZ_RELEASE_STATUS, VorbisCommentFieldKey.MUSICBRAINZ_ALBUMSTATUS);
 
94
        tagFieldToOggField.put(FieldKey.MUSICBRAINZ_RELEASE_TYPE, VorbisCommentFieldKey.MUSICBRAINZ_ALBUMTYPE);
 
95
        tagFieldToOggField.put(FieldKey.MUSICBRAINZ_TRACK_ID, VorbisCommentFieldKey.MUSICBRAINZ_TRACKID);
 
96
        tagFieldToOggField.put(FieldKey.MUSICBRAINZ_WORK_ID, VorbisCommentFieldKey.MUSICBRAINZ_WORKID);
 
97
        tagFieldToOggField.put(FieldKey.OCCASION, VorbisCommentFieldKey.OCCASION);
 
98
        tagFieldToOggField.put(FieldKey.ORIGINAL_ALBUM, VorbisCommentFieldKey.ORIGINAL_ALBUM);
 
99
        tagFieldToOggField.put(FieldKey.ORIGINAL_ARTIST, VorbisCommentFieldKey.ORIGINAL_ARTIST);
 
100
        tagFieldToOggField.put(FieldKey.ORIGINAL_LYRICIST, VorbisCommentFieldKey.ORIGINAL_LYRICIST);
 
101
        tagFieldToOggField.put(FieldKey.ORIGINAL_YEAR, VorbisCommentFieldKey.ORIGINAL_YEAR);
 
102
        tagFieldToOggField.put(FieldKey.MUSICIP_ID, VorbisCommentFieldKey.MUSICIP_PUID);
 
103
        tagFieldToOggField.put(FieldKey.QUALITY, VorbisCommentFieldKey.QUALITY);
 
104
        tagFieldToOggField.put(FieldKey.RATING, VorbisCommentFieldKey.RATING);
 
105
        tagFieldToOggField.put(FieldKey.RECORD_LABEL, VorbisCommentFieldKey.LABEL);
 
106
        tagFieldToOggField.put(FieldKey.REMIXER, VorbisCommentFieldKey.REMIXER);
 
107
        tagFieldToOggField.put(FieldKey.TAGS, VorbisCommentFieldKey.TAGS);
 
108
        tagFieldToOggField.put(FieldKey.SCRIPT, VorbisCommentFieldKey.SCRIPT);
 
109
        tagFieldToOggField.put(FieldKey.TEMPO, VorbisCommentFieldKey.TEMPO);
 
110
        tagFieldToOggField.put(FieldKey.TITLE, VorbisCommentFieldKey.TITLE);
 
111
        tagFieldToOggField.put(FieldKey.TITLE_SORT, VorbisCommentFieldKey.TITLESORT);
 
112
        tagFieldToOggField.put(FieldKey.TRACK, VorbisCommentFieldKey.TRACKNUMBER);
 
113
        tagFieldToOggField.put(FieldKey.TRACK_TOTAL, VorbisCommentFieldKey.TRACKTOTAL);
 
114
        tagFieldToOggField.put(FieldKey.URL_DISCOGS_ARTIST_SITE, VorbisCommentFieldKey.URL_DISCOGS_ARTIST_SITE);
 
115
        tagFieldToOggField.put(FieldKey.URL_DISCOGS_RELEASE_SITE, VorbisCommentFieldKey.URL_DISCOGS_RELEASE_SITE);
 
116
        tagFieldToOggField.put(FieldKey.URL_LYRICS_SITE, VorbisCommentFieldKey.URL_LYRICS_SITE);
 
117
        tagFieldToOggField.put(FieldKey.URL_OFFICIAL_ARTIST_SITE, VorbisCommentFieldKey.URL_OFFICIAL_ARTIST_SITE);
 
118
        tagFieldToOggField.put(FieldKey.URL_OFFICIAL_RELEASE_SITE, VorbisCommentFieldKey.URL_OFFICIAL_RELEASE_SITE);
 
119
        tagFieldToOggField.put(FieldKey.URL_WIKIPEDIA_ARTIST_SITE, VorbisCommentFieldKey.URL_WIKIPEDIA_ARTIST_SITE);
 
120
        tagFieldToOggField.put(FieldKey.URL_WIKIPEDIA_RELEASE_SITE, VorbisCommentFieldKey.URL_WIKIPEDIA_RELEASE_SITE);
 
121
        tagFieldToOggField.put(FieldKey.YEAR, VorbisCommentFieldKey.DATE);
 
122
 
 
123
        tagFieldToOggField.put(FieldKey.ENGINEER, VorbisCommentFieldKey.ENGINEER);
 
124
        tagFieldToOggField.put(FieldKey.PRODUCER, VorbisCommentFieldKey.PRODUCER);
 
125
        tagFieldToOggField.put(FieldKey.DJMIXER, VorbisCommentFieldKey.DJMIXER);
 
126
        tagFieldToOggField.put(FieldKey.MIXER, VorbisCommentFieldKey.MIXER);
 
127
        tagFieldToOggField.put(FieldKey.ARRANGER, VorbisCommentFieldKey.ARRANGER);
 
128
    }
 
129
 
 
130
    //This is the vendor string that will be written if no other is supplied. Should be the name of the software
 
131
    //that actually encoded the file in the first place.
 
132
    public static final String DEFAULT_VENDOR = "jaudiotagger";
 
133
 
 
134
    /**
 
135
     * Only used within Package, hidden because it doesnt set Vendor
 
136
     * which should be done when created by end user
 
137
     */
 
138
    VorbisCommentTag()
 
139
    {
 
140
 
 
141
    }
 
142
 
 
143
    /**
 
144
     * Use to construct a new tag properly initialized
 
145
     *
 
146
     * @return
 
147
     */
 
148
    public static VorbisCommentTag createNewTag()
 
149
    {
 
150
        VorbisCommentTag tag = new VorbisCommentTag();
 
151
        tag.setVendor(DEFAULT_VENDOR);
 
152
        return tag;
 
153
    }
 
154
 
 
155
    /**
 
156
     * @return the vendor, generically known as the encoder
 
157
     */
 
158
    public String getVendor()
 
159
    {
 
160
        return getFirst(VENDOR.getFieldName());
 
161
    }
 
162
 
 
163
    /**
 
164
     * Set the vendor, known as the encoder  generally
 
165
     * <p/>
 
166
     * We dont want this to be blank, when written to file this field is written to a different location
 
167
     * to all other fields but user of library can just reat it as another field
 
168
     *
 
169
     * @param vendor
 
170
     */
 
171
    public void setVendor(String vendor)
 
172
    {
 
173
        if (vendor == null)
 
174
        {
 
175
            vendor = DEFAULT_VENDOR;
 
176
        }
 
177
        super.setField(new VorbisCommentTagField(VENDOR.getFieldName(), vendor));
 
178
    }
 
179
 
 
180
    protected boolean isAllowedEncoding(String enc)
 
181
    {
 
182
        return enc.equals(VorbisHeader.CHARSET_UTF_8);
 
183
    }
 
184
 
 
185
    public String toString()
 
186
    {
 
187
        return "OGG " + super.toString();
 
188
    }
 
189
 
 
190
    /**
 
191
     * Create Tag Field using generic key
 
192
     */
 
193
    @Override
 
194
    public TagField createField(FieldKey genericKey, String value) throws KeyNotFoundException,FieldDataInvalidException
 
195
    {
 
196
        if (genericKey == null)
 
197
        {
 
198
            throw new KeyNotFoundException();
 
199
        }
 
200
        return createField(tagFieldToOggField.get(genericKey), value);
 
201
    }
 
202
 
 
203
    /**
 
204
     * Create Tag Field using ogg key
 
205
     *
 
206
     * @param vorbisCommentFieldKey
 
207
     * @param value
 
208
     * @return
 
209
     * @throws org.jaudiotagger.tag.KeyNotFoundException
 
210
     * @throws org.jaudiotagger.tag.FieldDataInvalidException
 
211
     */
 
212
    public TagField createField(VorbisCommentFieldKey vorbisCommentFieldKey, String value) throws KeyNotFoundException,FieldDataInvalidException
 
213
    {
 
214
        if (value == null)
 
215
        {
 
216
            throw new IllegalArgumentException(ErrorMessage.GENERAL_INVALID_NULL_ARGUMENT.getMsg());
 
217
        }
 
218
        if (vorbisCommentFieldKey == null)
 
219
        {
 
220
            throw new KeyNotFoundException();
 
221
        }
 
222
 
 
223
        return new VorbisCommentTagField(vorbisCommentFieldKey.getFieldName(), value);
 
224
    }
 
225
 
 
226
    /**
 
227
     * Create Tag Field using ogg key
 
228
     * <p/>
 
229
     * This method is provided to allow you to create key of any value because VorbisComment allows
 
230
     * arbitary keys.
 
231
     *
 
232
     * @param vorbisCommentFieldKey
 
233
     * @param value
 
234
     * @return
 
235
     */
 
236
    public TagField createField(String vorbisCommentFieldKey, String value)
 
237
    {
 
238
        if (value == null)
 
239
        {
 
240
            throw new IllegalArgumentException(ErrorMessage.GENERAL_INVALID_NULL_ARGUMENT.getMsg());
 
241
        }
 
242
        return new VorbisCommentTagField(vorbisCommentFieldKey, value);
 
243
    }
 
244
 
 
245
    /**
 
246
     * Maps the generic key to the ogg key and return the list of values for this field
 
247
     *
 
248
     * @param genericKey
 
249
     */
 
250
    @Override
 
251
    public List<TagField> getFields(FieldKey genericKey) throws KeyNotFoundException
 
252
    {
 
253
        VorbisCommentFieldKey vorbisCommentFieldKey = tagFieldToOggField.get(genericKey);
 
254
        if (vorbisCommentFieldKey == null)
 
255
        {
 
256
            throw new KeyNotFoundException();
 
257
        }
 
258
        return super.getFields(vorbisCommentFieldKey.getFieldName());
 
259
    }
 
260
 
 
261
    /**
 
262
     * Retrieve the first value that exists for this vorbis comment key
 
263
     *
 
264
     * @param vorbisCommentKey
 
265
     * @return
 
266
     * @throws org.jaudiotagger.tag.KeyNotFoundException
 
267
     */
 
268
    public List<TagField> get(VorbisCommentFieldKey vorbisCommentKey) throws KeyNotFoundException
 
269
    {
 
270
        if (vorbisCommentKey == null)
 
271
        {
 
272
            throw new KeyNotFoundException();
 
273
        }
 
274
        return super.getFields(vorbisCommentKey.getFieldName());
 
275
    }
 
276
 
 
277
    public String getValue(FieldKey genericKey,int index) throws KeyNotFoundException
 
278
    {
 
279
        VorbisCommentFieldKey vorbisCommentFieldKey = tagFieldToOggField.get(genericKey);
 
280
        if (vorbisCommentFieldKey == null)
 
281
        {
 
282
            throw new KeyNotFoundException();
 
283
        }
 
284
        return super.getItem(vorbisCommentFieldKey.getFieldName(),index);
 
285
    }
 
286
 
 
287
    /**
 
288
     * Retrieve the first value that exists for this vorbis comment key
 
289
     *
 
290
     * @param vorbisCommentKey
 
291
     * @return
 
292
     * @throws org.jaudiotagger.tag.KeyNotFoundException
 
293
     */
 
294
    public String getFirst(VorbisCommentFieldKey vorbisCommentKey) throws KeyNotFoundException
 
295
    {
 
296
        if (vorbisCommentKey == null)
 
297
        {
 
298
            throw new KeyNotFoundException();
 
299
        }
 
300
        return super.getFirst(vorbisCommentKey.getFieldName());
 
301
    }
 
302
 
 
303
    /**
 
304
     * Delete fields with this generic key
 
305
     *
 
306
     * @param genericKey
 
307
     */
 
308
    public void deleteField(FieldKey genericKey) throws KeyNotFoundException
 
309
    {
 
310
        if (genericKey == null)
 
311
        {
 
312
            throw new KeyNotFoundException();
 
313
        }
 
314
        VorbisCommentFieldKey vorbisCommentFieldKey = tagFieldToOggField.get(genericKey);
 
315
        deleteField(vorbisCommentFieldKey);
 
316
    }
 
317
 
 
318
    /**
 
319
     * Delete fields with this vorbisCommentFieldKey
 
320
     *
 
321
     * @param vorbisCommentFieldKey
 
322
     * @throws org.jaudiotagger.tag.KeyNotFoundException
 
323
     */
 
324
    public void deleteField(VorbisCommentFieldKey vorbisCommentFieldKey) throws KeyNotFoundException
 
325
    {
 
326
        if (vorbisCommentFieldKey == null)
 
327
        {
 
328
            throw new KeyNotFoundException();
 
329
        }
 
330
        super.deleteField(vorbisCommentFieldKey.getFieldName());
 
331
    }
 
332
 
 
333
 
 
334
 
 
335
    /**
 
336
     * Retrieve artwork raw data when using the deprecated COVERART format
 
337
     *
 
338
     * @return
 
339
     */
 
340
    public byte[] getArtworkBinaryData()
 
341
    {
 
342
        String base64data = this.getFirst(VorbisCommentFieldKey.COVERART);
 
343
        byte[] rawdata = Base64Coder.decode(base64data.toCharArray());
 
344
        return rawdata;
 
345
    }
 
346
 
 
347
    /**
 
348
     * Retrieve artwork mimeType when using deprecated COVERART format
 
349
     *
 
350
     * @return mimetype
 
351
     */
 
352
    public String getArtworkMimeType()
 
353
    {
 
354
        return this.getFirst(VorbisCommentFieldKey.COVERARTMIME);
 
355
    }
 
356
 
 
357
    /**
 
358
     * Is this tag empty
 
359
     * <p/>
 
360
     * <p>Overridden because check for size of one because there is always a vendor tag unless just
 
361
     * created an empty vorbis tag as part of flac tag in which case size could be zero
 
362
     *
 
363
     * @see org.jaudiotagger.tag.Tag#isEmpty()
 
364
     */
 
365
    public boolean isEmpty()
 
366
    {
 
367
        return fields.size() <= 1;
 
368
    }
 
369
 
 
370
    /**
 
371
     * Add Field
 
372
     * <p/>
 
373
     * <p>Overidden because there can only be one vendor set
 
374
     *
 
375
     * @param field
 
376
     */
 
377
    public void addField(TagField field)
 
378
    {
 
379
        if (field.getId().equals(VorbisCommentFieldKey.VENDOR.getFieldName()))
 
380
        {
 
381
            super.setField(field);
 
382
        }
 
383
        else
 
384
        {
 
385
            super.addField(field);
 
386
        }
 
387
    }
 
388
 
 
389
     public TagField getFirstField(FieldKey genericKey) throws KeyNotFoundException
 
390
    {
 
391
        if (genericKey == null)
 
392
        {
 
393
            throw new KeyNotFoundException();
 
394
        }
 
395
        return getFirstField(tagFieldToOggField.get(genericKey).getFieldName());
 
396
    }
 
397
 
 
398
    /**
 
399
     *
 
400
     * @return list of artwork images
 
401
     */
 
402
    public List<Artwork> getArtworkList()
 
403
    {
 
404
        List<Artwork>  artworkList  = new ArrayList<Artwork>(1);
 
405
 
 
406
        //Read Old Format
 
407
        if(getArtworkBinaryData()!=null & getArtworkBinaryData().length>0)
 
408
        {
 
409
            Artwork artwork=new Artwork();
 
410
            artwork.setMimeType(getArtworkMimeType());
 
411
            artwork.setBinaryData(getArtworkBinaryData());
 
412
            artworkList.add(artwork);
 
413
        }
 
414
 
 
415
        //New Format (Supports Multiple Images)
 
416
        List<TagField> metadataBlockPics = this.get(VorbisCommentFieldKey.METADATA_BLOCK_PICTURE);
 
417
        for(TagField tagField:metadataBlockPics)
 
418
        {
 
419
 
 
420
            try
 
421
            {
 
422
                byte[] imageBinaryData = Base64Coder.decode(((TagTextField)tagField).getContent());
 
423
                MetadataBlockDataPicture coverArt = new MetadataBlockDataPicture(ByteBuffer.wrap(imageBinaryData));
 
424
                Artwork artwork=Artwork.createArtworkFromMetadataBlockDataPicture(coverArt);
 
425
                artworkList.add(artwork);
 
426
            }
 
427
            catch(IOException ioe)
 
428
            {
 
429
                throw new RuntimeException(ioe);
 
430
            }
 
431
            catch(InvalidFrameException ife)
 
432
            {
 
433
                throw new RuntimeException(ife);
 
434
            }
 
435
        }
 
436
        return artworkList;
 
437
    }
 
438
 
 
439
 
 
440
    /**
 
441
       * Create MetadataBlockPicture field, this is the preferred way of storing artwork in VorbisComment tag now but
 
442
       * has to be base encoded to be stored in VorbisComment
 
443
       *
 
444
       * @return MetadataBlockDataPicture
 
445
     */
 
446
      private MetadataBlockDataPicture createMetadataBlockDataPicture(Artwork artwork) throws FieldDataInvalidException
 
447
      {
 
448
          if(artwork.isLinked())
 
449
          {
 
450
               return new MetadataBlockDataPicture(
 
451
                      Utils.getDefaultBytes(artwork.getImageUrl(), TextEncoding.CHARSET_ISO_8859_1),
 
452
                      artwork.getPictureType(),
 
453
                      MetadataBlockDataPicture.IMAGE_IS_URL,
 
454
                      "",
 
455
                      0,
 
456
                      0,
 
457
                      0,
 
458
                      0);
 
459
          }
 
460
          else
 
461
          {
 
462
              BufferedImage image;
 
463
              try
 
464
              {
 
465
                  image = artwork.getImage();
 
466
              }
 
467
              catch(IOException ioe)
 
468
              {
 
469
                  throw new FieldDataInvalidException("Unable to create MetadataBlockDataPicture from buffered:"+ioe.getMessage());
 
470
              }
 
471
 
 
472
              return new MetadataBlockDataPicture(artwork.getBinaryData(),
 
473
                      artwork.getPictureType(),
 
474
                      artwork.getMimeType(),
 
475
                      artwork.getDescription(),
 
476
                      image.getWidth(),
 
477
                      image.getHeight(),
 
478
                      0,
 
479
                      0);
 
480
          }
 
481
      }
 
482
 
 
483
    /**
 
484
     * Create Artwork field
 
485
     *
 
486
     * @param artwork
 
487
     * @return
 
488
     * @throws FieldDataInvalidException
 
489
     */
 
490
      public TagField createField(Artwork artwork) throws FieldDataInvalidException
 
491
      {
 
492
        try
 
493
        {
 
494
            char[] testdata = Base64Coder.encode(createMetadataBlockDataPicture(artwork).getRawContent());
 
495
            String base64image = new String(testdata);
 
496
            TagField imageTagField  = createField(VorbisCommentFieldKey.METADATA_BLOCK_PICTURE, base64image);
 
497
            return imageTagField;
 
498
        }
 
499
        catch(UnsupportedEncodingException uee)
 
500
        {
 
501
            throw new RuntimeException(uee);
 
502
        }
 
503
    }
 
504
 
 
505
    /**
 
506
     * Create and set artwork field
 
507
     *
 
508
     * @return
 
509
     */
 
510
    @Override
 
511
    public void setField(Artwork artwork) throws FieldDataInvalidException
 
512
    {
 
513
        //Set field
 
514
        this.setField(createField(artwork));
 
515
 
 
516
        //If worked okay above then that should be first artwork and if we still had old coverart format
 
517
        //that should be removed
 
518
        if(this.getFirst(VorbisCommentFieldKey.COVERART).length()>0)
 
519
        {
 
520
            this.deleteField(VorbisCommentFieldKey.COVERART);
 
521
            this.deleteField(VorbisCommentFieldKey.COVERARTMIME);
 
522
        }
 
523
    }
 
524
 
 
525
    /**
 
526
     * Add artwork field
 
527
     *
 
528
     * @param artwork
 
529
     * @throws FieldDataInvalidException
 
530
     */
 
531
    public void addField(Artwork artwork) throws FieldDataInvalidException
 
532
    {
 
533
        this.addField(createField(artwork));
 
534
    }
 
535
 
 
536
     /**
 
537
     * Create artwork field using the non-standard COVERART tag
 
538
     *
 
539
     * <p/>
 
540
     * Actually create two fields , the data field and the mimetype. Its is not recommended that you use this
 
541
     * method anymore.
 
542
      *
 
543
     * @param data     raw image data
 
544
     * @param mimeType mimeType of data
 
545
     *                 <p/>
 
546
     * @return
 
547
     */
 
548
    @Deprecated
 
549
    public void setArtworkField(byte[] data, String mimeType)
 
550
    {
 
551
        char[] testdata = Base64Coder.encode(data);
 
552
        String base64image = new String(testdata);
 
553
        VorbisCommentTagField dataField = new VorbisCommentTagField(VorbisCommentFieldKey.COVERART.getFieldName(), base64image);
 
554
        VorbisCommentTagField mimeField = new VorbisCommentTagField(VorbisCommentFieldKey.COVERARTMIME.getFieldName(), mimeType);
 
555
 
 
556
        setField(dataField);
 
557
        setField(mimeField);
 
558
 
 
559
    }
 
560
 
 
561
    /**
 
562
     * Create and set field with name of vorbisCommentkey
 
563
     *
 
564
     * @param vorbisCommentKey
 
565
     * @param value
 
566
     * @throws KeyNotFoundException
 
567
     * @throws FieldDataInvalidException
 
568
     */
 
569
    public void setField(String vorbisCommentKey, String value) throws KeyNotFoundException, FieldDataInvalidException
 
570
    {
 
571
        TagField tagfield = createField(vorbisCommentKey,value);
 
572
        setField(tagfield);
 
573
    }
 
574
 
 
575
    /**
 
576
     * Create and add field with name of vorbisCommentkey
 
577
     * @param vorbisCommentKey
 
578
     * @param value
 
579
     * @throws KeyNotFoundException
 
580
     * @throws FieldDataInvalidException
 
581
     */
 
582
    public void addField(String vorbisCommentKey, String value) throws KeyNotFoundException, FieldDataInvalidException
 
583
    {
 
584
        TagField tagfield = createField(vorbisCommentKey,value);
 
585
        addField(tagfield);
 
586
    }
 
587
 
 
588
     /**
 
589
     * Delete all instance of artwork Field
 
590
     *
 
591
     * @throws KeyNotFoundException
 
592
     */
 
593
    public void deleteArtworkField() throws KeyNotFoundException
 
594
    {
 
595
        //New Method
 
596
        this.deleteField(VorbisCommentFieldKey.METADATA_BLOCK_PICTURE);
 
597
 
 
598
        //Old Method
 
599
        this.deleteField(VorbisCommentFieldKey.COVERART);
 
600
        this.deleteField(VorbisCommentFieldKey.COVERARTMIME);
 
601
    }
 
602
}
 
603