com.hunnysoft.jmime
Class Body

java.lang.Object
  extended by com.hunnysoft.jmime.Node
      extended by com.hunnysoft.jmime.Body
All Implemented Interfaces:
java.lang.Cloneable

public class Body
extends Node

Class that represents the body of a message or body part.

Body represents a body, as described in RFC 2045. A body is always part of an entity, which may be either a message or a body part. If the content type of an entity is "multipart/*", then the body contains one or more body parts. If the content type is "message/rfc822", then the body contains an encapsulated message. For all other content types, the body contains a string of bytes, which may be text or some other kind of binary data..

In Hunny JMIME, a Body object is contained in an Entity object. The Body object may contain a discrete body consisting only of a string of bytes, or it may be a composite body, consisting of several contained BodyPart objects or a single contained Message object. The only reliable way to determine the type of Body is to access the Content-Type header field from the Headers object of the Entity that contains it. For this reason, a Body should always be part of a Entity.

In the document tree representation of a message, a Body object can be an intermediate node, having both a parent node and one or more child nodes, or a leaf node, having a parent but no child nodes. In either case, the parent node is the Entity object that contains it. If it is an intermediate node, it must be of type "multipart/*" with BodyPart objects as child nodes, or of type "message/rfc822" with a single Message object as its child node. (See the documentation for Node for a discussion of the tree representation of a message.)

Normally, you do not create a Body object directly, but you access it through the Entity.body() method of Entity, which creates the Body object for you.

To add a BodyPart to a multipart Body, call the addBodyPart(BodyPart) method or insertBodyPartAt(int,BodyPart) method. To get the number of body parts, call numBodyParts(). To access a particular body part, call bodyPartAt(int). To remove a single body part, call removeBodyPartAt(int). To remove and delete all body parts, call deleteAllBodyParts().

To get the Message object contained in a Body object with "message/rfc822" content type, call message(). To set the contained message, call setMessage(Message).


Field Summary
 
Fields inherited from class com.hunnysoft.jmime.Node
mIsModified, mParent, mString
 
Constructor Summary
Body()
          Default constructor.
Body(Body other)
          Copy constructor.
Body(ByteString bstr)
          Constructor that takes an initial byte string argument.
 
Method Summary
 void _parse(int depth)
           
 void addBodyPart(BodyPart part)
          Adds a body part to the end of the list.
 void assemble()
          Assembles the string representation.
 BodyPart bodyPartAt(int index)
          Gets the body part at the specified position.
 java.lang.Object clone()
          Creates a copy of this object.
 void deleteAllBodyParts()
          Deletes all body parts in the list.
 ByteString epilogue()
          Gets the epilogue of a multipart body.
 void insertBodyPartAt(int index, BodyPart part)
          Inserts a body part at the specified position.
 Message message()
          Gets the contained message (for type "message/rfc822" only).
 int numBodyParts()
          Gets the number of body parts in the list.
 void parse()
          Parses the string representation.
 ByteString preamble()
          Gets the preamble of a multipart body.
 BodyPart removeBodyPartAt(int index)
          Removes the body part at the specified position.
 void setEpilogue(ByteString epilogue)
          Sets the epilogue of a multipart body.
 void setMessage(Message message)
          Sets the contained message (for type "message/rfc822" only).
 void setPreamble(ByteString preamble)
          Sets the preamble of a multipart body.
 
Methods inherited from class com.hunnysoft.jmime.Node
_setString, isModified, parent, setModified, setParent, setString, string, toString
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Body

public Body()
Default constructor.

This constructor sets the Body object's string representation to the empty string and sets its parent node to null.


Body

public Body(Body other)
Copy constructor.

This constructor performs a deep copy of its argument. The parent node of the new Body object is set to null.

Parameters:
other - Body instance to copy

Body

public Body(ByteString bstr)
Constructor that takes an initial byte string argument.

This constructor sets the Body object's string representation to bstr and sets its parent node to null.

Normally, you call the parse() method immediately after this constructor to create the broken-down representation.

Parameters:
bstr - initial value for the string representation
Method Detail

clone

public java.lang.Object clone()
Creates a copy of this object.

This method, inherited from Object, creates a new Body object that has the same value as this Body object. The parent node of the new Body object is set to null.

