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

« back to all changes in this revision

Viewing changes to src/org/jaudiotagger/audio/AudioFile.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.audio;
20
 
 
21
 
import java.io.File;
22
 
import java.io.FileNotFoundException;
23
 
import java.io.RandomAccessFile;
24
 
import java.util.logging.Logger;
25
 
import java.util.ArrayList;
26
 
 
27
 
import org.jaudiotagger.audio.exceptions.CannotWriteException;
28
 
import org.jaudiotagger.audio.exceptions.ReadOnlyFileException;
29
 
import org.jaudiotagger.audio.flac.metadatablock.MetadataBlockDataPicture;
30
 
import org.jaudiotagger.audio.generic.GenericTag;
31
 
import org.jaudiotagger.audio.asf.tag.AsfTag;
32
 
import org.jaudiotagger.audio.wav.WavTag;
33
 
import org.jaudiotagger.audio.real.RealTag;
34
 
import org.jaudiotagger.tag.Tag;
35
 
import org.jaudiotagger.tag.mp4.Mp4Tag;
36
 
import org.jaudiotagger.tag.vorbiscomment.VorbisCommentTag;
37
 
import org.jaudiotagger.tag.flac.FlacTag;
38
 
 
39
 
/**
40
 
 * <p>This is the main object manipulated by the user representing an audiofile, its properties and its tag.</p>
41
 
 * <p>The prefered way to obtain an <code>AudioFile</code> is to use the <code>AudioFileIO.read(File)</code> method.</p>
42
 
 * <p>The <code>AudioFile</code> contains every properties associated with the file itself (no meta-data), like the bitrate, the sampling rate, the encoding audioHeaders, etc.</p>
43
 
 * <p>To get the meta-data contained in this file you have to get the <code>Tag</code> of this <code>AudioFile</code></p>
44
 
 *
45
 
 * @author Raphael Slinckx
46
 
 * @version $Id: AudioFile.java,v 1.15 2009/11/12 15:43:00 paultaylor Exp $
47
 
 * @see AudioFileIO
48
 
 * @see Tag
49
 
 * @since v0.01
50
 
 */
51
 
public class AudioFile
52
 
