/*
 * Copyright 2011 Lars Heuer (heuer[at]semagia.com). All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.semagia.cassa.server.store.sesame;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.openrdf.rio.RDFFormat;
import org.openrdf.rio.RDFParserRegistry;
import org.openrdf.rio.RDFWriterRegistry;
import org.openrdf.rio.Rio;

import com.semagia.cassa.common.MediaType;
import com.semagia.cassa.server.store.UnsupportedMediaTypeException;

/**
 * Internal utility functions.
 * 
 * @author Lars Heuer (heuer[at]semagia.com) <a href="http://www.semagia.com/">Semagia</a>
 */
final class SesameUtils {

    private static List<MediaType> _READABLE_MEDIATYPES;
    private static List<MediaType> _WRITABLE_MEDIATYPES;

    static {
        _READABLE_MEDIATYPES = new ArrayList<MediaType>();
        for (RDFFormat format: RDFParserRegistry.getInstance().getKeys()) {
            _READABLE_MEDIATYPES.add(asMediaType(format));
        }
        if (RDFParserRegistry.getInstance().has(RDFFormat.TURTLE)) {
            // <http://www.openrdf.org/issues/browse/RIO-74>
            _READABLE_MEDIATYPES.add(MediaType.TURTLE);
        }
        _READABLE_MEDIATYPES = Collections.unmodifiableList(_READABLE_MEDIATYPES);

        _WRITABLE_MEDIATYPES = new ArrayList<MediaType>();
        for (RDFFormat format: RDFWriterRegistry.getInstance().getKeys()) {
            _WRITABLE_MEDIATYPES.add(asMediaType(format));
        }
        if (RDFWriterRegistry.getInstance().has(RDFFormat.TURTLE)) {
            // <http://www.openrdf.org/issues/browse/RIO-74>
            _WRITABLE_MEDIATYPES.add(MediaType.TURTLE);
        }
        _WRITABLE_MEDIATYPES = Collections.unmodifiableList(_WRITABLE_MEDIATYPES);
    }

    private SesameUtils() {
        // noop.
    }

    /**
     * Returns all supported readable media types.
     *
     * @return An unordered list of readable media types.
     */
    public static List<MediaType> getReadableMediaTypes() {
        return _READABLE_MEDIATYPES;
    }

    /**
     * Returns all supported writable media types.
     *
     * @return An unordered list of writable media types.
     */
    public static List<MediaType> getWritableMediaTypes() {
        return _WRITABLE_MEDIATYPES;
    }

    /**
     * Converts the RDFFormat instance into a media type.
     *
     * @param format The RDFFormat to convert.
     * @return The RDFFormat equivalent.
     */
    private static MediaType asMediaType(final RDFFormat format) {
        return MediaType.valueOf(format.getDefaultMIMEType());
    }

    /**
     * Converts the provided media type into a string.
     * 
     * The default string representation of {@link MediaType} may contains the
     * paramaters (if any). This representation isn't comptible with {@link RDFFormat}.
     * 
     * This function returns just the type/subtype part of the provided media type.
     * 
     * @param mediaType The media type to convert.
     * @return "type/subtype"
     */
    private static String toString(final MediaType mediaType) {
        return mediaType.getType() + "/" + mediaType.getSubtype();
    }

    /**
     * Returns a readable RDFFormat.
     *
     * @param mediaType The requested media type.
     * @return The supported RDFFormat.
     * @throws UnsupportedMediaTypeException In case no reader for the media type can be found.
     */
    public static RDFFormat asReadableRDFFormat(final MediaType mediaType) throws UnsupportedMediaTypeException {
        RDFFormat format = Rio.getParserFormatForMIMEType(toString(mediaType));
        if (format == null && MediaType.TURTLE.equals(mediaType)) {
            format = RDFFormat.TURTLE;
        }
        if (format == null) {
            throw new UnsupportedMediaTypeException("The media type " + mediaType + " cannot be read", getReadableMediaTypes());
        }
        return format; 
    }

    /**
     * Returns a writable RDFFormat.
     *
     * @param mediaType The requested media type.
     * @return The supported RDFFormat.
     * @throws UnsupportedMediaTypeException In case no writer for the media type can be found.
     */
    public static RDFFormat asWritableRDFFormat(final MediaType mediaType) throws UnsupportedMediaTypeException {
        RDFFormat format = Rio.getWriterFormatForMIMEType(toString(mediaType));
        if (format == null && MediaType.TURTLE.equals(mediaType)) {
            format = RDFFormat.TURTLE;
        }
        if (format == null) {
            throw new UnsupportedMediaTypeException("The media type " + mediaType + " cannot be written", getWritableMediaTypes());
        }
        return format;
    }

}

@@ -3119,573 +3119,8 @@
  %7D%0A%0A
-    /**%0A     * Converts the provided media type into a string.%0A     * %0A     * The default string representation of %7B@link MediaType%7D may contains the%0A     * paramaters (if any). This representation isn't comptible with %7B@link RDFFormat%7D.%0A     * %0A     * This function returns just the type/subtype part of the provided media type.%0A     * %0A     * @param mediaType The media type to convert.%0A     * @return %22type/subtype%22%0A     */%0A    private static String toString(final MediaType mediaType) %7B%0A        return mediaType.getType() + %22/%22 + mediaType.getSubtype();%0A    %7D%0A%0A
     
@@ -3152,32 +3152,32 @@
 able RDFFormat.%0A
+
      *%0A     * @p
@@ -3529,33 +3529,24 @@
 ForMIMEType(
-toString(
 mediaType));
@@ -3534,32 +3534,59 @@
 METype(mediaType
+.toStringWithoutParameters(
 ));%0A        if (
@@ -4312,17 +4312,8 @@
 ype(
-toString(
 medi
@@ -4317,16 +4317,43 @@
 ediaType
+.toStringWithoutParameters(
 ));%0A    

Use new MediaType method toString w/o params