aboutsummaryrefslogtreecommitdiffstats
path: root/poi-scratchpad/src/main/java/org/apache/poi/hwpf/converter/AbstractWordConverter.java
blob: 0eca5fb49a8f426a636bb19fd444987c132519f0 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
/* ====================================================================
   Licensed to the Apache Software Foundation (ASF) under one or more
   contributor license agreements.  See the NOTICE file distributed with
   this work for additional information regarding copyright ownership.
   The ASF licenses this file to You under the Apache License, Version 2.0
   (the "License"); you may not use this file except in compliance with
   the License.  You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
==================================================================== */
package org.apache.poi.hwpf.converter;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.logging.log4j.Logger;
import org.apache.poi.logging.PoiLogManager;
import org.apache.poi.hpsf.SummaryInformation;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.HWPFDocumentCore;
import org.apache.poi.hwpf.converter.AbstractWordUtils.NumberingState;
import org.apache.poi.hwpf.converter.FontReplacer.Triplet;
import org.apache.poi.hwpf.model.FieldsDocumentPart;
import org.apache.poi.hwpf.usermodel.Bookmark;
import org.apache.poi.hwpf.usermodel.CharacterRun;
import org.apache.poi.hwpf.usermodel.Field;
import org.apache.poi.hwpf.usermodel.HWPFList;
import org.apache.poi.hwpf.usermodel.Notes;
import org.apache.poi.hwpf.usermodel.OfficeDrawing;
import org.apache.poi.hwpf.usermodel.Paragraph;
import org.apache.poi.hwpf.usermodel.Picture;
import org.apache.poi.hwpf.usermodel.PictureType;
import org.apache.poi.hwpf.usermodel.Range;
import org.apache.poi.hwpf.usermodel.Section;
import org.apache.poi.hwpf.usermodel.Table;
import org.apache.poi.hwpf.usermodel.TableCell;
import org.apache.poi.hwpf.usermodel.TableRow;
import org.apache.poi.poifs.filesystem.Entry;
import org.apache.poi.util.Beta;
import org.apache.poi.util.Internal;
import org.apache.poi.util.LocaleUtil;
import org.apache.poi.util.StringUtil;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import static org.apache.logging.log4j.util.Unbox.box;

@Beta
public abstract class AbstractWordConverter {
    private static class DeadFieldBoundaries {
        final int beginMark;
        final int endMark;
        final int separatorMark;

        public DeadFieldBoundaries(int beginMark, int separatorMark,
            int endMark) {
            this.beginMark = beginMark;
            this.separatorMark = separatorMark;
            this.endMark = endMark;
        }
    }

    private static final class Structure implements Comparable<Structure> {
        final int end;
        final int start;
        final Object structure;

        Structure(Bookmark bookmark) {
            this.start = bookmark.getStart();
            this.end = bookmark.getEnd();
            this.structure = bookmark;
        }

        Structure(DeadFieldBoundaries deadFieldBoundaries, int start, int end) {
            this.start = start;
            this.end = end;
            this.structure = deadFieldBoundaries;
        }

        Structure(Field field) {
            this.start = field.getFieldStartOffset();
            this.end = field.getFieldEndOffset();
            this.structure = field;
        }

        public int compareTo(Structure o) {
            return Integer.compare(start, o.start);
        }

        @Override
        public String toString() {
            return "Structure [" + start + "; " + end + "): "
                + structure;
        }
    }

    private static final byte BEL_MARK = 7;

    private static final byte FIELD_BEGIN_MARK = 19;
    private static final byte FIELD_END_MARK = 21;
    private static final byte FIELD_SEPARATOR_MARK = 20;
    private static final int FIELD_PAGE_REFERENCE = 37;
    private static final int FIELD_EMBEDDED_OBJECT = 58;
    private static final int FIELD_DROP_DOWN = 83;
    private static final int FIELD_HYPERLINK = 88;


    private static final Logger LOG = PoiLogManager.getLogger(AbstractWordConverter.class);

    private static final Pattern PATTERN_HYPERLINK_EXTERNAL = Pattern
        .compile("^[ \\t\\r\\n]*HYPERLINK \"(.*)\".*$");

    private static final Pattern PATTERN_HYPERLINK_LOCAL = Pattern
        .compile("^[ \\t\\r\\n]*HYPERLINK \\\\l \"(.*)\"[ ](.*)$");

    private static final Pattern PATTERN_PAGEREF = Pattern
        .compile("^[ \\t\\r\\n]*PAGEREF ([^ ]*)[ \\t\\r\\n]*\\\\h.*$");

    private static final byte SPECCHAR_AUTONUMBERED_FOOTNOTE_REFERENCE = 2;
    private static final byte SPECCHAR_DRAWN_OBJECT = 8;

    protected static final char UNICODECHAR_NO_BREAK_SPACE = '\u00a0';
    protected static final char UNICODECHAR_NONBREAKING_HYPHEN = '\u2011';
    protected static final char UNICODECHAR_ZERO_WIDTH_SPACE = '\u200b';

    private static void addToStructures(List<Structure> structures,
        Structure structure) {
        for (Iterator<Structure> iterator = structures.iterator(); iterator
            .hasNext(); ) {
            Structure another = iterator.next();

            if (another.start <= structure.start
                && another.end >= structure.start) {
                return;
            }

            if ((structure.start < another.start && another.start < structure.end)
                || (structure.start < another.start && another.end <= structure.end)
                || (structure.start <= another.start && another.end < structure.end)) {
                iterator.remove();
            }
        }
        structures.add(structure);
    }

    private final Set<Bookmark> bookmarkStack = new LinkedHashSet<>();

    private FontReplacer fontReplacer = new DefaultFontReplacer();

    private final NumberingState numberingState = new NumberingState();

    private PicturesManager picturesManager;

    /**
     * Special actions that need to be called after processing complete, like
     * updating stylesheets or building document notes list. Usually they are
     * called once, but it's okay to call them several times.
     */
    protected void afterProcess() {
        // by default no such actions needed
    }

    protected Triplet getCharacterRunTriplet(CharacterRun characterRun) {
        Triplet original = new Triplet();
        original.bold = characterRun.isBold();
        original.italic = characterRun.isItalic();
        original.fontName = characterRun.getFontName();
        return getFontReplacer().update(original);
    }

    public abstract Document getDocument();

    public FontReplacer getFontReplacer() {
        return fontReplacer;
    }

    protected int getNumberColumnsSpanned(int[] tableCellEdges,
        int currentEdgeIndex, TableCell tableCell) {
        int nextEdgeIndex = currentEdgeIndex;
        int colSpan = 0;
        int cellRightEdge = tableCell.getLeftEdge() + tableCell.getWidth();
        while (tableCellEdges[nextEdgeIndex] < cellRightEdge) {
            colSpan++;
            nextEdgeIndex++;
        }
        return colSpan;
    }

    protected int getNumberRowsSpanned(Table table,
        final int[] tableCellEdges, int currentRowIndex,
        int currentColumnIndex, TableCell tableCell) {
        if (!tableCell.isFirstVerticallyMerged()) {
            return 1;
        }

        final int numRows = table.numRows();

        int count = 1;
        for (int r1 = currentRowIndex + 1; r1 < numRows; r1++) {
            TableRow nextRow = table.getRow(r1);
            if (currentColumnIndex >= nextRow.numCells()) {
                break;
            }

            // we need to skip row if he don't have cells at all
            boolean hasCells = false;
            int currentEdgeIndex = 0;
            for (int c = 0; c < nextRow.numCells(); c++) {
                TableCell nextTableCell = nextRow.getCell(c);
                if (!nextTableCell.isVerticallyMerged()
                    || nextTableCell.isFirstVerticallyMerged()) {
                    int colSpan = getNumberColumnsSpanned(tableCellEdges,
                        currentEdgeIndex, nextTableCell);
                    currentEdgeIndex += colSpan;

                    if (colSpan != 0) {
                        hasCells = true;
                        break;
                    }
                } else {
                    currentEdgeIndex += getNumberColumnsSpanned(
                        tableCellEdges, currentEdgeIndex, nextTableCell);
                }
            }
            if (!hasCells) {
                continue;
            }

            TableCell nextCell = nextRow.getCell(currentColumnIndex);
            if (!nextCell.isVerticallyMerged()
                || nextCell.isFirstVerticallyMerged()) {
                break;
            }
            count++;
        }
        return count;
    }

    public PicturesManager getPicturesManager() {
        return picturesManager;
    }

    protected abstract void outputCharacters(Element block,
        CharacterRun characterRun, String text);

    /**
     * Wrap range into bookmark(s) and process it. All bookmarks have starts
     * equal to range start and ends equal to range end. Usually it's only one
     * bookmark.
     */
    protected abstract void processBookmarks(HWPFDocumentCore wordDocument,
        Element currentBlock, Range range, int currentTableLevel,
        List<Bookmark> rangeBookmarks);