{
53
 
    //Logger
54
 
    public static Logger logger = Logger.getLogger("org.jaudiotagger.audio");
55
 
 
56
 
    /**
57
 
     * The physical file that this instance represents.
58
 
     */
59
 
    protected File file;
60
 
 
61
 
    /**
62
 
     * The Audio header info
63
 
     */
64
 
    protected AudioHeader audioHeader;
65
 
 
66
 
    /**
67
 
     * The tag
68
 
     */
69
 
    protected Tag tag;
70
 
 
71
 
    public AudioFile()
72
 
    {
73
 
 
74
 
    }
75
 
 
76
 
    /**
77
 
     * <p>These constructors are used by the different readers, users should not use them, but use the <code>AudioFileIO.read(File)</code> method instead !.</p>
78
 
     * <p>Create the AudioFile representing file f, the encodingaudioHeaders and containing the tag</p>
79
 
     *
80
 
     * @param f           The file of the audiofile
81
 
     * @param audioHeader the encoding audioHeaders over this file
82
 
     * @param tag         the tag contained in this file or null if no tag exists
83
 
     */
84
 
    public AudioFile(File f, AudioHeader audioHeader, Tag tag)
85
 
    {
86
 
        this.file = f;
87
 
        this.audioHeader = audioHeader;
88
 
        this.tag = tag;
89
 
    }
90
 
 
91
 
 
92
 
    /**
93
 
     * <p>These constructors are used by the different readers, users should not use them, but use the <code>AudioFileIO.read(File)</code> method instead !.</p>
94
 
     * <p>Create the AudioFile representing file denoted by pathname s, the encodingaudioHeaders and containing the tag</p>
95
 
     *
96
 
     * @param s           The pathname of the audiofile
97
 
     * @param audioHeader the encoding audioHeaders over this file
98
 
     * @param tag         the tag contained in this file
99
 
     */
100
 
    public AudioFile(String s, AudioHeader audioHeader, Tag tag)
101
 
    {
102
 
        this.file = new File(s);
103
 
        this.audioHeader = audioHeader;
104
 
        this.tag = tag;
105
 
    }
106
 
 
107
 
    /**
108
 
     * <p>Write the tag contained in this AudioFile in the actual file on the disk, this is the same as calling the <code>AudioFileIO.write(this)</code> method.</p>
109
 
     *
110
 
     * @throws CannotWriteException If the file could not be written/accessed, the extension wasn't recognized, or other IO error occured.
111
 
     * @see AudioFileIO
112
 
     */
113
 
    public void commit() throws CannotWriteException
114
 
    {
115
 
        AudioFileIO.write(this);
116
 
    }
117
 
 
118
 
    /**
119
 
     * Set the file to store the info in
120
 
     *
121
 
     * @param file
122
 
     */
123
 
    public void setFile(File file)
124
 
    {
125
 
        this.file = file;
126
 
    }
127
 
 
128
 
    /**
129
 
     * Retrieve the physical file
130
 
     *
131
 
     * @return
132
 
     */
133
 
    public File getFile()
134
 
    {
135
 
        return file;
136
 
    }
137
 
 
138
 
    public void setTag(Tag tag)
139
 
    {
140
 
        this.tag = tag;
141
 
    }
142
 
 
143
 
    /**
144
 
     * Return audio header
145
 
     * @return
146
 
     */
147
 
    public AudioHeader getAudioHeader()
148
 
    {
149
 
        return audioHeader;
150
 
    }
151
 
 
152
 
    /**
153
 
     * <p>Returns the tag contained in this AudioFile, the <code>Tag</code> contains any useful meta-data, like
154
 
     * artist, album, title, etc. If the file does not contain any tag the null is returned. Some audio formats do
155
 
     * not allow there to be no tag so in this case the reader would return an empty tag whereas for others such
156
 
     * as mp3 it is purely optional.
157
 
     *
158
 
     * @return Returns the tag contained in this AudioFile, or null if no tag exists.
159
 
     */
160
 
    public Tag getTag()
161
 
    {
162
 
        return tag;
163
 
    }
164
 
 
165
 
    /**
166
 
     * <p>Returns a multi-line string with the file path, the encoding audioHeaderrmations, and the tag contents.</p>
167
 
     *
168
 
     * @return A multi-line string with the file path, the encoding audioHeaderrmations, and the tag contents.
169
 
     *         TODO Maybe this can be changed ?
170
 
     */
171
 
    public String toString()
172
 
    {
173
 
        return "AudioFile " + getFile().getAbsolutePath()
174
 
                + "  --------\n" + audioHeader.toString() + "\n" + ((tag == null) ? "" : tag.toString()) + "\n-------------------";
175
 
    }
176
 
 
177
 
    /**
178
 
     * Checks the file is accessible with the correct permissions, otherwise exception occurs
179
 
     *
180
 
     * @param file
181
 
     * @param readOnly
182
 
     * @throws ReadOnlyFileException
183
 
     * @throws FileNotFoundException
184
 
     * @return
185
 
     */
186
 
    protected RandomAccessFile checkFilePermissions(File file, boolean readOnly) throws ReadOnlyFileException, FileNotFoundException
187
 
    {
188
 
        RandomAccessFile newFile;
189
 
 
190
 
        logger.info("Reading file:" + "path" + file.getPath() + ":abs:" + file.getAbsolutePath());
191
 
        if (!file.exists())
192
 
        {
193
 
            logger.severe("Unable to find:" + file.getPath());
194
 
            throw new FileNotFoundException("Unable to find:" + file.getPath());
195
 
        }
196
 
 
197
 
        // Unless opened as readonly the file must be writable
198
 
        if (readOnly)
199
 
        {
200
 
            newFile = new RandomAccessFile(file, "r");
201
 
        }
202
 
        else
203
 
        {
204
 
            if (!file.canWrite())
205
 
            {
206
 
                logger.severe("Unable to write:" + file.getPath());
207
 
                throw new ReadOnlyFileException("Unable to write to:" + file.getPath());
208
 
            }
209
 
            newFile = new RandomAccessFile(file, "rws");
210
 
        }
211
 
        return newFile;
212
 
    }
213
 
 
214
 
    /**
215
 
     * Optional debugging method
216
 
     *
217
 
     * @return
218
 
     */
219
 
    public String displayStructureAsXML()
220
 
    {
221
 
        return "";
222
 
    }
223
 
 
224
 
    /**
225
 
     * Optional debugging method
226
 
     *
227
 
     * @return
228
 
     */
229
 
    public String displayStructureAsPlainText()
230
 
    {
231
 
        return "";
232
 
    }
233
 
 
234
 
 
235
 
    /** Create Default Tag
236
 
     *
237
 
     * @return
238
 
     */
239
 
    //TODO might be better to instantiate classes such as Mp4File,FlacFile ecetera
240
 
    //TODO Generic tag is very misleading because soem of these formats cannot actually save the tag
241
 
    public Tag createDefaultTag()
242
 
    {
243
 
        if(SupportedFileFormat.FLAC.getFilesuffix().equals(file.getName().substring(file.getName().lastIndexOf('.'))))
244
 
        {
245
 
            return new FlacTag(VorbisCommentTag.createNewTag(), new ArrayList< MetadataBlockDataPicture >());
246
 
        }
247
 
        else if(SupportedFileFormat.OGG.getFilesuffix().equals(file.getName().substring(file.getName().lastIndexOf('.'))))
248
 
        {
249
 
            return VorbisCommentTag.createNewTag();
250
 
        }
251
 
        else if(SupportedFileFormat.MP4.getFilesuffix().equals(file.getName().substring(file.getName().lastIndexOf('.'))))
252
 
        {
253
 
            return new Mp4Tag();
254
 
        }
255
 
        else if(SupportedFileFormat.M4A.getFilesuffix().equals(file.getName().substring(file.getName().lastIndexOf('.'))))
256
 
        {
257
 
            return new Mp4Tag();
258
 
        }
259
 
        else if(SupportedFileFormat.M4P.getFilesuffix().equals(file.getName().substring(file.getName().lastIndexOf('.'))))
260
 
        {
261
 
            return new Mp4Tag();
262
 
        }
263
 
        else if(SupportedFileFormat.WMA.getFilesuffix().equals(file.getName().substring(file.getName().lastIndexOf('.'))))
264
 
        {
265
 
            return new AsfTag();
266
 
        }
267
 
        else if(SupportedFileFormat.WAV.getFilesuffix().equals(file.getName().substring(file.getName().lastIndexOf('.'))))
268
 
        {
269
 
            return new WavTag();
270
 
        }
271
 
        else if(SupportedFileFormat.RA.getFilesuffix().equals(file.getName().substring(file.getName().lastIndexOf('.'))))
272
 
        {
273
 
            return new RealTag();
274
 
        }
275
 
        else if(SupportedFileFormat.RM.getFilesuffix().equals(file.getName().substring(file.getName().lastIndexOf('.'))))
276
 
        {
277
 
            return new RealTag();
278
 
        }
279
 
        else
280
 
        {
281
 
            throw new RuntimeException("Unable to create default tag for this file format");
282
 
        }
283
 
 
284
 
    }
285
 
 
286
 
    /**
287
 
     * Get the tag or if the file doesnt have one at all, create a default tag  and return
288
 
     *
289
 
     * @return
290
 
     */
291
 
    public Tag getTagOrCreateDefault()
292
 
    {
293
 
        Tag tag = getTag();
294
 
        if(tag==null)
295
 
        {
296
 
            return createDefaultTag();
297
 
        }
298
 
        return tag;
299
 
    }
300
 
 
301
 
     /**
302
 
     * Get the tag or if the file doesnt have one at all, create a default tag  and set it
303
 
     *
304
 
     * @return
305
 
     */
306
 
    public Tag getTagOrCreateAndSetDefault()
307
 
    {
308
 
        Tag tag = getTag();
309
 
        if(tag==null)
310
 
        {
311
 
            tag = createDefaultTag();
312
 
            setTag(tag);
313
 
            return tag;
314
 
        }
315
 
        return tag;
316
 
    }
317
 
 
318
 
    /**
319
 
     *
320
 
     * @param file
321
 
     * @return filename with audioformat seperator stripped of.
322
 
     */
323
 
    public static String getBaseFilename(File file)
324
 
    {
325
 
        int index=file.getName().toLowerCase().lastIndexOf(".");
326
 
        if(index>0)
327
 
        {
328
 
            return file.getName().substring(0,index);
329
 
        }
330
 
        return file.getName();
331
 
    }
332
 
}
 
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.audio;
 
