This is the mail archive of the java-patches@gcc.gnu.org mailing list for the Java project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Patch: Implement URLContentHandler for images


Mark Wielaard wrote:

Hi,

On Fri, 2004-07-23 at 07:37, Ranjit Mathew wrote:


Bryce McKinlay wrote:


2004-07-21 Bryce McKinlay <mckinlay@redhat.com>

* Makefile.am (ordinary_java_source_files): Add
DefaultContentHandlerFactory.java.


This breaks bootstrap as you didn't check this file
in. (It's also not there in your patch...)



I just checked in the following:


2004-07-23 Mark Wielaard <mark@klomp.org>

      * gnu/java/net/DefaultContentHandlerFactory.java: New dummy
      implementation.

This makes libjava at least compile again. Bryce can check in a real
version when he wakes up.


Doh. I forgot to "cvs add" it. Thanks for cleaning up after me.

Here's the complete patch that I checked in. I also checked in the gnu.java.awt* changes to java-gui-branch.

Regards

Bryce


2004-07-23  Bryce McKinlay  <mckinlay@redhat.com>

	* Makefile.am (ordinary_java_source_files): Add
	DefaultContentHandlerFactory.java.
	* Makefile.in: Rebuilt.
	* java/net/URLConnection.java (defaultFactory): New field.
	(getContent): 
	(getContentHandler): Renamed from 'setContentHandler'. Try 
	defaultFactory after user-set factory, if any. Search for content 
	handler implementations in gnu.java.net.content, not gnu.gcj.content.
	* gnu/java/net/protocol/file/Connection.java (getHeaderField):
	Implemented.
	(getLastModified): Implemented.
	(getPermission): Create file permission here, instead of in
	constructor.
	* gnu/java/net/protocol/gcjlib/Connection.java (getHeaderField):
	Implemented.
	* gnu/java/net/protocol/jar/Connection.java (getHeaderField):
	Implemented.
	(getLastModified): Implemented.
	* gnu/java/awt/ClasspathToolkit.java (createImageProducer): New.
	Default implementation.
	* gnu/java/awt/peer/gtk/GtkToolkit.java (createImageProducer): New.
	Implement using GdkPixbufDecoder.
	* gnu/java/net/DefaultContentHandlerFactory: New file.

Index: Makefile.am
===================================================================
RCS file: /cvs/gcc/gcc/libjava/Makefile.am,v
retrieving revision 1.392
diff -u -r1.392 Makefile.am
--- Makefile.am	17 Jul 2004 08:37:19 -0000	1.392
+++ Makefile.am	22 Jul 2004 01:59:33 -0000
@@ -2423,6 +2423,7 @@
 gnu/java/locale/LocaleInformation_zh_SG.java \
 gnu/java/locale/LocaleInformation_zh_TW.java \
 gnu/java/math/MPN.java \
+gnu/java/net/DefaultContentHandlerFactory.java \
 gnu/java/net/HeaderFieldHelper.java \
 gnu/java/net/PlainDatagramSocketImpl.java \
 gnu/java/net/PlainSocketImpl.java \
Index: gnu/java/net/protocol/file/Connection.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/gnu/java/net/protocol/file/Connection.java,v
retrieving revision 1.6
diff -u -r1.6 Connection.java
--- gnu/java/net/protocol/file/Connection.java	6 Jan 2004 15:00:11 -0000	1.6
+++ gnu/java/net/protocol/file/Connection.java	22 Jul 2004 01:59:33 -0000
@@ -50,6 +50,9 @@
 import java.net.URL;
 import java.net.URLConnection;
 import java.security.Permission;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.Locale;
 
 /**
  * This subclass of java.net.URLConnection models a URLConnection via
@@ -62,9 +65,11 @@
 public class Connection extends URLConnection
 {
   /**
-   * Default permission for a file
+   * HTTP-style DateFormat, used to format the last-modified header.
    */
-  private static final String DEFAULT_PERMISSION = "read";
+  private static SimpleDateFormat dateFormat
+    = new SimpleDateFormat("EEE, dd MMM yyyy hh:mm:ss 'GMT'",
+                           new Locale ("En", "Us", "Unix"));
 
   /**
    * This is a File object for this connection
@@ -82,18 +87,11 @@
   private OutputStream outputStream;
   
   /**
-   * FilePermission to read the file
-   */
-  private FilePermission permission;
-
-  /**
    * Calls superclass constructor to initialize.
    */
   public Connection(URL url)
   {
     super (url);
-
-    permission = new FilePermission(getURL().getFile(), DEFAULT_PERMISSION);
   }
   
   /**
@@ -156,45 +154,73 @@
   }
 
   /**
-   * Get the last modified time of the resource.
-   *
-   * @return the time since epoch that the resource was modified.
+   *  Get an http-style header field. Just handle a few common ones. 
    */
-  public long getLastModified()
+  public String getHeaderField(String field)
   {
     try
       {
 	if (!connected)
 	  connect();
 
-	return file.lastModified();
+	if (field.equals("content-type"))
+          return guessContentTypeFromName(file.getName());
+	else if (field.equals("content-length"))
+          return Long.toString(file.length());
+	else if (field.equals("last-modified"))
+	  {
+	    synchronized (dateFormat)
+	      {
+        	return dateFormat.format(new Date(file.lastModified()));
+	      }
+	  }
       }
     catch (IOException e)
       {
-	return -1;
+        // Fall through.
       }
+    return null;
   }
 
   /**
    * Get the length of content.
-   *
    * @return the length of the content.
    */
   public int getContentLength()
   {
     try
       {
+ 	if (!connected)
+ 	  connect();
+
+	return (int) file.length();
+      }
+    catch (IOException e)
+      {
+ 	return -1;
+      }
+  }
+
+  /**
+   * Get the last modified time of the resource.
+   *
+   * @return the time since epoch that the resource was modified.
+   */
+  public long getLastModified()
+  {
+    try
+      {
 	if (!connected)
 	  connect();
-        
-	return (int) file.length();
+
+	return file.lastModified();
       }
     catch (IOException e)
       {
 	return -1;
       }
   }
-  
+
   /**
    * This method returns a <code>Permission</code> object representing the
    * permissions required to access this URL.  This method returns a
@@ -205,6 +231,6 @@
    */
   public Permission getPermission() throws IOException
   {
-    return permission;
+    return new FilePermission(getURL().getFile(), "read");
   }
 }
