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