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