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.hssf.record;
019:
020: import org.apache.poi.util.LittleEndian;
021:
022: /**
023: * NOTE: Comment Associated with a Cell (1Ch)
024: *
025: * @author Yegor Kozlov
026: */
027: public class NoteRecord extends Record {
028: public final static short sid = 0x1C;
029:
030: /**
031: * Flag indicating that the comment is hidden (default)
032: */
033: public final static short NOTE_HIDDEN = 0x0;
034:
035: /**
036: * Flag indicating that the comment is visible
037: */
038: public final static short NOTE_VISIBLE = 0x2;
039:
040: private short field_1_row;
041: private short field_2_col;
042: private short field_3_flags;
043: private short field_4_shapeid;
044: private String field_5_author;
045:
046: /**
047: * Construct a new <code>NoteRecord</code> and
048: * fill its data with the default values
049: */
050: public NoteRecord() {
051: field_5_author = "";
052: field_3_flags = 0;
053: }
054:
055: /**
056: * Constructs a <code>NoteRecord</code> and fills its fields
057: * from the supplied <code>RecordInputStream</code>.
058: *
059: * @param in the stream to read from
060: */
061: public NoteRecord(RecordInputStream in) {
062: super (in);
063:
064: }
065:
066: /**
067: * @return id of this record.
068: */
069: public short getSid() {
070: return sid;
071: }
072:
073: /**
074: * Checks the sid matches the expected side for this record
075: *
076: * @param id the expected sid.
077: */
078: protected void validateSid(short id) {
079: if (id != sid) {
080: throw new RecordFormatException("Not a NoteRecord record");
081: }
082: }
083:
084: /**
085: * Read the record data from the supplied <code>RecordInputStream</code>
086: */
087: protected void fillFields(RecordInputStream in) {
088: field_1_row = in.readShort();
089: field_2_col = in.readShort();
090: field_3_flags = in.readShort();
091: field_4_shapeid = in.readShort();
092: int length = in.readShort();
093: byte[] bytes = in.readRemainder();
094: field_5_author = new String(bytes, 1, length);
095: }
096:
097: /**
098: * Serialize the record data into the supplied array of bytes
099: *
100: * @param offset offset in the <code>data</code>
101: * @param data the data to serialize into
102: *
103: * @return size of the record
104: */
105: public int serialize(int offset, byte[] data) {
106: LittleEndian.putShort(data, 0 + offset, sid);
107: LittleEndian.putShort(data, 2 + offset,
108: (short) (getRecordSize() - 4));
109:
110: LittleEndian.putShort(data, 4 + offset, field_1_row);
111: LittleEndian.putShort(data, 6 + offset, field_2_col);
112: LittleEndian.putShort(data, 8 + offset, field_3_flags);
113: LittleEndian.putShort(data, 10 + offset, field_4_shapeid);
114: LittleEndian.putShort(data, 12 + offset, (short) field_5_author
115: .length());
116:
117: byte[] str = field_5_author.getBytes();
118: System.arraycopy(str, 0, data, 15 + offset, str.length);
119:
120: return getRecordSize();
121: }
122:
123: /**
124: * Size of record
125: */
126: public int getRecordSize() {
127: int retval = 4 + 2 + 2 + 2 + 2 + 2 + 1
128: + field_5_author.length() + 1;
129:
130: return retval;
131: }
132:
133: /**
134: * Convert this record to string.
135: * Used by BiffViewer and other utulities.
136: */
137: public String toString() {
138: StringBuffer buffer = new StringBuffer();
139:
140: buffer.append("[NOTE]\n");
141: buffer.append(" .recordid = 0x"
142: + Integer.toHexString(getSid()) + ", size = "
143: + getRecordSize() + "\n");
144: buffer.append(" .row = " + field_1_row + "\n");
145: buffer.append(" .col = " + field_2_col + "\n");
146: buffer.append(" .flags = " + field_3_flags + "\n");
147: buffer.append(" .shapeid = " + field_4_shapeid + "\n");
148: buffer.append(" .author = " + field_5_author + "\n");
149: buffer.append("[/NOTE]\n");
150: return buffer.toString();
151: }
152:
153: /**
154: * Return the row that contains the comment
155: *
156: * @return the row that contains the comment
157: */
158: public short getRow() {
159: return field_1_row;
160: }
161:
162: /**
163: * Specify the row that contains the comment
164: *
165: * @param row the row that contains the comment
166: */
167: public void setRow(short row) {
168: field_1_row = row;
169: }
170:
171: /**
172: * Return the column that contains the comment
173: *
174: * @return the column that contains the comment
175: */
176: public short getColumn() {
177: return field_2_col;
178: }
179:
180: /**
181: * Specify the column that contains the comment
182: *
183: * @param col the column that contains the comment
184: */
185: public void setColumn(short col) {
186: field_2_col = col;
187: }
188:
189: /**
190: * Options flags.
191: *
192: * @return the options flag
193: * @see #NOTE_VISIBLE
194: * @see #NOTE_HIDDEN
195: */
196: public short getFlags() {
197: return field_3_flags;
198: }
199:
200: /**
201: * Options flag
202: *
203: * @param flags the options flag
204: * @see #NOTE_VISIBLE
205: * @see #NOTE_HIDDEN
206: */
207: public void setFlags(short flags) {
208: field_3_flags = flags;
209: }
210:
211: /**
212: * Object id for OBJ record that contains the comment
213: */
214: public short getShapeId() {
215: return field_4_shapeid;
216: }
217:
218: /**
219: * Object id for OBJ record that contains the comment
220: */
221: public void setShapeId(short id) {
222: field_4_shapeid = id;
223: }
224:
225: /**
226: * Name of the original comment author
227: *
228: * @return the name of the original author of the comment
229: */
230: public String getAuthor() {
231: return field_5_author;
232: }
233:
234: /**
235: * Name of the original comment author
236: *
237: * @param author the name of the original author of the comment
238: */
239: public void setAuthor(String author) {
240: field_5_author = author;
241: }
242: }
|