es.uc3m.it.xbe32
Class XBE32Printer

java.lang.Object
  extended byes.uc3m.it.xbe32.XBE32Printer
All Implemented Interfaces:
XBE32ParseListener

public class XBE32Printer
extends Object
implements XBE32ParseListener

This auxiliary XBE32ParseListerner class prints the XBE32 Elements being parsed as XML tags.
The Elements are printed in the specified PrintStream (or in System.out by default).

For example, the following sequence of XBE32ParseListener events:

    listener.startParsing();
    listener.complexElementStart(false, 0x0001, "service", false, false);
    listener.attributeElement(false, 0x2102, "type", false, false, 
                              XBE32Element.STRING_VALUE_TYPE, "printer");
    listener.attributeElement(false, 0x2C03, "addr", false, false, 
                              XBE32Element.OPAQUE4_VALUES_TYPE, new byte[][] {{ 0xA9, 0xFE, 0x55, 0x8B }});
    listener.attributeElement(false, 0x2904, "ports", false, false, 
                              XBE32Element.INT16_VALUES_TYPE, new short[] { 515, 631 });
    listener.complexElementStart(false, 0x0003, "options", true, true);
    listener.attributeElement(true, 0x000006, "color", false, true, 
                              XBE32Element.BOOLEAN_VALUES_TYPE, new boolean[] { false });
    listener.attributeElement(true, 0, "ppm", true, false, 
                              XBE32Element.FLOAT32_VALUES_TYPE, new float[] { 20.5 });
    listener.complexElementEnd(false, 0x0003, "options", true, true);
    listener.complexElementEnd(false, 0x0001, "service", false, false);
    listener.endParsing(76);
 

It would print the following XML fragment if processed by a XBE32Printer:

    <service>
      <type> printer </type>
      <addr> 0xA9FE558B </addr>
      <ports> 515, 631 </ports>
      <options c="true" e="true">
        <color ext="true" e="true" type="boolean"> false </color>
        <ppm ext="true" c="true" type="float32"> 20.5 </ppm>
      </options>
    </service>
 

It allows a second XBE32ParseListener to be registered. Thus it is able to process the XBE32 parsing events once the XBE32 Elements are printed into the specified output stream.

This class may be extended, for example to print opaque values with a better representation than its plain hexadecimal value.

See Also:
XBE32Parser.setListener(), XBE32Element

Constructor Summary
XBE32Printer()
          Creates a new XBE32Printer to print XBE32 parse events as XML tags
 
Method Summary
 void attributeElement(boolean ext, int id, String name, boolean c, boolean e, int type, Object val)
          This method is called when an Attribute Element is processed by the XBE32Parser.
 void complexElementEnd(boolean ext, int id, String name, boolean c, boolean e)
          This method is called when a Complex Element has been just processed by the XBE32Parser.
 void complexElementStart(boolean ext, int id, String name, boolean c, boolean e)
          This method is called when a Complex Element is found by the XBE32Parser.
 void endParsing(int length)
          This method is called when the XBE32Parser successfully ends parsing the full XBE32-encoded stream.
protected  String getCloseTag(String name, boolean attr)
          Returns the XML close tag for the specified XBE32 Element
protected  String getIndent()
          Returns the leading whitespace characters according to the current indent level.
 XBE32ParseListener getListener()
          Returns the registered XBE32ParseListener, if any
 String getOpenTag(boolean ext, int id, String name, boolean c, boolean e, int type)
          Returns the XML open tag for the specified XBE32 Element.
 PrintStream getPrintStream()
          Returns the PrintStream where XBE32 Elements are printed at as XML tags
static void main(String[] args)
          XBE32Printer can be executed as an standalone command-line application.
 void printElement(XBE32Element elem)
          Prints the specified XBE32 Element with XML tags.
 void setListener(XBE32ParseListener lis)
          Sets the XBE32ParseListener that receives XBE32 parsing events
 void setPrintStream(PrintStream out)
          Sets the PrintStream to write XBE32 Elementss at as XML tags
 void startParsing()
          This method is called when the XBE32Parser.parse() method is called, thus the parsing process starts.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

XBE32Printer

public XBE32Printer()
Creates a new XBE32Printer to print XBE32 parse events as XML tags

Method Detail

getPrintStream

public PrintStream getPrintStream()
Returns the PrintStream where XBE32 Elements are printed at as XML tags

Returns:
the PrintStream in use

setPrintStream

public void setPrintStream(PrintStream out)
                    throws IllegalArgumentException
Sets the PrintStream to write XBE32 Elementss at as XML tags

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

getListener

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

Returns:
the XBE32ParseListener registered to this one

setListener

public void setListener(XBE32ParseListener lis)
Sets the XBE32ParseListener that receives XBE32 parsing events

Parameters:
lis - the XBE32ParseListener to register

printElement

