1 | package org.farng.mp3.filename; |
2 | |
3 | import org.farng.mp3.AbstractMP3Tag; |
4 | import org.farng.mp3.MP3File; |
5 | import org.farng.mp3.TagConstant; |
6 | import org.farng.mp3.TagException; |
7 | import org.farng.mp3.TagUtility; |
8 | import org.farng.mp3.id3.AbstractID3v2Frame; |
9 | import org.farng.mp3.id3.FrameBodyCOMM; |
10 | import org.farng.mp3.id3.FrameBodySYLT; |
11 | import org.farng.mp3.id3.FrameBodyTALB; |
12 | import org.farng.mp3.id3.FrameBodyTCOM; |
13 | import org.farng.mp3.id3.FrameBodyTCON; |
14 | import org.farng.mp3.id3.FrameBodyTDRC; |
15 | import org.farng.mp3.id3.FrameBodyTIT2; |
16 | import org.farng.mp3.id3.FrameBodyTPE1; |
17 | import org.farng.mp3.id3.FrameBodyTRCK; |
18 | import org.farng.mp3.id3.FrameBodyUSLT; |
19 | import org.farng.mp3.id3.ID3v2_3Frame; |
20 | import org.farng.mp3.id3.ID3v2_4; |
21 | |
22 | import java.io.File; |
23 | import java.io.IOException; |
24 | import java.io.RandomAccessFile; |
25 | import java.util.Iterator; |
26 | |
27 | /** |
28 | * This class represents the filename. To create it, invoke <code>FilenameTagBuilder.createFilenameTagFromMP3File</code> |
29 | * which returns a complete parsed, evaluated, and matched FilenameTag. |
30 | * |
31 | * @author Eric Farng |
32 | * @version $Revision: 1.5 $ |
33 | */ |
34 | public class FilenameTag extends AbstractMP3Tag { |
35 | |
36 | /** |
37 | * parsed composite |
38 | */ |
39 | private AbstractFilenameComposite composite = null; |
40 | /** |
41 | * id3v2_4 tag created from composite |
42 | */ |
43 | private ID3v2_4 id3tag = null; |
44 | /** |
45 | * mp3file used to create this tag |
46 | */ |
47 | private MP3File mp3file = null; |
48 | /** |
49 | * file name extension used to create this tag |
50 | */ |
51 | private String extension = null; |
52 | |
53 | /** |
54 | * Creates a new FilenameTag object. |
55 | */ |
56 | public FilenameTag(final FilenameTag copyObject) { |
57 | super(copyObject); |
58 | composite = (AbstractFilenameComposite) TagUtility.copyObject(copyObject.composite); |
59 | id3tag = new ID3v2_4(copyObject.id3tag); |
60 | mp3file = new MP3File(copyObject.mp3file); |
61 | extension = copyObject.extension; |
62 | } |
63 | |
64 | /** |
65 | * Creates a new FilenameTag object. |
66 | */ |
67 | protected FilenameTag() { |
68 | super(); |
69 | } |
70 | |
71 | /** |
72 | * Sets the composite that this tag will use. |
73 | * |
74 | * @param composite the composite that this tag will use. |
75 | */ |
76 | public void setComposite(final AbstractFilenameComposite composite) { |
77 | this.composite = composite; |
78 | } |
79 | |
80 | /** |
81 | * Returns the composite that this tag will use. |
82 | * |
83 | * @return the composite that this tag will use. |
84 | */ |
85 | public AbstractFilenameComposite getComposite() { |
86 | return composite; |
87 | } |
88 | |
89 | public void setExtension(final String extension) { |
90 | this.extension = extension; |
91 | } |
92 | |
93 | public String getExtension() { |
94 | return extension; |
95 | } |
96 | |
97 | /** |
98 | * Sets the frame of this tag |
99 | * |
100 | * @param frame the frame to set |
101 | */ |
102 | public void setFrame(final AbstractID3v2Frame frame) { |
103 | if (frame != null) { |
104 | if (id3tag == null) { |
105 | id3tag = new ID3v2_4(); |
106 | } |
107 | id3tag.setFrame(frame); |
108 | if (composite != null) { |
109 | composite.setFrame(frame); |
110 | } |
111 | } |
112 | } |
113 | |
114 | /** |
115 | * Returns a frame of this tag |
116 | * |
117 | * @param identifier ID3v2_4 ID of frame to get |
118 | * |
119 | * @return a frame of this tag |
120 | */ |
121 | public AbstractID3v2Frame getFrame(final String identifier) { |
122 | AbstractID3v2Frame frame = null; |
123 | if (id3tag != null) { |
124 | frame = id3tag.getFrame(identifier); |
125 | } |
126 | return frame; |
127 | } |
128 | |
129 | public Iterator getFrameOfType(final String identifier) { |
130 | return id3tag.getFrameOfType(identifier); |
131 | } |
132 | |
133 | /** |
134 | * Sets the ID3v2_4 representation of this tag. |
135 | * |
136 | * @param id3tag the ID3v2_4 representation of this tag |
137 | */ |
138 | public void setId3tag(final ID3v2_4 id3tag) { |
139 | this.id3tag = id3tag; |
140 | if (id3tag != null) { |
141 | final Iterator iterator = id3tag.iterator(); |
142 | while (iterator.hasNext()) { |
143 | composite.setFrame((AbstractID3v2Frame) iterator.next()); |
144 | } |
145 | if (composite != null) { |
146 | composite.matchAgainstTag(id3tag); |
147 | } |
148 | } |
149 | } |
150 | |
151 | /** |
152 | * Returns the ID3v2_4 representation of this tag |
153 | * |
154 | * @return the ID3v2_4 representation of this tag |
155 | */ |
156 | public ID3v2_4 getId3tag() { |
157 | return id3tag; |
158 | } |
159 | |
160 | public String getIdentifier() { |
161 | return "FilenameTagv1.00"; |
162 | } |
163 | |
164 | public void setMp3file(final MP3File mp3file) { |
165 | this.mp3file = mp3file; |
166 | } |
167 | |
168 | public MP3File getMp3file() { |
169 | return mp3file; |
170 | } |
171 | |
172 | public int getSize() { |
173 | return composeFilename().length(); |
174 | } |
175 | |
176 | public void append(final AbstractMP3Tag abstractMP3Tag) { |
177 | //todo Implement this org.farng.mp3.AbstractMP3Tag abstract method |
178 | throw new UnsupportedOperationException("Method append() not yet implemented."); |
179 | } |
180 | |
181 | public void append(final RandomAccessFile file) { |
182 | //todo Implement this org.farng.mp3.AbstractMP3Tag abstract method |
183 | throw new UnsupportedOperationException("Method append() not yet implemented."); |
184 | } |
185 | |
186 | public String composeFilename() { |
187 | final StringBuffer filename = new StringBuffer(128); |
188 | if (composite != null) { |
189 | filename.append(composite.composeFilename().trim()); |
190 | filename.append('.'); |
191 | filename.append(extension); |
192 | } |
193 | return filename.toString(); |
194 | } |
195 | |
196 | public void delete(final RandomAccessFile file) { |
197 | //todo Implement this org.farng.mp3.AbstractMP3Tag abstract method |
198 | throw new UnsupportedOperationException("Method delete() not yet implemented."); |
199 | } |
200 | |
201 | public boolean hasFrame(final String identifier) { |
202 | if (id3tag != null) { |
203 | return id3tag.hasFrame(identifier); |
204 | } |
205 | return false; |
206 | } |
207 | |
208 | public boolean hasFrameOfType(final String identifier) { |
209 | if (id3tag != null) { |
210 | return id3tag.hasFrameOfType(identifier); |
211 | } |
212 | return false; |
213 | } |
214 | |
215 | public Iterator iterator() { |
216 | Iterator iterator = null; |
217 | if (composite != null) { |
218 | iterator = composite.iterator(); |
219 | } |
220 | return iterator; |
221 | } |
222 | |
223 | public void overwrite(final AbstractMP3Tag abstractMP3Tag) { |
224 | //todo Implement this org.farng.mp3.AbstractMP3Tag abstract method |
225 | throw new UnsupportedOperationException("Method overwrite() not yet implemented."); |
226 | } |
227 | |
228 | public void overwrite(final RandomAccessFile file) throws TagException, IOException { |
229 | write(file); |
230 | } |
231 | |
232 | public void read(final RandomAccessFile file) { |
233 | //todo Implement this org.farng.mp3.AbstractMP3Tag abstract method |
234 | throw new UnsupportedOperationException("Method read() not yet implemented."); |
235 | } |
236 | |
237 | public boolean seek(final RandomAccessFile file) { |
238 | //todo Implement this org.farng.mp3.AbstractMP3Tag abstract method |
239 | throw new UnsupportedOperationException("Method seek() not yet implemented."); |
240 | } |
241 | |
242 | public String toString() { |
243 | final StringBuffer stringBuffer = new StringBuffer(128); |
244 | final Iterator iterator = iterator(); |
245 | while (iterator.hasNext()) { |
246 | stringBuffer.append(iterator.next().toString()); |
247 | stringBuffer.append(TagConstant.SEPERATOR_LINE); |
248 | } |
249 | return stringBuffer.toString(); |
250 | } |
251 | |
252 | public void write(final AbstractMP3Tag abstractMP3Tag) { |
253 | //todo Implement this org.farng.mp3.AbstractMP3Tag abstract method |
254 | throw new UnsupportedOperationException("Method write() not yet implemented."); |
255 | } |
256 | |
257 | public void write(final RandomAccessFile file) throws IOException, TagException { |
258 | final File originalFile = getMp3file().getMp3file(); |
259 | final File newFile = new File(originalFile.getParentFile(), composeFilename()); |
260 | if (!newFile.getName().equals(originalFile.getName())) { |
261 | file.getFD().sync(); |
262 | file.getChannel().close(); |
263 | file.close(); |
264 | |
265 | // copy, then delete |
266 | TagUtility.copyFile(originalFile, newFile); |
267 | if (!originalFile.delete()) { |
268 | throw new TagException("Unable to delete original file: " + originalFile.getName()); |
269 | } |
270 | } |
271 | } |
272 | |
273 | public String getSongTitle() { |
274 | String text = null; |
275 | AbstractID3v2Frame frame = getFrame("TIT2"); |
276 | if (frame != null) { |
277 | FrameBodyTIT2 body = (FrameBodyTIT2) frame.getBody(); |
278 | text = body.getText(); |
279 | } |
280 | return text.trim(); |
281 | } |
282 | |
283 | public String getLeadArtist() { |
284 | String text = null; |
285 | AbstractID3v2Frame frame = getFrame("TPE1"); |
286 | if (frame != null) { |
287 | FrameBodyTPE1 body = (FrameBodyTPE1) frame.getBody(); |
288 | text = body.getText(); |
289 | } |
290 | return text.trim(); |
291 | } |
292 | |
293 | public String getAlbumTitle() { |
294 | String text = null; |
295 | AbstractID3v2Frame frame = getFrame("TALB"); |
296 | if (frame != null) { |
297 | FrameBodyTALB body = (FrameBodyTALB) frame.getBody(); |
298 | text = body.getText(); |
299 | } |
300 | return text.trim(); |
301 | } |
302 | |
303 | public String getYearReleased() { |
304 | String text = null; |
305 | AbstractID3v2Frame frame = getFrame("TDRC"); |
306 | if (frame != null) { |
307 | FrameBodyTDRC body = (FrameBodyTDRC) frame.getBody(); |
308 | text = body.getText(); |
309 | } |
310 | return text.trim(); |
311 | } |
312 | |
313 | public String getSongComment() { |
314 | String text = null; |
315 | AbstractID3v2Frame frame = getFrame("COMM"); |
316 | if (frame != null) { |
317 | FrameBodyCOMM body = (FrameBodyCOMM) frame.getBody(); |
318 | text = body.getText(); |
319 | } |
320 | return text.trim(); |
321 | } |
322 | |
323 | public String getSongGenre() { |
324 | String text = null; |
325 | AbstractID3v2Frame frame = getFrame("TCON"); |
326 | if (frame != null) { |
327 | FrameBodyTCON body = (FrameBodyTCON) frame.getBody(); |
328 | text = body.getText(); |
329 | } |
330 | return text.trim(); |
331 | } |
332 | |
333 | public String getTrackNumberOnAlbum() { |
334 | String text = null; |
335 | AbstractID3v2Frame frame = getFrame("TRCK"); |
336 | if (frame != null) { |
337 | FrameBodyTRCK body = (FrameBodyTRCK) frame.getBody(); |
338 | text = body.getText(); |
339 | } |
340 | return text.trim(); |
341 | } |
342 | |
343 | public String getSongLyric() { |
344 | String text = null; |
345 | AbstractID3v2Frame frame = getFrame("SYLT"); |
346 | if (frame != null) { |
347 | FrameBodySYLT body = (FrameBodySYLT) frame.getBody(); |
348 | text = body.getLyric(); |
349 | } |
350 | if (text == null) { |
351 | frame = getFrame("USLT"); |
352 | if (frame != null) { |
353 | FrameBodyUSLT body = (FrameBodyUSLT) frame.getBody(); |
354 | text = body.getLyric(); |
355 | } |
356 | } |
357 | return text.trim(); |
358 | } |
359 | |
360 | public String getAuthorComposer() { |
361 | String text = null; |
362 | AbstractID3v2Frame frame = getFrame("TCOM"); |
363 | if (frame != null) { |
364 | FrameBodyTCOM body = (FrameBodyTCOM) frame.getBody(); |
365 | text = body.getText(); |
366 | } |
367 | return text.trim(); |
368 | } |
369 | |
370 | public void setSongTitle(String songTitle) { |
371 | AbstractID3v2Frame field = getFrame("TIT2"); |
372 | if (field == null) { |
373 | field = new ID3v2_3Frame(new FrameBodyTIT2((byte) 0, songTitle.trim())); |
374 | setFrame(field); |
375 | } else { |
376 | ((FrameBodyTIT2) field.getBody()).setText(songTitle.trim()); |
377 | } |
378 | } |
379 | |
380 | public void setLeadArtist(String leadArtist) { |
381 | AbstractID3v2Frame field = getFrame("TPE1"); |
382 | if (field == null) { |
383 | field = new ID3v2_3Frame(new FrameBodyTPE1((byte) 0, leadArtist.trim())); |
384 | setFrame(field); |
385 | } else { |
386 | ((FrameBodyTPE1) field.getBody()).setText(leadArtist.trim()); |
387 | } |
388 | } |
389 | |
390 | public void setAlbumTitle(String albumTitle) { |
391 | AbstractID3v2Frame field = getFrame("TALB"); |
392 | if (field == null) { |
393 | field = new ID3v2_3Frame(new FrameBodyTALB((byte) 0, albumTitle.trim())); |
394 | setFrame(field); |
395 | } else { |
396 | ((FrameBodyTALB) field.getBody()).setText(albumTitle.trim()); |
397 | } |
398 | } |
399 | |
400 | public void setYearReleased(String yearReleased) { |
401 | AbstractID3v2Frame field = getFrame("TDRC"); |
402 | if (field == null) { |
403 | field = new ID3v2_3Frame(new FrameBodyTDRC((byte) 0, yearReleased.trim())); |
404 | setFrame(field); |
405 | } else { |
406 | ((FrameBodyTDRC) field.getBody()).setText(yearReleased.trim()); |
407 | } |
408 | } |
409 | |
410 | public void setSongComment(String songComment) { |
411 | AbstractID3v2Frame field = getFrame("COMM"); |
412 | if (field == null) { |
413 | field = new ID3v2_3Frame(new FrameBodyCOMM((byte) 0, "ENG", "", songComment.trim())); |
414 | setFrame(field); |
415 | } else { |
416 | ((FrameBodyCOMM) field.getBody()).setText(songComment.trim()); |
417 | } |
418 | } |
419 | |
420 | public void setSongGenre(String songGenre) { |
421 | AbstractID3v2Frame field = getFrame("TCON"); |
422 | if (field == null) { |
423 | field = new ID3v2_3Frame(new FrameBodyTCON((byte) 0, songGenre.trim())); |
424 | setFrame(field); |
425 | } else { |
426 | ((FrameBodyTCON) field.getBody()).setText(songGenre.trim()); |
427 | } |
428 | } |
429 | |
430 | public void setTrackNumberOnAlbum(String trackNumberOnAlbum) { |
431 | AbstractID3v2Frame field = getFrame("TRCK"); |
432 | if (field == null) { |
433 | field = new ID3v2_3Frame(new FrameBodyTRCK((byte) 0, trackNumberOnAlbum.trim())); |
434 | setFrame(field); |
435 | } else { |
436 | ((FrameBodyTRCK) field.getBody()).setText(trackNumberOnAlbum.trim()); |
437 | } |
438 | } |
439 | |
440 | public void setSongLyric(String songLyrics) { |
441 | AbstractID3v2Frame field = getFrame("SYLT"); |
442 | if (field == null) { |
443 | field = new ID3v2_3Frame(new FrameBodyUSLT((byte) 0, "ENG", "", songLyrics.trim())); |
444 | setFrame(field); |
445 | } else { |
446 | ((FrameBodyUSLT) field.getBody()).setLyric(songLyrics.trim()); |
447 | } |
448 | } |
449 | |
450 | public void setAuthorComposer(String authorComposer) { |
451 | AbstractID3v2Frame field = getFrame("TCOM"); |
452 | if (field == null) { |
453 | field = new ID3v2_3Frame(new FrameBodyTCOM((byte) 0, authorComposer.trim())); |
454 | setFrame(field); |
455 | } else { |
456 | ((FrameBodyTCOM) field.getBody()).setText(authorComposer.trim()); |
457 | } |
458 | } |
459 | } |