View | Details | Raw Unified | Return to bug 38647
Collapse All | Expand All

(-)src/scratchpad/src/org/apache/poi/hwpf/model/SavedByTable.java (+129 lines)
Line 0 Link Here
1
/* ====================================================================
2
   Copyright 2002-2004   Apache Software Foundation
3
4
   Licensed under the Apache License, Version 2.0 (the "License");
5
   you may not use this file except in compliance with the License.
6
   You may obtain a copy of the License at
7
8
       http://www.apache.org/licenses/LICENSE-2.0
9
10
   Unless required by applicable law or agreed to in writing, software
11
   distributed under the License is distributed on an "AS IS" BASIS,
12
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
   See the License for the specific language governing permissions and
14
   limitations under the License.
15
==================================================================== */
16
17
18
package org.apache.poi.hwpf.model;
19
20
import java.io.IOException;
21
import java.util.Arrays;
22
import java.util.Collections;
23
import java.util.List;
24
25
import org.apache.poi.util.LittleEndian;
26
import org.apache.poi.util.StringUtil;
27
28
import org.apache.poi.hwpf.model.io.HWPFOutputStream;
29
30
/**
31
 * String table containing the history of the last few revisions ("saves") of the document.
32
 * Read-only for the time being.
33
 * 
34
 * @author Daniel Noll
35
 */
36
public class SavedByTable
37
{
38
  /**
39
   * A value that I don't know what it does, but is maintained for accuracy.
40
   */
41
  private short unknownValue = -1;
42
43
  /**
44
   * Array of entries.
45
   */
46
  private SavedByEntry[] entries;
47
48
  /**
49
   * Constructor to create an empty table.
50
   */
51
  public SavedByTable()
52
  {
53
    entries = SavedByEntry[0];
54
  }
55
56
  /**
57
   * Constructor to read the table from the table stream.
58
   *
59
   * @param tableStream the table stream.
60
   * @param offset the offset into the byte array.
61
   * @param size the size of the table in the byte array.
62
   */
63
  public SavedByTable(byte[] tableStream, int offset, int size)
64
  {
65
    // Read the value that I don't know what it does. :-)
66
    unknownValue = LittleEndian.getShort(tableStream, offset);
67
    offset += 2;
68
69
    // The stored int is the number of strings, and there are two strings per entry.
70
    int numEntries = LittleEndian.getInt(tableStream, offset) / 2;
71
    offset += 4;
72
73
    entries = new SavedByEntry[numEntries];
74
    for (int i = 0; i < numEntries; i++)
75
    {
76
      int len = LittleEndian.getShort(tableStream, offset);
77
      offset += 2;
78
      String userName = StringUtil.getFromUnicodeLE(tableStream, offset, len);
79
      offset += len * 2;
80
      len = LittleEndian.getShort(tableStream, offset);
81
      offset += 2;
82
      String saveLocation = StringUtil.getFromUnicodeLE(tableStream, offset, len);
83
      offset += len * 2;
84
85
      entries[i] = new SavedByEntry(userName, saveLocation);
86
    }
87
  }
88
89
  /**
90
   * Gets the entries.  The returned list cannot be modified.
91
   *
92
   * @return the list of entries.
93
   */
94
  public List getEntries()
95
  {
96
    return Collections.unmodifiableList(Arrays.asList(entries));
97
  }
98
99
  /**
100
   * Writes this table to the table stream.
101
   *
102
   * @param tableStream the table stream to write to.
103
   * @throws IOException if an error occurs while writing.
104
   */
105
  public void writeTo(HWPFOutputStream tableStream)
106
    throws IOException
107
  {
108
    byte[] header = new byte[6];
109
    LittleEndian.putShort(header, 0, unknownValue);
110
    LittleEndian.putInt(header, 2, entries.length * 2);
111
    tableStream.write(header);
112
113
    for (int i = 0; i < entries.length; i++)
114
    {
115
      writeStringValue(tableStream, entries[i].getUserName());
116
      writeStringValue(tableStream, entries[i].getSaveLocation());
117
    }
118
  }
119
120
  private void writeStringValue(HWPFOutputStream tableStream, String value)
121
    throws IOException
122
  {
123
    byte[] buf = new byte[value.length() * 2 + 2];
124
    LittleEndian.putShort(buf, 0, (short) value.length());
125
    StringUtil.putUnicodeLE(value, buf, 2);
126
    tableStream.write(buf);
127
  }
128
}
129
0
  + *
130
  + *
(-)src/scratchpad/src/org/apache/poi/hwpf/model/SavedByEntry.java (+46 lines)
Line 0 Link Here
1
/* ====================================================================
2
   Copyright 2002-2004   Apache Software Foundation
3
4
   Licensed under the Apache License, Version 2.0 (the "License");
5
   you may not use this file except in compliance with the License.
6
   You may obtain a copy of the License at
7
8
       http://www.apache.org/licenses/LICENSE-2.0
9
10
   Unless required by applicable law or agreed to in writing, software
11
   distributed under the License is distributed on an "AS IS" BASIS,
12
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
   See the License for the specific language governing permissions and
14
   limitations under the License.
15
==================================================================== */
16
17
18
package org.apache.poi.hwpf.model;
19
20
21
/**
22
 * A single entry in the {@link SavedByTable}.
23
 * 
24
 * @author Daniel Noll
25
 */
