You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@xmlgraphics.apache.org by je...@apache.org on 2008/02/23 12:20:45 UTC

svn commit: r630423 - in /xmlgraphics/commons/trunk: ./ src/java/org/apache/xmlgraphics/image/loader/ src/java/org/apache/xmlgraphics/image/loader/impl/ src/java/org/apache/xmlgraphics/image/loader/util/ test/java/org/apache/xmlgraphics/image/loader/

Author: jeremias
Date: Sat Feb 23 03:20:41 2008
New Revision: 630423

URL: http://svn.apache.org/viewvc?rev=630423&view=rev
Log:
Bugzilla #44466:
Image Loading: Support for passing in SAXSource instance.
Suggested by: Adam Strzelecki <ono.at.java.pl>

Added:
    xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/image/loader/ImageSessionContextTestCase.java   (with props)
    xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/image/loader/SimpleURIResolverBasedImageSessionContext.java   (with props)
Modified:
    xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/image/loader/ImageSessionContext.java
    xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/image/loader/impl/AbstractImageSessionContext.java
    xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/image/loader/impl/DefaultImageSessionContext.java
    xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/image/loader/util/ImageUtil.java
    xmlgraphics/commons/trunk/status.xml
    xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/image/loader/ImagePreloaderTestCase.java
    xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/image/loader/MockImageContext.java
    xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/image/loader/MockImageSessionContext.java

Modified: xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/image/loader/ImageSessionContext.java
URL: http://svn.apache.org/viewvc/xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/image/loader/ImageSessionContext.java?rev=630423&r1=630422&r2=630423&view=diff
==============================================================================
--- xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/image/loader/ImageSessionContext.java (original)
+++ xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/image/loader/ImageSessionContext.java Sat Feb 23 03:20:41 2008
@@ -46,7 +46,7 @@
     
     /**
      * Attempts to create a Source object from the given URI. If possible this method returns
-     * ImageSource instance which provide the best possible method to access the image.
+     * an ImageSource instance which provides the best possible method to access the image.
      * @param uri URI to access
      * @return A {@link javax.xml.transform.Source} object, or null if the URI
      * cannot be resolved. 

Modified: xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/image/loader/impl/AbstractImageSessionContext.java
URL: http://svn.apache.org/viewvc/xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/image/loader/impl/AbstractImageSessionContext.java?rev=630423&r1=630422&r2=630423&view=diff
==============================================================================
--- xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/image/loader/impl/AbstractImageSessionContext.java (original)
+++ xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/image/loader/impl/AbstractImageSessionContext.java Sat Feb 23 03:20:41 2008
@@ -30,6 +30,7 @@
 import javax.imageio.stream.ImageInputStream;
 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.apache.commons.io.FileUtils;
@@ -68,7 +69,7 @@
             }
             return null;
         }
-        if (!(source instanceof StreamSource)) {
+        if (!(source instanceof StreamSource) && !(source instanceof SAXSource)) {
             //Return any non-stream Sources and let the ImageLoaders deal with them
             return source;
         }
@@ -85,20 +86,19 @@
         File f = FileUtils.toFile(url);
         if (f != null) {
             boolean directFileAccess = true;
-            InputStream in = null;
-            if (source instanceof StreamSource) {
-                StreamSource streamSource = (StreamSource)source; 
-                in = streamSource.getInputStream();
-                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;
-                    }
+            assert (source instanceof StreamSource) || (source instanceof SAXSource);
+            InputStream in = ImageUtil.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)) {
@@ -130,16 +130,12 @@
         }
         
         if (imageSource == null) {
-            // Got a valid source, obtain an InputStream from it
-            InputStream in = null;
-            if (source instanceof StreamSource) {
-                StreamSource ssrc = (StreamSource)source;
-                if (ssrc.getReader() != null && ssrc.getInputStream() == null) {
-                    //We don't handle Reader instances here so return the Source unchanged
-                    return ssrc;
-                }
-                in = ssrc.getInputStream();
+            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 = ImageUtil.getInputStream(source);
             if (in == null && url != null) {
                 try {
                     in = url.openStream();

Modified: xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/image/loader/impl/DefaultImageSessionContext.java
URL: http://svn.apache.org/viewvc/xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/image/loader/impl/DefaultImageSessionContext.java?rev=630423&r1=630422&r2=630423&view=diff
==============================================================================
--- xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/image/loader/impl/DefaultImageSessionContext.java (original)
+++ xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/image/loader/impl/DefaultImageSessionContext.java Sat Feb 23 03:20:41 2008
@@ -1,12 +1,13 @@
 /*
- * Copyright 2007 Jeremias Maerki
- *
- * Licensed 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
- *
+ * 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.
@@ -15,7 +16,7 @@
  */
 
 /* $Id$ */
-
+ 
 package org.apache.xmlgraphics.image.loader.impl;
 
 import java.io.File;
@@ -51,6 +52,14 @@
     /** {@inheritDoc} */
     public ImageContext getParentContext() {
         return this.context;
+    }
+    
+    /**
+     * Returns the base directory for resolving relative filenames.
+     * @return the base directory
+     */
+    public File getBaseDir() {
+        return this.baseDir;
     }
 
     /** {@inheritDoc} */

Modified: xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/image/loader/util/ImageUtil.java
URL: http://svn.apache.org/viewvc/xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/image/loader/util/ImageUtil.java?rev=630423&r1=630422&r2=630423&view=diff
==============================================================================
--- xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/image/loader/util/ImageUtil.java (original)
+++ xmlgraphics/commons/trunk/src/java/org/apache/xmlgraphics/image/loader/util/ImageUtil.java Sat Feb 23 03:20:41 2008
@@ -21,6 +21,7 @@
 
 import java.io.IOException;
 import java.io.InputStream;
+import java.io.Reader;
 import java.lang.reflect.InvocationHandler;
 import java.lang.reflect.Method;
 import java.lang.reflect.Proxy;
@@ -31,8 +32,11 @@
 
 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;
@@ -54,9 +58,13 @@
             return ((StreamSource)src).getInputStream();
         } else if (src instanceof ImageSource) {
             return new ImageInputStreamAdapter(((ImageSource)src).getImageInputStream());
-        } else {
-            return null;
+        } else if (src instanceof SAXSource) {
+            InputSource is = ((SAXSource)src).getInputSource();
+            if (is != null) {
+                return is.getByteStream();
+            }
         }
