?? escheraggregate.java
字號:
public static final short ST_ACTIONBUTTONHOME = (short) 190; public static final short ST_ACTIONBUTTONHELP = (short) 191; public static final short ST_ACTIONBUTTONINFORMATION = (short) 192; public static final short ST_ACTIONBUTTONFORWARDNEXT = (short) 193; public static final short ST_ACTIONBUTTONBACKPREVIOUS = (short) 194; public static final short ST_ACTIONBUTTONEND = (short) 195; public static final short ST_ACTIONBUTTONBEGINNING = (short) 196; public static final short ST_ACTIONBUTTONRETURN = (short) 197; public static final short ST_ACTIONBUTTONDOCUMENT = (short) 198; public static final short ST_ACTIONBUTTONSOUND = (short) 199; public static final short ST_ACTIONBUTTONMOVIE = (short) 200; public static final short ST_HOSTCONTROL = (short) 201; public static final short ST_TEXTBOX = (short) 202; public static final short ST_NIL = (short) 0x0FFF; protected HSSFPatriarch patriarch; /** Maps shape container objects to their OBJ records */ private Map shapeToObj = new HashMap(); private DrawingManager2 drawingManager; private short drawingGroupId; public EscherAggregate( DrawingManager2 drawingManager ) { this.drawingManager = drawingManager; } /** * @return Returns the current sid. */ public short getSid() { return sid; } /** * Unused since this is an aggregate record. Use createAggregate(). * * @see #createAggregate */ protected void fillFields( byte[] data, short size, int offset ) { throw new IllegalStateException( "Should not reach here" ); } /** * Calculates the string representation of this record. This is * simply a dump of all the records. */ public String toString() { String nl = System.getProperty( "line.separtor" ); StringBuffer result = new StringBuffer(); result.append( '[' ).append( getRecordName() ).append( ']' + nl ); for ( Iterator iterator = getEscherRecords().iterator(); iterator.hasNext(); ) { EscherRecord escherRecord = (EscherRecord) iterator.next(); result.append( escherRecord.toString() ); } result.append( "[/" ).append( getRecordName() ).append( ']' + nl ); return result.toString(); } /** * Collapses the drawing records into an aggregate. */ public static EscherAggregate createAggregate( List records, int locFirstDrawingRecord, DrawingManager2 drawingManager ) { // Keep track of any shape records created so we can match them back to the object id's. // Textbox objects are also treated as shape objects. final List shapeRecords = new ArrayList(); EscherRecordFactory recordFactory = new DefaultEscherRecordFactory() { public EscherRecord createRecord( byte[] data, int offset ) { EscherRecord r = super.createRecord( data, offset ); if ( r.getRecordId() == EscherClientDataRecord.RECORD_ID || r.getRecordId() == EscherTextboxRecord.RECORD_ID ) { shapeRecords.add( r ); } return r; } }; // Calculate the size of the buffer EscherAggregate agg = new EscherAggregate(drawingManager); int loc = locFirstDrawingRecord; int dataSize = 0; while ( loc + 1 < records.size() && sid( records, loc ) == DrawingRecord.sid && isObjectRecord( records, loc + 1 ) ) { dataSize += ( (DrawingRecord) records.get( loc ) ).getData().length; loc += 2; } // Create one big buffer byte buffer[] = new byte[dataSize]; int offset = 0; loc = locFirstDrawingRecord; while ( loc + 1 < records.size() && sid( records, loc ) == DrawingRecord.sid && isObjectRecord( records, loc + 1 ) ) { DrawingRecord drawingRecord = (DrawingRecord) records.get( loc ); System.arraycopy( drawingRecord.getData(), 0, buffer, offset, drawingRecord.getData().length ); offset += drawingRecord.getData().length; loc += 2; } // Decode the shapes // agg.escherRecords = new ArrayList(); int pos = 0; while ( pos < dataSize ) { EscherRecord r = recordFactory.createRecord( buffer, pos ); int bytesRead = r.fillFields( buffer, pos, recordFactory ); agg.addEscherRecord( r ); pos += bytesRead; } // Associate the object records with the shapes loc = locFirstDrawingRecord; int shapeIndex = 0; agg.shapeToObj = new HashMap(); while ( loc + 1 < records.size() && sid( records, loc ) == DrawingRecord.sid && isObjectRecord( records, loc + 1 ) ) { Record objRecord = (Record) records.get( loc + 1 ); agg.shapeToObj.put( shapeRecords.get( shapeIndex++ ), objRecord ); loc += 2; } return agg; } /** * Serializes this aggregate to a byte array. Since this is an aggregate * record it will effectively serialize the aggregated records. * * @param offset The offset into the start of the array. * @param data The byte array to serialize to. * @return The number of bytes serialized. */ public int serialize( int offset, byte[] data ) { convertUserModelToRecords(); // Determine buffer size List records = getEscherRecords(); int size = getEscherRecordSize( records ); byte[] buffer = new byte[size]; // Serialize escher records into one big data structure and keep note of ending offsets. final List spEndingOffsets = new ArrayList(); final List shapes = new ArrayList(); int pos = 0; for ( Iterator iterator = records.iterator(); iterator.hasNext(); ) { EscherRecord e = (EscherRecord) iterator.next(); pos += e.serialize( pos, buffer, new EscherSerializationListener() { public void beforeRecordSerialize( int offset, short recordId, EscherRecord record ) { } public void afterRecordSerialize( int offset, short recordId, int size, EscherRecord record ) { if ( recordId == EscherClientDataRecord.RECORD_ID || recordId == EscherTextboxRecord.RECORD_ID ) { spEndingOffsets.add( new Integer( offset ) ); shapes.add( record ); } } } ); } // todo: fix this shapes.add( 0, null ); spEndingOffsets.add( 0, null ); // Split escher records into separate MSODRAWING and OBJ, TXO records. (We don't break on // the first one because it's the patriach). pos = offset; for ( int i = 1; i < shapes.size(); i++ ) { int endOffset = ( (Integer) spEndingOffsets.get( i ) ).intValue() - 1; int startOffset; if ( i == 1 ) startOffset = 0; else startOffset = ( (Integer) spEndingOffsets.get( i - 1 ) ).intValue(); // Create and write a new MSODRAWING record DrawingRecord drawing = new DrawingRecord(); byte[] drawingData = new byte[endOffset - startOffset + 1]; System.arraycopy( buffer, startOffset, drawingData, 0, drawingData.length ); drawing.setData( drawingData ); int temp = drawing.serialize( pos, data ); pos += temp; // Write the matching OBJ record Record obj = (Record) shapeToObj.get( shapes.get( i ) ); temp = obj.serialize( pos, data ); pos += temp; } int bytesWritten = pos - offset; if ( bytesWritten != getRecordSize() ) throw new RecordFormatException( bytesWritten + " bytes written but getRecordSize() reports " + getRecordSize() ); return bytesWritten; } /** * How many bytes do the raw escher records contain. * @param records List of escher records * @return the number of bytes */ private int getEscherRecordSize( List records ) { int size = 0; for ( Iterator iterator = records.iterator(); iterator.hasNext(); ) size += ( (EscherRecord) iterator.next() ).getRecordSize(); return size; } /** * The number of bytes required to serialize this record. */ public int getRecordSize() { convertUserModelToRecords(); List records = getEscherRecords(); int rawEscherSize = getEscherRecordSize( records );
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -