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 */ 017 018package org.apache.commons.imaging.internal; 019 020import java.io.IOException; 021import java.util.function.Predicate; 022import java.util.function.Supplier; 023 024import org.apache.commons.imaging.AbstractImageParser; 025import org.apache.commons.imaging.ImageFormat; 026import org.apache.commons.imaging.ImageFormats; 027import org.apache.commons.imaging.Imaging; 028import org.apache.commons.imaging.ImagingParameters; 029import org.apache.commons.imaging.bytesource.ByteSource; 030 031/** 032 * Internal utilities. 033 * 034 * @since 1.0-alpha3 035 */ 036public final class ImageParserFactory { 037 038 public static <T extends ImagingParameters<T>> AbstractImageParser<T> getImageParser(final ByteSource byteSource) throws IOException { 039 // TODO: circular dependency between Imaging and internal Util class below. 040 final ImageFormat format = Imaging.guessFormat(byteSource); 041 if (!format.equals(ImageFormats.UNKNOWN)) { 042 return getImageParser(format); 043 } 044 final String fileName = byteSource.getFileName(); 045 if (fileName != null) { 046 return getImageParser(fileName); 047 } 048 throw new IllegalArgumentException("Can't parse this format."); 049 } 050 051 public static <T extends ImagingParameters<T>> AbstractImageParser<T> getImageParser(final ImageFormat format) { 052 return getImageParser(parser -> parser.canAcceptType(format), () -> new IllegalArgumentException("Unknown ImageFormat: " + format)); 053 } 054 055 // This generics suppression is as good as the predicate given. If the predicate violates a generics design, 056 // then there will be an error during runtime. 057 @SuppressWarnings("unchecked") 058 private static <T extends ImagingParameters<T>> AbstractImageParser<T> getImageParser(final Predicate<AbstractImageParser<?>> pred, 059 final Supplier<? extends RuntimeException> supplier) { 060 return (AbstractImageParser<T>) AbstractImageParser.getAllImageParsers().stream().filter(pred).findFirst().orElseThrow(supplier); 061 } 062 063 public static <T extends ImagingParameters<T>> AbstractImageParser<T> getImageParser(final String fileExtension) { 064 return getImageParser(parser -> parser.canAcceptExtension(fileExtension), () -> new IllegalArgumentException("Unknown extension: " + fileExtension)); 065 } 066 067 private ImageParserFactory() { 068 } 069}