package net.sf.jpluck.plucker;
import net.sf.jpluck.palm.PdbOutputStream;
import net.sf.jpluck.palm.Record;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
public abstract class DataRecord implements Record {
public static final int PHTML = 0;
public static final int TBMP = 2;
public static final int MAILTO = 4;
public static final int LINK_INDEX = 5;
public static final int LINKS = 6;
public static final int BOOKMARKS = 8;
public static final int CATEGORY = 9;
public static final int METADATA = 10;
public static final int TABLE = 13;
public static final int COMPOSITE_IMAGE = 15;
protected Document document;
private int recordId;
final void setDocument(Document document) {
this.document = document;
}
final void setRecordId(int recordId) {
this.recordId = recordId;
}
public final int getRecordId() {
return recordId;
}
public final void write(PdbOutputStream out) throws IOException {
ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
PdbOutputStream pdb = new PdbOutputStream(dataStream);
Paragraph[] paragraphs = writeData(pdb);
byte[] data = dataStream.toByteArray();
int uncompressedSize = data.length;
int type = getType();
// TODO: Add support for TABLE_COMPRESSED
if (type == PHTML || type == TBMP || type == LINKS) {
if (document.isDocCompressed()) {
byte[] compressed = net.sf.jpluck.util.CompressionUtil.compressDOC(data);
if (compressed.length < data.length) {
type += 1; // Adding 1 to the type will make PHTML->PHTML_COMPRESSED etc. (see constants)
data = compressed;
}
}
if (document.isZLIBCompressed()) {
byte[] compressed = net.sf.jpluck.util.CompressionUtil.compressZLIB(data);
if (compressed.length < data.length) {
type += 1; // Adding 1 to the type will make PHTML->PHTML_COMPRESSED etc. (see constants)
data = compressed;
}
}
}
out.writeShort(recordId);
out.writeShort((paragraphs != null ? paragraphs.length : 0));
out.writeShort(uncompressedSize);
out.writeByte(type);
if (this instanceof TextRecord && ((TextRecord)this).nextSegmentFollows) {
out.writeByte(1);
} else {
out.writeByte(0);
}
if (paragraphs != null) {
for (int i = 0; i < paragraphs.length; i++) {
Paragraph paragraph = paragraphs[i];
out.writeShort(paragraph.getWrittenSize());
out.writeShort(paragraph.getSpacing());
}
}
out.write(data);
}
protected abstract Paragraph[] writeData(PdbOutputStream out) throws IOException;
protected abstract int getType();
}