Remove unnecessary cast and reformat some code a bit

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1771247 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Dominik Stadler 2016-11-25 05:34:58 +00:00
parent f656e74c4b
commit f2975b1e5f
7 changed files with 376 additions and 461 deletions

View File

@ -32,7 +32,7 @@ public class WorkingWithRichText {
XSSFWorkbook wb = new XSSFWorkbook(); //or new HSSFWorkbook();
try {
XSSFSheet sheet = wb.createSheet();
XSSFRow row = sheet.createRow((short) 2);
XSSFRow row = sheet.createRow(2);
XSSFCell cell = row.createCell(1);
XSSFRichTextString rt = new XSSFRichTextString("The quick brown fox");

View File

@ -28,9 +28,7 @@ import org.apache.poi.util.Internal;
import org.apache.poi.util.LittleEndian;
@Internal
public final class ComplexFileTable
{
public final class ComplexFileTable {
private static final byte GRPPRL_TYPE = 1;
private static final byte TEXT_PIECE_TABLE_TYPE = 2;
@ -38,18 +36,15 @@ public final class ComplexFileTable
private SprmBuffer[] _grpprls;
public ComplexFileTable()
{
public ComplexFileTable() {
_tpt = new TextPieceTable();
}
public ComplexFileTable(byte[] documentStream, byte[] tableStream, int offset, int fcMin) throws IOException
{
public ComplexFileTable(byte[] documentStream, byte[] tableStream, int offset, int fcMin) throws IOException {
//skips through the prms before we reach the piece table. These contain data
//for actual fast saved files
List<SprmBuffer> sprmBuffers = new LinkedList<SprmBuffer>();
while ( tableStream[offset] == GRPPRL_TYPE )
{
while (tableStream[offset] == GRPPRL_TYPE) {
offset++;
int size = LittleEndian.getShort(tableStream, offset);
offset += LittleEndian.SHORT_SIZE;
@ -61,8 +56,7 @@ public final class ComplexFileTable
}
this._grpprls = sprmBuffers.toArray(new SprmBuffer[sprmBuffers.size()]);
if(tableStream[offset] != TEXT_PIECE_TABLE_TYPE)
{
if (tableStream[offset] != TEXT_PIECE_TABLE_TYPE) {
throw new IOException("The text piece table is corrupted");
}
int pieceTableSize = LittleEndian.getInt(tableStream, ++offset);
@ -70,19 +64,16 @@ public final class ComplexFileTable
_tpt = new TextPieceTable(documentStream, tableStream, offset, pieceTableSize, fcMin);
}
public TextPieceTable getTextPieceTable()
{
public TextPieceTable getTextPieceTable() {
return _tpt;
}
public SprmBuffer[] getGrpprls()
{
public SprmBuffer[] getGrpprls() {
return _grpprls;
}
@Deprecated
public void writeTo( HWPFFileSystem sys ) throws IOException
{
public void writeTo(HWPFFileSystem sys) throws IOException {
HWPFOutputStream docStream = sys.getStream("WordDocument");
HWPFOutputStream tableStream = sys.getStream("1Table");
@ -90,8 +81,7 @@ public final class ComplexFileTable
}
public void writeTo(HWPFOutputStream wordDocumentStream,
HWPFOutputStream tableStream ) throws IOException
{
HWPFOutputStream tableStream) throws IOException {
tableStream.write(TEXT_PIECE_TABLE_TYPE);
byte[] table = _tpt.writeTo(wordDocumentStream);

View File

@ -41,27 +41,23 @@ import org.apache.poi.util.LittleEndian;
* @author Ryan Ackley
*/
@Internal
public abstract class FormattedDiskPage
{
public abstract class FormattedDiskPage {
protected byte[] _fkp;
protected int _crun;
protected int _offset;
public FormattedDiskPage()
{
public FormattedDiskPage() {
}
/**
* Uses a 512-byte array to create a FKP
*/
public FormattedDiskPage(byte[] documentStream, int offset)
{
public FormattedDiskPage(byte[] documentStream, int offset) {
_crun = LittleEndian.getUByte(documentStream, offset + 511);
_fkp = documentStream;
_offset = offset;
}
/**
* Used to get a text offset corresponding to a grpprl in this fkp.
* @param index The index of the property in this FKP
@ -71,6 +67,7 @@ public abstract class FormattedDiskPage
{
return LittleEndian.getInt(_fkp, _offset + (index * 4));
}
/**
* Used to get the end of the text corresponding to a grpprl in this fkp.
* @param index The index of the property in this fkp.
@ -80,6 +77,7 @@ public abstract class FormattedDiskPage
{
return LittleEndian.getInt(_fkp, _offset + ((index + 1) * 4));
}
/**
* Used to get the total number of grrprl's stored int this FKP
* @return The number of grpprls in this FKP

View File

@ -25,23 +25,21 @@ import org.apache.poi.util.LittleEndian;
/**
* Plex of CPs stored in File (PLCF)
*
* <p>
* common data structure in a Word file. Contains an array of 4 byte ints in the
* front that relate to an array of arbitrary data structures in the back.
*
* <p>
* See page 184 of official documentation for details
*
* @author Ryan Ackley
*/
public final class PlexOfCps
{
public final class PlexOfCps {
private int _iMac;
private int _offset;
private int _cbStruct;
private List<GenericPropertyNode> _props;
public PlexOfCps( int sizeOfStruct )
{
public PlexOfCps(int sizeOfStruct) {
_props = new ArrayList<GenericPropertyNode>();
_cbStruct = sizeOfStruct;
}
@ -49,74 +47,56 @@ public final class PlexOfCps
/**
* Constructor
*
* @param cb
* The size of PLCF in bytes
* @param cbStruct
* The size of the data structure type stored in this PlexOfCps.
* @param cb The size of PLCF in bytes
* @param cbStruct The size of the data structure type stored in this PlexOfCps.
*/
public PlexOfCps( byte[] buf, int start, int cb, int cbStruct )
{
public PlexOfCps(byte[] buf, int start, int cb, int cbStruct) {
// Figure out the number we hold
_iMac = (cb - 4) / (4 + cbStruct);
_cbStruct = cbStruct;
_props = new ArrayList<GenericPropertyNode>(_iMac);
for ( int x = 0; x < _iMac; x++ )
{
for (int x = 0; x < _iMac; x++) {
_props.add(getProperty(x, buf, start));
}
}
@Internal
void adjust( int startCp, int shift )
{
for ( GenericPropertyNode node : _props )
{
if ( node.getStart() > startCp )
{
if ( node.getStart() + shift < startCp )
{
void adjust(int startCp, int shift) {
for (GenericPropertyNode node : _props) {
if (node.getStart() > startCp) {
if (node.getStart() + shift < startCp) {
node.setStart(startCp);
}
else
{
} else {
node.setStart(node.getStart() + shift);
}
}
if ( node.getEnd() >= startCp )
{
if ( node.getEnd() + shift < startCp )
{
if (node.getEnd() >= startCp) {
if (node.getEnd() + shift < startCp) {
node.setEnd(startCp);
}
else
{
} else {
node.setEnd(node.getEnd() + shift);
}
}
}
}
public GenericPropertyNode getProperty( int index )
{
public GenericPropertyNode getProperty(int index) {
return _props.get(index);
}
public void addProperty( GenericPropertyNode node )
{
public void addProperty(GenericPropertyNode node) {
_props.add(node);
_iMac++;
}
void remove( int index )
{
void remove(int index) {
_props.remove(index);
_iMac--;
}
public byte[] toByteArray()
{
public byte[] toByteArray() {
int size = _props.size();
int cpBufSize = ((size + 1) * LittleEndian.INT_SIZE);
int structBufSize = +(_cbStruct * size);
@ -125,8 +105,7 @@ public final class PlexOfCps
byte[] buf = new byte[bufSize];
GenericPropertyNode node = null;
for ( int x = 0; x < size; x++ )
{
for (int x = 0; x < size; x++) {
node = _props.get(x);
// put the starting offset of the property into the plcf.
@ -141,11 +120,9 @@ public final class PlexOfCps
LittleEndian.putInt(buf, LittleEndian.INT_SIZE * size, node.getEnd());
return buf;
}
private GenericPropertyNode getProperty( int index, byte[] buf, int offset )
{
private GenericPropertyNode getProperty(int index, byte[] buf, int offset) {
int start = LittleEndian.getInt(buf, offset + getIntOffset(index));
int end = LittleEndian.getInt(buf, offset + getIntOffset(index + 1));
@ -156,8 +133,7 @@ public final class PlexOfCps
return new GenericPropertyNode(start, end, struct);
}
private int getIntOffset( int index )
{
private int getIntOffset(int index) {
return index * 4;
}
@ -166,8 +142,7 @@ public final class PlexOfCps
*
* @return The number of data structures in this PlexofCps
*/
public int length()
{
public int length() {
return _iMac;
}
@ -175,19 +150,15 @@ public final class PlexOfCps
* Returns the offset, in bytes, from the beginning if this PlexOfCps to the
* data structure at index.
*
* @param index
* The index of the data structure.
*
* @param index The index of the data structure.
* @return The offset, in bytes, from the beginning if this PlexOfCps to the
* data structure at index.
*/
private int getStructOffset( int index )
{
private int getStructOffset(int index) {
return (4 * (_iMac + 1)) + (_cbStruct * index);
}
GenericPropertyNode[] toPropertiesArray()
{
GenericPropertyNode[] toPropertiesArray() {
if (_props == null || _props.isEmpty())
return new GenericPropertyNode[0];
@ -195,8 +166,7 @@ public final class PlexOfCps
}
@Override
public String toString()
{
public String toString() {
return "PLCF (cbStruct: " + _cbStruct + "; iMac: " + _iMac + ")";
}
}

View File

@ -34,16 +34,13 @@ import org.apache.poi.util.POILogger;
* @author Ryan Ackley
*/
@Internal
public abstract class PropertyNode<T extends PropertyNode<T>> implements Comparable<T>, Cloneable
{
public abstract class PropertyNode<T extends PropertyNode<T>> implements Comparable<T>, Cloneable {
public static final class EndComparator implements
Comparator<PropertyNode<?>>
{
Comparator<PropertyNode<?>> {
public static final EndComparator instance = new EndComparator();
public int compare( PropertyNode<?> o1, PropertyNode<?> o2 )
{
public int compare(PropertyNode<?> o1, PropertyNode<?> o2) {
int thisVal = o1.getEnd();
int anotherVal = o2.getEnd();
return (thisVal < anotherVal ? -1 : (thisVal == anotherVal ? 0
@ -52,12 +49,10 @@ public abstract class PropertyNode<T extends PropertyNode<T>> implements Compar
}
public static final class StartComparator implements
Comparator<PropertyNode<?>>
{
Comparator<PropertyNode<?>> {
public static final StartComparator instance = new StartComparator();
public int compare( PropertyNode<?> o1, PropertyNode<?> o2 )
{
public int compare(PropertyNode<?> o1, PropertyNode<?> o2) {
int thisVal = o1.getStart();
int anotherVal = o2.getStart();
return (thisVal < anotherVal ? -1 : (thisVal == anotherVal ? 0
@ -67,9 +62,13 @@ public abstract class PropertyNode<T extends PropertyNode<T>> implements Compar
private final static POILogger _logger = POILogFactory.getLogger(PropertyNode.class);
protected Object _buf;
/** The start, in characters */
/**
* The start, in characters
*/
private int _cpStart;
/** The end, in characters */
/**
* The end, in characters
*/
private int _cpEnd;
@ -78,8 +77,7 @@ public abstract class PropertyNode<T extends PropertyNode<T>> implements Compar
* @param fcEnd The end of the text for this property, in characters.
* @param buf FIXME: Old documentation is: "grpprl The property description in compressed form."
*/
protected PropertyNode(int fcStart, int fcEnd, Object buf)
{
protected PropertyNode(int fcStart, int fcEnd, Object buf) {
_cpStart = fcStart;
_cpEnd = fcEnd;
_buf = buf;
@ -89,8 +87,7 @@ public abstract class PropertyNode<T extends PropertyNode<T>> implements Compar
_cpStart = 0;
}
if ( _cpEnd < _cpStart )
{
if (_cpEnd < _cpStart) {
_logger.log(POILogger.WARN, "A property claimed to end (" + _cpEnd
+ ") before start! "
+ "Resetting end to start, and hoping for the best");
@ -101,36 +98,32 @@ public abstract class PropertyNode<T extends PropertyNode<T>> implements Compar
/**
* @return The start offset of this property's text.
*/
public int getStart()
{
public int getStart() {
return _cpStart;
}
public void setStart(int start)
{
public void setStart(int start) {
_cpStart = start;
}
/**
* @return The offset of the end of this property's text.
*/
public int getEnd()
{
public int getEnd() {
return _cpEnd;
}
public void setEnd(int end)
{
public void setEnd(int end) {
_cpEnd = end;
}
/**
* Adjust for a deletion that can span multiple PropertyNodes.
*
* @param start
* @param length
*/
public void adjustForDelete(int start, int length)
{
public void adjustForDelete(int start, int length) {
int end = start + length;
if (_cpEnd > start) {
@ -148,28 +141,23 @@ public abstract class PropertyNode<T extends PropertyNode<T>> implements Compar
}
}
protected boolean limitsAreEqual(Object o)
{
protected boolean limitsAreEqual(Object o) {
return ((PropertyNode<?>) o).getStart() == _cpStart &&
((PropertyNode<?>) o).getEnd() == _cpEnd;
}
@Override
public int hashCode()
{
public int hashCode() {
return this._cpStart * 31 + this._buf.hashCode();
}
public boolean equals(Object o)
{
public boolean equals(Object o) {
if (!(o instanceof PropertyNode)) return false;
if (limitsAreEqual(o))
{
if (limitsAreEqual(o)) {
Object testBuf = ((PropertyNode<?>) o)._buf;
if (testBuf instanceof byte[] && _buf instanceof byte[])
{
if (testBuf instanceof byte[] && _buf instanceof byte[]) {
return Arrays.equals((byte[]) testBuf, (byte[]) _buf);
}
return _buf.equals(testBuf);
@ -178,27 +166,20 @@ public abstract class PropertyNode<T extends PropertyNode<T>> implements Compar
}
@SuppressWarnings("unchecked")
public T clone() throws CloneNotSupportedException
{
public T clone() throws CloneNotSupportedException {
return (T) super.clone();
}
/**
* Used for sorting in collections.
*/
public int compareTo(T o)
{
public int compareTo(T o) {
int cpEnd = o.getEnd();
if(_cpEnd == cpEnd)
{
if (_cpEnd == cpEnd) {
return 0;
}
else if(_cpEnd < cpEnd)
{
} else if (_cpEnd < cpEnd) {
return -1;
}
else
{
} else {
return 1;
}
}

View File

@ -31,38 +31,29 @@ import org.apache.poi.util.Internal;
* @author Ryan Ackley
*/
@Internal
public class TextPiece extends PropertyNode<TextPiece>
{
public class TextPiece extends PropertyNode<TextPiece> {
private boolean _usesUnicode;
private PieceDescriptor _pd;
/**
* @param start
* Beginning offset in main document stream, in characters.
* @param end
* Ending offset in main document stream, in characters.
* @param text
* The raw bytes of our text
* @param start Beginning offset in main document stream, in characters.
* @param end Ending offset in main document stream, in characters.
* @param text The raw bytes of our text
* @deprecated Use {@link #TextPiece(int, int, byte[], PieceDescriptor)}
* instead
*/
public TextPiece(int start, int end, byte[] text, PieceDescriptor pd,
int cpStart )
{
int cpStart) {
this(start, end, text, pd);
}
/**
* @param start
* Beginning offset in main document stream, in characters.
* @param end
* Ending offset in main document stream, in characters.
* @param text
* The raw bytes of our text
* @param start Beginning offset in main document stream, in characters.
* @param end Ending offset in main document stream, in characters.
* @param text The raw bytes of our text
*/
public TextPiece( int start, int end, byte[] text, PieceDescriptor pd )
{
public TextPiece(int start, int end, byte[] text, PieceDescriptor pd) {
super(start, end, buildInitSB(text, pd));
_usesUnicode = pd.isUnicode();
_pd = pd;
@ -89,29 +80,24 @@ public class TextPiece extends PropertyNode<TextPiece>
/**
* @return If this text piece is unicode
*/
public boolean isUnicode()
{
public boolean isUnicode() {
return _usesUnicode;
}
public PieceDescriptor getPieceDescriptor()
{
public PieceDescriptor getPieceDescriptor() {
return _pd;
}
@Deprecated
public StringBuffer getStringBuffer()
{
public StringBuffer getStringBuffer() {
return new StringBuffer(getStringBuilder());
}
public StringBuilder getStringBuilder()
{
public StringBuilder getStringBuilder() {
return (StringBuilder) _buf;
}
public byte[] getRawBytes()
{
public byte[] getRawBytes() {
return ((CharSequence) _buf).toString().getBytes(
Charset.forName(_usesUnicode ? "UTF-16LE" : "Cp1252")
);
@ -120,12 +106,12 @@ public class TextPiece extends PropertyNode<TextPiece>
/**
* Returns part of the string.
* Works only in characters, not in bytes!
*
* @param start Local start position, in characters
* @param end Local end position, in characters
*/
@Deprecated
public String substring(int start, int end)
{
public String substring(int start, int end) {
StringBuilder buf = (StringBuilder) _buf;
// Validate
@ -144,16 +130,15 @@ public class TextPiece extends PropertyNode<TextPiece>
/**
* Adjusts the internal string for deletinging
* some characters within this.
*
* @param start The start position for the delete, in characters
* @param length The number of characters to delete
*/
@Deprecated
public void adjustForDelete(int start, int length) {
int numChars = length;
int myStart = getStart();
int myEnd = getEnd();
int end = start + numChars;
int end = start + length;
/* do we have to delete from this text piece? */
if (start <= myEnd && end >= myStart) {
@ -179,10 +164,10 @@ public class TextPiece extends PropertyNode<TextPiece>
* Returns the length, in characters
*/
@Deprecated
public int characterLength()
{
public int characterLength() {
return (getEnd() - getStart());
}
/**
* Returns the length, in bytes
*/
@ -191,8 +176,7 @@ public class TextPiece extends PropertyNode<TextPiece>
}
@Override
public boolean equals(Object o)
{
public boolean equals(Object o) {
if (!(o instanceof TextPiece)) return false;
TextPiece tp = (TextPiece) o;
assert (_buf != null && tp._buf != null && _pd != null && tp._pd != null);
@ -215,13 +199,11 @@ public class TextPiece extends PropertyNode<TextPiece>
/**
* Returns the character position we start at.
*/
public int getCP()
{
public int getCP() {
return getStart();
}
public String toString()
{
public String toString() {
return "TextPiece from " + getStart() + " to " + getEnd() + " ("
+ getPieceDescriptor() + ")";
}

View File

@ -22,35 +22,29 @@ import java.io.ByteArrayOutputStream;
import org.apache.poi.util.Internal;
@Internal
public final class HWPFOutputStream extends ByteArrayOutputStream
{
public final class HWPFOutputStream extends ByteArrayOutputStream {
int _offset;
public HWPFOutputStream()
{
public HWPFOutputStream() {
super();
}
public int getOffset()
{
public int getOffset() {
return _offset;
}
public synchronized void reset()
{
public synchronized void reset() {
super.reset();
_offset = 0;
}
public synchronized void write(byte[] buf, int off, int len)
{
public synchronized void write(byte[] buf, int off, int len) {
super.write(buf, off, len);
_offset += len;
}
public synchronized void write(int b)
{
public synchronized void write(int b) {
super.write(b);
_offset++;
}