You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@xmlgraphics.apache.org by me...@apache.org on 2012/09/19 15:27:17 UTC
svn commit: r1387585 - in /xmlgraphics/commons/branches/Temp_URI_Resolution:
src/java/org/apache/xmlgraphics/image/loader/
src/java/org/apache/xmlgraphics/image/loader/impl/
src/java/org/apache/xmlgraphics/image/loader/impl/imageio/
src/java/org/apache...
Author: mehdi
Date: Wed Sep 19 13:27:16 2012
New Revision: 1387585
URL: http://svn.apache.org/viewvc?rev=1387585&view=rev
Log:
Extracted javax...Source handling utility into its own class, created a fallback resolver to be used as the default mechanism and a restricted resolver.
Added:
xmlgraphics/commons/branches/Temp_URI_Resolution/src/java/org/apache/xmlgraphics/io/XmlSourceUtil.java (with props)
xmlgraphics/commons/branches/Temp_URI_Resolution/test/java/org/apache/xmlgraphics/io/XmlSourceUtilTestCase.java (with props)
Modified:
xmlgraphics/commons/branches/Temp_URI_Resolution/src/java/org/apache/xmlgraphics/image/loader/ImageManager.java
xmlgraphics/commons/branches/Temp_URI_Resolution/src/java/org/apache/xmlgraphics/image/loader/impl/AbstractImageSessionContext.java
xmlgraphics/commons/branches/Temp_URI_Resolution/src/java/org/apache/xmlgraphics/image/loader/impl/ImageLoaderEPS.java
xmlgraphics/commons/branches/Temp_URI_Resolution/src/java/org/apache/xmlgraphics/image/loader/impl/ImageLoaderRaw.java
xmlgraphics/commons/branches/Temp_URI_Resolution/src/java/org/apache/xmlgraphics/image/loader/impl/ImageLoaderRawCCITTFax.java
xmlgraphics/commons/branches/Temp_URI_Resolution/src/java/org/apache/xmlgraphics/image/loader/impl/ImageLoaderRawJPEG.java
xmlgraphics/commons/branches/Temp_URI_Resolution/src/java/org/apache/xmlgraphics/image/loader/impl/imageio/ImageLoaderImageIO.java
xmlgraphics/commons/branches/Temp_URI_Resolution/src/java/org/apache/xmlgraphics/image/loader/util/ImageUtil.java
xmlgraphics/commons/branches/Temp_URI_Resolution/src/java/org/apache/xmlgraphics/io/URIResolverAdapter.java
xmlgraphics/commons/branches/Temp_URI_Resolution/test/java/org/apache/xmlgraphics/image/loader/ImageSessionContextTestCase.java
xmlgraphics/commons/branches/Temp_URI_Resolution/test/java/org/apache/xmlgraphics/io/URIResolverAdapterTestCase.java
Modified: xmlgraphics/commons/branches/Temp_URI_Resolution/src/java/org/apache/xmlgraphics/image/loader/ImageManager.java
URL: http://svn.apache.org/viewvc/xmlgraphics/commons/branches/Temp_URI_Resolution/src/java/org/apache/xmlgraphics/image/loader/ImageManager.java?rev=1387585&r1=1387584&r2=1387585&view=diff
==============================================================================
--- xmlgraphics/commons/branches/Temp_URI_Resolution/src/java/org/apache/xmlgraphics/image/loader/ImageManager.java (original)
+++ xmlgraphics/commons/branches/Temp_URI_Resolution/src/java/org/apache/xmlgraphics/image/loader/ImageManager.java Wed Sep 19 13:27:16 2012
@@ -35,6 +35,7 @@ import org.apache.xmlgraphics.image.load
import org.apache.xmlgraphics.image.loader.spi.ImagePreloader;
import org.apache.xmlgraphics.image.loader.util.ImageUtil;
import org.apache.xmlgraphics.image.loader.util.Penalty;
+import org.apache.xmlgraphics.io.XmlSourceUtil;
/**
* ImageManager is the central starting point for image access.
@@ -231,7 +232,7 @@ public class ImageManager {
"Cannot load image (no suitable loader/converter combination available) for "
+ info);
}
- ImageUtil.closeQuietly(session.getSource(info.getOriginalURI()));
+ XmlSourceUtil.closeQuietly(session.getSource(info.getOriginalURI()));
return img;
}
@@ -272,7 +273,7 @@ public class ImageManager {
"Cannot load image (no suitable loader/converter combination available) for "
+ info);
}
- ImageUtil.closeQuietly(session.getSource(info.getOriginalURI()));
+ XmlSourceUtil.closeQuietly(session.getSource(info.getOriginalURI()));
return img;
}
Modified: xmlgraphics/commons/branches/Temp_URI_Resolution/src/java/org/apache/xmlgraphics/image/loader/impl/AbstractImageSessionContext.java
URL: http://svn.apache.org/viewvc/xmlgraphics/commons/branches/Temp_URI_Resolution/src/java/org/apache/xmlgraphics/image/loader/impl/AbstractImageSessionContext.java?rev=1387585&r1=1387584&r2=1387585&view=diff
==============================================================================
--- xmlgraphics/commons/branches/Temp_URI_Resolution/src/java/org/apache/xmlgraphics/image/loader/impl/AbstractImageSessionContext.java (original)
+++ xmlgraphics/commons/branches/Temp_URI_Resolution/src/java/org/apache/xmlgraphics/image/loader/impl/AbstractImageSessionContext.java Wed Sep 19 13:27:16 2012
@@ -44,7 +44,7 @@ import org.apache.xmlgraphics.image.load
import org.apache.xmlgraphics.image.loader.ImageSource;
import org.apache.xmlgraphics.image.loader.util.ImageUtil;
import org.apache.xmlgraphics.image.loader.util.SoftMapCache;
-import org.apache.xmlgraphics.io.URIResolverAdapter;
+import org.apache.xmlgraphics.io.XmlSourceUtil;
/**
* Abstract base class for classes implementing ImageSessionContext. This class provides all the
@@ -65,6 +65,20 @@ public abstract class AbstractImageSessi
noSourceReuse = Boolean.valueOf(noSourceReuseString);
}
+ private final FallbackResolver fallbackResolver;
+
+ public AbstractImageSessionContext() {
+ fallbackResolver = new UnrestrictedFallbackResolver();
+ }
+
+ /**
+ * @param fallbackResolver the fallback resolution mechanism to be used when simply getting an
+ * {@link InputStream} that backs a {@link Source} isn't possible.
+ */
+ public AbstractImageSessionContext(FallbackResolver fallbackResolver) {
+ this.fallbackResolver = fallbackResolver;
+ }
+
/**
* Attempts to resolve the given URI.
* @param uri URI to access
@@ -76,118 +90,14 @@ public abstract class AbstractImageSessi
/** {@inheritDoc} */
public Source newSource(String uri) {
Source source = resolveURI(uri);
- if (source == null) {
- if (log.isDebugEnabled()) {
- log.debug("URI could not be resolved: " + uri);
- }
- return null;
- }
- if (!(source instanceof StreamSource) && !(source instanceof SAXSource)) {
- //Return any non-stream Sources and let the ImageLoaders deal with them
- return source;
- }
-
- ImageSource imageSource = null;
-
- String resolvedURI = source.getSystemId();
- URL url;
- try {
- url = new URL(resolvedURI);
- } catch (MalformedURLException e) {
- url = null;
- }
- File f = /*FileUtils.*/toFile(url);
- if (f != null) {
- boolean directFileAccess = true;
- assert (source instanceof StreamSource) || (source instanceof SAXSource);
- InputStream in = URIResolverAdapter.getInputStream(source);
- if (in == null) {
- try {
- in = new java.io.FileInputStream(f);
- } catch (FileNotFoundException fnfe) {
- log.error("Error while opening file."
- + " Could not load image from system identifier '"
- + source.getSystemId() + "' (" + fnfe.getMessage() + ")");
- return null;
- }
- }
- if (in != null) {
- in = ImageUtil.decorateMarkSupported(in);
- try {
- if (ImageUtil.isGZIPCompressed(in)) {
- //GZIPped stream are not seekable, so buffer/cache like other URLs
- directFileAccess = false;
- }
- } catch (IOException ioe) {
- log.error("Error while checking the InputStream for GZIP compression."
- + " Could not load image from system identifier '"
- + source.getSystemId() + "' (" + ioe.getMessage() + ")");
- return null;
- }
- }
-
- if (directFileAccess) {
- //Close as the file is reopened in a more optimal way
- IOUtils.closeQuietly(in);
- try {
- // We let the OS' file system cache do the caching for us
- // --> lower Java memory consumption, probably no speed loss
- final ImageInputStream newInputStream = ImageIO
- .createImageInputStream(f);
- if (newInputStream == null) {
- log.error("Unable to create ImageInputStream for local file "
- + f
- + " from system identifier '"
- + source.getSystemId() + "'");
- return null;
- } else {
- imageSource = new ImageSource(newInputStream,
- resolvedURI, true);
- }
- } catch (IOException ioe) {
- log.error("Unable to create ImageInputStream for local file"
- + " from system identifier '"
- + source.getSystemId() + "' (" + ioe.getMessage() + ")");
- }
- }
+ if (source instanceof StreamSource || source instanceof SAXSource) {
+ return fallbackResolver.createSource(source, uri);
}
-
- if (imageSource == null) {
- if (ImageUtil.hasReader(source) && !ImageUtil.hasInputStream(source)) {
- //We don't handle Reader instances here so return the Source unchanged
- return source;
- }
- // Got a valid source, obtain an InputStream from it
- InputStream in = URIResolverAdapter.getInputStream(source);
- if (in == null && url != null) {
- try {
- in = url.openStream();
- } catch (Exception ex) {
- log.error("Unable to obtain stream from system identifier '"
- + source.getSystemId() + "'");
- }
- }
- if (in == null) {
- log.error("The Source that was returned from URI resolution didn't contain"
- + " an InputStream for URI: " + uri);
- return null;
- }
-
- try {
- //Buffer and uncompress if necessary
- in = ImageUtil.autoDecorateInputStream(in);
- imageSource = new ImageSource(
- createImageInputStream(in), source.getSystemId(), false);
- } catch (IOException ioe) {
- log.error("Unable to create ImageInputStream for InputStream"
- + " from system identifier '"
- + source.getSystemId() + "' (" + ioe.getMessage() + ")");
- }
- }
- return imageSource;
+ //Return any non-stream Sources and let the ImageLoaders deal with them
+ return source;
}
- protected ImageInputStream createImageInputStream(InputStream in) throws IOException {
+ protected static ImageInputStream createImageInputStream(InputStream in) throws IOException {
ImageInputStream iin = ImageIO.createImageInputStream(in);
return (ImageInputStream) Proxy.newProxyInstance(
ImageInputStream.class.getClassLoader(),
@@ -300,7 +210,7 @@ public abstract class AbstractImageSessi
}
} catch (IOException ioe) {
//Ignore exception
- ImageUtil.closeQuietly(src);
+ XmlSourceUtil.closeQuietly(src);
}
if (isReusable(src)) {
@@ -309,7 +219,7 @@ public abstract class AbstractImageSessi
sessionSources.put(uri, src);
} else {
//Otherwise, try to close if possible and forget about it
- ImageUtil.closeQuietly(src);
+ XmlSourceUtil.closeQuietly(src);
}
}
@@ -334,4 +244,163 @@ public abstract class AbstractImageSessi
}
return false;
}
+
+ /**
+ * Implementations of this interface will be used as the mechanism for creating the
+ * {@link Source} that wraps resources. This interface allows clients to define their own
+ * implementations so that they have fine-grained control over how resources are acquired.
+ */
+ public interface FallbackResolver {
+
+ /**
+ * The fallback mechanism used to create the source which takes in both the {@link Source}
+ * that the the regular mechanisms attempted to create and the URI that the user provided.
+ *
+ * @param source the source
+ * @param uri the URI provided by the user
+ * @return the source that the contingency mechanism has been acquired
+ */
+ Source createSource(Source source, String uri);
+ }
+
+ /**
+ * An unrestricted resolver that has various contingency mechanisms that access the file-system.
+ * This is most suitable for use via the CLI or in environments where controlling I/O isn't a
+ * priority.
+ */
+ public static final class UnrestrictedFallbackResolver implements FallbackResolver {
+
+ /** {@inheritDoc} */
+ public Source createSource(Source source, String uri) {
+ if (source == null) {
+ if (log.isDebugEnabled()) {
+ log.debug("URI could not be resolved: " + uri);
+ }
+ return null;
+ }
+ ImageSource imageSource = null;
+
+ String resolvedURI = source.getSystemId();
+ URL url;
+ try {
+ url = new URL(resolvedURI);
+ } catch (MalformedURLException e) {
+ url = null;
+ }
+ File f = /*FileUtils.*/toFile(url);
+ if (f != null) {
+ boolean directFileAccess = true;
+ assert (source instanceof StreamSource) || (source instanceof SAXSource);
+ InputStream in = XmlSourceUtil.getInputStream(source);
+ if (in == null) {
+ try {
+ in = new java.io.FileInputStream(f);
+ } catch (FileNotFoundException fnfe) {
+ log.error("Error while opening file."
+ + " Could not load image from system identifier '"
+ + source.getSystemId() + "' (" + fnfe.getMessage() + ")");
+ return null;
+ }
+ }
+ if (in != null) {
+ in = ImageUtil.decorateMarkSupported(in);
+ try {
+ if (ImageUtil.isGZIPCompressed(in)) {
+ //GZIPped stream are not seekable, so buffer/cache like other URLs
+ directFileAccess = false;
+ }
+ } catch (IOException ioe) {
+ log.error("Error while checking the InputStream for GZIP compression."
+ + " Could not load image from system identifier '"
+ + source.getSystemId() + "' (" + ioe.getMessage() + ")");
+ return null;
+ }
+ }
+
+ if (directFileAccess) {
+ //Close as the file is reopened in a more optimal way
+ IOUtils.closeQuietly(in);
+ try {
+ // We let the OS' file system cache do the caching for us
+ // --> lower Java memory consumption, probably no speed loss
+ final ImageInputStream newInputStream = ImageIO
+ .createImageInputStream(f);
+ if (newInputStream == null) {
+ log.error("Unable to create ImageInputStream for local file "
+ + f
+ + " from system identifier '"
+ + source.getSystemId() + "'");
+ return null;
+ } else {
+ imageSource = new ImageSource(newInputStream,
+ resolvedURI, true);
+ }
+ } catch (IOException ioe) {
+ log.error("Unable to create ImageInputStream for local file"
+ + " from system identifier '"
+ + source.getSystemId() + "' (" + ioe.getMessage() + ")");
+ }
+ }
+ }
+
+ if (imageSource == null) {
+ if (XmlSourceUtil.hasReader(source) && !ImageUtil.hasInputStream(source)) {
+ //We don't handle Reader instances here so return the Source unchanged
+ return source;
+ }
+ // Got a valid source, obtain an InputStream from it
+ InputStream in = XmlSourceUtil.getInputStream(source);
+ if (in == null && url != null) {
+ try {
+ in = url.openStream();
+ } catch (Exception ex) {
+ log.error("Unable to obtain stream from system identifier '"
+ + source.getSystemId() + "'");
+ }
+ }
+ if (in == null) {
+ log.error("The Source that was returned from URI resolution didn't contain"
+ + " an InputStream for URI: " + uri);
+ return null;
+ }
+ return createImageSource(in, source);
+ }
+ return imageSource;
+ }
+ }
+
+ private static ImageSource createImageSource(InputStream in, Source source) {
+ try {
+ //Buffer and uncompress if necessary
+ return new ImageSource(createImageInputStream(ImageUtil.autoDecorateInputStream(in)),
+ source.getSystemId(), false);
+ } catch (IOException ioe) {
+ log.error("Unable to create ImageInputStream for InputStream"
+ + " from system identifier '"
+ + source.getSystemId() + "' (" + ioe.getMessage() + ")");
+ }
+ return null;
+ }
+
+ /**
+ * This fallback resolver is to be used in environments where controlling file access is of
+ * critical importance. It disallows any contingency mechanisms by which a {@link Source} object
+ * would be created.
+ */
+ public static final class RestrictedFallbackResolver implements FallbackResolver {
+
+ /** {@inheritDoc} */
+ public Source createSource(Source source, String uri) {
+ if (source == null) {
+ if (log.isDebugEnabled()) {
+ log.debug("URI could not be resolved: " + uri);
+ }
+ return null;
+ }
+ if (ImageUtil.hasInputStream(source)) {
+ return createImageSource(XmlSourceUtil.getInputStream(source), source);
+ }
+ throw new UnsupportedOperationException("There are no contingency mechanisms for I/O.");
+ }
+ }
}
Modified: xmlgraphics/commons/branches/Temp_URI_Resolution/src/java/org/apache/xmlgraphics/image/loader/impl/ImageLoaderEPS.java
URL: http://svn.apache.org/viewvc/xmlgraphics/commons/branches/Temp_URI_Resolution/src/java/org/apache/xmlgraphics/image/loader/impl/ImageLoaderEPS.java?rev=1387585&r1=1387584&r2=1387585&view=diff
==============================================================================
--- xmlgraphics/commons/branches/Temp_URI_Resolution/src/java/org/apache/xmlgraphics/image/loader/impl/ImageLoaderEPS.java (original)
+++ xmlgraphics/commons/branches/Temp_URI_Resolution/src/java/org/apache/xmlgraphics/image/loader/impl/ImageLoaderEPS.java Wed Sep 19 13:27:16 2012
@@ -30,7 +30,7 @@ import org.apache.xmlgraphics.image.load
import org.apache.xmlgraphics.image.loader.ImageFlavor;
import org.apache.xmlgraphics.image.loader.ImageInfo;
import org.apache.xmlgraphics.image.loader.ImageSessionContext;
-import org.apache.xmlgraphics.image.loader.util.ImageUtil;
+import org.apache.xmlgraphics.io.XmlSourceUtil;
import org.apache.xmlgraphics.util.MimeConstants;
import org.apache.xmlgraphics.util.io.SubInputStream;
@@ -58,8 +58,8 @@ public class ImageLoaderEPS extends Abst
"ImageInfo must be from a image with MIME type: " + MimeConstants.MIME_EPS);
}
Source src = session.needSource(info.getOriginalURI());
- InputStream in = ImageUtil.needInputStream(src);
- ImageUtil.removeStreams(src); //so others cannot close them, we take them over
+ InputStream in = XmlSourceUtil.needInputStream(src);
+ XmlSourceUtil.removeStreams(src); //so others cannot close them, we take them over
PreloaderEPS.EPSBinaryFileHeader binaryHeader;
binaryHeader = (PreloaderEPS.EPSBinaryFileHeader)info.getCustomObjects().get(
Modified: xmlgraphics/commons/branches/Temp_URI_Resolution/src/java/org/apache/xmlgraphics/image/loader/impl/ImageLoaderRaw.java
URL: http://svn.apache.org/viewvc/xmlgraphics/commons/branches/Temp_URI_Resolution/src/java/org/apache/xmlgraphics/image/loader/impl/ImageLoaderRaw.java?rev=1387585&r1=1387584&r2=1387585&view=diff
==============================================================================
--- xmlgraphics/commons/branches/Temp_URI_Resolution/src/java/org/apache/xmlgraphics/image/loader/impl/ImageLoaderRaw.java (original)
+++ xmlgraphics/commons/branches/Temp_URI_Resolution/src/java/org/apache/xmlgraphics/image/loader/impl/ImageLoaderRaw.java Wed Sep 19 13:27:16 2012
@@ -29,7 +29,7 @@ import org.apache.xmlgraphics.image.load
import org.apache.xmlgraphics.image.loader.ImageFlavor;
import org.apache.xmlgraphics.image.loader.ImageInfo;
import org.apache.xmlgraphics.image.loader.ImageSessionContext;
-import org.apache.xmlgraphics.image.loader.util.ImageUtil;
+import org.apache.xmlgraphics.io.XmlSourceUtil;
/**
* ImageLoader for formats consumed "raw" (undecoded). Provides a raw/undecoded stream.
@@ -62,7 +62,7 @@ public class ImageLoaderRaw extends Abst
}
Source src = session.needSource(info.getOriginalURI());
ImageRawStream rawImage = new ImageRawStream(info, getTargetFlavor(),
- ImageUtil.needInputStream(src));
+ XmlSourceUtil.needInputStream(src));
return rawImage;
}
Modified: xmlgraphics/commons/branches/Temp_URI_Resolution/src/java/org/apache/xmlgraphics/image/loader/impl/ImageLoaderRawCCITTFax.java
URL: http://svn.apache.org/viewvc/xmlgraphics/commons/branches/Temp_URI_Resolution/src/java/org/apache/xmlgraphics/image/loader/impl/ImageLoaderRawCCITTFax.java?rev=1387585&r1=1387584&r2=1387585&view=diff
==============================================================================
--- xmlgraphics/commons/branches/Temp_URI_Resolution/src/java/org/apache/xmlgraphics/image/loader/impl/ImageLoaderRawCCITTFax.java (original)
+++ xmlgraphics/commons/branches/Temp_URI_Resolution/src/java/org/apache/xmlgraphics/image/loader/impl/ImageLoaderRawCCITTFax.java Wed Sep 19 13:27:16 2012
@@ -42,6 +42,7 @@ import org.apache.xmlgraphics.image.load
import org.apache.xmlgraphics.image.loader.ImageSessionContext;
import org.apache.xmlgraphics.image.loader.util.ImageUtil;
import org.apache.xmlgraphics.image.loader.util.SeekableStreamAdapter;
+import org.apache.xmlgraphics.io.XmlSourceUtil;
import org.apache.xmlgraphics.util.MimeConstants;
import org.apache.xmlgraphics.util.io.SubInputStream;
@@ -139,14 +140,14 @@ public class ImageLoaderRawCCITTFax exte
}
in.seek(stripOffset);
- InputStream subin = new SubInputStream(ImageUtil.needInputStream(src), stripLength, true);
+ InputStream subin = new SubInputStream(XmlSourceUtil.needInputStream(src), stripLength, true);
if (fillOrder == 2) {
//Decorate to flip bit order
subin = new FillOrderChangeInputStream(subin);
}
ImageRawCCITTFax rawImage = new ImageRawCCITTFax(info, subin, compression);
//Strip stream from source as we pass it on internally
- ImageUtil.removeStreams(src);
+ XmlSourceUtil.removeStreams(src);
return rawImage;
}
Modified: xmlgraphics/commons/branches/Temp_URI_Resolution/src/java/org/apache/xmlgraphics/image/loader/impl/ImageLoaderRawJPEG.java
URL: http://svn.apache.org/viewvc/xmlgraphics/commons/branches/Temp_URI_Resolution/src/java/org/apache/xmlgraphics/image/loader/impl/ImageLoaderRawJPEG.java?rev=1387585&r1=1387584&r2=1387585&view=diff
==============================================================================
--- xmlgraphics/commons/branches/Temp_URI_Resolution/src/java/org/apache/xmlgraphics/image/loader/impl/ImageLoaderRawJPEG.java (original)
+++ xmlgraphics/commons/branches/Temp_URI_Resolution/src/java/org/apache/xmlgraphics/image/loader/impl/ImageLoaderRawJPEG.java Wed Sep 19 13:27:16 2012
@@ -39,6 +39,7 @@ import org.apache.xmlgraphics.image.load
import org.apache.xmlgraphics.image.loader.ImageInfo;
import org.apache.xmlgraphics.image.loader.ImageSessionContext;
import org.apache.xmlgraphics.image.loader.util.ImageUtil;
+import org.apache.xmlgraphics.io.XmlSourceUtil;
import org.apache.xmlgraphics.java2d.color.ColorSpaces;
import org.apache.xmlgraphics.java2d.color.profile.ColorProfileUtil;
import org.apache.xmlgraphics.util.MimeConstants;
@@ -211,7 +212,7 @@ public class ImageLoaderRawJPEG extends
}
ImageRawJPEG rawImage = new ImageRawJPEG(info,
- ImageUtil.needInputStream(src),
+ XmlSourceUtil.needInputStream(src),
sofType, colorSpace, iccProfile, invertImage);
return rawImage;
}
Modified: xmlgraphics/commons/branches/Temp_URI_Resolution/src/java/org/apache/xmlgraphics/image/loader/impl/imageio/ImageLoaderImageIO.java
URL: http://svn.apache.org/viewvc/xmlgraphics/commons/branches/Temp_URI_Resolution/src/java/org/apache/xmlgraphics/image/loader/impl/imageio/ImageLoaderImageIO.java?rev=1387585&r1=1387584&r2=1387585&view=diff
==============================================================================
--- xmlgraphics/commons/branches/Temp_URI_Resolution/src/java/org/apache/xmlgraphics/image/loader/impl/imageio/ImageLoaderImageIO.java (original)
+++ xmlgraphics/commons/branches/Temp_URI_Resolution/src/java/org/apache/xmlgraphics/image/loader/impl/imageio/ImageLoaderImageIO.java Wed Sep 19 13:27:16 2012
@@ -65,6 +65,7 @@ import org.apache.xmlgraphics.image.load
import org.apache.xmlgraphics.image.loader.impl.ImageBuffered;
import org.apache.xmlgraphics.image.loader.impl.ImageRendered;
import org.apache.xmlgraphics.image.loader.util.ImageUtil;
+import org.apache.xmlgraphics.io.XmlSourceUtil;
import org.apache.xmlgraphics.java2d.color.profile.ColorProfileUtil;
/**
@@ -167,7 +168,7 @@ public class ImageLoaderImageIO extends
}
}
} finally {
- ImageUtil.closeQuietly(src);
+ XmlSourceUtil.closeQuietly(src);
//TODO Some codecs may do late reading.
}
if (firstException != null) {
Modified: xmlgraphics/commons/branches/Temp_URI_Resolution/src/java/org/apache/xmlgraphics/image/loader/util/ImageUtil.java
URL: http://svn.apache.org/viewvc/xmlgraphics/commons/branches/Temp_URI_Resolution/src/java/org/apache/xmlgraphics/image/loader/util/ImageUtil.java?rev=1387585&r1=1387584&r2=1387585&view=diff
==============================================================================
--- xmlgraphics/commons/branches/Temp_URI_Resolution/src/java/org/apache/xmlgraphics/image/loader/util/ImageUtil.java (original)
+++ xmlgraphics/commons/branches/Temp_URI_Resolution/src/java/org/apache/xmlgraphics/image/loader/util/ImageUtil.java Wed Sep 19 13:27:16 2012
@@ -21,7 +21,6 @@ package org.apache.xmlgraphics.image.loa
import java.io.IOException;
import java.io.InputStream;
-import java.io.Reader;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
@@ -33,17 +32,11 @@ import java.util.zip.GZIPInputStream;
import javax.imageio.stream.ImageInputStream;
import javax.xml.transform.Source;
-import javax.xml.transform.sax.SAXSource;
-import javax.xml.transform.stream.StreamSource;
-
-import org.xml.sax.InputSource;
-
-import org.apache.commons.io.IOUtils;
import org.apache.xmlgraphics.image.loader.ImageProcessingHints;
import org.apache.xmlgraphics.image.loader.ImageSessionContext;
import org.apache.xmlgraphics.image.loader.ImageSource;
-import org.apache.xmlgraphics.io.URIResolverAdapter;
+import org.apache.xmlgraphics.io.XmlSourceUtil;
/**
* Helper and convenience methods for working with the image package.
@@ -71,15 +64,10 @@ public final class ImageUtil {
* if there's no InputStream instance available from the Source object.
* @param src the Source object
* @return the InputStream
+ * @deprecated use {@link XmlSourceUtil#needInputStream(Source)} instead
*/
public static InputStream needInputStream(Source src) {
- InputStream in = URIResolverAdapter.getInputStream(src);
- if (in != null) {
- return in;
- } else {
- throw new IllegalArgumentException("Source must be a StreamSource with an InputStream"
- + " or an ImageSource");
- }
+ return XmlSourceUtil.needInputStream(src);
}
/**
@@ -108,29 +96,17 @@ public final class ImageUtil {
* @return true if an InputStream is available
*/
public static boolean hasInputStream(Source src) {
- InputStream stream = URIResolverAdapter.getInputStream(src);
- if (stream != null) {
- return true;
- }
- return hasImageInputStream(src);
+ return XmlSourceUtil.hasInputStream(src) || hasImageInputStream(src);
}
/**
* Indicates whether the Source object has a Reader instance.
* @param src the Source object
* @return true if an Reader is available
+ * @deprecated use {@link XmlSourceUtil#hasReader(Source)} instead
*/
public static boolean hasReader(Source src) {
- if (src instanceof StreamSource) {
- Reader reader = ((StreamSource) src).getReader();
- return (reader != null);
- } else if (src instanceof SAXSource) {
- InputSource is = ((SAXSource) src).getInputSource();
- if (is != null) {
- return (is.getCharacterStream() != null);
- }
- }
- return false;
+ return XmlSourceUtil.hasReader(src);
}
/**
@@ -146,56 +122,20 @@ public final class ImageUtil {
* Removes any references to InputStreams or Readers from the given Source to prohibit
* accidental/unwanted use by a component further downstream.
* @param src the Source object
+ * @deprecated use {@link XmlSourceUtil#removeStreams(Source)} instead
*/
public static void removeStreams(Source src) {
- if (src instanceof ImageSource) {
- ImageSource isrc = (ImageSource) src;
- isrc.setImageInputStream(null);
- } else if (src instanceof StreamSource) {
- StreamSource ssrc = (StreamSource) src;
- ssrc.setInputStream(null);
- ssrc.setReader(null);
- } else if (src instanceof SAXSource) {
- InputSource is = ((SAXSource) src).getInputSource();
- if (is != null) {
- is.setByteStream(null);
- is.setCharacterStream(null);
- }
- }
+ XmlSourceUtil.removeStreams(src);
}
/**
* Closes the InputStreams or ImageInputStreams of Source objects. Any exception occurring
* while closing the stream is ignored.
* @param src the Source object
+ * @deprecated use {@link XmlSourceUtil#closeQuietly(Source)} instead
*/
public static void closeQuietly(Source src) {
- if (src == null) {
- return;
- }
- if (src instanceof StreamSource) {
- StreamSource streamSource = (StreamSource) src;
- streamSource.setInputStream(null);
- IOUtils.closeQuietly(streamSource.getReader());
- streamSource.setReader(null);
- } else if (src instanceof ImageSource) {
- if (getImageInputStream(src) != null) {
- try {
- getImageInputStream(src).close();
- } catch (IOException ioe) {
- // ignore
- }
- ((ImageSource) src).setImageInputStream(null);
- }
- } else if (src instanceof SAXSource) {
- InputSource is = ((SAXSource) src).getInputSource();
- if (is != null) {
- IOUtils.closeQuietly(is.getByteStream());
- is.setByteStream(null);
- IOUtils.closeQuietly(is.getCharacterStream());
- is.setCharacterStream(null);
- }
- }
+ XmlSourceUtil.closeQuietly(src);
}
/**
Modified: xmlgraphics/commons/branches/Temp_URI_Resolution/src/java/org/apache/xmlgraphics/io/URIResolverAdapter.java
URL: http://svn.apache.org/viewvc/xmlgraphics/commons/branches/Temp_URI_Resolution/src/java/org/apache/xmlgraphics/io/URIResolverAdapter.java?rev=1387585&r1=1387584&r2=1387585&view=diff
==============================================================================
--- xmlgraphics/commons/branches/Temp_URI_Resolution/src/java/org/apache/xmlgraphics/io/URIResolverAdapter.java (original)
+++ xmlgraphics/commons/branches/Temp_URI_Resolution/src/java/org/apache/xmlgraphics/io/URIResolverAdapter.java Wed Sep 19 13:27:16 2012
@@ -17,8 +17,6 @@
package org.apache.xmlgraphics.io;
-import java.io.ByteArrayInputStream;
-import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
@@ -27,15 +25,7 @@ import java.net.URL;
import javax.xml.transform.Source;
import javax.xml.transform.TransformerException;
-import javax.xml.transform.TransformerFactory;
import javax.xml.transform.URIResolver;
-import javax.xml.transform.dom.DOMSource;
-import javax.xml.transform.sax.SAXSource;
-import javax.xml.transform.stream.StreamResult;
-import javax.xml.transform.stream.StreamSource;
-
-import org.apache.xmlgraphics.image.loader.ImageSource;
-import org.apache.xmlgraphics.image.loader.util.ImageInputStreamAdapter;
/**
* An adapter between {@link URIResolver} to {@link ResourceResolver}. This adapter allows users
@@ -45,23 +35,18 @@ public class URIResolverAdapter implemen
private final URIResolver resolver;
- private final ResourceResolver outputStreamResolver;
-
/**
* @param resolver the desired {@link URIResolver}
- * @param outputStreamResolver a resource resolver used only for creating {@link OutputStream}s
*/
- public URIResolverAdapter(URIResolver resolver, ResourceResolver outputStreamResolver) {
+ public URIResolverAdapter(URIResolver resolver) {
this.resolver = resolver;
- this.outputStreamResolver = outputStreamResolver;
}
/** {@inheritDoc} */
public Resource getResource(URI uri) throws IOException {
try {
Source src = resolver.resolve(uri.toASCIIString(), null);
- InputStream resourceStream = null;
-
+ InputStream resourceStream = XmlSourceUtil.getInputStream(src);
if (resourceStream == null) {
URL url = new URL(src.getSystemId());
@@ -75,32 +60,11 @@ public class URIResolverAdapter implemen
/** {@inheritDoc} */
public OutputStream getOutputStream(URI uri) throws IOException {
- return outputStreamResolver.getOutputStream(uri);
- }
-
- /**
- * 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
+ Source src = resolver.resolve(uri.toASCIIString(), null);
+ return new URL(src.getSystemId()).openConnection().getOutputStream();
+ } catch (TransformerException te) {
+ throw new IOException(te.getMessage());
}
- return null;
}
}
Added: xmlgraphics/commons/branches/Temp_URI_Resolution/src/java/org/apache/xmlgraphics/io/XmlSourceUtil.java
URL: http://svn.apache.org/viewvc/xmlgraphics/commons/branches/Temp_URI_Resolution/src/java/org/apache/xmlgraphics/io/XmlSourceUtil.java?rev=1387585&view=auto
==============================================================================
--- xmlgraphics/commons/branches/Temp_URI_Resolution/src/java/org/apache/xmlgraphics/io/XmlSourceUtil.java (added)
+++ xmlgraphics/commons/branches/Temp_URI_Resolution/src/java/org/apache/xmlgraphics/io/XmlSourceUtil.java Wed Sep 19 13:27:16 2012
@@ -0,0 +1,167 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.xmlgraphics.io;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.Reader;
+
+import javax.xml.transform.Source;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.sax.SAXSource;
+import javax.xml.transform.stream.StreamResult;
+import javax.xml.transform.stream.StreamSource;
+
+import org.xml.sax.InputSource;
+
+import org.apache.commons.io.IOUtils;
+
+import org.apache.xmlgraphics.image.loader.ImageSource;
+import org.apache.xmlgraphics.image.loader.util.ImageInputStreamAdapter;
+import org.apache.xmlgraphics.image.loader.util.ImageUtil;
+
+/**
+ * A utility class for handling {@link Source} objects, more specficially the streams that back
+ * the {@link Source}.
+ */
+public final class XmlSourceUtil {
+
+ private XmlSourceUtil() {
+ }
+
+ /**
+ * 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;
+ }
+
+ /**
+ * Returns the InputStream of a Source object. This method throws an IllegalArgumentException
+ * if there's no InputStream instance available from the Source object.
+ * @param src the Source object
+ * @return the InputStream
+ */
+ public static InputStream needInputStream(Source src) {
+ InputStream in = getInputStream(src);
+ if (in != null) {
+ return in;
+ } else {
+ throw new IllegalArgumentException("Source must be a StreamSource with an InputStream"
+ + " or an ImageSource");
+ }
+ }
+
+ /**
+ * Indicates whether the Source object has a Reader instance.
+ * @param src the Source object
+ * @return true if an Reader is available
+ */
+ public static boolean hasReader(Source src) {
+ if (src instanceof StreamSource) {
+ Reader reader = ((StreamSource) src).getReader();
+ return (reader != null);
+ } else if (src instanceof SAXSource) {
+ InputSource is = ((SAXSource) src).getInputSource();
+ if (is != null) {
+ return (is.getCharacterStream() != null);
+ }
+ }
+ return false;
+ }
+
+ /**
+ * Removes any references to InputStreams or Readers from the given Source to prohibit
+ * accidental/unwanted use by a component further downstream.
+ * @param src the Source object
+ */
+ public static void removeStreams(Source src) {
+ if (src instanceof ImageSource) {
+ ImageSource isrc = (ImageSource) src;
+ isrc.setImageInputStream(null);
+ } else if (src instanceof StreamSource) {
+ StreamSource ssrc = (StreamSource) src;
+ ssrc.setInputStream(null);
+ ssrc.setReader(null);
+ } else if (src instanceof SAXSource) {
+ InputSource is = ((SAXSource) src).getInputSource();
+ if (is != null) {
+ is.setByteStream(null);
+ is.setCharacterStream(null);
+ }
+ }
+ }
+
+ /**
+ * Closes the InputStreams or ImageInputStreams of Source objects. Any exception occurring
+ * while closing the stream is ignored.
+ * @param src the Source object
+ */
+ public static void closeQuietly(Source src) {
+ if (src instanceof StreamSource) {
+ StreamSource streamSource = (StreamSource) src;
+ IOUtils.closeQuietly(streamSource.getReader());
+ } else if (src instanceof ImageSource) {
+ if (ImageUtil.getImageInputStream(src) != null) {
+ try {
+ ImageUtil.getImageInputStream(src).close();
+ } catch (IOException ioe) {
+ // ignore
+ }
+ }
+ } else if (src instanceof SAXSource) {
+ InputSource is = ((SAXSource) src).getInputSource();
+ if (is != null) {
+ IOUtils.closeQuietly(is.getByteStream());
+ IOUtils.closeQuietly(is.getCharacterStream());
+ }
+ }
+ removeStreams(src);
+ }
+
+ /**
+ * Indicates whether the Source object has an InputStream instance.
+ * @param src the Source object
+ * @return true if an InputStream is available
+ */
+ public static boolean hasInputStream(Source src) {
+ return getInputStream(src) != null;
+ }
+}
Propchange: xmlgraphics/commons/branches/Temp_URI_Resolution/src/java/org/apache/xmlgraphics/io/XmlSourceUtil.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: xmlgraphics/commons/branches/Temp_URI_Resolution/test/java/org/apache/xmlgraphics/image/loader/ImageSessionContextTestCase.java
URL: http://svn.apache.org/viewvc/xmlgraphics/commons/branches/Temp_URI_Resolution/test/java/org/apache/xmlgraphics/image/loader/ImageSessionContextTestCase.java?rev=1387585&r1=1387584&r2=1387585&view=diff
==============================================================================
--- xmlgraphics/commons/branches/Temp_URI_Resolution/test/java/org/apache/xmlgraphics/image/loader/ImageSessionContextTestCase.java (original)
+++ xmlgraphics/commons/branches/Temp_URI_Resolution/test/java/org/apache/xmlgraphics/image/loader/ImageSessionContextTestCase.java Wed Sep 19 13:27:16 2012
@@ -43,6 +43,7 @@ import static org.junit.Assert.assertTru
import static org.junit.Assert.fail;
import org.apache.xmlgraphics.image.loader.util.ImageUtil;
+import org.apache.xmlgraphics.io.XmlSourceUtil;
/**
* Tests for AbstractImageSessionContext.
@@ -154,7 +155,7 @@ public class ImageSessionContextTestCase
Source src = resolve(uri, resolver);
assertTrue(src instanceof StreamSource); //Source remains unchanged
- assertTrue(ImageUtil.hasReader(src));
+ assertTrue(XmlSourceUtil.hasReader(src));
}
private ImageSource checkImageInputStreamAvailable(String uri, URIResolver resolver) {
@@ -234,7 +235,7 @@ public class ImageSessionContextTestCase
Source src = resolve(uri, resolver);
assertTrue(src instanceof SAXSource); //Source remains unchanged
- assertTrue(ImageUtil.hasReader(src));
+ assertTrue(XmlSourceUtil.hasReader(src));
}
private static final String SOME_XML = "<root><child id='1'>Hello World!</child></root>";
Modified: xmlgraphics/commons/branches/Temp_URI_Resolution/test/java/org/apache/xmlgraphics/io/URIResolverAdapterTestCase.java
URL: http://svn.apache.org/viewvc/xmlgraphics/commons/branches/Temp_URI_Resolution/test/java/org/apache/xmlgraphics/io/URIResolverAdapterTestCase.java?rev=1387585&r1=1387584&r2=1387585&view=diff
==============================================================================
--- xmlgraphics/commons/branches/Temp_URI_Resolution/test/java/org/apache/xmlgraphics/io/URIResolverAdapterTestCase.java (original)
+++ xmlgraphics/commons/branches/Temp_URI_Resolution/test/java/org/apache/xmlgraphics/io/URIResolverAdapterTestCase.java Wed Sep 19 13:27:16 2012
@@ -53,7 +53,6 @@ public class URIResolverAdapterTestCase
CatalogResolver catalogResolver = new CatalogResolver();
Source src = catalogResolver.resolve(textFileURI.toASCIIString(), null);
if (src instanceof SAXSource) {
- System.out.println(src.getSystemId());
testInputStream(new URL(src.getSystemId()).openStream());
}
}
@@ -61,7 +60,7 @@ public class URIResolverAdapterTestCase
@Test
@Ignore("Literally no idea why this doesn't work... Gonna look at the catalog resolver source")
public void testCatalogResolverInAdapter() throws IOException {
- ResourceResolver resourceResolver = new URIResolverAdapter(new CatalogResolver(), null);
+ ResourceResolver resourceResolver = new URIResolverAdapter(new CatalogResolver());
testInputStream(resourceResolver.getResource(textFileURI));
}
Added: xmlgraphics/commons/branches/Temp_URI_Resolution/test/java/org/apache/xmlgraphics/io/XmlSourceUtilTestCase.java
URL: http://svn.apache.org/viewvc/xmlgraphics/commons/branches/Temp_URI_Resolution/test/java/org/apache/xmlgraphics/io/XmlSourceUtilTestCase.java?rev=1387585&view=auto
==============================================================================
--- xmlgraphics/commons/branches/Temp_URI_Resolution/test/java/org/apache/xmlgraphics/io/XmlSourceUtilTestCase.java (added)
+++ xmlgraphics/commons/branches/Temp_URI_Resolution/test/java/org/apache/xmlgraphics/io/XmlSourceUtilTestCase.java Wed Sep 19 13:27:16 2012
@@ -0,0 +1,237 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.xmlgraphics.io;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.Reader;
+import java.io.StringWriter;
+
+import javax.imageio.stream.ImageInputStream;
+import javax.xml.parsers.DocumentBuilder;
+import javax.xml.parsers.DocumentBuilderFactory;
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.transform.Source;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.sax.SAXSource;
+import javax.xml.transform.stream.StreamSource;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.xml.sax.InputSource;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import org.apache.commons.io.IOUtils;
+
+import org.apache.xmlgraphics.image.loader.ImageSource;
+
+import static org.apache.xmlgraphics.io.XmlSourceUtil.closeQuietly;
+import static org.apache.xmlgraphics.io.XmlSourceUtil.getInputStream;
+import static org.apache.xmlgraphics.io.XmlSourceUtil.hasInputStream;
+import static org.apache.xmlgraphics.io.XmlSourceUtil.hasReader;
+import static org.apache.xmlgraphics.io.XmlSourceUtil.needInputStream;
+import static org.apache.xmlgraphics.io.XmlSourceUtil.removeStreams;
+
+public class XmlSourceUtilTestCase {
+
+ private StreamSource streamSource;
+ private SAXSource saxSource;
+ private InputSource inputSource;
+ private ImageSource imgSource;
+ private ImageInputStream imgInStream;
+ private DOMSource domSource;
+ private StringWriter writer;
+ private InputStream testStream;
+ private Reader reader;
+
+ @Before
+ public void setUp() throws IOException, ParserConfigurationException {
+ testStream = mock(InputStream.class);
+ reader = mock(Reader.class);
+
+ streamSource = mock(StreamSource.class);
+ when(streamSource.getInputStream()).thenReturn(testStream);
+ when(streamSource.getReader()).thenReturn(reader);
+
+ saxSource = mock(SAXSource.class);
+ inputSource = mock(InputSource.class);
+ when(saxSource.getInputSource()).thenReturn(inputSource);
+ when(inputSource.getByteStream()).thenReturn(testStream);
+ when(inputSource.getCharacterStream()).thenReturn(reader);
+
+ imgSource = mock(ImageSource.class);
+ imgInStream = mock(ImageInputStream.class);
+ when(imgSource.getImageInputStream()).thenReturn(imgInStream);
+
+
+ DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
+ DocumentBuilder db = dbf.newDocumentBuilder();
+ domSource = new DOMSource(db.newDocument().createElement("test"));
+ InputStream inStream = XmlSourceUtil.getInputStream(domSource);
+ writer = new StringWriter();
+ IOUtils.copy(inStream, writer);
+ }
+
+ @Test
+ public void testGetInputStream() throws ParserConfigurationException, IOException {
+ getInputStream(streamSource);
+ verify(streamSource).getInputStream();
+
+ getInputStream(saxSource);
+ verify(inputSource).getByteStream();
+
+ getInputStream(imgSource);
+ verify(imgSource).getImageInputStream();
+
+ assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?><test/>", writer.toString());
+
+ // Test negative case
+ Source src = mock(Source.class);
+ assertNull(getInputStream(src));
+
+ getInputStream(null);
+ }
+
+ @Test
+ public void testNeedInputStream() throws IOException, ParserConfigurationException {
+ assertEquals(testStream, needInputStream(streamSource));
+
+ assertEquals(testStream, needInputStream(saxSource));
+
+ needInputStream(imgSource);
+ verify(imgSource).getImageInputStream();
+
+ assertEquals("<?xml version=\"1.0\" encoding=\"UTF-8\"?><test/>", writer.toString());
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testNeedInputStreamFailureCaseSource() {
+ Source src = mock(Source.class);
+ needInputStream(src);
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testNeedInputStreamFailureCaseStreamSource() {
+ needInputStream(mock(StreamSource.class));
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testNeedInputStreamFailureCaseSAXSource() {
+ needInputStream(mock(SAXSource.class));
+ }
+
+ public void testNeedInputStreamFailureCaseDOMSource() throws IOException {
+ InputStream inStream = needInputStream(new DOMSource());
+ StringWriter writer = new StringWriter();
+ IOUtils.copy(inStream, writer);
+ assertEquals("", writer.toString());
+ }
+
+ @Test(expected = AssertionError.class)
+ public void testNeedInputStreamFailureCaseStreamImage() {
+ needInputStream(mock(ImageSource.class));
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ public void testNeedInputStreamFailureCaseNullArg() {
+ needInputStream(null);
+ }
+
+ @Test
+ public void testHasReader() {
+ assertTrue(hasReader(streamSource));
+
+ assertTrue(hasReader(saxSource));
+
+ when(streamSource.getReader()).thenReturn(null);
+ when(inputSource.getCharacterStream()).thenReturn(null);
+
+ assertFalse(hasReader(streamSource));
+ assertFalse(hasReader(saxSource));
+ assertFalse(hasReader(imgSource));
+ assertFalse(hasReader(domSource));
+
+ hasReader(null);
+ }
+
+ @Test
+ public void testRemoveStreams() {
+ removeStreams(streamSource);
+ verify(streamSource).setInputStream(null);
+ verify(streamSource).setReader(null);
+
+ removeStreams(imgSource);
+ verify(imgSource).setImageInputStream(null);
+
+ removeStreams(saxSource);
+ verify(inputSource).setByteStream(null);
+ verify(inputSource).setCharacterStream(null);
+
+ removeStreams(null);
+ }
+
+ @Test
+ public void testCloseQuietlyStreamSource() throws IOException {
+ closeQuietly(streamSource);
+ verify(reader).close();
+ verify(streamSource).setInputStream(null);
+ verify(streamSource).setReader(null);
+ }
+
+ @Test
+ public void testCloseQuietlySaxSource() throws IOException {
+ closeQuietly(saxSource);
+ verify(testStream).close();
+ verify(reader).close();
+ verify(inputSource).setByteStream(null);
+ verify(inputSource).setCharacterStream(null);
+ }
+
+ @Test
+ public void testCloseQuietlyImageSource() throws IOException {
+ closeQuietly(imgSource);
+ verify(imgInStream).close();
+ verify(imgSource).setImageInputStream(null);
+ }
+
+ @Test
+ public void testCloseQuietlyNull() {
+ XmlSourceUtil.closeQuietly(null);
+ }
+
+ @Test
+ public void testHasInputStream() {
+ assertTrue(hasInputStream(streamSource));
+ assertTrue(hasInputStream(saxSource));
+ assertTrue(hasInputStream(imgSource));
+ assertTrue(hasInputStream(domSource));
+
+ assertFalse(hasInputStream(mock(StreamSource.class)));
+ assertFalse(hasInputStream(mock(SAXSource.class)));
+ // Can't do the ImageSource test because of an assert, do we want that assert there?
+ // assertFalse(hasInputStream(mock(ImageSource.class)));
+ assertFalse(hasInputStream(mock(StreamSource.class)));
+ }
+}
Propchange: xmlgraphics/commons/branches/Temp_URI_Resolution/test/java/org/apache/xmlgraphics/io/XmlSourceUtilTestCase.java
------------------------------------------------------------------------------
svn:eol-style = native
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: commits-help@xmlgraphics.apache.org