Index: gnu/java/net/protocol/gcjlib/Connection.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/gnu/java/net/protocol/gcjlib/Connection.java,v
retrieving revision 1.1
diff -u -r1.1 Connection.java
--- gnu/java/net/protocol/gcjlib/Connection.java	8 Oct 2003 16:28:28 -0000	1.1
+++ gnu/java/net/protocol/gcjlib/Connection.java	22 Jul 2004 01:59:33 -0000
@@ -61,4 +61,23 @@
     connect();
     return new CoreInputStream(core);
   }
+  
+  public String getHeaderField(String field)
+  {
+    try
+      {
+	if (!connected)
+	  connect();
+
+	if (field.equals("content-type"))
+          return guessContentTypeFromName(name);
+	else if (field.equals("content-length"))
+          return Long.toString(core.length);
+      }
+    catch (IOException e)
+      {
+        // Fall through.
+      }
+    return null;
+  }  
 }
Index: gnu/java/net/protocol/jar/Connection.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/gnu/java/net/protocol/jar/Connection.java,v
retrieving revision 1.6
diff -u -r1.6 Connection.java
--- gnu/java/net/protocol/jar/Connection.java	11 Mar 2004 17:06:11 -0000	1.6
+++ gnu/java/net/protocol/jar/Connection.java	22 Jul 2004 01:59:33 -0000
@@ -49,8 +49,11 @@
 import java.net.ProtocolException;
 import java.net.URL;
 import java.net.URLConnection;
+import java.text.SimpleDateFormat;
+import java.util.Date;
 import java.util.HashMap;
 import java.util.Hashtable;
+import java.util.Locale;
 import java.util.jar.JarEntry;
 import java.util.jar.JarFile;
 import java.util.jar.JarInputStream;
@@ -66,6 +69,14 @@
 public final class Connection extends JarURLConnection
 {
   private static Hashtable file_cache = new Hashtable();
+
+  /**
+   * HTTP-style DateFormat, used to format the last-modified header.
+   */
+  private static SimpleDateFormat dateFormat
+    = new SimpleDateFormat("EEE, dd MMM yyyy hh:mm:ss 'GMT'",
+                           new Locale ("En", "Us", "Unix"));
+
   private JarFile jar_file;
 
   /**
@@ -219,6 +230,32 @@
     return jar_file;
   }
 
+  public String getHeaderField(String field)
+  {
+    try
+      {
+	if (!connected)
+	  connect();
+
+	if (field.equals("content-type"))
+          return guessContentTypeFromName(getJarEntry().getName());
+	else if (field.equals("content-length"))
+          return Long.toString(getJarEntry().getSize());
+	else if (field.equals("last-modified"))
+	  {
+	    synchronized (dateFormat)
+	      {
+        	return dateFormat.format(new Date(getJarEntry().getTime()));
+	      }
+	  }
+      }
+    catch (IOException e)
+      {
+        // Fall through.
+      }
+    return null;
+  }
+
   public int getContentLength()
   {
     if (!connected)
@@ -233,4 +270,19 @@
 	return -1;
       }
   }
+
+  public long getLastModified()
+  {
+    if (!connected)
+      return -1;
+
+    try
+      {
+	return getJarEntry().getTime();
+      }
+    catch (IOException e)
+      {
+	return -1;
+      }
+  }
 }
Index: gnu/java/awt/ClasspathToolkit.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/gnu/java/awt/ClasspathToolkit.java,v
retrieving revision 1.1
diff -u -r1.1 ClasspathToolkit.java
--- gnu/java/awt/ClasspathToolkit.java	25 Oct 2003 18:41:45 -0000	1.1
+++ gnu/java/awt/ClasspathToolkit.java	22 Jul 2004 01:59:34 -0000
@@ -48,6 +48,7 @@
 import java.awt.HeadlessException;
 import java.awt.Toolkit;
 import java.awt.image.ColorModel;
+import java.awt.image.ImageProducer;
 import java.io.File;
 import java.io.InputStream;
 import java.io.IOException;
@@ -331,4 +332,17 @@
         .initCause(muex);
     }
   }
+  
+  /**
+   * Creates an ImageProducer from the specified URL. The image is assumed
+   * to be in a recognised format. If the toolkit does not implement the
+   * image format or the image format is not recognised, null is returned.
+   * This default implementation is overriden by the Toolkit implementations.
+   *
+   * @param url URL to read image data from.
+   */
+  public ImageProducer createImageProducer(URL url)
+  {
+    return null;
+  }
 }
Index: gnu/java/awt/peer/gtk/GtkToolkit.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/gnu/java/awt/peer/gtk/GtkToolkit.java,v
retrieving revision 1.12
diff -u -r1.12 GtkToolkit.java
--- gnu/java/awt/peer/gtk/GtkToolkit.java	17 Jun 2004 23:43:10 -0000	1.12
+++ gnu/java/awt/peer/gtk/GtkToolkit.java	22 Jul 2004 01:59:34 -0000
@@ -182,6 +182,17 @@
         return image;        
       }
   }
