001 /*
002 *
003 * $Revision: 13085 $ $Date: 2008-02-06 18:27:24 +0100 (Mi, 06 Feb 2008) $
004 *
005 * This file is part of *** M y C o R e ***
006 * See http://www.mycore.de/ for details.
007 *
008 * This program is free software; you can use it, redistribute it
009 * and / or modify it under the terms of the GNU General Public License
010 * (GPL) as published by the Free Software Foundation; either version 2
011 * of the License or (at your option) any later version.
012 *
013 * This program is distributed in the hope that it will be useful, but
014 * WITHOUT ANY WARRANTY; without even the implied warranty of
015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016 * GNU General Public License for more details.
017 *
018 * You should have received a copy of the GNU General Public License
019 * along with this program, in a file called gpl.txt or license.txt.
020 * If not, write to the Free Software Foundation Inc.,
021 * 59 Temple Place - Suite 330, Boston, MA 02111-1307 USA
022 */
023
024 package org.mycore.datamodel.ifs.extractors;
025
026 import java.io.InputStream;
027 import java.util.Iterator;
028
029 import org.apache.log4j.Logger;
030 import org.jdom.Element;
031
032 import com.drew.imaging.jpeg.JpegMetadataReader;
033 import com.drew.imaging.jpeg.JpegProcessingException;
034 import com.drew.metadata.Directory;
035 import com.drew.metadata.Metadata;
036 import com.drew.metadata.Tag;
037
038 /**
039 * Extracts EXIF and IPTC metadata from JPEG files. Uses the metadata extraction
040 * library from Drew Noakes in the com.drew.** packages. See
041 * http://www.drewnoakes.com/code/exif/ for details.
042 *
043 * @author Frank Lützenkirchen
044 * @version $Revision: 13085 $ $Date: 2008-02-06 18:27:24 +0100 (Mi, 06 Feb 2008) $
045 */
046 public class MCRDataExtractorJPEG extends MCRDataExtractor {
047
048 /** The logger */
049 private final static Logger LOGGER = Logger.getLogger(MCRDataExtractorJPEG.class);
050
051 protected String getSupportedContentTypeIDs() {
052 return "jpeg";
053 }
054
055 protected void extractData(Element container, InputStream in) throws JpegProcessingException {
056 Metadata metadata = JpegMetadataReader.readMetadata(in);
057 Iterator directories = metadata.getDirectoryIterator();
058 while (directories.hasNext()) {
059 Directory directory = (Directory) directories.next();
060 extractDirectoryData(container, directory);
061 }
062 }
063
064 /**
065 * Extract data from the given metadata directory (like EXIF, IPTC)
066 */
067 private void extractDirectoryData(Element xData, Directory directory) {
068 try {
069 Element xDirectory = new Element("directory");
070 xData.addContent(xDirectory);
071 String dirName = directory.getName();
072 if (dirName != null)
073 xDirectory.setAttribute("name", dirName);
074 Iterator tags = directory.getTagIterator();
075 while (tags.hasNext()) {
076 Tag tag = (Tag) tags.next();
077 extractTagData(xDirectory, tag);
078 }
079 } catch (Exception ex) {
080 LOGGER.debug(ex.getClass().getName() + ": " + ex.getLocalizedMessage());
081 }
082 }
083
084 /**
085 * Extract data of a single metadata tag
086 */
087 private void extractTagData(Element xDirectory, Tag tag) {
088 try {
089 Element xTag = new Element("tag");
090 xTag.setAttribute("name", tag.getTagName());
091 xTag.setText(tag.getDescription());
092 xDirectory.addContent(xTag);
093 } catch (Exception ex) {
094 LOGGER.debug(ex.getClass().getName() + ": " + ex.getLocalizedMessage());
095 }
096 }
097
098 /**
099 * Test application that outputs extracted metadata for a given local file.
100 *
101 * @param args
102 * the path to a locally stored JPEG file
103 */
104 public static void main(String[] args) {
105 new MCRDataExtractorJPEG().testLocalFile(args[0]);
106 }
107 }