SonarCube fix - make members private

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1772585 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Andreas Beeker 2016-12-04 23:49:49 +00:00
parent f5d9984c49
commit 75dc015766
17 changed files with 147 additions and 128 deletions

View File

@ -31,7 +31,7 @@ import org.apache.poi.util.LittleEndian;
*/
public abstract class AbstractEscherOptRecord extends EscherRecord
{
protected List<EscherProperty> properties = new ArrayList<EscherProperty>();
private List<EscherProperty> properties = new ArrayList<EscherProperty>();
/**
* Add a property to this record.

View File

@ -63,59 +63,47 @@ public final class EscherArrayProperty extends EscherComplexProperty implements
}
public int getNumberOfElementsInArray() {
return (emptyComplexPart) ? 0 : LittleEndian.getUShort(_complexData, 0);
return (emptyComplexPart) ? 0 : LittleEndian.getUShort(getComplexData(), 0);
}
public void setNumberOfElementsInArray(int numberOfElements) {
int expectedArraySize = numberOfElements * getActualSizeOfElements(getSizeOfElements()) + FIXED_SIZE;
if (expectedArraySize != _complexData.length) {
byte[] newArray = new byte[expectedArraySize];
System.arraycopy(_complexData, 0, newArray, 0, _complexData.length);
_complexData = newArray;
}
LittleEndian.putShort(_complexData, 0, (short) numberOfElements);
int expectedArraySize = getArraySizeInBytes(numberOfElements, getSizeOfElements());
resizeComplexData(expectedArraySize, getComplexData().length);
LittleEndian.putShort(getComplexData(), 0, (short)numberOfElements);
}
public int getNumberOfElementsInMemory() {
return (emptyComplexPart) ? 0 : LittleEndian.getUShort(_complexData, 2);
return (emptyComplexPart) ? 0 : LittleEndian.getUShort(getComplexData(), 2);
}
public void setNumberOfElementsInMemory(int numberOfElements) {
int expectedArraySize = numberOfElements * getActualSizeOfElements(getSizeOfElements()) + FIXED_SIZE;
if (expectedArraySize != _complexData.length) {
byte[] newArray = new byte[expectedArraySize];
System.arraycopy(_complexData, 0, newArray, 0, expectedArraySize);
_complexData = newArray;
}
LittleEndian.putShort(_complexData, 2, (short) numberOfElements);
int expectedArraySize = getArraySizeInBytes(numberOfElements, getSizeOfElements());
resizeComplexData(expectedArraySize, expectedArraySize);
LittleEndian.putShort(getComplexData(), 2, (short) numberOfElements);
}
public short getSizeOfElements() {
return (emptyComplexPart) ? 0 : LittleEndian.getShort( _complexData, 4 );
return (emptyComplexPart) ? 0 : LittleEndian.getShort( getComplexData(), 4 );
}
public void setSizeOfElements(int sizeOfElements) {
LittleEndian.putShort( _complexData, 4, (short) sizeOfElements );
LittleEndian.putShort( getComplexData(), 4, (short) sizeOfElements );
int expectedArraySize = getNumberOfElementsInArray() * getActualSizeOfElements(getSizeOfElements()) + FIXED_SIZE;
if (expectedArraySize != _complexData.length) {
int expectedArraySize = getArraySizeInBytes(getNumberOfElementsInArray(), sizeOfElements);
// Keep just the first 6 bytes. The rest is no good to us anyway.
byte[] newArray = new byte[expectedArraySize];
System.arraycopy( _complexData, 0, newArray, 0, 6 );
_complexData = newArray;
}
resizeComplexData(expectedArraySize, 6);
}
public byte[] getElement(int index) {
int actualSize = getActualSizeOfElements(getSizeOfElements());
byte[] result = new byte[actualSize];
System.arraycopy(_complexData, FIXED_SIZE + index * actualSize, result, 0, result.length );
System.arraycopy(getComplexData(), FIXED_SIZE + index * actualSize, result, 0, result.length );
return result;
}
public void setElement(int index, byte[] element) {
int actualSize = getActualSizeOfElements(getSizeOfElements());
System.arraycopy( element, 0, _complexData, FIXED_SIZE + index * actualSize, actualSize);
System.arraycopy( element, 0, getComplexData(), FIXED_SIZE + index * actualSize, actualSize);
}
@Override
@ -152,7 +140,7 @@ public final class EscherArrayProperty extends EscherComplexProperty implements
/**
* We have this method because the way in which arrays in escher works
* is screwed for seemly arbitary reasons. While most properties are
* is screwed for seemly arbitrary reasons. While most properties are
* fairly consistent and have a predictable array size, escher arrays
* have special cases.
*
@ -162,21 +150,25 @@ public final class EscherArrayProperty extends EscherComplexProperty implements
*/
public int setArrayData(byte[] data, int offset) {
if (emptyComplexPart){
_complexData = new byte[0];
} else {
resizeComplexData(0, 0);
return 0;
}
short numElements = LittleEndian.getShort(data, offset);
// LittleEndian.getShort(data, offset + 2); // numReserved
short sizeOfElements = LittleEndian.getShort(data, offset + 4);
int arraySize = getActualSizeOfElements(sizeOfElements) * numElements;
if (arraySize == _complexData.length) {
// TODO: this part is strange - it doesn't make sense to compare
// the size of the existing data when setting a new data array ...
int arraySize = getArraySizeInBytes(numElements, sizeOfElements);
if (arraySize - FIXED_SIZE == getComplexData().length) {
// The stored data size in the simple block excludes the header size
_complexData = new byte[arraySize + 6];
sizeIncludesHeaderSize = false;
}
System.arraycopy(data, offset, _complexData, 0, _complexData.length );
}
return _complexData.length;
int cpySize = Math.min(arraySize, data.length-offset);
resizeComplexData(cpySize, 0);
System.arraycopy(data, offset, getComplexData(), 0, cpySize);
return cpySize;
}
/**
@ -188,7 +180,7 @@ public final class EscherArrayProperty extends EscherComplexProperty implements
@Override
public int serializeSimplePart(byte[] data, int pos) {
LittleEndian.putShort(data, pos, getId());
int recordSize = _complexData.length;
int recordSize = getComplexData().length;
if(!sizeIncludesHeaderSize) {
recordSize -= 6;
}
@ -207,10 +199,15 @@ public final class EscherArrayProperty extends EscherComplexProperty implements
return sizeOfElements;
}
private static int getArraySizeInBytes(int numberOfElements, int sizeOfElements) {
return numberOfElements * getActualSizeOfElements((short)(sizeOfElements & 0xFFFF)) + FIXED_SIZE;
}
@Override
public Iterator<byte[]> iterator() {
return new Iterator<byte[]>(){
int idx = 0;
private int idx = 0;
@Override
public boolean hasNext() {
return (idx < getNumberOfElementsInArray());

View File

@ -38,8 +38,7 @@ public class EscherBitmapBlip extends EscherBlipRecord {
System.arraycopy( data, pos, field_1_UID, 0, 16 ); pos += 16;
field_2_marker = data[pos]; pos++;
field_pictureData = new byte[bytesAfterHeader - 17];
System.arraycopy( data, pos, field_pictureData, 0, field_pictureData.length );
setPictureData(data, pos, bytesAfterHeader - 17);
return bytesAfterHeader + HEADER_SIZE;
}
@ -55,15 +54,16 @@ public class EscherBitmapBlip extends EscherBlipRecord {
System.arraycopy( field_1_UID, 0, data, pos, 16 );
data[pos + 16] = field_2_marker;
System.arraycopy( field_pictureData, 0, data, pos + 17, field_pictureData.length );
byte pd[] = getPicturedata();
System.arraycopy( pd, 0, data, pos + 17, pd.length );
listener.afterRecordSerialize(offset + getRecordSize(), getRecordId(), getRecordSize(), this);
return HEADER_SIZE + 16 + 1 + field_pictureData.length;
return HEADER_SIZE + 16 + 1 + pd.length;
}
@Override
public int getRecordSize() {
return 8 + 16 + 1 + field_pictureData.length;
return 8 + 16 + 1 + getPicturedata().length;
}
/**
@ -113,7 +113,7 @@ public class EscherBitmapBlip extends EscherBlipRecord {
public String toString() {
String nl = System.getProperty( "line.separator" );
String extraData = HexDump.dump(this.field_pictureData, 0, 0);
String extraData = HexDump.dump(getPicturedata(), 0, 0);
return getClass().getName() + ":" + nl +
" RecordId: 0x" + HexDump.toHex( getRecordId() ) + nl +
@ -126,7 +126,7 @@ public class EscherBitmapBlip extends EscherBlipRecord {
@Override
public String toXml(String tab) {
String extraData = HexDump.dump(this.field_pictureData, 0, 0);
String extraData = HexDump.dump(getPicturedata(), 0, 0);
StringBuilder builder = new StringBuilder();
builder.append(tab).append(formatXmlRecordHeader(getClass().getSimpleName(), HexDump.toHex(getRecordId()), HexDump.toHex(getVersion()), HexDump.toHex(getInstance())))
.append(tab).append("\t").append("<UID>0x").append(HexDump.toHex(field_1_UID)).append("</UID>\n")

View File

@ -27,7 +27,7 @@ public class EscherBlipRecord extends EscherRecord {
private static final int HEADER_SIZE = 8;
protected byte[] field_pictureData;
private byte[] field_pictureData;
public EscherBlipRecord() {
}
@ -81,10 +81,22 @@ public class EscherBlipRecord extends EscherRecord {
* @param pictureData the picture data
*/
public void setPictureData(byte[] pictureData) {
if (pictureData == null) {
setPictureData(pictureData, 0, (pictureData == null ? 0 : pictureData.length));
}
/**
* Sets the picture data bytes
*
* @param pictureData the picture data
* @param offset the offset into the picture data
* @param length the amount of bytes to be used
*/
public void setPictureData(byte[] pictureData, int offset, int length) {
if (pictureData == null || offset < 0 || length < 0 || pictureData.length < offset+length) {
throw new IllegalArgumentException("picture data can't be null");
}
field_pictureData = pictureData.clone();
field_pictureData = new byte[length];
System.arraycopy(pictureData, offset, field_pictureData, 0, length);
}
@Override

View File

@ -49,7 +49,7 @@ public class EscherBoolProperty
*/
public boolean isTrue()
{
return propertyValue != 0;
return getPropertyValue() != 0;
}
/**
@ -61,7 +61,7 @@ public class EscherBoolProperty
*/
public boolean isFalse()
{
return propertyValue == 0;
return !isTrue();
}
// public String toString()

View File

@ -24,8 +24,9 @@ import org.apache.poi.util.LittleEndian;
* An OfficeArtCOLORREF structure entry which also handles color extension opid data
*/
public class EscherColorRef {
int opid = -1;
int colorRef = 0;
@SuppressWarnings("unused")
private int opid = -1;
private int colorRef = 0;
public enum SysIndexSource {
/** Use the fill color of the shape. */
@ -45,7 +46,7 @@ public class EscherColorRef {
/** If the shape contains a fill, use the fill color of the shape. Otherwise, use the line color. */
FILL_OR_LINE_COLOR(0xF7)
;
int value;
private int value;
SysIndexSource(int value) { this.value = value; }
}
@ -102,7 +103,7 @@ public class EscherColorRef {
*/
INVERT_HIGHBIT_AFTER(0x40)
;
BitField mask;
private BitField mask;
SysIndexProcedure(int mask) {
this.mask = new BitField(mask);
}

View File

@ -29,7 +29,7 @@ import org.apache.poi.util.LittleEndian;
*/
public class EscherComplexProperty extends EscherProperty {
// TODO - make private and final
protected byte[] _complexData;
private byte[] _complexData;
/**
* Create a complex property using the property id and a byte array containing the complex
@ -95,6 +95,16 @@ public class EscherComplexProperty extends EscherProperty {
return _complexData;
}
protected void resizeComplexData(int newSize, int bytesToKeep) {
if (newSize == _complexData.length) {
return;
}
byte[] newArray = new byte[newSize];
System.arraycopy(_complexData, 0, newArray, 0, Math.min(bytesToKeep, newSize));
_complexData = newArray;
}
/**
* Determine whether this property is equal to another property.
*

View File

@ -87,9 +87,9 @@ public final class EscherMetafileBlip extends EscherBlipRecord {
// 0 means DEFLATE compression
// 0xFE means no compression
if (field_6_fCompression == 0) {
field_pictureData = inflatePictureData(raw_pictureData);
super.setPictureData(inflatePictureData(raw_pictureData));
} else {
field_pictureData = raw_pictureData;
super.setPictureData(raw_pictureData);
}
int remaining = bytesAfterHeader - pos + offset + HEADER_SIZE;

View File

@ -32,7 +32,7 @@ public class EscherOptRecord extends AbstractEscherOptRecord
@Override
public short getInstance()
{
setInstance( (short) properties.size() );
setInstance( (short) getEscherProperties().size() );
return super.getInstance();
}

View File

@ -76,11 +76,11 @@ public final class EscherPictBlip extends EscherBlipRecord {
// 0xFE means no compression
if (field_6_fCompression == 0)
{
field_pictureData = inflatePictureData(raw_pictureData);
super.setPictureData(inflatePictureData(raw_pictureData));
}
else
{
field_pictureData = raw_pictureData;
super.setPictureData(raw_pictureData);
}
return bytesAfterHeader + HEADER_SIZE;
@ -264,7 +264,7 @@ public final class EscherPictBlip extends EscherBlipRecord {
@Override
public String toString() {
String extraData = HexDump.toHex(field_pictureData, 32);
String extraData = HexDump.toHex(getPicturedata(), 32);
return getClass().getName() + ":" + '\n' +
" RecordId: 0x" + HexDump.toHex( getRecordId() ) + '\n' +
" Version: 0x" + HexDump.toHex( getVersion() ) + '\n' +

View File

@ -49,24 +49,28 @@ public final class EscherPropertyFactory {
// boolean isBlipId = ( propId & (short) 0x4000 ) != 0;
byte propertyType = EscherProperties.getPropertyType(propNumber);
if ( propertyType == EscherPropertyMetaData.TYPE_BOOLEAN )
results.add( new EscherBoolProperty( propId, propData ) );
else if ( propertyType == EscherPropertyMetaData.TYPE_RGB )
results.add( new EscherRGBProperty( propId, propData ) );
else if ( propertyType == EscherPropertyMetaData.TYPE_SHAPEPATH )
results.add( new EscherShapePathProperty( propId, propData ) );
else
{
if ( !isComplex )
results.add( new EscherSimpleProperty( propId, propData ) );
else
{
if ( propertyType == EscherPropertyMetaData.TYPE_ARRAY)
results.add( new EscherArrayProperty( propId, new byte[propData]) );
else
results.add( new EscherComplexProperty( propId, new byte[propData]) );
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;
}
results.add( ep );
pos += 6;
}

View File

@ -36,7 +36,7 @@ public class EscherRGBProperty
*/
public int getRgbColor()
{
return propertyValue;
return getPropertyValue();
}
/**
@ -44,7 +44,7 @@ public class EscherRGBProperty
*/
public byte getRed()
{
return (byte) ( propertyValue & 0xFF );
return (byte) ( getRgbColor() & 0xFF );
}
/**
@ -52,7 +52,7 @@ public class EscherRGBProperty
*/
public byte getGreen()
{
return (byte) ( (propertyValue >> 8) & 0xFF );
return (byte) ( (getRgbColor() >> 8) & 0xFF );
}
/**
@ -60,7 +60,7 @@ public class EscherRGBProperty
*/
public byte getBlue()
{
return (byte) ( (propertyValue >> 16) & 0xFF );
return (byte) ( (getRgbColor() >> 16) & 0xFF );
}
@Override
@ -68,7 +68,7 @@ public class EscherRGBProperty
StringBuilder builder = new StringBuilder();
builder.append(tab).append("<").append(getClass().getSimpleName()).append(" id=\"0x").append(HexDump.toHex(getId()))
.append("\" name=\"").append(getName()).append("\" blipId=\"")
.append(isBlipId()).append("\" value=\"0x").append(HexDump.toHex(propertyValue)).append("\"/>\n");
.append(isBlipId()).append("\" value=\"0x").append(HexDump.toHex(getRgbColor())).append("\"/>\n");
return builder.toString();
}
}

View File

@ -27,7 +27,7 @@ import org.apache.poi.util.HexDump;
*/
public class EscherSimpleProperty extends EscherProperty
{
protected int propertyValue;
private int propertyValue;
/**
* The id is distinct from the actual property number. The id includes the property number the blip id

View File

@ -24,17 +24,17 @@ public abstract class POIXMLRelation {
/**
* Describes the content stored in a part.
*/
protected String _type;
private String _type;
/**
* The kind of connection between a source part and a target part in a package.
*/
protected String _relation;
private String _relation;
/**
* The path component of a pack URI.
*/
protected String _defaultName;
private String _defaultName;
/**
* Defines what object is used to construct instances of this relationship

View File

@ -368,7 +368,7 @@ public final class XSSFRelation extends POIXMLRelation {
*/
public InputStream getContents(PackagePart corePart) throws IOException, InvalidFormatException {
PackageRelationshipCollection prc =
corePart.getRelationshipsByType(_relation);
corePart.getRelationshipsByType(getRelation());
Iterator<PackageRelationship> it = prc.iterator();
if(it.hasNext()) {
PackageRelationship rel = it.next();
@ -376,7 +376,7 @@ public final class XSSFRelation extends POIXMLRelation {
PackagePart part = corePart.getPackage().getPart(relName);
return part.getInputStream();
}
log.log(POILogger.WARN, "No part " + _defaultName + " found");
log.log(POILogger.WARN, "No part " + getDefaultFileName() + " found");
return null;
}

View File

@ -17,29 +17,26 @@
package org.apache.poi.ddf;
import junit.framework.Test;
import junit.framework.TestSuite;
/**
* Tests for org.apache.poi.ddf<br/>
*/
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
@RunWith(Suite.class)
@Suite.SuiteClasses({
TestEscherBSERecord.class,
TestEscherBlipRecord.class,
TestEscherBoolProperty.class,
TestEscherChildAnchorRecord.class,
TestEscherClientAnchorRecord.class,
TestEscherClientDataRecord.class,
TestEscherContainerRecord.class,
TestEscherDgRecord.class,
TestEscherDggRecord.class,
TestEscherOptRecord.class,
TestEscherPropertyFactory.class,
TestEscherSpRecord.class,
TestEscherSpgrRecord.class,
TestEscherSplitMenuColorsRecord.class,
TestUnknownEscherRecord.class
})
public final class AllPOIDDFTests {
public static Test suite() {
TestSuite result = new TestSuite("Tests for org.apache.poi.ddf");
result.addTestSuite(TestEscherBSERecord.class);
result.addTestSuite(TestEscherBlipRecord.class);
result.addTestSuite(TestEscherBoolProperty.class);
result.addTestSuite(TestEscherChildAnchorRecord.class);
result.addTestSuite(TestEscherClientAnchorRecord.class);
result.addTestSuite(TestEscherClientDataRecord.class);
result.addTestSuite(TestEscherContainerRecord.class);
result.addTestSuite(TestEscherDgRecord.class);
result.addTestSuite(TestEscherDggRecord.class);
result.addTestSuite(TestEscherOptRecord.class);
result.addTestSuite(TestEscherPropertyFactory.class);
result.addTestSuite(TestEscherSpRecord.class);
result.addTestSuite(TestEscherSpgrRecord.class);
result.addTestSuite(TestEscherSplitMenuColorsRecord.class);
result.addTestSuite(TestUnknownEscherRecord.class);
return result;
}
}

View File

@ -17,18 +17,16 @@
package org.apache.poi.ddf;
import junit.framework.TestCase;
import static org.junit.Assert.assertEquals;
import java.util.List;
import org.apache.poi.util.HexRead;
import org.apache.poi.util.HexDump;
import org.apache.poi.util.HexRead;
import org.junit.Test;
/**
* @author Glen Stampoultzis (glens @ superlinksoftware.com)
*/
public class TestEscherPropertyFactory extends TestCase
{
public class TestEscherPropertyFactory {
@Test
public void testCreateProperties() {
String dataStr = "41 C1 " + // propid, complex ind
"03 00 00 00 " + // size of complex property
@ -41,7 +39,7 @@ public class TestEscherPropertyFactory extends TestCase
;
byte[] data = HexRead.readFromString( dataStr );
EscherPropertyFactory f = new EscherPropertyFactory();
List props = f.createProperties( data, 0, (short)3 );
List<EscherProperty> props = f.createProperties( data, 0, (short)3 );
EscherComplexProperty p1 = (EscherComplexProperty) props.get( 0 );
assertEquals( (short)0xC141, p1.getId() );
assertEquals( "[01, 02, 03]", HexDump.toHex( p1.getComplexData() ) );