+        return null;
     }
 
     /**
@@ -119,9 +127,31 @@
             return (in != null);
         } else if (src instanceof ImageSource) {
             return hasImageInputStream(src);
-        } else {
-            return false;
+        } else if (src instanceof SAXSource) {
+            InputSource is = ((SAXSource)src).getInputSource();
+            if (is != null) {
+                return (is.getByteStream() != null);
+            }
+        }
+        return false;
+    }
+
+    /**
+     * 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;
     }
 
     /**
@@ -152,6 +182,12 @@
             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);
+            }
         }
     }
     
@@ -178,6 +214,14 @@
                     //ignore
                 }
                 imageSource.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);
             }
         }
     }

Modified: xmlgraphics/commons/trunk/status.xml
URL: http://svn.apache.org/viewvc/xmlgraphics/commons/trunk/status.xml?rev=630423&r1=630422&r2=630423&view=diff
==============================================================================
--- xmlgraphics/commons/trunk/status.xml (original)
+++ xmlgraphics/commons/trunk/status.xml Sat Feb 23 03:20:41 2008
@@ -25,6 +25,9 @@
 	</todo>
 	<changes>
 		<release version="Trunk" date="n/a">
+		  <action context="Code" dev="JM" type="add" fixes-bug="44466" due-to="Adam Strzelecki">
+		    Image Loading: Support for passing in SAXSource instance.
+		  </action>
 		  <action context="Code" dev="JM" type="fix">
 		    Bugfix for ISO 8601 date formatting with negative time zones in the XMP package.
 		  </action>

Modified: xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/image/loader/ImagePreloaderTestCase.java
URL: http://svn.apache.org/viewvc/xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/image/loader/ImagePreloaderTestCase.java?rev=630423&r1=630422&r2=630423&view=diff
==============================================================================
--- xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/image/loader/ImagePreloaderTestCase.java (original)
+++ xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/image/loader/ImagePreloaderTestCase.java Sat Feb 23 03:20:41 2008
@@ -19,10 +19,19 @@
 
 package org.apache.xmlgraphics.image.loader;
 
+import java.io.File;
 import java.io.FileNotFoundException;
+import java.io.IOException;
+
+import javax.xml.transform.Source;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.URIResolver;
+import javax.xml.transform.sax.SAXSource;
 
 import junit.framework.TestCase;
 
+import org.xml.sax.InputSource;
+
 import org.apache.xmlgraphics.image.loader.spi.ImageLoaderFactory;
 import org.apache.xmlgraphics.util.MimeConstants;
 
@@ -245,4 +254,51 @@
         assertEquals(17000, info.getSize().getHeightMpt());
     }
  
+    public void testSAXSourceWithSystemID() throws Exception {
+        URIResolver resolver = new URIResolver() {
+            public Source resolve(String href, String base) throws TransformerException {
+                if (href.startsWith("img:")) {
+                    String filename = href.substring(4);
+                    InputSource is = new InputSource(base + filename);
+                    return new SAXSource(is);
+                } else {
+                    return null;
+                }
+            }
+        };
+        checkImageFound("img:asf-logo.png", resolver);
+    }
+    
+    public void testSAXSourceWithInputStream() throws Exception {
+        URIResolver resolver = new URIResolver() {
+            public Source resolve(String href, String base) throws TransformerException {
+                if (href.startsWith("img:")) {
+                    String filename = href.substring(4);
+                    InputSource is;
+                    try {
+                        is = new InputSource(new java.io.FileInputStream(
+                                new File(MockImageSessionContext.IMAGE_BASE_DIR, filename)));
+                    } catch (FileNotFoundException e) {
+                        throw new TransformerException(e);
+                    }
+                    return new SAXSource(is);
+                } else {
+                    return null;
+                }
+            }
+        };
+        checkImageFound("img:asf-logo.png", resolver);
+    }
+    
+    private void checkImageFound(String uri, URIResolver resolver)
+                throws ImageException, IOException {
+        ImageSessionContext sessionContext = new SimpleURIResolverBasedImageSessionContext(
+                imageContext, MockImageSessionContext.IMAGE_BASE_DIR, resolver);
+        ImageManager manager = imageContext.getImageManager();
+        
+        ImageInfo info = manager.preloadImage(uri, sessionContext);
+        assertNotNull("ImageInfo must not be null", info);
+        assertEquals(uri, info.getOriginalURI());
+    }
+    
 }

Added: xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/image/loader/ImageSessionContextTestCase.java
URL: http://svn.apache.org/viewvc/xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/image/loader/ImageSessionContextTestCase.java?rev=630423&view=auto
==============================================================================
--- xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/image/loader/ImageSessionContextTestCase.java (added)
+++ xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/image/loader/ImageSessionContextTestCase.java Sat Feb 23 03:20:41 2008
@@ -0,0 +1,295 @@
+/*
+ * 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.
+ */
+
+/* $Id$ */
+
+package org.apache.xmlgraphics.image.loader;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+
+import javax.xml.parsers.SAXParser;
+import javax.xml.parsers.SAXParserFactory;
+import javax.xml.transform.Source;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.URIResolver;
+import javax.xml.transform.dom.DOMResult;
+import javax.xml.transform.dom.DOMSource;
+import javax.xml.transform.sax.SAXSource;
+import javax.xml.transform.stream.StreamSource;
+
+import junit.framework.TestCase;
+
+import org.xml.sax.InputSource;
+import org.xml.sax.XMLReader;
+
+import org.apache.xmlgraphics.image.loader.util.ImageUtil;
+
+/**
+ * Tests for AbstractImageSessionContext.
+ */
+public class ImageSessionContextTestCase extends TestCase {
+
+    private MockImageContext imageContext = MockImageContext.getInstance();
+    
+    public ImageSessionContextTestCase(String name) {
+        super(name);
+    }
+    
+    public void testStreamSourceWithSystemID() throws Exception {
+        URIResolver resolver = new URIResolver() {
+            public Source resolve(String href, String base) throws TransformerException {
+                if (href.startsWith("img:")) {
+                    String filename = href.substring(4);
+                    return new StreamSource(base + filename);
+                } else {
+                    return null;
+                }
+            }
+        };
+        String uri = "img:asf-logo.png";
+        
+        ImageSource imgSrc = checkImageInputStreamAvailable(uri, resolver);
+        assertTrue(imgSrc.isFastSource()); //Access through local file system
+    }
+
+    public void testStreamSourceWithInputStream() throws Exception {
+        URIResolver resolver = new URIResolver() {
+            public Source resolve(String href, String base) throws TransformerException {
+                if (href.startsWith("img:")) {
+                    String filename = href.substring(4);
+                    try {
+                        return new StreamSource(new java.io.FileInputStream(
+                                new File(MockImageSessionContext.IMAGE_BASE_DIR, filename)));
+                    } catch (FileNotFoundException e) {
+                        throw new TransformerException(e);
+                    }
+                } else {
+                    return null;
+                }
+            }
+        };
+        String uri = "img:asf-logo.png";
+        
+        ImageSource imgSrc = checkImageInputStreamAvailable(uri, resolver);
+        //We don't pass in the URI, so no fast source is possible
+        assertTrue(!imgSrc.isFastSource());
+    }
+
+    public void testStreamSourceWithFile() throws Exception {
+        URIResolver resolver = new URIResolver() {
+            public Source resolve(String href, String base) throws TransformerException {
+                if (href.startsWith("img:")) {
+                    String filename = href.substring(4);
+                    File f = new File(MockImageSessionContext.IMAGE_BASE_DIR, filename);
+                    return new StreamSource(f);
+                } else {
+                    return null;
+                }
+            }
+        };
+        String uri = "img:asf-logo.png";
+        
+        ImageSource imgSrc = checkImageInputStreamAvailable(uri, resolver);
+        assertTrue(imgSrc.isFastSource()); //Accessed through the local file system
+    }
+
+    public void testStreamSourceWithInputStreamAndSystemID() throws Exception {
+        URIResolver resolver = new URIResolver() {
+            public Source resolve(String href, String base) throws TransformerException {
+                if (href.startsWith("img:")) {
+                    String filename = href.substring(4);
+                    try {
+                        File f = new File(MockImageSessionContext.IMAGE_BASE_DIR, filename);
+                        return new StreamSource(
+                                new java.io.FileInputStream(f),
+                                f.toURI().toASCIIString());
+                    } catch (FileNotFoundException e) {
+                        throw new TransformerException(e);
+                    }
+                } else {
+                    return null;
+                }
+            }
+        };
+        String uri = "img:asf-logo.png";
+        
+        ImageSource imgSrc = checkImageInputStreamAvailable(uri, resolver);
+        assertTrue(imgSrc.isFastSource()); //Access through local file system (thanks to the URI
+                                           //being passed through by the URIResolver)
+    }
+
+    public void testStreamSourceWithReader() throws Exception {
+        URIResolver resolver = new URIResolver() {
+            public Source resolve(String href, String base) throws TransformerException {
+                if (href.startsWith("img:")) {
+                    String filename = href.substring(4);
+                    return new StreamSource(new java.io.StringReader(filename));
+                } else {
+                    return null;
+                }
+            }
+        };
+        String uri = "img:asf-logo.png";
+
+        Source src = resolve(uri, resolver);
+        assertTrue(src instanceof StreamSource); //Source remains unchanged
+        assertTrue(ImageUtil.hasReader(src));
+    }
+    
+    private ImageSource checkImageInputStreamAvailable(String uri, URIResolver resolver) {
+        Source src = resolve(uri, resolver);
+        assertNotNull("Source must not be null", src);
+        assertTrue("Source must be an ImageSource", src instanceof ImageSource);
+        ImageSource imgSrc = (ImageSource)src;
+        assertTrue(ImageUtil.hasImageInputStream(imgSrc));
+        return imgSrc;
+    }
+
+    private Source resolve(String uri, URIResolver resolver) {
+        ImageSessionContext sessionContext = new SimpleURIResolverBasedImageSessionContext(
+                imageContext, MockImageSessionContext.IMAGE_BASE_DIR, resolver);
+        Source src = sessionContext.newSource(uri);
+        return src;
+    }
+    
+    public void testSAXSourceWithSystemID() throws Exception {
+        URIResolver resolver = new URIResolver() {
+            public Source resolve(String href, String base) throws TransformerException {
+                if (href.startsWith("img:")) {
+                    String filename = href.substring(4);
+                    InputSource is = new InputSource(base + filename);
+                    return new SAXSource(is);
+                } else {
+                    return null;
+                }
+            }
+        };
+        String uri = "img:asf-logo.png";
+        
+        ImageSource imgSrc = checkImageInputStreamAvailable(uri, resolver);
+        assertTrue(imgSrc.isFastSource());
+    }
+    
+    public void testSAXSourceWithInputStream() throws Exception {
+        URIResolver resolver = new URIResolver() {
+            public Source resolve(String href, String base) throws TransformerException {
+                if (href.startsWith("img:")) {
+                    String filename = href.substring(4);
+                    InputSource is;
+                    try {
+                        is = new InputSource(new java.io.FileInputStream(
+                                new File(MockImageSessionContext.IMAGE_BASE_DIR, filename)));
+                    } catch (FileNotFoundException e) {
+                        throw new TransformerException(e);
+                    }
+                    return new SAXSource(is);
+                } else {
+                    return null;
+                }
+            }
+        };
+        String uri = "img:asf-logo.png";
+
+        checkImageInputStreamAvailable(uri, resolver);
+    }
+    
+    public void testSAXSourceWithReader() throws Exception {
+        URIResolver resolver = new URIResolver() {
+            public Source resolve(String href, String base) throws TransformerException {
+                if (href.startsWith("img:")) {
+                    String filename = href.substring(4);
+                    InputSource is;
+                    is = new InputSource(new java.io.StringReader(filename));
+                    return new SAXSource(is);
+                } else {
+                    return null;
+                }
+            }
+        };
+        String uri = "img:asf-logo.png";
+
+        Source src = resolve(uri, resolver);
+        assertTrue(src instanceof SAXSource); //Source remains unchanged
+        assertTrue(ImageUtil.hasReader(src));
+    }
+    
+    private static final String SOME_XML = "<root><child id='1'>Hello World!</child></root>";
+    
+    public void testSAXSourceWithXMLReader() throws Exception {
+        URIResolver resolver = new URIResolver() {
+            public Source resolve(String href, String base) throws TransformerException {
+                if (href.startsWith("xml:")) {
+                    String xml = href.substring(4);
+                    InputSource is = new InputSource(new java.io.StringReader(xml));
+                    return new SAXSource(createSomeXMLReader(), is);
+                } else {
+                    return null;
+                }
+            }
+        };
+        String uri = "xml:" + SOME_XML;
+
+        Source src = resolve(uri, resolver);
+        assertTrue(src instanceof SAXSource); //Source remains unchanged
+        SAXSource saxSrc = (SAXSource)src;
+        assertNotNull(saxSrc.getXMLReader());
+        assertNotNull(saxSrc.getInputSource());
+    }
+    
+    public void testDOMSource() throws Exception {
+        URIResolver resolver = new URIResolver() {
+            public Source resolve(String href, String base) throws TransformerException {
+                if (href.startsWith("xml:")) {
+                    String xml = href.substring(4);
+                    InputSource is = new InputSource(new java.io.StringReader(xml));
+                    SAXSource sax = new SAXSource(createSomeXMLReader(), is);
+                    
+                    //Convert SAXSource to DOMSource
+                    TransformerFactory tFactory = TransformerFactory.newInstance();
+                    Transformer transformer = tFactory.newTransformer();
+                    DOMResult res = new DOMResult();
+                    transformer.transform(sax, res);
+                    return new DOMSource(res.getNode());
+                } else {
+                    return null;
+                }
+            }
+        };
+        String uri = "xml:" + SOME_XML;
+
+        Source src = resolve(uri, resolver);
+        assertTrue(src instanceof DOMSource); //Source remains unchanged
+        DOMSource domSrc = (DOMSource)src;
+        assertNotNull(domSrc.getNode());
+    }
+    
+    private XMLReader createSomeXMLReader() {
+        SAXParserFactory parserFactory = SAXParserFactory.newInstance();
+        SAXParser parser;
+        try {
+            parser = parserFactory.newSAXParser();
+            return parser.getXMLReader();
+        } catch (Exception e) {
+            fail("Could not create XMLReader");
+            return null;
+        }
+    }
+    
+}

