martes, 30 de noviembre de 2010

Chrstimas Sale!


Starting today and for all December...get a 25% discount in all my books!

Blag en Lulu.com

Go get this awesome sale!

Greetings,

Blag.

miércoles, 10 de noviembre de 2010

XML and RegEx again...


Today I was thinking about XML and RegEx...You can parse an XML document using RegEx, not the best way, but surely is fun...so...what about creating an XML file using RegEx? More fun for sure! -:D Of course...and just to make it clear...there are other more standard and easier ways to achieve the same: Simple Transformations, XLST Transformation y XML DOM.

But as always...this code was very fun to made -;)


TYPES: BEGIN OF TY_XML,
FIELD TYPE STRING,
END OF TY_XML.

DATA: T_DD03P_TAB TYPE STANDARD TABLE OF DD03P,
T_FILETAB TYPE FILETABLE,
T_XML TYPE STANDARD TABLE OF TY_XML.

DATA: L_FLAG TYPE C,
L_AUX TYPE STRING,
L_STRING TYPE STRING,
L_XML_LINE TYPE STRING,
DATA_TAB TYPE REF TO DATA,
SIZE TYPE I,
W_SUBRC TYPE SY-SUBRC,
W_FILE_OUT TYPE STRING.

FIELD-SYMBOLS: TYPE STANDARD TABLE,
TYPE ANY,
TYPE ANY,
LIKE LINE OF T_XML,
LIKE LINE OF T_DD03P_TAB,
LIKE LINE OF T_FILETAB.

SELECTION-SCREEN BEGIN OF BLOCK TEST WITH FRAME.
PARAMETERS:
TABNAME TYPE DD02L-TABNAME,
FILE_OUT LIKE RLGRAP-FILENAME.
SELECTION-SCREEN END OF BLOCK TEST.

START-OF-SELECTION.
PERFORM VALIDATE_TABLE USING TABNAME
CHANGING L_FLAG.
IF L_FLAG EQ SPACE.
PERFORM LOAD_TABLE_STRUCT USING TABNAME.
PERFORM LOAD_TABLE_INFO USING TABNAME.
PERFORM CREATE_XML.
PERFORM DOWNLOAD_XML.
ENDIF.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR FILE_OUT.
CALL METHOD CL_GUI_FRONTEND_SERVICES=>FILE_OPEN_DIALOG
EXPORTING
WINDOW_TITLE = 'Seleccionar archivo'
DEFAULT_FILENAME = '*.xml'
FILE_FILTER = '*.xml'
CHANGING
FILE_TABLE = T_FILETAB
RC = W_SUBRC.

READ TABLE T_FILETAB INDEX 1
ASSIGNING .
FILE_OUT = .
W_FILE_OUT = FILE_OUT.

IF FILE_OUT IS INITIAL.
EXIT.
ENDIF.

*&------------------------------------------------------*
*& Form VALIDATE_TABLE *
*&------------------------------------------------------*
FORM VALIDATE_TABLE USING P_TABNAME
CHANGING P_FLAG.

SELECT SINGLE TABNAME
INTO P_TABNAME
FROM DD02L
WHERE TABNAME EQ P_TABNAME.
IF SY-SUBRC NE 0.
P_FLAG = 'X'.
ELSE.
CLEAR P_FLAG.
ENDIF.

ENDFORM. " VALIDATE_TABLE

*&------------------------------------------------------*
*& Form LOAD_TABLE_STRUCT *
*&------------------------------------------------------*
FORM LOAD_TABLE_STRUCT USING P_TABNAME.

CALL FUNCTION 'DDIF_TABL_GET'
EXPORTING
NAME = P_TABNAME
TABLES
DD03P_TAB = T_DD03P_TAB
EXCEPTIONS
ILLEGAL_INPUT = 1
OTHERS = 2.

ENDFORM. " LOAD_TABLE_STRUCT

*&------------------------------------------------------*
*& Form LOAD_TABLE_INFO *
*&------------------------------------------------------*
FORM LOAD_TABLE_INFO USING P_TABNAME.

CREATE DATA DATA_TAB TYPE STANDARD TABLE OF (P_TABNAME).
ASSIGN DATA_TAB->* TO .
SELECT *
FROM (P_TABNAME)
INTO TABLE .

ENDFORM. " LOAD_TABLE_INFO

*&------------------------------------------------------*
*& Form CREATE_XML *
*&------------------------------------------------------*
FORM CREATE_XML.

APPEND INITIAL LINE TO T_XML ASSIGNING .
-FIELD = ''.
APPEND INITIAL LINE TO T_XML ASSIGNING .
-FIELD = ''.
*XML Logic here!
LOOP AT ASSIGNING .
L_XML_LINE = ''.
LOOP AT T_DD03P_TAB ASSIGNING .
ASSIGN COMPONENT -FIELDNAME OF
STRUCTURE TO .
L_AUX = .
CONDENSE L_AUX NO-GAPS.
CONCATENATE -FIELDNAME '/' L_AUX
INTO L_STRING.
REPLACE REGEX '(\w+)[\/](\w+)' IN L_STRING
WITH '<$1>$2'.
CONCATENATE L_XML_LINE L_STRING INTO L_XML_LINE.
ENDLOOP.
APPEND INITIAL LINE TO T_XML ASSIGNING .
-FIELD = L_XML_LINE.
CONCATENATE -FIELD '
'
INTO -FIELD.
ENDLOOP.
*XML Logic here!
APPEND INITIAL LINE TO T_XML ASSIGNING .
-FIELD = '
'.

ENDFORM. " CREATE_XML

*&------------------------------------------------------*
*& Form DOWNLOAD_XML *
*&------------------------------------------------------*
FORM DOWNLOAD_XML.

CALL METHOD CL_GUI_FRONTEND_SERVICES=>GUI_DOWNLOAD
EXPORTING
BIN_FILESIZE = SIZE
FILENAME = W_FILE_OUT
FILETYPE = 'DAT'
CHANGING
DATA_TAB = T_XML.

ENDFORM. " DOWNLOAD_XML



Greetings,

Blag.

jueves, 4 de noviembre de 2010

Programming non-sense #1


With this post, I want to start a small series about ABAP programs that doesn't bring any value but are fun to code -:) I believe that ABAP coding is fun...believe it or not...

Anyway...my first code, that by the way doesn't work in the best possible way, it's a clock...we simply read the system time and show it on digital form...then, we activate a temporizer that it's going to be executed every second and it's going to read again the system timeand show it on digital format...of course, as we don't have in ABAP and CLRSCR(); or an CLS, we must read every line and modify their contents.


DATA: temporizador TYPE REF TO cl_gui_timer,
hour(2) TYPE c,
minute(2) TYPE c,
second(2) TYPE c,
one(1) TYPE c,
two(1) TYPE c,
line1 TYPE string,
line2 TYPE string,
line3 TYPE string.

*----------------------------------------------------------------------*
* CLASS clock DEFINITION *
*----------------------------------------------------------------------*
CLASS clock DEFINITION.
PUBLIC SECTION.
METHODS: zero IMPORTING line TYPE string,
one IMPORTING line TYPE string,
two IMPORTING line TYPE string,
three IMPORTING line TYPE string,
four IMPORTING line TYPE string,
five IMPORTING line TYPE string,
six IMPORTING line TYPE string,
seven IMPORTING line TYPE string,
eight IMPORTING line TYPE string,
nine IMPORTING line TYPE string,
periods IMPORTING line TYPE string,
show IMPORTING number TYPE c.
ENDCLASS. "clock DEFINITION

*----------------------------------------------------------------------*
* CLASS clock IMPLEMENTATION *
*----------------------------------------------------------------------*
CLASS clock IMPLEMENTATION.
METHOD zero.
CASE line.
WHEN 1.
CONCATENATE line1 ' _ ' INTO line1 SEPARATED BY space RESPECTING BLANKS.
WHEN 2.
CONCATENATE line2 '| |' INTO line2 SEPARATED BY space RESPECTING BLANKS.
WHEN 3.
CONCATENATE line3 '|_|' INTO line3 SEPARATED BY space RESPECTING BLANKS.
ENDCASE.
ENDMETHOD. "zero
METHOD one.
CASE line.
WHEN 1.
CONCATENATE line1 ' ' INTO line1 SEPARATED BY space RESPECTING BLANKS.
WHEN 2.
CONCATENATE line2 ' |' INTO line2 SEPARATED BY space RESPECTING BLANKS.
WHEN 3.
CONCATENATE line3 ' |' INTO line3 SEPARATED BY space RESPECTING BLANKS.
ENDCASE.
ENDMETHOD. "one
METHOD two.
CASE line.
WHEN 1.
CONCATENATE line1 ' _ ' INTO line1 SEPARATED BY space RESPECTING BLANKS.
WHEN 2.
CONCATENATE line2 ' _|' INTO line2 SEPARATED BY space RESPECTING BLANKS.
WHEN 3.
CONCATENATE line3 '|_ ' INTO line3 SEPARATED BY space RESPECTING BLANKS.
ENDCASE.
ENDMETHOD. "two
METHOD three.
CASE line.
WHEN 1.
CONCATENATE line1 ' _ ' INTO line1 SEPARATED BY space RESPECTING BLANKS.
WHEN 2.
CONCATENATE line2 ' _|' INTO line2 SEPARATED BY space RESPECTING BLANKS.
WHEN 3.
CONCATENATE line3 ' _|' INTO line3 SEPARATED BY space RESPECTING BLANKS.
ENDCASE.
ENDMETHOD. "three
METHOD four.
CASE line.
WHEN 1.
CONCATENATE line1 ' ' INTO line1 SEPARATED BY space RESPECTING BLANKS.
WHEN 2.
CONCATENATE line2 '|_|' INTO line2 SEPARATED BY space RESPECTING BLANKS.
WHEN 3.
CONCATENATE line3 ' |' INTO line3 SEPARATED BY space RESPECTING BLANKS.
ENDCASE.
ENDMETHOD. "four
METHOD five.
CASE line.
WHEN 1.
CONCATENATE line1 ' _ ' INTO line1 SEPARATED BY space RESPECTING BLANKS.
WHEN 2.
CONCATENATE line2 '|_ ' INTO line2 SEPARATED BY space RESPECTING BLANKS.
WHEN 3.
CONCATENATE line3 ' _|' INTO line3 SEPARATED BY space RESPECTING BLANKS.
ENDCASE.
ENDMETHOD. "five
METHOD six.
CASE line.
WHEN 1.
CONCATENATE line1 ' _ ' INTO line1 SEPARATED BY space RESPECTING BLANKS.
WHEN 2.
CONCATENATE line2 '|_ ' INTO line2 SEPARATED BY space RESPECTING BLANKS.
WHEN 3.
CONCATENATE line3 '|_|' INTO line3 SEPARATED BY space RESPECTING BLANKS.
ENDCASE.
ENDMETHOD. "six
METHOD seven.
CASE line.
WHEN 1.
CONCATENATE line1 '_ ' INTO line1 SEPARATED BY space RESPECTING BLANKS.
WHEN 2.
CONCATENATE line2 ' | ' INTO line2 SEPARATED BY space RESPECTING BLANKS.
WHEN 3.
CONCATENATE line3 ' | ' INTO line3 SEPARATED BY space RESPECTING BLANKS.
ENDCASE.
ENDMETHOD. "seven
METHOD eight.
CASE line.
WHEN 1.
CONCATENATE line1 ' _ ' INTO line1 SEPARATED BY space RESPECTING BLANKS.
WHEN 2.
CONCATENATE line2 '|_|' INTO line2 SEPARATED BY space RESPECTING BLANKS.
WHEN 3.
CONCATENATE line3 '|_|' INTO line3 SEPARATED BY space RESPECTING BLANKS.
ENDCASE.
ENDMETHOD. "eight
METHOD nine.
CASE line.
WHEN 1.
CONCATENATE line1 ' _ ' INTO line1 SEPARATED BY space RESPECTING BLANKS.
WHEN 2.
CONCATENATE line2 '|_|' INTO line2 SEPARATED BY space RESPECTING BLANKS.
WHEN 3.
CONCATENATE line3 ' _|' INTO line3 SEPARATED BY space RESPECTING BLANKS.
ENDCASE.
ENDMETHOD. "nine
METHOD periods.
CASE line.
WHEN 1.
CONCATENATE line1 ' ' INTO line1 SEPARATED BY space RESPECTING BLANKS.
WHEN 2.
CONCATENATE line2 ' * ' INTO line2 SEPARATED BY space RESPECTING BLANKS.
WHEN 3.
CONCATENATE line3 ' * ' INTO line3 SEPARATED BY space RESPECTING BLANKS.
ENDCASE.
ENDMETHOD. "periods
METHOD show.
CASE number.
WHEN 0.
zero( EXPORTING line = '1' ).
zero( EXPORTING line = '2' ).
zero( EXPORTING line = '3' ).
WHEN 1.
one( EXPORTING line = '1' ).
one( EXPORTING line = '2' ).
one( EXPORTING line = '3' ).
WHEN 2.
two( EXPORTING line = '1' ).
two( EXPORTING line = '2' ).
two( EXPORTING line = '3' ).
WHEN 3.
three( EXPORTING line = '1' ).
three( EXPORTING line = '2' ).
three( EXPORTING line = '3' ).
WHEN 4.
four( EXPORTING line = '1' ).
four( EXPORTING line = '2' ).
four( EXPORTING line = '3' ).
WHEN 5.
five( EXPORTING line = '1' ).
five( EXPORTING line = '2' ).
five( EXPORTING line = '3' ).
WHEN 6.
six( EXPORTING line = '1' ).
six( EXPORTING line = '2' ).
six( EXPORTING line = '3' ).
WHEN 7.
seven( EXPORTING line = '1' ).
seven( EXPORTING line = '2' ).
seven( EXPORTING line = '3' ).
WHEN 8.
eight( EXPORTING line = '1' ).
eight( EXPORTING line = '2' ).
eight( EXPORTING line = '3' ).
WHEN 9.
nine( EXPORTING line = '1' ).
nine( EXPORTING line = '2' ).
nine( EXPORTING line = '3' ).
ENDCASE.
ENDMETHOD. "show
ENDCLASS. "clock IMPLEMENTATION