+  
+  /**
+   * Creates an ImageProducer from the specified URL. The image is assumed
+   * to be in a recognised format. 
+   *
+   * @param url URL to read image data from.
+   */  
+  public ImageProducer createImageProducer(URL url)
+  {
+    return new GdkPixbufDecoder(url);  
+  }
 
   public ColorModel getColorModel () 
   {
Index: URLConnection.java
===================================================================
RCS file: /cvs/gcc/gcc/libjava/java/net/URLConnection.java,v
retrieving revision 1.32
diff -u -r1.32 URLConnection.java
--- URLConnection.java	22 Jul 2004 01:33:11 -0000	1.32
+++ URLConnection.java	23 Jul 2004 01:16:17 -0000
@@ -116,6 +116,9 @@
    */
   private static boolean defaultUseCaches = true;
 
+  private static ContentHandlerFactory defaultFactory
+    = new gnu.java.net.DefaultContentHandlerFactory();
+
   /**
    * This variable determines whether or not interaction is allowed with
    * the user.  For example, to prompt for a username and password.
@@ -436,7 +439,7 @@
     // guessContentTypeFromName() and guessContentTypeFromStream methods
     // as well as FileNameMap class & fileNameMap field & get/set methods.
     String type = getContentType();
-    ContentHandler ch = setContentHandler(type);
+    ContentHandler ch = getContentHandler(type);
 
     if (ch == null)
       return getInputStream();
@@ -963,7 +966,7 @@
     fileNameMap = map;
   }
 
-  private ContentHandler setContentHandler(String contentType)
+  private ContentHandler getContentHandler(String contentType)
   {
     ContentHandler handler;
 
@@ -981,12 +984,17 @@
       else
 	return null;
 
-    // If a non-default factory has been set, use it to find the content type.
+    // If a non-default factory has been set, use it.
     if (factory != null)
       handler = factory.createContentHandler(contentType);
 
-    // Non-default factory may have returned null or a factory wasn't set.
-    // Use the default search algorithm to find a handler for this content type.
+    // Now try default factory. Using this factory to instantiate built-in
+    // content handlers is preferable  
+    if (handler == null)
+      handler = defaultFactory.createContentHandler(contentType);
+
+    // User-set factory has not returned a handler. Use the default search 
+    // algorithm.
     if (handler == null)
       {
 	// Get the list of packages to check and append our default handler
@@ -995,7 +1003,7 @@
 	// ever be needed (or available).
 	String propVal = System.getProperty("java.content.handler.pkgs");
 	propVal = (propVal == null) ? "" : (propVal + "|");
-	propVal = propVal + "gnu.gcj.content|sun.net.www.content";
+	propVal = propVal + "gnu.java.net.content|sun.net.www.content";
 
 	// Replace the '/' character in the content type with '.' and
 	// all other non-alphabetic, non-numeric characters with '_'.


--- /dev/null	2004-02-23 16:02:56.000000000 -0500
+++ gnu/java/net/DefaultContentHandlerFactory.java	2004-07-23 10:24:57.744898178 -0400
@@ -0,0 +1,95 @@
+/* DefaultContentHandlerFactory.java
+   Copyright (C) 2004 Free Software Foundation, Inc.
+
+This file is part of GNU Classpath.
+
+GNU Classpath is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+ 
+GNU Classpath is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with GNU Classpath; see the file COPYING.  If not, write to the
+Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+02111-1307 USA.
+
+Linking this library statically or dynamically with other modules is
+making a combined work based on this library.  Thus, the terms and
+conditions of the GNU General Public License cover the whole
+combination.
+
+As a special exception, the copyright holders of this library give you
+permission to link this library with independent modules to produce an
+executable, regardless of the license terms of these independent
+modules, and to copy and distribute the resulting executable under
+terms of your choice, provided that you also meet, for each linked
+independent module, the terms and conditions of the license of that
+module.  An independent module is a module which is not derived from
+or based on this library.  If you modify this library, you may extend
+this exception to your version of the library, but you are not
+obligated to do so.  If you do not wish to do so, delete this
+exception statement from your version. */
+
+
+package gnu.java.net;
+
+import java.awt.Toolkit;
+import java.io.IOException;
+import java.net.ContentHandler;
+import java.net.ContentHandlerFactory;
+import java.net.URLConnection;
+import java.util.Arrays;
+import java.util.HashSet;
+
+import gnu.java.awt.ClasspathToolkit;
+
+/** Content Handler for Image types, using the AWT Toolkit's image decoder. */
+class ImageHandler extends ContentHandler
+{
+  static ImageHandler instance = new ImageHandler();
+  
+  public Object getContent(URLConnection urlc) throws IOException
+  {
+    ClasspathToolkit tk = (ClasspathToolkit) Toolkit.getDefaultToolkit();
+    java.awt.image.ImageProducer ip = tk.createImageProducer(urlc.getURL());
+    return ip;
+  }
+}
+
+/**
+ */
+public class DefaultContentHandlerFactory implements ContentHandlerFactory
+{
+  /** For now, hard code the list of types that we assume should
+   *  be supported by the Toolkit. ClasspathToolkit should perhaps provide
+   *  an API to express what Image MIME types the Toolkit understands.
+   */
+  private static String[] known_image_types =
+    {
+      "image/bmp",
+      "image/gif",
+      "image/jpeg",
+      "image/png",
+      "image/tiff",
+      "image/x-portable-anymap",
+      "image/x-cmu-raster",
+      "image/x-xbitmap",
+      "image/x-xpixmap"
+    };
+   
+  private static HashSet imageTypes
+    = new HashSet(Arrays.asList(known_image_types));
+
+  public ContentHandler createContentHandler(String mimeType)
+  {
+    if (imageTypes.contains(mimeType))
+      return ImageHandler.instance;
+    // Currently, only image types are handled.
+    return null;
+  }
+}

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]