Added stuff for writing
git-svn-id: https://svn.apache.org/repos/asf/jakarta/poi/trunk@353125 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
6ba649ee20
commit
be73326e1d
@ -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);
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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());
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user