Overrides:
clone in class Node
Returns:
a copy of this object

parse

public void parse()
Parses the string representation.

This method, inherited from Node, executes the parse operation for Body objects. The parse operation creates or updates the broken-down representation from the string representation. For a multipart Body object, the parse method creates a collection of BodyPart objects. For a message/rfc822 Body, the parse method creates a single Message object. For any other type of Body, the parse method does nothing.

This method calls the parse() method of any objects it creates.

You should call this method after you set or modify the string representation, and before you access a contained BodyPart or Message.

This method clears the is-modified flag.

Note: If the Body object has no parent node -- that is, it is not contained by an Entity object -- then the parse method does nothing, since it is unable to determine the type of body.

Specified by:
parse in class Node
See Also:
Node.assemble()

_parse

public void _parse(int depth)

assemble

public void assemble()
Assembles the string representation.

This method, inherited from Node, executes the assemble operation for Body objects. The assemble operation creates or updates the string representation from the broken-down representation. Only Body objects with content type of "multipart/*" or "message/rfc822" require assembling. In either case, the Body object must be able to find the headers of the message or body part that contains it. Therefore, if the Body object is not the child of an Entity (that is, a Message or BodyPart) object, the Body cannot be assembled because the content type cannot be determined.

This method calls the assemble() method of any BodyPart or Message object it contains.

You should call this method after you add a BodyPart object to a "multipart/*" body, or add a Message object to a "message/rfc822" body, and before you access the object's string representation.

This method clears the is-modified flag.

Specified by:
assemble in class Node
See Also:
Node.parse()

numBodyParts

public int numBodyParts()
Gets the number of body parts in the list.

Returns:
number of body parts in the list

addBodyPart

public void addBodyPart(BodyPart part)
Adds a body part to the end of the list.

Parameters:
part - BodyPart object to add

deleteAllBodyParts

public void deleteAllBodyParts()
Deletes all body parts in the list.


bodyPartAt

public BodyPart bodyPartAt(int index)
Gets the body part at the specified position.

Parameters:
index - position of the BodyPart object to get (0 <= index < numBodyParts())
Returns:
BodyPart object at the specified position
Throws:
java.lang.IndexOutOfBoundsException - if index is out of range

insertBodyPartAt

public void insertBodyPartAt(int index,
                             BodyPart part)
Inserts a body part at the specified position.

Inserting at position 0 inserts the body part at the beginning. Inserting at position numBodyParts() appends the body part to the end.

Parameters:
index - position in list to insert the BodyPart object (0 <= index <= numBodyParts())
part - BodyPart object to insert
Throws:
java.lang.IndexOutOfBoundsException - if index is out of range

removeBodyPartAt

public BodyPart removeBodyPartAt(int index)
Removes the body part at the specified position.

Parameters:
index - position of the BodyPart object to remove (0 <= index < numBodyParts())
Returns:
the removed BodyPart object
Throws:
java.lang.IndexOutOfBoundsException - if index is out of range

message

public Message message()
Gets the contained message (for type "message/rfc822" only).

If the media type of the message or body part that contains this body is "message/rfc822", then the body contains an encapsulated message. Use this method to get the encapsulated message.

Returns:
the Message object; or null if there is no Message object

setMessage

public void setMessage(Message message)
Sets the contained message (for type "message/rfc822" only).

Parameters:
message - the Message object

preamble

public ByteString preamble()
Gets the preamble of a multipart body.

The preamble (a term from the MIME standard) is the optional text that appears before the first boundary in a multipart message. The text typically contains a message intended for users who use mail user agents that do not support MIME. In many cases, the preamble is empty.

Returns:
the preamble of the body

setPreamble

public void setPreamble(ByteString preamble)
Sets the preamble of a multipart body.

Parameters:
preamble - the preamble of the body

epilogue

public ByteString epilogue()
Gets the epilogue of a multipart body.

The epilogue (a term from the MIME standard) is the optional text that appears after the final boundary in a multipart message. In most cases, the epilogue is empty. Any use of the epilogue by applications is non-standard.

Returns:
the epilogue of the body

setEpilogue

public void setEpilogue(ByteString epilogue)
Sets the epilogue of a multipart body.

Parameters:
epilogue - the epilogue of the body