View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.tika.parser.image;
18  
19   import java.io.IOException;
20  import java.io.InputStream;
21  import java.util.Arrays;
22  import java.util.Collections;
23  import java.util.HashSet;
24  import java.util.Iterator;
25  import java.util.Set;
26  
27  import javax.imageio.IIOException;
28  import javax.imageio.ImageIO;
29  import javax.imageio.ImageReader;
30  
31  import org.apache.tika.exception.TikaException;
32  import org.apache.tika.io.CloseShieldInputStream;
33  import org.apache.tika.metadata.Metadata;
34  import org.apache.tika.mime.MediaType;
35  import org.apache.tika.parser.ParseContext;
36  import org.apache.tika.parser.Parser;
37  import org.apache.tika.sax.XHTMLContentHandler;
38  import org.xml.sax.ContentHandler;
39  import org.xml.sax.SAXException;
40  
41  public class ImageParser implements Parser {
42  
43      private static final Set<MediaType> SUPPORTED_TYPES =
44          Collections.unmodifiableSet(new HashSet<MediaType>(Arrays.asList(
45                  MediaType.image("bmp"),
46                  MediaType.image("gif"),
47                  MediaType.image("png"),
48                  MediaType.image("tiff"),
49                  MediaType.image("vnd.wap.wbmp"),
50                  MediaType.image("x-icon"),
51                  MediaType.image("x-psd"),
52                  MediaType.image("x-xcf"))));
53  
54      public Set<MediaType> getSupportedTypes(ParseContext context) {
55          return SUPPORTED_TYPES;
56      }
57  
58      public void parse(
59              InputStream stream, ContentHandler handler,
60              Metadata metadata, ParseContext context)
61              throws IOException, SAXException, TikaException {
62          String type = metadata.get(Metadata.CONTENT_TYPE);
63          if (type != null) {
64              try {
65                  Iterator<ImageReader> iterator =
66                      ImageIO.getImageReadersByMIMEType(type);
67                  if (iterator.hasNext()) {
68                      ImageReader reader = iterator.next();
69                      reader.setInput(ImageIO.createImageInputStream(
70                              new CloseShieldInputStream(stream)));
71                      metadata.set("height", Integer.toString(reader.getHeight(0)));
72                      metadata.set("width", Integer.toString(reader.getWidth(0)));
73                      reader.dispose();
74                  }
75              } catch (IIOException e) {
76                  throw new TikaException(type + " parse error", e);
77              }
78          }
79  
80          XHTMLContentHandler xhtml = new XHTMLContentHandler(handler, metadata);
81          xhtml.startDocument();
82          xhtml.endDocument();
83      }
84  
85      /**
86       * @deprecated This method will be removed in Apache Tika 1.0.
87       */
88      public void parse(
89              InputStream stream, ContentHandler handler, Metadata metadata)
90              throws IOException, SAXException, TikaException {
91          parse(stream, handler, metadata, new ParseContext());
92      }
93  
94  }