-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathzcl_filehandler_base.clas.abap
468 lines (379 loc) · 13.4 KB
/
zcl_filehandler_base.clas.abap
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
CLASS zcl_filehandler_base DEFINITION
PUBLIC
ABSTRACT
CREATE PUBLIC .
PUBLIC SECTION.
*"* public components of class ZCL_BC_FILEHANDLER_BASE
*"* do not include other source files here!!!
INTERFACES zif_codepages_const .
ALIASES mc_codepage_big5 FOR zif_codepages_const~mc_codepage_big5.
ALIASES mc_codepage_ibm_ebcdic FOR zif_codepages_const~mc_codepage_ibm_ebcdic.
ALIASES mc_codepage_iso8859_1 FOR zif_codepages_const~mc_codepage_iso8859_1.
ALIASES mc_codepage_iso8859_15 FOR zif_codepages_const~mc_codepage_iso8859_15.
ALIASES mc_codepage_shift_js FOR zif_codepages_const~mc_codepage_shift_js.
ALIASES mc_codepage_us_ascii FOR zif_codepages_const~mc_codepage_us_ascii.
ALIASES mc_codepage_utf16_be FOR zif_codepages_const~mc_codepage_utf16_be.
ALIASES mc_codepage_utf16_le FOR zif_codepages_const~mc_codepage_utf16_le.
ALIASES mc_codepage_utf32_be FOR zif_codepages_const~mc_codepage_utf32_be.
ALIASES mc_codepage_utf32_le FOR zif_codepages_const~mc_codepage_utf32_le.
ALIASES mc_codepage_utf8 FOR zif_codepages_const~mc_codepage_utf8.
TYPE-POOLS abap .
CONSTANTS mc_filename_replace TYPE dxfilename VALUE '<FILENAME>'. "#EC NOTEXT
CONSTANTS mc_filetype_dir TYPE filetype VALUE 'DIR' . "#EC NOTEXT
CONSTANTS mc_filetype_file TYPE filetype VALUE 'FILE' . "#EC NOTEXT
DATA mt_content TYPE REF TO data .
DATA mv_as_binary TYPE flag READ-ONLY.
DATA mv_bom TYPE xfeld VALUE abap_true.
DATA mv_destination TYPE ze_destination .
DATA mv_encoding TYPE abap_encoding VALUE mc_codepage_utf8.
DATA mv_extension TYPE hcskw_file_extension .
DATA mv_filesize TYPE i.
DATA mv_fullpath TYPE string .
DATA mv_filename TYPE dxfilename .
DATA mv_path TYPE dirname_al11 .
METHODS constructor
IMPORTING
!iv_fullpath TYPE string OPTIONAL
EXCEPTIONS
not_implemented .
CLASS-METHODS create
IMPORTING
!iv_fullpath TYPE csequence
!iv_destination TYPE ze_destination
RETURNING
VALUE(ro_file) TYPE REF TO zcl_filehandler_base .
CLASS-METHODS f4_help_all
IMPORTING
!iv_file_from_frontend TYPE flag OPTIONAL
!iv_file_from_sapserver TYPE flag OPTIONAL
CHANGING
!cv_fullpath TYPE string .
METHODS exists ABSTRACT
RETURNING VALUE(rv_exists) TYPE abap_bool .
METHODS delete_file
ABSTRACT
RETURNING
VALUE(rv_subrc) TYPE sy-subrc .
CLASS-METHODS get_destination
IMPORTING
!iv_frontend TYPE flag
!iv_backend TYPE flag
RETURNING
VALUE(rv_destination) TYPE ze_destination .
METHODS get_dir_content
ABSTRACT
IMPORTING
!iv_dirpath TYPE csequence OPTIONAL
!iv_only_subdir TYPE flag DEFAULT abap_false
!iv_only_files TYPE flag DEFAULT abap_false
!iv_build_object TYPE flag DEFAULT abap_false
!iv_append_rettable TYPE flag DEFAULT abap_false
RETURNING
VALUE(rt_directory_content) TYPE ztt_basic_file_list.
METHODS get_filesize
IMPORTING
!iv_in_binary TYPE flag DEFAULT abap_true
RETURNING
VALUE(rv_filesize) TYPE int4 .
METHODS get_fullpath RETURNING VALUE(r_result) TYPE string.
METHODS get_os
ABSTRACT
RETURNING
VALUE(rv_os) TYPE sy-opsys .
METHODS get_physical_file IMPORTING iv_log_filename TYPE filename-fileintern
iv_param1 TYPE csequence
iv_param2 TYPE csequence
iv_param3 TYPE csequence
RETURNING VALUE(rv_subrc) TYPE sy-subrc.
METHODS get_physical_path
IMPORTING
!iv_logpath TYPE filepath-pathintern
RETURNING
VALUE(rv_subrc) TYPE sy-subrc .
METHODS get_sep
ABSTRACT
RETURNING
VALUE(rv_sep) TYPE char01 .
CLASS zcl_bc_filehandler_sapserver DEFINITION LOAD .
METHODS move_file
IMPORTING
!iv_destination TYPE ze_destination
!iv_path TYPE csequence
!iv_copy_only TYPE flag DEFAULT abap_false
RETURNING
VALUE(ro_file) TYPE REF TO zcl_filehandler_base .
METHODS read_file
ABSTRACT
IMPORTING
!iv_read_as_binary TYPE flag DEFAULT abap_false
EXPORTING
!ev_length TYPE int4 .
METHODS replace_fullpath_variables .
METHODS set_encoding
IMPORTING
!iv_encoding TYPE abap_encoding
EXCEPTIONS
not_supported .
METHODS set_fullpath
IMPORTING
!iv_fullpath TYPE csequence OPTIONAL
EXCEPTIONS
not_implemented .
METHODS set_path
IMPORTING
iv_path TYPE dirname_al11
iv_filename TYPE dxfilename
iv_extension TYPE hcskw_file_extension.
METHODS write_file
ABSTRACT
IMPORTING
iv_write_as_binary TYPE flag OPTIONAL
iv_binsize TYPE int4 OPTIONAL .
PROTECTED SECTION.
METHODS build_fullpath .
METHODS split_fullpath
EXCEPTIONS
not_implemented .
PRIVATE SECTION.
*"* private components of class ZCL_BC_FILEHANDLER_BASE
*"* do not include other source files here!!!
ENDCLASS.
CLASS ZCL_FILEHANDLER_BASE IMPLEMENTATION.
METHOD build_fullpath.
* build the full path
CLEAR mv_fullpath.
IF mv_filename IS NOT INITIAL AND mv_extension IS NOT INITIAL.
mv_fullpath = mv_path && get_sep( ) && mv_filename && '.' && mv_extension.
ELSEIF mv_filename IS NOT INITIAL.
mv_fullpath = mv_path && get_sep( ) && mv_filename.
ELSE.
mv_fullpath = mv_path.
ENDIF.
* automatic replacements
replace_fullpath_variables( ).
ENDMETHOD. "build_fullpath
METHOD constructor.
IF iv_fullpath IS NOT INITIAL.
set_fullpath( iv_fullpath = iv_fullpath ).
ENDIF.
ENDMETHOD. "CONSTRUCTOR
METHOD create.
DATA: lv_class TYPE seoclsname.
CASE iv_destination.
WHEN zcl_filehandler_frontend=>mc_destination_frontend.
lv_class = 'ZCL_BC_FILEHANDLER_FRONTEND'.
WHEN zcl_filehandler_sapserver=>mc_destination_sap.
lv_class = 'ZCL_BC_FILEHANDLER_SAPSERVER'.
WHEN OTHERS.
EXIT.
ENDCASE.
CREATE OBJECT ro_file TYPE (lv_class)
EXPORTING
iv_path = iv_fullpath.
ENDMETHOD. "create
METHOD f4_help_all.
CASE abap_true.
WHEN iv_file_from_frontend.
zcl_filehandler_frontend=>f4_help( CHANGING cv_fullpath = cv_fullpath ).
WHEN iv_file_from_sapserver.
zcl_filehandler_sapserver=>f4_help( CHANGING cv_fullpath = cv_fullpath ).
WHEN OTHERS.
"ok ??? :D
ENDCASE.
ENDMETHOD.
METHOD get_destination.
IF iv_frontend = abap_true.
rv_destination = zcl_filehandler_frontend=>mc_destination_frontend.
ELSEIF iv_backend = abap_true.
rv_destination = zcl_filehandler_sapserver=>mc_destination_sap.
ELSE.
CLEAR rv_destination.
ENDIF.
ENDMETHOD.
METHOD get_filesize.
FIELD-SYMBOLS: <table> TYPE table,
<line> TYPE any.
rv_filesize = -1.
ASSIGN mt_content->* TO <table>.
CHECK sy-subrc = 0.
rv_filesize = 0.
LOOP AT <table> ASSIGNING <line>.
IF iv_in_binary = abap_true.
rv_filesize = rv_filesize + xstrlen( <line> ).
ELSE.
rv_filesize = rv_filesize + strlen( <line> ).
ENDIF.
ENDLOOP.
ENDMETHOD. "GET_FILESIZE
METHOD get_fullpath.
r_result = me->mv_fullpath.
ENDMETHOD.
METHOD get_physical_file.
DATA: lv_emergency TYPE c.
IF mv_destination = zcl_filehandler_frontend=>mc_destination_frontend.
DATA(lv_frontend) = abap_true.
ENDIF.
DATA(lv_os) = get_os( ).
CALL FUNCTION 'FILE_GET_NAME'
EXPORTING
logical_filename = iv_log_filename
operating_system = sy-opsys
parameter_1 = iv_param1
parameter_2 = iv_param2
parameter_3 = iv_param3
use_presentation_server = lv_frontend
with_file_extension = abap_false
eleminate_blanks = abap_false
IMPORTING
emergency_flag = lv_emergency
* FILE_FORMAT =
file_name = mv_fullpath
EXCEPTIONS
file_not_found = 1
OTHERS = 2.
IF sy-subrc <> 0 OR lv_emergency IS NOT INITIAL.
FREE: mv_fullpath.
rv_subrc = 8.
ELSE.
split_fullpath( ).
ENDIF.
ENDMETHOD.
METHOD get_physical_path.
DATA:
lv_file LIKE mv_filename,
lv_pres TYPE abap_bool.
* Build filename
IF mv_filename IS INITIAL AND mv_fullpath IS NOT INITIAL.
split_fullpath( ).
lv_file = mv_filename.
ENDIF.
IF mv_filename IS INITIAL.
lv_file = mc_filename_replace.
mv_filename = mc_filename_replace.
ELSE.
lv_file = mv_filename.
ENDIF.
* Where are we?
IF mv_destination = zcl_filehandler_frontend=>mc_destination_frontend.
lv_pres = abap_true.
ENDIF.
CALL FUNCTION 'FILE_GET_NAME_USING_PATH'
EXPORTING
logical_path = iv_logpath
file_name = lv_file
use_presentation_server = lv_pres
eleminate_blanks = abap_false
IMPORTING
file_name_with_path = mv_fullpath
EXCEPTIONS
path_not_found = 1
missing_parameter = 2
operating_system_not_found = 3
file_system_not_found = 4
OTHERS = 5.
rv_subrc = sy-subrc.
IF rv_subrc = 0.
split_fullpath( ).
ENDIF.
ENDMETHOD. "get_physical_path
METHOD move_file.
* create the new file
ro_file = create( iv_fullpath = iv_path
iv_destination = iv_destination " Ziel des Datenstroms
).
IF ro_file IS NOT BOUND.
RETURN.
ENDIF.
* move data
ro_file->mt_content = me->mt_content.
ro_file->mv_filesize = me->mv_filesize. "Größe der gespeicherten Datei IN Bytes
ro_file->mv_as_binary = me->mv_as_binary. "wurde die Datei binär gelesen?
* Write new file
ro_file->write_file( iv_write_as_binary = ro_file->mv_as_binary ).
* Delete old file
IF iv_copy_only = abap_false.
me->delete_file( ).
ENDIF.
ENDMETHOD.
METHOD replace_fullpath_variables.
DATA: lv_repl(3).
* Sys-ID ... replacement
REPLACE ALL OCCURRENCES OF '<sys-id>' IN mv_fullpath WITH sy-sysid IGNORING CASE.
REPLACE ALL OCCURRENCES OF '<sys-id>' IN mv_path WITH sy-sysid IGNORING CASE.
* Env-ID ... Just a sample
IF sy-sysid = 'DEV' OR sy-sysid = 'QS1' OR sy-sysid = 'QS2'.
lv_repl = 'development'.
ELSE.
lv_repl = 'productive'.
ENDIF.
REPLACE ALL OCCURRENCES OF '<env-id>' IN mv_fullpath WITH lv_repl IGNORING CASE.
REPLACE ALL OCCURRENCES OF '<env-id>' IN mv_path WITH lv_repl IGNORING CASE.
ENDMETHOD. "replace_fullpath_variables
METHOD set_encoding.
IF iv_encoding <> mc_codepage_iso8859_15
AND iv_encoding <> mc_codepage_utf16_be
AND iv_encoding <> mc_codepage_utf16_le
AND iv_encoding <> mc_codepage_utf32_be
AND iv_encoding <> mc_codepage_utf32_le
AND iv_encoding <> mc_codepage_utf8.
RAISE not_supported.
ENDIF.
mv_encoding = iv_encoding.
IF mv_encoding = mc_codepage_utf16_be
OR mv_encoding = mc_codepage_utf16_le
OR mv_encoding = mc_codepage_utf32_be
OR mv_encoding = mc_codepage_utf32_le.
mv_bom = abap_true.
ENDIF.
ENDMETHOD.
METHOD set_fullpath.
IF iv_fullpath IS SUPPLIED.
mv_fullpath = iv_fullpath.
ELSEIF mv_path IS NOT INITIAL OR mv_filename IS NOT INITIAL.
IF mv_extension IS INITIAL.
mv_fullpath = mv_path && get_sep( ) && mv_filename.
ELSE.
mv_fullpath = mv_path && get_sep( ) && mv_filename && '.' && mv_extension.
ENDIF.
ELSE.
RAISE not_implemented .
ENDIF.
replace_fullpath_variables( ).
* CHECK mv_fullpath IS NOT INITIAL.
* split_fullpath( ).
ENDMETHOD. "SET_FULLPATH
METHOD set_path.
mv_path = iv_path .
mv_filename = iv_filename .
mv_extension = iv_extension.
set_fullpath( ).
ENDMETHOD.
METHOD split_fullpath.
DATA: lv_sep(2),
lv_regex TYPE string.
CHECK mv_fullpath IS NOT INITIAL.
lv_sep = get_sep( ).
IF lv_sep = '\'.
lv_sep = '\\'.
ENDIF.
lv_regex = '(.*)' && lv_sep && '(.*)\.(.*)$'.
CONDENSE lv_regex.
* All at once, if in correct order
FIND FIRST OCCURRENCE OF REGEX lv_regex IN mv_fullpath SUBMATCHES mv_path mv_filename mv_extension.
CHECK sy-subrc <> 0. "Im Fehlerfall Probieren wir noch was ... :)
IF mv_fullpath CA '\/'.
CLEAR lv_regex.
lv_regex = '(.*)' && lv_sep && '(.*)'.
CONDENSE lv_regex.
FIND FIRST OCCURRENCE OF REGEX lv_regex IN mv_fullpath SUBMATCHES mv_path mv_filename.
CHECK sy-subrc <> 0.
ELSEIF mv_fullpath CA '.'.
CLEAR lv_regex.
lv_regex = '(.*)\.(.*)$'.
FIND FIRST OCCURRENCE OF REGEX lv_regex IN mv_fullpath SUBMATCHES mv_path.
CHECK sy-subrc <> 0.
ENDIF.
* We didnt find anything
RAISE not_implemented.
ENDMETHOD. "split_fullpath
ENDCLASS.