001: /* ====================================================================
002: Licensed to the Apache Software Foundation (ASF) under one or more
003: contributor license agreements. See the NOTICE file distributed with
004: this work for additional information regarding copyright ownership.
005: The ASF licenses this file to You under the Apache License, Version 2.0
006: (the "License"); you may not use this file except in compliance with
007: the License. You may obtain a copy of the License at
008:
009: http://www.apache.org/licenses/LICENSE-2.0
010:
011: Unless required by applicable law or agreed to in writing, software
012: distributed under the License is distributed on an "AS IS" BASIS,
013: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: See the License for the specific language governing permissions and
015: limitations under the License.
016: ==================================================================== */
017:
018: package org.apache.poi.hwpf.model;
019:
020: import java.io.IOException;
021: import java.util.Arrays;
022: import java.util.Collections;
023: import java.util.List;
024:
025: import org.apache.poi.util.LittleEndian;
026: import org.apache.poi.util.StringUtil;
027:
028: import org.apache.poi.hwpf.model.io.HWPFOutputStream;
029:
030: /**
031: * String table containing the history of the last few revisions ("saves") of the document.
032: * Read-only for the time being.
033: *
034: * @author Daniel Noll
035: */
036: public class SavedByTable {
037: /**
038: * A value that I don't know what it does, but is maintained for accuracy.
039: */
040: private short unknownValue = -1;
041:
042: /**
043: * Array of entries.
044: */
045: private SavedByEntry[] entries;
046:
047: /**
048: * Constructor to read the table from the table stream.
049: *
050: * @param tableStream the table stream.
051: * @param offset the offset into the byte array.
052: * @param size the size of the table in the byte array.
053: */
054: public SavedByTable(byte[] tableStream, int offset, int size) {
055: // Read the value that I don't know what it does. :-)
056: unknownValue = LittleEndian.getShort(tableStream, offset);
057: offset += 2;
058:
059: // The stored int is the number of strings, and there are two strings per entry.
060: int numEntries = LittleEndian.getInt(tableStream, offset) / 2;
061: offset += 4;
062:
063: entries = new SavedByEntry[numEntries];
064: for (int i = 0; i < numEntries; i++) {
065: int len = LittleEndian.getShort(tableStream, offset);
066: offset += 2;
067: String userName = StringUtil.getFromUnicodeLE(tableStream,
068: offset, len);
069: offset += len * 2;
070: len = LittleEndian.getShort(tableStream, offset);
071: offset += 2;
072: String saveLocation = StringUtil.getFromUnicodeLE(
073: tableStream, offset, len);
074: offset += len * 2;
075:
076: entries[i] = new SavedByEntry(userName, saveLocation);
077: }
078: }
079:
080: /**
081: * Gets the entries. The returned list cannot be modified.
082: *
083: * @return the list of entries.
084: */
085: public List getEntries() {
086: return Collections.unmodifiableList(Arrays.asList(entries));
087: }
088:
089: /**
090: * Writes this table to the table stream.
091: *
092: * @param tableStream the table stream to write to.
093: * @throws IOException if an error occurs while writing.
094: */
095: public void writeTo(HWPFOutputStream tableStream)
096: throws IOException {
097: byte[] header = new byte[6];
098: LittleEndian.putShort(header, 0, unknownValue);
099: LittleEndian.putInt(header, 2, entries.length * 2);
100: tableStream.write(header);
101:
102: for (int i = 0; i < entries.length; i++) {
103: writeStringValue(tableStream, entries[i].getUserName());
104: writeStringValue(tableStream, entries[i].getSaveLocation());
105: }
106: }
107:
108: private void writeStringValue(HWPFOutputStream tableStream,
109: String value) throws IOException {
110: byte[] buf = new byte[value.length() * 2 + 2];
111: LittleEndian.putShort(buf, 0, (short) value.length());
112: StringUtil.putUnicodeLE(value, buf, 2);
113: tableStream.write(buf);
114: }
115: }
|