您的位置:首页 > 编程语言 > PHP开发

ABAP CL_DEMO_OUTPUT类

2016-06-03 14:11 609 查看
原文:http://scn.sap.com/community/abap/blog/2016/05/10/cldemooutput-part-1-of-2--usage

好吧 没啥好翻译的。。看代码,看效果图。。ok。

 


Disclaimer(免责声明)

 

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.

在这里介绍的类目的不是为了在生产机上使用。你可以在示例程序,本地测试程序或者生产机程序里临时测试的时候使用它们。在生产机程序上你不能有效的使用它们。(注:这个类主要是来方便我们输出结果测试用的,真正展示一般不会用这个方法。)



Motivation(动机)

 

From the beginning, ABAP lacked a simple way of outputting data for test or demonstration purposes (System.out.println(...)in
Java or printf in C so to say). In the old SAP GUI days of classical dynpros you could use the
MESSAGE statement for short outputs in information messages or you mainly (mis)used the WRITE statemtent
for creating list output. Simple and straightforward - but only in executable programs (reports)! Already in classical dialog programs this kind of WRITE output
failed. Even worse in service oriented programming using classes. And nowadays when programming ABAP in ADT for UI5 and FIORI? Don't ask. The
class CL_DEMO_OUTPUT was invented as a demonstration,
how this lack can be circumvented.

从一开始,ABAP就缺少一中见到的输出数据的方法用来测试或展示(如java的System.out.prinln(...)或者C的printf)。在老的SAP
GUI 你可以用MESSAGE语句用来简短的输出消息或者使用write语句创建列表的输出.简单易懂-但是只能在可执行程序(report)使用。在典型的对话框程序使用write输出是不行的,在使用类的面向服务端的编程更加不行/糟糕。对于用在UI5,FIORI编程更不用说了。CL_DEMO_OUTPUT这个类就是用来展示数据规避之前这些问题。


Methods of CL_DEMO_OUTPUT

 

The methods of class CL_DEMO_OUTPUT create simple outputs of data
in example programs without the need of classical lists. The class can be used via static methods and instance methods. The following methods create output in an output stream:

类CL_DEMO_OUTPUT 在示例程序中创造了很多简单的数据输出的方法而不需要经典的list。这个类可以通过静态或实例化的方式使用。下面这些方法在输出流中创建输出。

Methods BEGIN_SECTION, NEXT_SECTION, and END_SECTION create
headers and open or close header levels.

方法BEGIN_SECTION,NEXT_SECTION和END_SECTION创建标题和开始或结束标题级别

Methods WRITE_DATA,
WRITE_TEXT, WRITE_XML, WRITE_JSON, and WRITE_HTML write
different kinds of output into the output stream.

方法WRITE_DATA,WRITE_TEXT,WRITE_XML,WRITE_JSON和WRITE_HTML显示不同类型的输出。

With method WRITE_DATA you can write elementary data objects (no
reference variables), structures with elementary components, and internal tables of such line types.

使用WRITE_DATA可以输出基本类型对象(不是引用类型),结构的组件和内表的行类型。

The other methods create formated outputs of texts, XML, JSON, or HTML data.

其它方法创建格式化的输出数据如xml ,json,HTML。

Method WRITE is generic. It handles ABAP data as well as texts
(in non proportional format).

 

Methods DISPLAY_... (available  as static methods only) work as WRITE_...,
but close the current output stream and open a new one. If a SAP GUI is available, the output is displayed in a window.

Method LINE creates
a horzontal line.

Method DISPLAY closes
the current output stream and opens a new one. If a SAP GUI is available, the output is displayed in a window. Optionally you can also pass data to DISPLAY as
you can do forWRITE.

Method GET works
like DISPLAY but does not display the data.
Instead the formated output data are returned in a text string and can be handled further.

 

The standard output format is HTML. Optionally you can choose a simple text format with tabulators and line breaks. You choose the format with method SET_MODE for
the static methods or using the input parameter MODE of the factory
methodNEW for the instance methods.

 

The class CL_DEMO_OUTPUT is available in release 7.03/7.31
since SP07 and in higher releases. It has a class documentation.


Code Examples

 

The most simple and common type of usage might look as follows:

SELECT *

       FROM scarr

       INTO TABLE @DATA(carriers).

 

cl_demo_output=>display( carriers ).

The output is:





A program using more than one static method of CL_DEMO_OUTPUT might
look as follows:

SELECT *

       FROM scarr

       INTO TABLE @DATA(carriers).

CALL TRANSFORMATION id SOURCE carriers = carriers

                       RESULT XML DATA(xml).

cl_demo_output=>begin_section( `Some Text` ).

cl_demo_output=>write_text( |blah blah blah \n| &&

                            |blah blah blah| ).

cl_demo_output=>next_section( `Some Data` ).

cl_demo_output=>begin_section( `Elementary Object` ).

cl_demo_output=>write_data( carriers[ 1 ]-carrid ).

cl_demo_output=>next_section( `Internal Table` ).

cl_demo_output=>write_data( carriers ).

cl_demo_output=>end_section( ).

cl_demo_output=>next_section( `XML` ).

cl_demo_output=>write_xml( xml ).

cl_demo_output=>display( ).

Since this looks very ugly, it is better to use the instance methods
instead of the static methods if you call more than 3 to 4 methods of the class within a program:

SELECT *

       FROM scarr

       INTO TABLE @DATA(carriers).

CALL TRANSFORMATION id SOURCE carriers = carriers

                       RESULT XML DATA(xml).

 

cl_demo_output=>new(

  )->begin_section( `Some Text`

  )->write_text( |blah blah blah \n| &&

                 |blah blah blah|

  )->next_section( `Some Data`

  )->begin_section( `Elementary Object`

  )->write_data( carriers[ 1 ]-carrid

  )->next_section( `Internal Table`

  )->write_data( carriers

  )->end_section(

  )->next_section( `XML`

  )->write_xml( xml

  )->display( ).

Both give the same output:





You might ask yourself two things:

How can static methods and instance methods have the same name? 

The instance methods are interface methods. Method NEW returns
a reference variable of typeIF_DEMO_OUTPUT.
This interface is implemented by CL_DEMO_OUTPUT.
The interface methods have the same names as the static methods of the class.  

        

Why can you chain these methods? 

For this convenience, each instance method returns the self reference me.

If you want a more simplistic output, you can switch to text mode:

SELECT *

       FROM scarr

       INTO TABLE @DATA(carriers).

 

cl_demo_output=>new( 'TEXT'

  )->display( carriers ).

 




If you want to deal with the resulting formatted data yourself, you
use GET instead of DISPLAY:

SELECT *

       FROM scarr

       INTO TABLE @DATA(carriers).

DATA(html) = cl_demo_output=>get( carriers ).

cl_abap_browser=>show_html( html_string = html ).

This produces the same output as the first example above.

You can also examine and run the following programs to get a complete
overview of all possiblities:

DEMO_USAGE_OUTPUT_STATIC

DEMO_USAGE_OUTPUT_INSTANCE


Examples of Usage

 

An example how CL_DEMO_OUTPUT can be used
by a framework is provided by the ABAP Keyword Documentation in ADT (aka ABAP in Eclipse). If an example of the ABAP Example Library uses CL_DEMO_OUTPUT,
the documentation framework allows you to execute the example and displays the output. This is done by getting the HTML output fromCL_DEMO_OUTPUT 
and merging it into the (non SAP GUI) documentation display.

 





 

Another example is quite remarkable. CL_DEMO_OUTPUT made
it to the stage in SAP Teched 2013!

 

Here a snapshot from Dr.
Vishal Sikka's keynote:

 


 

I guess it is quite safe to assume that nobody recognized how that demo output was created  

.

(B.t.w., see  AMDP,
Comparison of SQLScript with Open SQL for a closer look at the performance results of that example; The bad ABAP results above come from nested SELECT loops
...).

 

 


Conclusion

 

The methods of CL_DEMO_OUTPUT can
serve for quick and dirty outputs in tests or in demo programs. But the class is not made for productive usage. Particularly, CL_DEMO_OUTPUT and
the framework
behind are not prepared for handling large amounts of data.

 

 


Note

 

CL_DEMO_OUTPUT has a little sister CL_DEMO_INPUT that
can be used for simple inputs of elementary values.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: