001    /*
002     * JBoss, Home of Professional Open Source.
003     * Copyright 2008, Red Hat Middleware LLC, and individual contributors
004     * as indicated by the @author tags. See the copyright.txt file in the
005     * distribution for a full listing of individual contributors. 
006     *
007     * This is free software; you can redistribute it and/or modify it
008     * under the terms of the GNU Lesser General Public License as
009     * published by the Free Software Foundation; either version 2.1 of
010     * the License, or (at your option) any later version.
011     *
012     * This software is distributed in the hope that it will be useful,
013     * but WITHOUT ANY WARRANTY; without even the implied warranty of
014     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015     * Lesser General Public License for more details.
016     *
017     * You should have received a copy of the GNU Lesser General Public
018     * License along with this software; if not, write to the Free
019     * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020     * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021     */
022    package org.jboss.dna.sequencer.mp3;
023    
024    import java.io.InputStream;
025    import org.jboss.dna.graph.sequencers.SequencerContext;
026    import org.jboss.dna.graph.sequencers.SequencerOutput;
027    import org.jboss.dna.graph.sequencers.StreamSequencer;
028    
029    /**
030     * A sequencer that processes the binary content of an MP3 audio file, extracts the metadata for the file, and then writes that
031     * audio metadata to the repository.
032     * <p>
033     * This sequencer produces data that corresponds to the following structure:
034     * <ul>
035     * <li><strong>mp3:metadata</strong> node of type <code>mp3:metadata</code>
036     * <ul>
037     * <li><strong>mp3:title</strong> - optional string property for the name of the audio file or recording</li>
038     * <li><strong>mp3:author</strong> - optional string property for the author of the recording</li>
039     * <li><strong>mp3:album</strong> - optional string property for the name of the album</li>
040     * <li><strong>mp3:year</strong> - optional integer property for the year the recording as created</li>
041     * <li><strong>mp3:comment</strong> - optional string property specifying a comment</li>
042     * </ul>
043     * </li>
044     * </ul>
045     * </p>
046     * 
047     * @author Stefano Maestri
048     * @author Randall Hauch
049     * @author John Verhaeg
050     */
051    public class Mp3MetadataSequencer implements StreamSequencer {
052    
053        public static final String METADATA_NODE = "mp3:metadata";
054        public static final String MP3_PRIMARY_TYPE = "jcr:primaryType";
055        public static final String MP3_TITLE = "mp3:title";
056        public static final String MP3_AUTHOR = "mp3:author";
057        public static final String MP3_ALBUM = "mp3:album";
058        public static final String MP3_YEAR = "mp3:year";
059        public static final String MP3_COMMENT = "mp3:comment";
060    
061        /**
062         * {@inheritDoc}
063         * 
064         * @see StreamSequencer#sequence(InputStream, SequencerOutput, SequencerContext)
065         */
066        public void sequence( InputStream stream,
067                              SequencerOutput output,
068                              SequencerContext context ) {
069            Mp3Metadata metadata = Mp3Metadata.instance(stream);
070    
071            if (metadata != null) {
072                // Place the image metadata into the output map ...
073                output.setProperty(METADATA_NODE, MP3_PRIMARY_TYPE, "mp3:metadata");
074                output.setProperty(METADATA_NODE, MP3_TITLE, metadata.getTitle());
075                output.setProperty(METADATA_NODE, MP3_AUTHOR, metadata.getAuthor());
076                output.setProperty(METADATA_NODE, MP3_ALBUM, metadata.getAlbum());
077                output.setProperty(METADATA_NODE, MP3_YEAR, metadata.getYear());
078                output.setProperty(METADATA_NODE, MP3_COMMENT, metadata.getComment());
079            }
080        }
081    }