org.jnetpcap.packet.format
Class JFormatter

java.lang.Object
  extended by org.jnetpcap.packet.format.JFormatter
Direct Known Subclasses:
TextFormatter, XmlFormatter

public abstract class JFormatter
extends java.lang.Object

Formats decoded contents of a JPacket for output.

Author:
Mark Bednarczyk, Sly Technologies, Inc.

Nested Class Summary
static class JFormatter.Detail
          Detail level to include in formatted output.
static class JFormatter.Priority
          Priority assigned to JFields.
static class JFormatter.Style
          Various output formatting styles for JField values.
 
Field Summary
protected  int frameIndex
          The frame index.
protected  java.util.Formatter out
          The out.
 
Constructor Summary
JFormatter()
          Instantiates a new j formatter.
JFormatter(java.lang.Appendable out)
          Creates a formatter.
JFormatter(java.lang.StringBuilder out)
          Creates a formatter.
 
Method Summary
protected  void decLevel()
          Dec level.
protected abstract  void fieldAfter(JHeader header, JField field, JFormatter.Detail detail)
          Field after.
protected abstract  void fieldBefore(JHeader header, JField field, JFormatter.Detail detail)
          Field before.
protected  void fieldNull(JHeader header, JField field, JFormatter.Detail detail)
          Field null.
 void format(JHeader header)
          Format.
 void format(JHeader header, JField field)
          Format.
 void format(JHeader header, JField field, JFormatter.Detail detail)
          Format.
 void format(JHeader header, JFormatter.Detail detail)
          Format.
 void format(JHeader header, JHeader subHeader, JFormatter.Detail detail)
          Format.
 void format(JPacket packet)
          Format.
 void format(JPacket packet, JFormatter.Detail detail)
          Formats a packet for output.
 void format(java.lang.StringBuilder out, JPacket packet)
          Formats a packet for output.
static JFormatter getDefault()
          Gets the default formatter.
protected abstract  void headerAfter(JHeader header, JFormatter.Detail detail)
          Called as the last step after the header has been formatted.
protected abstract  void headerBefore(JHeader header, JFormatter.Detail detail)
          Called as the first step before the header has been formatted.
protected  void headerNull(JHeader header, JFormatter.Detail detail)
          Header null.
protected  void incLevel(int count)
          Increment the padding level using default padding string.
protected  void incLevel(int count, char c)
          Inc level.
protected  void incLevel(java.lang.String pad)
          Inc level.
abstract  void packetAfter(JPacket packet, JFormatter.Detail detail)
          Packet after.
abstract  void packetBefore(JPacket packet, JFormatter.Detail detail)
          Packet before.
protected  void packetNull(JPacket packet, JFormatter.Detail detail)
          Packet null.
protected  java.util.Formatter pad()
          Appends a string, a pad, to the beginning of the line.
 void printf(java.lang.String format, java.lang.Object... args)
          Printf.
 void println(java.lang.String text)
          Println.
 void reset()
          If the current output device is a StringBuilder, it resets the buffer.
static void setDefault(JFormatter formatter)
          Sets the default.
static void setDefaultDisplayPayload(boolean enable)
          Sets a global flag that will enable or disable display of payload header in a packet.
static void setDefaultResolveAddress(boolean enable)
          Sets the default resolve address.
 void setDetail(JFormatter.Detail detail)
          Changes the detail level that is displayed with formatted output.
 void setDetail(JFormatter.Detail detail, int id)
          Changes the detail level that is displayed for formatted output for a specific header type.
 void setDisplayPayload(boolean enable)
          Sets weather the payload header will be part of the display of a packet.
 void setFrameIndex(int index)
          Sets the packet frame number, as an index.
 void setOutput(java.lang.Appendable out)
          Changes the output device for this formatter.
 void setOutput(java.lang.StringBuilder out)
          Changes the output device for this formatter.
 void setResolveAddresses(boolean enable)
          Sets a flag which will enable address resolutions.
protected  java.lang.String[] stylizeMultiLine(JHeader header, JField field, JFormatter.Style style, java.lang.Object value)
          Stylize multi line.
protected  java.lang.String[] stylizeMultiLine(JHeader header, JField field, java.lang.Object value)
          Stylize multi line.
protected  java.lang.String stylizeSingleLine(JHeader header, JField field, java.lang.Object value)
          Stylize single line.
protected abstract  void subHeaderAfter(JHeader header, JHeader subHeader, JFormatter.Detail detail)
          Sub header after.
protected abstract  void subHeaderBefore(JHeader header, JHeader subHeader, JFormatter.Detail detail)
          Sub header before.
 java.lang.String toString()
          To string.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

frameIndex

