View Javadoc

1   package com.melloware.jukes.file.tag;
2   
3   import java.io.File;
4   
5   import org.apache.commons.io.FilenameUtils;
6   import org.apache.commons.logging.Log;
7   import org.apache.commons.logging.LogFactory;
8   
9   import com.melloware.jukes.exception.MusicTagException;
10  import com.melloware.jukes.file.filter.MusicFilter;
11  
12  /**
13   * Implements the Factory pattern for creating instances of music tag objects.
14   * Determines, at runtime, based on the file ending which type of music tag to
15   * created. MP3Tag's for .mp3 files, AudioFileTag's for .ogg, .flac, .spx, .ape.
16   * <p>
17   * Copyright (c) 2006 Melloware, Inc. <http://www.melloware.com>
18   * @author Emil A. Lefkof III <info@melloware.com>
19   * @version 4.0
20   */
21  public final class TagFactory {
22  
23     private static final Log LOG = LogFactory.getLog(TagFactory.class);
24  
25     /**
26      * Default constructor. Private so no instantiation.
27      */
28     private TagFactory() {
29        super();
30     }
31  
32     /**
33      * Gets the proper music tag type based on aFile.
34      * <p>
35      * @param aFile the file to load
36      * @return MusicTag the tag loaded
37      * @throws MusicTagExeption if any error occurs
38      */
39     public static MusicTag getTag(final File aFile) throws MusicTagException {
40  
41        MusicTag tag = null;
42        if (LOG.isDebugEnabled()) {
43           LOG.debug("Loading tag: " + aFile.getAbsolutePath());
44        }
45  
46        if (FilenameUtils.isExtension(aFile.getName(), MusicFilter.MP3_EXTENSIONS)) {
47           tag = new Mp3Tag(aFile);
48        } else if (FilenameUtils.isExtension(aFile.getName(), MusicFilter.ENTAGGED_EXTENSIONS)) {
49           tag = new AudioFileTag(aFile);
50        } else if (FilenameUtils.isExtension(aFile.getName(), MusicFilter.APE_EXTENSIONS)) {
51           tag = new ApeFileTag(aFile);
52        } else {
53           throw new MusicTagException(aFile + " is not a known music file type.");
54        }
55  
56        return tag;
57     }
58  
59     /**
60      * Gets the proper music tag type based on aFile.
61      * <p>
62      * @param aFileLocation the file path
63      * @return MusicTag the tag loaded
64      * @throws MusicTagExcption if any error occurs
65      */
66     public static MusicTag getTag(final String aFileLocation) throws MusicTagException {
67        return getTag(new File(aFileLocation));
68     }
69  }