26
public class SavedByEntry
27
{
28
  private String userName;
29
  private String saveLocation;
30
31
  public SavedByEntry(String userName, String saveLocation)
32
  {
33
    this.userName = userName;
34
    this.saveLocation = saveLocation;
35
  }
36
37
  public String getUserName()
38
  {
39
    return userName;
40
  }
41
42
  public String getSaveLocation()
43
  {
44
    return saveLocation;
45
  }
46
}
(-)src/scratchpad/src/org/apache/poi/hwpf/model/FileInformationBlock.java (+21 lines)
Lines 61-66 Link Here
61
      fieldSet.add(new Integer(FIBFieldHandler.PLFLFO));
61
      fieldSet.add(new Integer(FIBFieldHandler.PLFLFO));
62
      fieldSet.add(new Integer(FIBFieldHandler.PLCFFLDMOM));
62
      fieldSet.add(new Integer(FIBFieldHandler.PLCFFLDMOM));
63
      fieldSet.add(new Integer(FIBFieldHandler.STTBFFFN));
63
      fieldSet.add(new Integer(FIBFieldHandler.STTBFFFN));
64
      fieldSet.add(new Integer(FIBFieldHandler.STTBSAVEDBY));
64
      fieldSet.add(new Integer(FIBFieldHandler.MODIFIED));
65
      fieldSet.add(new Integer(FIBFieldHandler.MODIFIED));
65
66
66
67
Lines 251-256 Link Here
251
      _fieldHandler.setFieldSize(FIBFieldHandler.STTBFFFN, lcbSttbFffn);
252
      _fieldHandler.setFieldSize(FIBFieldHandler.STTBFFFN, lcbSttbFffn);
252
    }
253
    }
253
254
255
    public int getFcSttbSavedBy()
256
    {
257
        return _fieldHandler.getFieldOffset(FIBFieldHandler.STTBSAVEDBY);
258
    }
259
260
    public int getLcbSttbSavedBy()
261
    {
262
        return _fieldHandler.getFieldSize(FIBFieldHandler.STTBSAVEDBY);
263
    }
264
265
    public void setFcSttbSavedBy(int fcSttbSavedBy)
266
    {
267
      _fieldHandler.setFieldOffset(FIBFieldHandler.STTBSAVEDBY, fcSttbSavedBy);
268
    }
269
270
    public void setLcbSttbSavedBy(int fcSttbSavedBy)
271
    {
272
      _fieldHandler.setFieldSize(FIBFieldHandler.STTBSAVEDBY, fcSttbSavedBy);
273
    }
274
254
    public int getModifiedLow()
275
    public int getModifiedLow()
255
    {
276
    {
256
      return _fieldHandler.getFieldOffset(FIBFieldHandler.PLFLFO);
277
      return _fieldHandler.getFieldOffset(FIBFieldHandler.PLFLFO);
(-)src/scratchpad/src/org/apache/poi/hwpf/HWPFDocument.java (+35 lines)
Lines 84-89 Link Here
84
  /** Hold list tables */
84
  /** Hold list tables */
85
  protected ListTables _lt;
85
  protected ListTables _lt;
86
86
87
  /** Holds the save history for this document. */
88
  protected SavedByTable _sbt;
89
87
  protected HWPFDocument()
90
  protected HWPFDocument()
88
  {
91
  {
89
92
Lines 181-186 Link Here
181
      _lt = new ListTables(_tableStream, _fib.getFcPlcfLst(), _fib.getFcPlfLfo());
184
      _lt = new ListTables(_tableStream, _fib.getFcPlcfLst(), _fib.getFcPlfLfo());
182
    }
185
    }
183
186
187
    int sbtOffset = _fib.getFcSttbSavedBy();
188
    int sbtLength = _fib.getLcbSttbSavedBy();
189
    if (sbtOffset != 0 && sbtLength != 0)
190
    {
191
      _sbt = new SavedByTable(_tableStream, sbtOffset, sbtLength);
192
    }
193
    else
194
    {
195
      _sbt = new SavedByTable();
196
    }
197
184
    PlexOfCps plc = new PlexOfCps(_tableStream, _fib.getFcPlcffldMom(), _fib.getLcbPlcffldMom(), 2);
198
    PlexOfCps plc = new PlexOfCps(_tableStream, _fib.getFcPlcffldMom(), _fib.getLcbPlcffldMom(), 2);
185
    for (int x = 0; x < plc.length(); x++)
199
    for (int x = 0; x < plc.length(); x++)
186
    {
200
    {
Lines 236-242 Link Here
236
  {
250
  {
237
    return _lt;
251
    return _lt;
238
  }
252
  }
253
239
  /**
254
  /**
255
   * Gets a reference to the saved -by table, which holds the save history for the document.
256
   *
257
   * @return the saved-by table.
258
   */
259
  public SavedByTable getSavedByTable()
260
  {
261
    return _sbt;
262
  }
263
264
  /**
240
   * Writes out the word file that is represented by an instance of this class.
265
   * Writes out the word file that is represented by an instance of this class.
241
   *
266
   *
242
   * @param out The OutputStream to write to.
267
   * @param out The OutputStream to write to.
Lines 316-321 Link Here
316
      tableOffset = tableStream.getOffset();
341
      tableOffset = tableStream.getOffset();
317
    }
342
    }
318
343
344
    // write out the saved-by table.
345
    if (_sbt != null)
346
    {
347
      _fib.setFcSttbSavedBy(tableOffset);
348
      _sbt.writeTo(tableStream);
349
      _fib.setLcbSttbSavedBy(tableStream.getOffset() - tableOffset);
350
351
      tableOffset = tableStream.getOffset();
352
    }
353
319
    // write out the FontTable.
354
    // write out the FontTable.
320
    _fib.setFcSttbfffn(tableOffset);
355
    _fib.setFcSttbfffn(tableOffset);
321
    _ft.writeTo(docSys);
356
    _ft.writeTo(docSys);

Return to bug 38647