-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathzcl_file__base.clas.abap
More file actions
323 lines (238 loc) · 8.27 KB
/
zcl_file__base.clas.abap
File metadata and controls
323 lines (238 loc) · 8.27 KB
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
CLASS zcl_file__base DEFINITION
PUBLIC
ABSTRACT
CREATE PROTECTED .
PUBLIC SECTION.
INTERFACES zif_file
ABSTRACT METHODS
delete_file
get_directory_content
get_directory_seperator
read_file
write_file .
CONSTANTS c_os_windows TYPE sy-opsys VALUE 'Windows NT'.
CONSTANTS c_os_linux TYPE sy-opsys VALUE 'Linux'.
CONSTANTS c_os_hpux TYPE sy-opsys VALUE 'HP-UX'.
CONSTANTS c_os_os400 TYPE sy-opsys VALUE 'OS400'.
CONSTANTS c_pathseperator_windows TYPE c LENGTH 1 VALUE '\'.
CONSTANTS c_pathseperator_other TYPE c LENGTH 1 VALUE '/'.
CONSTANTS c_root_windows TYPE c LENGTH 3 VALUE 'C:\'.
CONSTANTS c_root_linux TYPE c LENGTH 3 VALUE '/'.
PROTECTED SECTION.
METHODS add_length_to_filesize IMPORTING i_fileline TYPE any.
METHODS create_content_table.
PRIVATE SECTION.
DATA m_content TYPE REF TO data.
DATA m_full_path TYPE string.
DATA m_directory TYPE string.
DATA m_filename TYPE string.
DATA m_file_extension TYPE string.
DATA m_filesize TYPE i.
DATA m_operating_system TYPE sy-opsys.
DATA m_encoding TYPE abap_encoding.
DATA m_file_is_binary TYPE abap_bool.
DATA m_location TYPE ze_file_location.
METHODS replace_fullpath_variables .
METHODS split_fullpath_into_parts .
ENDCLASS.
CLASS zcl_file__base IMPLEMENTATION.
METHOD replace_fullpath_variables.
DATA: replacement(3).
* THESE ARE JUST EXAMPLE REPLACEMENTS... Invent your own :-) Date? Timestamp? you name it.
* Sys-ID ...
REPLACE ALL OCCURRENCES OF '<sys-id>' IN m_full_path WITH sy-sysid IGNORING CASE.
REPLACE ALL OCCURRENCES OF '<sys-id>' IN m_directory WITH sy-sysid IGNORING CASE.
REPLACE ALL OCCURRENCES OF '<sys-id>' IN m_filename WITH sy-sysid IGNORING CASE.
* Environment-ID ...
IF sy-sysid = 'DEV' OR sy-sysid = 'QS1' OR sy-sysid = 'QS2'.
replacement = 'development'.
ELSE.
replacement = 'productive'.
ENDIF.
REPLACE ALL OCCURRENCES OF '<env-id>' IN m_full_path WITH replacement IGNORING CASE.
REPLACE ALL OCCURRENCES OF '<env-id>' IN m_directory WITH replacement IGNORING CASE.
REPLACE ALL OCCURRENCES OF '<env-id>' IN m_filename WITH replacement IGNORING CASE.
ENDMETHOD.
METHOD zif_file~copy_file.
* create the new file
r_result = zcl_file=>create( i_full_path = i_path
i_location = i_destination ).
IF r_result IS NOT BOUND.
RETURN.
ENDIF.
* move data
r_result->set_filesize( zif_file~get_filesize( ) ).
r_result->set_file_is_binary( zif_file~get_file_is_binary( ) ).
* Write new file
r_result->write_file( i_write_as_binary = zif_file~get_file_is_binary( ) ).
ENDMETHOD.
METHOD zif_file~get_content.
r_result = me->m_content.
ENDMETHOD.
METHOD zif_file~get_directory.
r_result = me->m_directory.
ENDMETHOD.
METHOD zif_file~get_encoding.
r_result = me->m_encoding.
ENDMETHOD.
METHOD zif_file~get_filename.
r_result = me->m_filename.
ENDMETHOD.
METHOD zif_file~get_filesize.
r_result = me->m_filesize.
ENDMETHOD.
METHOD zif_file~get_file_extension.
r_result = me->m_file_extension.
ENDMETHOD.
METHOD zif_file~get_file_is_binary.
r_result = me->m_file_is_binary.
ENDMETHOD.
METHOD zif_file~get_full_path.
r_result = m_full_path.
ENDMETHOD.
METHOD zif_file~get_operating_system.
r_result = me->m_operating_system.
ENDMETHOD.
METHOD zif_file~move_file.
r_result = zif_file~copy_file( i_destination = i_destination
i_path = i_path ).
* Delete old file
zif_file~delete_file( ).
ENDMETHOD.
METHOD zif_file~set_content.
me->m_content = i_content.
ENDMETHOD.
METHOD zif_file~set_directory.
me->m_directory = i_directory.
ENDMETHOD.
METHOD zif_file~set_encoding.
me->m_encoding = i_encoding.
ENDMETHOD.
METHOD zif_file~set_filename.
me->m_filename = i_filename.
ENDMETHOD.
METHOD zif_file~set_filesize.
me->m_filesize = i_m_filesize.
ENDMETHOD.
METHOD zif_file~set_file_extension.
me->m_file_extension = i_file_extension.
ENDMETHOD.
METHOD zif_file~set_file_is_binary.
me->m_file_is_binary = i_file_is_binary.
ENDMETHOD.
METHOD zif_file~set_fullpath_by_logic_filename.
DATA: emergency_flag TYPE abap_bool.
IF m_location = zif_file=>mc_loc_sapgui.
DATA(is_frontend) = abap_true.
ENDIF.
DATA(operating_system) = zif_file~get_operating_system( ).
CALL FUNCTION 'FILE_GET_NAME'
EXPORTING
logical_filename = i_log_filename
operating_system = operating_system
parameter_1 = i_param1
parameter_2 = i_param2
parameter_3 = i_param3
use_presentation_server = is_frontend
with_file_extension = abap_false
eleminate_blanks = abap_false
IMPORTING
emergency_flag = emergency_flag
* FILE_FORMAT =
file_name = m_full_path
EXCEPTIONS
file_not_found = 1
OTHERS = 2.
IF sy-subrc <> 0 OR emergency_flag IS NOT INITIAL.
FREE: m_full_path.
r_result = 8.
ELSE.
split_fullpath_into_parts( ).
ENDIF.
ENDMETHOD.
METHOD zif_file~set_fullpath_by_logic_path.
DATA:
filename TYPE string,
is_frontend_server TYPE abap_bool.
* Build filename
IF m_filename IS INITIAL AND m_full_path IS NOT INITIAL.
split_fullpath_into_parts( ).
filename = m_full_path.
ENDIF.
IF m_filename IS INITIAL.
filename = zif_file=>mc_filename_replace.
ELSE.
filename = m_filename.
ENDIF.
* Where are we?
IF m_location = zcl_filehandler_frontend=>mc_destination_frontend.
is_frontend_server = abap_true.
ENDIF.
CALL FUNCTION 'FILE_GET_NAME_USING_PATH'
EXPORTING
logical_path = i_logpath
file_name = filename
use_presentation_server = is_frontend_server
eleminate_blanks = abap_false
IMPORTING
file_name_with_path = m_full_path
EXCEPTIONS
path_not_found = 1
missing_parameter = 2
operating_system_not_found = 3
file_system_not_found = 4
OTHERS = 5.
r_result = sy-subrc.
IF r_result = 0.
split_fullpath_into_parts( ).
ENDIF.
ENDMETHOD.
METHOD zif_file~set_full_path.
m_full_path = i_full_path.
ENDMETHOD.
METHOD zif_file~set_operating_system.
me->m_operating_system = i_operating_system.
ENDMETHOD.
METHOD zif_file~get_m_location.
r_result = me->m_location.
ENDMETHOD.
METHOD zif_file~set_m_location.
me->m_location = i_m_location.
ENDMETHOD.
METHOD split_fullpath_into_parts.
DATA: filesystem_seperator(2),
regex_statement TYPE string.
CHECK m_full_path IS NOT INITIAL.
filesystem_seperator = zif_file~get_directory_seperator( ).
IF filesystem_seperator = c_pathseperator_windows.
filesystem_seperator = `\` && c_pathseperator_windows. "REGEX-escape + seperator = \\
ENDIF.
regex_statement = condense( `(.*)` && filesystem_seperator && `(.*)\.(.*)$` ).
* All at once, if in correct order
FIND FIRST OCCURRENCE OF REGEX regex_statement IN m_full_path SUBMATCHES m_directory m_filename m_file_extension.
CHECK sy-subrc <> 0.
IF m_full_path CA '\/'.
regex_statement = condense( '(.*)' && filesystem_seperator && '(.*)' ).
FIND FIRST OCCURRENCE OF REGEX regex_statement IN m_full_path SUBMATCHES m_directory m_filename.
CHECK sy-subrc <> 0.
ELSEIF m_full_path CA '.'.
regex_statement = '(.*)\.(.*)$'.
FIND FIRST OCCURRENCE OF REGEX regex_statement IN m_full_path SUBMATCHES m_directory.
CHECK sy-subrc <> 0.
ENDIF.
ENDMETHOD.
METHOD create_content_table.
IF zif_file~get_file_is_binary( ) = abap_true.
CREATE DATA m_content TYPE ztt_xfilecontent.
ELSE.
CREATE DATA m_content TYPE ztt_filecontent.
ENDIF.
ENDMETHOD.
METHOD add_length_to_filesize.
IF zif_file~get_file_is_binary( ) = abap_true.
m_filesize = m_filesize + xstrlen( i_fileline ).
ELSE.
m_filesize = m_filesize + strlen( i_fileline ).
ENDIF.
ENDMETHOD.
ENDCLASS.