JXTA

net.jxta.document
Class StructuredDocumentFactory

java.lang.Object
  extended by net.jxta.util.ClassFactory<MimeMediaType,StructuredDocumentFactory.Instantiator>
      extended by net.jxta.document.StructuredDocumentFactory

public final class StructuredDocumentFactory
extends ClassFactory<MimeMediaType,StructuredDocumentFactory.Instantiator>

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.

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<MimeMediaType,StructuredDocumentFactory.Instantiator> getAssocTable()
          Used by ClassFactory methods to get the mapping of keys to constructors.
protected  Class<MimeMediaType> getClassForKey()
          Used by ClassFactory methods to ensure that all keys used with the mapping are of the correct type.
protected  Class<StructuredDocumentFactory.Instantiator> getClassOfInstantiators()
          Used by ClassFactory methods to ensure that all of the instance classes which register with this factory have the correct base class
static String getFileExtensionForMimeType(MimeMediaType mimetype)
          Returns the preferred extension for a given mime-type.
static MimeMediaType getMimeTypeForFileExtension(String extension)
          Returns the preferred mime-type for a given file extension.
static StructuredDocument newStructuredDocument(MessageElement element)
          Constructs an instance of StructuredDocument based upon the content of the provided message element.
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, registerFromFile, registerFromResources, registerFromString, registerProviders
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getAssocTable

protected Map<MimeMediaType,StructuredDocumentFactory.Instantiator> getAssocTable()
Used by ClassFactory methods to get the mapping of keys to constructors.

Specified by:
getAssocTable in class ClassFactory<MimeMediaType,StructuredDocumentFactory.Instantiator>
Returns:
the map containing the mappings.

getClassForKey

protected Class<MimeMediaType> 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<MimeMediaType,StructuredDocumentFactory.Instantiator>
Returns:
Class object of the key type.

getClassOfInstantiators

protected Class<StructuredDocumentFactory.Instantiator> getClassOfInstantiators()
Used by ClassFactory methods to ensure that all of the instance classes which register with this factory have the correct base class

Specified by:
getClassOfInstantiators in class ClassFactory<MimeMediaType,StructuredDocumentFactory.Instantiator>
Returns:
Class object of the "Factory" type.

registerAssoc

protected boolean registerAssoc(String className)
Register a class with the factory from its class name. Since class name doesn't tell us much, we just load the class and hope that something happens as a result of the class loading. This class is often overridden in class factories to interogate the instance class before registering the instance class.

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<MimeMediaType,StructuredDocumentFactory.Instantiator>
Parameters:
className - The class name which will be registered.
Returns:
boolean true if the class was registered otherwise false.

getFileExtensionForMimeType

public static String getFileExtensionForMimeType(MimeMediaType mimetype)
Returns the preferred extension for a given mime-type. If there is no mapping or no preferred extension for this MIME type 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 preferred 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 registered.

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 registered.

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 registered.
UnsupportedOperationException - if the mime-type provided is not a text oriented MIME type.

newStructuredDocument

public static StructuredDocument newStructuredDocument(MessageElement element)
                                                throws IOException
Constructs an instance of StructuredDocument based upon the content of the provided message element.

Parameters:
element - The message element from which to create the document.
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 registered.

JXSE