2004-04-09 07:45:38 -04:00
|
|
|
/* ====================================================================
|
2006-12-22 14:18:16 -05:00
|
|
|
Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
contributor license agreements. See the NOTICE file distributed with
|
|
|
|
this work for additional information regarding copyright ownership.
|
|
|
|
The ASF licenses this file to You under the Apache License, Version 2.0
|
|
|
|
(the "License"); you may not use this file except in compliance with
|
|
|
|
the License. You may obtain a copy of the License at
|
2004-04-09 09:05:39 -04:00
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
==================================================================== */
|
2009-05-19 15:21:12 -04:00
|
|
|
|
2004-04-09 07:45:38 -04:00
|
|
|
package org.apache.poi.ddf;
|
|
|
|
|
2009-05-19 15:21:12 -04:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
2004-04-09 07:45:38 -04:00
|
|
|
import org.apache.poi.util.HexDump;
|
|
|
|
import org.apache.poi.util.LittleEndian;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This record is used whenever a escher record is encountered that
|
|
|
|
* we do not explicitly support.
|
|
|
|
*
|
|
|
|
* @author Glen Stampoultzis (glens at apache.org)
|
2010-07-27 11:24:25 -04:00
|
|
|
* @author Zhang Zhang (zhangzzh at gmail.com)
|
2004-04-09 07:45:38 -04:00
|
|
|
*/
|
2009-05-19 15:21:12 -04:00
|
|
|
public final class UnknownEscherRecord extends EscherRecord {
|
2004-04-09 07:45:38 -04:00
|
|
|
private static final byte[] NO_BYTES = new byte[0];
|
|
|
|
|
|
|
|
/** The data for this record not including the the 8 byte header */
|
|
|
|
private byte[] thedata = NO_BYTES;
|
2009-05-19 15:21:12 -04:00
|
|
|
private List<EscherRecord> _childRecords;
|
2004-04-09 07:45:38 -04:00
|
|
|
|
2009-05-19 15:21:12 -04:00
|
|
|
public UnknownEscherRecord() {
|
|
|
|
_childRecords = new ArrayList<EscherRecord>();
|
2004-04-09 07:45:38 -04:00
|
|
|
}
|
|
|
|
|
2009-05-19 15:21:12 -04:00
|
|
|
public int fillFields(byte[] data, int offset, EscherRecordFactory recordFactory) {
|
2004-04-09 07:45:38 -04:00
|
|
|
int bytesRemaining = readHeader( data, offset );
|
2010-07-27 11:24:25 -04:00
|
|
|
/*
|
|
|
|
* Modified by Zhang Zhang
|
|
|
|
* Have a check between avaliable bytes and bytesRemaining,
|
|
|
|
* take the avaliable length if the bytesRemaining out of range.
|
|
|
|
* July 09, 2010
|
|
|
|
*/
|
|
|
|
int avaliable = data.length - (offset + 8);
|
|
|
|
if (bytesRemaining > avaliable) {
|
|
|
|
bytesRemaining = avaliable;
|
|
|
|
}
|
|
|
|
|
2009-05-19 15:21:12 -04:00
|
|
|
if (isContainerRecord()) {
|
2004-04-09 07:45:38 -04:00
|
|
|
int bytesWritten = 0;
|
|
|
|
thedata = new byte[0];
|
|
|
|
offset += 8;
|
|
|
|
bytesWritten += 8;
|
|
|
|
while ( bytesRemaining > 0 )
|
|
|
|
{
|
|
|
|
EscherRecord child = recordFactory.createRecord( data, offset );
|
|
|
|
int childBytesWritten = child.fillFields( data, offset, recordFactory );
|
|
|
|
bytesWritten += childBytesWritten;
|
|
|
|
offset += childBytesWritten;
|
|
|
|
bytesRemaining -= childBytesWritten;
|
|
|
|
getChildRecords().add( child );
|
|
|
|
}
|
|
|
|
return bytesWritten;
|
|
|
|
}
|
2010-07-27 11:24:25 -04:00
|
|
|
|
2009-05-19 15:21:12 -04:00
|
|
|
thedata = new byte[bytesRemaining];
|
|
|
|
System.arraycopy( data, offset + 8, thedata, 0, bytesRemaining );
|
|
|
|
return bytesRemaining + 8;
|
2004-04-09 07:45:38 -04:00
|
|
|
}
|
|
|
|
|
2009-05-19 15:21:12 -04:00
|
|
|
public int serialize(int offset, byte[] data, EscherSerializationListener listener) {
|
2004-04-09 07:45:38 -04:00
|
|
|
listener.beforeRecordSerialize( offset, getRecordId(), this );
|
|
|
|
|
|
|
|
LittleEndian.putShort(data, offset, getOptions());
|
|
|
|
LittleEndian.putShort(data, offset+2, getRecordId());
|
|
|
|
int remainingBytes = thedata.length;
|
2009-05-19 15:21:12 -04:00
|
|
|
for (EscherRecord r : _childRecords) {
|
2004-04-09 07:45:38 -04:00
|
|
|
remainingBytes += r.getRecordSize();
|
|
|
|
}
|
|
|
|
LittleEndian.putInt(data, offset+4, remainingBytes);
|
|
|
|
System.arraycopy(thedata, 0, data, offset+8, thedata.length);
|
|
|
|
int pos = offset+8+thedata.length;
|
2009-05-19 15:21:12 -04:00
|
|
|
for (EscherRecord r : _childRecords) {
|
2004-04-09 07:45:38 -04:00
|
|
|
pos += r.serialize(pos, data, listener );
|
|
|
|
}
|
|
|
|
|
|
|
|
listener.afterRecordSerialize( pos, getRecordId(), pos - offset, this );
|
|
|
|
return pos - offset;
|
|
|
|
}
|
|
|
|
|
2009-05-19 15:21:12 -04:00
|
|
|
public byte[] getData() {
|
2004-04-09 07:45:38 -04:00
|
|
|
return thedata;
|
|
|
|
}
|
|
|
|
|
2009-05-19 15:21:12 -04:00
|
|
|
public int getRecordSize() {
|
2004-04-09 07:45:38 -04:00
|
|
|
return 8 + thedata.length;
|
|
|
|
}
|
|
|
|
|
2009-05-19 15:21:12 -04:00
|
|
|
public List<EscherRecord> getChildRecords() {
|
|
|
|
return _childRecords;
|
2004-04-09 07:45:38 -04:00
|
|
|
}
|
|
|
|
|
2009-05-19 15:21:12 -04:00
|
|
|
public void setChildRecords(List<EscherRecord> childRecords) {
|
|
|
|
_childRecords = childRecords;
|
2004-04-09 07:45:38 -04:00
|
|
|
}
|
|
|
|
|
2009-05-19 15:21:12 -04:00
|
|
|
public Object clone() {
|
2004-04-09 07:45:38 -04:00
|
|
|
// shallow clone
|
|
|
|
return super.clone();
|
|
|
|
}
|
|
|
|
|
2009-05-19 15:21:12 -04:00
|
|
|
public String getRecordName() {
|
2004-04-09 07:45:38 -04:00
|
|
|
return "Unknown 0x" + HexDump.toHex(getRecordId());
|
|
|
|
}
|
|
|
|
|
2009-05-19 15:21:12 -04:00
|
|
|
public String toString() {
|
2004-04-09 07:45:38 -04:00
|
|
|
StringBuffer children = new StringBuffer();
|
2009-05-19 15:21:12 -04:00
|
|
|
if (getChildRecords().size() > 0) {
|
|
|
|
children.append( " children: " + '\n' );
|
|
|
|
for (EscherRecord record : _childRecords) {
|
2004-04-09 07:45:38 -04:00
|
|
|
children.append( record.toString() );
|
2009-05-19 15:21:12 -04:00
|
|
|
children.append( '\n' );
|
2004-04-09 07:45:38 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-19 15:21:12 -04:00
|
|
|
String theDumpHex = HexDump.toHex(thedata, 32);
|
2004-04-09 07:45:38 -04:00
|
|
|
|
2009-05-19 15:21:12 -04:00
|
|
|
return getClass().getName() + ":" + '\n' +
|
|
|
|
" isContainer: " + isContainerRecord() + '\n' +
|
|
|
|
" options: 0x" + HexDump.toHex( getOptions() ) + '\n' +
|
|
|
|
" recordId: 0x" + HexDump.toHex( getRecordId() ) + '\n' +
|
|
|
|
" numchildren: " + getChildRecords().size() + '\n' +
|
2004-04-09 07:45:38 -04:00
|
|
|
theDumpHex +
|
|
|
|
children.toString();
|
|
|
|
}
|
|
|
|
|
2009-05-19 15:21:12 -04:00
|
|
|
public void addChildRecord(EscherRecord childRecord) {
|
2004-04-09 07:45:38 -04:00
|
|
|
getChildRecords().add( childRecord );
|
|
|
|
}
|
|
|
|
}
|