20
 
 
21
import java.io.File;
 
22
import java.io.FileNotFoundException;
 
23
import java.io.RandomAccessFile;
 
24
import java.util.logging.Logger;
 
25
import java.util.ArrayList;
 
26
 
 
27
import org.jaudiotagger.audio.exceptions.CannotWriteException;
 
28
import org.jaudiotagger.audio.exceptions.ReadOnlyFileException;
 
29
import org.jaudiotagger.audio.flac.metadatablock.MetadataBlockDataPicture;
 
30
import org.jaudiotagger.logging.ErrorMessage;
 
31
import org.jaudiotagger.tag.asf.AsfTag;
 
32
import org.jaudiotagger.audio.wav.WavTag;
 
33
import org.jaudiotagger.audio.real.RealTag;
 
34
import org.jaudiotagger.tag.Tag;
 
35
import org.jaudiotagger.tag.mp4.Mp4Tag;
 
36
import org.jaudiotagger.tag.vorbiscomment.VorbisCommentTag;
 
37
import org.jaudiotagger.tag.flac.FlacTag;
 
38
 
 
39
/**
 
40
 * <p>This is the main object manipulated by the user representing an audiofile, its properties and its tag.</p>
 
41
 * <p>The prefered way to obtain an <code>AudioFile</code> is to use the <code>AudioFileIO.read(File)</code> method.</p>
 
42
 * <p>The <code>AudioFile</code> contains every properties associated with the file itself (no meta-data), like the bitrate, the sampling rate, the encoding audioHeaders, etc.</p>
 
43
 * <p>To get the meta-data contained in this file you have to get the <code>Tag</code> of this <code>AudioFile</code></p>
 
44
 *
 
45
 * @author Raphael Slinckx
 
46
 * @version $Id: AudioFile.java 929 2010-11-17 12:36:46Z paultaylor $
 
47
 * @see AudioFileIO
 
48
 * @see Tag
 
49
 * @since v0.01
 
50
 */
 
