-
Notifications
You must be signed in to change notification settings - Fork 0
/
zcl_docx_class.abap
1720 lines (1211 loc) · 40.4 KB
/
zcl_docx_class.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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
*&---------------------------------------------------------------------*
*& Include ZCL_DOCX_CLASS
*&
*& Author: Sikidin A.P. anton.sikidin@gmail.com
*&
*& ZCL_DOCX is replasement of zwww_openform for docx
*&
*&---------------------------------------------------------------------*
class lcl_recursive_data definition deferred.
types
: begin of t_key_value
, key type string
, value type string
, end of t_key_value
, tt_key_value type table of t_key_value
, begin of t_key_table
, key type string
, value type ref to data
, end of t_key_table
, tt_key_table type table of t_key_table
, t_lcl type ref to lcl_recursive_data
, tt_lcl type table of t_lcl
, tt_keys type table of string
.
class lcl_recursive_data definition.
public section.
data
: key_value type tt_key_value
, key_table type tt_key_table
, key type string
, keys type tt_keys
, key_lcl type tt_lcl
.
methods append_key_value
importing
value(iv_key) type string
!iv_value type any .
methods append_key_table
importing
value(iv_key) type string
!iv_table type any table.
methods create_document
importing
value(iv_key) type string
returning
value(r_data) type ref to lcl_recursive_data .
endclass.
class lcl_recursive_data implementation.
method append_key_value.
translate iv_key to upper case.
append initial line to key_value assigning field-symbol(<fs_key_value>).
<fs_key_value>-key = |\{{ iv_key }\}| .
<fs_key_value>-value = iv_value.
endmethod.
method append_key_table.
translate iv_key to upper case.
append initial line to key_table assigning field-symbol(<fs_key_table>).
<fs_key_table>-key = iv_key.
create data <fs_key_table>-value like iv_table.
assign <fs_key_table>-value->* to field-symbol(<fs_any_table>).
<fs_any_table> = iv_table.
endmethod.
method create_document .
translate iv_key to upper case.
create object r_data.
r_data->key = iv_key.
collect iv_key into keys.
append r_data to key_lcl.
endmethod.
endclass.
class lcl_docx definition.
public section.
types
: t_ref_node type ref to if_ixml_node
, tt_node type standard table of t_ref_node
, begin of t_bookmark
, id type string
, start_node type t_ref_node
, end_node type t_ref_node
, in_table type xfeld
, name type string
, start_height type i
, end_height type i
, end of t_bookmark
, begin of t_stack_data
, start type xfeld
, id type string
, node type t_ref_node
, position type i
, length type i
, collision type i
, end of t_stack_data
, begin of t_collision
, collision type string
, count type i
, end of t_collision
.
methods load_smw0
importing
!i_w3objid type w3objid .
methods add_file
importing
!iv_path type string
!iv_data type xstring.
methods save
importing
!on_desktop type xfeld default 'X'
!iv_folder type string default 'report'
!iv_path type string default ''
!iv_file_name type string default 'report.docx'
!no_execute type xfeld default '' .
methods map_data
importing
!ir_xml_node type ref to if_ixml_document optional
!ir_data type ref to lcl_recursive_data.
methods check_flag
importing
!it_keys type tt_keys.
protected section.
constants c_document type string value 'word/document.xml' ##NO_TEXT.
methods map_values
importing
!ir_xml_node type ref to if_ixml_node optional
!it_key_value type tt_key_value .
methods map_table
importing
!ir_xml_node type ref to if_ixml_document optional
!iv_key type string
!it_data type any table .
methods append_node
importing
!ir_source type ref to if_ixml_node optional
!ir_dest type ref to if_ixml_node optional
!iv_key type string.
methods get_fragment
importing
!ir_xml_node type ref to if_ixml_document optional
!iv_key type string
returning
value(rr_fragment) type ref to if_ixml_document.
methods normalize_key.
methods align_bookmark.
methods get_from_zip_archive
importing
!i_filename type string
returning
value(r_content) type xstring .
methods get_ixml_from_zip_archive
importing
!i_filename type string
returning
value(r_ixml) type ref to if_ixml_document .
methods normalize_space
importing
!iv_content type xstring
returning
value(r_content) type xstring.
private section.
data zip type ref to cl_abap_zip .
data document type ref to if_ixml_document .
methods upper_case
importing
!iv_str type string
returning
value(rv_str) type string.
methods dump_data
importing
!node type ref to if_ixml_document optional
!node_node type ref to if_ixml_node optional
!fname type string.
methods create_document
returning
value(rp_content) type xstring .
methods map_line
importing
!node type ref to if_ixml_document
!components type abap_compdescr_tab
!data type any .
endclass.
class lcl_docx implementation.
method load_smw0.
data
: lv_templ_xstr type xstring
, lt_mime type table of w3mime
.
data(ls_key) = value wwwdatatab(
relid = 'MI'
objid = i_w3objid ).
call function 'WWWDATA_IMPORT'
exporting
key = ls_key
tables
mime = lt_mime
exceptions
others = 1.
if sy-subrc <> 0.
return.
endif.
try.
lv_templ_xstr = cl_bcs_convert=>xtab_to_xstring( lt_mime ).
catch cx_bcs.
return.
endtry.
if zip is initial.
create object zip.
endif.
zip->load( lv_templ_xstr ).
document = me->get_ixml_from_zip_archive( me->c_document ).
normalize_key( ).
* align_bookmark( ).
endmethod.
method normalize_key.
data
: lt_nodes type table of t_ref_node
, lv_in type c
, lv_regex_open type string value '\{[^\}]*$'
, lv_regex_close type string value '\}[^\{]*'
, lv_tmp_str type string
.
* dump_data( node = document
* fname = 'before' ).
data(iterator) = document->create_iterator( ).
do.
data(node) = iterator->get_next( ).
if node is initial.
exit.
endif.
check node->get_type( ) = if_ixml_node=>co_node_element.
check node->get_name( ) = 'p'.
refresh lt_nodes.
clear lv_in.
data(nodes) = node->get_children( ).
do nodes->get_length( ) times.
data(child) = nodes->get_item( sy-index - 1 ).
data(nodes_2) = child->get_children( ).
do nodes_2->get_length( ) times.
data(child_2) = nodes_2->get_item( sy-index - 1 ).
check child_2->get_name( ) = 't'.
data(child_2_value) = child_2->get_value( ).
child_2_value = child_2->get_value( ).
child_2->set_value( upper_case( child_2_value ) ).
if lv_in is not initial.
append child_2 to lt_nodes.
find regex lv_regex_close in child_2_value.
check sy-subrc = 0.
find regex lv_regex_open in child_2_value.
check sy-subrc ne 0.
clear lv_tmp_str .
loop at lt_nodes assigning field-symbol(<fs_node>).
child_2_value = <fs_node>->get_value( ).
lv_tmp_str = |{ lv_tmp_str }{ child_2_value }|.
<fs_node>->set_value( '' ).
endloop.
read table lt_nodes assigning <fs_node> index 1.
<fs_node>->set_value( upper_case( lv_tmp_str ) ).
data
: lo_element type ref to if_ixml_element
.
lo_element ?= <fs_node>.
lo_element->set_attribute( name = 'space'
namespace = 'xml'
value = 'preserve' ).
clear lv_in.
refresh lt_nodes.
else.
find regex lv_regex_open in child_2_value.
check sy-subrc = 0.
lv_in = 'X'.
append child_2 to lt_nodes.
endif.
enddo.
enddo.
enddo.
* dump_data( node = document
* fname = 'after' ).
endmethod.
method align_bookmark.
data
: lt_bokkmarks type table of t_bookmark
.
data(iterator) = document->create_iterator( ).
do .
data(node) = iterator->get_next( ).
if node is initial.
exit.
endif.
check node->get_type( ) = if_ixml_node=>co_node_element.
data(name) = node->get_name( ).
case name.
when 'bookmarkStart'.
when 'bookmarkEnd'.
when others.
continue.
endcase.
data(attributes) = node->get_attributes( ).
data
: lv_name type string
, lv_id type string
.
do attributes->get_length( ) times.
data(attribute) = attributes->get_item( sy-index - 1 ).
case attribute->get_name( ).
when 'id'.
lv_id = attribute->get_value( ).
read table lt_bokkmarks assigning field-symbol(<fs_bookmark>) with key id = lv_id.
if sy-subrc ne 0.
append initial line to lt_bokkmarks assigning <fs_bookmark>.
<fs_bookmark>-id = lv_id.
endif.
case name.
when 'bookmarkStart'.
<fs_bookmark>-start_node = node.
<fs_bookmark>-start_height = node->get_height( ).
when 'bookmarkEnd'.
<fs_bookmark>-end_node = node.
<fs_bookmark>-end_height = node->get_height( ).
endcase.
when 'name'.
lv_name = attribute->get_value( ) .
translate lv_name to upper case.
attribute->set_value( lv_name ) .
<fs_bookmark>-name = lv_name.
endcase.
enddo.
enddo.
* table or not
data
: lv_node type t_ref_node
, lv_ref_node type t_ref_node
, lv_ref_node_2 type t_ref_node
.
loop at lt_bokkmarks assigning <fs_bookmark>.
lv_node = <fs_bookmark>-start_node.
do .
if lv_node->is_root( ) is not initial.
exit.
endif.
data(node_name) = lv_node->get_name( ).
if node_name = 'tr'.
<fs_bookmark>-in_table = 'X'.
exit.
endif.
lv_node = lv_node->get_parent( ).
enddo.
endloop.
sort lt_bokkmarks by id ascending .
loop at lt_bokkmarks assigning <fs_bookmark>.
if <fs_bookmark>-in_table = 'X'.
lv_ref_node = <fs_bookmark>-start_node.
do .
node_name = lv_ref_node->get_name( ).
if node_name = 'tr'.
lv_node = lv_ref_node->get_parent( ).
exit.
endif.
lv_ref_node = lv_ref_node->get_parent( ).
enddo.
<fs_bookmark>-start_node->remove_node( ).
lv_node->insert_child( new_child = <fs_bookmark>-start_node
ref_child = lv_ref_node ).
clear
: lv_ref_node_2
.
lv_ref_node_2 = <fs_bookmark>-end_node.
do .
if lv_ref_node_2 is initial.
exit.
endif.
node_name = lv_ref_node_2->get_name( ).
if node_name = 'tr'.
exit.
endif.
lv_ref_node_2 = lv_ref_node_2->get_parent( ).
enddo.
if lv_ref_node_2 is not initial.
<fs_bookmark>-end_node->remove_node( ).
lv_node->insert_child( new_child = <fs_bookmark>-end_node
ref_child = lv_ref_node_2 ).
* ELSE.
* lv_node->append_child( new_child = <fs_bookmark>-end_node ).
endif.
else.
lv_ref_node = <fs_bookmark>-start_node->get_parent( ).
<fs_bookmark>-start_node->remove_node( ).
lv_node = lv_ref_node->get_parent( ).
lv_node->insert_child( new_child = <fs_bookmark>-start_node
ref_child = lv_ref_node ).
data
: lv_height_start type i
, lv_height_end type i
.
lv_height_start = <fs_bookmark>-start_node->get_height( ).
lv_ref_node = <fs_bookmark>-end_node.
do .
if lv_ref_node is initial.
exit.
endif.
lv_height_end = lv_ref_node->get_height( ).
if lv_height_end = lv_height_start.
exit.
endif.
lv_ref_node = lv_ref_node->get_parent( ).
enddo.
if lv_ref_node ne <fs_bookmark>-end_node.
<fs_bookmark>-end_node->remove_node( ).
lv_node = lv_ref_node->get_parent( ).
lv_node->insert_child( new_child = <fs_bookmark>-end_node
ref_child = lv_ref_node ).
endif.
endif.
endloop.
data
: lv_position type i
, lt_stack_data type table of t_stack_data
, lt_stack_data_sorted type table of t_stack_data
, lt_id type table of string
, lt_collision type table of t_collision
, ls_collision type t_collision
, lt_old type table of t_ref_node
, lt_sorted type table of t_ref_node
.
iterator = document->create_iterator( ).
do .
node = iterator->get_next( ).
if node is initial.
exit.
endif.
check node->get_type( ) = if_ixml_node=>co_node_element.
name = node->get_name( ).
case name.
when 'bookmarkStart'.
when 'bookmarkEnd'.
when others.
add 1 to lv_position.
continue.
endcase.
attributes = node->get_attributes( ).
append initial line to lt_stack_data assigning field-symbol(<fs_stack_data>).
if name = 'bookmarkStart'.
<fs_stack_data>-start = 'X'.
endif.
<fs_stack_data>-position = lv_position.
<fs_stack_data>-collision = lv_position.
<fs_stack_data>-node = node.
ls_collision-collision = lv_position.
ls_collision-count = 1.
collect ls_collision into lt_collision.
do attributes->get_length( ) times.
attribute = attributes->get_item( sy-index - 1 ).
check attribute->get_name( ) = 'id'.
<fs_stack_data>-id = attribute->get_value( ).
collect <fs_stack_data>-id into lt_id.
enddo.
enddo.
lt_stack_data_sorted = lt_stack_data.
data
: lv_length type i
.
loop at lt_id assigning field-symbol(<fs_id>).
clear lv_length.
loop at lt_stack_data_sorted assigning <fs_stack_data> where id = <fs_id>.
if lv_length is initial.
lv_length = <fs_stack_data>-position.
else.
lv_length = <fs_stack_data>-position - lv_length .
endif.
endloop.
loop at lt_stack_data_sorted assigning <fs_stack_data> where id = <fs_id>.
case 'X'.
when <fs_stack_data>-start.
<fs_stack_data>-length = lv_length.
when others.
<fs_stack_data>-length = lv_length * -1 .
endcase.
endloop.
endloop.
sort lt_stack_data_sorted by collision start length descending.
loop at lt_collision assigning field-symbol(<fs_collision>) where count > 1.
refresh
: lt_old
, lt_sorted
.
loop at lt_stack_data assigning <fs_stack_data> where collision = <fs_collision>-collision.
append <fs_stack_data>-node to lt_old.
endloop.
loop at lt_stack_data_sorted assigning <fs_stack_data> where collision = <fs_collision>-collision.
append <fs_stack_data>-node to lt_sorted.
endloop.
check lt_old ne lt_sorted.
read table lt_old assigning field-symbol(<fs_old>) index <fs_collision>-count.
clear lv_ref_node.
lv_node = <fs_old>->get_parent( ).
lv_ref_node = <fs_old>->get_next( ).
loop at lt_old assigning <fs_old>.
<fs_old>->remove_node( ).
endloop.
loop at lt_sorted assigning field-symbol(<fs_sorted>).
if lv_ref_node is not initial.
lv_node->insert_child( new_child = <fs_sorted>
ref_child = lv_ref_node ).
else.
lv_node->append_child( new_child = <fs_sorted> ).
endif.
endloop.
endloop.
endmethod.
method add_file.
zip->delete( name = iv_path ).
zip->add( name = iv_path
content = iv_data ).
endmethod.
method save.
data
: lv_content type xstring
, lv_content2 type xstring
.
* dump_data( node = document
* fname = 'before save' ).
lv_content = me->create_document( ).
data
: lv_string type string
, lt_content_source type table of string
, lt_content_dest type table of string
, lt_string type table of string
, lt_data type table of text255
.
call function 'CRM_IC_XML_XSTRING2STRING'
exporting
inxstring = lv_content
importing
outstring = lv_string.
split lv_string at '[SPACE]' into table lt_string.
data
: lv_len type i
, lv_buf type text255
, lv_pos type i
, lv_rem type i
.
lv_pos = 0.
lv_rem = 255.
loop at lt_string assigning field-symbol(<fs_str>).
lv_len = strlen( <fs_str> ).
while lv_len > 0.
if lv_len > lv_rem.
lv_buf+lv_pos(lv_rem) = <fs_str>(lv_rem).
append lv_buf to lt_data.
<fs_str> = <fs_str>+lv_rem.
lv_pos = 0.
lv_rem = 255.
clear lv_buf.
else.
lv_buf+lv_pos = <fs_str>.
lv_pos = lv_pos + lv_len.
lv_rem = lv_rem - lv_len.
clear <fs_str>.
endif.
lv_len = strlen( <fs_str> ).
endwhile.
if lv_pos < 254.
lv_pos = lv_pos + 1.
lv_rem = lv_rem - 1.
elseif lv_pos = 254.
append lv_buf to lt_data.
lv_pos = 0.
lv_rem = 255.
clear lv_buf.
else.
append lv_buf to lt_data.
lv_pos = 1.
lv_rem = 254.
clear lv_buf.
endif.
endloop.
append lv_buf to lt_data.
field-symbols
: <xstr> type x
.
data
: lv_x1(1) type x
, lv_i type i
, lv_i2 type i
, lv_str TYPE string
.
clear lv_content.
* loop at lt_data assigning <xstr> casting .
* concatenate lv_content <xstr> into lv_content in byte mode.
* endloop.
LOOP AT lt_data ASSIGNING FIELD-SYMBOL(<fs_data>).
CONCATENATE lv_str <fs_data> INTO lv_str RESPECTING BLANKS.
ENDLOOP.
DATA
: lr_conv_out TYPE REF TO cl_abap_conv_out_ce
, lv_echo_xstring TYPE xstring
.
lr_conv_out = cl_abap_conv_out_ce=>create(
* encoding = 'UTF-8' " Кодировка в которую будем преобразовывать
).
lr_conv_out->convert( EXPORTING data = lv_str IMPORTING buffer = lv_content ).
zip->delete( name = me->c_document ).
zip->add( name = me->c_document
content = lv_content ).
lv_content = zip->save( ).
data
: lt_file_tab type solix_tab
, lv_bytecount type i
, lv_path type string
.
lt_file_tab = cl_bcs_convert=>xstring_to_solix( iv_xstring = lv_content ).
lv_bytecount = xstrlen( lv_content ).
if iv_path is initial.
if on_desktop is not initial.
cl_gui_frontend_services=>get_desktop_directory( changing desktop_directory = lv_path ).
else.
cl_gui_frontend_services=>get_temp_directory( changing temp_dir = lv_path ).
endif.
cl_gui_cfw=>flush( ).
else.
lv_path = iv_path.
endif.
concatenate lv_path '\' iv_folder '\' iv_file_name into lv_path.
cl_gui_frontend_services=>gui_download( exporting bin_filesize = lv_bytecount
filename = lv_path
filetype = 'BIN'
changing data_tab = lt_file_tab
exceptions
file_write_error = 1
no_batch = 2
gui_refuse_filetransfer = 3
invalid_type = 4
no_authority = 5
unknown_error = 6
header_not_allowed = 7
separator_not_allowed = 8
filesize_not_allowed = 9
header_too_long = 10
dp_error_create = 11
dp_error_send = 12
dp_error_write = 13
unknown_dp_error = 14
access_denied = 15
dp_out_of_memory = 16
disk_full = 17
dp_timeout = 18
file_not_found = 19
dataprovider_exception = 20
control_flush_error = 21
not_supported_by_gui = 22
error_no_gui = 23
others = 24
).
check no_execute is initial.
cl_gui_frontend_services=>execute( document = lv_path ).
endmethod.
method map_values.
check it_key_value is not initial.
if ir_xml_node is bound.
data(iterator) = ir_xml_node->create_iterator( ).
else.
iterator = document->create_iterator( ).
endif.
do.
data(node) = iterator->get_next( ).
if node is initial.
exit.
endif.
check node->get_type( ) = if_ixml_node=>co_node_element.
check node->get_name( ) = 't'.
data(value) = node->get_value( ).
loop at it_key_value assigning field-symbol(<fs_key_value>).
replace all occurrences of <fs_key_value>-key in value with <fs_key_value>-value.
check sy-subrc = 0.
node->set_value( value ).
endloop.
enddo.
endmethod.
method get_fragment.
data
: lv_found type c
, lv_id type string
, lr_start type ref to if_ixml_node
, lt_node type tt_node
, lv_first_run type c
, lv_start type c
.
data(iterator) = ir_xml_node->create_iterator( ).
data(ixmlfactory) = cl_ixml=>create( ).