From dc06a38d0a936ac55a9cb87a6bef896b9cc68066 Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Mon, 7 Nov 2005 23:03:05 +0000 Subject: [PATCH] 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 --- .../org/apache/poi/hslf/model/TextRun.java | 24 +++++++++++++ ...myPositionSensitiveRecordWithChildren.java | 6 ---- .../hslf/record/DummyRecordWithChildren.java | 6 ---- .../poi/hslf/record/EscherTextboxWrapper.java | 6 ---- .../poi/hslf/record/FontCollection.java | 8 ++--- .../src/org/apache/poi/hslf/record/Notes.java | 6 ---- .../poi/hslf/record/RecordContainer.java | 35 +++++++++++++++++++ .../src/org/apache/poi/hslf/record/Slide.java | 6 ---- .../poi/hslf/record/SlideListWithText.java | 6 ---- 9 files changed, 62 insertions(+), 41 deletions(-) diff --git a/src/scratchpad/src/org/apache/poi/hslf/model/TextRun.java b/src/scratchpad/src/org/apache/poi/hslf/model/TextRun.java index 48acd57e5..6e08df7b3 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/model/TextRun.java +++ b/src/scratchpad/src/org/apache/poi/hslf/model/TextRun.java @@ -135,6 +135,9 @@ public class 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 LinkedList pStyles = _styleAtom.getParagraphStyles(); LinkedList cStyles = _styleAtom.getCharacterStyles(); @@ -191,6 +194,27 @@ public class TextRun _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 diff --git a/src/scratchpad/src/org/apache/poi/hslf/record/DummyPositionSensitiveRecordWithChildren.java b/src/scratchpad/src/org/apache/poi/hslf/record/DummyPositionSensitiveRecordWithChildren.java index 3f2674bf8..74495d03b 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/record/DummyPositionSensitiveRecordWithChildren.java +++ b/src/scratchpad/src/org/apache/poi/hslf/record/DummyPositionSensitiveRecordWithChildren.java @@ -37,7 +37,6 @@ import java.io.ByteArrayOutputStream; public class DummyPositionSensitiveRecordWithChildren extends PositionDependentRecordContainer { - private Record[] _children; private byte[] _header; private long _type; @@ -60,11 +59,6 @@ public class DummyPositionSensitiveRecordWithChildren extends PositionDependentR */ 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 * to disk diff --git a/src/scratchpad/src/org/apache/poi/hslf/record/DummyRecordWithChildren.java b/src/scratchpad/src/org/apache/poi/hslf/record/DummyRecordWithChildren.java index 5a30fae6f..eb7478fde 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/record/DummyRecordWithChildren.java +++ b/src/scratchpad/src/org/apache/poi/hslf/record/DummyRecordWithChildren.java @@ -33,7 +33,6 @@ import java.io.ByteArrayOutputStream; public class DummyRecordWithChildren extends RecordContainer { - private Record[] _children; private byte[] _header; private long _type; @@ -55,11 +54,6 @@ public class DummyRecordWithChildren extends RecordContainer */ 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 * to disk diff --git a/src/scratchpad/src/org/apache/poi/hslf/record/EscherTextboxWrapper.java b/src/scratchpad/src/org/apache/poi/hslf/record/EscherTextboxWrapper.java index 04d388d25..c07291f21 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/record/EscherTextboxWrapper.java +++ b/src/scratchpad/src/org/apache/poi/hslf/record/EscherTextboxWrapper.java @@ -37,7 +37,6 @@ import java.io.ByteArrayOutputStream; public class EscherTextboxWrapper extends RecordContainer { private EscherTextboxRecord _escherRecord; - private Record[] _children; private long _type; /** @@ -63,11 +62,6 @@ public class EscherTextboxWrapper extends RecordContainer */ 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. * Doesn't actually do the writing out, that's left to the Escher diff --git a/src/scratchpad/src/org/apache/poi/hslf/record/FontCollection.java b/src/scratchpad/src/org/apache/poi/hslf/record/FontCollection.java index 97866de1f..c6ee18e53 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/record/FontCollection.java +++ b/src/scratchpad/src/org/apache/poi/hslf/record/FontCollection.java @@ -30,7 +30,6 @@ import java.util.*; public class FontCollection extends RecordContainer { private List fonts; - private Record[] _children; private byte[] _header; 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() { return RecordTypes.FontCollection.typeID; } - public Record[] getChildRecords() { - return _children; - } - /** * Write the contents of the record back, so it can be written * to disk diff --git a/src/scratchpad/src/org/apache/poi/hslf/record/Notes.java b/src/scratchpad/src/org/apache/poi/hslf/record/Notes.java index 3e7c9e7e2..f7506d83b 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/record/Notes.java +++ b/src/scratchpad/src/org/apache/poi/hslf/record/Notes.java @@ -32,7 +32,6 @@ import java.io.ByteArrayOutputStream; public class Notes extends PositionDependentRecordContainer { - private Record[] _children; private byte[] _header; private static long _type = 1008l; @@ -80,11 +79,6 @@ public class Notes extends PositionDependentRecordContainer */ 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 * to disk diff --git a/src/scratchpad/src/org/apache/poi/hslf/record/RecordContainer.java b/src/scratchpad/src/org/apache/poi/hslf/record/RecordContainer.java index 275e87441..b3d9dee3f 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/record/RecordContainer.java +++ b/src/scratchpad/src/org/apache/poi/hslf/record/RecordContainer.java @@ -34,6 +34,13 @@ import java.io.ByteArrayOutputStream; public abstract class RecordContainer extends Record { + protected Record[] _children; + + /** + * Return any children + */ + public Record[] getChildRecords() { return _children; } + /** * We're not an atom */ @@ -49,6 +56,34 @@ public abstract class RecordContainer extends Record r[r.length-1] = newChild; 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. diff --git a/src/scratchpad/src/org/apache/poi/hslf/record/Slide.java b/src/scratchpad/src/org/apache/poi/hslf/record/Slide.java index 2f29b0e4d..9717a58c2 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/record/Slide.java +++ b/src/scratchpad/src/org/apache/poi/hslf/record/Slide.java @@ -32,7 +32,6 @@ import java.io.ByteArrayOutputStream; public class Slide extends PositionDependentRecordContainer { - private Record[] _children; private byte[] _header; private static long _type = 1006l; @@ -80,11 +79,6 @@ public class Slide extends PositionDependentRecordContainer */ 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 * to disk diff --git a/src/scratchpad/src/org/apache/poi/hslf/record/SlideListWithText.java b/src/scratchpad/src/org/apache/poi/hslf/record/SlideListWithText.java index c8ed47551..ca1f88ff2 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/record/SlideListWithText.java +++ b/src/scratchpad/src/org/apache/poi/hslf/record/SlideListWithText.java @@ -49,7 +49,6 @@ import java.util.Vector; // For now, pretend to be an atom public class SlideListWithText extends RecordContainer { - private Record[] _children; private byte[] _header; private static long _type = 4080; @@ -109,11 +108,6 @@ public class SlideListWithText extends RecordContainer */ 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 * to disk