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.Iterator;
|
|
|
|
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;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The escher container record is used to hold escher records. It is abstract and
|
|
|
|
* must be subclassed for maximum benefit.
|
|
|
|
*
|
|
|
|
* @author Glen Stampoultzis (glens at apache.org)
|
2004-08-23 04:52:54 -04:00
|
|
|
* @author Michael Zalewski (zalewski at optonline.net)
|
2004-04-09 07:45:38 -04:00
|
|
|
*/
|
|
|
|
public abstract class AbstractEscherHolderRecord
|
|
|
|
extends Record
|
|
|
|
{
|
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
|
|
|
|
|
|
|
private List escherRecords;
|
|
|
|
private byte[] rawData;
|
|
|
|
|
|
|
|
|
|
|
|
public AbstractEscherHolderRecord()
|
|
|
|
{
|
|
|
|
escherRecords = new ArrayList();
|
|
|
|
}
|
|
|
|
|
2005-08-18 03:06:44 -04:00
|
|
|
public AbstractEscherHolderRecord(RecordInputStream in)
|
2004-04-09 07:45:38 -04:00
|
|
|
{
|
|
|
|
escherRecords = new ArrayList();
|
|
|
|
if (! DESERIALISE )
|
|
|
|
{
|
2005-08-18 03:06:44 -04:00
|
|
|
rawData = in.readRemainder();
|
2004-04-09 07:45:38 -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() {
|
|
|
|
convertToEscherRecords(0, rawData.length, rawData);
|
|
|
|
}
|
2005-05-01 07:26:18 -04:00
|
|
|
private void convertToEscherRecords( int offset, int size, byte[] data )
|
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2004-04-09 07:45:38 -04:00
|
|
|
for ( Iterator iterator = escherRecords.iterator(); iterator.hasNext(); )
|
|
|
|
{
|
|
|
|
EscherRecord r = (EscherRecord) iterator.next();
|
|
|
|
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();
|
|
|
|
|
|
|
|
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 ) );
|
|
|
|
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
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2005-05-01 07:26:18 -04:00
|
|
|
LittleEndian.putShort(data, 0 + offset, getSid());
|
|
|
|
LittleEndian.putShort(data, 2 + offset, (short)(getRecordSize() - 4));
|
|
|
|
|
2004-04-09 07:45:38 -04:00
|
|
|
int pos = offset + 4;
|
|
|
|
for ( Iterator iterator = escherRecords.iterator(); iterator.hasNext(); )
|
|
|
|
{
|
|
|
|
EscherRecord r = (EscherRecord) iterator.next();
|
2004-08-23 04:52:54 -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
|
|
|
}
|
|
|
|
|
2004-08-23 04:52:54 -04:00
|
|
|
// public int serialize(int offset, byte[] data)
|
|
|
|
// {
|
|
|
|
// if (escherRecords.size() == 0 && rawData != null)
|
|
|
|
// {
|
|
|
|
// System.arraycopy( rawData, 0, data, offset, rawData.length);
|
|
|
|
// return rawData.length;
|
|
|
|
// }
|
|
|
|
// else
|
|
|
|
// {
|
|
|
|
// collapseShapeInformation();
|
|
|
|
//
|
|
|
|
// LittleEndian.putShort(data, 0 + offset, getSid());
|
|
|
|
// LittleEndian.putShort(data, 2 + offset, (short)(getRecordSize() - 4));
|
|
|
|
//
|
|
|
|
// int pos = offset + 4;
|
|
|
|
// for ( Iterator iterator = escherRecords.iterator(); iterator.hasNext(); )
|
|
|
|
// {
|
|
|
|
// EscherRecord r = (EscherRecord) iterator.next();
|
|
|
|
// pos += r.serialize(pos, data, new NullEscherSerializationListener() );
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// return getRecordSize();
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
|
2004-04-09 07:45:38 -04:00
|
|
|
/**
|
|
|
|
* Size of record (including 4 byte header)
|
|
|
|
*/
|
|
|
|
public int getRecordSize()
|
|
|
|
{
|
|
|
|
if (escherRecords.size() == 0 && rawData != null)
|
|
|
|
{
|
2004-08-23 04:52:54 -04:00
|
|
|
return rawData.length + 4;
|
2004-04-09 07:45:38 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int size = 4;
|
|
|
|
for ( Iterator iterator = escherRecords.iterator(); iterator.hasNext(); )
|
|
|
|
{
|
|
|
|
EscherRecord r = (EscherRecord) iterator.next();
|
|
|
|
size += r.getRecordSize();
|
|
|
|
}
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-08-23 04:52:54 -04:00
|
|
|
//
|
|
|
|
// /**
|
|
|
|
// * Size of record (including 4 byte header)
|
|
|
|
// */
|
|
|
|
// public int getRecordSize()
|
|
|
|
// {
|
|
|
|
// if (escherRecords.size() == 0 && rawData != null)
|
|
|
|
// {
|
|
|
|
// return rawData.length;
|
|
|
|
// }
|
|
|
|
// else
|
|
|
|
// {
|
|
|
|
// collapseShapeInformation();
|
|
|
|
//
|
|
|
|
// int size = 4;
|
|
|
|
// for ( Iterator iterator = escherRecords.iterator(); iterator.hasNext(); )
|
|
|
|
// {
|
|
|
|
// EscherRecord r = (EscherRecord) iterator.next();
|
|
|
|
// size += r.getRecordSize();
|
|
|
|
// }
|
|
|
|
// return size;
|
|
|
|
// }
|
|
|
|
// }
|
2004-04-09 07:45:38 -04:00
|
|
|
|
|
|
|
public abstract short getSid();
|
|
|
|
|
|
|
|
public Object clone()
|
|
|
|
{
|
2008-01-15 11:42:44 -05:00
|
|
|
return 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 );
|
|
|
|
}
|
|
|
|
|
|
|
|
public List getEscherRecords()
|
|
|
|
{
|
|
|
|
return escherRecords;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void clearEscherRecords()
|
|
|
|
{
|
|
|
|
escherRecords.clear();
|
|
|
|
}
|
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.
|
|
|
|
*/
|
|
|
|
public EscherContainerRecord getEscherContainer() {
|
|
|
|
for(Iterator it = escherRecords.iterator(); it.hasNext();) {
|
|
|
|
Object er = it.next();
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
public EscherRecord findFirstWithId(short id) {
|
|
|
|
return findFirstWithId(id, getEscherRecords());
|
|
|
|
}
|
|
|
|
private EscherRecord findFirstWithId(short id, List records) {
|
|
|
|
// Check at our level
|
|
|
|
for(Iterator it = records.iterator(); it.hasNext();) {
|
|
|
|
EscherRecord r = (EscherRecord)it.next();
|
|
|
|
if(r.getRecordId() == id) {
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Then check our children in turn
|
|
|
|
for(Iterator it = records.iterator(); it.hasNext();) {
|
|
|
|
EscherRecord r = (EscherRecord)it.next();
|
|
|
|
if(r.isContainerRecord()) {
|
|
|
|
EscherRecord found =
|
|
|
|
findFirstWithId(id, r.getChildRecords());
|
|
|
|
if(found != null) {
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Not found in this lot
|
|
|
|
return null;
|
|
|
|
}
|
2004-04-09 07:45:38 -04:00
|
|
|
|
|
|
|
|
|
|
|
public EscherRecord getEscherRecord(int index)
|
|
|
|
{
|
|
|
|
return (EscherRecord) escherRecords.get(index);
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
public void join( AbstractEscherHolderRecord record )
|
|
|
|
{
|
|
|
|
int length = this.rawData.length + record.getRawData().length;
|
|
|
|
byte[] data = new byte[length];
|
|
|
|
System.arraycopy( rawData, 0, data, 0, rawData.length );
|
|
|
|
System.arraycopy( record.getRawData(), 0, data, rawData.length, record.getRawData().length );
|
|
|
|
rawData = data;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void processContinueRecord( byte[] record )
|
|
|
|
{
|
|
|
|
int length = this.rawData.length + record.length;
|
|
|
|
byte[] data = new byte[length];
|
|
|
|
System.arraycopy( rawData, 0, data, 0, rawData.length );
|
|
|
|
System.arraycopy( record, 0, data, rawData.length, record.length );
|
|
|
|
rawData = data;
|
|
|
|
}
|
|
|
|
|
|
|
|
public byte[] getRawData()
|
|
|
|
{
|
|
|
|
return rawData;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setRawData( byte[] rawData )
|
|
|
|
{
|
|
|
|
this.rawData = rawData;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert raw data to escher records.
|
|
|
|
*/
|
|
|
|
public void decode()
|
|
|
|
{
|
|
|
|
convertToEscherRecords(0, rawData.length, rawData );
|
|
|
|
}
|
2004-04-09 07:45:38 -04:00
|
|
|
|
|
|
|
} // END OF CLASS
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|