View Javadoc

1   /*
2    * Copyright 2002 - 2008 JEuclid, http://jeuclid.sf.net
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  /* $Id: ImageLoaderMathML.java,v 70597ba9d706 2008/02/18 19:42:05 maxberger $ */
18  
19  package net.sourceforge.jeuclid.xmlgraphics;
20  
21  import java.io.IOException;
22  import java.util.Map;
23  
24  import net.sourceforge.jeuclid.Constants;
25  import net.sourceforge.jeuclid.elements.AbstractJEuclidElement;
26  
27  import org.apache.xmlgraphics.image.loader.Image;
28  import org.apache.xmlgraphics.image.loader.ImageException;
29  import org.apache.xmlgraphics.image.loader.ImageFlavor;
30  import org.apache.xmlgraphics.image.loader.ImageInfo;
31  import org.apache.xmlgraphics.image.loader.ImageSessionContext;
32  import org.apache.xmlgraphics.image.loader.impl.AbstractImageLoader;
33  import org.apache.xmlgraphics.image.loader.impl.ImageXMLDOM;
34  
35  /**
36   * @version $Revision: 70597ba9d706 $
37   */
38  public class ImageLoaderMathML extends AbstractImageLoader {
39  
40      private final ImageFlavor targetFlavor;
41  
42      /**
43       * Main constructor.
44       * 
45       * @param target
46       *            the target flavor
47       */
48      public ImageLoaderMathML(final ImageFlavor target) {
49          if (!(ImageFlavor.XML_DOM.equals(target))) {
50              throw new IllegalArgumentException(
51                      "Unsupported target ImageFlavor: " + target);
52          }
53          this.targetFlavor = target;
54      }
55  
56      /** {@inheritDoc} */
57      public ImageFlavor getTargetFlavor() {
58          return this.targetFlavor;
59      }
60  
61      /** {@inheritDoc} */
62      @SuppressWarnings("unchecked")
63      public Image loadImage(final ImageInfo info, final Map hints,
64              final ImageSessionContext session) throws ImageException,
65              IOException {
66          if (!Constants.MATHML_MIMETYPE.equals(info.getMimeType())) {
67              throw new IllegalArgumentException(
68                      "ImageInfo must be from an MathML image");
69          }
70          final Image img = info.getOriginalImage();
71          if (!(img instanceof ImageXMLDOM)) {
72              throw new IllegalArgumentException(
73                      "ImageInfo was expected to contain the MathML document as DOM");
74          }
75          final ImageXMLDOM mmlImage = (ImageXMLDOM) img;
76          if (!AbstractJEuclidElement.URI.equals(mmlImage.getRootNamespace())) {
77              throw new IllegalArgumentException(
78                      "The Image is not in the MathML namespace: "
79                              + mmlImage.getRootNamespace());
80          }
81          return mmlImage;
82      }
83  }