blob: dc4678ba1cb2fa4efbbdf43dfa57df524064dc41 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
|
/* $Id$
* Copyright (C) 2001 The Apache Software Foundation. All rights reserved.
* For details on use and redistribution please refer to the
* LICENSE file included with these sources."
*/
package org.apache.fop.image;
// Java
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.MalformedURLException;
import java.lang.reflect.Constructor;
import java.util.Hashtable;
// FOP
import org.apache.fop.messaging.MessageHandler;
import org.apache.fop.image.analyser.ImageReaderFactory;
import org.apache.fop.image.analyser.ImageReader;
import org.apache.fop.configuration.Configuration;
/**
* create FopImage objects (with a configuration file - not yet implemented).
* @author Eric SCHAEFFER
*/
public class FopImageFactory {
private static Hashtable m_urlMap = new Hashtable();
/**
* create an FopImage objects.
* @param href image URL as a String
* @return a new FopImage object
* @exception java.net.MalformedURLException bad URL
* @exception FopImageException an error occured during construction
*/
public static FopImage Make(String href)
throws MalformedURLException, FopImageException {
// Get the absolute URL
URL absoluteURL = null;
InputStream imgIS = null;
try {
absoluteURL = new URL(href);
imgIS = absoluteURL.openStream();
} catch (Exception e) {
// maybe relative
URL context_url = null;
try {
String baseDir = Configuration.getStringValue("baseDir");
context_url = new URL(baseDir); // how to get the context URL ?
try {
absoluteURL = new URL(context_url, href);
} catch (MalformedURLException e_abs) {
// not found
throw new FopImageException( "Invalid Image URL : " +
e_abs.getMessage() + "(base URL " +
context_url.toString() + ")");
}
} catch (MalformedURLException e_context) {
// pb context url
throw new FopImageException(
"Invalid Image URL - error on relative URL : " +
e_context.getMessage());
}
}
// check if already created
FopImage imageObject =
(FopImage) m_urlMap.get(absoluteURL.toString());
if (imageObject != null)
return imageObject;
// If not, check image type
ImageReader imgReader = null;
try {
if (imgIS == null) {
imgIS = absoluteURL.openStream();
}
imgReader = ImageReaderFactory.Make(absoluteURL.toExternalForm(), imgIS);
} catch (Exception e) {
throw new FopImageException(
"Error while recovering Image Informations (" +
absoluteURL.toString() + ") : " + e.getMessage());
}
finally { if (imgIS != null) {
try {
imgIS.close();
} catch (IOException e) {}
}
} if (imgReader == null)
throw new FopImageException(
"No ImageReader for this type of image (" +
absoluteURL.toString() + ")");
// Associate mime-type to FopImage class
String imgMimeType = imgReader.getMimeType();
String imgClassName = null;
if ("image/gif".equals(imgMimeType)) {
imgClassName = "org.apache.fop.image.GifJpegImage";
// imgClassName = "org.apache.fop.image.JAIImage";
} else if ("image/jpeg".equals(imgMimeType)) {
imgClassName = "org.apache.fop.image.GifJpegImage";
// imgClassName = "org.apache.fop.image.JAIImage";
} else if ("image/bmp".equals(imgMimeType)) {
imgClassName = "org.apache.fop.image.BmpImage";
// imgClassName = "org.apache.fop.image.JAIImage";
} else if ("image/png".equals(imgMimeType)) {
imgClassName = "org.apache.fop.image.JimiImage";
// imgClassName = "org.apache.fop.image.JAIImage";
} else if ("image/tga".equals(imgMimeType)) {
imgClassName = "org.apache.fop.image.JimiImage";
// imgClassName = "org.apache.fop.image.JAIImage";
} else if ("image/tiff".equals(imgMimeType)) {
imgClassName = "org.apache.fop.image.JimiImage";
// imgClassName = "org.apache.fop.image.JAIImage";
} else if ("image/svg-xml".equals(imgMimeType)) {
imgClassName = "org.apache.fop.image.SVGImage";
}
if (imgClassName == null)
throw new FopImageException("Unsupported image type (" +
absoluteURL.toString() + ") : " + imgMimeType);
// load the right image class
// return new <FopImage implementing class>
Object imageInstance = null;
Class imageClass = null;
try {
imageClass = Class.forName(imgClassName);
Class[] imageConstructorParameters = new Class[2];
imageConstructorParameters[0] = Class.forName("java.net.URL");
imageConstructorParameters[1] = Class.forName("org.apache.fop.image.analyser.ImageReader");
Constructor imageConstructor =
imageClass.getDeclaredConstructor(
imageConstructorParameters);
Object[] initArgs = new Object[2];
initArgs[0] = absoluteURL;
initArgs[1] = imgReader;
imageInstance = imageConstructor.newInstance(initArgs);
} catch (java.lang.reflect.InvocationTargetException ex) {
Throwable t = ex.getTargetException();
String msg;
if (t != null) {
msg = t.getMessage();
} else {
msg = ex.getMessage();
}
throw new FopImageException(
"Error creating FopImage object (" +
absoluteURL.toString() + ") : " + msg);
}
catch (Exception ex) {
throw new FopImageException(
"Error creating FopImage object (" +
"Error creating FopImage object (" +
absoluteURL.toString() + ") : " + ex.getMessage());
}
if (! (imageInstance instanceof org.apache.fop.image.FopImage)) {
throw new FopImageException(
"Error creating FopImage object (" +
absoluteURL.toString() + ") : " + "class " +
imageClass.getName() + " doesn't implement org.apache.fop.image.FopImage interface");
}
m_urlMap.put(absoluteURL.toString(), imageInstance);
return (FopImage) imageInstance;
}
}
|