    protected boolean processCharacters(final HWPFDocumentCore wordDocument,
        final int currentTableLevel, final Range range, final Element block) {
        if (range == null) {
            return false;
        }

        boolean haveAnyText = false;

        /*
         * In text there can be fields, bookmarks, may be other structures (code
         * below allows extension). Those structures can overlaps, so either we
         * should process char-by-char (slow) or find a correct way to
         * reconstruct the structure of range -- sergey
         */
        List<Structure> structures = new LinkedList<>();
        if (wordDocument instanceof HWPFDocument) {
            final HWPFDocument doc = (HWPFDocument) wordDocument;

            Map<Integer, List<Bookmark>> rangeBookmarks = doc.getBookmarks()
                .getBookmarksStartedBetween(range.getStartOffset(),
                    range.getEndOffset());

            if (rangeBookmarks != null) {
                for (List<Bookmark> lists : rangeBookmarks.values()) {
                    for (Bookmark bookmark : lists) {
                        if (!bookmarkStack.contains(bookmark)) {
                            addToStructures(structures, new Structure(
                                bookmark));
                        }
                    }
                }
            }

            // TODO: dead fields?
            int skipUntil = -1;
            for (int c = 0; c < range.numCharacterRuns(); c++) {
                CharacterRun characterRun = range.getCharacterRun(c);
                if (characterRun == null) {
                    throw new AssertionError();
                }
                if (characterRun.getStartOffset() < skipUntil) {
                    continue;
                }
                String text = characterRun.text();
                if (text == null || text.isEmpty() || text.charAt(0) != FIELD_BEGIN_MARK) {
                    continue;
                }

                Field aliveField = ((HWPFDocument) wordDocument).getFields()
                    .getFieldByStartOffset(FieldsDocumentPart.MAIN,
                        characterRun.getStartOffset());
                if (aliveField != null) {
                    addToStructures(structures, new Structure(aliveField));
                } else {
                    int[] separatorEnd = tryDeadField_lookupFieldSeparatorEnd(
                        range, c);
                    if (separatorEnd != null) {
                        addToStructures(
                            structures,
                            new Structure(new DeadFieldBoundaries(c,
                                separatorEnd[0], separatorEnd[1]),
                                characterRun.getStartOffset(), range
                                .getCharacterRun(
                                    separatorEnd[1])
                                .getEndOffset()));
                        c = separatorEnd[1];
                    }
                }
            }
        }

        structures = new ArrayList<>(structures);
        Collections.sort(structures);

        int previous = range.getStartOffset();
        for (Structure structure : structures) {
            if (structure.start != previous) {
                Range subrange = new Range(previous, structure.start, range) {
                    @Override
                    public String toString() {
                        return "BetweenStructuresSubrange " + super.toString();
                    }
                };
                processCharacters(wordDocument, currentTableLevel, subrange,
                    block);
            }

            if (structure.structure instanceof Bookmark) {
                assert(wordDocument instanceof HWPFDocument);
                // other bookmarks with same boundaries
                List<Bookmark> bookmarks = new LinkedList<>();
                for (Bookmark bookmark : ((HWPFDocument) wordDocument)
                    .getBookmarks()
                    .getBookmarksStartedBetween(structure.start,
                        structure.start + 1).values().iterator()
                    .next()) {
                    if (bookmark.getStart() == structure.start
                        && bookmark.getEnd() == structure.end) {
                        bookmarks.add(bookmark);
                    }
                }

                bookmarkStack.addAll(bookmarks);
                try {
                    int end = Math.min(range.getEndOffset(), structure.end);
                    Range subrange = new Range(structure.start, end, range) {
                        @Override
                        public String toString() {
                            return "BookmarksSubrange " + super.toString();
                        }
                    };

                    processBookmarks(wordDocument, block, subrange,
                        currentTableLevel, bookmarks);
                } finally {
                    bookmarkStack.removeAll(bookmarks);
                }
            } else if (structure.structure instanceof Field) {
                assert(wordDocument instanceof HWPFDocument);
                Field field = (Field) structure.structure;
                processField((HWPFDocument) wordDocument, range, currentTableLevel, field, block);
            } else if (structure.structure instanceof DeadFieldBoundaries) {
                DeadFieldBoundaries boundaries = (DeadFieldBoundaries) structure.structure;
                processDeadField(wordDocument, block, range,
                    currentTableLevel, boundaries.beginMark,
                    boundaries.separatorMark, boundaries.endMark);
            } else {
                throw new UnsupportedOperationException("NYI: "
                    + structure.structure.getClass());
            }

            previous = Math.min(range.getEndOffset(), structure.end);
        }

        if (previous != range.getStartOffset()) {
            if (previous > range.getEndOffset()) {
                LOG.atWarn().log("Latest structure in {} ended at #{} after range boundaries [{}; {})",
                        range, box(previous),box(range.getStartOffset()),box(range.getEndOffset()));
                return true;
            }

            if (previous < range.getEndOffset()) {
                Range subrange = new Range(previous, range.getEndOffset(),
                    range) {
                    @Override
                    public String toString() {
                        return "AfterStructureSubrange " + super.toString();
                    }
                };
                processCharacters(wordDocument, currentTableLevel, subrange,
                    block);
            }
            return true;
        }

        for (int c = 0; c < range.numCharacterRuns(); c++) {
            CharacterRun characterRun = range.getCharacterRun(c);

            if (characterRun == null) {
                throw new AssertionError();
            }

            if (wordDocument instanceof HWPFDocument
                && ((HWPFDocument) wordDocument).getPicturesTable()
                .hasPicture(characterRun)) {
                HWPFDocument newFormat = (HWPFDocument) wordDocument;
                Picture picture = newFormat.getPicturesTable().extractPicture(
                    characterRun, true);

                processImage(block, characterRun.text().charAt(0) == 0x01,
                    picture);
                continue;
            }

            String text = characterRun.text();
            if (text.isEmpty()) {
                continue;
            }

            if (characterRun.isCapitalized() || characterRun.isSmallCaps()) {
                text = text.toUpperCase(LocaleUtil.getUserLocale());
            }

            if (characterRun.isSpecialCharacter()) {
                if (text.charAt(0) == SPECCHAR_AUTONUMBERED_FOOTNOTE_REFERENCE
                    && (wordDocument instanceof HWPFDocument)) {
                    HWPFDocument doc = (HWPFDocument) wordDocument;
                    processNoteAnchor(doc, characterRun, block);
                    continue;
                }
                if (text.charAt(0) == SPECCHAR_DRAWN_OBJECT
                    && (wordDocument instanceof HWPFDocument)) {
                    HWPFDocument doc = (HWPFDocument) wordDocument;
                    processDrawnObject(doc, characterRun, block);
                    continue;
                }
                if (characterRun.isOle2()
                    && (wordDocument instanceof HWPFDocument)) {
                    HWPFDocument doc = (HWPFDocument) wordDocument;
                    processOle2(doc, characterRun, block);
                    continue;
                }
                if (characterRun.isSymbol()
                    && (wordDocument instanceof HWPFDocument)) {
                    HWPFDocument doc = (HWPFDocument) wordDocument;
                    processSymbol(doc, characterRun, block);
                    continue;
                }
            }

            if (text.charAt(0) == FIELD_BEGIN_MARK) {
                if (wordDocument instanceof HWPFDocument) {
                    Field aliveField = ((HWPFDocument) wordDocument)
                        .getFields().getFieldByStartOffset(
                            FieldsDocumentPart.MAIN,
                            characterRun.getStartOffset());
                    if (aliveField != null) {
                        processField(((HWPFDocument) wordDocument), range,
                            currentTableLevel, aliveField, block);

                        int continueAfter = aliveField.getFieldEndOffset();
                        while (c < range.numCharacterRuns()
                            && range.getCharacterRun(c).getEndOffset() <= continueAfter) {
                            c++;
                        }

                        if (c < range.numCharacterRuns()) {
                            c--;
                        }

                        continue;
                    }
                }

                int skipTo = tryDeadField(wordDocument, range,
                    currentTableLevel, c, block);

                if (skipTo != c) {
                    c = skipTo;
                    continue;
                }

                continue;
            }
            if (text.charAt(0) == FIELD_SEPARATOR_MARK) {
                // shall not appear without FIELD_BEGIN_MARK
                continue;
            }
            if (text.charAt(0) == FIELD_END_MARK) {
                // shall not appear without FIELD_BEGIN_MARK
                continue;
            }

            if (characterRun.isSpecialCharacter() || characterRun.isObj()
                || characterRun.isOle2()) {
                continue;
            }

            if (text.endsWith("\r")
                || (text.charAt(text.length() - 1) == BEL_MARK && currentTableLevel != Integer.MIN_VALUE)) {
                text = text.substring(0, text.length() - 1);
            }

            {
                // line breaks
                StringBuilder stringBuilder = new StringBuilder();
                for (char charChar : text.toCharArray()) {
                    if (charChar == 11) {
                        if (stringBuilder.length() > 0) {
                            outputCharacters(block, characterRun,
                                stringBuilder.toString());
                            stringBuilder.setLength(0);
                        }
                        processLineBreak(block, characterRun);
                    } else if (charChar == 30) {
                        // Non-breaking hyphens are stored as ASCII 30
                        stringBuilder.append(UNICODECHAR_NONBREAKING_HYPHEN);
                    } else if (charChar == 31) {
                        // Non-required hyphens to zero-width space
                        stringBuilder.append(UNICODECHAR_ZERO_WIDTH_SPACE);
                    } else if (charChar >= 0x20 || charChar == 0x09
                        || charChar == 0x0A || charChar == 0x0D) {
                        stringBuilder.append(charChar);
                    }
                }
                if (stringBuilder.length() > 0) {
                    outputCharacters(block, characterRun,
                        stringBuilder.toString());
                    stringBuilder.setLength(0);
                }
            }

            haveAnyText |= StringUtil.isNotBlank(text);
        }

        return haveAnyText;
    }

