mirror of
https://github.com/apache/poi.git
synced 2025-02-07 18:48:20 +00:00
#64036 - Replace reflection calls in factories for Java 9+
ImageRenderer implementation are now loaded via ServiceLoader fixed the ServiceLoader.load invocations to pass a sensible ClassLoader as OSGi preparation git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1880911 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
afe4a109a8
commit
506089a9f1
@ -23,7 +23,9 @@ import java.awt.Insets;
|
|||||||
import java.awt.Paint;
|
import java.awt.Paint;
|
||||||
import java.awt.geom.Rectangle2D;
|
import java.awt.geom.Rectangle2D;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.lang.reflect.InvocationTargetException;
|
import java.util.ServiceLoader;
|
||||||
|
import java.util.function.Supplier;
|
||||||
|
import java.util.stream.StreamSupport;
|
||||||
|
|
||||||
import org.apache.poi.sl.usermodel.PictureData;
|
import org.apache.poi.sl.usermodel.PictureData;
|
||||||
import org.apache.poi.sl.usermodel.PictureShape;
|
import org.apache.poi.sl.usermodel.PictureShape;
|
||||||
@ -77,44 +79,31 @@ public class DrawPictureShape extends DrawSimpleShape {
|
|||||||
* @param graphics the graphics context
|
* @param graphics the graphics context
|
||||||
* @return the image renderer
|
* @return the image renderer
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings({"unchecked"})
|
|
||||||
public static ImageRenderer getImageRenderer(Graphics2D graphics, String contentType) {
|
public static ImageRenderer getImageRenderer(Graphics2D graphics, String contentType) {
|
||||||
final ImageRenderer renderer = (graphics != null) ? (ImageRenderer)graphics.getRenderingHint(Drawable.IMAGE_RENDERER) : null;
|
final ImageRenderer renderer = (graphics != null) ? (ImageRenderer)graphics.getRenderingHint(Drawable.IMAGE_RENDERER) : null;
|
||||||
if (renderer != null && renderer.canRender(contentType)) {
|
if (renderer != null && renderer.canRender(contentType)) {
|
||||||
return renderer;
|
return renderer;
|
||||||
}
|
}
|
||||||
|
|
||||||
// first try with our default image renderer
|
final BitmapImageRenderer fallback = new BitmapImageRenderer();
|
||||||
final BitmapImageRenderer bir = new BitmapImageRenderer();
|
if (fallback.canRender(contentType)) {
|
||||||
if (bir.canRender(contentType)) {
|
return fallback;
|
||||||
return bir;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// then iterate through the scratchpad renderers
|
// the fallback is the BitmapImageRenderer, at least it gracefully handles invalid images
|
||||||
//
|
final Supplier<ImageRenderer> getFallback = () -> {
|
||||||
// this could be nicely implemented via a j.u.ServiceLoader, but OSGi makes things complicated ...
|
LOG.log(POILogger.WARN, "No suiteable image renderer found for content-type '"+
|
||||||
// https://blog.osgi.org/2013/02/javautilserviceloader-in-osgi.html
|
contentType+"' - include poi-scratchpad (for wmf/emf) or poi-ooxml (for svg) jars!");
|
||||||
// ... therefore falling back to classloading attempts
|
return fallback;
|
||||||
ClassLoader cl = ImageRenderer.class.getClassLoader();
|
};
|
||||||
for (String kr : KNOWN_RENDERER) {
|
|
||||||
final ImageRenderer ir;
|
|
||||||
try {
|
|
||||||
ir = ((Class<? extends ImageRenderer>)cl.loadClass(kr)).getConstructor().newInstance();
|
|
||||||
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
|
|
||||||
// scratchpad was not on the path, ignore and continue
|
|
||||||
LOG.log(POILogger.INFO, "Known image renderer '"+kr+" not found/loaded - include poi-scratchpad jar!", e);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (ir.canRender(contentType)) {
|
|
||||||
return ir;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
LOG.log(POILogger.WARN, "No suiteable image renderer found for content-type '"+
|
ClassLoader cl = DrawPictureShape.class.getClassLoader();
|
||||||
contentType+"' - include poi-scratchpad jar!");
|
return StreamSupport
|
||||||
|
.stream(ServiceLoader.load(ImageRenderer.class, cl).spliterator(), false)
|
||||||
// falling back to BitmapImageRenderer, at least it gracefully handles invalid images
|
.filter(ir -> ir.canRender(contentType))
|
||||||
return bir;
|
.findFirst()
|
||||||
|
.orElseGet(getFallback)
|
||||||
|
;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
Loading…
x
Reference in New Issue
Block a user