In this blog we will look a little bit closer behind the curtain of CL_DEMO_OUTPUT, the usage of which was introduced in Part 1.
The classes described here are not intended for productive usage. You might use them in demonstration programs, local test programs or for temporary testing in productive programs. You must not use them productively in productive programs.
XML Output Stream
While it is always CL_DEMO_OUTPUT that appears in demos and examples, the most important thing lies behind: An XML-based output stream that was invented to demonstrate how ABAP data can be written device independently into a stream for further processing. This output stream is produced by class CL_DEMO_OUTPUT_STREAM. The usage of this class can look as follows:
SELECT * FROM scarr INTO TABLE @DATA(carriers). DATA(stream) = cl_demo_output_stream=>open( ). stream->write_text( iv_text = 'XML of CL_DEMO_OUTPUT_STREAM' iv_format = if_demo_output_formats=>heading iv_level = 1 ). stream->write_data( ia_value = carriers[ 1 ]-carrid iv_name = 'FIELD' iv_format = if_demo_output_formats=>nonprop ). stream->write_data( ia_value = carriers iv_name = 'CARRIERS' iv_format = if_demo_output_formats=>nonprop ). DATA(xml) = stream->close( ). cl_abap_browser=>show_xml( xml_xstring = xml ).
The XML result is:
- An output stream is an object opened by static method OPEN.
- For filling the stream, class CL_DEMO_OUTPUT_STREAM accepts texts, elementary ABAP data, structures with elementary components and internal tables of these line types. CL_DEMO_OUTPUT_STREAM interprets each ABAP data object as a table with a structured line type – an idea taken from OData. This means for each kind of possible data input the same XML structure emerges: an object with a name, a list of components and a set of rows. An elementary data object is represented like a table with one row. This format is simpler than OData and can easily handled by a consumer and, e.g., be converted into HTML.
- An output stream can be closed by instance method CLOSE. This raises an instance event COMPLETED.
CL_DEMO_OUTPUT_STREAM demonstrates how a simple XML output stream for ABAP data might look like. The next step are examples for consumers of such a stream. Such examples are the classes CL_DEMO_OUTPUT_HTML and CL_DEMO_OUTPUT_TEXT.
An example for handling the output stream with CL_DEMO_OUTPUT_HTML can look like:
SELECT * FROM scarr INTO TABLE @DATA(carriers). DATA(stream) = cl_demo_output_stream=>open( ). SET HANDLER cl_demo_output_html=>handle_output FOR stream ACTIVATION 'X'. stream->write_text( iv_text = 'HTML for CL_DEMO_OUTPUT_STREAM' iv_format = if_demo_output_formats=>heading iv_level = 1 ). stream->write_data( ia_value = carriers[ 1 ]-carrid iv_name = 'FIELD' iv_format = if_demo_output_formats=>nonprop ). stream->write_data( ia_value = carriers iv_name = 'CARRIERS' iv_format = if_demo_output_formats=>nonprop ). DATA(xml) = stream->close( ). SET HANDLER cl_demo_output_html=>handle_output FOR stream ACTIVATION ' '.
Method HANDLE_OUTPUT of CL_DEMO_OUTPUT_HTML converts the XML stream to HTML, writes it to the ABAP memory, and displays it if a SAP GUI is available.
You can do the same with CL_DEMO_OUTPUT_TEXT in order to convert the XML stream to a simple text string. Methods HANDLE_OUTPUT of these classes also raise an event when the conversion is completed.
Putting it Together
Now that we have demonstration classes that show how an output stream can be created and handled we only need a framework that allows its comfortable usage. An that’s what CL_DEMO_OUTPUT demonstrates.
- CL_DEMO_OUTPUT is simply a wrapper for CL_DEMO_OUTPUT_STREAM, CL_DEMO_OUTPUT_HTML, and CL_DEMO_OUTPUT_TEXT.
- The methods of CL_DEMO_OUTPUT
- open and close streams
- set the handlers
- pass data to the methods of CL_DEMO_OUTPUT_STREAM
That’s basically all!
Besides that, some effort goes into retrieving the names of the ABAP data in order to pass them to the stream, but that’s a gimmick.
Examples for Usage
In the package SABAP_DEMOS_OUTPUT_STREAM you find the following programs that demonstrate the usage of the output stream:
The demonstration classes shown here serve as an example how a simple output framework could be achieved for ABAP.
- CL_DEMO_OUTPUT_STREAM creates an XML output stream. The XML format is a working example how such a stream might look like, if you do not want to use more complicated things like OData. You can easily invent other or better formats. The class CL_DEMO_OUTPUT_STREAM is a working example, for creating such a stream but its implementation is far from being marketable.
- CL_DEMO_OUTPUT_HTML, and CL_DEMO_OUTPUT_TEXT are working examples how an XML output stream can be handled. Again the implementation is far from being suited for productive usage. If you take a class as CL_DEMO_OUTPUT_STREAM as given, you can easily go and create your own handlers for that class in order to consume the output stream.
- Last but not least CL_DEMO_OUTPUT is nothing but a working example, how an output stream and its consumers might be used conveniently.
And that’s that …