001: /* ====================================================================
002: Licensed to the Apache Software Foundation (ASF) under one or more
003: contributor license agreements. See the NOTICE file distributed with
004: this work for additional information regarding copyright ownership.
005: The ASF licenses this file to You under the Apache License, Version 2.0
006: (the "License"); you may not use this file except in compliance with
007: the License. You may obtain a copy of the License at
008:
009: http://www.apache.org/licenses/LICENSE-2.0
010:
011: Unless required by applicable law or agreed to in writing, software
012: distributed under the License is distributed on an "AS IS" BASIS,
013: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: See the License for the specific language governing permissions and
015: limitations under the License.
016: ==================================================================== */
017:
018: package org.apache.poi.ddf;
019:
020: import org.apache.poi.util.HexDump;
021: import org.apache.poi.util.LittleEndian;
022:
023: /**
024: * This record simply holds the number of shapes in the drawing group and the
025: * last shape id used for this drawing group.
026: *
027: * @author Glen Stampoultzis
028: */
029: public class EscherDgRecord extends EscherRecord {
030: public static final short RECORD_ID = (short) 0xF008;
031: public static final String RECORD_DESCRIPTION = "MsofbtDg";
032:
033: private int field_1_numShapes;
034: private int field_2_lastMSOSPID;
035:
036: /**
037: * This method deserializes the record from a byte array.
038: *
039: * @param data The byte array containing the escher record information
040: * @param offset The starting offset into <code>data</code>.
041: * @param recordFactory May be null since this is not a container record.
042: * @return The number of bytes read from the byte array.
043: */
044: public int fillFields(byte[] data, int offset,
045: EscherRecordFactory recordFactory) {
046: int bytesRemaining = readHeader(data, offset);
047: int pos = offset + 8;
048: int size = 0;
049: field_1_numShapes = LittleEndian.getInt(data, pos + size);
050: size += 4;
051: field_2_lastMSOSPID = LittleEndian.getInt(data, pos + size);
052: size += 4;
053: // bytesRemaining -= size;
054: // remainingData = new byte[bytesRemaining];
055: // System.arraycopy( data, pos + size, remainingData, 0, bytesRemaining );
056: return getRecordSize();
057: }
058:
059: /**
060: * This method serializes this escher record into a byte array.
061: *
062: * @param offset The offset into <code>data</code> to start writing the record data to.
063: * @param data The byte array to serialize to.
064: * @param listener A listener to retrieve start and end callbacks. Use a <code>NullEscherSerailizationListener</code> to ignore these events.
065: * @return The number of bytes written.
066: * @see NullEscherSerializationListener
067: */
068: public int serialize(int offset, byte[] data,
069: EscherSerializationListener listener) {
070: listener.beforeRecordSerialize(offset, getRecordId(), this );
071:
072: LittleEndian.putShort(data, offset, getOptions());
073: LittleEndian.putShort(data, offset + 2, getRecordId());
074: LittleEndian.putInt(data, offset + 4, 8);
075: LittleEndian.putInt(data, offset + 8, field_1_numShapes);
076: LittleEndian.putInt(data, offset + 12, field_2_lastMSOSPID);
077: // System.arraycopy( remainingData, 0, data, offset + 26, remainingData.length );
078: // int pos = offset + 8 + 18 + remainingData.length;
079:
080: listener.afterRecordSerialize(offset + 16, getRecordId(),
081: getRecordSize(), this );
082: return getRecordSize();
083: }
084:
085: /**
086: * Returns the number of bytes that are required to serialize this record.
087: *
088: * @return Number of bytes
089: */
090: public int getRecordSize() {
091: return 8 + 8;
092: }
093:
094: public short getRecordId() {
095: return RECORD_ID;
096: }
097:
098: /**
099: * The short name for this record
100: */
101: public String getRecordName() {
102: return "Dg";
103: }
104:
105: /**
106: * Returns the string representation of this record.
107: */
108: public String toString() {
109: String nl = System.getProperty("line.separator");
110:
111: // String extraData;
112: // ByteArrayOutputStream b = new ByteArrayOutputStream();
113: // try
114: // {
115: // HexDump.dump(this.remainingData, 0, b, 0);
116: // extraData = b.toString();
117: // }
118: // catch ( Exception e )
119: // {
120: // extraData = "error";
121: // }
122: return getClass().getName() + ":" + nl + " RecordId: 0x"
123: + HexDump.toHex(RECORD_ID) + nl + " Options: 0x"
124: + HexDump.toHex(getOptions()) + nl + " NumShapes: "
125: + field_1_numShapes + nl + " LastMSOSPID: "
126: + field_2_lastMSOSPID + nl;
127:
128: }
129:
130: /**
131: * The number of shapes in this drawing group.
132: */
133: public int getNumShapes() {
134: return field_1_numShapes;
135: }
136:
137: /**
138: * The number of shapes in this drawing group.
139: */
140: public void setNumShapes(int field_1_numShapes) {
141: this .field_1_numShapes = field_1_numShapes;
142: }
143:
144: /**
145: * The last shape id used in this drawing group.
146: */
147: public int getLastMSOSPID() {
148: return field_2_lastMSOSPID;
149: }
150:
151: /**
152: * The last shape id used in this drawing group.
153: */
154: public void setLastMSOSPID(int field_2_lastMSOSPID) {
155: this .field_2_lastMSOSPID = field_2_lastMSOSPID;
156: }
157:
158: /**
159: * Gets the drawing group id for this record. This is encoded in the
160: * instance part of the option record.
161: *
162: * @return a drawing group id.
163: */
164: public short getDrawingGroupId() {
165: return (short) (getOptions() >> 4);
166: }
167:
168: public void incrementShapeCount() {
169: this.field_1_numShapes++;
170: }
171: }
|