throw new BridgeException(ctx, e, secEx, ERR_URI_UNSECURE,
new Object[] {purl});
}
DocumentLoader loader = ctx.getDocumentLoader();
ImageTagRegistry reg = ImageTagRegistry.getRegistry();
ICCColorSpaceExt colorspace = extractColorSpace(e, ctx);
{
/**
* Before we open the URL we see if we have the
* URL already cached and parsed
*/
try {
/* Check the document loader cache */
Document doc = loader.checkCache(purl.toString());
if (doc != null) {
imgDocument = (SVGDocument)doc;
return createSVGImageNode(ctx, e, imgDocument);
}
} catch (BridgeException ex) {
throw ex;
} catch (Exception ex) {
/* Nothing to do */
}
/* Check the ImageTagRegistry Cache */
Filter img = reg.checkCache(purl, colorspace);
if (img != null) {
return createRasterImageNode(ctx, e, img, purl);
}
}
/* The Protected Stream ensures that the stream doesn't
* get closed unless we want it to. It is also based on
* a Buffered Reader so in general we can mark the start
* and reset rather than reopening the stream. Finally
* it hides the mark/reset methods so only we get to
* use them.
*/
ProtectedStream reference = null;
try {
reference = openStream(e, purl);
} catch (SecurityException secEx ) {
throw new BridgeException(ctx, e, secEx, ERR_URI_UNSECURE,
new Object[] {purl});
} catch (IOException ioe) {
return createBrokenImageNode(ctx, e, purl.toString(),
ioe.getLocalizedMessage());
}
{
/**
* First see if we can id the file as a Raster via magic
* number. This is probably the fastest mechanism.
* We tell the registry what the source purl is but we
* tell it not to open that url.
*/
Filter img = reg.readURL(reference, purl, colorspace,
false, false);
if (img != null) {
// It's a bouncing baby Raster...
return createRasterImageNode(ctx, e, img, purl);
}
}
try {
// Reset the stream for next try.
reference.retry();
} catch (IOException ioe) {
reference.release();
reference = null;
try {
// Couldn't reset stream so reopen it.
reference = openStream(e, purl);
} catch (IOException ioe2) {
// Since we already opened the stream this is unlikely.
return createBrokenImageNode(ctx, e, purl.toString(),
ioe2.getLocalizedMessage());
}
}
try {
/**
* Next see if it's an XML document.
*/
Document doc = loader.loadDocument(purl.toString(), reference);
imgDocument = (SVGDocument)doc;
return createSVGImageNode(ctx, e, imgDocument);
} catch (BridgeException ex) {
throw ex;
} catch (SecurityException secEx ) {
throw new BridgeException(ctx, e, secEx, ERR_URI_UNSECURE,
new Object[] {purl});
} catch (InterruptedIOException iioe) {
if (HaltingThread.hasBeenHalted())
throw new InterruptedBridgeException();
} catch (InterruptedBridgeException ibe) {
throw ibe;
} catch (Exception ex) {
/* Nothing to do */
// ex.printStackTrace();
}
try {
reference.retry();
} catch (IOException ioe) {
reference.release();
reference = null;
try {
// Couldn't reset stream so reopen it.
reference = openStream(e, purl);
} catch (IOException ioe2) {
return createBrokenImageNode(ctx, e, purl.toString(),
ioe2.getLocalizedMessage());
}
}
try {
// Finally try to load the image as a raster image (JPG or
// PNG) allowing the registry to open the url (so the
// JDK readers can be checked).
Filter img = reg.readURL(reference, purl, colorspace,
true, true);
if (img != null) {
// It's a bouncing baby Raster...
return createRasterImageNode(ctx, e, img, purl);
}