001/*
002 * Copyright 2002 - 2008 JEuclid, http://jeuclid.sf.net
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 */
016
017/* $Id$ */
018
019package net.sourceforge.jeuclid.xmlgraphics;
020
021import java.io.IOException;
022import java.util.Map;
023
024import net.sourceforge.jeuclid.Constants;
025import net.sourceforge.jeuclid.elements.AbstractJEuclidElement;
026
027import org.apache.xmlgraphics.image.loader.Image;
028import org.apache.xmlgraphics.image.loader.ImageException;
029import org.apache.xmlgraphics.image.loader.ImageFlavor;
030import org.apache.xmlgraphics.image.loader.ImageInfo;
031import org.apache.xmlgraphics.image.loader.ImageSessionContext;
032import org.apache.xmlgraphics.image.loader.impl.AbstractImageLoader;
033import org.apache.xmlgraphics.image.loader.impl.ImageXMLDOM;
034
035/**
036 * @version $Revision$
037 */
038public class ImageLoaderMathML extends AbstractImageLoader {
039
040    private final ImageFlavor targetFlavor;
041
042    /**
043     * Main constructor.
044     * 
045     * @param target
046     *            the target flavor
047     */
048    public ImageLoaderMathML(final ImageFlavor target) {
049        if (!(ImageFlavor.XML_DOM.equals(target))) {
050            throw new IllegalArgumentException(
051                    "Unsupported target ImageFlavor: " + target);
052        }
053        this.targetFlavor = target;
054    }
055
056    /** {@inheritDoc} */
057    public ImageFlavor getTargetFlavor() {
058        return this.targetFlavor;
059    }
060
061    /** {@inheritDoc} */
062    @SuppressWarnings("unchecked")
063    public Image loadImage(final ImageInfo info, final Map hints,
064            final ImageSessionContext session) throws ImageException,
065            IOException {
066        if (!Constants.MATHML_MIMETYPE.equals(info.getMimeType())) {
067            throw new IllegalArgumentException(
068                    "ImageInfo must be from an MathML image");
069        }
070        final Image img = info.getOriginalImage();
071        if (!(img instanceof ImageXMLDOM)) {
072            throw new IllegalArgumentException(
073                    "ImageInfo was expected to contain the MathML document as DOM");
074        }
075        final ImageXMLDOM mmlImage = (ImageXMLDOM) img;
076        if (!AbstractJEuclidElement.URI.equals(mmlImage.getRootNamespace())) {
077            throw new IllegalArgumentException(
078                    "The Image is not in the MathML namespace: "
079                            + mmlImage.getRootNamespace());
080        }
081        return mmlImage;
082    }
083}