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.
|
|
|
|
==================================================================== */
|
2004-04-09 07:45:38 -04:00
|
|
|
|
|
|
|
package org.apache.poi.hssf.record;
|
|
|
|
|
2008-01-09 18:21:35 -05:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
2004-04-09 07:45:38 -04:00
|
|
|
import org.apache.poi.ddf.DefaultEscherRecordFactory;
|
2008-01-09 18:21:35 -05:00
|
|
|
import org.apache.poi.ddf.EscherContainerRecord;
|
2004-04-09 07:45:38 -04:00
|
|
|
import org.apache.poi.ddf.EscherRecord;
|
|
|
|
import org.apache.poi.ddf.EscherRecordFactory;
|
|
|
|
import org.apache.poi.ddf.NullEscherSerializationListener;
|
|
|
|
import org.apache.poi.util.LittleEndian;
|
2010-05-05 12:45:58 -04:00
|
|
|
import org.apache.poi.hssf.util.LazilyConcatenatedByteArray;
|
2004-04-09 07:45:38 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The escher container record is used to hold escher records. It is abstract and
|
|
|
|
* must be subclassed for maximum benefit.
|
|
|
|
*/
|
2015-09-28 19:16:58 -04:00
|
|
|
public abstract class AbstractEscherHolderRecord extends Record implements Cloneable {
|
2007-08-15 10:19:08 -04:00
|
|
|
private static boolean DESERIALISE;
|
|
|
|
static {
|
|
|
|
try {
|
|
|
|
DESERIALISE = (System.getProperty("poi.deserialize.escher") != null);
|
|
|
|
} catch (SecurityException e) {
|
|
|
|
DESERIALISE = false;
|
|
|
|
}
|
|
|
|
}
|
2004-04-09 07:45:38 -04:00
|
|
|
|
2016-07-04 19:45:19 -04:00
|
|
|
private final List<EscherRecord> escherRecords;
|
|
|
|
private final LazilyConcatenatedByteArray rawDataContainer = new LazilyConcatenatedByteArray();
|
2004-04-09 07:45:38 -04:00
|
|
|
|
|
|
|
public AbstractEscherHolderRecord()
|
|
|
|
{
|
2010-01-13 08:42:15 -05:00
|
|
|
escherRecords = new ArrayList<EscherRecord>();
|
2004-04-09 07:45:38 -04:00
|
|
|
}
|
|
|
|
|
2005-08-18 03:06:44 -04:00
|
|
|
public AbstractEscherHolderRecord(RecordInputStream in)
|
2004-04-09 07:45:38 -04:00
|
|
|
{
|
2010-01-13 08:42:15 -05:00
|
|
|
escherRecords = new ArrayList<EscherRecord>();
|
2014-08-31 07:40:22 -04:00
|
|
|
if (! DESERIALISE ) {
|
2010-05-05 12:45:58 -04:00
|
|
|
rawDataContainer.concatenate(in.readRemainder());
|
2014-08-31 07:40:22 -04:00
|
|
|
} else {
|
2005-08-18 03:06:44 -04:00
|
|
|
byte[] data = in.readAllContinuedRemainder();
|
|
|
|
convertToEscherRecords( 0, data.length, data );
|
2005-05-01 07:26:18 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-01-09 18:21:35 -05:00
|
|
|
protected void convertRawBytesToEscherRecords() {
|
2014-08-31 07:40:22 -04:00
|
|
|
if (! DESERIALISE ) {
|
|
|
|
byte[] rawData = getRawData();
|
|
|
|
convertToEscherRecords(0, rawData.length, rawData);
|
|
|
|
}
|
2008-01-09 18:21:35 -05:00
|
|
|
}
|
2005-05-01 07:26:18 -04:00
|
|
|
private void convertToEscherRecords( int offset, int size, byte[] data )
|
|
|
|
{
|
2011-07-20 03:38:01 -04:00
|
|
|
escherRecords.clear();
|
2005-05-01 07:26:18 -04:00
|
|
|
EscherRecordFactory recordFactory = new DefaultEscherRecordFactory();
|
|
|
|
int pos = offset;
|
|
|
|
while ( pos < offset + size )
|
|
|
|
{
|
|
|
|
EscherRecord r = recordFactory.createRecord(data, pos);
|
|
|
|
int bytesRead = r.fillFields(data, pos, recordFactory );
|
|
|
|
escherRecords.add(r);
|
|
|
|
pos += bytesRead;
|
2004-04-09 07:45:38 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-04 19:45:19 -04:00
|
|
|
@Override
|
2004-04-09 07:45:38 -04:00
|
|
|
public String toString()
|
|
|
|
{
|
|
|
|
StringBuffer buffer = new StringBuffer();
|
|
|
|
|
|
|
|
final String nl = System.getProperty("line.separator");
|
2004-08-23 04:52:54 -04:00
|
|
|
buffer.append('[' + getRecordName() + ']' + nl);
|
2005-05-01 07:26:18 -04:00
|
|
|
if (escherRecords.size() == 0)
|
|
|
|
buffer.append("No Escher Records Decoded" + nl);
|
2016-07-04 19:45:19 -04:00
|
|
|
for (EscherRecord r : escherRecords) {
|
2004-04-09 07:45:38 -04:00
|
|
|
buffer.append(r.toString());
|
|
|
|
}
|
2004-08-23 04:52:54 -04:00
|
|
|
buffer.append("[/" + getRecordName() + ']' + nl);
|
2004-04-09 07:45:38 -04:00
|
|
|
|
|
|
|
return buffer.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected abstract String getRecordName();
|
|
|
|
|
2016-07-04 19:45:19 -04:00
|
|
|
@Override
|
2004-04-09 07:45:38 -04:00
|
|
|
public int serialize(int offset, byte[] data)
|
|
|
|
{
|
2004-08-23 04:52:54 -04:00
|
|
|
LittleEndian.putShort( data, 0 + offset, getSid() );
|
|
|
|
LittleEndian.putShort( data, 2 + offset, (short) ( getRecordSize() - 4 ) );
|
2010-05-05 12:45:58 -04:00
|
|
|
byte[] rawData = getRawData();
|
2004-08-23 04:52:54 -04:00
|
|
|
if ( escherRecords.size() == 0 && rawData != null )
|
2004-04-09 07:45:38 -04:00
|
|
|
{
|
2005-05-01 07:26:18 -04:00
|
|
|
LittleEndian.putShort(data, 0 + offset, getSid());
|
|
|
|
LittleEndian.putShort(data, 2 + offset, (short)(getRecordSize() - 4));
|
|
|
|
System.arraycopy( rawData, 0, data, 4 + offset, rawData.length);
|
|
|
|
return rawData.length + 4;
|
2004-04-09 07:45:38 -04:00
|
|
|
}
|
2009-08-18 12:50:24 -04:00
|
|
|
LittleEndian.putShort(data, 0 + offset, getSid());
|
|
|
|
LittleEndian.putShort(data, 2 + offset, (short)(getRecordSize() - 4));
|
2005-05-01 07:26:18 -04:00
|
|
|
|
2009-08-18 12:50:24 -04:00
|
|
|
int pos = offset + 4;
|
2016-07-04 19:45:19 -04:00
|
|
|
for (EscherRecord r : escherRecords) {
|
2009-08-18 12:50:24 -04:00
|
|
|
pos += r.serialize( pos, data, new NullEscherSerializationListener() );
|
2004-04-09 07:45:38 -04:00
|
|
|
}
|
2004-08-23 04:52:54 -04:00
|
|
|
return getRecordSize();
|
2004-04-09 07:45:38 -04:00
|
|
|
}
|
2008-12-03 19:08:45 -05:00
|
|
|
|
2016-07-04 19:45:19 -04:00
|
|
|
@Override
|
2008-12-03 19:08:45 -05:00
|
|
|
public int getRecordSize() {
|
2010-05-05 12:45:58 -04:00
|
|
|
byte[] rawData = getRawData();
|
2008-10-30 21:02:55 -04:00
|
|
|
if (escherRecords.size() == 0 && rawData != null) {
|
2010-05-05 12:45:58 -04:00
|
|
|
// XXX: It should be possible to derive this without concatenating the array, too.
|
2008-10-30 21:02:55 -04:00
|
|
|
return rawData.length;
|
2004-04-09 07:45:38 -04:00
|
|
|
}
|
2008-10-30 21:02:55 -04:00
|
|
|
int size = 0;
|
2016-07-04 19:45:19 -04:00
|
|
|
for (EscherRecord r : escherRecords) {
|
2008-10-30 21:02:55 -04:00
|
|
|
size += r.getRecordSize();
|
2004-04-09 07:45:38 -04:00
|
|
|
}
|
2008-10-30 21:02:55 -04:00
|
|
|
return size;
|
2004-04-09 07:45:38 -04:00
|
|
|
}
|
|
|
|
|
2008-10-30 21:02:55 -04:00
|
|
|
|
2004-04-09 07:45:38 -04:00
|
|
|
|
2016-07-04 19:45:19 -04:00
|
|
|
@Override
|
2004-04-09 07:45:38 -04:00
|
|
|
public abstract short getSid();
|
|
|
|
|
2015-09-28 19:16:58 -04:00
|
|
|
@Override
|
|
|
|
public AbstractEscherHolderRecord clone() {
|
|
|
|
return (AbstractEscherHolderRecord)cloneViaReserialise();
|
2004-04-09 07:45:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public void addEscherRecord(int index, EscherRecord element)
|
|
|
|
{
|
|
|
|
escherRecords.add( index, element );
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean addEscherRecord(EscherRecord element)
|
|
|
|
{
|
|
|
|
return escherRecords.add( element );
|
|
|
|
}
|
|
|
|
|
2010-01-13 08:42:15 -05:00
|
|
|
public List<EscherRecord> getEscherRecords()
|
2004-04-09 07:45:38 -04:00
|
|
|
{
|
|
|
|
return escherRecords;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void clearEscherRecords()
|
|
|
|
{
|
|
|
|
escherRecords.clear();
|
|
|
|
}
|
2009-08-18 12:50:24 -04:00
|
|
|
|
2008-01-09 18:21:35 -05:00
|
|
|
/**
|
|
|
|
* If we have a EscherContainerRecord as one of our
|
|
|
|
* children (and most top level escher holders do),
|
|
|
|
* then return that.
|
2016-07-04 19:45:19 -04:00
|
|
|
*
|
|
|
|
* @return the EscherContainerRecord or {@code null} if no child is a container record
|
2008-01-09 18:21:35 -05:00
|
|
|
*/
|
|
|
|
public EscherContainerRecord getEscherContainer() {
|
2016-07-04 19:45:19 -04:00
|
|
|
for (EscherRecord er : escherRecords) {
|
2008-01-09 18:21:35 -05:00
|
|
|
if(er instanceof EscherContainerRecord) {
|
|
|
|
return (EscherContainerRecord)er;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Descends into all our children, returning the
|
|
|
|
* first EscherRecord with the given id, or null
|
|
|
|
* if none found
|
2016-07-04 19:45:19 -04:00
|
|
|
*
|
|
|
|
* @param id the record to look for
|
|
|
|
*
|
|
|
|
* @return the record or {@code null} if it can't be found
|
2008-01-09 18:21:35 -05:00
|
|
|
*/
|
|
|
|
public EscherRecord findFirstWithId(short id) {
|
|
|
|
return findFirstWithId(id, getEscherRecords());
|
|
|
|
}
|
2016-07-04 19:45:19 -04:00
|
|
|
|
2009-02-19 20:55:47 -05:00
|
|
|
private EscherRecord findFirstWithId(short id, List<EscherRecord> records) {
|
2008-01-09 18:21:35 -05:00
|
|
|
// Check at our level
|
2016-07-04 19:45:19 -04:00
|
|
|
for (EscherRecord r : records) {
|
2008-01-09 18:21:35 -05:00
|
|
|
if(r.getRecordId() == id) {
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
}
|
2009-08-18 12:50:24 -04:00
|
|
|
|
2008-01-09 18:21:35 -05:00
|
|
|
// Then check our children in turn
|
2016-07-04 19:45:19 -04:00
|
|
|
for (EscherRecord r : records) {
|
2008-01-09 18:21:35 -05:00
|
|
|
if(r.isContainerRecord()) {
|
2009-02-19 20:55:47 -05:00
|
|
|
EscherRecord found = findFirstWithId(id, r.getChildRecords());
|
2008-01-09 18:21:35 -05:00
|
|
|
if(found != null) {
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-08-18 12:50:24 -04:00
|
|
|
|
2008-01-09 18:21:35 -05:00
|
|
|
// Not found in this lot
|
|
|
|
return null;
|
|
|
|
}
|
2004-04-09 07:45:38 -04:00
|
|
|
|
|
|
|
|
|
|
|
public EscherRecord getEscherRecord(int index)
|
|
|
|
{
|
2014-08-31 07:40:22 -04:00
|
|
|
return escherRecords.get(index);
|
2004-04-09 07:45:38 -04:00
|
|
|
}
|
|
|
|
|
2005-05-01 07:26:18 -04:00
|
|
|
/**
|
|
|
|
* Big drawing group records are split but it's easier to deal with them
|
|
|
|
* as a whole group so we need to join them together.
|
2016-07-04 19:45:19 -04:00
|
|
|
*
|
|
|
|
* @param record the record data to concatenate to the end
|
2005-05-01 07:26:18 -04:00
|
|
|
*/
|
|
|
|
public void join( AbstractEscherHolderRecord record )
|
|
|
|
{
|
2010-05-05 12:45:58 -04:00
|
|
|
rawDataContainer.concatenate(record.getRawData());
|
2005-05-01 07:26:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public void processContinueRecord( byte[] record )
|
|
|
|
{
|
2010-05-05 12:45:58 -04:00
|
|
|
rawDataContainer.concatenate(record);
|
2005-05-01 07:26:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public byte[] getRawData()
|
|
|
|
{
|
2010-05-05 12:45:58 -04:00
|
|
|
return rawDataContainer.toArray();
|
2005-05-01 07:26:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public void setRawData( byte[] rawData )
|
|
|
|
{
|
2010-05-05 12:45:58 -04:00
|
|
|
rawDataContainer.clear();
|
|
|
|
rawDataContainer.concatenate(rawData);
|
2005-05-01 07:26:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert raw data to escher records.
|
|
|
|
*/
|
|
|
|
public void decode()
|
|
|
|
{
|
2012-08-14 17:11:53 -04:00
|
|
|
if (null == escherRecords || 0 == escherRecords.size()){
|
|
|
|
byte[] rawData = getRawData();
|
|
|
|
convertToEscherRecords(0, rawData.length, rawData );
|
|
|
|
}
|
2005-05-01 07:26:18 -04:00
|
|
|
}
|
2008-10-05 03:22:12 -04:00
|
|
|
}
|