/** * Returns the ImageInputStream of a Source object. * @param src the Source object * @return the ImageInputStream (or null if there's not ImageInputStream available) */ public static ImageInputStream getImageInputStream(Source src) { if (src instanceof ImageSource) { return ((ImageSource) src).getImageInputStream(); } else { return null; } }
/** * Indicates whether a Source is reusable. A Source object is reusable if it's an * {@link ImageSource} (containing an {@link ImageInputStream}) or a {@link DOMSource}. * @param src the Source object * @return true if the Source is reusable */ protected boolean isReusable(Source src) { if (noSourceReuse) { return false; } if (src instanceof ImageSource) { ImageSource is = (ImageSource) src; if (is.getImageInputStream() != null) { return true; } } if (src instanceof DOMSource) { return true; } return false; }
/** * Returns the ImageInputStream of a Source object. This method throws an * IllegalArgumentException if there's no ImageInputStream instance available from the * Source object. * @param src the Source object * @return the ImageInputStream */ public static ImageInputStream needImageInputStream(Source src) { if (src instanceof ImageSource) { ImageSource isrc = (ImageSource) src; if (isrc.getImageInputStream() == null) { throw new IllegalArgumentException( "ImageInputStream is null/cleared on ImageSource"); } return isrc.getImageInputStream(); } else { throw new IllegalArgumentException("Source must be an ImageSource"); } }
/** * Returns the {@link InputStream} that is backing the given {@link Source} object. * * @param src is backed by an {@link InputStream} * @return the input stream */ public static InputStream getInputStream(Source src) { try { if (src instanceof StreamSource) { return ((StreamSource) src).getInputStream(); } else if (src instanceof DOMSource) { ByteArrayOutputStream outStream = new ByteArrayOutputStream(); StreamResult xmlSource = new StreamResult(outStream); TransformerFactory.newInstance().newTransformer().transform(src, xmlSource); return new ByteArrayInputStream(outStream.toByteArray()); } else if (src instanceof SAXSource) { return ((SAXSource) src).getInputSource().getByteStream(); } else if (src instanceof ImageSource) { return new ImageInputStreamAdapter(((ImageSource) src).getImageInputStream()); } } catch (Exception e) { // TODO: How do we want to handle these? They all come from the TransformerFactory } return null; }
/** {@inheritDoc} */ public String toString() { return (isFastSource() ? "FAST " : "") + "ImageSource: " + getSystemId() + " " + getImageInputStream(); }