View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.tika.parser.mp3;
18  
19  import java.io.IOException;
20  
21  import org.apache.tika.exception.TikaException;
22  import org.apache.tika.parser.mp3.ID3v2Frame.RawTag;
23  import org.apache.tika.parser.mp3.ID3v2Frame.RawTagIterator;
24  import org.xml.sax.SAXException;
25  
26  /**
27   * This is used to parse ID3 Version 2.3 Tag information from an MP3 file,
28   * if available.
29   *
30   * @see <a href="http://id3lib.sourceforge.net/id3/id3v2.3.0.html">MP3 ID3 Version 2.3 specification</a>
31   */
32  public class ID3v23Handler implements ID3Tags {
33      private String title;
34      private String artist;
35      private String album;
36      private String year;
37      private String comment;
38      private String genre;
39      private String trackNumber;
40  
41      public ID3v23Handler(ID3v2Frame frame)
42              throws IOException, SAXException, TikaException {
43          RawTagIterator tags = new RawV23TagIterator(frame);
44          while (tags.hasNext()) {
45              RawTag tag = tags.next();
46              if (tag.name.equals("TIT2")) {
47                  title = getTagString(tag.data, 0, tag.data.length); 
48              } else if (tag.name.equals("TPE1")) {
49                  artist = getTagString(tag.data, 0, tag.data.length); 
50              } else if (tag.name.equals("TALB")) {
51                  album = getTagString(tag.data, 0, tag.data.length); 
52              } else if (tag.name.equals("TYER")) {
53                  year = getTagString(tag.data, 0, tag.data.length); 
54              } else if (tag.name.equals("COMM")) {
55                  comment = getTagString(tag.data, 0, tag.data.length); 
56              } else if (tag.name.equals("TRCK")) {
57                  trackNumber = getTagString(tag.data, 0, tag.data.length); 
58              } else if (tag.name.equals("TCON")) {
59                  String rawGenre = getTagString(tag.data, 0, tag.data.length);
60                  int open = rawGenre.indexOf("(");
61                  int close = rawGenre.indexOf(")");
62                  if (open < close) {
63                      try {
64                          int genreID = Integer.parseInt(rawGenre.substring(open+1, close));
65                          genre = ID3Tags.GENRES[genreID];
66                      } catch(NumberFormatException ignore) {
67                      }
68                  }
69              }
70          }
71      }
72  
73      private String getTagString(byte[] data, int offset, int length) {
74          return ID3v2Frame.getTagString(data, offset, length);
75      }
76  
77      public boolean getTagsPresent() {
78          return true;
79      }
80  
81      public String getTitle() {
82          return title;
83      }
84  
85      public String getArtist() {
86          return artist;
87      }
88  
89      public String getAlbum() {
90          return album;
91      }
92  
93      public String getYear() {
94          return year;
95      }
96  
97      public String getComment() {
98          return comment;
99      }
100 
101     public String getGenre() {
102         return genre;
103     }
104 
105     public String getTrackNumber() {
106         return trackNumber;
107     }
108 
109     private class RawV23TagIterator extends RawTagIterator {
110         private RawV23TagIterator(ID3v2Frame frame) {
111             frame.super(4, 4, 1, 2);
112         }
113     }
114 
115 }