hillion     02/04/10 09:19:03

  Modified:    sources/org/apache/batik/bridge
                        BaseScriptingEnvironment.java
                        ScriptingEnvironment.java
               sources/org/apache/batik/script Window.java
               sources/org/apache/batik/script/rhino RhinoInterpreter.java
                        WindowWrapper.java
               xdocs    scriptFeatures.xml
  Log:
  - getURL(uri, function[, encoding]) implemented,
  - doc updated.
  
  Revision  Changes    Path
  1.4       +18 -1     
xml-batik/sources/org/apache/batik/bridge/BaseScriptingEnvironment.java
  
  Index: BaseScriptingEnvironment.java
  ===================================================================
  RCS file: 
/home/cvs/xml-batik/sources/org/apache/batik/bridge/BaseScriptingEnvironment.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- BaseScriptingEnvironment.java     10 Apr 2002 07:23:11 -0000      1.3
  +++ BaseScriptingEnvironment.java     10 Apr 2002 16:19:02 -0000      1.4
  @@ -56,7 +56,7 @@
    * This class is the base class for SVG scripting.
    *
    * @author <a href="mailto:[EMAIL PROTECTED]";>Stephane Hillion</a>
  - * @version $Id: BaseScriptingEnvironment.java,v 1.3 2002/04/10 07:23:11 hillion 
Exp $
  + * @version $Id: BaseScriptingEnvironment.java,v 1.4 2002/04/10 16:19:02 hillion 
