class Loader {
private ImageInfo getImage(String uri, Source src,
ImageContext context) {
// parse document and get the size attributes of the svg element
InputStream in = new UnclosableInputStream(ImageUtil.needInputStream(src));
try {
in.mark(4 + 1);
DataInputStream din = new DataInputStream(in);
int magic = EndianUtils.swapInteger(din.readInt());
din.reset();
if (magic != WMFConstants.META_ALDUS_APM) {
return null; //Not a WMF file
}
WMFRecordStore wmfStore = new WMFRecordStore();
wmfStore.read(din);
IOUtils.closeQuietly(din);
int width = wmfStore.getWidthUnits();
int height = wmfStore.getHeightUnits();
int dpi = wmfStore.getMetaFileUnitsPerInch();
ImageInfo info = new ImageInfo(uri, "image/x-wmf");
ImageSize size = new ImageSize();
size.setSizeInPixels(width, height);
size.setResolution(dpi);
size.calcSizeFromPixels();
info.setSize(size);
ImageWMF img = new ImageWMF(info, wmfStore);
info.getCustomObjects().put(ImageInfo.ORIGINAL_IMAGE, img);
return info;
} catch (NoClassDefFoundError ncdfe) {
try {
in.reset();
} catch (IOException ioe) {
// we're more interested in the original exception
}
batikAvailable = false;
log.warn("Batik not in class path", ncdfe);
return null;
} catch (IOException e) {
// If the svg is invalid then it throws an IOException
// so there is no way of knowing if it is an svg document
log.debug("Error while trying to load stream as an WMF file: "
+ e.getMessage());
// assuming any exception means this document is not svg
// or could not be loaded for some reason
try {
in.reset();
} catch (IOException ioe) {
// we're more interested in the original exception
}
return null;
}