diff options
author | Tom Tromey <tromey@redhat.com> | 2006-04-05 20:16:02 +0000 |
---|---|---|
committer | Tom Tromey <tromey@redhat.com> | 2006-04-05 20:16:02 +0000 |
commit | b2279e53a29b07dd8925b0d7a3e6a3c3b7454757 (patch) | |
tree | 21f9d6aeca46e6b4a3325995631c7b4dc9654f24 /java | |
parent | e31741703142702594b1cebaae8d508d796692e4 (diff) | |
download | classpath-b2279e53a29b07dd8925b0d7a3e6a3c3b7454757.tar.gz |
* java/net/MimeTypeMapper.java (MimeTypeMapper): Look for system
property with mime.types name.
* gnu/classpath/SystemProperties.java: Set
gnu.classpath.mime.types.file if not already set.
* java/net/URLConnection.java (defaultFactory): New field.
(guessContentTypeFromStream): Mark as unimplemented.
(getContentHandler): Updated with libgcj's implementation.
* gnu/java/net/DefaultContentHandlerFactory.java: New file,
from libgcj.
Diffstat (limited to 'java')
-rw-r--r-- | java/net/MimeTypeMapper.java | 16 | ||||
-rw-r--r-- | java/net/URLConnection.java | 98 |
2 files changed, 78 insertions, 36 deletions
diff --git a/java/net/MimeTypeMapper.java b/java/net/MimeTypeMapper.java index ce27e14c0..8153694b4 100644 --- a/java/net/MimeTypeMapper.java +++ b/java/net/MimeTypeMapper.java @@ -37,6 +37,8 @@ exception statement from your version. */ package java.net; +import gnu.classpath.SystemProperties; + import java.io.FileReader; import java.io.IOException; import java.io.LineNumberReader; @@ -240,18 +242,19 @@ class MimeTypeMapper implements FileNameMap for (int i = 0; i < mime_strings.length; i++) mime_types.put(mime_strings[i][0], mime_strings[i][1]); - // Now read from /etc/mime.types, if it exists. Entries found + // Now read from the system mime database, if it exists. Entries found // here override our internal ones. try { - fillFromFile(mime_types, "/etc/mime.types"); + // On Linux this usually means /etc/mime.types. + String file + = SystemProperties.getProperty("gnu.classpath.mime.types.file"); + if (file != null) + fillFromFile(mime_types, file); } catch (IOException ignore) { } - - // FIXME: should read ~/.mime.types. - // FIXME: should consider having a mime.types in $JAVA_HOME/lib/. } public static void fillFromFile (Map table, String fname) @@ -323,6 +326,9 @@ class MimeTypeMapper implements FileNameMap public static void main(String[] args) throws IOException { TreeMap map = new TreeMap(); + // It is fine to hard-code the name here. This is only ever + // used by maintainers, who can hack it if they need to re-run + // it. fillFromFile(map, "/etc/mime.types"); Iterator it = map.keySet().iterator(); boolean first = true; diff --git a/java/net/URLConnection.java b/java/net/URLConnection.java index 65755c201..7b5c636bd 100644 --- a/java/net/URLConnection.java +++ b/java/net/URLConnection.java @@ -38,6 +38,9 @@ exception statement from your version. */ package java.net; +import gnu.classpath.NotImplementedException; +import gnu.classpath.SystemProperties; + import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; @@ -47,8 +50,10 @@ import java.text.ParsePosition; import java.text.SimpleDateFormat; import java.util.Collections; import java.util.Date; +import java.util.Hashtable; import java.util.Locale; import java.util.Map; +import java.util.StringTokenizer; /** * Written using on-line Java Platform 1.2 API Specification, as well @@ -114,6 +119,12 @@ public abstract class URLConnection private static boolean defaultUseCaches = true; /** + * Default internal content handler factory. + */ + 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. */ @@ -160,6 +171,7 @@ public abstract class URLConnection * This is the URL associated with this connection */ protected URL url; + private static SimpleDateFormat[] dateFormats; private static boolean dateformats_initialized; @@ -923,8 +935,10 @@ public abstract class URLConnection * @exception IOException If an error occurs */ public static String guessContentTypeFromStream(InputStream is) - throws IOException + throws IOException, NotImplementedException { + // See /etc/gnome-vfs-mime-magic or /etc/mime-magic for a reasonable + // idea of how to handle this. return "application/octet-stream"; } @@ -979,44 +993,66 @@ public abstract class URLConnection if (factory != null) handler = factory.createContentHandler(contentType); - // Then try our default class. - try - { - String typeClass = contentType.replace('/', '.'); - - // Deal with "Content-Type: text/html; charset=ISO-8859-1". - int parameterBegin = typeClass.indexOf(';'); - if (parameterBegin >= 1) - typeClass = typeClass.substring(0, parameterBegin); + // Now try default factory. Using this factory to instantiate built-in + // content handlers is preferable + if (handler == null) + handler = defaultFactory.createContentHandler(contentType); - Class cls = Class.forName("gnu.java.net.content." + typeClass); - Object obj = cls.newInstance(); - - if (obj instanceof ContentHandler) - { - handler = (ContentHandler) obj; - return handler; - } - } - catch (ClassNotFoundException e) - { - // Ignore. - } - catch (InstantiationException e) - { - // Ignore. - } - catch (IllegalAccessException e) + // User-set factory has not returned a handler. Use the default search + // algorithm. + if (handler == null) { - // Ignore. + // Get the list of packages to check and append our default handler + // to it, along with the JDK specified default as a last resort. + // Except in very unusual environments the JDK specified one shouldn't + // ever be needed (or available). + String propVal = SystemProperties.getProperty("java.content.handler.pkgs"); + propVal = (((propVal == null) ? "" : (propVal + "|")) + + "gnu.java.net.content|sun.net.www.content"); + + // Deal with "Content-Type: text/html; charset=ISO-8859-1". + int parameterBegin = contentType.indexOf(';'); + if (parameterBegin >= 1) + contentType = contentType.substring(0, parameterBegin); + contentType = contentType.trim(); + + // Replace the '/' character in the content type with '.' and + // all other non-alphabetic, non-numeric characters with '_'. + char[] cArray = contentType.toCharArray(); + for (int i = 0; i < cArray.length; i++) + { + if (cArray[i] == '/') + cArray[i] = '.'; + else if (! ((cArray[i] >= 'A' && cArray[i] <= 'Z') || + (cArray[i] >= 'a' && cArray[i] <= 'z') || + (cArray[i] >= '0' && cArray[i] <= '9'))) + cArray[i] = '_'; + } + String contentClass = new String(cArray); + + // See if a class of this content type exists in any of the packages. + StringTokenizer pkgPrefix = new StringTokenizer(propVal, "|"); + do + { + String facName = pkgPrefix.nextToken() + "." + contentClass; + try + { + handler = + (ContentHandler) Class.forName(facName).newInstance(); + } + catch (Exception e) + { + // Can't instantiate; handler still null, go on to next element. + } + } while (handler == null && pkgPrefix.hasMoreTokens()); } return handler; } // We don't put these in a static initializer, because it creates problems - // with initializer co-dependency: SimpleDateFormat's constructors eventually - // depend on URLConnection (via the java.text.*Symbols classes). + // with initializer co-dependency: SimpleDateFormat's constructors + // eventually depend on URLConnection (via the java.text.*Symbols classes). private static synchronized void initializeDateFormats() { if (dateformats_initialized) |