Add ExObjList support

git-svn-id: https://svn.apache.org/repos/asf/jakarta/poi/trunk@497107 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Nick Burch 2007-01-17 18:09:24 +00:00
parent 1557708b2a
commit 066910f3c0
8 changed files with 430 additions and 7 deletions

View File

@ -39,6 +39,7 @@ public class Document extends PositionDependentRecordContainer
private Environment environment;
private PPDrawingGroup ppDrawing;
private SlideListWithText[] slwts;
private ExObjList exObjList; // Can be null
/**
* Returns the DocumentAtom of this Document
@ -54,6 +55,12 @@ public class Document extends PositionDependentRecordContainer
* that contains information on pictures in the slides.
*/
public PPDrawingGroup getPPDrawingGroup() { return ppDrawing; }
/**
* Returns the ExObjList, which holds the references to
* external objects used in the slides. This may be null, if
* there are no external references.
*/
public ExObjList getExObjList() { return exObjList; }
/**
* Returns all the SlideListWithTexts that are defined for
@ -116,6 +123,9 @@ public class Document extends PositionDependentRecordContainer
if(_children[i] instanceof PPDrawingGroup) {
ppDrawing = (PPDrawingGroup)_children[i];
}
if(_children[i] instanceof ExObjList) {
exObjList = (ExObjList)_children[i];
}
}
// You should only every have 1, 2 or 3 SLWTs

View File

@ -0,0 +1,112 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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;
import java.util.ArrayList;
import org.apache.poi.util.LittleEndian;
/**
* This class holds the links to exernal objects referenced
* from the document.
* @author Nick Burch
*/
public class ExObjList extends RecordContainer {
private byte[] _header;
private static long _type = 1033;
// Links to our more interesting children
private ExObjListAtom exObjListAtom;
/**
* Returns the ExObjListAtom of this list
*/
public ExObjListAtom getExObjListAtom() { return exObjListAtom; }
/**
* Returns all the ExHyperlinks
*/
public ExHyperlink[] getExHyperlinks() {
ArrayList links = new ArrayList();
for(int i=0; i<_children.length; i++) {
if(_children[i] instanceof ExHyperlink) {
links.add(_children[i]);
}
}
return (ExHyperlink[])links.toArray(new ExHyperlink[links.size()]);
}
/**
* Set things up, and find our more interesting children
*/
protected ExObjList(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);
findInterestingChildren();
}
/**
* Go through our child records, picking out the ones that are
* interesting, and saving those for use by the easy helper
* methods.
*/
private void findInterestingChildren() {
// First child should be the atom
if(_children[0] instanceof ExObjListAtom) {
exObjListAtom = (ExObjListAtom)_children[0];
} else {
throw new IllegalStateException("First child record wasn't a ExObjListAtom, was of type " + _children[0].getRecordType());
}
}
/**
* Create a new ExObjList, with blank fields
*/
public ExObjList() {
_header = new byte[8];
_children = new Record[1];
// Setup our header block
_header[0] = 0x0f; // We are a container record
LittleEndian.putShort(_header, 2, (short)_type);
// Setup our child records
_children[0] = new ExObjListAtom();
findInterestingChildren();
}
/**
* We are of type 1033
*/
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

