001/**
002 * Copyright 2015 DuraSpace, Inc.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.fcrepo.transform;
017
018import org.fcrepo.transform.transformations.LDPathTransform;
019import org.fcrepo.transform.transformations.SparqlQueryTransform;
020
021import javax.ws.rs.core.MediaType;
022
023import java.io.InputStream;
024import java.util.HashMap;
025import java.util.Map;
026
027import static org.apache.jena.riot.WebContent.contentTypeSPARQLQuery;
028import static org.fcrepo.transform.transformations.LDPathTransform.APPLICATION_RDF_LDPATH;
029
030/**
031 * Get a Transformation from a MediaType
032 *
033 * @author cbeer
034 */
035public class TransformationFactory {
036
037    private Map<String, Transformation<?>> mimeToTransform = new HashMap<>();
038
039    /**
040     * Get a new TransformationFactory with the default classes
041     * @throws SecurityException if security exception occurred
042     */
043    public TransformationFactory() {
044        mimeToTransform.put(contentTypeSPARQLQuery, new SparqlQueryTransform(null));
045        mimeToTransform.put(APPLICATION_RDF_LDPATH, new LDPathTransform(null));
046    }
047
048    /**
049     * Get a Transformation from a MediaType and an InputStream with
050     * the transform program
051     * @param contentType the content type
052     * @param inputStream the input stream
053     * @return a Transformation
054     */
055
056    public <T> Transformation<T> getTransform(final MediaType contentType, final InputStream inputStream) {
057        final String mimeType = contentType.toString();
058        if (mimeToTransform.containsKey(mimeType)) {
059            return (Transformation<T>) mimeToTransform.get(contentType.toString()).newTransform(inputStream);
060        }
061        throw new UnsupportedOperationException(
062                "No transform type exists for media type " + mimeType + "!");
063    }
064}