    protected void processDeadField(HWPFDocumentCore wordDocument,
        Element currentBlock, Range range, int currentTableLevel,
        int beginMark, int separatorMark, int endMark) {
        if (beginMark + 1 < separatorMark && separatorMark + 1 < endMark) {
            Range formulaRange = new Range(range.getCharacterRun(
                beginMark + 1).getStartOffset(), range.getCharacterRun(
                separatorMark - 1).getEndOffset(), range) {
                @Override
                public String toString() {
                    return "Dead field formula subrange: " + super.toString();
                }
            };
            Range valueRange = new Range(range.getCharacterRun(
                separatorMark + 1).getStartOffset(), range
                .getCharacterRun(endMark - 1).getEndOffset(), range) {
                @Override
                public String toString() {
                    return "Dead field value subrange: " + super.toString();
                }
            };
            String formula = formulaRange.text();
            final Matcher matcher = PATTERN_HYPERLINK_LOCAL.matcher(formula);
            if (matcher.matches()) {
                String localref = matcher.group(1);
                processPageref(wordDocument, currentBlock, valueRange,
                    currentTableLevel, localref);
                return;
            }
        }

        StringBuilder debug = new StringBuilder("Unsupported field type: \n");
        for (int i = beginMark; i <= endMark; i++) {
            debug.append("\t");
            debug.append(range.getCharacterRun(i));
            debug.append("\n");
        }
        LOG.atWarn().log(debug);

        Range deadFieldValueSubrage = new Range(range.getCharacterRun(
            separatorMark).getStartOffset() + 1, range.getCharacterRun(
            endMark).getStartOffset(), range) {
            @Override
            public String toString() {
                return "DeadFieldValueSubrange (" + super.toString() + ")";
            }
        };

        // just output field value
        if (separatorMark + 1 < endMark) {
            processCharacters(wordDocument, currentTableLevel,
                deadFieldValueSubrage, currentBlock);
        }
    }

