001: /*
002: * @(#)ContentHandler.java 1.20 06/10/10
003: *
004: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: *
026: */
027:
028: package java.net;
029:
030: import java.io.IOException;
031:
032: /**
033: * The abstract class <code>ContentHandler</code> is the superclass
034: * of all classes that read an <code>Object</code> from a
035: * <code>URLConnection</code>.
036: * <p>
037: * An application does not generally call the
038: * <code>getContent</code> method in this class directly. Instead, an
039: * application calls the <code>getContent</code> method in class
040: * <code>URL</code> or in <code>URLConnection</code>.
041: * The application's content handler factory (an instance of a class that
042: * implements the interface <code>ContentHandlerFactory</code> set
043: * up by a call to <code>setContentHandler</code>) is
044: * called with a <code>String</code> giving the MIME type of the
045: * object being received on the socket. The factory returns an
046: * instance of a subclass of <code>ContentHandler</code>, and its
047: * <code>getContent</code> method is called to create the object.
048: * <p>
049: * If no content handler could be found, URLConnection will
050: * look for a content handler in a user-defineable set of places.
051: * By default it looks in sun.net.www.content, but users can define a
052: * vertical-bar delimited set of class prefixes to search through in
053: * addition by defining the java.content.handler.pkgs property.
054: * The class name must be of the form:
055: * <pre>
056: * {package-prefix}.{major}.{minor}
057: * e.g.
058: * YoyoDyne.experimental.text.plain
059: * </pre>
060: * If the loading of the content handler class would be performed by
061: * a classloader that is outside of the the delegation chain of the caller,
062: * the JVM will need the RuntimePermission "getClassLoader".
063: *
064: * @author James Gosling
065: * @version 1.14, 02/02/00
066: * @see java.net.ContentHandler#getContent(java.net.URLConnection)
067: * @see java.net.ContentHandlerFactory
068: * @see java.net.URL#getContent()
069: * @see java.net.URLConnection
070: * @see java.net.URLConnection#getContent()
071: * @see java.net.URLConnection#setContentHandlerFactory(java.net.ContentHandlerFactory)
072: * @since JDK1.0
073: */
074: abstract public class ContentHandler {
075: /**
076: * Given a URL connect stream positioned at the beginning of the
077: * representation of an object, this method reads that stream and
078: * creates an object from it.
079: *
080: * @param urlc a URL connection.
081: * @return the object read by the <code>ContentHandler</code>.
082: * @exception IOException if an I/O error occurs while reading the object.
083: */
084: abstract public Object getContent(URLConnection urlc)
085: throws IOException;
086:
087: /**
088: * Given a URL connect stream positioned at the beginning of the
089: * representation of an object, this method reads that stream and
090: * creates an object that matches one of the types specified.
091: *
092: * The default implementation of this method should call getContent()
093: * and screen the return type for a match of the suggested types.
094: *
095: * @param urlc a URL connection.
096: * @param classes an array of types requested
097: * @return the object read by the <code>ContentHandler</code> that is
098: * the first match of the suggested types.
099: * null if none of the requested are supported.
100: * @exception IOException if an I/O error occurs while reading the object.
101: */
102: public Object getContent(URLConnection urlc, Class[] classes)
103: throws IOException {
104: Object obj = getContent(urlc);
105:
106: for (int i = 0; i < classes.length; i++) {
107: if (classes[i].isInstance(obj)) {
108: return obj;
109: }
110: }
111: return null;
112: }
113:
114: }
|