51
public class AudioFile
 
52
{
 
53
    //Logger
 
54
    public static Logger logger = Logger.getLogger("org.jaudiotagger.audio");
 
55
 
 
56
    /**
 
57
     * The physical file that this instance represents.
 
58
     */
 
59
    protected File file;
 
60
 
 
61
    /**
 
62
     * The Audio header info
 
63
     */
 
64
    protected AudioHeader audioHeader;
 
65
 
 
66
    /**
 
67
     * The tag
 
68
     */
 
69
    protected Tag tag;
 
70
 
 
71
    public AudioFile()
 
72
    {
 
73
 
 
74
    }
 
75
 
 
76
    /**
 
77
     * <p>These constructors are used by the different readers, users should not use them, but use the <code>AudioFileIO.read(File)</code> method instead !.</p>
 
78
     * <p>Create the AudioFile representing file f, the encoding audio headers and containing the tag</p>
 
79
     *
 
80
     * @param f           The file of the audio file
 
81
     * @param audioHeader the encoding audioHeaders over this file
 
82
     * @param tag         the tag contained in this file or null if no tag exists
 
83
     */
 
84
    public AudioFile(File f, AudioHeader audioHeader, Tag tag)
 
85
    {
 
86
        this.file = f;
 
87
        this.audioHeader = audioHeader;
 
88
        this.tag = tag;
 
89
    }
 
90
 
 
91
 
 
92
    /**
 
93
     * <p>These constructors are used by the different readers, users should not use them, but use the <code>AudioFileIO.read(File)</code> method instead !.</p>
 
94
     * <p>Create the AudioFile representing file denoted by pathnames, the encoding audio Headers and containing the tag</p>
 
95
     *
 
96
     * @param s           The pathname of the audio file
 
97
     * @param audioHeader the encoding audioHeaders over this file
 
98
     * @param tag         the tag contained in this file
 
99
     */
 
100
    public AudioFile(String s, AudioHeader audioHeader, Tag tag)
 
101
    {
 
102
        this.file = new File(s);
 
103
        this.audioHeader = audioHeader;
 
104
        this.tag = tag;
 
105
    }
 
106
 
 
107
    /**
 
108
     * <p>Write the tag contained in this AudioFile in the actual file on the disk, this is the same as calling the <code>AudioFileIO.write(this)</code> method.</p>
 
109
     *
 
110
     * @throws CannotWriteException If the file could not be written/accessed, the extension wasn't recognized, or other IO error occured.
 
111
     * @see AudioFileIO
 
112
     */
 
113
    public void commit() throws CannotWriteException
 
114
    {
 
115
        AudioFileIO.write(this);
 
116
    }
 
117
 
 
118
    /**
 
119
     * Set the file to store the info in
 
120
     *
 
121
     * @param file
 
122
     */
 
123
    public void setFile(File file)
 
124
    {
 
125
        this.file = file;
 
126
    }
 
127
 
 
128
    /**
 
129
     * Retrieve the physical file
 
130
     *
 
131
     * @return
 
132
     */
 
133
    public File getFile()
 
134
    {
 
135
        return file;
 
136
    }
 
137
 
 
138
    public void setTag(Tag tag)
 
139
    {
 
140
        this.tag = tag;
 
141
    }
 
142
 
 
143
    /**
 
144
     * Return audio header
 
145
     * @return
 
146
     */
 
147
    public AudioHeader getAudioHeader()
 
148
    {
 
149
        return audioHeader;
 
150
    }
 
151
 
 
152
    /**
 
153
     * <p>Returns the tag contained in this AudioFile, the <code>Tag</code> contains any useful meta-data, like
 
154
     * artist, album, title, etc. If the file does not contain any tag the null is returned. Some audio formats do
 
155
     * not allow there to be no tag so in this case the reader would return an empty tag whereas for others such
 
156
     * as mp3 it is purely optional.
 
157
     *
 
158
     * @return Returns the tag contained in this AudioFile, or null if no tag exists.
 
159
     */
 
160
    public Tag getTag()
 
161
    {
 
162
        return tag;
 
163
    }
 
164
 
 
165
    /**
 
166
     * <p>Returns a multi-line string with the file path, the encoding audioHeader, and the tag contents.</p>
 
167
     *
 
168
     * @return A multi-line string with the file path, the encoding audioHeader, and the tag contents.
 
169
     *         TODO Maybe this can be changed ?
 
170
     */
 
171
    public String toString()
 
172
    {
 
173
        return "AudioFile " + getFile().getAbsolutePath()
 
174
                + "  --------\n" + audioHeader.toString() + "\n" + ((tag == null) ? "" : tag.toString()) + "\n-------------------";
 
175
    }
 
176
 
 
177
    /**
 
178
     * Check does file exist
 
179
     *
 
180
     * @param file
 
181
     * @throws FileNotFoundException
 
182
     */
 
183
    public void checkFileExists(File file)throws FileNotFoundException
 
184
    {
 
185
        logger.info("Reading file:" + "path" + file.getPath() + ":abs:" + file.getAbsolutePath());
 
186
        if (!file.exists())
 
187
        {
 
188
            logger.severe("Unable to find:" + file.getPath());
 
189
            throw new FileNotFoundException(ErrorMessage.UNABLE_TO_FIND_FILE.getMsg(file.getPath()));
 
190
        }
 
191
    }
 
192
 
 
193
    /**
 
194
     * Checks the file is accessible with the correct permissions, otherwise exception occurs
 
195
     *
 
196
     * @param file
 
197
     * @param readOnly
 
198
     * @throws ReadOnlyFileException
 
199
     * @throws FileNotFoundException
 
200
     * @return
 
201
     */
 
202
    protected RandomAccessFile checkFilePermissions(File file, boolean readOnly) throws ReadOnlyFileException, FileNotFoundException
 
203
    {
 
204
        RandomAccessFile newFile;
 
205
 
 
206
        checkFileExists(file);
 
207
 
 
208
        // Unless opened as readonly the file must be writable
 
209
        if (readOnly)
 
210
        {
 
211
            newFile = new RandomAccessFile(file, "r");
 
212
        }
 
213
        else
 
214
        {
 
215
            if (!file.canWrite())
 
216
            {
 
217
                logger.severe("Unable to write:" + file.getPath());
 
218
                throw new ReadOnlyFileException(ErrorMessage.NO_PERMISSIONS_TO_WRITE_TO_FILE.getMsg(file.getPath()));
 
219
            }
 
220
            newFile = new RandomAccessFile(file, "rws");
 
221
        }
 
222
        return newFile;
 
223
    }
 
224
 
 
225
    /**
 
226
     * Optional debugging method
 
227
     *
 
228
     * @return
 
229
     */
 
230
    public String displayStructureAsXML()
 
231
    {
 
232
        return "";
 
233
    }
 
234
 
 
235
    /**
 
236
     * Optional debugging method
 
237
     *
 
238
     * @return
 
239
     */
 
240
    public String displayStructureAsPlainText()
 
241
    {
 
242
        return "";
 
243
    }
 
244
 
 
245
 
 
246
    /** Create Default Tag
 
247
     *
 
248
     * @return
 
249
     */
 
250
    //TODO might be better to instantiate classes such as Mp4File,FlacFile ecetera
 
251
    //TODO Generic tag is very misleading because soem of these formats cannot actually save the tag
 
252
    public Tag createDefaultTag()
 
253
    {
 
254
        if(SupportedFileFormat.FLAC.getFilesuffix().equals(file.getName().substring(file.getName().lastIndexOf('.'))))
 
255
        {
 
256
            return new FlacTag(VorbisCommentTag.createNewTag(), new ArrayList< MetadataBlockDataPicture >());
 
257
        }
 
258
        else if(SupportedFileFormat.OGG.getFilesuffix().equals(file.getName().substring(file.getName().lastIndexOf('.'))))
 
259
        {
 
260
            return VorbisCommentTag.createNewTag();
 
261
        }
 
262
        else if(SupportedFileFormat.MP4.getFilesuffix().equals(file.getName().substring(file.getName().lastIndexOf('.'))))
 
263
        {
 
264
            return new Mp4Tag();
 
265
        }
 
266
        else if(SupportedFileFormat.M4A.getFilesuffix().equals(file.getName().substring(file.getName().lastIndexOf('.'))))
 
267
        {
 
268
            return new Mp4Tag();
 
269
        }
 
270
        else if(SupportedFileFormat.M4P.getFilesuffix().equals(file.getName().substring(file.getName().lastIndexOf('.'))))
 
271
        {
 
272
            return new Mp4Tag();
 
273
        }
 
274
        else if(SupportedFileFormat.WMA.getFilesuffix().equals(file.getName().substring(file.getName().lastIndexOf('.'))))
 
275
        {
 
276
            return new AsfTag();
 
277
        }
 
278
        else if(SupportedFileFormat.WAV.getFilesuffix().equals(file.getName().substring(file.getName().lastIndexOf('.'))))
 
279
        {
 
280
            return new WavTag();
 
281
        }
 
282
        else if(SupportedFileFormat.RA.getFilesuffix().equals(file.getName().substring(file.getName().lastIndexOf('.'))))
 
283
        {
 
284
            return new RealTag();
 
285
        }
 
286
        else if(SupportedFileFormat.RM.getFilesuffix().equals(file.getName().substring(file.getName().lastIndexOf('.'))))
 
287
        {
 
288
            return new RealTag();
 
289
        }
 
290
        else
 
291
        {
 
292
            throw new RuntimeException("Unable to create default tag for this file format");
 
293
        }
 
294
 
 
295
    }
 
296
 
 
297
    /**
 
298
     * Get the tag or if the file doesn't have one at all, create a default tag  and return
 
299
     *
 
300
     * @return
 
301
     */
 
302
    public Tag getTagOrCreateDefault()
 
303
    {
 
304
        Tag tag = getTag();
 
305
        if(tag==null)
 
306
        {
 
307
            return createDefaultTag();
 
308
        }
 
309
        return tag;
 
310
    }
 
311
 
 
312
     /**
 
313
     * Get the tag or if the file doesn't have one at all, create a default tag  and set it
 
314
     *
 
315
     * @return
 
316
     */
 
317
    public Tag getTagOrCreateAndSetDefault()
 
318
    {
 
319
        Tag tag = getTag();
 
320
        if(tag==null)
 
321
        {
 
322
            tag = createDefaultTag();
 
323
            setTag(tag);
 
324
            return tag;
 
325
        }
 
326
        return tag;
 
327
    }
 
328
 
 
329
    /**
 
330
     *
 
331
     * @param file
 
332
     * @return filename with audioFormat separator stripped of.
 
333
     */
 
334
    public static String getBaseFilename(File file)
 
335
    {
 
336
        int index=file.getName().toLowerCase().lastIndexOf(".");
 
337
        if(index>0)
 
338
        {
 
339
            return file.getName().substring(0,index);
 
340
        }
 
341
        return file.getName();
 
342
    }
 
343
}