Should have been part of c746085 (More fixes to allow EscherContainerRecord to exclusively maintain 'child records' field due to r745976 / r746018)

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@746086 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Josh Micich 2009-02-20 01:57:45 +00:00
parent 8dcd0c8c58
commit d707f50d5d

View File

@ -1,4 +1,3 @@
/* ==================================================================== /* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with contributor license agreements. See the NOTICE file distributed with
@ -24,6 +23,7 @@ import org.apache.poi.util.LittleEndian;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.NoSuchElementException;
import java.io.PrintWriter; import java.io.PrintWriter;
/** /**
@ -44,21 +44,18 @@ public final class EscherContainerRecord extends EscherRecord {
private final List<EscherRecord> _childRecords = new ArrayList<EscherRecord>(); private final List<EscherRecord> _childRecords = new ArrayList<EscherRecord>();
public int fillFields( byte[] data, int offset, EscherRecordFactory recordFactory ) public int fillFields(byte[] data, int pOffset, EscherRecordFactory recordFactory) {
{ int bytesRemaining = readHeader(data, pOffset);
int bytesRemaining = readHeader( data, offset );
int bytesWritten = 8; int bytesWritten = 8;
offset += 8; int offset = pOffset + 8;
while ( bytesRemaining > 0 && offset < data.length ) while (bytesRemaining > 0 && offset < data.length) {
{
EscherRecord child = recordFactory.createRecord(data, offset); EscherRecord child = recordFactory.createRecord(data, offset);
int childBytesWritten = child.fillFields( data, offset, recordFactory ); int childBytesWritten = child.fillFields(data, offset, recordFactory);
bytesWritten += childBytesWritten; bytesWritten += childBytesWritten;
offset += childBytesWritten; offset += childBytesWritten;
bytesRemaining -= childBytesWritten; bytesRemaining -= childBytesWritten;
addChildRecord(child); addChildRecord(child);
if (offset >= data.length && bytesRemaining > 0) if (offset >= data.length && bytesRemaining > 0) {
{
System.out.println("WARNING: " + bytesRemaining + " bytes remaining but no space left"); System.out.println("WARNING: " + bytesRemaining + " bytes remaining but no space left");
} }
} }
@ -113,6 +110,9 @@ public final class EscherContainerRecord extends EscherRecord {
} }
return false; return false;
} }
public EscherRecord getChild( int index ) {
return _childRecords.get(index);
}
/** /**
* @return a copy of the list of all the child records of the container. * @return a copy of the list of all the child records of the container.
@ -120,6 +120,32 @@ public final class EscherContainerRecord extends EscherRecord {
public List<EscherRecord> getChildRecords() { public List<EscherRecord> getChildRecords() {
return new ArrayList<EscherRecord>(_childRecords); return new ArrayList<EscherRecord>(_childRecords);
} }
public Iterator<EscherRecord> getChildIterator() {
return new ReadOnlyIterator(_childRecords);
}
private static final class ReadOnlyIterator implements Iterator<EscherRecord> {
private final List<EscherRecord> _list;
private int _index;
public ReadOnlyIterator(List<EscherRecord> list) {
_list = list;
_index = 0;
}
public boolean hasNext() {
return _index < _list.size();
}
public EscherRecord next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
return _list.get(_index++);
}
public void remove() {
throw new UnsupportedOperationException();
}
}
/** /**
* replaces the internal child list with the contents of the supplied <tt>childRecords</tt> * replaces the internal child list with the contents of the supplied <tt>childRecords</tt>
*/ */
@ -131,6 +157,11 @@ public final class EscherContainerRecord extends EscherRecord {
_childRecords.addAll(childRecords); _childRecords.addAll(childRecords);
} }
public boolean removeChildRecord(EscherRecord toBeRemoved) {
return _childRecords.remove(toBeRemoved);
}
/** /**
* Returns all of our children which are also * Returns all of our children which are also
@ -178,7 +209,17 @@ public final class EscherContainerRecord extends EscherRecord {
} }
public void addChildRecord(EscherRecord record) { public void addChildRecord(EscherRecord record) {
_childRecords.add( record ); _childRecords.add(record);
}
public void addChildBefore(EscherRecord record, int insertBeforeRecordId) {
for (int i = 0; i < _childRecords.size(); i++) {
EscherRecord rec = _childRecords.get(i);
if(rec.getRecordId() == insertBeforeRecordId){
_childRecords.add(i++, record);
// TODO - keep looping? Do we expect multiple matches?
}
}
} }
public String toString() public String toString()
@ -248,5 +289,4 @@ public final class EscherContainerRecord extends EscherRecord {
} }
} }
} }
} }