    public void processDocument(HWPFDocumentCore wordDocument) {
        try {
            final SummaryInformation summaryInformation = wordDocument
                .getSummaryInformation();
            if (summaryInformation != null) {
                processDocumentInformation(summaryInformation);
            }
        } catch (Exception exc) {
            LOG.atWarn().withThrowable(exc).log("Unable to process document summary information");
        }

        final Range docRange = wordDocument.getRange();

        if (docRange.numSections() == 1) {
            processSingleSection(wordDocument, docRange.getSection(0));
            afterProcess();
            return;
        }

        processDocumentPart(wordDocument, docRange);
        afterProcess();
    }

    protected abstract void processDocumentInformation(
        SummaryInformation summaryInformation);

    protected void processDocumentPart(HWPFDocumentCore wordDocument,
        final Range range) {
        for (int s = 0; s < range.numSections(); s++) {
            processSection(wordDocument, range.getSection(s), s);
        }
    }

    protected void processDrawnObject(HWPFDocument doc,
        CharacterRun characterRun, Element block) {
        if (getPicturesManager() == null) {
            return;
        }

        // TODO: support headers
        OfficeDrawing officeDrawing = doc.getOfficeDrawingsMain()
            .getOfficeDrawingAt(characterRun.getStartOffset());
        if (officeDrawing == null) {
            LOG.atWarn().log("Characters #{} references missing drawn object", characterRun);
            return;
        }

        byte[] pictureData = officeDrawing.getPictureData();
        if (pictureData == null)
        // usual shape?
        {
            return;
        }

        float width = (officeDrawing.getRectangleRight() - officeDrawing
            .getRectangleLeft()) / AbstractWordUtils.TWIPS_PER_INCH;
        float height = (officeDrawing.getRectangleBottom() - officeDrawing
            .getRectangleTop()) / AbstractWordUtils.TWIPS_PER_INCH;

        final PictureType type = PictureType.findMatchingType(pictureData);
        String path = getPicturesManager()
            .savePicture(pictureData, type,
                "s" + characterRun.getStartOffset() + "." + type,
                width, height);

        processDrawnObject(doc, characterRun, officeDrawing, path, block);
    }

    protected abstract void processDrawnObject(HWPFDocument doc,
        CharacterRun characterRun, OfficeDrawing officeDrawing,
        String path, Element block);

    protected void processDropDownList(Element block,
        CharacterRun characterRun, String[] values, int defaultIndex) {
        outputCharacters(block, characterRun, values[defaultIndex]);
    }

    protected abstract void processEndnoteAutonumbered(
        HWPFDocument wordDocument, int noteIndex, Element block,
        Range endnoteTextRange);

    protected void processField(HWPFDocument wordDocument, Range parentRange,
        int currentTableLevel, Field field, Element currentBlock) {
        switch (field.getType()) {
            case FIELD_PAGE_REFERENCE: {
                final Range firstSubrange = field.firstSubrange(parentRange);
                if (firstSubrange != null) {
                    String formula = firstSubrange.text();
                    Matcher matcher = PATTERN_PAGEREF.matcher(formula);
                    if (matcher.find()) {
                        String pageref = matcher.group(1);
                        processPageref(wordDocument, currentBlock,
                            field.secondSubrange(parentRange),
                            currentTableLevel, pageref);
                        return;
                    }
                }
                break;
            }
            case FIELD_EMBEDDED_OBJECT: {
                if (!field.hasSeparator()) {
                    LOG.atWarn().log("{} contains {} with 'Embedded Object' but without separator mark", parentRange, field);
                    return;
                }

                CharacterRun separator = field
                    .getMarkSeparatorCharacterRun(parentRange);

                if (separator.isOle2()) {
                    // the only supported so far
                    boolean processed = processOle2(wordDocument, separator,
                        currentBlock);

                    // if we didn't output OLE - output field value
                    if (!processed) {
                        processCharacters(wordDocument, currentTableLevel,
                            field.secondSubrange(parentRange), currentBlock);
                    }

                    return;
                }

                break;
            }
            case FIELD_DROP_DOWN: {
                Range fieldContent = field.firstSubrange(parentRange);
                if (fieldContent == null) {
                    throw new IllegalStateException("Cannot read field content from field " + field + " and range " + parentRange);
                }

                CharacterRun cr = fieldContent.getCharacterRun(fieldContent
                    .numCharacterRuns() - 1);
                String[] values = cr.getDropDownListValues();
                Integer defIndex = cr.getDropDownListDefaultItemIndex();

                if (values != null && values.length > 0) {
                    processDropDownList(currentBlock, cr, values,
                        defIndex == null ? -1 : defIndex);
                    return;
                }
                break;
            }
            case FIELD_HYPERLINK: {
                final Range firstSubrange = field.firstSubrange(parentRange);
                if (firstSubrange != null) {
                    String formula = firstSubrange.text();
                    Matcher matcher = PATTERN_HYPERLINK_EXTERNAL.matcher(formula);
                    if (matcher.matches()) {
                        String hyperlink = matcher.group(1);
                        processHyperlink(wordDocument, currentBlock,
                            field.secondSubrange(parentRange),
                            currentTableLevel, hyperlink);
                        return;
                    }
                    matcher.usePattern(PATTERN_HYPERLINK_LOCAL);
                    if (matcher.matches()) {
                        String hyperlink = matcher.group(1);
                        Range textRange = null;
                        String text = matcher.group(2);
                        if (AbstractWordUtils.isNotEmpty(text)) {
                            textRange = new Range(firstSubrange.getStartOffset()
                                + matcher.start(2),
                                firstSubrange.getStartOffset()
                                    + matcher.end(2), firstSubrange) {
                                @Override
                                public String toString() {
                                    return "Local hyperlink text";
                                }
                            };
                        }
                        processPageref(wordDocument, currentBlock, textRange,
                            currentTableLevel, hyperlink);
                        return;
                    }
                }
                break;
            }
        }

        LOG.atWarn().log("{} contains {} with unsupported type or format", parentRange, field);
        processCharacters(wordDocument, currentTableLevel,
            field.secondSubrange(parentRange), currentBlock);
    }

