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.4 Tag information from an MP3 file,
28   * if available.
29   *
30   * @see <a href="http://www.id3.org/id3v2.4.0-structures">MP3 ID3 Version 2.4 specification</a>
31   * @see <a href="http://www.id3.org/id3v2.4.0-frames">MP3 ID3 Version 2.4 frames/tags</a>
32   */
33  public class ID3v24Handler implements ID3Tags {
34      private String title;
35      private String artist;
36      private String album;
37      private String year;
38      private String comment;
39      private String genre;
40      private String trackNumber;
41  
42      public ID3v24Handler(ID3v2Frame frame)
43              throws IOException, SAXException, TikaException {
44          RawTagIterator tags = new RawV24TagIterator(frame);
45          while (tags.hasNext()) {
46              RawTag tag = tags.next();
47              if (tag.name.equals("TIT2")) {
48                  title = getTagString(tag.data, 0, tag.data.length); 
49              } else if (tag.name.equals("TPE1")) {
50                  artist = getTagString(tag.data, 0, tag.data.length); 
51              } else if (tag.name.equals("TALB")) {
52                  album = getTagString(tag.data, 0, tag.data.length); 
53              } else if (tag.name.equals("TYER")) {
54                  year = getTagString(tag.data, 0, tag.data.length); 
55              } else if (tag.name.equals("COMM")) {
56                  comment = getTagString(tag.data, 0, tag.data.length); 
57              } else if (tag.name.equals("TRCK")) {
58                  trackNumber = getTagString(tag.data, 0, tag.data.length); 
59              } else if (tag.name.equals("TCON")) {
60                  String rawGenre = getTagString(tag.data, 0, tag.data.length);
61                  int open = rawGenre.indexOf("(");
62                  int close = rawGenre.indexOf(")");
63                  if (open < close) {
64                      try {
65                          int genreID = Integer.parseInt(rawGenre.substring(open+1, close));
66                          genre = ID3Tags.GENRES[genreID];
67                      } catch(NumberFormatException ignore) {
68                      }
69                  }
70              }
71          }
72      }
73  
74      private String getTagString(byte[] data, int offset, int length) {
75          return ID3v2Frame.getTagString(data, offset, length);
76      }
77  
78      public boolean getTagsPresent() {
79          return true;
80      }
81  
82      public String getTitle() {
83          return title;
84      }
85  
86      public String getArtist() {
87          return artist;
88      }
89  
90      public String getAlbum() {
91          return album;
92      }
93  
94      public String getYear() {
95          return year;
96      }
97  
98      public String getComment() {
99          return comment;
100     }
101 
102     public String getGenre() {
103         return genre;
104     }
105 
106     public String getTrackNumber() {
107         return trackNumber;
108     }
109 
110     private class RawV24TagIterator extends RawTagIterator {
111         private RawV24TagIterator(ID3v2Frame frame) {
112             frame.super(4, 4, 4, 2);
113         }
114     }
115 
116 }