001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.commons.imaging.internal; 018 019import java.io.IOException; 020import java.util.function.Predicate; 021import java.util.function.Supplier; 022 023import org.apache.commons.imaging.AbstractImageParser; 024import org.apache.commons.imaging.ImageFormat; 025import org.apache.commons.imaging.ImageFormats; 026import org.apache.commons.imaging.Imaging; 027import org.apache.commons.imaging.ImagingParameters; 028import org.apache.commons.imaging.bytesource.ByteSource; 029 030/** 031 * Internal utilities. 032 * 033 * @since 1.0-alpha3 034 */ 035public final class ImageParserFactory { 036 037 public static <T extends ImagingParameters<T>> AbstractImageParser<T> getImageParser(final ByteSource byteSource) throws IOException { 038 // TODO: circular dependency between Imaging and internal Util class below. 039 final ImageFormat format = Imaging.guessFormat(byteSource); 040 if (!format.equals(ImageFormats.UNKNOWN)) { 041 return ImageParserFactory.getImageParser(format); 042 } 043 044 final String fileName = byteSource.getFileName(); 045 if (fileName != null) { 046 return ImageParserFactory.getImageParser(fileName); 047 } 048 049 throw new IllegalArgumentException("Can't parse this format."); 050 } 051 052 public static <T extends ImagingParameters<T>> AbstractImageParser<T> getImageParser(final ImageFormat format) { 053 return getImageParser(parser -> parser.canAcceptType(format), () -> new IllegalArgumentException("Unknown ImageFormat: " + format)); 054 } 055 056 // This generics suppression is as good as the predicate given. If the predicate violates a generics design, 057 // then there will be an error during runtime. 058 @SuppressWarnings("unchecked") 059 private static <T extends ImagingParameters<T>> AbstractImageParser<T> getImageParser(final Predicate<AbstractImageParser<?>> pred, 060 final Supplier<? extends RuntimeException> supplier) { 061 return (AbstractImageParser<T>) AbstractImageParser.getAllImageParsers().stream().filter(pred).findFirst().orElseThrow(supplier); 062 } 063 064 public static <T extends ImagingParameters<T>> AbstractImageParser<T> getImageParser(final String fileExtension) { 065 return getImageParser(parser -> parser.canAcceptExtension(fileExtension), () -> new IllegalArgumentException("Unknown extension: " + fileExtension)); 066 } 067 068 private ImageParserFactory() { 069 } 070}