package org.codehaus.xfire.wsdl;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;

import org.codehaus.xfire.util.Resolver;

/**
 * Create a WSDL instance from a URI.
 *
 * @author <a href="mailto:dan@envoisolutions.com">Dan Diephouse</a>
 */
public class ResourceWSDL
	implements WSDLWriter
{
    private InputStream is;
    private URL wsdlUrl;

    /**
     * @param wsdlUrl
     */
    public ResourceWSDL(String wsdlUrl) throws IOException
    {
        this.is = new Resolver(wsdlUrl).getInputStream();
    }

    public ResourceWSDL(String baseUri, String wsdlUrl) throws IOException
    {
        this.is = new Resolver(baseUri, wsdlUrl).getInputStream();
    }
    
    /**
     * @param wsdlUrl
     */
    public ResourceWSDL(URL wsdlUrl)
    {
        this.wsdlUrl = wsdlUrl;
    }

    public void write(OutputStream out) throws IOException
    {
        if (wsdlUrl != null)
            is = wsdlUrl.openStream();
        
        copy(is, out, 8096 );
    }

    private void copy(final InputStream input,
                     final OutputStream output,
                     final int bufferSize)
        throws IOException
    {
        try
        {
            final byte[] buffer = new byte[bufferSize];

            int n = 0;
            while (-1 != (n = input.read(buffer)))
            {
                output.write(buffer, 0, n);
            }
        }
        finally
        {
            input.close();
        }
    }
}

@@ -361,47 +361,81 @@
 ate 
-InputStream is;%0A    private URL wsdlUrl
+URL wsdlUrl;%0A    private String wsdlString;%0A    private String baseString
 ;%0A%0A 
@@ -548,56 +548,53 @@
     
-this.is = new Resolver(wsdlUrl).getInputStream()
+wsdlString = wsdlUrl;%0A        baseString = %22%22
 ;%0A  
@@ -691,73 +691,62 @@
     
-this.is = new Resolver(baseUri, wsdlUrl).getInputStream()
+wsdlString=wsdlUrl;%0A        baseString=baseUri
 ;%0A    %7D%0A
     
@@ -741,21 +741,17 @@
 ;%0A    %7D%0A
-    
 %0A
+
     /**%0A
@@ -919,32 +919,56 @@
 Exception%0A    %7B%0A
+        InputStream is;%0A
         if (wsdl
@@ -1027,16 +1027,93 @@
         
+else%0A            is = new Resolver(baseString, wsdlString).getInputStream();%0A
 %0A       
@@ -1392,16 +1392,16 @@
 Size%5D;%0A%0A
+
         
@@ -1413,12 +1413,8 @@
 nt n
- = 0
 ;%0A  

fix XFIRE-494 in trunk