1. /*
  2. * @(#)ContentHandler.java 1.14 00/02/02
  3. *
  4. * Copyright 1995-2000 Sun Microsystems, Inc. All Rights Reserved.
  5. *
  6. * This software is the proprietary information of Sun Microsystems, Inc.
  7. * Use is subject to license terms.
  8. *
  9. */
  10. package java.net;
  11. import java.io.IOException;
  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 the delegation chain of the caller,
  42. * the JVM will need the RuntimePermission "getClassLoader".
  43. *
  44. * @author James Gosling
  45. * @version 1.14, 02/02/00
  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 getContent(URLConnection urlc) throws IOException;
  65. /**
  66. * Given a URL connect stream positioned at the beginning of the
  67. * representation of an object, this method reads that stream and
  68. * creates an object that matches one of the types specified.
  69. *
  70. * The default implementation of this method should call getContent()
  71. * and screen the return type for a match of the suggested types.
  72. *
  73. * @param urlc a URL connection.
  74. * @param classes an array of types requested
  75. * @return the object read by the <code>ContentHandler</code> that is
  76. * the first match of the suggested types.
  77. * null if none of the requested are supported.
  78. * @exception IOException if an I/O error occurs while reading the object.
  79. */
  80. public Object getContent(URLConnection urlc, Class[] classes) throws IOException {
  81. Object obj = getContent(urlc);
  82. for (int i = 0; i < classes.length; i++) {
  83. if (classes[i].isInstance(obj)) {
  84. return obj;
  85. }
  86. }
  87. return null;
  88. }
  89. }