Propchange: xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/image/loader/ImageSessionContextTestCase.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/image/loader/ImageSessionContextTestCase.java
------------------------------------------------------------------------------
    svn:keywords = Id

Modified: xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/image/loader/MockImageContext.java
URL: http://svn.apache.org/viewvc/xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/image/loader/MockImageContext.java?rev=630423&r1=630422&r2=630423&view=diff
==============================================================================
--- xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/image/loader/MockImageContext.java (original)
+++ xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/image/loader/MockImageContext.java Sat Feb 23 03:20:41 2008
@@ -45,6 +45,6 @@
     }
     
     public ImageSessionContext newSessionContext() {
-        return new MockImageSessionContext();
+        return new MockImageSessionContext(this);
     }
 }

Modified: xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/image/loader/MockImageSessionContext.java
URL: http://svn.apache.org/viewvc/xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/image/loader/MockImageSessionContext.java?rev=630423&r1=630422&r2=630423&view=diff
==============================================================================
--- xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/image/loader/MockImageSessionContext.java (original)
+++ xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/image/loader/MockImageSessionContext.java Sat Feb 23 03:20:41 2008
@@ -28,8 +28,10 @@
  */
 public class MockImageSessionContext extends DefaultImageSessionContext {
 
-    public MockImageSessionContext() {
-        super(MockImageContext.getInstance(), new File("./test/images/"));
+    public static final File IMAGE_BASE_DIR = new File("./test/images/");
+    
+    public MockImageSessionContext(ImageContext context) {
+        super(context, IMAGE_BASE_DIR);
     }
     
     /** {@inheritDoc} */

Added: xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/image/loader/SimpleURIResolverBasedImageSessionContext.java
URL: http://svn.apache.org/viewvc/xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/image/loader/SimpleURIResolverBasedImageSessionContext.java?rev=630423&view=auto
==============================================================================
--- xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/image/loader/SimpleURIResolverBasedImageSessionContext.java (added)
+++ xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/image/loader/SimpleURIResolverBasedImageSessionContext.java Sat Feb 23 03:20:41 2008
@@ -0,0 +1,60 @@
+/*
+ * 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.
+ */
+
+/* $Id$ */
+ 
+package org.apache.xmlgraphics.image.loader;
+
+import java.io.File;
+
+import javax.xml.transform.Source;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.URIResolver;
+
+import org.apache.xmlgraphics.image.loader.impl.DefaultImageSessionContext;
+
+/**
+ * ImageSessionContext which uses a URIResolver to resolve URIs.
+ */
+public class SimpleURIResolverBasedImageSessionContext
+            extends DefaultImageSessionContext {
+
+    private URIResolver resolver;
+
+    /**
+     * Main constructor
+     * @param context the parent image context
+     * @param baseDir the base directory
+     * @param resolver the URI resolver
+     */
+    public SimpleURIResolverBasedImageSessionContext(ImageContext context, 
+            File baseDir, URIResolver resolver) {
+        super(context, baseDir);
+        this.resolver = resolver;
+    }
+    
+    /** {@inheritDoc} */
+    protected Source resolveURI(String uri) {
+        try {
+            return this.resolver.resolve(uri, getBaseDir().toURI().toASCIIString());
+        } catch (TransformerException e) {
+            e.printStackTrace();
+            return null;
+        }
+    }
+
+}
\ No newline at end of file

Propchange: xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/image/loader/SimpleURIResolverBasedImageSessionContext.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: xmlgraphics/commons/trunk/test/java/org/apache/xmlgraphics/image/loader/SimpleURIResolverBasedImageSessionContext.java
------------------------------------------------------------------------------
    svn:keywords = Id



---------------------------------------------------------------------
Apache XML Graphics Project URL: http://xmlgraphics.apache.org/
To unsubscribe, e-mail: commits-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: commits-help@xmlgraphics.apache.org