    protected abstract void processFootnoteAutonumbered(
        HWPFDocument wordDocument, int noteIndex, Element block,
        Range footnoteTextRange);

    protected abstract void processHyperlink(HWPFDocumentCore wordDocument,
        Element currentBlock, Range textRange, int currentTableLevel,
        String hyperlink);

    protected void processImage(Element currentBlock, boolean inlined, Picture picture) {
        PicturesManager fileManager = getPicturesManager();
        if (fileManager != null) {
            final float aspectRatioX = picture.getHorizontalScalingFactor();
            final float aspectRatioY = picture.getVerticalScalingFactor();

            float imageWidth = picture.getDxaGoal();
            if (aspectRatioX > 0) {
                imageWidth *= aspectRatioX / 1000f;
            }
            imageWidth /= AbstractWordUtils.TWIPS_PER_INCH;

            float imageHeight = picture.getDyaGoal();
            if (aspectRatioY > 0) {
                imageHeight *= aspectRatioY / 1000f;
            }
            imageHeight /= AbstractWordUtils.TWIPS_PER_INCH;

            String url = fileManager.savePicture(picture.getContent(),
                picture.suggestPictureType(),
                picture.suggestFullFileName(), imageWidth, imageHeight);

            if (AbstractWordUtils.isNotEmpty(url)) {
                processImage(currentBlock, inlined, picture, url);
                return;
            }
        }

        processImageWithoutPicturesManager(currentBlock, inlined, picture);

    }

    protected abstract void processImage(Element currentBlock,
        boolean inlined, Picture picture, String url);

    @Internal
    protected abstract void processImageWithoutPicturesManager(
        Element currentBlock, boolean inlined, Picture picture);

    protected abstract void processLineBreak(Element block, CharacterRun characterRun);

    protected void processNoteAnchor(HWPFDocument doc, CharacterRun characterRun, final Element block) {
        Notes footnotes = doc.getFootnotes();
        int footIndex = footnotes.getNoteIndexByAnchorPosition(characterRun.getStartOffset());
        if (footIndex != -1) {
            Range footRange = doc.getFootnoteRange();
            int rangeStartOffset = footRange.getStartOffset();
            int noteTextStartOffset = footnotes.getNoteTextStartOffset(footIndex);
            int noteTextEndOffset = footnotes.getNoteTextEndOffset(footIndex);

            Range noteTextRange = new Range(rangeStartOffset
                + noteTextStartOffset, rangeStartOffset
                + noteTextEndOffset, doc);

            processFootnoteAutonumbered(doc, footIndex, block, noteTextRange);
            return;
        }

        Notes endnotes = doc.getEndnotes();
        int endIndex = endnotes.getNoteIndexByAnchorPosition(characterRun.getStartOffset());
        if (endIndex != -1) {
            Range endnoteRange = doc.getEndnoteRange();
            int rangeStartOffset = endnoteRange.getStartOffset();
            int noteTextStartOffset = endnotes.getNoteTextStartOffset(endIndex);
            int noteTextEndOffset = endnotes.getNoteTextEndOffset(endIndex);

            Range noteTextRange = new Range(rangeStartOffset
                + noteTextStartOffset, rangeStartOffset
                + noteTextEndOffset, doc);

            processEndnoteAutonumbered(doc, endIndex, block, noteTextRange);
        }
    }

    private boolean processOle2(HWPFDocument doc, CharacterRun characterRun,
        Element block) {
        Entry entry = doc.getObjectsPool().getObjectById("_" + characterRun.getPicOffset());
        if (entry == null) {
            LOG.atWarn().log("Referenced OLE2 object '{}' not found in ObjectPool", box(characterRun.getPicOffset()));
            return false;
        }

        try {
            return processOle2(doc, block, entry);
        } catch (Exception exc) {
            LOG.atWarn().withThrowable(exc).log("Unable to convert internal OLE2 object '{}'", box(characterRun.getPicOffset()));
            return false;
        }
    }

