package org.apache.poi.ddf;
import java.io.PrintWriter;
-import java.util.*;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.List;
import org.apache.poi.util.HexDump;
import org.apache.poi.util.LittleEndian;
import org.apache.poi.util.POILogFactory;
import org.apache.poi.util.POILogger;
+import org.apache.poi.util.Removal;
/**
* Escher container records store other escher records as children.
* the standard header used by all escher records. This one record is
* used to represent many different types of records.
*/
-public final class EscherContainerRecord extends EscherRecord {
+public final class EscherContainerRecord extends EscherRecord implements Iterable<EscherRecord> {
public static final short DGG_CONTAINER = (short)0xF000;
public static final short BSTORE_CONTAINER = (short)0xF001;
public static final short DG_CONTAINER = (short)0xF002;
LittleEndian.putShort(data, offset, getOptions());
LittleEndian.putShort(data, offset+2, getRecordId());
int remainingBytes = 0;
- Iterator<EscherRecord> iterator = _childRecords.iterator();
- while (iterator.hasNext()) {
- EscherRecord r = iterator.next();
+ for (EscherRecord r : this) {
remainingBytes += r.getRecordSize();
}
remainingBytes += _remainingLength;
LittleEndian.putInt(data, offset+4, remainingBytes);
int pos = offset+8;
- iterator = _childRecords.iterator();
- while (iterator.hasNext()) {
- EscherRecord r = iterator.next();
+ for (EscherRecord r : this) {
pos += r.serialize(pos, data, listener );
}
@Override
public int getRecordSize() {
int childRecordsSize = 0;
- Iterator<EscherRecord> iterator = _childRecords.iterator();
- while (iterator.hasNext()) {
- EscherRecord r = iterator.next();
+ for (EscherRecord r : this) {
childRecordsSize += r.getRecordSize();
}
return 8 + childRecordsSize;
* @return true, if any child has the given recordId
*/
public boolean hasChildOfType(short recordId) {
- Iterator<EscherRecord> iterator = _childRecords.iterator();
- while (iterator.hasNext()) {
- EscherRecord r = iterator.next();
+ for (EscherRecord r : this) {
if(r.getRecordId() == recordId) {
return true;
}
/**
* @return an iterator over the child records
+ * @deprecated POI 3.16 beta 1. use iterator() or loop over the container record instead,
+ * e.g. "for (EscherRecord r : container) ..."
*/
+ @Removal(version="3.18")
+ @Deprecated
public Iterator<EscherRecord> getChildIterator() {
+ return iterator();
+ }
+
+ /**
+ * @return an iterator over the child records
+ */
+ @Override
+ public Iterator<EscherRecord> iterator() {
return Collections.unmodifiableList(_childRecords).iterator();
}
*/
public List<EscherContainerRecord> getChildContainers() {
List<EscherContainerRecord> containers = new ArrayList<EscherContainerRecord>();
- Iterator<EscherRecord> iterator = _childRecords.iterator();
- while (iterator.hasNext()) {
- EscherRecord r = iterator.next();
+ for (EscherRecord r : this) {
if(r instanceof EscherContainerRecord) {
containers.add((EscherContainerRecord) r);
}
@Override
public void display(PrintWriter w, int indent) {
super.display(w, indent);
- for (Iterator<EscherRecord> iterator = _childRecords.iterator(); iterator.hasNext();)
- {
- EscherRecord escherRecord = iterator.next();
+ for (EscherRecord escherRecord : this) {
escherRecord.display(w, indent + 1);
}
}
*/
public void addChildBefore(EscherRecord record, int insertBeforeRecordId) {
int idx = 0;
- for (EscherRecord rec : _childRecords) {
- if(rec.getRecordId() == (short)insertBeforeRecordId) break;
+ for (EscherRecord rec : this) {
+ if(rec.getRecordId() == (short)insertBeforeRecordId) {
+ break;
+ }
// TODO - keep looping? Do we expect multiple matches?
idx++;
}
children.append( " children: " + nl );
int count = 0;
- for ( Iterator<EscherRecord> iterator = _childRecords.iterator(); iterator
- .hasNext(); )
- {
- EscherRecord record = iterator.next();
+ for ( EscherRecord record : this ) {
children.append( " Child " + count + ":" + nl );
String childResult = String.valueOf( record );
childResult = childResult.replaceAll( "\n", "\n " );
public String toXml(String tab) {
StringBuilder builder = new StringBuilder();
builder.append(tab).append(formatXmlRecordHeader(getRecordName(), HexDump.toHex(getRecordId()), HexDump.toHex(getVersion()), HexDump.toHex(getInstance())));
- for ( Iterator<EscherRecord> iterator = _childRecords.iterator(); iterator
- .hasNext(); )
- {
- EscherRecord record = iterator.next();
+ for ( EscherRecord record : this ) {
builder.append(record.toXml(tab+"\t"));
}
builder.append(tab).append("</").append(getRecordName()).append(">\n");
return builder.toString();
}
- public <T extends EscherRecord> T getChildById( short recordId )
- {
- for ( EscherRecord childRecord : _childRecords )
- {
- if ( childRecord.getRecordId() == recordId )
- {
+ public <T extends EscherRecord> T getChildById( short recordId ) {
+ for ( EscherRecord childRecord : this ) {
+ if ( childRecord.getRecordId() == recordId ) {
@SuppressWarnings( "unchecked" )
final T result = (T) childRecord;
return result;
* @param out - list to store found records
*/
public void getRecordsById(short recordId, List<EscherRecord> out){
- Iterator<EscherRecord> iterator = _childRecords.iterator();
- while (iterator.hasNext()) {
- EscherRecord r = iterator.next();
+ for (EscherRecord r : this) {
if(r instanceof EscherContainerRecord) {
EscherContainerRecord c = (EscherContainerRecord)r;
c.getRecordsById(recordId, out );
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.NPOIFSFileSystem;
import org.apache.poi.util.HexDump;
+import org.apache.poi.util.IOUtils;
import org.apache.poi.util.LittleEndian;
import org.apache.poi.util.POILogFactory;
import org.apache.poi.util.POILogger;
}
temp.add(record);
- if (dumpInterpretedRecords) {
- for (String header : recListener.getRecentHeaders()) {
- ps.println(header);
- }
- ps.print(record.toString());
+ for (String header : recListener.getRecentHeaders()) {
+ ps.println(header);
}
+ ps.print(record.toString());
} else {
recStream.readRemainder();
}
pw = new PrintWriter(new OutputStreamWriter(System.out, Charset.defaultCharset()));
}
+ NPOIFSFileSystem fs = null;
+ InputStream is = null;
try {
- NPOIFSFileSystem fs = new NPOIFSFileSystem(cmdArgs.getFile(), true);
- try {
- InputStream is = getPOIFSInputStream(fs);
+ fs = new NPOIFSFileSystem(cmdArgs.getFile(), true);
+ is = getPOIFSInputStream(fs);
- try {
- if (cmdArgs.shouldOutputRawHexOnly()) {
- int size = is.available();
- byte[] data = new byte[size];
-
- is.read(data);
- HexDump.dump(data, 0, System.out, 0);
- } else {
- boolean dumpInterpretedRecords = cmdArgs.shouldDumpRecordInterpretations();
- boolean dumpHex = cmdArgs.shouldDumpBiffHex();
- boolean zeroAlignHexDump = dumpInterpretedRecords; // TODO - fix non-zeroAlign
- runBiffViewer(pw, is, dumpInterpretedRecords, dumpHex, zeroAlignHexDump,
- cmdArgs.suppressHeader());
- }
- } finally {
- is.close();
- }
- } finally {
- fs.close();
+ if (cmdArgs.shouldOutputRawHexOnly()) {
+ byte[] data = IOUtils.toByteArray(is);
+ HexDump.dump(data, 0, System.out, 0);
+ } else {
+ boolean dumpInterpretedRecords = cmdArgs.shouldDumpRecordInterpretations();
+ boolean dumpHex = cmdArgs.shouldDumpBiffHex();
+ boolean zeroAlignHexDump = dumpInterpretedRecords; // TODO - fix non-zeroAlign
+ runBiffViewer(pw, is, dumpInterpretedRecords, dumpHex, zeroAlignHexDump,
+ cmdArgs.suppressHeader());
}
} finally {
- pw.close();
+ IOUtils.closeQuietly(is);
+ IOUtils.closeQuietly(fs);
+ IOUtils.closeQuietly(pw);
}
}
return;
} catch (OldExcelFormatException e) {
// will be handled by workaround below
- if (poifs != null) {
- poifs.close();
- }
} catch (NotOLE2FileException e) {
// will be handled by workaround below
- if (poifs != null) {
- poifs.close();
- }
} catch (IOException e) {
// ensure streams are closed correctly
- if (poifs != null) {
- poifs.close();
- }
-
throw e;
} catch (RuntimeException e) {
// ensure streams are closed correctly
- if (poifs != null) {
- poifs.close();
- }
-
throw e;
+ } finally {
+ if (toClose == null) {
+ IOUtils.closeQuietly(poifs);
+ }
}
@SuppressWarnings("resource")
import org.apache.poi.util.LocaleUtil;
import org.apache.poi.util.POILogFactory;
import org.apache.poi.util.POILogger;
+import org.apache.poi.util.RecordFormatException;
/**
* Low level model implementation of a Workbook. Provides creational methods
EscherDggRecord dgg = null;
EscherContainerRecord bStore = null;
- for(Iterator<EscherRecord> it = cr.getChildIterator(); it.hasNext();) {
- EscherRecord er = it.next();
+ for(EscherRecord er : cr) {
if(er instanceof EscherDggRecord) {
dgg = (EscherDggRecord)er;
} else if (er.getRecordId() == EscherContainerRecord.BSTORE_CONTAINER) {
dgg.setDrawingsSaved(dgg.getDrawingsSaved() + 1);
EscherDgRecord dg = null;
- for(Iterator<EscherRecord> it = escherContainer.getChildIterator(); it.hasNext();) {
- EscherRecord er = it.next();
+ for(EscherRecord er : escherContainer) {
if(er instanceof EscherDgRecord) {
dg = (EscherDgRecord)er;
//update id of the drawing in the cloned sheet
for(EscherRecord shapeChildRecord : shapeContainer.getChildRecords()) {
int recordId = shapeChildRecord.getRecordId();
if (recordId == EscherSpRecord.RECORD_ID){
+ if (dg == null) {
+ throw new RecordFormatException("EscherDgRecord wasn't set/processed before.");
+ }
EscherSpRecord sp = (EscherSpRecord)shapeChildRecord;
int shapeId = drawingManager.allocateShapeId((short)dgId, dg);
//allocateShapeId increments the number of shapes. roll back to the previous value
}
}
- if (extendedDataSize > 0) {
+ if (extendedDataSize > 0 && field_5_ext_rst != null) {
field_5_ext_rst.serialize(out);
}
}
\r
package org.apache.poi.hssf.usermodel;\r
\r
-import org.apache.poi.ddf.*;\r
-import org.apache.poi.hssf.record.*;\r
-import org.apache.poi.poifs.filesystem.DirectoryNode;\r
-\r
-import java.util.Iterator;\r
import java.util.List;\r
import java.util.Map;\r
\r
+import org.apache.poi.ddf.EscherClientDataRecord;\r
+import org.apache.poi.ddf.EscherContainerRecord;\r
+import org.apache.poi.ddf.EscherOptRecord;\r
+import org.apache.poi.ddf.EscherProperties;\r
+import org.apache.poi.ddf.EscherProperty;\r
+import org.apache.poi.ddf.EscherRecord;\r
+import org.apache.poi.ddf.EscherTextboxRecord;\r
+import org.apache.poi.hssf.record.CommonObjectDataSubRecord;\r
+import org.apache.poi.hssf.record.EmbeddedObjectRefSubRecord;\r
+import org.apache.poi.hssf.record.EscherAggregate;\r
+import org.apache.poi.hssf.record.ObjRecord;\r
+import org.apache.poi.hssf.record.Record;\r
+import org.apache.poi.hssf.record.SubRecord;\r
+import org.apache.poi.hssf.record.TextObjectRecord;\r
+import org.apache.poi.poifs.filesystem.DirectoryNode;\r
+import org.apache.poi.util.RecordFormatException;\r
+\r
/**\r
* Factory class for producing Excel Shapes from Escher records\r
*/\r
ObjRecord objRecord = null;\r
TextObjectRecord txtRecord = null;\r
\r
- for (EscherRecord record : container.getChildRecords()) {\r
+ for (EscherRecord record : container) {\r
switch (record.getRecordId()) {\r
case EscherClientDataRecord.RECORD_ID:\r
objRecord = (ObjRecord) shapeToObj.get(record);\r
break;\r
}\r
}\r
+ if (objRecord == null) {\r
+ throw new RecordFormatException("EscherClientDataRecord can't be found.");\r
+ }\r
if (isEmbeddedObject(objRecord)) {\r
HSSFObjectData objectData = new HSSFObjectData(container, objRecord, root);\r
out.addShape(objectData);\r
}\r
\r
private static boolean isEmbeddedObject(ObjRecord obj) {\r
- Iterator<SubRecord> subRecordIter = obj.getSubRecords().iterator();\r
- while (subRecordIter.hasNext()) {\r
- SubRecord sub = subRecordIter.next();\r
+ for (SubRecord sub : obj.getSubRecords()) {\r
if (sub instanceof EmbeddedObjectRefSubRecord) {\r
return true;\r
}\r
// Need to walk backward to find the last non-blank row
// NOTE: n is always negative here
_lastrow = Math.min(endRow + n, SpreadsheetVersion.EXCEL97.getLastRowIndex());
- for (int i = endRow - 1; i > endRow + n; i++) {
+ for (int i = endRow - 1; i > endRow + n; i--) {
if (getRow(i) != null) {
_lastrow = i;
break;
public void writeOut(OutputStream out) throws IOException {
ByteArrayOutputStream bout = new ByteArrayOutputStream();
- Iterator<EscherRecord> iter = dggContainer.getChildIterator();
- while (iter.hasNext()) {
- EscherRecord r = iter.next();
+ for (EscherRecord r : dggContainer) {
if (r.getRecordId() == EscherContainerRecord.BSTORE_CONTAINER){
EscherContainerRecord bstore = (EscherContainerRecord)r;
ByteArrayOutputStream b2 = new ByteArrayOutputStream();
- for (Iterator<EscherRecord> it= bstore.getChildIterator(); it.hasNext();) {
- EscherBSERecord bse = (EscherBSERecord)it.next();
+ for (EscherRecord br : bstore) {
byte[] b = new byte[36+8];
- bse.serialize(0, b);
+ br.serialize(0, b);
b2.write(b);
}
byte[] bstorehead = new byte[8];
public EscherDggRecord getEscherDggRecord(){
if(dgg == null){
- for(Iterator<EscherRecord> it = dggContainer.getChildIterator(); it.hasNext();){
- EscherRecord r = it.next();
+ for(EscherRecord r : dggContainer){
if(r instanceof EscherDggRecord){
dgg = (EscherDggRecord)r;
break;
@Override
public List<HSLFShape> getShapes() {
- // Out escher container record should contain several
- // SpContainers, the first of which is the group shape itself
- Iterator<EscherRecord> iter = getSpContainer().getChildIterator();
-
- // Don't include the first SpContainer, it is always NotPrimitive
- if (iter.hasNext()) {
- iter.next();
- }
+ // Our escher container record should contain several
+ // SpContainers, the first of which is the group shape itself
List<HSLFShape> shapeList = new ArrayList<HSLFShape>();
- while (iter.hasNext()) {
- EscherRecord r = iter.next();
+ boolean isFirst = true;
+ for (EscherRecord r : getSpContainer()) {
+ if (isFirst) {
+ // Don't include the first SpContainer, it is always NotPrimitive
+ isFirst = false;
+ continue;
+ }
+
if(r instanceof EscherContainerRecord) {
// Create the Shape for it
EscherContainerRecord container = (EscherContainerRecord)r;
EscherContainerRecord dg = ppdrawing.getDgContainer();
EscherContainerRecord spgr = null;
- for (Iterator<EscherRecord> it = dg.getChildIterator(); it.hasNext();) {
- EscherRecord rec = it.next();
+ for (EscherRecord rec : dg) {
if (rec.getRecordId() == EscherContainerRecord.SPGR_CONTAINER) {
spgr = (EscherContainerRecord) rec;
break;
}
List<HSLFShape> shapeList = new ArrayList<HSLFShape>();
- Iterator<EscherRecord> it = spgr.getChildIterator();
- if (it.hasNext()) {
- // skip first item
- it.next();
- }
- for (; it.hasNext();) {
- EscherContainerRecord sp = (EscherContainerRecord) it.next();
+ boolean isFirst = true;
+ for (EscherRecord r : spgr) {
+ if (isFirst) {
+ // skip first item
+ isFirst = false;
+ continue;
+ }
+
+ EscherContainerRecord sp = (EscherContainerRecord)r;
HSLFShape sh = HSLFShapeFactory.createShape(sp, null);
sh.setSheet(this);
} catch (Exception e) {
throw new EncryptedPowerPointFileException(e);
} finally {
- try {
- if (ccos != null) {
- ccos.close();
- }
- los.close();
- } catch (IOException e) {
- throw new EncryptedPowerPointFileException(e);
- }
+ IOUtils.closeQuietly(ccos);
+ IOUtils.closeQuietly(los);
}
}
}\r
}\r
\r
- if (lastPTPC == null || lastRTPC == null || ptpc == null || rtpc == null) {\r
+ if (lastPTPC == null || lastRTPC == null || ptpc == null || rtpc == null) { // NOSONAR\r
throw new HSLFException("Not all TextPropCollection could be determined.");\r
}\r
\r
if(r instanceof ChartRecord) {
lastSeries = null;
-
lastChart = new HSSFChart(sheet,(ChartRecord)r);
charts.add(lastChart);
- } else if(r instanceof LegendRecord) {
+ } else if (r instanceof LinkedDataRecord) {
+ LinkedDataRecord linkedDataRecord = (LinkedDataRecord) r;
+ if (lastSeries != null) {
+ lastSeries.insertData(linkedDataRecord);
+ }
+ }
+
+ if (lastChart == null) {
+ continue;
+ }
+
+ if (r instanceof LegendRecord) {
lastChart.legendRecord = (LegendRecord)r;
} else if(r instanceof SeriesRecord) {
HSSFSeries series = new HSSFSeries( (SeriesRecord)r );
lastChart.series.add(series);
lastSeries = series;
} else if(r instanceof ChartTitleFormatRecord) {
- lastChart.chartTitleFormat =
- (ChartTitleFormatRecord)r;
+ lastChart.chartTitleFormat = (ChartTitleFormatRecord)r;
} else if(r instanceof SeriesTextRecord) {
- // Applies to a series, unless we've seen
- // a legend already
+ // Applies to a series, unless we've seen a legend already
SeriesTextRecord str = (SeriesTextRecord)r;
- if(lastChart.legendRecord == null &&
- lastChart.series.size() > 0) {
+ if(lastChart.legendRecord == null && lastChart.series.size() > 0) {
HSSFSeries series = lastChart.series.get(lastChart.series.size()-1);
series.seriesTitleText = str;
} else {
lastChart.chartTitleText = str;
}
- } else if (r instanceof LinkedDataRecord) {
- LinkedDataRecord linkedDataRecord = (LinkedDataRecord) r;
- if (lastSeries != null) {
- lastSeries.insertData(linkedDataRecord);
- }
} else if(r instanceof ValueRangeRecord){
lastChart.valueRanges.add((ValueRangeRecord)r);
} else if (r instanceof Record) {
- if (lastChart != null)
- {
- Record record = (Record) r;
- for (HSSFChartType type : HSSFChartType.values()) {
- if (type == HSSFChartType.Unknown)
- {
- continue;
- }
- if (record.getSid() == type.getSid()) {
- lastChart.type = type ;
- break;
- }
+ Record record = (Record) r;
+ for (HSSFChartType type : HSSFChartType.values()) {
+ if (type == HSSFChartType.Unknown) {
+ continue;
+ }
+ if (record.getSid() == type.getSid()) {
+ lastChart.type = type;
+ break;
}
}
}