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: import org.apache.poi.hssf.record.RecordFormatException;
023:
024: /**
025: * The spgr record defines information about a shape group. Groups in escher
026: * are simply another form of shape that you can't physically see.
027: *
028: * @author Glen Stampoultzis (glens at apache.org)
029: */
030: public class EscherSpgrRecord extends EscherRecord {
031: public static final short RECORD_ID = (short) 0xF009;
032: public static final String RECORD_DESCRIPTION = "MsofbtSpgr";
033:
034: private int field_1_rectX1;
035: private int field_2_rectY1;
036: private int field_3_rectX2;
037: private int field_4_rectY2;
038:
039: /**
040: * This method deserializes the record from a byte array.
041: *
042: * @param data The byte array containing the escher record information
043: * @param offset The starting offset into <code>data</code>.
044: * @param recordFactory May be null since this is not a container record.
045: * @return The number of bytes read from the byte array.
046: */
047: public int fillFields(byte[] data, int offset,
048: EscherRecordFactory recordFactory) {
049: int bytesRemaining = readHeader(data, offset);
050: int pos = offset + 8;
051: int size = 0;
052: field_1_rectX1 = LittleEndian.getInt(data, pos + size);
053: size += 4;
054: field_2_rectY1 = LittleEndian.getInt(data, pos + size);
055: size += 4;
056: field_3_rectX2 = LittleEndian.getInt(data, pos + size);
057: size += 4;
058: field_4_rectY2 = LittleEndian.getInt(data, pos + size);
059: size += 4;
060: bytesRemaining -= size;
061: if (bytesRemaining != 0)
062: throw new RecordFormatException(
063: "Expected no remaining bytes but got "
064: + bytesRemaining);
065: // remainingData = new byte[bytesRemaining];
066: // System.arraycopy( data, pos + size, remainingData, 0, bytesRemaining );
067: return 8 + size + bytesRemaining;
068: }
069:
070: /**
071: * This method serializes this escher record into a byte array.
072: *
073: * @param offset The offset into <code>data</code> to start writing the record data to.
074: * @param data The byte array to serialize to.
075: * @param listener A listener to retrieve start and end callbacks. Use a <code>NullEscherSerailizationListener</code> to ignore these events.
076: * @return The number of bytes written.
077: *
078: * @see NullEscherSerializationListener
079: */
080: public int serialize(int offset, byte[] data,
081: EscherSerializationListener listener) {
082: listener.beforeRecordSerialize(offset, getRecordId(), this );
083: LittleEndian.putShort(data, offset, getOptions());
084: LittleEndian.putShort(data, offset + 2, getRecordId());
085: int remainingBytes = 16;
086: LittleEndian.putInt(data, offset + 4, remainingBytes);
087: LittleEndian.putInt(data, offset + 8, field_1_rectX1);
088: LittleEndian.putInt(data, offset + 12, field_2_rectY1);
089: LittleEndian.putInt(data, offset + 16, field_3_rectX2);
090: LittleEndian.putInt(data, offset + 20, field_4_rectY2);
091: // System.arraycopy( remainingData, 0, data, offset + 26, remainingData.length );
092: // int pos = offset + 8 + 18 + remainingData.length;
093: listener.afterRecordSerialize(offset + getRecordSize(),
094: getRecordId(), offset + getRecordSize(), this );
095: return 8 + 16;
096: }
097:
098: /**
099: * Returns the number of bytes that are required to serialize this record.
100: *
101: * @return Number of bytes
102: */
103: public int getRecordSize() {
104: return 8 + 16;
105: }
106:
107: /**
108: * The 16 bit identifier of this shape group record.
109: */
110: public short getRecordId() {
111: return RECORD_ID;
112: }
113:
114: /**
115: * The short name for this record
116: */
117: public String getRecordName() {
118: return "Spgr";
119: }
120:
121: /**
122: * @return the string representation of this record.
123: */
124: public String toString() {
125: String nl = System.getProperty("line.separator");
126:
127: // String extraData;
128: // ByteArrayOutputStream b = new ByteArrayOutputStream();
129: // try
130: // {
131: // HexDump.dump(this.remainingData, 0, b, 0);
132: // extraData = b.toString();
133: // }
134: // catch ( Exception e )
135: // {
136: // extraData = "error";
137: // }
138: return getClass().getName() + ":" + nl + " RecordId: 0x"
139: + HexDump.toHex(RECORD_ID) + nl + " Options: 0x"
140: + HexDump.toHex(getOptions()) + nl + " RectX: "
141: + field_1_rectX1 + nl + " RectY: " + field_2_rectY1
142: + nl + " RectWidth: " + field_3_rectX2 + nl
143: + " RectHeight: " + field_4_rectY2 + nl;
144:
145: }
146:
147: /**
148: * The starting top-left coordinate of child records.
149: */
150: public int getRectX1() {
151: return field_1_rectX1;
152: }
153:
154: /**
155: * The starting top-left coordinate of child records.
156: */
157: public void setRectX1(int x1) {
158: this .field_1_rectX1 = x1;
159: }
160:
161: /**
162: * The starting top-left coordinate of child records.
163: */
164: public int getRectY1() {
165: return field_2_rectY1;
166: }
167:
168: /**
169: * The starting top-left coordinate of child records.
170: */
171: public void setRectY1(int y1) {
172: this .field_2_rectY1 = y1;
173: }
174:
175: /**
176: * The starting bottom-right coordinate of child records.
177: */
178: public int getRectX2() {
179: return field_3_rectX2;
180: }
181:
182: /**
183: * The starting bottom-right coordinate of child records.
184: */
185: public void setRectX2(int x2) {
186: this .field_3_rectX2 = x2;
187: }
188:
189: /**
190: * The starting bottom-right coordinate of child records.
191: */
192: public int getRectY2() {
193: return field_4_rectY2;
194: }
195:
196: /**
197: * The starting bottom-right coordinate of child records.
198: */
199: public void setRectY2(int field_4_rectY2) {
200: this.field_4_rectY2 = field_4_rectY2;
201: }
202: }
|