protected int frameIndex
The frame index.


out

protected java.util.Formatter out
The out.

Constructor Detail

JFormatter

public JFormatter()
Instantiates a new j formatter.


JFormatter

public JFormatter(java.lang.Appendable out)
Creates a formatter.

Parameters:
out - appendable device where to send output

JFormatter

public JFormatter(java.lang.StringBuilder out)
Creates a formatter.

Parameters:
out - buffer where to send output
Method Detail

getDefault

public static JFormatter getDefault()
Gets the default formatter.

Returns:
default formatter

setDefault

public static void setDefault(JFormatter formatter)
Sets the default.

Parameters:
formatter - the new default

setDefaultDisplayPayload

public static void setDefaultDisplayPayload(boolean enable)
Sets a global flag that will enable or disable display of payload header in a packet. If packet contains a payload header at the end of the packet this flag determines if the header is displayed along with the rest of the display or not. The default is to enable. This method sets a global flag for all new formatters. Any existing formatters already instantiated will not have their flag changed by this global method.

Parameters:
enable - true will enable display of payload header otherwise disable
See Also:
setDisplayPayload(boolean)

setDefaultResolveAddress

public static void setDefaultResolveAddress(boolean enable)
Sets the default resolve address.

Parameters:
enable - the new default resolve address

decLevel

protected void decLevel()
Dec level.


fieldAfter

protected abstract void fieldAfter(JHeader header,
                                   JField field,
                                   JFormatter.Detail detail)
                            throws java.io.IOException
Field after.

Parameters:
header - the header
field - the field
detail - the detail
Throws:
java.io.IOException - Signals that an I/O exception has occurred.

fieldBefore

protected abstract void fieldBefore(JHeader header,
                                    JField field,
                                    JFormatter.Detail detail)
                             throws java.io.IOException
Field before.

Parameters:
header - the header
field - the field
detail - the detail
Throws:
java.io.IOException - Signals that an I/O exception has occurred.

fieldNull

protected void fieldNull(JHeader header,
                         JField field,
                         JFormatter.Detail detail)
Field null.

Parameters:
header - the header
field - the field
detail - the detail

format

public void format(JHeader header)
            throws java.io.IOException
Format.

Parameters:
header - the header
Throws:
java.io.IOException - Signals that an I/O exception has occurred.

format

public void format(JHeader header,
                   JFormatter.Detail detail)
            throws java.io.IOException
Format.

Parameters:
header - the header
detail - the detail
Throws:
java.io.IOException - Signals that an I/O exception has occurred.

format

public void format(JHeader header,
                   JField field)
            throws java.io.IOException
Format.

Parameters:
header - the header
field - the field
Throws:
java.io.IOException - Signals that an I/O exception has occurred.

format

public void format(JHeader header,
                   JField field,
                   JFormatter.Detail detail)
            throws java.io.IOException
Format.

Parameters:
header - the header
field - the field
detail - the detail
Throws:
java.io.IOException - Signals that an I/O exception has occurred.

format

public void format(JHeader header,
                   JHeader subHeader,
                   JFormatter.Detail detail)
            throws java.io.IOException
Format.

Parameters:
header - the header
subHeader - the sub header
detail - the detail
Throws:
java.io.IOException - Signals that an I/O exception has occurred.

format

public void format(JPacket packet)
            throws java.io.IOException
Format.

Parameters:
packet - the packet
Throws:
java.io.IOException - Signals that an I/O exception has occurred.

format

public void format(JPacket packet,
                   JFormatter.Detail detail)
            throws java.io.IOException
Formats a packet for output.

Parameters:
packet - packet to format
detail - detail level
Throws:
java.io.IOException - any IO errors when sending data to default output device

format

public void format(java.lang.StringBuilder out,
                   JPacket packet)
Formats a packet for output.

Parameters:
out - string buffer to send output to
packet - packet to format

headerAfter

protected abstract void headerAfter(JHeader header,
                                    JFormatter.Detail detail)
                             throws java.io.IOException
Called as the last step after the header has been formatted.

Parameters:
header - headercurrently being formatted
detail - detail level to include
Throws:
java.io.IOException - any IO errors while sending data to output device

headerBefore

protected abstract void headerBefore(JHeader header,
                                     JFormatter.Detail detail)
                              throws java.io.IOException
Called as the first step before the header has been formatted.

Parameters:
header - headercurrently being formatted
detail - detail level to include
Throws:
java.io.IOException - any IO errors while sending data to output device

headerNull

protected void headerNull(JHeader header,
                          JFormatter.Detail detail)
Header null.

Parameters:
header - the header
detail - the detail

incLevel

protected void incLevel(int count)
Increment the padding level using default padding string.

