From 36fcee8b0e267ec525e6a9e301ff3ce25a5de590 Mon Sep 17 00:00:00 2001 From: Said Ryan Ackley Date: Mon, 9 Jun 2003 01:55:10 +0000 Subject: [PATCH] Added stuff for writing git-svn-id: https://svn.apache.org/repos/asf/jakarta/poi/trunk@353125 13f79535-47bb-0310-9956-ffa450edef68 --- .../poi/hwpf/model/hdftypes/PlexOfCps.java | 46 ++++++++++++++-- .../apache/poi/hwpf/model/hdftypes/SEPX.java | 10 +++- .../model/hdftypes/SectionDescriptor.java | 24 +++++++++ .../poi/hwpf/model/hdftypes/SectionTable.java | 53 +++++++++++++++++-- 4 files changed, 124 insertions(+), 9 deletions(-) diff --git a/src/scratchpad/src/org/apache/poi/hwpf/model/hdftypes/PlexOfCps.java b/src/scratchpad/src/org/apache/poi/hwpf/model/hdftypes/PlexOfCps.java index e610d6eb8d..8a75cd3216 100644 --- a/src/scratchpad/src/org/apache/poi/hwpf/model/hdftypes/PlexOfCps.java +++ b/src/scratchpad/src/org/apache/poi/hwpf/model/hdftypes/PlexOfCps.java @@ -73,6 +73,13 @@ public class PlexOfCps private int _sizeOfStruct; private ArrayList _props; + + public PlexOfCps(int sizeOfStruct) + { + _props = new ArrayList(); + _sizeOfStruct = sizeOfStruct; + } + /** * Constructor * @@ -97,7 +104,40 @@ public class PlexOfCps return (PropertyNode)_props.get(index); } - protected PropertyNode getProperty(int index, byte[] buf, int offset) + public void addProperty(PropertyNode node) + { + _props.add(node); + } + + public byte[] toByteArray() + { + int size = _props.size(); + int cpBufSize = ((size + 1) * LittleEndian.INT_SIZE); + int structBufSize = + (_sizeOfStruct * size); + int bufSize = cpBufSize + structBufSize; + + byte[] buf = new byte[bufSize]; + + PropertyNode node = null; + for (int x = 0; x < size; x++) + { + node = (PropertyNode)_props.get(x); + + // put the starting offset of the property into the plcf. + LittleEndian.putInt(buf, (LittleEndian.INT_SIZE * x), node.getStart()); + + // put the struct into the plcf + System.arraycopy(node.getBuf(), 0, buf, cpBufSize + (x * _sizeOfStruct), + _sizeOfStruct); + } + // put the ending offset of the last property into the plcf. + LittleEndian.putInt(buf, LittleEndian.INT_SIZE * size, node.getEnd()); + + return buf; + + } + + private PropertyNode getProperty(int index, byte[] buf, int offset) { int start = LittleEndian.getInt(buf, offset + getIntOffset(index)); int end = LittleEndian.getInt(buf, offset + getIntOffset(index+1)); @@ -108,7 +148,7 @@ public class PlexOfCps return new PropertyNode(start, end, struct); } - protected int getIntOffset(int index) + private int getIntOffset(int index) { return index * 4; } @@ -132,7 +172,7 @@ public class PlexOfCps * @return The offset, in bytes, from the beginning if this PlexOfCps to * the data structure at index. */ - protected int getStructOffset(int index) + private int getStructOffset(int index) { return (4 * (_count + 1)) + (_sizeOfStruct * index); } diff --git a/src/scratchpad/src/org/apache/poi/hwpf/model/hdftypes/SEPX.java b/src/scratchpad/src/org/apache/poi/hwpf/model/hdftypes/SEPX.java index 7dcb92af6c..97951e3801 100644 --- a/src/scratchpad/src/org/apache/poi/hwpf/model/hdftypes/SEPX.java +++ b/src/scratchpad/src/org/apache/poi/hwpf/model/hdftypes/SEPX.java @@ -60,9 +60,12 @@ package org.apache.poi.hwpf.model.hdftypes; public class SEPX extends PropertyNode { - public SEPX(int start, int end, byte[] grpprl) + SectionDescriptor _sed; + + public SEPX(SectionDescriptor sed, int start, int end, byte[] grpprl) { super(start, end, grpprl); + _sed = sed; } public byte[] getGrpprl() @@ -70,4 +73,9 @@ public class SEPX extends PropertyNode return super.getBuf(); } + public SectionDescriptor getSectionDescriptor() + { + return _sed; + } + } diff --git a/src/scratchpad/src/org/apache/poi/hwpf/model/hdftypes/SectionDescriptor.java b/src/scratchpad/src/org/apache/poi/hwpf/model/hdftypes/SectionDescriptor.java index d3a6be9b5c..5c2d4a0ae9 100644 --- a/src/scratchpad/src/org/apache/poi/hwpf/model/hdftypes/SectionDescriptor.java +++ b/src/scratchpad/src/org/apache/poi/hwpf/model/hdftypes/SectionDescriptor.java @@ -66,6 +66,10 @@ public class SectionDescriptor private short fnMpr; private int fcMpr; + public SectionDescriptor() + { + } + public SectionDescriptor(byte[] buf, int offset) { fn = LittleEndian.getShort(buf, offset); @@ -82,4 +86,24 @@ public class SectionDescriptor return fc; } + public void setFc(int fc) + { + this.fc = fc; + } + + public byte[] toByteArray() + { + int offset = 0; + byte[] buf = new byte[12]; + + LittleEndian.putShort(buf, offset, fn); + offset += LittleEndian.SHORT_SIZE; + LittleEndian.putInt(buf, offset, fc); + offset += LittleEndian.INT_SIZE; + LittleEndian.putShort(buf, offset, fnMpr); + offset += LittleEndian.SHORT_SIZE; + LittleEndian.putInt(buf, offset, fcMpr); + + return buf; + } } diff --git a/src/scratchpad/src/org/apache/poi/hwpf/model/hdftypes/SectionTable.java b/src/scratchpad/src/org/apache/poi/hwpf/model/hdftypes/SectionTable.java index 94773da1f2..f2e8108a8b 100644 --- a/src/scratchpad/src/org/apache/poi/hwpf/model/hdftypes/SectionTable.java +++ b/src/scratchpad/src/org/apache/poi/hwpf/model/hdftypes/SectionTable.java @@ -57,12 +57,17 @@ package org.apache.poi.hwpf.model.hdftypes; import java.util.ArrayList; +import java.io.ByteArrayOutputStream; +import java.io.IOException; + +import org.apache.poi.util.LittleEndian; +import org.apache.poi.hwpf.model.io.*; public class SectionTable { private static final int SED_SIZE = 12; - private ArrayList sections; + private ArrayList _sections; public SectionTable(byte[] documentStream, byte[] tableStream, int offset, int size) @@ -81,16 +86,54 @@ public class SectionTable // check for the optimization if (fileOffset == 0xffffffff) { - sections.add(new SEPX(node.getStart(), node.getEnd(), new byte[0])); + _sections.add(new SEPX(sed, node.getStart(), node.getEnd(), new byte[0])); } else { - // The first byte at the offset is the size of the grpprl. - byte[] buf = new byte[documentStream[fileOffset++]]; + // The first short at the offset is the size of the grpprl. + int sepxSize = LittleEndian.getShort(documentStream, fileOffset); + byte[] buf = new byte[sepxSize]; + fileOffset += LittleEndian.SHORT_SIZE; System.arraycopy(documentStream, fileOffset, buf, 0, buf.length); - sections.add(new SEPX(node.getStart(), node.getEnd(), buf)); + _sections.add(new SEPX(sed, node.getStart(), node.getEnd(), buf)); } } } + public void writeTo(HWPFFileSystem sys) + throws IOException + { + HWPFOutputStream docStream = sys.getStream("WordDocument"); + HWPFOutputStream tableStream = sys.getStream("1Table"); + + int offset = docStream.getOffset(); + int len = _sections.size(); + PlexOfCps plex = new PlexOfCps(SED_SIZE); + + for (int x = 0; x < len; x++) + { + SEPX sepx = (SEPX)_sections.get(x); + byte[] grpprl = sepx.getGrpprl(); + + // write the sepx to the document stream. starts with a 2 byte size + // followed by the grpprl + byte[] shortBuf = new byte[2]; + LittleEndian.putShort(shortBuf, (short)grpprl.length); + + docStream.write(shortBuf); + docStream.write(grpprl); + + // set the fc in the section descriptor + SectionDescriptor sed = sepx.getSectionDescriptor(); + sed.setFc(offset); + + // add the section descriptor bytes to the PlexOfCps. + PropertyNode property = new PropertyNode(sepx.getStart(), sepx.getEnd(), sed.toByteArray()); + plex.addProperty(property); + + offset = docStream.getOffset(); + } + tableStream.write(plex.toByteArray()); + } + }