public void printElement(XBE32Element elem)
Prints the specified XBE32 Element with XML tags.
If it is a Complex one, all its subelements will be also printed.

Parameters:
elem - the XBE32 Element to be printed.

startParsing

public void startParsing()
Description copied from interface: XBE32ParseListener
This method is called when the XBE32Parser.parse() method is called, thus the parsing process starts.

Specified by:
startParsing in interface XBE32ParseListener
See Also:
XBE32Parser.parse()

complexElementStart

public void complexElementStart(boolean ext,
                                int id,
                                String name,
                                boolean c,
                                boolean e)
                         throws XBE32ParseException
Description copied from interface: XBE32ParseListener
This method is called when a Complex Element is found by the XBE32Parser.

Specified by:
complexElementStart in interface XBE32ParseListener
Parameters:
ext - whether the Complex Element that to be parsed is an Extensible or a Compact one
id - the identifier of the Complex Element to be parsed
name - the name of the Complex Element to be parsed
c - the Continue flag of the Complex Element to be parsed
e - the Notify Error flag of the Complex Element to be parsed
Throws:
XBE32ParseException - the listener may throw this exception if any syntax error occurs while processing this event
See Also:
XBE32Parser.parse(), XBE32Parser.parseElement()

complexElementEnd

public void complexElementEnd(boolean ext,
                              int id,
                              String name,
                              boolean c,
                              boolean e)
                       throws XBE32ParseException
Description copied from interface: XBE32ParseListener
This method is called when a Complex Element has been just processed by the XBE32Parser.

Specified by:
complexElementEnd in interface XBE32ParseListener
Parameters:
ext - whether the Complex Element that has been parsed is an extensible or a compact one
id - the identifier of the Complex Element that has been parsed
name - the name of the Complex Element that has been parsed
c - the Continue flag of the Complex Element that has been parsed
e - the Notify Error flag of the Complex Element that has beee parsed
Throws:
XBE32ParseException - the listener may throw this exception if any syntax error occurs while processing this event
See Also:
XBE32Parser.parse(), XBE32Parser.parseElement()

attributeElement

public void attributeElement(boolean ext,
                             int id,
                             String name,
                             boolean c,
                             boolean e,
                             int type,
                             Object val)
                      throws XBE32ParseException
Description copied from interface: XBE32ParseListener
This method is called when an Attribute Element is processed by the XBE32Parser.

Specified by:
attributeElement in interface XBE32ParseListener
Parameters:
ext - whether the Attrbiute Element being parsed is an Extensible or a Compact one
id - the identifier of the Attribute Element being parsed
name - the name of the Attribute Element being parsed
c - the Continue flag of the Attribute Element being parsed
e - the Notify Error flag of the Attribute Element being parsed
type - the Type of the Attribute's Value being parsed
val - the Values field of the Attribute Element being parsed
Throws:
XBE32ParseException - the listener may throw this exception if any syntax error occurs while processing this event
See Also:
XBE32Element.OPAQUE_VALUE_TYPE, XBE32Element.STRING_VALUE_TYPE, XBE32Element.OPAQUE1_VALUES_TYPE, XBE32Element.INT8_VALUES_TYPE, XBE32Element.BOOLEAN_VALUES_TYPE, XBE32Element.OPAQUE2_VALUES_TYPE, XBE32Element.INT16_VALUES_TYPE, XBE32Element.OPAQUE4_VALUES_TYPE, XBE32Element.INT32_VALUES_TYPE, XBE32Element.FLOAT32_VALUES_TYPE, XBE32Element.OPAQUE8_VALUES_TYPE, XBE32Element.INT64_VALUES_TYPE, XBE32Element.FLOAT64_VALUES_TYPE, XBE32Element.OPAQUE12_VALUES_TYPE, XBE32Element.OPAQUE16_VALUES_TYPE, XBE32Parser.parse(), XBE32Parser.parseElement()

endParsing

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

Specified by:
endParsing in interface XBE32ParseListener
See Also:
XBE32Parser.parse()

getIndent

protected String getIndent()
Returns the leading whitespace characters according to the current indent level.

Returns:
the current indent String

getOpenTag

public String getOpenTag(boolean ext,
                         int id,
                         String name,
                         boolean c,
                         boolean e,
                         int type)
Returns the XML open tag for the specified XBE32 Element.

Parameters:
ext - whether the Element is an Extensible or a Compact one
id - the identifier of the Element
name - the name of the Element
c - the Continue flag of the Element
e - the Notify Error flag of the Element
type - the Type of the Element
Returns:
the open tag String

getCloseTag

protected String getCloseTag(String name,
                             boolean attr)
Returns the XML close tag for the specified XBE32 Element

Parameters:
name - the Element name
attr - whether the specified Element is an Attribute or a Complex one
Returns:
the close tag String

main

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

Parameters:
args - Command line parameters