KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > net > ContentHandler


1 /*
2  * @(#)ContentHandler.java 1.20 06/04/07
3  *
4  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package java.net;
9
10 import java.io.IOException JavaDoc;
11
12 /**
13  * The abstract class <code>ContentHandler</code> is the superclass
14  * of all classes that read an <code>Object</code> from a
15  * <code>URLConnection</code>.
16  * <p>
17  * An application does not generally call the
18  * <code>getContent</code> method in this class directly. Instead, an
19  * application calls the <code>getContent</code> method in class
20  * <code>URL</code> or in <code>URLConnection</code>.
21  * The application's content handler factory (an instance of a class that
22  * implements the interface <code>ContentHandlerFactory</code> set
23  * up by a call to <code>setContentHandler</code>) is
24  * called with a <code>String</code> giving the MIME type of the
25  * object being received on the socket. The factory returns an
26  * instance of a subclass of <code>ContentHandler</code>, and its
27  * <code>getContent</code> method is called to create the object.
28  * <p>
29  * If no content handler could be found, URLConnection will
30  * look for a content handler in a user-defineable set of places.
31  * By default it looks in sun.net.www.content, but users can define a
32  * vertical-bar delimited set of class prefixes to search through in
33  * addition by defining the java.content.handler.pkgs property.
34  * The class name must be of the form:
35  * <pre>
36  * {package-prefix}.{major}.{minor}
37  * e.g.
38  * YoyoDyne.experimental.text.plain
39  * </pre>
40  * If the loading of the content handler class would be performed by
41  * a classloader that is outside of the delegation chain of the caller,
42  * the JVM will need the RuntimePermission "getClassLoader".
43  *
44  * @author James Gosling
45  * @version 1.20, 04/07/06
46  * @see java.net.ContentHandler#getContent(java.net.URLConnection)
47  * @see java.net.ContentHandlerFactory
48  * @see java.net.URL#getContent()
49  * @see java.net.URLConnection
50  * @see java.net.URLConnection#getContent()
51  * @see java.net.URLConnection#setContentHandlerFactory(java.net.ContentHandlerFactory)
52  * @since JDK1.0
53  */

54 abstract public class ContentHandler {
55     /**
56      * Given a URL connect stream positioned at the beginning of the
57      * representation of an object, this method reads that stream and
58      * creates an object from it.
59      *
60      * @param urlc a URL connection.
61      * @return the object read by the <code>ContentHandler</code>.
62      * @exception IOException if an I/O error occurs while reading the object.
63      */

64     abstract public Object JavaDoc getContent(URLConnection JavaDoc urlc) throws IOException JavaDoc;
65
66     /**
67      * Given a URL connect stream positioned at the beginning of the
68      * representation of an object, this method reads that stream and
69      * creates an object that matches one of the types specified.
70      *
71      * The default implementation of this method should call getContent()
72      * and screen the return type for a match of the suggested types.
73      *
74      * @param urlc a URL connection.
75      * @param classes an array of types requested
76      * @return the object read by the <code>ContentHandler</code> that is
77      * the first match of the suggested types.
78      * null if none of the requested are supported.
79      * @exception IOException if an I/O error occurs while reading the object.
80      * @since 1.3
81      */

82     public Object JavaDoc getContent(URLConnection JavaDoc urlc, Class JavaDoc[] classes) throws IOException JavaDoc {
83         Object JavaDoc obj = getContent(urlc);
84
85     for (int i = 0; i < classes.length; i++) {
86       if (classes[i].isInstance(obj)) {
87         return obj;
88       }
89     }
90     return null;
91     }
92
93 }
94
Popular Tags