J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 1995-2006 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun in the LICENSE file that accompanied this code. |
| 10 | * |
| 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | * version 2 for more details (a copy is included in the LICENSE file that |
| 15 | * accompanied this code). |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License version |
| 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | * |
| 21 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any questions. |
| 24 | */ |
| 25 | |
| 26 | package java.net; |
| 27 | |
| 28 | import java.io.IOException; |
| 29 | |
| 30 | /** |
| 31 | * The abstract class <code>ContentHandler</code> is the superclass |
| 32 | * of all classes that read an <code>Object</code> from a |
| 33 | * <code>URLConnection</code>. |
| 34 | * <p> |
| 35 | * An application does not generally call the |
| 36 | * <code>getContent</code> method in this class directly. Instead, an |
| 37 | * application calls the <code>getContent</code> method in class |
| 38 | * <code>URL</code> or in <code>URLConnection</code>. |
| 39 | * The application's content handler factory (an instance of a class that |
| 40 | * implements the interface <code>ContentHandlerFactory</code> set |
| 41 | * up by a call to <code>setContentHandler</code>) is |
| 42 | * called with a <code>String</code> giving the MIME type of the |
| 43 | * object being received on the socket. The factory returns an |
| 44 | * instance of a subclass of <code>ContentHandler</code>, and its |
| 45 | * <code>getContent</code> method is called to create the object. |
| 46 | * <p> |
| 47 | * If no content handler could be found, URLConnection will |
| 48 | * look for a content handler in a user-defineable set of places. |
| 49 | * By default it looks in sun.net.www.content, but users can define a |
| 50 | * vertical-bar delimited set of class prefixes to search through in |
| 51 | * addition by defining the java.content.handler.pkgs property. |
| 52 | * The class name must be of the form: |
| 53 | * <pre> |
| 54 | * {package-prefix}.{major}.{minor} |
| 55 | * e.g. |
| 56 | * YoyoDyne.experimental.text.plain |
| 57 | * </pre> |
| 58 | * If the loading of the content handler class would be performed by |
| 59 | * a classloader that is outside of the delegation chain of the caller, |
| 60 | * the JVM will need the RuntimePermission "getClassLoader". |
| 61 | * |
| 62 | * @author James Gosling |
| 63 | * @see java.net.ContentHandler#getContent(java.net.URLConnection) |
| 64 | * @see java.net.ContentHandlerFactory |
| 65 | * @see java.net.URL#getContent() |
| 66 | * @see java.net.URLConnection |
| 67 | * @see java.net.URLConnection#getContent() |
| 68 | * @see java.net.URLConnection#setContentHandlerFactory(java.net.ContentHandlerFactory) |
| 69 | * @since JDK1.0 |
| 70 | */ |
| 71 | abstract public class ContentHandler { |
| 72 | /** |
| 73 | * Given a URL connect stream positioned at the beginning of the |
| 74 | * representation of an object, this method reads that stream and |
| 75 | * creates an object from it. |
| 76 | * |
| 77 | * @param urlc a URL connection. |
| 78 | * @return the object read by the <code>ContentHandler</code>. |
| 79 | * @exception IOException if an I/O error occurs while reading the object. |
| 80 | */ |
| 81 | abstract public Object getContent(URLConnection urlc) throws IOException; |
| 82 | |
| 83 | /** |
| 84 | * Given a URL connect stream positioned at the beginning of the |
| 85 | * representation of an object, this method reads that stream and |
| 86 | * creates an object that matches one of the types specified. |
| 87 | * |
| 88 | * The default implementation of this method should call getContent() |
| 89 | * and screen the return type for a match of the suggested types. |
| 90 | * |
| 91 | * @param urlc a URL connection. |
| 92 | * @param classes an array of types requested |
| 93 | * @return the object read by the <code>ContentHandler</code> that is |
| 94 | * the first match of the suggested types. |
| 95 | * null if none of the requested are supported. |
| 96 | * @exception IOException if an I/O error occurs while reading the object. |
| 97 | * @since 1.3 |
| 98 | */ |
| 99 | public Object getContent(URLConnection urlc, Class[] classes) throws IOException { |
| 100 | Object obj = getContent(urlc); |
| 101 | |
| 102 | for (int i = 0; i < classes.length; i++) { |
| 103 | if (classes[i].isInstance(obj)) { |
| 104 | return obj; |
| 105 | } |
| 106 | } |
| 107 | return null; |
| 108 | } |
| 109 | |
| 110 | } |