Parameters:
count - numer of pad strings to pad

incLevel

protected void incLevel(int count,
                        char c)
Inc level.

Parameters:
count - the count
c - the c

incLevel

protected void incLevel(java.lang.String pad)
Inc level.

Parameters:
pad - the pad

packetAfter

public abstract void packetAfter(JPacket packet,
                                 JFormatter.Detail detail)
                          throws java.io.IOException
Packet after.

Parameters:
packet - the packet
detail - the detail
Throws:
java.io.IOException - Signals that an I/O exception has occurred.

packetBefore

public abstract void packetBefore(JPacket packet,
                                  JFormatter.Detail detail)
                           throws java.io.IOException
Packet before.

Parameters:
packet - the packet
detail - the detail
Throws:
java.io.IOException - Signals that an I/O exception has occurred.

packetNull

protected void packetNull(JPacket packet,
                          JFormatter.Detail detail)
Packet null.

Parameters:
packet - the packet
detail - the detail

pad

protected java.util.Formatter pad()
Appends a string, a pad, to the beginning of the line.

Returns:
this formatter

reset

public void reset()
If the current output device is a StringBuilder, it resets the buffer. Otherwise this method does nothing.


setDetail

public void setDetail(JFormatter.Detail detail)
Changes the detail level that is displayed with formatted output.

Parameters:
detail - the level of detail to set for all headers

setDetail

public void setDetail(JFormatter.Detail detail,
                      int id)
Changes the detail level that is displayed for formatted output for a specific header type.

Parameters:
detail - the level of detail set for this particular header
id - header id

setDisplayPayload

public void setDisplayPayload(boolean enable)
Sets weather the payload header will be part of the display of a packet. This is an instance method that defaults the global setting. You can change this flag on an instance by instance basis.

Parameters:
enable - if true will include payload header in the display, otherwise it will not
See Also:
setDefaultDisplayPayload(boolean)

setFrameIndex

public void setFrameIndex(int index)
Sets the packet frame number, as an index. This value will be used in display of the header. Once set to a value of 0 or more, it will be automatically incremented for every new packet frame displayed. It can be also set to new value between each format call.

Parameters:
index - initial index for frame number

setOutput

public void setOutput(java.lang.Appendable out)
Changes the output device for this formatter. Output produced will be sent to the specified device.

Parameters:
out - new formatter device

setOutput

public void setOutput(java.lang.StringBuilder out)
Changes the output device for this formatter. Output produced will be sent to the specified device.

Parameters:
out - new formatter device

setResolveAddresses

public void setResolveAddresses(boolean enable)
Sets a flag which will enable address resolutions. This is an instance method setter that will change the flag only for this instance of the formatter. The default is set to global default which is set using

Parameters:
enable - true to enable address resolution, otherwise false setDefaultResolveAddress(boolean).
See Also:
setDefaultResolveAddress(boolean)

stylizeMultiLine

protected java.lang.String[] stylizeMultiLine(JHeader header,
                                              JField field,
                                              java.lang.Object value)
Stylize multi line.

Parameters:
header - the header
field - the field
value - the value
Returns:
the string[]

stylizeMultiLine

protected java.lang.String[] stylizeMultiLine(JHeader header,
                                              JField field,
                                              JFormatter.Style style,
                                              java.lang.Object value)
Stylize multi line.

Parameters:
header - the header
field - the field
style - the style
value - the value
Returns:
the string[]

stylizeSingleLine

protected java.lang.String stylizeSingleLine(JHeader header,
                                             JField field,
                                             java.lang.Object value)
Stylize single line.

Parameters:
header - the header
field - the field
value - the value
Returns:
the string

subHeaderAfter

protected abstract void subHeaderAfter(JHeader header,
                                       JHeader subHeader,
                                       JFormatter.Detail detail)
                                throws java.io.IOException
Sub header after.

Parameters:
header - the header
subHeader - the sub header
detail - the detail
Throws:
java.io.IOException - Signals that an I/O exception has occurred.

subHeaderBefore

protected abstract void subHeaderBefore(JHeader header,
                                        JHeader subHeader,
                                        JFormatter.Detail detail)
                                 throws java.io.IOException
Sub header before.

Parameters:
header - the header
subHeader - the sub header
detail - the detail
Throws:
java.io.IOException - Signals that an I/O exception has occurred.

toString

public java.lang.String toString()
To string.

Overrides:
toString in class java.lang.Object
Returns:
the string
See Also:
Object.toString()

println

public void println(java.lang.String text)
Println.

Parameters:
text - the text

printf

public void printf(java.lang.String format,
                   java.lang.Object... args)
Printf.

Parameters:
format - the format
args - the args