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.commons.imaging.internal;
18  
19  import java.io.IOException;
20  import java.util.function.Predicate;
21  import java.util.function.Supplier;
22  
23  import org.apache.commons.imaging.AbstractImageParser;
24  import org.apache.commons.imaging.ImageFormat;
25  import org.apache.commons.imaging.ImageFormats;
26  import org.apache.commons.imaging.Imaging;
27  import org.apache.commons.imaging.ImagingParameters;
28  import org.apache.commons.imaging.bytesource.ByteSource;
29  
30  /**
31   * Internal utilities.
32   *
33   * @since 1.0-alpha3
34   */
35  public final class ImageParserFactory {
36  
37      public static <T extends ImagingParameters<T>> AbstractImageParser<T> getImageParser(final ByteSource byteSource) throws IOException {
38          // TODO: circular dependency between Imaging and internal Util class below.
39          final ImageFormat format = Imaging.guessFormat(byteSource);
40          if (!format.equals(ImageFormats.UNKNOWN)) {
41              return ImageParserFactory.getImageParser(format);
42          }
43  
44          final String fileName = byteSource.getFileName();
45          if (fileName != null) {
46              return ImageParserFactory.getImageParser(fileName);
47          }
48  
49          throw new IllegalArgumentException("Can't parse this format.");
50      }
51  
52      public static <T extends ImagingParameters<T>> AbstractImageParser<T> getImageParser(final ImageFormat format) {
53          return getImageParser(parser -> parser.canAcceptType(format), () -> new IllegalArgumentException("Unknown ImageFormat: " + format));
54      }
55  
56      // This generics suppression is as good as the predicate given. If the predicate violates a generics design,
57      // then there will be an error during runtime.
58      @SuppressWarnings("unchecked")
59      private static <T extends ImagingParameters<T>> AbstractImageParser<T> getImageParser(final Predicate<AbstractImageParser<?>> pred,
60              final Supplier<? extends RuntimeException> supplier) {
61          return (AbstractImageParser<T>) AbstractImageParser.getAllImageParsers().stream().filter(pred).findFirst().orElseThrow(supplier);
62      }
63  
64      public static <T extends ImagingParameters<T>> AbstractImageParser<T> getImageParser(final String fileExtension) {
65          return getImageParser(parser -> parser.canAcceptExtension(fileExtension), () -> new IllegalArgumentException("Unknown extension: " + fileExtension));
66      }
67  
68      private ImageParserFactory() {
69      }
70  }