aboutsummaryrefslogtreecommitdiffstats
path: root/poi-scratchpad/src/main/java/org/apache/poi/hwpf/usermodel/HeaderStories.java
blob: 738253c3d5020665ba160379a2d0abf1f9424ac0 (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
/* ====================================================================
   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.usermodel;

import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.model.FileInformationBlock;
import org.apache.poi.hwpf.model.GenericPropertyNode;
import org.apache.poi.hwpf.model.PlexOfCps;
import org.apache.poi.hwpf.model.SubdocumentType;

/**
 * A HeaderStory is a Header, a Footer, or footnote/endnote
 *  separator.
 * All the Header Stories get stored in the same Range in the
 *  document, and this handles getting out all the individual
 *  parts.
 *
 * WARNING - you shouldn't change the headers or footers,
 *  as offsets are not yet updated!
 */
public final class HeaderStories {
    private final Range headerStories;
    private PlexOfCps plcfHdd;

    private boolean stripFields;

    public HeaderStories(HWPFDocument doc) {
        this.headerStories = doc.getHeaderStoryRange();
        FileInformationBlock fib = doc.getFileInformationBlock();

//        // If there's no PlcfHdd, nothing to do
//        if(fib.getCcpHdd() == 0) {
//            return;
//        }

        if (fib.getSubdocumentTextStreamLength( SubdocumentType.HEADER ) == 0)
            return;

        if(fib.getPlcfHddSize() == 0) {
            return;
        }

        // Handle the PlcfHdd
        /*
         * Page 88:
         *
         * "The plcfhdd, a table whose location and length within the file is
         * stored in fib.fcPlcfhdd and fib.cbPlcfhdd, describes where the text
         * of each header/footer begins. If there are n headers/footers stored
         * in the Word file, the plcfhdd consists of n+2 CP entries. The
         * beginning CP of the ith header/footer is the ith CP in the plcfhdd.
         * The limit CP (the CP of character 1 position past the end of a
         * header/footer) of the ith header/footer is the i+1st CP in the
         * plcfhdd. Note: at the limit CP - 1, Word always places a chEop as a
         * placeholder which is never displayed as part of the header/footer.
         * This allows Word to change an existing header/footer to be empty.
         *
         * If there are n header/footers, the n+2nd CP entry value is always 1
         * greater than the n+1st CP entry value. A paragraph end (ASCII 13) is
         * always stored at the file position marked by the n+1st CP value.
         *
         * The transformation in a full saved file from a header/footer CP to an
         * offset from the beginning of a file (fc) is
         * fc=fib.fcMin+ccpText+ccpFtn+cp."
         */
        plcfHdd = new PlexOfCps( doc.getTableStream(), fib.getPlcfHddOffset(),
                fib.getPlcfHddSize(), 0 );
    }

    /**
     * @deprecated 3.8 beta 4
     */
    @Deprecated
    public String getFootnoteSeparator()
    {
        return getAt( 0 );
    }

    /**
     * @deprecated 3.8 beta 4
     */
    @Deprecated
    public String getFootnoteContSeparator()
    {
        return getAt( 1 );
    }

    /**
     * @deprecated 3.8 beta 4
     */
    @Deprecated
    public String getFootnoteContNote()
    {
        return getAt( 2 );
    }

    /**
     * @deprecated 3.8 beta 4
     */
    @Deprecated
    public String getEndnoteSeparator()
    {
        return getAt( 3 );
    }

    /**
     * @deprecated 3.8 beta 4
     */
    @Deprecated
    public String getEndnoteContSeparator()
    {
        return getAt( 4 );
    }

    /**
     * @deprecated 3.8 beta 4
     */
    @Deprecated
    public String getEndnoteContNote()
    {
        return getAt( 5 );
    }

    public Range getFootnoteSeparatorSubrange()
    {
        return getSubrangeAt( 0 );
    }

    public Range getFootnoteContSeparatorSubrange()
    {
        return getSubrangeAt( 1 );
    }

    public Range getFootnoteContNoteSubrange()
    {
        return getSubrangeAt( 2 );
    }

    public Range getEndnoteSeparatorSubrange()
    {
        return getSubrangeAt( 3 );
    }

    public Range getEndnoteContSeparatorSubrange()
    {
        return getSubrangeAt( 4 );
    }

    public Range getEndnoteContNoteSubrange()
    {
        return getSubrangeAt( 5 );
    }

    /**
     * @deprecated 3.8 beta 4
     */
    @Deprecated
    public String getEvenHeader() {
        return getAt(6+0);
    }
    /**
     * @deprecated 3.8 beta 4
     */
    @Deprecated
    public String getOddHeader() {
        return getAt(6+1);
    }
    /**
     * @deprecated 3.8 beta 4
     */
    @Deprecated
    public String getFirstHeader() {
        return getAt(6+4);
    }


    public Range getEvenHeaderSubrange() {
        return getSubrangeAt(6+0);
    }
    public Range getOddHeaderSubrange() {
        return getSubrangeAt(6+1);
    }
    public Range getFirstHeaderSubrange() {
        return getSubrangeAt(6+4);
    }

    /**
     * Returns the correct, defined header for the given
     *  one based page
     * @param pageNumber The one based page number
     */
    public String getHeader(int pageNumber) {
        // First page header is optional, only return
        //  if it's set
        if(pageNumber == 1) {
            final String fh = getFirstHeader();
            if(fh != null && !fh.isEmpty()) {
                return fh;
            }
        }
        // Even page header is optional, only return
        //  if it's set
        if(pageNumber % 2 == 0) {
            final String eh = getEvenHeader();
            if(eh != null && !eh.isEmpty()) {
                return eh;
            }
        }
        // Odd is the default
        return getOddHeader();
    }

    /**
     * @deprecated 3.8 beta 4
     */
    @Deprecated
    public String getEvenFooter()
    {
        return getAt( 6 + 2 );
    }

    /**
     * @deprecated 3.8 beta 4
     */
    @Deprecated
    public String getOddFooter()
    {
        return getAt( 6 + 3 );
    }

    /**
     * @deprecated 3.8 beta 4
     */
    @Deprecated
    public String getFirstFooter()
    {
        return getAt( 6 + 5 );
    }

    public Range getEvenFooterSubrange()
    {
        return getSubrangeAt( 6 + 2 );
    }

    public Range getOddFooterSubrange()
    {
        return getSubrangeAt( 6 + 3 );
    }

    public Range getFirstFooterSubrange()
    {
        return getSubrangeAt( 6 + 5 );
    }

    /**
     * Returns the correct, defined footer for the given
     *  one based page
     * @param pageNumber The one based page number
     */
    public String getFooter(int pageNumber) {
        // First page footer is optional, only return
        //  if it's set
        if(pageNumber == 1) {
            final String ff = getFirstFooter();
            if(ff != null && !ff.isEmpty()) {
                return ff;
            }
        }
        // Even page footer is optional, only return
        //  if it's set
        if(pageNumber % 2 == 0) {
            final String ef = getEvenFooter();
            if(ef != null && !ef.isEmpty()) {
                return ef;
            }
        }
        // Odd is the default
        return getOddFooter();
    }


    /**
     * Get the string that's pointed to by the
     *  given plcfHdd index
     * @deprecated 3.8 beta 4
     */
    @Deprecated
    private String getAt(int plcfHddIndex) {
        if(plcfHdd == null) return null;

        GenericPropertyNode prop = plcfHdd.getProperty(plcfHddIndex);
        if(prop.getStart() == prop.getEnd()) {
            // Empty story
            return "";
        }
        if(prop.getEnd() < prop.getStart()) {
           // Broken properties?
           return "";
        }

        // Ensure we're getting a sensible length
        String rawText = headerStories.text();
        int start = Math.min(prop.getStart(), rawText.length());
        int end = Math.min(prop.getEnd(), rawText.length());

        // Grab the contents
        String text = rawText.substring(start, end);

        // Strip off fields and macros if requested
        if(stripFields) {
            return Range.stripFields(text);
        }
        // If you create a header/footer, then remove it again, word
        //  will leave \r\r. Turn these back into an empty string,
        //  which is more what you'd expect
        if(text.equals("\r\r")) {
            return "";
        }

        return text;
    }

    private Range getSubrangeAt( int plcfHddIndex )
    {
        if ( plcfHdd == null )
            return null;

        GenericPropertyNode prop = plcfHdd.getProperty( plcfHddIndex );
        if ( prop.getStart() == prop.getEnd() )
        {
            // Empty story
            return null;
        }
        if ( prop.getEnd() < prop.getStart() )
        {
            // Broken properties?
            return null;
        }

        final int headersLength = headerStories.getEndOffset()
                - headerStories.getStartOffset();
        int start = Math.min( prop.getStart(), headersLength );
        int end = Math.min( prop.getEnd(), headersLength );

        return new Range( headerStories.getStartOffset() + start,
                headerStories.getStartOffset() + end, headerStories );
    }

    public Range getRange() {
        return headerStories;
    }
    protected PlexOfCps getPlcfHdd() {
        return plcfHdd;
    }

    /**
     * Are fields currently being stripped from
     *  the text that this HeaderStories returns?
     *  Default is false, but can be changed
     */
    public boolean areFieldsStripped() {
        return stripFields;
    }
    /**
     * Should fields (eg macros) be stripped from
     *  the text that this class returns?
     * Default is not to strip.
     */
    public void setAreFieldsStripped(boolean stripFields) {
        this.stripFields = stripFields;
    }
}