*----------------------------------------------------------------------*
* CLASS lcl_event_handler DEFINITION *
*----------------------------------------------------------------------*
CLASS lcl_event_handler DEFINITION.
PUBLIC SECTION.
CLASS-METHODS: on_timer FOR EVENT finished OF cl_gui_timer
IMPORTING sender.
ENDCLASS. "lcl_event_handler DEFINITION

*----------------------------------------------------------------------*
* CLASS lcl_event_handler IMPLEMENTATION *
*----------------------------------------------------------------------*
CLASS lcl_event_handler IMPLEMENTATION.
METHOD on_timer.
DATA: g_clock TYPE REF TO clock.

IF NOT g_clock IS BOUND.
CREATE OBJECT g_clock.
ENDIF.

CLEAR: line1, line2, line3.

hour = sy-uzeit+0(2).
one = hour+0(1).
g_clock->show( EXPORTING number = one ).
two = hour+1(1).
g_clock->show( EXPORTING number = two ).
g_clock->periods( EXPORTING line = '1' ).
g_clock->periods( EXPORTING line = '2' ).
g_clock->periods( EXPORTING line = '3' ).
minute = sy-uzeit+2(2).
one = minute+0(1).
g_clock->show( EXPORTING number = one ).
two = minute+1(1).
g_clock->show( EXPORTING number = two ).
g_clock->periods( EXPORTING line = '1' ).
g_clock->periods( EXPORTING line = '2' ).
g_clock->periods( EXPORTING line = '3' ).
second = sy-uzeit+4(2).
one = second+0(1).
g_clock->show( EXPORTING number = one ).
two = second+1(1).
g_clock->show( EXPORTING number = two ).

READ LINE 1.
MODIFY LINE 1 FIELD VALUE line1 FROM line1.
READ LINE 2.
MODIFY LINE 2 FIELD VALUE line2 FROM line2.
READ LINE 3.
MODIFY LINE 3 FIELD VALUE line3 FROM line3.

sender->run( ).
ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
CREATE OBJECT temporizador.
SET HANDLER lcl_event_handler=>on_timer FOR temporizador.

temporizador->interval = 1.
temporizador->run( ).

DATA: g_clock TYPE REF TO clock.
CREATE OBJECT g_clock.

hour = sy-uzeit+0(2).
one = hour+0(1).
g_clock->show( EXPORTING number = one ).
two = hour+1(1).
g_clock->show( EXPORTING number = two ).
g_clock->periods( EXPORTING line = '1' ).
g_clock->periods( EXPORTING line = '2' ).
g_clock->periods( EXPORTING line = '3' ).
minute = sy-uzeit+2(2).
one = minute+0(1).
g_clock->show( EXPORTING number = one ).
two = minute+1(1).
g_clock->show( EXPORTING number = two ).
g_clock->periods( EXPORTING line = '1' ).
g_clock->periods( EXPORTING line = '2' ).
g_clock->periods( EXPORTING line = '3' ).
second = sy-uzeit+4(2).
one = second+0(1).
g_clock->show( EXPORTING number = one ).
two = second+1(1).
g_clock->show( EXPORTING number = two ).

WRITE:/ line1.
WRITE:/ line2.
WRITE:/ line3.


Greetings,

Blag.