Create a Document record class, to do a lot of the common Document stuff for us

git-svn-id: https://svn.apache.org/repos/asf/jakarta/poi/trunk@386981 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Nick Burch 2006-03-19 15:59:23 +00:00
parent 00011b1c8e
commit e76e2d8526
3 changed files with 221 additions and 25 deletions

View File

@ -0,0 +1,110 @@
/* ====================================================================
Copyright 2002-2004 Apache Software Foundation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
package org.apache.poi.hslf.record;
import java.io.IOException;
import java.io.OutputStream;
/**
* Master container for Document. There is one of these for every
* slideshow, and it holds lots of definitions, and some summaries.
*
* @author Nick Burch
*/
public class Document extends PositionDependentRecordContainer
{
private byte[] _header;
private static long _type = 1000;
// Links to our more interesting children
private DocumentAtom documentAtom;
private Record environment;
private SlideListWithText[] slwts;
/**
* Returns the DocumentAtom of this Document
*/
public DocumentAtom getDocumentAtom() { return documentAtom; }
/**
* Returns the Environment of this Notes, which lots of
* settings for the document in it
*/
public Record getEnvironment() { return environment; }
/**
* Returns all the SlideListWithTexts that are defined for
* this Document. They hold the text, and some of the text
* properties, which are referred to by the slides.
*/
public SlideListWithText[] getSlideListWithTexts() { return slwts; }
/**
* Set things up, and find our more interesting children
*/
protected Document(byte[] source, int start, int len) {
// Grab the header
_header = new byte[8];
System.arraycopy(source,start,_header,0,8);
// Find our children
_children = Record.findChildRecords(source,start+8,len-8);
// Our first one should be a document atom
if(! (_children[0] instanceof DocumentAtom)) {
throw new IllegalStateException("The first child of a Document must be a DocumentAtom");
}
documentAtom = (DocumentAtom)_children[0];
// Find how many SlideListWithTexts we have
// Also, grab the Environment record on our way past
int slwtcount = 0;
for(int i=1; i<_children.length; i++) {
if(_children[i] instanceof SlideListWithText) {
slwtcount++;
}
if(_children[i].getRecordType() == RecordTypes.Environment.typeID) {
environment = _children[i];
}
}
// Now grab them all
slwts = new SlideListWithText[slwtcount];
slwtcount = 0;
for(int i=1; i<_children.length; i++) {
if(_children[i] instanceof SlideListWithText) {
slwts[slwtcount] = (SlideListWithText)_children[i];
slwtcount++;
}
}
}
/**
* We are of type 1000
*/
public long getRecordType() { return _type; }
/**
* Write the contents of the record back, so it can be written
* to disk
*/
public void writeOut(OutputStream out) throws IOException {
writeOut(_header[0],_header[1],_type,_children,out);
}
}

View File

