Bit of refactoring - move child records into parent RecordContainer

class, so we can then have more generic child record modification code


git-svn-id: https://svn.apache.org/repos/asf/jakarta/poi/trunk@353790 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Nick Burch 2005-11-07 23:03:05 +00:00
parent 8e1d29cd39
commit dc06a38d0a
9 changed files with 62 additions and 41 deletions

View File

@ -135,6 +135,9 @@ public class TextRun
throw new IllegalArgumentException("Supplied RichTextRun wasn't from this TextRun"); throw new IllegalArgumentException("Supplied RichTextRun wasn't from this TextRun");
} }
// Ensure a StyleTextPropAtom is present, adding if required
ensureStyleAtomPresent();
// Update the text length for its Paragraph and Character stylings // Update the text length for its Paragraph and Character stylings
LinkedList pStyles = _styleAtom.getParagraphStyles(); LinkedList pStyles = _styleAtom.getParagraphStyles();
LinkedList cStyles = _styleAtom.getCharacterStyles(); LinkedList cStyles = _styleAtom.getCharacterStyles();
@ -191,6 +194,27 @@ public class TextRun
_rtRuns[0] = new RichTextRun(this,0,s.length()); _rtRuns[0] = new RichTextRun(this,0,s.length());
} }
/**
* Ensure a StyleTextPropAtom is present for this run,
* by adding if required
*/
private synchronized void ensureStyleAtomPresent() {
if(_styleAtom != null) {
// All there
return;
}
// Create a new one
_styleAtom = new StyleTextPropAtom(0);
// Use the TextHeader atom to get at the parent
RecordContainer runAtomsParent = _headerAtom.getParentRecord();
// Add the new StyleTextPropAtom after the TextCharsAtom / TextBytesAtom
Record addAfter = _byteAtom;
if(_byteAtom == null) { addAfter = _charAtom; }
runAtomsParent.addChildAfter(_styleAtom, addAfter);
}
// Accesser methods follow // Accesser methods follow

View File

@ -37,7 +37,6 @@ import java.io.ByteArrayOutputStream;
public class DummyPositionSensitiveRecordWithChildren extends PositionDependentRecordContainer public class DummyPositionSensitiveRecordWithChildren extends PositionDependentRecordContainer
{ {
private Record[] _children;
private byte[] _header; private byte[] _header;
private long _type; private long _type;
@ -60,11 +59,6 @@ public class DummyPositionSensitiveRecordWithChildren extends PositionDependentR
*/ */
public long getRecordType() { return _type; } public long getRecordType() { return _type; }
/**
* Return any children
*/
public Record[] getChildRecords() { return _children; }
/** /**
* Write the contents of the record back, so it can be written * Write the contents of the record back, so it can be written
* to disk * to disk

View File

@ -33,7 +33,6 @@ import java.io.ByteArrayOutputStream;
public class DummyRecordWithChildren extends RecordContainer public class DummyRecordWithChildren extends RecordContainer
{ {
private Record[] _children;
private byte[] _header; private byte[] _header;
private long _type; private long _type;
@ -55,11 +54,6 @@ public class DummyRecordWithChildren extends RecordContainer
*/ */
public long getRecordType() { return _type; } public long getRecordType() { return _type; }
/**
* Return any children
*/
public Record[] getChildRecords() { return _children; }
/** /**
* Write the contents of the record back, so it can be written * Write the contents of the record back, so it can be written
* to disk * to disk

View File

@ -37,7 +37,6 @@ import java.io.ByteArrayOutputStream;
public class EscherTextboxWrapper extends RecordContainer public class EscherTextboxWrapper extends RecordContainer
{ {
private EscherTextboxRecord _escherRecord; private EscherTextboxRecord _escherRecord;
private Record[] _children;
private long _type; private long _type;
/** /**
@ -63,11 +62,6 @@ public class EscherTextboxWrapper extends RecordContainer
*/ */
public long getRecordType() { return _type; } public long getRecordType() { return _type; }
/**
* Return any children
*/
public Record[] getChildRecords() { return _children; }
/** /**
* Stores the data for the child records back into the Escher layer. * Stores the data for the child records back into the Escher layer.
* Doesn't actually do the writing out, that's left to the Escher * Doesn't actually do the writing out, that's left to the Escher

View File

@ -30,7 +30,6 @@ import java.util.*;
public class FontCollection extends RecordContainer { public class FontCollection extends RecordContainer {
private List fonts; private List fonts;
private Record[] _children;
private byte[] _header; private byte[] _header;
protected FontCollection(byte[] source, int start, int len) { protected FontCollection(byte[] source, int start, int len) {
@ -48,14 +47,13 @@ public class FontCollection extends RecordContainer {
} }
/**
* Return the type, which is 2005
*/
public long getRecordType() { public long getRecordType() {
return RecordTypes.FontCollection.typeID; return RecordTypes.FontCollection.typeID;
} }
public Record[] getChildRecords() {
return _children;
}
/** /**
* Write the contents of the record back, so it can be written * Write the contents of the record back, so it can be written
* to disk * to disk

View File

@ -32,7 +32,6 @@ import java.io.ByteArrayOutputStream;
public class Notes extends PositionDependentRecordContainer public class Notes extends PositionDependentRecordContainer
{ {
private Record[] _children;
private byte[] _header; private byte[] _header;
private static long _type = 1008l; private static long _type = 1008l;
@ -80,11 +79,6 @@ public class Notes extends PositionDependentRecordContainer
*/ */
public long getRecordType() { return _type; } public long getRecordType() { return _type; }
/**
* Return any children
*/
public Record[] getChildRecords() { return _children; }
/** /**
* Write the contents of the record back, so it can be written * Write the contents of the record back, so it can be written
* to disk * to disk

View File

@ -34,6 +34,13 @@ import java.io.ByteArrayOutputStream;
public abstract class RecordContainer extends Record public abstract class RecordContainer extends Record
{ {
protected Record[] _children;
/**
* Return any children
*/
public Record[] getChildRecords() { return _children; }
/** /**
* We're not an atom * We're not an atom
*/ */
@ -49,6 +56,34 @@ public abstract class RecordContainer extends Record
r[r.length-1] = newChild; r[r.length-1] = newChild;
return r; return r;
} }
/**
* Adds the given Child Record after the supplied record
* @param newChild
* @param after
*/
public synchronized void addChildAfter(Record newChild, Record after) {
boolean added = false;
Record[] newChildren = new Record[_children.length+1];
for(int i=0; i<_children.length; i++) {
int newPos = i;
if(added) { newPos++; }
newChildren[newPos] = _children[i];
if(_children[i].equals(after)) {
// Found one to add after
added = true;
newPos++;
newChildren[newPos] = newChild;
}
}
if(added) {
_children = newChildren;
} else {
throw new IllegalArgumentException("Asked to add a new child after another record, but that record wasn't one of our children!");
}
}
/** /**
* Write out our header, and our children. * Write out our header, and our children.

View File

@ -32,7 +32,6 @@ import java.io.ByteArrayOutputStream;
public class Slide extends PositionDependentRecordContainer public class Slide extends PositionDependentRecordContainer
{ {
private Record[] _children;
private byte[] _header; private byte[] _header;
private static long _type = 1006l; private static long _type = 1006l;
@ -80,11 +79,6 @@ public class Slide extends PositionDependentRecordContainer
*/ */
public long getRecordType() { return _type; } public long getRecordType() { return _type; }
/**
* Return any children
*/
public Record[] getChildRecords() { return _children; }
/** /**
* Write the contents of the record back, so it can be written * Write the contents of the record back, so it can be written
* to disk * to disk

View File

@ -49,7 +49,6 @@ import java.util.Vector;
// For now, pretend to be an atom // For now, pretend to be an atom
public class SlideListWithText extends RecordContainer public class SlideListWithText extends RecordContainer
{ {
private Record[] _children;
private byte[] _header; private byte[] _header;
private static long _type = 4080; private static long _type = 4080;
@ -109,11 +108,6 @@ public class SlideListWithText extends RecordContainer
*/ */
public long getRecordType() { return _type; } public long getRecordType() { return _type; }
/**
* We're pretending to be an atom, so return null
*/
public Record[] getChildRecords() { return _children; }
/** /**
* Write the contents of the record back, so it can be written * Write the contents of the record back, so it can be written
* to disk * to disk