es.uc3m.it.xbe32.tlv
Class TLVPrinter

java.lang.Object
  extended byes.uc3m.it.xbe32.tlv.TLVPrinter
All Implemented Interfaces:
TLVParseListener

public class TLVPrinter
extends Object
implements TLVParseListener

This auxiliary TLVParseListener class prints the fields of the TLVs being parsed from an XBE32-encoded stream.
The TLVs are printed in the specified PrintStream (or in System.out by default).

For example, a XBE32-encoded stream that generates the following TLVParseListener events when parsed by a TLVParser:

        listener.startParsing();
        listener.complexTLVStart(0xDFFF, 0);
        listener.simpleTLV(0x2CFF, 8, new byte[][] {{ 0x10, 0x00, 0x00, 0x01 }});
        listener.simpleTLV(0xA602, 5, new boolean[] { true });
        listener.complexTLVStart(0x1F00, 28);
        listener.simpleTLV(0x21FF, 7, "áb");
        listener.simpleTLV(0x2900, 8, new short[] { -32768, 0 });
        listener.simpleTLV(0x2900, 6, new short[] {  32767 });
        listener.complexTLVEnd(0x1F00, 28);
        listener.simpleTLV(0x7204, 12, new double[] { 4.9E-324 });
        listener.complexTLVEnd(0x1FFF, 64);
        listener.endParsing(64);
 

The TLVPrinter would print the following text at the specified PrintStream:

         0                   1                   2                   3
         0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
        +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
0       |C|E|    <EXT COMPLEX TLV>      |      <UNSPECIFIED LENGTH>     |  DFFF 0000
        +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
4       |           <Ext Id TLV>        |          Length = 8           |  2CFF 0008
        +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
8       |                          0x00000001                           |  0000 0001
        +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
12      |C|       Type = 0xA602         |          Length = 5           |  A602 0005
        +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
16      |      true     |                                               |  FF00 0000
        +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
20      |       <EXT ATTRIBUTE TLV>     |          LENGTH = 28          |  1F00 001C
        +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
24      |          <Ext Name TLV>       |          Length = 7           |  21FF 0007
        +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
28      |      0xC2     |      0x81     |       'b'     |               |  C281 6200
        +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
32      |       <Ext Int16 Value TLV>   |          Length = 8           |  2900 0008
        +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
36      |            -32768             |               0               |  8000 0000
        +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
40      |       <Ext Int16 Value TLV>   |          Length = 6           |  2900 0006
        +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
44      |             32767             |                               |  7FFF 0000
        +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
48      | |E|     Type = 0x7204         |          Length = 12          |  7204 000C
        +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
52      |                                                               |  0000 0000
        +                           4.9E-324                            +
56      |                                                               |  0000 0001
        +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
60      |      <END-OF-TLV 0xDFFF>      |          LENGTH = 4           |  0000 0004
        +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 

It allows a second TLVParseListener to be registered. Thus it is able to process the TLV parsing events once the TLVs are printed into the specified output stream.

Finally, a XBE32Dictionary could be also set to translate the TLV's Type field into a human-readable name.

See Also:
TLVParser.setListener(), TLV

Constructor Summary
TLVPrinter(TLVParser parser)
          Creates a new TLVPrinter for the specified TLVParser
 
Method Summary
 void complexTLVEnd(int type, int length)
          This method is called when a Complex TLV has just been processed by the TLVParser.
 void complexTLVStart(int type, int length)
          This method is called when a Complex TLV is found by the TLVParser.
 void endParsing(int length)
          This method is called when the TLVParser successfully ends parsing the full XBE32-encoded stream.
 XBE32Dictionary getDictionary()
          Returns the dictionary employed to translate TLV Types into a human-readable name
 TLVParseListener getListener()
          Returns the registered TLVParseListener, if any
 PrintStream getPrintStream()
          Returns the PrintStream where TLVs are printed in