@ -34,7 +34,7 @@ public class RecordTypes {
public static HashMap typeToClass;
public static final Type Unknown = new Type(0,null);
public static final Type Document = new Type(1000,DummyPositionSensitiveRecordWithChildren.class);
public static final Type Document = new Type(1000,Document.class);
public static final Type DocumentAtom = new Type(1001,DocumentAtom.class);
public static final Type EndDocument = new Type(1002,null);
public static final Type Slide = new Type(1006,Slide.class);

View File

@ -20,12 +20,13 @@
package org.apache.poi.hslf.usermodel;
import java.util.*;
import java.awt.Dimension;
import java.io.*;
import org.apache.poi.util.LittleEndian;
import org.apache.poi.hslf.*;
import org.apache.poi.hslf.model.*;
import org.apache.poi.hslf.record.Document;
import org.apache.poi.hslf.record.DocumentAtom;
import org.apache.poi.hslf.record.FontCollection;
import org.apache.poi.hslf.record.ParentAwareRecord;
import org.apache.poi.hslf.record.Record;
@ -62,7 +63,7 @@ public class SlideShow
private Record[] _mostRecentCoreRecords;
// Records that are interesting
private Record _documentRecord;
private Document _documentRecord;
// Friendly objects for people to deal with
private Slide[] _slides;
@ -201,7 +202,7 @@ public class SlideShow
// Now look for the interesting records in there
for(int i=0; i<_mostRecentCoreRecords.length; i++) {
if(_mostRecentCoreRecords[i].getRecordType() == RecordTypes.Document.typeID) {
_documentRecord = _mostRecentCoreRecords[i];
_documentRecord = (Document)_mostRecentCoreRecords[i];
}
}
}
@ -380,6 +381,83 @@ public class SlideShow
}
}
/**
* Create a blank <code>Slide</code>.
*
* @return the created <code>Slide</code>
* @throws IOException
*/
public Slide createSlide() throws IOException {
// RecordContainer slist=null;
// Record[] rec = doc.getChildRecords();
// int num = 0;
// for (int i = 0; i < rec.length; i++) {
// Record record = rec[i];
// if (record.getRecordType() == RecordTypes.SlideListWithText.typeID){
// if (num > 0){
// slist = (RecordContainer)record;
// }
// num++;
// }
// }
// if (num == 1){
// slist = new SlideListWithText();
// rec = doc.getChildRecords();
// for (int i = 0; i < rec.length-1; i++) {
// Record record = rec[i+1];
// if (record.getRecordType() == RecordTypes.EndDocument.typeID){
//
// doc.addChildAfter(slist, rec[i]);
// }
// }
// }
// rec = slist.getChildRecords();
//
// //add SlidePersistAtom
// SlidePersistAtom prev = rec.length == 0 ? null : (SlidePersistAtom)rec[rec.length - 1];
// SlidePersistAtom sp = new SlidePersistAtom();
//
// //refernce is the 1-based index of the slide container in the document root.
// //it always starts with 3 (1 is Document, 2 is MainMaster, 3 is the first slide)
// sp.setRefID(prev == null ? 3 : (prev.getRefID() + 1));
// //first slideId is always 256
// sp.setSlideIdentifier(prev == null ? 256 : (prev.getSlideIdentifier() + 1));
//
// Record[] r = slist.appendChildRecord(sp,
// slist.getChildRecords() == null ? new Record[]{} : slist.getChildRecords());
// slist.setChildRecords(r);
// Slide slide = new Slide();
//
// int offset = 0;
// List lst = new ArrayList();
// for (int i = 0; i < _records.length; i++) {
// Record record = _records[i];
// lst.add(record);
// ByteArrayOutputStream out = new ByteArrayOutputStream();
// record.writeOut(out);
//
// if (_records[i].getRecordType() == RecordTypes.PersistPtrIncrementalBlock.typeID){
// lst.add(i, slide.getSlideRecord());
//
// slide.getSlideRecord().setLastOnDiskOffset(offset);
// PersistPtrHolder ptr = (PersistPtrHolder)_records[i];
// int id = sp.getRefID();
// ptr.getSlideDataLocationsLookup().put(new Integer(id), new Integer((i+1)*4));
// ptr.getSlideLocationsLookup().put(new Integer(id), new Integer(offset));
// ptr.addSlideLookup(id, offset);
//
// }
// offset += out.size() ;
// }
// _records = (Record[])lst.toArray(new Record[lst.size()]);
// _hslfSlideShow.setRecords(_records);
//
// UserEditAtom usr = (UserEditAtom)_records[_records.length-1];
// usr.setLastViewType((short)UserEditAtom.LAST_VIEW_SLIDE_VIEW);
// return slide;
return null;
}
/**
* Writes out the slideshow file the is represented by an instance of
@ -393,29 +471,29 @@ public class SlideShow
}
// Accesser methods follow
// Accesser methods follow
/**
* Returns an array of the most recent version of all the interesting
* records
*/
public Record[] getMostRecentCoreRecords() { return _mostRecentCoreRecords; }
/**
* Returns an array of the most recent version of all the interesting
* records
*/
public Record[] getMostRecentCoreRecords() { return _mostRecentCoreRecords; }
/**
* Returns an array of all the normal Slides found in the slideshow
*/
public Slide[] getSlides() { return _slides; }
/**
* Returns an array of all the normal Slides found in the slideshow
*/
public Slide[] getSlides() { return _slides; }
/**
* Returns an array of all the normal Notes found in the slideshow
*/
public Notes[] getNotes() { return _notes; }
/**
* Returns an array of all the normal Notes found in the slideshow
*/
public Notes[] getNotes() { return _notes; }
/**
* Returns an array of all the meta Sheets (master sheets etc)
* found in the slideshow
*/
//public MetaSheet[] getMetaSheets() { return _msheets; }
/**
* Returns an array of all the meta Sheets (master sheets etc)
* found in the slideshow
*/
//public MetaSheet[] getMetaSheets() { return _msheets; }
/**
* Returns all the pictures attached to the SlideShow
@ -424,6 +502,14 @@ public class SlideShow
return _hslfSlideShow.getPictures();
}
/**
* Return the current page size
*/
public Dimension getPageSize(){
DocumentAtom docatom = _documentRecord.getDocumentAtom();
return new Dimension((int)docatom.getSlideSizeX(), (int)docatom.getSlideSizeY());
}
/**
* Helper method for usermodel: Get the font collection
*/
@ -431,5 +517,5 @@ public class SlideShow
/**
* Helper method for usermodel: Get the document record
*/
protected Record getDocumentRecord() { return _documentRecord; }
protected Document getDocumentRecord() { return _documentRecord; }
}