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.hslf.record;
019:
020: import java.io.OutputStream;
021: import java.io.IOException;
022:
023: import org.apache.poi.util.LittleEndian;
024: import org.apache.poi.util.POILogger;
025:
026: /**
027: * This data represents an embedded object in the document.
028: *
029: * @author Daniel Noll
030: */
031: public class ExEmbed extends RecordContainer {
032:
033: /**
034: * Record header data.
035: */
036: private byte[] _header;
037:
038: // Links to our more interesting children
039: private ExEmbedAtom embedAtom;
040: private ExOleObjAtom oleObjAtom;
041: private CString menuName;
042: private CString progId;
043: private CString clipboardName;
044:
045: /**
046: * Set things up, and find our more interesting children
047: *
048: * @param source the source data as a byte array.
049: * @param start the start offset into the byte array.
050: * @param len the length of the slice in the byte array.
051: */
052: protected ExEmbed(byte[] source, int start, int len) {
053: // Grab the header
054: _header = new byte[8];
055: System.arraycopy(source, start, _header, 0, 8);
056:
057: // Find our children
058: _children = Record.findChildRecords(source, start + 8, len - 8);
059: findInterestingChildren();
060: }
061:
062: /**
063: * Create a new ExEmbed, with blank fields
064: */
065: public ExEmbed() {
066: _header = new byte[8];
067: _children = new Record[5];
068:
069: // Setup our header block
070: _header[0] = 0x0f; // We are a container record
071: LittleEndian.putShort(_header, 2, (short) getRecordType());
072:
073: // Setup our child records
074: CString cs1 = new CString();
075: CString cs2 = new CString();
076: CString cs3 = new CString();
077: // cs1.setCount(0x00);
078: // cs2.setCount(0x10);
079: _children[0] = new ExEmbedAtom();
080: _children[1] = new ExOleObjAtom();
081: _children[2] = cs1;
082: _children[3] = cs2;
083: _children[4] = cs3;
084: findInterestingChildren();
085: }
086:
087: /**
088: * Go through our child records, picking out the ones that are
089: * interesting, and saving those for use by the easy helper methods.
090: */
091: private void findInterestingChildren() {
092:
093: // First child should be the ExHyperlinkAtom
094: if (_children[0] instanceof ExEmbedAtom) {
095: embedAtom = (ExEmbedAtom) _children[0];
096: } else {
097: logger.log(POILogger.ERROR,
098: "First child record wasn't a ExEmbedAtom, was of type "
099: + _children[0].getRecordType());
100: }
101:
102: // Second child should be the ExOleObjAtom
103: if (_children[1] instanceof ExOleObjAtom) {
104: oleObjAtom = (ExOleObjAtom) _children[1];
105: } else {
106: logger.log(POILogger.ERROR,
107: "Second child record wasn't a ExOleObjAtom, was of type "
108: + _children[1].getRecordType());
109: }
110:
111: for (int i = 2; i < _children.length; i++) {
112: if (_children[i] instanceof CString) {
113: if (menuName == null)
114: menuName = (CString) _children[i];
115: else if (progId == null)
116: progId = (CString) _children[i];
117: else if (clipboardName == null)
118: clipboardName = (CString) _children[i];
119: } else {
120: logger.log(POILogger.ERROR,
121: "Record after atoms wasn't a CString, was of type "
122: + _children[i].getRecordType());
123: }
124: }
125: }
126:
127: /**
128: * Gets the {@code ExEmbedAtom}.
129: *
130: * @return the {@code ExEmbedAtom}.
131: */
132: public ExEmbedAtom getExEmbedAtom() {
133: return embedAtom;
134: }
135:
136: /**
137: * Gets the {@code ExOleObjAtom}.
138: *
139: * @return the {@code ExOleObjAtom}.
140: */
141: public ExOleObjAtom getExOleObjAtom() {
142: return oleObjAtom;
143: }
144:
145: /**
146: * Gets the name used for menus and the Links dialog box.
147: *
148: * @return the name used for menus and the Links dialog box.
149: */
150: public String getMenuName() {
151: return menuName == null ? null : menuName.getText();
152: }
153:
154: /**
155: * Gets the OLE Programmatic Identifier.
156: *
157: * @return the OLE Programmatic Identifier.
158: */
159: public String getProgId() {
160: return progId == null ? null : progId.getText();
161: }
162:
163: /**
164: * Gets the name that appears in the paste special dialog.
165: *
166: * @return the name that appears in the paste special dialog.
167: */
168: public String getClipboardName() {
169: return clipboardName == null ? null : clipboardName.getText();
170: }
171:
172: /**
173: * Returns the type (held as a little endian in bytes 3 and 4)
174: * that this class handles.
175: *
176: * @return the record type.
177: */
178: public long getRecordType() {
179: return RecordTypes.ExEmbed.typeID;
180: }
181:
182: /**
183: * Have the contents printer out into an OutputStream, used when
184: * writing a file back out to disk.
185: *
186: * @param out the output stream.
187: * @throws IOException if there was an error writing to the stream.
188: */
189: public void writeOut(OutputStream out) throws IOException {
190: writeOut(_header[0], _header[1], getRecordType(), _children,
191: out);
192: }
193: }
|