Exp $
    */
   public class BaseScriptingEnvironment {
   
  @@ -476,6 +476,23 @@
            */
           public DocumentFragment parseXML(String text, Document doc) {
               return null;
  +        }
  +
  +        /**
  +         * Gets data from the given URI.
  +         * @param uri The URI where the data is located.
  +         * @param h A handler called when the data is available.
  +         */
  +        public void getURL(String uri, GetURLHandler h) {
  +        }
  +
  +        /**
  +         * Gets data from the given URI.
  +         * @param uri The URI where the data is located.
  +         * @param h A handler called when the data is available.
  +         * @param enc The character encoding of the data.
  +         */
  +        public void getURL(String uri, GetURLHandler h, String enc) {
           }
   
           /**
  
  
  
  1.19      +63 -1     
xml-batik/sources/org/apache/batik/bridge/ScriptingEnvironment.java
  
  Index: ScriptingEnvironment.java
  ===================================================================
  RCS file: 
/home/cvs/xml-batik/sources/org/apache/batik/bridge/ScriptingEnvironment.java,v
  retrieving revision 1.18
  retrieving revision 1.19
  diff -u -r1.18 -r1.19
  --- ScriptingEnvironment.java 10 Apr 2002 07:23:11 -0000      1.18
  +++ ScriptingEnvironment.java 10 Apr 2002 16:19:02 -0000      1.19
  @@ -8,7 +8,13 @@
   
   package org.apache.batik.bridge;
   
  +import java.io.BufferedReader;
  +import java.io.InputStreamReader;
  +import java.io.Reader;
   import java.io.StringReader;
  +import java.io.StringWriter;
  +
  +import java.net.URL;
   
   import java.util.Timer;
   import java.util.TimerTask;
  @@ -21,6 +27,8 @@
   import org.apache.batik.script.Interpreter;
   import org.apache.batik.script.InterpreterException;
   
  +import org.apache.batik.util.EncodingUtilities;
  +import org.apache.batik.util.ParsedURL;
   import org.apache.batik.util.RunnableQueue;
   import org.apache.batik.util.SVGConstants;
   import org.apache.batik.util.XMLResourceDescriptor;
  @@ -38,7 +46,7 @@
    * This class contains the informations needed by the SVG scripting.
    *
    * @author <a href="mailto:[EMAIL PROTECTED]";>Stephane Hillion</a>
  - * @version $Id: ScriptingEnvironment.java,v 1.18 2002/04/10 07:23:11 hillion Exp $
  + * @version $Id: ScriptingEnvironment.java,v 1.19 2002/04/10 16:19:02 hillion Exp $
    */
   public class ScriptingEnvironment extends BaseScriptingEnvironment {
   
  @@ -617,6 +625,60 @@
               }
               return result;
           }
  +
  +        /**
  +         * Implements {@link
  +         * org.apache.batik.script.Window#getURL(String,GetURLHandler)}.
  +         */
  +        public void getURL(String uri, GetURLHandler h) {
  +            getURL(uri, h, "UTF8");
  +        }
  +
  +        /**
  +         * Implements {@link
  +         * org.apache.batik.script.Window#getURL(String,GetURLHandler,String)}.
  +         */
  +        public void getURL(final String uri,
  +                           final GetURLHandler h,
  +                           final String enc) {
  +            Thread t = new Thread() {
  +                    public void run() {
  +                        try {
  +                            URL burl;
  +                            burl = ((SVGOMDocument)document).getURLObject();
  +                            final ParsedURL purl = new ParsedURL(burl, uri);
  +                            String e = EncodingUtilities.javaEncoding(enc);
  +                            e = (e == null) ? enc : e;
  +                            Reader r =
  +                                new InputStreamReader(purl.openStream(), e);
  +                            r = new BufferedReader(r);
  +                            final StringWriter sw = new StringWriter();
  +                            int read;
  +                            char[] buf = new char[4096];
  +                            while ((read = r.read(buf, 0, buf.length)) != -1) {
  +                                sw.write(buf, 0, read);
  +                            }
  +
  +                            updateRunnableQueue.invokeLater(new Runnable() {
  +                                    public void run() {
  +                                        h.getURLDone(true,
  +                                                     purl.getContentType(),
  +                                                     sw.toString());
  +                                    }
  +                                });
  +                        } catch (Exception e) {
  +                            updateRunnableQueue.invokeLater(new Runnable() {
  +                                    public void run() {
  +                                        h.getURLDone(false, "", "");
  +                                    }
  +                                });
  +                        }
  +                    }
  +                };
  +            t.setPriority(Thread.MIN_PRIORITY);
  +            t.start();
  +        }
  +
   
           /**
            * Displays an alert dialog box.
  
  
  
  1.3       +30 -1     xml-batik/sources/org/apache/batik/script/Window.java
  
  Index: Window.java
  ===================================================================
  RCS file: /home/cvs/xml-batik/sources/org/apache/batik/script/Window.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- Window.java       10 Apr 2002 07:23:11 -0000      1.2
  +++ Window.java       10 Apr 2002 16:19:03 -0000      1.3
  @@ -18,7 +18,7 @@
    * environment of a SVG document.
    *
    * @author <a href="mailto:[EMAIL PROTECTED]";>Stephane Hillion</a>
  - * @version $Id: Window.java,v 1.2 2002/04/10 07:23:11 hillion Exp $
  + * @version $Id: Window.java,v 1.3 2002/04/10 16:19:03 hillion Exp $
    */
   public interface Window {
   
  @@ -71,6 +71,35 @@
        * @return The document fragment or null on error.
        */
       DocumentFragment parseXML(String text, Document doc);
  +
  +    /**
  +     * Gets data from the given URI.
  +     * @param uri The URI where the data is located.
  +     * @param h A handler called when the data is available.
  +     */
  +    void getURL(String uri, GetURLHandler h);
  +
  +    /**
  +     * Gets data from the given URI.
  +     * @param uri The URI where the data is located.
  +     * @param h A handler called when the data is available.
  +     * @param enc The character encoding of the data.
  +     */
  +    void getURL(String uri, GetURLHandler h, String enc);
  +
  +    /**
  +     * To handle the completion of a 'getURL()' call.
  +     */
  +    public interface GetURLHandler {
  +        
  +        /**
  +         * Called when 'getURL()' returns.
  +         * @param success Whether the data was successfully retreived.
  +         * @param mime The data MIME type.
  +         * @param content The data.
  +         */
  +        void getURLDone(boolean success, String mime, String content);
  +    }
   
       /**
        * Displays an alert dialog box.
  
  
  
  1.14      +23 -1     
xml-batik/sources/org/apache/batik/script/rhino/RhinoInterpreter.java
  
  Index: RhinoInterpreter.java
  ===================================================================
  RCS file: 
/home/cvs/xml-batik/sources/org/apache/batik/script/rhino/RhinoInterpreter.java,v
  retrieving revision 1.13
  retrieving revision 1.14
  diff -u -r1.13 -r1.14
  --- RhinoInterpreter.java     25 Feb 2002 15:05:33 -0000      1.13
  +++ RhinoInterpreter.java     10 Apr 2002 16:19:03 -0000      1.14
  @@ -37,7 +37,7 @@
    * A simple implementation of <code>Interpreter</code> interface to use
    * Rhino ECMAScript interpreter.
    * @author <a href="mailto:[EMAIL PROTECTED]";>Christophe Jolif</a>
  - * @version $Id: RhinoInterpreter.java,v 1.13 2002/02/25 15:05:33 hillion Exp $
  + * @version $Id: RhinoInterpreter.java,v 1.14 2002/04/10 16:19:03 hillion Exp $
    */
   public class RhinoInterpreter implements Interpreter {
       private static String[] TO_BE_IMPORTED = {
  @@ -293,6 +293,28 @@
           } finally {
               Context.exit();
           }
  +    }
  +
  +    /**
  +     * To be used by <code>WindowWrapper</code>.
  +     */
  +    void callHandler(Function handler,
  +                     ArgumentsBuilder ab)
  +        throws JavaScriptException {
  +        Context ctx = Context.enter();
  +        ctx.setWrapHandler(wrapHandler);
  +        try {
  +            handler.call(ctx, globalObject, globalObject, ab.buildArguments());
  +        } finally {
  +            Context.exit();
  +        }
  +    }
  +
  +    /**
  +     * To build an argument list.
  +     */
  +    public interface ArgumentsBuilder { 
  +        Object[] buildArguments();
       }
   
       /**
  
  
  
  1.6       +118 -6    
xml-batik/sources/org/apache/batik/script/rhino/WindowWrapper.java
  
  Index: WindowWrapper.java
  ===================================================================
  RCS file: 
/home/cvs/xml-batik/sources/org/apache/batik/script/rhino/WindowWrapper.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- WindowWrapper.java        10 Apr 2002 07:23:11 -0000      1.5
  +++ WindowWrapper.java        10 Apr 2002 16:19:03 -0000      1.6
  @@ -8,10 +8,14 @@
   
   package org.apache.batik.script.rhino;
   
  +import java.io.IOException;
  +import java.io.StringReader;
  +
   import org.mozilla.javascript.Context;
   import org.mozilla.javascript.Function;
   import org.mozilla.javascript.FunctionObject;
   import org.mozilla.javascript.JavaScriptException;
  +import org.mozilla.javascript.NativeBoolean;
   import org.mozilla.javascript.NativeJavaObject;
   import org.mozilla.javascript.Scriptable;
   import org.mozilla.javascript.ScriptableObject;
  @@ -29,7 +33,7 @@
    * This class wraps a Window object to expose it to the interpreter.
    *
    * @author <a href="mailto:[EMAIL PROTECTED]";>Stephane Hillion</a>
  - * @version $Id: WindowWrapper.java,v 1.5 2002/04/10 07:23:11 hillion Exp $
  + * @version $Id: WindowWrapper.java,v 1.6 2002/04/10 16:19:03 hillion Exp $
    */
   public class WindowWrapper extends ScriptableObject {
   
  @@ -172,12 +176,41 @@
           int len = args.length;
           WindowWrapper ww = (WindowWrapper)thisObj;
           Window window = ww.window;
  -        if (len >= 2) {
  -            return window.parseXML
  -              ((String)NativeJavaObject.coerceType(String.class, args[0]),
  -               (Document)NativeJavaObject.coerceType(Document.class, args[1]));
  +        if (len < 2) {
  +            throw Context.reportRuntimeError("invalid argument count");
  +        }
  +        return window.parseXML
  +            ((String)NativeJavaObject.coerceType(String.class, args[0]),
  +             (Document)NativeJavaObject.coerceType(Document.class, args[1]));
  +    }
  +
  +    /**
  +     * Wraps the 'getURL' method of the Window interface.
  +     */
  +    public static void jsFunction_getURL(Context cx,
  +                                         Scriptable thisObj,
  +                                         Object[] args,
  +                                         Function funObj)
  +        throws JavaScriptException {
  +        int len = args.length;
  +        WindowWrapper ww = (WindowWrapper)thisObj;
  +        Window window = ww.window;
  +        if (len < 2) {
  +            throw Context.reportRuntimeError("invalid argument count");
  +        }
  +        RhinoInterpreter interp =
  +            (RhinoInterpreter)window.getInterpreter();
  +        String uri;
  +        uri = (String)NativeJavaObject.coerceType(String.class, args[0]);
  +        GetURLFunctionWrapper fw;
  +        fw = new GetURLFunctionWrapper(interp, (Function)args[1], ww);
  +        if (len == 2) {
  +            window.getURL(uri, fw);
  +        } else {
  +            window.getURL
  +                (uri, fw,
  +                 (String)NativeJavaObject.coerceType(String.class, args[2]));
           }
  -        return null;
       }
   
       /**
  @@ -283,6 +316,85 @@
           public void run() {
               try {
                   interpreter.callHandler(function, arguments);
  +            } catch (JavaScriptException e) {
  +                throw new WrappedException(e);
  +            }
  +        }
  +    }
  +
  +    /**
  +     * To wrap a function passed to getURL().
  +     */
  +    protected static class GetURLFunctionWrapper
  +        implements Window.GetURLHandler {
  +        
  +        /**
  +         * The current interpreter.
  +         */
  +        protected RhinoInterpreter interpreter;
  +
  +        /**
  +         * The function wrapper.
  +         */
  +        protected Function function;
  +
  +        /**
  +         * The WindowWrapper.
  +         */
  +        protected WindowWrapper windowWrapper;
  +
  +        /**
  +         * Creates a wrapper.
  +         */
  +        public GetURLFunctionWrapper(RhinoInterpreter ri, Function fct,
  +                                     WindowWrapper ww) {
  +            interpreter = ri;
  +            function = fct;
  +            windowWrapper = ww;
  +        }
  +
  +        /**
  +         * Called before 'getURL()' returns.
  +         * @param success Whether the data was successfully retreived.
  +         * @param mime The data MIME type.
  +         * @param content The data.
  +         */
  +        public void getURLDone(final boolean success,
  +                               final String mime,
  +                               final String content) {
  +            try {
  +                interpreter.callHandler(function,
  +                    new RhinoInterpreter.ArgumentsBuilder() {
  +                        public Object[] buildArguments() {
  +                            try {
  +                                Object[] arguments = new Object[1];
  +                                ScriptableObject so =
  +                                    (ScriptableObject)interpreter.evaluate
  +                                    (new StringReader("new Object()"));
  +                                so.put("success", so,
  +                                       Context.toObject((success) ?
  +                                                        Boolean.TRUE :
  +                                                        Boolean.FALSE,
  +                                                        windowWrapper));
  +                                if (mime != null) {
  +                                    so.put("contentType", so,
  +                                           Context.toObject(mime,
  +                                                            windowWrapper));
  +                                }
  +                                if (content != null) {
  +                                    so.put("content", so,
  +                                           Context.toObject(content,
  +                                                            windowWrapper));
  +                                }
  +                                arguments[0] = so;
  +                                return arguments;
  +                            } catch (IOException e) {
  +                                throw new WrappedException(e);
  +                            } catch (InterpreterException e) {
  +                                throw new WrappedException(e);
  +                            }
  +                        }
  +                    });
               } catch (JavaScriptException e) {
                   throw new WrappedException(e);
               }
  
  
  
  1.6       +34 -1     xml-batik/xdocs/scriptFeatures.xml
  
  Index: scriptFeatures.xml
  ===================================================================
  RCS file: /home/cvs/xml-batik/xdocs/scriptFeatures.xml,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- scriptFeatures.xml        10 Apr 2002 07:23:11 -0000      1.5
  +++ scriptFeatures.xml        10 Apr 2002 16:19:03 -0000      1.6
  @@ -11,7 +11,7 @@
   
   <!-- ========================================================================= -->
   <!-- author [EMAIL PROTECTED]                                                     -->
  -<!-- version $Id: scriptFeatures.xml,v 1.5 2002/04/10 07:23:11 hillion Exp $ -->    
  
  +<!-- version $Id: scriptFeatures.xml,v 1.6 2002/04/10 16:19:03 hillion Exp $ -->    
  
   <!-- ========================================================================= -->
   <document>
     <header>
  @@ -274,6 +274,39 @@
         <p>
         This method returns a <code>org.w3c.dom.DocumentFragment</code> object.
         </p>
  +
  +
  +      <!-- ========================================================= -->
  +
  +      <table>
  +        <tr>
  +          <td>Method <code>getURL</code>(<em>uri</em>,
  +                                         <em>function</em>[,
  +                                         <em>encoding</em>])</td>
  +        
  +        </tr>
  +      </table>
  +      <p>
  +      Gets data from the given URI. This method returns immediately and
  +      the given function is called when the data is fully downloaded.
  +      </p>
  +      <ul>
  +        <li><em>uri</em>: A string representing the location of the data.</li>
  +        <li><em>function</em>: A function called when the data is available,
  +            or when the loading has failed. The argument passed to the
  +            function is an ECMAScript Object with 3 properties:
  +          <ul>
  +            <li><em>success</em>: true if the data is available, false
  +                otherwise,</li>
  +            <li><em>contentType</em>: the content type of the data, if the
  +                information is known by the viewer,</li>
  +            <li><em>content</em>: A string representing the data.</li>
  +          </ul>
  +        </li>
  +        <li><em>encoding</em>: The character encoding of the data file,
  +            by default UTF-8 is used.</li>
  +
  +      </ul>
   
       </s1>
     </body>
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to