001 package net.sourceforge.mp3dings;
002
003 /*
004 * Mp3dings - manage mp3 meta-information
005 * Copyright (C) 2003 Moritz Ringler
006 * $Id: Field.java 125 2007-12-22 14:31:36Z ringler $
007 *
008 * This program is free software; you can redistribute it and/or
009 * modify it under the terms of the GNU General Public License
010 * as published by the Free Software Foundation; either version 2
011 * of the License, or (at your option) any later version.
012 *
013 * This program is distributed in the hope that it will be useful,
014 * but WITHOUT ANY WARRANTY; without even the implied warranty of
015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016 * GNU General Public License for more details.
017 *
018 * You should have received a copy of the GNU General Public License
019 * along with this program; if not, write to the Free Software
020 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
021 */
022 import java.util.TreeMap;
023 import java.util.ResourceBundle;
024 import java.util.Collections;
025 import java.util.Iterator;
026 import java.util.HashMap;
027 import java.util.Comparator;
028 import java.util.ArrayList;
029 import java.util.List;
030 import java.util.Properties;
031 import java.util.Map;
032 import java.util.regex.Pattern;
033 import java.io.ObjectStreamException;
034 import java.io.IOException;
035 import java.io.File;
036 import java.io.PrintStream;
037 import java.io.BufferedInputStream;
038 import java.io.FileInputStream;
039 import java.io.Serializable;
040 import java.net.URL;
041 import de.mospace.lang.FullTextString;
042 import helliker.id3.APICImage;
043
044 /**
045 * A class providing integer identifiers for ID3 data fields.
046 *
047 * @author ringler
048 */
049 public final class Field implements Comparable, Serializable {
050 static final long serialVersionUID = -8016192977811392213L;
051
052 /** The contents of this field should be regarded as opaque by the application. */
053 public final static int TYPE_OTHER = 0;
054 /** Description of the Field */
055 public final static int TYPE_NUMERIC = 1;
056 /** Description of the Field */
057 public final static int TYPE_STRING = 2;
058 /** Description of the Field */
059 public final static int TYPE_BOOLEAN = 3;
060 /** Description of the Field */
061 public final static int TYPE_TIMESTAMP = 4;
062 /** Description of the Field */
063 public final static int TYPE_LONGTEXT = 5;
064 /** Picture type for ID3v2 APIC frame **/
065 public final static int TYPE_PICTURE = 6;
066 /** Description of the Field */
067 public final static int TYPE_URL = 2;
068 //TODO: change this to its own distinct value
069
070 private final String name;
071 private final int type;
072 private final String id3v2Frame;
073 private final boolean writable;
074 private final Object example;
075 private final static ResourceBundle LR =
076 ResourceBundle.getBundle("net.sourceforge.mp3dings.LanguageResource");
077
078 private static int nextOrdinal = 0;
079 private final int ordinal;
080 private static List values = new ArrayList(30);
081 private static Map nameToField = new TreeMap();
082
083 /** Description of the Field */
084 public final static Comparator BY_LONGNAME =
085 new Comparator() {
086 public int compare(Object fieldA, Object fieldB) {
087 return ((Field) fieldA).longname().compareTo(
088 ((Field) fieldB).longname());
089 }
090 };
091
092 /** Description of the Field */
093 public final static Field FILENAME = new Field("FILENAME",
094 TYPE_STRING, "", false, "Blur - Far out.mp3");
095
096 /** Description of the Field */
097 public final static Field ARTIST = new Field("ARTIST",
098 TYPE_STRING, "TPE1", true, "Blur");
099
100 /** Description of the Field */
101 public final static Field ORIGINAL_ARTIST = new Field("ORIGINAL_ARTIST",
102 TYPE_STRING, "TOPE", true, "Elvis Presley");
103
104 /** Description of the Field */
105 public final static Field LYRICIST = new Field("LYRICIST",
106 TYPE_STRING, "TEXT", true, "Alex James");
107
108 /** Description of the Field */
109 public final static Field ORIGINAL_LYRICIST = new Field("ORIGINAL_LYRICIST",
110 TYPE_STRING, "TOLY", true, "Bob Dylan");
111
112 /** Description of the Field */
113 public final static Field CONDUCTOR = new Field("CONDUCTOR",
114 TYPE_STRING, "TPE3", true, "Claudio Abbado");
115
116 /** Description of the Field */
117 public final static Field REMIXED_BY = new Field("REMIXED_BY",
118 TYPE_STRING, "TPE4", true, "Pet Shop Boys");
119
120 /** Description of the Field */
121 public final static Field TITLE = new Field("TITLE",
122 TYPE_STRING, "TIT2", true, "Adagio");
123
124 /** Description of the Field */
125 public final static Field SUBTITLE = new Field("SUBTITLE",
126 TYPE_STRING, "TIT3", true, "Op. 16");
127
128 /** Description of the Field */
129 public final static Field CONTENT_GROUP = new Field("CONTENT_GROUP",
130 TYPE_STRING, "TIT1", true, "Piano Concerto");
131
132 /** Description of the Field */
133 public final static Field ALBUM = new Field("ALBUM",
134 TYPE_STRING, "TALB", true, "Parklife");
135
136 /** Description of the Field */
137 public final static Field ORIGINAL_ALBUM = new Field("ORIGINAL_ALBUM",
138 TYPE_STRING, "TOAL", true, "Yellow submarine");
139
140 /** Description of the Field */
141 public final static Field TRACK = new Field("TRACK",
142 TYPE_NUMERIC, "TRCK", true, new Integer(4));
143
144 /** Description of the Field */
145 public final static Field PART_OF_SET = new Field("PART_OF_SET",
146 TYPE_NUMERIC, "TPOS", true, new Integer(1));
147
148 /** Description of the Field */
149 public final static Field SET_SUBTITLE = new Field("SET_SUBTITLE",
150 TYPE_STRING, "TSST", true, "");
151
152 /** Description of the Field */
153 public final static Field ISRC = new Field("ISRC",
154 TYPE_NUMERIC, "TSRC", true, "DEA239810012");
155
156 /** Description of the Field */
157 public final static Field YEAR = new Field("YEAR",
158 TYPE_NUMERIC, "TYER", true, new Integer(1994));
159
160 /** Description of the Field */
161 public final static Field GENRE = new Field("GENRE",
162 TYPE_STRING, "TCON", true, "Pop");
163
164 /** Description of the Field */
165 public final static Field COMMENT = new Field("COMMENT",
166 TYPE_STRING, "COMM", true, "I like it.");
167
168 /** Description of the Field */
169 public final static Field NEW_FILENAME = new Field("NEW_FILENAME",
170 TYPE_STRING, "", false, "Blur - Parklife - 04.mp3");
171
172 /** Description of the Field */
173 public final static Field OLD_FILENAME = new Field("OLD_FILENAME",
174 TYPE_STRING, "", false, "Blur - Far out.mp3");
175
176 /** Description of the Field */
177 public final static Field INCLUDE = new Field("INCLUDE",
178 TYPE_BOOLEAN, "", false, Boolean.TRUE);
179
180 /** Description of the Field */
181 public final static Field FULLPATH = new Field("FULLPATH",
182 TYPE_STRING, "", false, "/var/tmp/music/Blur/Blur - Far out.mp3");
183
184 /** Description of the Field */
185 public final static Field LENGTH = new Field("LENGTH",
186 TYPE_NUMERIC, "", false, new Long(260l));
187
188 /** Description of the Field */
189 public final static Field SIZE = new Field("SIZE",
190 TYPE_NUMERIC, "", false, new Long(4178665l));
191
192 /** Description of the Field */
193 public final static Field COMPOSER = new Field("COMPOSER",
194 TYPE_STRING, "TCOM", true, "Ludwig van Beethoven");
195
196 /** Description of the Field */
197 public final static Field BPM = new Field("BPM",
198 TYPE_NUMERIC, "TBPM", true, new Integer(120));
199
200 /** Description of the Field */
201 public final static Field ORIGINAL_FILENAME = new Field("ORIGINAL_FILENAME",
202 TYPE_STRING, "TOFN", true, "Blur - Far out.wav");
203
204 /** Description of the Field */
205 public final static Field COPYRIGHT = new Field("COPYRIGHT",
206 TYPE_STRING, "TCOP", true, "2003 EMI");
207
208 /** Description of the Field */
209 public final static Field ENCODER = new Field("ENCODER",
210 TYPE_STRING, "TENC", true, "Goofy");
211
212 /** Description of the Field */
213 public final static Field MOOD = new Field("MOOD",
214 TYPE_STRING, "TMOO", true, "Sad");
215
216 /** Description of the Field */
217 public final static Field LANGUAGE = new Field("LANGUAGE",
218 TYPE_STRING, "TLAN", true, "EN");
219
220 /** Description of the Field */
221 public final static Field ORCHESTRA = new Field("ORCHESTRA",
222 TYPE_STRING, "TPE2", true, "Bamberger Symphoniker");
223
224 /** Description of the Field */
225 public final static Field PUBLISHER = new Field("PUBLISHER",
226 TYPE_STRING, "TPUB", true, "Food/MCA Music");
227
228 /** Description of the Field */
229 public final static Field KEY = new Field("KEY",
230 TYPE_STRING, "TKEY", true, "D#m");
231
232 /** Description of the Field */
233 public final static Field LYRICS = new Field("LYRICS",
234 TYPE_LONGTEXT, "USLT", true, "Ladadi\nLadida");
235
236 /** Description of the Field */
237 public final static Field COM_INFO_URL = new Field("COM_INFO_URL",
238 TYPE_URL, "WCOM", true, "http://www.amazon.com");
239
240 /** Description of the Field */
241 public final static Field LEGAL_URL = new Field("LEGAL_URL",
242 TYPE_URL, "WCOP", true, "http://www.copyright.net");
243
244 /** Description of the Field */
245 public final static Field OFFICIAL_URL = new Field("OFFICIAL_URL",
246 TYPE_URL, "WOAF", true, "http://www.music.org/album/titel");
247
248 /** Description of the Field */
249 public final static Field ARTIST_URL = new Field("ARTIST_URL",
250 TYPE_URL, "WOAR", true, "http://www.blur.co.uk");
251
252 /** Description of the Field */
253 public final static Field SOURCE_URL = new Field("SOURCE_URL",
254 TYPE_URL, "WOAS", true, "http://www.source.url");
255
256 /** Description of the Field */
257 public final static Field WEB_RADIO_URL = new Field("WEB_RADIO_URL",
258 TYPE_URL, "WORS", true, "http://www.dlf.de");
259
260 /** Description of the Field */
261 public final static Field PAY_URL = new Field("PAY_URL",
262 TYPE_URL, "WPAY", true, "http://www.chargealot.com");
263
264 /** Description of the Field */
265 public final static Field PUBLISHER_URL = new Field("PUBLISHER_URL",
266 TYPE_URL, "WPUB", true, "http://www.parlophone.co.uk");
267
268 public final static Field ATTACHED_PICTURE = new Field("ATTACHED_PICTURE",
269 TYPE_PICTURE, "APIC", true, exampleImage());
270
271 public final static Field ENCODER_SETTINGS = new Field("ENCODER_SETTINGS",
272 TYPE_STRING, "TSSE", false, "LAME dll 3.97, VBR 2");
273
274 /** Description of the Field */
275 public final static Field ALL_FIELDS = new Field("ALL_FIELDS",
276 TYPE_OTHER, "", false, "", Integer.MAX_VALUE);
277 //public static final int
278 /** Description of the Field */
279 public final static int NUM_OF_FIELDS = nextOrdinal;
280 public final static String NAME_PATTERN = "[A-Z][A-Z0-9_]+[A-Z0-9]+";
281
282 private Field(String name,
283 int type,
284 String id3v2Frame,
285 boolean writable,
286 Object example) {
287 if(!Pattern.matches(NAME_PATTERN, name)){
288 throw new IllegalArgumentException("Field names must match " + NAME_PATTERN);
289 }
290 this.name = name;
291 this.id3v2Frame = id3v2Frame;
292 this.type = type;
293 this.writable = writable;
294 this.example = example;
295 ordinal = nextOrdinal++;
296 values.add(this);
297 }
298
299
300 /**
301 * use only for special "Fields" such as ALL_FIELDS *
302 *
303 * @param name Description of the Parameter
304 * @param type Description of the Parameter
305 * @param id3v2Frame Description of the Parameter
306 * @param writable Description of the Parameter
307 * @param example Description of the Parameter
308 * @param ordinal Description of the Parameter
309 */
310 private Field(String name,
311 int type,
312 String id3v2Frame,
313 boolean writable,
314 Object example,
315 int ordinal) {
316 if(!Pattern.matches(NAME_PATTERN, name)){
317 throw new IllegalArgumentException("Field names must match " + NAME_PATTERN);
318 }
319 this.name = name;
320 this.id3v2Frame = id3v2Frame;
321 this.type = type;
322 this.writable = writable;
323 this.example = example;
324 this.ordinal = ordinal;
325 }
326
327 private static final APICImage exampleImage(){
328 try{
329 APICImage result = new APICImage(
330 new URL("http://mp3dings.sourceforge.net/mp3dings_logo.png")
331 );
332 result.setType(APICImage.PictureType.PublisherOrStudioLogo);
333 result.setDescription("Das mp3dings Logo");
334 return result;
335 } catch (Exception ex){
336 throw new Error(ex);
337 }
338 }
339
340 /**
341 * Description of the Method
342 *
343 * @return Description of the Return Value
344 */
345 public String toString() {
346 return this.name;
347 }
348
349
350 /**
351 * Description of the Method
352 *
353 * @param o Description of the Parameter
354 * @return Description of the Return Value
355 */
356 public int compareTo(Object o) {
357 return ordinal - ((Field) o).ordinal;
358 }
359
360
361 /**
362 * Gets the classForType attribute of the Field class
363 *
364 * @param type Description of the Parameter
365 * @return The classForType value
366 */
367 public static Class getClassForType(int type) {
368 switch (type) {
369 case TYPE_STRING:
370 return String.class;
371 case TYPE_NUMERIC:
372 return Number.class;
373 case TYPE_BOOLEAN:
374 return Boolean.class;
375 case TYPE_TIMESTAMP:
376 return Long.class;
377 case TYPE_LONGTEXT:
378 return FullTextString.class;
379 // case TYPE_URL: return String.class;
380 case TYPE_PICTURE:
381 return APICImage.class;
382 default:
383 return Object.class;
384 }
385 }
386
387
388 /**
389 * Description of the Method
390 *
391 * @return Description of the Return Value
392 */
393 public Class typeAsClass() {
394 return getClassForType(type);
395 }
396
397
398 /**
399 * Description of the Method
400 *
401 * @return Description of the Return Value
402 */
403 public String name() {
404 return name;
405 }
406
407
408 /**
409 * Description of the Method
410 *
411 * @return Description of the Return Value
412 */
413 public String longname() {
414 return LR.getString("FIELD_" + name());
415 }
416
417
418 /**
419 * Returns an exemplary value for the specified field. *
420 *
421 * @return Description of the Return Value
422 */
423 public Object example() {
424 return EXAMPLES.containsKey(this)
425 ? EXAMPLES.get(this)
426 : example;
427 }
428
429
430 /**
431 * Gets the writable attribute of the Field object
432 *
433 * @return The writable value
434 */
435 public boolean isWritable() {
436 return writable;
437 }
438
439
440 /**
441 * Description of the Method
442 *
443 * @return Description of the Return Value
444 */
445 public int ordinal() {
446 return ordinal;
447 }
448
449
450 /**
451 * Description of the Method
452 *
453 * @return Description of the Return Value
454 */
455 public static List values() {
456 synchronized (values) {
457 values = Collections.unmodifiableList(values);
458 }
459 return values;
460 }
461
462
463 /**
464 * Description of the Method
465 *
466 * @param name Description of the Parameter
467 * @return Description of the Return Value
468 */
469 public static Field forName(String name) {
470 synchronized (nameToField) {
471 if (nameToField.size() == 0) {
472 Iterator it = values().iterator();
473 while (it.hasNext()) {
474 Field f = (Field) it.next();
475 nameToField.put(f.name(), f);
476 }
477 }
478 return (Field) nameToField.get(name);
479 }
480 }
481
482
483 private Object readResolve()
484 throws ObjectStreamException {
485 return values.get(ordinal);
486 // Canonicalize
487 }
488
489
490 /**
491 * Returns the official four letter ID3v2 field name for the specified
492 * mp3dings field. *
493 *
494 * @return Description of the Return Value
495 */
496 public String id3v2Frame() {
497 return id3v2Frame;
498 }
499
500
501 /**
502 * Description of the Method
503 *
504 * @return Description of the Return Value
505 */
506 public int type() {
507 return type;
508 }
509
510 public Object valueOf(String s) {
511 Object result = null;
512 if (s == null) {
513 return null;
514 }
515 switch (type) {
516 case Field.TYPE_BOOLEAN:
517 result = Boolean.valueOf(s);
518 break;
519 case Field.TYPE_STRING:
520 result = s;
521 break;
522 case Field.TYPE_LONGTEXT:
523 result = new FullTextString(s);
524 break;
525 case Field.TYPE_TIMESTAMP:
526 //falls through
527 case Field.TYPE_NUMERIC:
528 try{
529 double d = Double.parseDouble(s);
530 long l = (long) d;
531 /* if the new number is an integer make a Long
532 * object, otherwise make a Double
533 */
534 if (d == 0 || Math.abs((d-l)/d)<1e-9){
535 result = new Long(l);
536 } else {
537 result = new Double(d);
538 }
539 }catch (NumberFormatException ex){
540 }
541 break;
542 default:
543 }
544 return result;
545 }
546
547
548 static final Map EXAMPLES = initializeExamples();
549 private static Map initializeExamples(){
550 HashMap result = new HashMap();
551 Properties p = new Properties();
552 BufferedInputStream bis = null;
553 try{
554 File f = new File(Globals.getDir("HOME"), "field-examples.properties");
555 if(!f.isFile()){
556 return result;
557 }
558 FileInputStream fis = new FileInputStream(f);
559 bis = new BufferedInputStream(fis);
560 p.load(bis);
561 } catch (Exception ex){
562 ex.printStackTrace();
563 return result;
564 } finally {
565 if(bis != null){
566 try {
567 bis.close();
568 } catch (IOException ignore){
569 }
570 }
571 }
572 Iterator it = p.keySet().iterator();
573 Object ex;
574 while(it.hasNext()){
575 String key = (String) it.next();
576 Field field = Field.forName(key);
577 if(field != null){
578 ex = field.valueOf(p.getProperty(key));
579 if(ex != null){
580 result.put(field, ex);
581 }
582 }
583 }
584 return result;
585 }
586
587 public static void main(String argv[]){
588 generateExampleProperties(System.out);
589 }
590
591 public static void generateExampleProperties(PrintStream out){
592 Iterator it = values().iterator();
593 while(it.hasNext()){
594 Field field = (Field) it.next();
595 out.println(field.name()+"="+String.valueOf(field.example()));
596 }
597 }
598
599 }