Project JXTA

net.jxta.document
Class StructuredDocumentFactory

java.lang.Object
  extended by net.jxta.util.ClassFactory
      extended by net.jxta.document.StructuredDocumentFactory

public final class StructuredDocumentFactory
extends ClassFactory

A factory for constructing instances of StructuredDocument. Behind the scenes, it also provides for the registration of the mime-types and constructors needed to accomplish the construction. All supported mime-types will need to register their implementation in this factory.

The configuration is done via the property net.jxta.impl.config.StructuredDocumentInstanceTypes

See Also:
Document, StructuredTextDocument, StructuredDocument, MimeMediaType

Nested Class Summary
static interface StructuredDocumentFactory.Instantiator
          Interface for instantiators of StructuredDocuments
static interface StructuredDocumentFactory.TextInstantiator
          Interface for instantiators of StructuredTextDocuments
 
Method Summary
protected  Map getAssocTable()
          Used by ClassFactory methods to get the mapping of Mime Types to constructors.
protected  Class getClassForKey()
          Used by ClassFactory methods to ensure that all keys used with the mapping are of the correct type.
protected  Class getClassOfInstantiators()
          Used by ClassFactory methods to ensure that all of the instantiators which are registered with this factory have the correct interface.
static String getFileExtensionForMimeType(MimeMediaType mimetype)
          Returns the prefered extension for a given mime-type.
static MimeMediaType getMimeTypeForFileExtension(String extension)
          Returns the prefered mime-type for a given file extension.
static StructuredDocument newStructuredDocument(MimeMediaType mimetype, InputStream stream)
          Constructs an instance of StructuredDocument matching the mime-type specified by the mimetype parameter.
static StructuredDocument newStructuredDocument(MimeMediaType mimetype, Reader reader)
          Constructs an instance of StructuredDocument matching the mime-type specified by the mimetype parameter.
static StructuredDocument newStructuredDocument(MimeMediaType mimetype, String doctype)
          Constructs an instance of StructuredDocument matching the mime-type specified by the mimetype parameter.
static StructuredDocument newStructuredDocument(MimeMediaType mimetype, String doctype, String value)
          Constructs an instance of StructuredDocument matching the mime-type specified by the mimetype parameter.
protected  boolean registerAssoc(String className)
          Register a class with the factory from its class name.
static boolean registerInstantiator(MimeMediaType mimetype, StructuredDocumentFactory.Instantiator instantiator)
          Register an instantiator object a mime-type of documents to be constructed.
 
Methods inherited from class net.jxta.util.ClassFactory
getAvailableKeys, getEntrySet, getInstantiator, registerAssoc, registerFromResources, registerFromString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getAssocTable

protected Map getAssocTable()
Used by ClassFactory methods to get the mapping of Mime Types to constructors.

Specified by:
getAssocTable in class ClassFactory
Returns:
Hastable the hashtable containing the mappings.

getClassForKey

protected Class getClassForKey()
Used by ClassFactory methods to ensure that all keys used with the mapping are of the correct type.

Specified by:
getClassForKey in class ClassFactory
Returns:
Class object of the key type.

getClassOfInstantiators

protected Class getClassOfInstantiators()
Used by ClassFactory methods to ensure that all of the instantiators which are registered with this factory have the correct interface.

Specified by:
getClassOfInstantiators in class ClassFactory
Returns:
Class object of the "Factory" type.

registerAssoc

protected boolean registerAssoc(String className)
Register a class with the factory from its class name. We override the standard implementation to get the mime type from the class and use that as the key to register the class with the factory.

Overrides:
registerAssoc in class ClassFactory
Parameters:
className - The class name which will be regiestered.
Returns:
boolean true if the class was registered otherwise false.

getFileExtensionForMimeType

public static String getFileExtensionForMimeType(MimeMediaType mimetype)
Returns the prefered extension for a given mime-type. If there is no mapping or no prefered extension for this mimetype then null is returned.

Parameters:
mimetype - the MimeMediaType we wish to know the file extension for.
Returns:
String containing the extension or null for mime-types with no known association.

getMimeTypeForFileExtension

public static MimeMediaType getMimeTypeForFileExtension(String extension)
Returns the prefered mime-type for a given file extension. If there is no mapping then null is returned.

Parameters:
extension - The extension we wish to know the mime-type for.
Returns:
MimeMediaType associated with this file extension.

registerInstantiator

public static boolean registerInstantiator(MimeMediaType mimetype,
                                           StructuredDocumentFactory.Instantiator instantiator)
Register an instantiator object a mime-type of documents to be constructed.

Parameters:
mimetype - the mime-type associated.
instantiator - the instantiator that wants to be registered..
Returns:
boolean true if the instantiator for this mime-type is now registered. If there was already an instantiator this mime-type then false will be returned.
Throws:
SecurityException - there were permission problems registering the instantiator.

newStructuredDocument

public static StructuredDocument newStructuredDocument(MimeMediaType mimetype,
                                                       String doctype)
Constructs an instance of StructuredDocument matching the mime-type specified by the mimetype parameter. The doctype parameter identifies the base type of the StructuredDocument.

Parameters:
mimetype - Specifies the mime media type to be associated with the StructuredDocument to be created.
doctype - Specifies the root type of the StructuredDocument to be created.
Returns:
StructuredDocument The instance of StructuredDocument or null if it could not be created.
Throws:
NoSuchElementException - invalid mime-media-type

newStructuredDocument

public static StructuredDocument newStructuredDocument(MimeMediaType mimetype,
                                                       String doctype,
                                                       String value)
Constructs an instance of StructuredDocument matching the mime-type specified by the mimetype parameter. The doctype parameter identifies the base type of the StructuredDocument. Value supplies a value for the root element.

Parameters:
mimetype - Specifies the mime media type to be associated with the StructuredDocument to be created.
doctype - Specifies the root type of the StructuredDocument to be created.
value - Specifies a value for the root element.
Returns:
StructuredDocument The instance of StructuredDocument or null if it could not be created.
Throws:
NoSuchElementException - if the mime-type has not been registerd.

newStructuredDocument

public static StructuredDocument newStructuredDocument(MimeMediaType mimetype,
                                                       InputStream stream)
                                                throws IOException
Constructs an instance of StructuredDocument matching the mime-type specified by the mimetype parameter. The doctype parameter identifies the base type of the StructuredDocument.

Parameters:
mimetype - Specifies the mime media type to be associated with the StructuredDocument to be created.
stream - Contains an InputStream from which the document will be constructed.
Returns:
StructuredDocument The instance of StructuredDocument or null if it could not be created.
Throws:
IOException - If there is a problem reading from the stream.
NoSuchElementException - if the mime-type has not been registerd.

newStructuredDocument

public static StructuredDocument newStructuredDocument(MimeMediaType mimetype,
                                                       Reader reader)
                                                throws IOException
Constructs an instance of StructuredDocument matching the mime-type specified by the mimetype parameter. The doctype parameter identifies the base type of the StructuredDocument.

Parameters:
mimetype - Specifies the mime media type to be associated with the StructuredDocument to be created.
reader - A Reader from which the document will be constructed.
Returns:
StructuredDocument The instance of StructuredDocument or null if it could not be created.
Throws:
IOException - If there is a problem reading from the stream.
NoSuchElementException - if the mime-type has not been registerd.
UnsupportedOperationException - if the mime-type provided is not a text oriented mimetype.

JXTA J2SE