    protected boolean processOle2(HWPFDocument wordDocument, Element block, Entry entry) throws Exception {
        return false;
    }

    protected abstract void processPageBreak(HWPFDocumentCore wordDocument, Element flow);

    protected abstract void processPageref(HWPFDocumentCore wordDocument,
        Element currentBlock, Range textRange, int currentTableLevel,
        String pageref);

    protected abstract void processParagraph(HWPFDocumentCore wordDocument,
        Element parentElement, int currentTableLevel, Paragraph paragraph,
        String bulletText);

    protected void processParagraphes(HWPFDocumentCore wordDocument, Element flow, Range range, int currentTableLevel) {
        final int paragraphs = range.numParagraphs();
        for (int p = 0; p < paragraphs; p++) {
            Paragraph paragraph = range.getParagraph(p);

            if (paragraph.isInTable()
                && paragraph.getTableLevel() != currentTableLevel) {
                if (paragraph.getTableLevel() < currentTableLevel) {
                    throw new IllegalStateException("Trying to process table cell with higher level (" +
                        paragraph.getTableLevel() + ") than current table level ("
                        + currentTableLevel + ") as inner table part");
                }

                Table table = range.getTable(paragraph);
                processTable(wordDocument, flow, table);

                p += table.numParagraphs() - 1;
                continue;
            }

            if (paragraph.text().equals("\u000c")) {
                processPageBreak(wordDocument, flow);
            }

            boolean processed = false;
            if (paragraph.isInList()) {
                try {
                    HWPFList hwpfList = paragraph.getList();

                    String label = AbstractWordUtils.getBulletText(
                        numberingState, hwpfList,
                        (char) paragraph.getIlvl());

                    processParagraph(wordDocument, flow, currentTableLevel, paragraph, label);
                    processed = true;
                } catch (Exception exc) {
                    LOG.atWarn().withThrowable(exc).log("Can't process paragraph as list entry, will be processed without list information");
                }
            }

            if (!processed) {
                processParagraph(wordDocument, flow, currentTableLevel,
                    paragraph, AbstractWordUtils.EMPTY);
            }
        }

    }

    protected abstract void processSection(HWPFDocumentCore wordDocument, Section section, int s);

    protected void processSingleSection(HWPFDocumentCore wordDocument, Section section) {
        processSection(wordDocument, section, 0);
    }

    protected void processSymbol(HWPFDocument doc, CharacterRun characterRun, Element block) {

    }

    protected abstract void processTable(HWPFDocumentCore wordDocument, Element flow, Table table);

    public void setFontReplacer(FontReplacer fontReplacer) {
        this.fontReplacer = fontReplacer;
    }

    public void setPicturesManager(PicturesManager fileManager) {
        this.picturesManager = fileManager;
    }

    protected int tryDeadField(HWPFDocumentCore wordDocument, Range range,
        int currentTableLevel, int beginMark, Element currentBlock) {
        int[] separatorEnd = tryDeadField_lookupFieldSeparatorEnd(
            range, beginMark);
        if (separatorEnd == null) {
            return beginMark;
        }

        processDeadField(wordDocument, currentBlock, range, currentTableLevel,
            beginMark, separatorEnd[0], separatorEnd[1]);

        return separatorEnd[1];
    }

    private int[] tryDeadField_lookupFieldSeparatorEnd(Range range, int beginMark) {
        int separatorMark = -1;
        int endMark = -1;
        for (int c = beginMark + 1; c < range.numCharacterRuns(); c++) {
            CharacterRun characterRun = range.getCharacterRun(c);

            String text = characterRun.text();
            if (text.isEmpty()) {
                continue;
            }

            final char firstByte = text.charAt(0);
            if (firstByte == FIELD_BEGIN_MARK) {
                int[] nested = tryDeadField_lookupFieldSeparatorEnd(
                    range, c);
                if (nested != null) {
                    c = nested[1];
                }
                continue;
            }

            if (firstByte == FIELD_SEPARATOR_MARK) {
                if (separatorMark != -1) {
                    // double; incorrect format
                    return null;
                }

                separatorMark = c;
                continue;
            }

            if (firstByte == FIELD_END_MARK) {
                endMark = c;
                break;
            }
        }

        if (separatorMark == -1 || endMark == -1) {
            return null;
        }

        return new int[]{separatorMark, endMark};
    }

}