@ -0,0 +1,114 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You 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;
import org.apache.poi.util.LittleEndian;
/**
* Tne atom that holds the seed info used by a ExObjList
*
* @author Nick Burch
*/
public class ExObjListAtom extends RecordAtom
{
/**
* Record header.
*/
private byte[] _header;
/**
* Record data.
*/
private byte[] _data;
/**
* Constructs a brand new link related atom record.
*/
protected ExObjListAtom() {
_header = new byte[8];
_data = new byte[4];
LittleEndian.putShort(_header, 2, (short)getRecordType());
LittleEndian.putInt(_header, 4, _data.length);
// It is fine for the other values to be zero
}
/**
* Constructs the link related atom record from its
* source data.
*
* @param source the source data as a byte array.
* @param start the start offset into the byte array.
* @param len the length of the slice in the byte array.
*/
protected ExObjListAtom(byte[] source, int start, int len) {
// Get the header.
_header = new byte[8];
System.arraycopy(source,start,_header,0,8);
// Get the record data.
_data = new byte[len-8];
System.arraycopy(source,start+8,_data,0,len-8);
// Must be at least 4 bytes long
if(_data.length < 4) {
throw new IllegalArgumentException("The length of the data for a ExObjListAtom must be at least 4 bytes, but was only " + _data.length);
}
}
/**
* Gets the object ID seed, which will be used as the unique
* OLE identifier for the next OLE object added
* @return the object ID seed
*/
public long getObjectIDSeed() {
return LittleEndian.getUInt(_data,0);
}
/**
* Sets the object ID seed
* @param seed the new ID seed
*/
public void setObjectIDSeed(int seed) {
LittleEndian.putInt(_data,0,seed);
}
/**
* Gets the record type.
* @return the record type.
*/
public long getRecordType() { return RecordTypes.ExObjListAtom.typeID; }
/**
* Write the contents of the record back, so it can be written
* to disk
*
* @param out the output stream to write to.
* @throws IOException if an error occurs.
*/
public void writeOut(OutputStream out) throws IOException {
out.write(_header);
out.write(_data);
}
}

View File

