1. /*
  2. * @(#)ContentHandler.java 1.16 03/01/23
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.net;
  8. import java.io.IOException;
  9. /**
  10. * The abstract class <code>ContentHandler</code> is the superclass
  11. * of all classes that read an <code>Object</code> from a
  12. * <code>URLConnection</code>.
  13. * <p>
  14. * An application does not generally call the
  15. * <code>getContent</code> method in this class directly. Instead, an
  16. * application calls the <code>getContent</code> method in class
  17. * <code>URL</code> or in <code>URLConnection</code>.
  18. * The application's content handler factory (an instance of a class that
  19. * implements the interface <code>ContentHandlerFactory</code> set
  20. * up by a call to <code>setContentHandler</code>) is
  21. * called with a <code>String</code> giving the MIME type of the
  22. * object being received on the socket. The factory returns an
  23. * instance of a subclass of <code>ContentHandler</code>, and its
  24. * <code>getContent</code> method is called to create the object.
  25. * <p>
  26. * If no content handler could be found, URLConnection will
  27. * look for a content handler in a user-defineable set of places.
  28. * By default it looks in sun.net.www.content, but users can define a
  29. * vertical-bar delimited set of class prefixes to search through in
  30. * addition by defining the java.content.handler.pkgs property.
  31. * The class name must be of the form:
  32. * <pre>
  33. * {package-prefix}.{major}.{minor}
  34. * e.g.
  35. * YoyoDyne.experimental.text.plain
  36. * </pre>
  37. * If the loading of the content handler class would be performed by
  38. * a classloader that is outside of the the delegation chain of the caller,
  39. * the JVM will need the RuntimePermission "getClassLoader".
  40. *
  41. * @author James Gosling
  42. * @version 1.16, 01/23/03
  43. * @see java.net.ContentHandler#getContent(java.net.URLConnection)
  44. * @see java.net.ContentHandlerFactory
  45. * @see java.net.URL#getContent()
  46. * @see java.net.URLConnection
  47. * @see java.net.URLConnection#getContent()
  48. * @see java.net.URLConnection#setContentHandlerFactory(java.net.ContentHandlerFactory)
  49. * @since JDK1.0
  50. */
  51. abstract public class ContentHandler {
  52. /**
  53. * Given a URL connect stream positioned at the beginning of the
  54. * representation of an object, this method reads that stream and
  55. * creates an object from it.
  56. *
  57. * @param urlc a URL connection.
  58. * @return the object read by the <code>ContentHandler</code>.
  59. * @exception IOException if an I/O error occurs while reading the object.
  60. */
  61. abstract public Object getContent(URLConnection urlc) throws IOException;
  62. /**
  63. * Given a URL connect stream positioned at the beginning of the
  64. * representation of an object, this method reads that stream and
  65. * creates an object that matches one of the types specified.
  66. *
  67. * The default implementation of this method should call getContent()
  68. * and screen the return type for a match of the suggested types.
  69. *
  70. * @param urlc a URL connection.
  71. * @param classes an array of types requested
  72. * @return the object read by the <code>ContentHandler</code> that is
  73. * the first match of the suggested types.
  74. * null if none of the requested are supported.
  75. * @exception IOException if an I/O error occurs while reading the object.
  76. */
  77. public Object getContent(URLConnection urlc, Class[] classes) throws IOException {
  78. Object obj = getContent(urlc);
  79. for (int i = 0; i < classes.length; i++) {
  80. if (classes[i].isInstance(obj)) {
  81. return obj;
  82. }
  83. }
  84. return null;
  85. }
  86. }