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 read the table from the table stream. |
50 |
* |
51 |
* @param tableStream the table stream. |
52 |
* @param offset the offset into the byte array. |
53 |
* @param size the size of the table in the byte array. |
54 |
*/ |
55 |
public SavedByTable(byte[] tableStream, int offset, int size) |
56 |
{ |
57 |
// Read the value that I don't know what it does. :-) |
58 |
unknownValue = LittleEndian.getShort(tableStream, offset); |
59 |
offset += 2; |
60 |
|
61 |
// The stored int is the number of strings, and there are two strings per entry. |
62 |
int numEntries = LittleEndian.getInt(tableStream, offset) / 2; |
63 |
offset += 4; |
64 |
|
65 |
entries = new SavedByEntry[numEntries]; |
66 |
for (int i = 0; i < numEntries; i++) |
67 |
{ |
68 |
int len = LittleEndian.getShort(tableStream, offset); |
69 |
offset += 2; |
70 |
String userName = StringUtil.getFromUnicodeLE(tableStream, offset, len); |
71 |
offset += len * 2; |
72 |
len = LittleEndian.getShort(tableStream, offset); |
73 |
offset += 2; |
74 |
String saveLocation = StringUtil.getFromUnicodeLE(tableStream, offset, len); |
75 |
offset += len * 2; |
76 |
|
77 |
entries[i] = new SavedByEntry(userName, saveLocation); |
78 |
} |
79 |
} |
80 |
|
81 |
/** |
82 |
* Gets the entries. The returned list cannot be modified. |
83 |
* |
84 |
* @return the list of entries. |
85 |
*/ |
86 |
public List getEntries() |
87 |
{ |
88 |
return Collections.unmodifiableList(Arrays.asList(entries)); |
89 |
} |
90 |
|
91 |
/** |
92 |
* Writes this table to the table stream. |
93 |
* |
94 |
* @param tableStream the table stream to write to. |
95 |
* @throws IOException if an error occurs while writing. |
96 |
*/ |
97 |
public void writeTo(HWPFOutputStream tableStream) |
98 |
throws IOException |
99 |
{ |
100 |
byte[] header = new byte[6]; |
101 |
LittleEndian.putShort(header, 0, unknownValue); |
102 |
LittleEndian.putInt(header, 2, entries.length * 2); |
103 |
tableStream.write(header); |
104 |
|
105 |
for (int i = 0; i < entries.length; i++) |
106 |
{ |
107 |
writeStringValue(tableStream, entries[i].getUserName()); |
108 |
writeStringValue(tableStream, entries[i].getSaveLocation()); |
109 |
} |
110 |
} |
111 |
|
112 |
private void writeStringValue(HWPFOutputStream tableStream, String value) |
113 |
throws IOException |
114 |
{ |
115 |
byte[] buf = new byte[value.length() * 2 + 2]; |
116 |
LittleEndian.putShort(buf, 0, (short) value.length()); |
117 |
StringUtil.putUnicodeLE(value, buf, 2); |
118 |
tableStream.write(buf); |
119 |
} |
120 |
} |
121 |
|
0 |
+ * |
122 |
+ * |