001: /*
002:
003: * Project: Lius Package: de.teamskill.lius.index.audio
004:
005: *
006:
007: * Copyright (c) 2004 by Jens Fendler <jf@teamskill.de>
008:
009: *
010:
011: * This program is a free software; you can redistribute it and/or modify it
012:
013: * under the terms of the GNU General Public License as published by the Free
014:
015: * Software Foundation; either version 2 of the License, or (at your option) any
016:
017: * later version. This program is distributed in the hope that it will be
018:
019: * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
020:
021: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
022:
023: * Public License for more details. You should have received a copy of the GNU
024:
025: * General Public License along with this program; if not, write to the Free
026:
027: * Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
028:
029: * USA
030:
031: */
032:
033: package de.teamskill.lius.index.audio;
034:
035: import java.io.File;
036: import java.io.IOException;
037: import java.util.ArrayList;
038: import java.util.Collection;
039: import java.util.Iterator;
040:
041: import javax.sound.sampled.UnsupportedAudioFileException;
042:
043: import org.apache.log4j.Logger;
044: import org.apache.lucene.document.Document;
045:
046: import ca.ulaval.bibl.lius.config.LiusConfig;
047: import ca.ulaval.bibl.lius.config.LiusConfigBuilder;
048: import ca.ulaval.bibl.lius.config.LiusField;
049: import ca.ulaval.bibl.lius.index.Indexer;
050:
051: /**
052: *
053: * Class: MP3Indexer <br>
054: *
055: *
056: *
057: * Changelog:
058: *
059: * <ul>
060: *
061: * <li>17.05.2005: Initial implementation</li>
062: *
063: * </ul>
064: *
065: *
066: *
067: * @author <a href="mailto:jf@teamskill.de">Jens Fendler </a>
068: *
069: */
070:
071: public class MP3Indexer extends Indexer {
072:
073: static Logger logger = Logger.getRootLogger();
074:
075: /**
076: *
077: * @see ca.ulaval.bibl.lius.index.Indexer#createLuceneDocument(java.lang.String,
078: *
079: * ca.ulaval.bibl.lius.config.LiusConfig)
080: *
081: */
082:
083: public Document createLuceneDocument(String file, LiusConfig lc) {
084:
085: Document doc = createLuceneDocument(file, lc.getMP3Fields());
086:
087: return doc;
088:
089: }
090:
091: /**
092: *
093: * @see ca.ulaval.bibl.lius.index.Indexer#getLiusFields(ca.ulaval.bibl.lius.config.LiusConfig)
094: *
095: */
096:
097: public Collection getLiusFields(LiusConfig lc) {
098:
099: return lc.getMP3Fields();
100:
101: }
102:
103: /**
104: *
105: * @see ca.ulaval.bibl.lius.index.Indexer#getPopulatedCollection(java.lang.Object,
106: *
107: * java.util.Collection)
108: *
109: */
110:
111: public Collection getPopulatedCollection(Object file,
112: Collection liusFields) {
113:
114: Collection c = new ArrayList();
115:
116: MpegInfo mi = new MpegInfo();
117:
118: try {
119:
120: mi.load(new File((String) file));
121:
122: for (Iterator i = liusFields.iterator(); i.hasNext();) {
123:
124: Object next = i.next();
125:
126: if (next instanceof LiusField) {
127:
128: LiusField lf = (LiusField) next;
129:
130: if ("channels".equalsIgnoreCase(lf.getGet())) {
131:
132: lf.setValue("" + mi.getChannels());
133:
134: c.add(lf);
135:
136: } else if ("channelsmode".equalsIgnoreCase(lf
137: .getGet())) {
138:
139: lf.setValue(mi.getChannelsMode());
140:
141: c.add(lf);
142:
143: } else if ("version".equalsIgnoreCase(lf.getGet())
144:
145: && mi.getVersion() != null) {
146:
147: lf.setValue(mi.getVersion());
148:
149: c.add(lf);
150:
151: } else if ("samplingrate".equalsIgnoreCase(lf
152: .getGet())) {
153:
154: lf.setValue("" + mi.getSamplingRate());
155:
156: c.add(lf);
157:
158: } else if ("layer".equalsIgnoreCase(lf.getGet())
159:
160: && mi.getLayer() != null) {
161:
162: lf.setValue(mi.getLayer());
163:
164: c.add(lf);
165:
166: } else if ("emphasis".equalsIgnoreCase(lf.getGet())
167:
168: && mi.getEmphasis() != null) {
169:
170: lf.setValue(mi.getEmphasis());
171:
172: c.add(lf);
173:
174: } else if ("nominalbitrate".equalsIgnoreCase(lf
175: .getGet())) {
176:
177: lf.setValue("" + mi.getBitRate());
178:
179: c.add(lf);
180:
181: } else if ("duration".equalsIgnoreCase(lf.getGet())) {
182:
183: lf.setValue("" + mi.getPlayTime());
184:
185: c.add(lf);
186:
187: } else if ("location".equalsIgnoreCase(lf.getGet())
188:
189: && mi.getLocation() != null) {
190:
191: lf.setValue(mi.getLocation());
192:
193: c.add(lf);
194:
195: } else if ("size".equalsIgnoreCase(lf.getGet())) {
196:
197: lf.setValue("" + mi.getSize());
198:
199: c.add(lf);
200:
201: } else if ("copyright"
202: .equalsIgnoreCase(lf.getGet())) {
203:
204: lf.setValue(mi.getCopyright() ? "true"
205: : "false");
206:
207: c.add(lf);
208:
209: } else if ("crc".equalsIgnoreCase(lf.getGet())) {
210:
211: lf.setValue(mi.getCRC() ? "true" : "false");
212:
213: c.add(lf);
214:
215: } else if ("original".equalsIgnoreCase(lf.getGet())) {
216:
217: lf
218: .setValue(mi.getOriginal() ? "true"
219: : "false");
220:
221: c.add(lf);
222:
223: } else if ("vbr".equalsIgnoreCase(lf.getGet())) {
224:
225: lf.setValue(mi.getVBR() ? "true" : "false");
226:
227: c.add(lf);
228:
229: } else if ("track".equalsIgnoreCase(lf.getGet())) {
230:
231: lf.setValue("" + mi.getTrack());
232:
233: c.add(lf);
234:
235: } else if ("year".equalsIgnoreCase(lf.getGet())
236:
237: && mi.getYear() != null) {
238:
239: lf.setValue(mi.getYear());
240:
241: c.add(lf);
242:
243: } else if ("genre".equalsIgnoreCase(lf.getGet())
244:
245: && mi.getGenre() != null) {
246:
247: lf.setValue(mi.getGenre());
248:
249: c.add(lf);
250:
251: } else if ("title".equalsIgnoreCase(lf.getGet())
252:
253: && mi.getTitle() != null) {
254:
255: lf.setValue(mi.getTitle());
256:
257: c.add(lf);
258:
259: } else if ("artist".equalsIgnoreCase(lf.getGet())
260:
261: && mi.getArtist() != null) {
262:
263: lf.setValue(mi.getArtist());
264:
265: c.add(lf);
266:
267: } else if ("album".equalsIgnoreCase(lf.getGet())
268:
269: && mi.getAlbum() != null) {
270:
271: lf.setValue(mi.getAlbum());
272:
273: c.add(lf);
274:
275: } else if ("comments".equalsIgnoreCase(lf.getGet())
276:
277: && mi.getComment() != null) {
278:
279: lf.setValue(mi.getComment().toString());
280:
281: c.add(lf);
282:
283: }
284:
285: } else
286:
287: c.add(next);
288:
289: }
290:
291: } catch (IOException e) {
292:
293: logger.error(e.getMessage());
294:
295: } catch (UnsupportedAudioFileException e) {
296:
297: logger.error(e.getMessage());
298:
299: }
300:
301: return c;
302:
303: }
304:
305: /**
306: *
307: * @see ca.ulaval.bibl.lius.index.Indexer#getPopulatedCollection(java.lang.Object,
308: *
309: * java.lang.String)
310: *
311: */
312:
313: public Collection getPopulatedCollection(Object file,
314: String liusConfig) {
315:
316: LiusConfig lc = LiusConfigBuilder.getSingletonInstance()
317: .getLiusConfig(
318:
319: liusConfig);
320:
321: return getPopulatedCollection(file, lc);
322:
323: }
324:
325: /**
326: *
327: * @see ca.ulaval.bibl.lius.index.Indexer#getPopulatedCollection(java.lang.Object,
328: *
329: * ca.ulaval.bibl.lius.config.LiusConfig)
330: *
331: */
332:
333: public Collection getPopulatedCollection(Object file,
334: LiusConfig liusConfig) {
335:
336: return getPopulatedCollection(file, liusConfig.getMP3Fields());
337:
338: }
339:
340: }
|