1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
37
38 public class ImageLoaderMathML extends AbstractImageLoader {
39
40 private final ImageFlavor targetFlavor;
41
42
43
44
45
46
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
57 public ImageFlavor getTargetFlavor() {
58 return this.targetFlavor;
59 }
60
61
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 }