@ -59,8 +59,8 @@ public class RecordTypes {
public static final Type DocRoutingSlip = new Type(1030,null);
public static final Type OutlineViewInfo = new Type(1031,null);
public static final Type SorterViewInfo = new Type(1032,null);
public static final Type ExObjList = new Type(1033,DummyRecordWithChildren.class);
public static final Type ExObjListAtom = new Type(1034,null);
public static final Type ExObjList = new Type(1033,ExObjList.class);
public static final Type ExObjListAtom = new Type(1034,ExObjListAtom.class);
public static final Type PPDrawingGroup = new Type(1035,PPDrawingGroup.class);
public static final Type PPDrawing = new Type(1036,PPDrawing.class);
public static final Type NamedShows = new Type(1040,null);

View File

@ -23,6 +23,7 @@ package org.apache.poi.hslf.record;
import junit.framework.TestCase;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
@ -100,16 +101,16 @@ public class TestExHyperlink extends TestCase {
public void testRealFile() throws Exception {
String dirname = System.getProperty("HSLF.testdata.path");
HSLFSlideShow hss = new HSLFSlideShow(dirname + "WithLinks.ppt");
HSLFSlideShow hss = new HSLFSlideShow(dirname + File.separator + "WithLinks.ppt");
SlideShow ss = new SlideShow(hss);
// Get the document
Document doc = ss.getDocumentRecord();
// Get the ExObjList
RecordContainer exObjList = null;
ExObjList exObjList = null;
for(int i=0; i<doc._children.length; i++) {
if(doc._children[i].getRecordType() == RecordTypes.ExObjList.typeID) {
exObjList = (RecordContainer)doc._children[i];
if(doc._children[i] instanceof ExObjList) {
exObjList = (ExObjList)doc._children[i];
}
}
assertNotNull(exObjList);
@ -127,6 +128,10 @@ public class TestExHyperlink extends TestCase {
ExHyperlink[] links = new ExHyperlink[linksA.size()];
linksA.toArray(links);
assertEquals(4, exObjList.getExHyperlinks().length);
// Check the other way
// Check they have what we expect in them
assertEquals(1, links[0].getExHyperlinkAtom().getNumber());
assertEquals("http://jakarta.apache.org/poi/", links[0].getLinkURL());

View File

@ -27,7 +27,7 @@ import java.text.SimpleDateFormat;
import java.util.Date;
/**
* Tests that InteractiveInfoAtom works properly.
* Tests that ExHyperlinkAtom works properly.
*
* @author Nick Burch (nick at torchbox dot com)
*/

View File

@ -0,0 +1,76 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You 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 junit.framework.TestCase;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import org.apache.poi.hslf.HSLFSlideShow;
import org.apache.poi.hslf.usermodel.SlideShow;
/**
* Tests that ExObjList works properly.
*
* @author Nick Burch (nick at torchbox dot com)
*/
public class TestExObjList extends TestCase {
public void testRealFile() throws Exception {
String dirname = System.getProperty("HSLF.testdata.path");
HSLFSlideShow hss = new HSLFSlideShow(dirname + File.separator + "WithLinks.ppt");
SlideShow ss = new SlideShow(hss);
// Get the document
Document doc = ss.getDocumentRecord();
// Get the ExObjList
ExObjList exObjList = doc.getExObjList();
assertNotNull(exObjList);
assertEquals(1033l, exObjList.getRecordType());
// Check the atom
assertNotNull(exObjList.getExObjListAtom());
assertEquals(4, exObjList.getExObjListAtom().getObjectIDSeed());
// Check the Hyperlinks
assertEquals(4, exObjList.getExHyperlinks().length);
// Check the contents
ExHyperlink[] links = exObjList.getExHyperlinks();
// Check they have what we expect in them
assertEquals(1, links[0].getExHyperlinkAtom().getNumber());
assertEquals("http://jakarta.apache.org/poi/", links[0].getLinkURL());
assertEquals(2, links[1].getExHyperlinkAtom().getNumber());
assertEquals("http://slashdot.org/", links[1].getLinkURL());
assertEquals(3, links[2].getExHyperlinkAtom().getNumber());
assertEquals("http://jakarta.apache.org/poi/hssf/", links[2].getLinkURL());
assertEquals(4, links[3].getExHyperlinkAtom().getNumber());
assertEquals("http://jakarta.apache.org/hslf/", links[3].getLinkURL());
}
}

View File

@ -0,0 +1,106 @@
/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You 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 junit.framework.TestCase;
import java.io.ByteArrayOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* Tests that ExObjListAtom works properly.
*
* @author Nick Burch (nick at torchbox dot com)
*/
public class TestExObjListAtom extends TestCase {
// From a real file
private byte[] data_a = new byte[] {
00, 00, 0x0A, 0x04, 04, 00, 00, 00,
01, 00, 00, 00
};
private byte[] data_b = new byte[] {
00, 00, 0x0A, 0x04, 04, 00, 00, 00,
04, 00, 00, 00
};
public void testRecordType() throws Exception {
ExObjListAtom eoa = new ExObjListAtom(data_a, 0, data_a.length);
assertEquals(1034l, eoa.getRecordType());
}
public void testGetSeed() throws Exception {
ExObjListAtom eoa = new ExObjListAtom(data_a, 0, data_a.length);
ExObjListAtom eob = new ExObjListAtom(data_b, 0, data_b.length);
assertEquals(1, eoa.getObjectIDSeed());
assertEquals(4, eob.getObjectIDSeed());
}
public void testWrite() throws Exception {
ExObjListAtom eoa = new ExObjListAtom(data_a, 0, data_a.length);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
eoa.writeOut(baos);
byte[] b = baos.toByteArray();
assertEquals(data_a.length, b.length);
for(int i=0; i<data_a.length; i++) {
assertEquals(data_a[i],b[i]);
}
}
// Create A from scratch
public void testCreate() throws Exception {
ExObjListAtom eoa = new ExObjListAtom();
// Set seed
eoa.setObjectIDSeed(1);
// Check it's now the same as a
ByteArrayOutputStream baos = new ByteArrayOutputStream();
eoa.writeOut(baos);
byte[] b = baos.toByteArray();
assertEquals(data_a.length, b.length);
for(int i=0; i<data_a.length; i++) {
assertEquals(data_a[i],b[i]);
}
}
// Try to turn a into b
public void testChange() throws Exception {
ExObjListAtom eoa = new ExObjListAtom(data_a, 0, data_a.length);
// Change the number
eoa.setObjectIDSeed(4);
// Check bytes are now the same
ByteArrayOutputStream baos = new ByteArrayOutputStream();
eoa.writeOut(baos);
byte[] b = baos.toByteArray();
// Should now be the same
assertEquals(data_b.length, b.length);
for(int i=0; i<data_b.length; i++) {
assertEquals(data_b[i],b[i]);
}
}
}