2004-04-09 09:05:39 -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;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
2005-05-01 07:26:18 -04:00
|
|
|
import java.util.List;
|
2004-04-09 07:45:38 -04:00
|
|
|
|
2016-06-22 18:26:08 -04:00
|
|
|
import org.apache.poi.util.LittleEndian;
|
|
|
|
|
2004-04-09 07:45:38 -04:00
|
|
|
/**
|
|
|
|
* Generates a property given a reference into the byte array storing that property.
|
|
|
|
*/
|
2009-05-19 15:21:12 -04:00
|
|
|
public final class EscherPropertyFactory {
|
2004-04-09 07:45:38 -04:00
|
|
|
/**
|
|
|
|
* Create new properties from a byte array.
|
|
|
|
*
|
|
|
|
* @param data The byte array containing the property
|
|
|
|
* @param offset The starting offset into the byte array
|
2016-06-22 18:26:08 -04:00
|
|
|
* @param numProperties The number of properties to be read
|
2004-04-09 07:45:38 -04:00
|
|
|
* @return The new properties
|
|
|
|
*/
|
2009-05-19 15:21:12 -04:00
|
|
|
public List<EscherProperty> createProperties(byte[] data, int offset, short numProperties) {
|
|
|
|
List<EscherProperty> results = new ArrayList<EscherProperty>();
|
2004-04-09 07:45:38 -04:00
|
|
|
|
|
|
|
int pos = offset;
|
2005-05-01 07:26:18 -04:00
|
|
|
|
2009-05-19 15:21:12 -04:00
|
|
|
for (int i = 0; i < numProperties; i++) {
|
2004-04-09 07:45:38 -04:00
|
|
|
short propId;
|
|
|
|
int propData;
|
|
|
|
propId = LittleEndian.getShort( data, pos );
|
|
|
|
propData = LittleEndian.getInt( data, pos + 2 );
|
|
|
|
short propNumber = (short) ( propId & (short) 0x3FFF );
|
|
|
|
boolean isComplex = ( propId & (short) 0x8000 ) != 0;
|
2016-06-22 18:26:08 -04:00
|
|
|
// boolean isBlipId = ( propId & (short) 0x4000 ) != 0;
|
2005-05-01 07:26:18 -04:00
|
|
|
|
2009-05-19 15:21:12 -04:00
|
|
|
byte propertyType = EscherProperties.getPropertyType(propNumber);
|
2016-12-04 18:49:49 -05:00
|
|
|
EscherProperty ep;
|
|
|
|
switch (propertyType) {
|
|
|
|
case EscherPropertyMetaData.TYPE_BOOLEAN:
|
|
|
|
ep = new EscherBoolProperty( propId, propData );
|
|
|
|
break;
|
|
|
|
case EscherPropertyMetaData.TYPE_RGB:
|
|
|
|
ep = new EscherRGBProperty( propId, propData );
|
|
|
|
break;
|
|
|
|
case EscherPropertyMetaData.TYPE_SHAPEPATH:
|
|
|
|
ep = new EscherShapePathProperty( propId, propData );
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if ( !isComplex ) {
|
|
|
|
ep = new EscherSimpleProperty( propId, propData );
|
|
|
|
} else if ( propertyType == EscherPropertyMetaData.TYPE_ARRAY) {
|
|
|
|
ep = new EscherArrayProperty( propId, new byte[propData]);
|
|
|
|
} else {
|
|
|
|
ep = new EscherComplexProperty( propId, new byte[propData]);
|
|
|
|
}
|
|
|
|
break;
|
2004-04-09 07:45:38 -04:00
|
|
|
}
|
2016-12-04 18:49:49 -05:00
|
|
|
results.add( ep );
|
2004-04-09 07:45:38 -04:00
|
|
|
pos += 6;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get complex data
|
2016-07-17 17:18:07 -04:00
|
|
|
for (EscherProperty p : results) {
|
2009-05-19 15:21:12 -04:00
|
|
|
if (p instanceof EscherComplexProperty) {
|
|
|
|
if (p instanceof EscherArrayProperty) {
|
2004-04-09 07:45:38 -04:00
|
|
|
pos += ((EscherArrayProperty)p).setArrayData(data, pos);
|
2009-05-19 15:21:12 -04:00
|
|
|
} else {
|
2004-04-09 07:45:38 -04:00
|
|
|
byte[] complexData = ((EscherComplexProperty)p).getComplexData();
|
2015-05-11 16:00:42 -04:00
|
|
|
|
2016-07-17 17:18:07 -04:00
|
|
|
int leftover = data.length - pos;
|
|
|
|
if (leftover < complexData.length) {
|
2017-03-19 16:30:04 -04:00
|
|
|
throw new IllegalStateException("Could not read complex escher property, length was " + complexData.length + ", but had only " +
|
2015-05-11 16:03:36 -04:00
|
|
|
leftover + " bytes left");
|
2015-05-11 16:00:42 -04:00
|
|
|
}
|
|
|
|
|
2015-05-11 16:03:36 -04:00
|
|
|
System.arraycopy(data, pos, complexData, 0, complexData.length);
|
2004-04-09 07:45:38 -04:00
|
|
|
pos += complexData.length;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
}
|