|
|||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||||
java.lang.Objectorg.apache.james.mime4j.MimeTokenStream
Parses MIME (or RFC822) message streams of bytes or characters. The stream is converted into an event stream.
Typical usage:
MimeTokenStream stream = new MimeTokenStream();
stream.parse(new BufferedInputStream(new FileInputStream("mime.msg")));
for (int state = stream.getState();
state != MimeTokenStream.T_END_OF_STREAM;
state = stream.next()) {
switch (state) {
case MimeTokenStream.T_BODY:
System.out.println("Body detected, contents = "
+ stream.getInputStream() + ", header data = "
+ stream.getBodyDescriptor());
break;
case MimeTokenStream.T_FIELD:
System.out.println("Header field detected: "
+ stream.getField());
break;
case MimeTokenStream.T_START_MULTIPART:
System.out.println("Multipart message detexted,"
+ " header data = "
+ stream.getBodyDescriptor());
...
}
}
NOTE: All lines must end with CRLF
(\r\n). If you are unsure of the line endings in your stream
you should wrap it in a EOLConvertingInputStream
instance.
Instances of MimeTokenStream are reusable: Invoking the
method parse(InputStream) resets the token streams internal
state. However, they are definitely not thread safe. If you
have a multi threaded application, then the suggested use is to have
one instance per thread.
| Nested Class Summary | |
static class |
MimeTokenStream.Event
Enumerates events which can be monitored. |
| Field Summary | |
static int |
M_NO_RECURSE
Do not recurse message/rfc822 parts |
static int |
M_RAW
Parse into raw entities |
static int |
M_RECURSE
Recursively parse every message/rfc822 part |
static int |
T_BODY
This token indicates, that an atomic entity is being parsed. |
static int |
T_END_BODYPART
This token indicates, that the MIME stream is currently at the end of a body part. |
static int |
T_END_HEADER
This token indicates, that part headers have now been parsed. |
static int |
T_END_MESSAGE
This token indicates, that the MIME stream is currently at the end of a message. |
static int |
T_END_MULTIPART
This token indicates, that a multipart body has been parsed. |
static int |
T_END_OF_STREAM
This token indicates, that the MIME stream has been completely and successfully parsed, and no more data is available. |
static int |
T_EPILOGUE
This token indicates, that a multiparts epilogue is being parsed. |
static int |
T_FIELD
This token indicates, that a message parts field has now been parsed. |
static int |
T_PREAMBLE
This token indicates, that a multiparts preamble is being parsed. |
static int |
T_RAW_ENTITY
This token indicates, that a raw entity is currently being processed. |
static int |
T_START_BODYPART
This token indicates, that the MIME stream is currently at the beginning of a body part. |
static int |
T_START_HEADER
This token indicates, that a message parts headers are now being parsed. |
static int |
T_START_MESSAGE
This token indicates, that the MIME stream is currently at the beginning of a message. |
static int |
T_START_MULTIPART
This token indicates, that a multipart body is being parsed. |
| Constructor Summary | |
MimeTokenStream()
Constructs a standard (lax) stream. |
|
| Method Summary | |
static MimeTokenStream |
createStrictValidationStream()
Creates a stream that strictly validates the input. |
protected void |
debug(MimeTokenStream.Event event)
Logs (at debug) an indicative message based on the given event and the current state of the system. |
BodyDescriptor |
getBodyDescriptor()
Gets a descriptor for the current entity. |
java.lang.String |
getField()
This method is valid, if getState() returns T_FIELD. |
java.lang.String |
getFieldName()
This method is valid, if getState() returns T_FIELD. |
java.lang.String |
getFieldValue()
This method is valid, if getState() returns T_FIELD. |
java.io.InputStream |
getInputStream()
This method is valid, if getState() returns either of
T_RAW_ENTITY, T_PREAMBLE, or T_EPILOGUE.
|
java.io.Reader |
getReader()
Gets a reader configured for the current body or body part. |
int |
getRecursionMode()
Gets the current recursion mode. |
int |
getState()
Returns the current state. |
boolean |
isRaw()
Determines if this parser is currently in raw mode. |
protected java.lang.String |
message(MimeTokenStream.Event event)
Creates an indicative message suitable for display based on the given event and the current state of the system. |
protected void |
monitor(MimeTokenStream.Event event)
Monitors the given event. |
protected BodyDescriptor |
newBodyDescriptor(BodyDescriptor pParent)
Creates a new instance of BodyDescriptor. |
int |
next()
This method advances the token stream to the next token. |
void |
parse(java.io.InputStream stream)
Instructs the to parse the given streams contents. |
void |
setRaw(boolean raw)
Deprecated. pass M_RAW to setRecursionMode(int) |
void |
setRecursionMode(int mode)
Sets the current recursion. |
static java.lang.String |
stateToString(int state)
Renders a state as a string suitable for logging. |
void |
stop()
Finishes the parsing and stops reading lines. |
protected void |
warn(MimeTokenStream.Event event)
Logs (at warn) an indicative message based on the given event and the current state of the system. |
| Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
public static final int T_END_OF_STREAM
public static final int T_START_MESSAGE
public static final int T_END_MESSAGE
public static final int T_RAW_ENTITY
getInputStream() to obtain the raw entity
data.
public static final int T_START_HEADER
public static final int T_FIELD
getField() to obtain the
raw field contents.
public static final int T_END_HEADER
public static final int T_START_MULTIPART
public static final int T_END_MULTIPART
public static final int T_PREAMBLE
getInputStream() to access the
preamble contents.
public static final int T_EPILOGUE
getInputStream() to access the
epilogue contents.
public static final int T_START_BODYPART
public static final int T_END_BODYPART
public static final int T_BODY
getInputStream() to access the entity contents.
public static final int M_RECURSE
message/rfc822 part
getRecursionMode(),
Constant Field Valuespublic static final int M_NO_RECURSE
message/rfc822 parts
getRecursionMode(),
Constant Field Valuespublic static final int M_RAW
getRecursionMode(),
Constant Field Values| Constructor Detail |
public MimeTokenStream()
createStrictValidationStream() to create
a stream that strictly validates the input.
| Method Detail |
public static final java.lang.String stateToString(int state)
state -
public static final MimeTokenStream createStrictValidationStream()
MimeTokenStream which throws a
MimeException whenever possible issues
are dedicated in the inputpublic void parse(java.io.InputStream stream)
public boolean isRaw()
true if in raw mode, false
otherwise.setRaw(boolean)public void setRaw(boolean raw)
M_RAW to setRecursionMode(int)
ContentHandler.raw(InputStream) handler method only.
The stream will contain the entire unparsed entity contents
including header fields and whatever is in the body.
raw - true enables raw mode, false
disables it.public int getRecursionMode()
M_RAW mode does not parse the part at all.
M_RECURSE mode recursively parses each mail
when an message/rfc822 part is encounted;
M_NO_RECURSE does not.
M_RECURSE, M_RAW or M_NO_RECURSEpublic void setRecursionMode(int mode)
M_RAW mode does not parse the part at all.
M_RECURSE mode recursively parses each mail
when an message/rfc822 part is encounted;
M_NO_RECURSE does not.
mode - M_RECURSE, M_RAW or M_NO_RECURSEpublic void stop()
ContentHandler.endMultipart(),
ContentHandler.endBodyPart(),
ContentHandler.endMessage(), etc to match previous calls
to
ContentHandler.startMultipart(BodyDescriptor),
ContentHandler.startBodyPart(),
ContentHandler.startMessage(), etc.
public int getState()
public java.lang.String getField()
getState() returns T_FIELD.
java.lang.IllegalStateException - getState() returns another
value than T_FIELD.public java.lang.String getFieldName()
getState() returns T_FIELD.
java.lang.IllegalStateException - getState() returns another
value than T_FIELD.public java.lang.String getFieldValue()
getState() returns T_FIELD.
java.lang.IllegalStateException - getState() returns another
value than T_FIELD.
protected void monitor(MimeTokenStream.Event event)
throws MimeException,
java.io.IOException
event - Event, not null
MimeException - subclasses may elect to throw this exception upon
invalid content
java.io.IOException - subclasses may elect to throw this exceptionprotected java.lang.String message(MimeTokenStream.Event event)
event - Event, not null
protected void warn(MimeTokenStream.Event event)
event - Event, not nullprotected void debug(MimeTokenStream.Event event)
event - Event, not nullpublic java.io.InputStream getInputStream()
getState() returns either of
T_RAW_ENTITY, T_PREAMBLE, or T_EPILOGUE.
It returns the raw entity, preamble, or epilogue contents.
java.lang.IllegalStateException - getState() returns an
invalid value.public java.io.Reader getReader()
getInputStream().
Consult the javadoc for that method for known limitations.
Reader, not null
java.lang.IllegalStateException - getState() returns an
invalid value
UnsupportedCharsetException - if there is no JVM support
for decoding the charset
IllegalCharsetNameException - if the charset name specified
in the mime type is illegalgetInputStream()public BodyDescriptor getBodyDescriptor()
Gets a descriptor for the current entity.
This method is valid if getState() returns:
BodyDescriptor, not nulls
public int next()
throws java.io.IOException,
MimeException
java.lang.IllegalStateException - The method has been called, although
getState() was already T_END_OF_STREAM.
java.io.IOException
MimeExceptionprotected BodyDescriptor newBodyDescriptor(BodyDescriptor pParent)
BodyDescriptor. Subclasses may override
this in order to create body descriptors, that provide more specific
information.
|
|||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||||