KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > services > fileextraction > MP3Extractor


1 package org.jahia.services.fileextraction;
2
3 import java.io.InputStream JavaDoc;
4 import java.io.File JavaDoc;
5 import org.apache.commons.io.CopyUtils;
6 import java.io.FileOutputStream JavaDoc;
7 import org.apache.slide.util.conf.ConfigurationException;
8 import java.util.Enumeration JavaDoc;
9 import org.apache.slide.util.conf.Configuration;
10 import java.util.Map JavaDoc;
11 import java.util.HashMap JavaDoc;
12 import org.blinkenlights.jid3.MP3File;
13 import org.blinkenlights.jid3.ID3Tag;
14 import org.blinkenlights.jid3.v1.ID3V1Tag;
15 import org.blinkenlights.jid3.v2.ID3V2Tag;
16 import org.jahia.services.sites.JahiaSitesSlideService;
17
18 /**
19  * <p>Title: </p>
20  *
21  * <p>Description: </p>
22  *
23  * <p>Copyright: Copyright (c) 2004</p>
24  *
25  * <p>Company: Jahia Ltd</p>
26  *
27  * @author Serge Huber
28  * @version 1.0
29  */

30 public class MP3Extractor implements FileExtractor {
31
32     protected Map JavaDoc propertyMap = new HashMap JavaDoc();
33
34     public MP3Extractor () {
35     }
36
37     /**
38      *
39      * @param path String
40      * @param lastModified long
41      * @param fileStream InputStream
42      * @throws Exception
43      * @return String
44      * @todo Implement this org.jahia.services.fileextraction.FileExtractor
45      * method
46      */

47     public String JavaDoc getContentAsString (String JavaDoc path, long lastModified,
48                                       InputStream JavaDoc fileStream)
49         throws Exception JavaDoc {
50         return getContentAsString(path, lastModified, fileStream, null);
51     }
52
53     /**
54      *
55      * @param path String
56      * @param lastModified long
57      * @param fileStream InputStream
58      * @param charSet String
59      * @return String
60      * @throws Exception
61      * @todo Implement this org.jahia.services.fileextraction.FileExtractor
62      * method
63      */

64     public String JavaDoc getContentAsString (String JavaDoc path, long lastModified,
65                                       InputStream JavaDoc fileStream, String JavaDoc charSet)
66         throws Exception JavaDoc {
67
68         StringBuffer JavaDoc contentBuf = new StringBuffer JavaDoc();
69
70         // this is ugly, we create a copy of the MP3 file, but unfortunately
71
// the API of the library we are using doesn't allow to do it differently.
72
File JavaDoc tempFile = File.createTempFile("mp3extrator", "temp");
73         FileOutputStream JavaDoc out = new FileOutputStream JavaDoc(tempFile);
74         CopyUtils.copy(fileStream, out);
75         out.flush();
76
77         this.configure(((JahiaSitesSlideService)JahiaSitesSlideService.getInstance()).getConfiguration().getConfiguration("mp3-property-mapping"));
78
79         MP3File mp3File = new MP3File(tempFile);
80         ID3Tag[] tags = mp3File.getTags();
81
82         for (int i=0; i < tags.length; i++) {
83             if (tags[i] instanceof ID3V1Tag) {
84                 ID3V1Tag v1Tag = (ID3V1Tag) tags[i];
85                 if (v1Tag.getTitle() != null) {
86                     contentBuf.append(v1Tag.getTitle());
87                     contentBuf.append(" ");
88                 }
89                 if (v1Tag.getArtist() != null) {
90                     contentBuf.append(v1Tag.getArtist());
91                     contentBuf.append(" ");
92                 }
93             } else if (tags[i] instanceof ID3V2Tag) {
94                 ID3V2Tag v2Tag = (ID3V2Tag) tags[i];
95                 if (v2Tag.getTitle() != null) {
96                     contentBuf.append(v2Tag.getTitle());
97                     contentBuf.append(" ");
98                 }
99                 if (v2Tag.getArtist() != null) {
100                     contentBuf.append(v2Tag.getArtist());
101                     contentBuf.append(" ");
102                 }
103             }
104         }
105         tempFile.delete();
106         return contentBuf.toString();
107     }
108
109     /**
110      *
111      * @param path String
112      * @param lastModified long
113      * @param fileStream InputStream
114      * @throws Exception
115      * @return String
116      * @todo Implement this org.jahia.services.fileextraction.FileExtractor
117      * method
118      */

119     public ExtractedDocument getExtractedDocument (String JavaDoc path,
120         long lastModified, InputStream JavaDoc fileStream)
121         throws Exception JavaDoc {
122         return getExtractedDocument(path, lastModified, fileStream, null);
123     }
124
125     /**
126      *
127      * @param path String
128      * @param lastModified long
129      * @param fileStream InputStream
130      * @param charSet String
131      * @throws Exception
132      * @return String
133      * @todo Implement this org.jahia.services.fileextraction.FileExtractor
134      * method
135      */

136     public ExtractedDocument getExtractedDocument (String JavaDoc path,
137         long lastModified, InputStream JavaDoc fileStream, String JavaDoc charSet)
138         throws Exception JavaDoc {
139
140         ExtractedDocumentImpl extDoc = new ExtractedDocumentImpl();
141         StringBuffer JavaDoc contentBuf = new StringBuffer JavaDoc();
142
143         // this is ugly, we create a copy of the MP3 file, but unfortunately
144
// the API of the library we are using doesn't allow to do it differently.
145
File JavaDoc tempFile = File.createTempFile("mp3extrator", ".mp3");
146         FileOutputStream JavaDoc out = new FileOutputStream JavaDoc(tempFile);
147         CopyUtils.copy(fileStream, out);
148         out.flush();
149         out.close();
150
151         this.configure(((JahiaSitesSlideService)JahiaSitesSlideService.getInstance()).getConfiguration().getConfiguration("mp3-property-mapping"));
152
153         MP3File mp3File = new MP3File(tempFile);
154         ID3Tag[] tags = mp3File.getTags();
155
156         for (int i=0; i < tags.length; i++) {
157             if (tags[i] instanceof ID3V1Tag) {
158                 ID3V1Tag v1Tag = (ID3V1Tag) tags[i];
159                 if (v1Tag.getTitle() != null) {
160                     extDoc.setProperty( (String JavaDoc) propertyMap.get("Title"),
161                                        v1Tag.getTitle());
162                     contentBuf.append(v1Tag.getTitle());
163                     contentBuf.append(" ");
164                 }
165                 if (v1Tag.getArtist() != null) {
166                     extDoc.setProperty( (String JavaDoc) propertyMap.get("Artist"),
167                                        v1Tag.getArtist());
168                     contentBuf.append(v1Tag.getArtist());
169                     contentBuf.append(" ");
170                 }
171             } else if (tags[i] instanceof ID3V2Tag) {
172                 ID3V2Tag v2Tag = (ID3V2Tag) tags[i];
173                 if (v2Tag.getTitle() != null) {
174                     extDoc.setProperty( (String JavaDoc) propertyMap.get("Title"),
175                                        v2Tag.getTitle());
176                     contentBuf.append(v2Tag.getTitle());
177                     contentBuf.append(" ");
178                 }
179                 if (v2Tag.getArtist() != null) {
180                     extDoc.setProperty( (String JavaDoc) propertyMap.get("Artist"),
181                                        v2Tag.getArtist());
182                     contentBuf.append(v2Tag.getArtist());
183                     contentBuf.append(" ");
184                 }
185             }
186         }
187
188         tempFile.delete();
189         extDoc.setContent(contentBuf.toString());
190         return extDoc;
191     }
192
193     public void configure(Configuration configuration) throws ConfigurationException {
194         Enumeration JavaDoc instructions = configuration.getConfigurations("instruction");
195         while (instructions.hasMoreElements()) {
196             Configuration extract = (Configuration)instructions.nextElement();
197             String JavaDoc property = extract.getAttribute("property");
198             String JavaDoc id = extract.getAttribute("id");
199             propertyMap.put(id, property);
200         }
201     }
202
203 }
204
Popular Tags