static void main(String[] args)
          TLVPrinter can be executed as an standalone command-line application.
 void setDictionary(XBE32Dictionary dict)
          Sets the dictionary to be employed to translate TLV Types into a human-readable name
 void setListener(TLVParseListener lis)
          Sets the TLVParseListener that receives TLV parsing events
 void setPrintStream(PrintStream out)
          Sets the PrintStream to write TLVs at
 void simpleTLV(int type, int length, Object value)
          This method is called when a Simple TLV is processed by the TLVParser.
 void startParsing()
          This method is called when the TLVParser.parse() method is called and the parsing process starts.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

TLVPrinter

public TLVPrinter(TLVParser parser)
Creates a new TLVPrinter for the specified TLVParser

Parameters:
parser - the TLVParser to register at
Method Detail

getListener

public TLVParseListener getListener()
Returns the registered TLVParseListener, if any

Returns:
the TLVParseListener registered to this one

setListener

public void setListener(TLVParseListener lis)
Sets the TLVParseListener that receives TLV parsing events

Parameters:
lis - the TLVParseListener to register

getDictionary

public XBE32Dictionary getDictionary()
Returns the dictionary employed to translate TLV Types into a human-readable name

Returns:
the current dictionary, if any

setDictionary

public void setDictionary(XBE32Dictionary dict)
Sets the dictionary to be employed to translate TLV Types into a human-readable name

Parameters:
dict - the new dictionary

getPrintStream

public PrintStream getPrintStream()
Returns the PrintStream where TLVs are printed in

Returns:
the PrintStream in use

setPrintStream

public void setPrintStream(PrintStream out)
                    throws IllegalArgumentException
Sets the PrintStream to write TLVs at

Parameters:
out - the PrintStream to use
Throws:
IllegalArgumentException - if out is null

startParsing

public void startParsing()
Description copied from interface: TLVParseListener
This method is called when the TLVParser.parse() method is called and the parsing process starts.

Specified by:
startParsing in interface TLVParseListener
See Also:
TLVParser.parse()

complexTLVStart

public void complexTLVStart(int type,
                            int length)
                     throws TLVParseException
Description copied from interface: TLVParseListener
This method is called when a Complex TLV is found by the TLVParser.

Specified by:
complexTLVStart in interface TLVParseListener
Parameters:
type - the Type field of the Complex TLV to be parsed
length - the Length field of the Complex TLV to be parsed
Throws:
TLVParseException - the listener may throw this exception if any syntax error occurs while processing this event
See Also:
TLVParser.parse(), TLVParser.parseTLV()

complexTLVEnd

public void complexTLVEnd(int type,
                          int length)
                   throws TLVParseException
Description copied from interface: TLVParseListener
This method is called when a Complex TLV has just been processed by the TLVParser.

Specified by:
complexTLVEnd in interface TLVParseListener
Parameters:
type - the Type field of the Complex TLV that has been parsed
length - the Length field of the Complex TLV thas has been parsed
Throws:
TLVParseException - the listener may throw this exception if any syntax error occurs while processing this event
See Also:
TLVParser.parse(), TLVParser.parseTLV()

simpleTLV

public void simpleTLV(int type,
                      int length,
                      Object value)
               throws TLVParseException
Description copied from interface: TLVParseListener
This method is called when a Simple TLV is processed by the TLVParser.

Specified by:
simpleTLV in interface TLVParseListener
Parameters:
type - the Type field of the Simple TLV being parsed
length - the Length field of the Simple TLV being parsed
value - the Values field of the Simple TLV being parsed
Throws:
TLVParseException - the listener may throw this exception if any syntax error occurs while processing this event
See Also:
TLVParser.parse(), TLVParser.parseTLV()

endParsing

public void endParsing(int length)
Description copied from interface: TLVParseListener
This method is called when the TLVParser successfully ends parsing the full XBE32-encoded stream.

Specified by:
endParsing in interface TLVParseListener
See Also:
TLVParser.parse()

main

public static void main(String[] args)
TLVPrinter can be executed as an standalone command-line application.
It reads the specified XBE32-encoded file and prints it at the standard output.
    Usage: TLVPrinter <xbe32> [<dict>]
      <xbe32> : XBE32 encoded file to be printed
               (Use '-' to read from stdin).
      <dict> : Properties file with all the 'name=id' mappings.
     

Parameters:
args - Command line parameters