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
           
protected  java.util.Formatter out
           
 
Constructor Summary
JFormatter()
           
JFormatter(java.lang.Appendable out)
          Creates a formatter.
JFormatter(java.lang.StringBuilder out)
          Creates a formatter.
 
Method Summary
protected  void analysisAfter(JHeader header, JAnalysis analysis, JFormatter.Detail detail)
           
protected  void analysisBefore(JHeader header, JAnalysis analysis, JFormatter.Detail detail)
           
protected  void decLevel()
           
protected abstract  void fieldAfter(JHeader header, JField field, JFormatter.Detail detail)
           
protected abstract  void fieldBefore(JHeader header, JField field, JFormatter.Detail detail)
           
protected  void fieldNull(JHeader header, JField field, JFormatter.Detail detail)
           
 void format(JHeader header)
           
 void format(JHeader header, JAnalysis analysis, JAnalysis subAnalysis, JFormatter.Detail detail)
           
 void format(JHeader header, JAnalysis analysis, JFormatter.Detail detail)
           
 void format(JHeader header, JField field)
           
 void format(JHeader header, JField field, JFormatter.Detail detail)
           
 void format(JHeader header, JFormatter.Detail detail)
           
 void format(JHeader header, JHeader subHeader, JFormatter.Detail detail)
           
 void format(JPacket packet)
           
 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)
           
protected  void incLevel(int count)
          Increment the padding level using default padding string
protected  void incLevel(int count, char c)
           
protected  void incLevel(java.lang.String pad)
           
abstract  void packetAfter(JPacket packet, JFormatter.Detail detail)
           
abstract  void packetBefore(JPacket packet, JFormatter.Detail detail)
           
protected  void packetNull(JPacket packet, JFormatter.Detail detail)
           
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)
           
 void println(java.lang.String text)
           
 void reset()
          If the current output device is a StringBuilder, it resets the buffer.
static void setDefault(JFormatter formatter)
           
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)
           
 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)
           
protected  java.lang.String[] stylizeMultiLine(JHeader header, JField field, java.lang.Object value)
           
protected  java.lang.String stylizeSingleLine(JHeader header, JField field, java.lang.Object value)
           
protected  void subAnalysisAfter(JHeader header, JAnalysis analysis, JAnalysis subAnalysis, JFormatter.Detail detail)
           
protected  void subAnalysisBefore(JHeader header, JAnalysis analysis, JAnalysis subAnalysis, JFormatter.Detail detail)
           
protected abstract  void subHeaderAfter(JHeader header, JHeader subHeader, JFormatter.Detail detail)
           
protected abstract  void subHeaderBefore(JHeader header, JHeader subHeader, JFormatter.Detail detail)
           
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

frameIndex

protected int frameIndex

out

protected java.util.Formatter out
Constructor Detail

JFormatter

public JFormatter()

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)
Parameters:
formatter -

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)

analysisAfter

protected void analysisAfter(JHeader header,
                             JAnalysis analysis,
                             JFormatter.Detail detail)
                      throws java.io.IOException
Parameters:
analysis -
detail -
Throws:
java.io.IOException

analysisBefore

protected void analysisBefore(JHeader header,
                              JAnalysis analysis,
                              JFormatter.Detail detail)
                       throws java.io.IOException
Parameters:
analysis -
detail -
Throws:
java.io.IOException

decLevel

protected void decLevel()

fieldAfter

protected abstract void fieldAfter(JHeader header,
                                   JField field,
                                   JFormatter.Detail detail)
                            throws java.io.IOException
Parameters:
header -
field -
detail -
Throws:
java.io.IOException

fieldBefore

protected abstract void fieldBefore(JHeader header,
                                    JField field,
                                    JFormatter.Detail detail)
                             throws java.io.IOException
Parameters:
header -
field -
detail -
Throws:
java.io.IOException

fieldNull

protected void fieldNull(JHeader header,
                         JField field,
                         JFormatter.Detail detail)
Parameters:
packet -
detail -

format

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

format

public void format(JHeader header,
                   JFormatter.Detail detail)
            throws java.io.IOException
Parameters:
header -
detail -
Throws:
java.io.IOException

format

public void format(JHeader header,
                   JAnalysis analysis,
                   JFormatter.Detail detail)
            throws java.io.IOException
Parameters:
header -
analysis -
detail -
Throws:
java.io.IOException

format

public void format(JHeader header,
                   JAnalysis analysis,
                   JAnalysis subAnalysis,
                   JFormatter.Detail detail)
            throws java.io.IOException
Parameters:
header -
analysis -
subAnalysis -
Throws:
java.io.IOException

format

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

format

public void format(JHeader header,
                   JField field,
                   JFormatter.Detail detail)
            throws java.io.IOException
Parameters:
header -
field -
detail -
Throws:
java.io.IOException

format

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

format

public void format(JPacket packet)
            throws java.io.IOException
Parameters:
packet -
Throws:
java.io.IOException

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)
Parameters:
packet -
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)
Parameters:
count -
c -

incLevel

protected void incLevel(java.lang.String pad)
Parameters:
pad -

packetAfter

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

packetBefore

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

packetNull

protected void packetNull(JPacket packet,
                          JFormatter.Detail detail)
Parameters:
packet -
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 setDefaultResolveAddress(boolean).

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

stylizeMultiLine

protected java.lang.String[] stylizeMultiLine(JHeader header,
                                              JField field,
                                              java.lang.Object value)
Parameters:
header -
field -
value -
Returns:

stylizeMultiLine

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

stylizeSingleLine

protected java.lang.String stylizeSingleLine(JHeader header,
                                             JField field,
                                             java.lang.Object value)
Parameters:
header -
field -
value -
Returns:

subAnalysisAfter

protected void subAnalysisAfter(JHeader header,
                                JAnalysis analysis,
                                JAnalysis subAnalysis,
                                JFormatter.Detail detail)
                         throws java.io.IOException
Parameters:
header -
analysis -
subAnalysis -
Throws:
java.io.IOException

subAnalysisBefore

protected void subAnalysisBefore(JHeader header,
                                 JAnalysis analysis,
                                 JAnalysis subAnalysis,
                                 JFormatter.Detail detail)
                          throws java.io.IOException
Parameters:
header -
analysis -
subAnalysis -
Throws:
java.io.IOException

subHeaderAfter

protected abstract void subHeaderAfter(JHeader header,
                                       JHeader subHeader,
                                       JFormatter.Detail detail)
                                throws java.io.IOException
Parameters:
header -
subHeader -
detail -
Throws:
java.io.IOException

subHeaderBefore

protected abstract void subHeaderBefore(JHeader header,
                                        JHeader subHeader,
                                        JFormatter.Detail detail)
                                 throws java.io.IOException
Parameters:
header -
subHeader -
detail -
Throws:
java.io.IOException

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

println

public void println(java.lang.String text)
Parameters:
text -

printf

public void printf(java.lang.String format,
                   java.lang.Object... args)
Parameters:
text -