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 org.apache.poi.util.POILogger;
021:
022: import java.io.IOException;
023: import java.io.OutputStream;
024:
025: /**
026: * Master container for Document. There is one of these for every
027: * slideshow, and it holds lots of definitions, and some summaries.
028: *
029: * @author Nick Burch
030: */
031:
032: public class Document extends PositionDependentRecordContainer {
033: private byte[] _header;
034: private static long _type = 1000;
035:
036: // Links to our more interesting children
037: private DocumentAtom documentAtom;
038: private Environment environment;
039: private PPDrawingGroup ppDrawing;
040: private SlideListWithText[] slwts;
041: private ExObjList exObjList; // Can be null
042:
043: /**
044: * Returns the DocumentAtom of this Document
045: */
046: public DocumentAtom getDocumentAtom() {
047: return documentAtom;
048: }
049:
050: /**
051: * Returns the Environment of this Notes, which lots of
052: * settings for the document in it
053: */
054: public Environment getEnvironment() {
055: return environment;
056: }
057:
058: /**
059: * Returns the PPDrawingGroup, which holds an Escher Structure
060: * that contains information on pictures in the slides.
061: */
062: public PPDrawingGroup getPPDrawingGroup() {
063: return ppDrawing;
064: }
065:
066: /**
067: * Returns the ExObjList, which holds the references to
068: * external objects used in the slides. This may be null, if
069: * there are no external references.
070: */
071: public ExObjList getExObjList() {
072: return exObjList;
073: }
074:
075: /**
076: * Returns all the SlideListWithTexts that are defined for
077: * this Document. They hold the text, and some of the text
078: * properties, which are referred to by the slides.
079: * This will normally return an array of size 2 or 3
080: */
081: public SlideListWithText[] getSlideListWithTexts() {
082: return slwts;
083: }
084:
085: /**
086: * Returns the SlideListWithText that deals with the
087: * Master Slides
088: */
089: public SlideListWithText getMasterSlideListWithText() {
090: if (slwts.length > 0) {
091: return slwts[0];
092: }
093: return null;
094: }
095:
096: /**
097: * Returns the SlideListWithText that deals with the
098: * Slides, or null if there isn't one
099: */
100: public SlideListWithText getSlideSlideListWithText() {
101: if (slwts.length > 1) {
102: return slwts[1];
103: }
104: return null;
105: }
106:
107: /**
108: * Returns the SlideListWithText that deals with the
109: * notes, or null if there isn't one
110: */
111: public SlideListWithText getNotesSlideListWithText() {
112: if (slwts.length > 2) {
113: return slwts[2];
114: }
115: return null;
116: }
117:
118: /**
119: * Set things up, and find our more interesting children
120: */
121: protected Document(byte[] source, int start, int len) {
122: // Grab the header
123: _header = new byte[8];
124: System.arraycopy(source, start, _header, 0, 8);
125:
126: // Find our children
127: _children = Record.findChildRecords(source, start + 8, len - 8);
128:
129: // Our first one should be a document atom
130: if (!(_children[0] instanceof DocumentAtom)) {
131: throw new IllegalStateException(
132: "The first child of a Document must be a DocumentAtom");
133: }
134: documentAtom = (DocumentAtom) _children[0];
135:
136: // Find how many SlideListWithTexts we have
137: // Also, grab the Environment and PPDrawing records
138: // on our way past
139: int slwtcount = 0;
140: for (int i = 1; i < _children.length; i++) {
141: if (_children[i] instanceof SlideListWithText) {
142: slwtcount++;
143: }
144: if (_children[i] instanceof Environment) {
145: environment = (Environment) _children[i];
146: }
147: if (_children[i] instanceof PPDrawingGroup) {
148: ppDrawing = (PPDrawingGroup) _children[i];
149: }
150: if (_children[i] instanceof ExObjList) {
151: exObjList = (ExObjList) _children[i];
152: }
153: }
154:
155: // You should only every have 1, 2 or 3 SLWTs
156: // (normally it's 2, or 3 if you have notes)
157: // Complain if it's not
158: if (slwtcount == 0) {
159: logger
160: .log(POILogger.WARN,
161: "No SlideListWithText's found - there should normally be at least one!");
162: }
163: if (slwtcount > 3) {
164: logger
165: .log(
166: POILogger.WARN,
167: "Found "
168: + slwtcount
169: + " SlideListWithTexts - normally there should only be three!");
170: }
171:
172: // Now grab all the SLWTs
173: slwts = new SlideListWithText[slwtcount];
174: slwtcount = 0;
175: for (int i = 1; i < _children.length; i++) {
176: if (_children[i] instanceof SlideListWithText) {
177: slwts[slwtcount] = (SlideListWithText) _children[i];
178: slwtcount++;
179: }
180: }
181: }
182:
183: /**
184: * Adds a new SlideListWithText record, at the appropriate
185: * point in the child records.
186: */
187: public void addSlideListWithText(SlideListWithText slwt) {
188: // The new SlideListWithText should go in
189: // just before the EndDocumentRecord
190: Record endDoc = _children[_children.length - 1];
191: if (endDoc.getRecordType() != RecordTypes.EndDocument.typeID) {
192: throw new IllegalStateException(
193: "The last child record of a Document should be EndDocument, but it was "
194: + endDoc);
195: }
196:
197: // Add in the record
198: addChildBefore(slwt, endDoc);
199:
200: // Updated our cached list of SlideListWithText records
201: int newSize = slwts.length + 1;
202: SlideListWithText[] nl = new SlideListWithText[newSize];
203: System.arraycopy(slwts, 0, nl, 0, slwts.length);
204: nl[nl.length - 1] = slwt;
205: slwts = nl;
206: }
207:
208: /**
209: * We are of type 1000
210: */
211: public long getRecordType() {
212: return _type;
213: }
214:
215: /**
216: * Write the contents of the record back, so it can be written
217: * to disk
218: */
219: public void writeOut(OutputStream out) throws IOException {
220: writeOut(_header[0], _header[1], _type, _children, out);
221: }
222: }
|