Home | | Internet Programming | Java - Content handlers

Chapter: Web or internet Programming : Java Programming

Java - Content handlers

Handling the content means converting the raw data into a format Java understands, for example, an InputStream or an AudioClip. A content handler is an instance of a subclass of java.net.ContentHandler:

Content handlers

 

       Handling the content means converting the raw data into a format Java understands, for example, an InputStream or an AudioClip. A content handler is an instance of a subclass of java.net.ContentHandler:

public abstract class ContentHandler extends Object

 

       Java can already download classes from the Internet. Thus, there isn't much magic to getting it to download a class that can understand a new content type.

 

       A content handler is just a .class file like any other. The magic is all inside the web browser, which knows when and where to request a .class file to view a new content type. Of course, some browsers are more magical than others.

 

       The only way to make this work in a browser is in conjunction with an applet that knows how to request the content handler explicitly.

 

       A content handler reads data from a URLConnection and constructs an object appropriate for the content type from the data. Each subclass of ContentHandler handles a specific MIME type and subtype, such as text/plain or image/gif. Thus, an image/gif content handler returns a URLImageSource object (a class that implements the ImageProducer interface), while a text/plain content handler returns a String. A database content handler might return a java.sql.ResultSet object.

 

The sequence of events:

1. A URL object is created that points at some Internet resource.

 

2. The URL's getContent( ) method is called to return an object representing the contents of the resource.

 

3. The getContent( ) method of the URL calls the getContent( ) method of its underlying URLConnection.

 

4. The URLConnectiongetContent( ) method calls the nonpublic method getContentHandler( ) to find a content handler for the MIME type and subtype.

 

5. getContentHandler( ) checks to see whether it already has a handler for this type in

 

its cache. If it does, that handler is returned to getContent( ). Thus, browsers won't download content handlers for common types, such as text/html, every time the user goes to a new web page.

 

6. If there wasn't an appropriate ContentHandler in the cache and the ContentHandlerFactory isn't null, getContentHandler( ) calls the ContentHandlerFactory's createContentHandler( ) method to instantiate a new ContentHandler. If this is successful, the ContentHandler object is returned to getContent( ).

 

7. If the ContentHandlerFactory is null or createContentHandler( ) fails to instantiate a new ContentHandler, then Java looks for a content handler class named type.subtype, where type is the MIME type of the content and subtype is the MIME subtype in one of the packages named in the java.content.handler.pkgs system property. If a content handler is found, it is returned. Otherwise . . .

 

8. Java looks for a content handler class named sun.net.www.content.type.subtype. If it's found, it's returned. Otherwise, createContentHandler( ) returns null.

 

9. If the ContentHandler object is not null, then this ContentHandler's getContent( ) method is called. This method returns an object appropriate for the content type. If the ContentHandler is null, an IOException is thrown.

 

10.Either the returned object or the exception is passed up the call chain, eventually reaching the method that invoked getContent( ).

 

Example

 

The full package-qualified name is com.macfaq.net.www.content.text.tab_separated_values. This unusual class name follows the naming convention for a content handler for the MIME type text/tab-separated-values. Since MIME types often contain hyphens, as in this example, a convention exists to replace these with the underscore (_). Thus text/tab-separated-values becomes text.tab_separated_values. To install this content handler, all that's needed is to put the compiled

 

.class file somewhere the class loader can find it and set the java.content.handler.pkgs property to com.macfaq.net.www.content.

 

package com.macfaq.net.www.content.text;

import java.net.*;

 

import java.io.*;

import java.util.*;

import com.macfaq.io.SafeBufferedReader;

private String[] lineToArray(String line) {

public class tab_separated_values extends ContentHandler {

 

public Object getContent(URLConnection uc) throws IOException {

String theLine;

 

Vector v = new Vector( );

InputStreamReader isr = new InputStreamReader(uc.getInputStream( ));

 

SafeBufferedReader in = new SafeBufferedReader(isr); while ((theLine = in.readLine( )) != null) {

 

String[] linearray = lineToArray(theLine);

v.addElement(linearray);

}

return v;

 

}

 

int numFields = 1;

for (int i = 0; i < line.length( ); i++) {

 

if (line.charAt(i) == '\t') numFields++;

}

 

String[] fields = new String[numFields];

int position = 0;

for (int i = 0; i < numFields; i++) {

StringBuffer buffer = new StringBuffer( );

 

while (position < line.length( ) && line.charAt(position) !='\t') { buffer.append(line.charAt(position));

position++;

}

 

fields[i] = buffer.toString( ); position++;

}

return fields;

 

}

}

Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
Web or internet Programming : Java Programming : Java - Content handlers |


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.