More HPBF stuff, and some tests

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@687423 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Nick Burch 2008-08-20 18:50:15 +00:00
parent 6a1cf3281c
commit b35e6d5bf0
8 changed files with 275 additions and 3 deletions

View File

@ -18,9 +18,12 @@ package org.apache.poi.hpbf;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.poi.POIDocument;
import org.apache.poi.hpbf.model.EscherDelayStm;
import org.apache.poi.hpbf.model.EscherStm;
import org.apache.poi.hpbf.model.MainContents;
import org.apache.poi.hpbf.model.QuillContents;
import org.apache.poi.poifs.filesystem.DirectoryNode;
@ -34,6 +37,8 @@ import org.apache.poi.poifs.filesystem.POIFSFileSystem;
public class HPBFDocument extends POIDocument {
private MainContents mainContents;
private QuillContents quillContents;
private EscherStm escherStm;
private EscherDelayStm escherDelayStm;
/**
* Opens a new publisher document
@ -41,6 +46,9 @@ public class HPBFDocument extends POIDocument {
public HPBFDocument(POIFSFileSystem fs) throws IOException {
this(fs.getRoot(), fs);
}
public HPBFDocument(InputStream inp) throws IOException {
this(new POIFSFileSystem(inp));
}
/**
* Opens an embeded publisher document,
@ -54,12 +62,24 @@ public class HPBFDocument extends POIDocument {
try {
mainContents = new MainContents(dir);
} catch(FileNotFoundException e) {
throw new IllegalArgumentException("File invalid - missing required main Contents part");
throw new IllegalArgumentException("File invalid - missing required main Contents part", e);
}
try {
quillContents = new QuillContents(dir);
} catch(FileNotFoundException e) {
throw new IllegalArgumentException("File invalid - missing required Quill CONTENTS part");
throw new IllegalArgumentException("File invalid - missing required Quill CONTENTS part", e);
}
// Now the Escher bits
try {
escherStm = new EscherStm(dir);
} catch(FileNotFoundException e) {
throw new IllegalArgumentException("File invalid - missing required EscherStm part", e);
}
try {
escherDelayStm = new EscherDelayStm(dir);
} catch(FileNotFoundException e) {
throw new IllegalArgumentException("File invalid - missing required EscherDelayStm part", e);
}
}
@ -69,7 +89,13 @@ public class HPBFDocument extends POIDocument {
public QuillContents getQuillContents() {
return quillContents;
}
public EscherStm getEscherStm() {
return escherStm;
}
public EscherDelayStm getEscherDelayStm() {
return escherDelayStm;
}
public void write(OutputStream out) throws IOException {
throw new IllegalStateException("Writing is not yet implemented, see http://poi.apache.org/hpbf/");
}

View File

@ -0,0 +1,35 @@
/* ====================================================================
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.hpbf.model;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.apache.poi.poifs.filesystem.DirectoryNode;
public class EscherDelayStm extends EscherPart {
public EscherDelayStm(DirectoryNode baseDir) throws FileNotFoundException,
IOException {
super(baseDir);
}
public String[] getPath() {
return new String[] {
"Escher", "EscherDelayStm"
};
}
}

View File

@ -0,0 +1,81 @@
/* ====================================================================
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.hpbf.model;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import org.apache.poi.ddf.DefaultEscherRecordFactory;
import org.apache.poi.ddf.EscherRecord;
import org.apache.poi.poifs.filesystem.DirectoryNode;
/**
* Parent class of all Escher parts
*/
public abstract class EscherPart extends HPBFPart {
private EscherRecord[] records;
/**
* Creates the Escher Part, and finds our child
* escher records
*/
public EscherPart(DirectoryNode baseDir) throws FileNotFoundException,
IOException {
super(baseDir);
// Now create our Escher children
DefaultEscherRecordFactory erf =
new DefaultEscherRecordFactory();
ArrayList ec = new ArrayList();
int left = data.length;
while(left > 0) {
EscherRecord er = erf.createRecord(data, 0);
er.fillFields(data, 0, erf);
left -= er.getRecordSize();
ec.add(er);
}
records = (EscherRecord[])
ec.toArray(new EscherRecord[ec.size()]);
}
public EscherRecord[] getEscherRecords() {
return records;
}
/**
* Serialises our Escher children back
* into bytes.
*/
protected void generateData() {
int size = 0;
for(int i=0; i<records.length; i++) {
size += records[i].getRecordSize();
}
data = new byte[size];
size = 0;
for(int i=0; i<records.length; i++) {
int thisSize =
records[i].serialize(size, data);
size += thisSize;
}
}
}

View File

@ -0,0 +1,35 @@
/* ====================================================================
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.hpbf.model;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.apache.poi.poifs.filesystem.DirectoryNode;
public class EscherStm extends EscherPart {
public EscherStm(DirectoryNode baseDir) throws FileNotFoundException,
IOException {
super(baseDir);
}
public String[] getPath() {
return new String[] {
"Escher", "EscherStm"
};
}
}

View File

@ -16,6 +16,7 @@
==================================================================== */
package org.apache.poi.hpbf.model;
import java.io.ByteArrayInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
@ -51,9 +52,33 @@ public abstract class HPBFPart {
}
public void writeOut(DirectoryNode baseDir) throws IOException {
String[] path = getPath();
// Ensure that all parent directories exist
DirectoryNode dir = baseDir;
for(int i=0; i<path.length-1; i++) {
try {
dir = (DirectoryNode)dir.getEntry(path[i]);
} catch(FileNotFoundException e) {
dir.createDirectory(path[i]);
}
}
// Update the byte array with the latest data
generateData();
// Write out
ByteArrayInputStream bais = new ByteArrayInputStream(data);
dir.createDocument(path[path.length-1], bais);
}
/**
* Called just before writing out, to trigger
* the data byte array to be updated with the
* latest contents.
*/
protected abstract void generateData();
/**
* Returns the raw data that makes up
* this document part.

View File

@ -33,4 +33,9 @@ public class MainContents extends HPBFPart {
public String[] getPath() {
return new String[] { "Contents" };
}
protected void generateData() {
// We don't parse the contents, so
// nothing will have changed
}
}

View File

@ -32,6 +32,11 @@ public class QuillContents extends HPBFPart {
// Now parse the first 512 bytes, and produce
// all our bits
}
protected void generateData() {
// TODO
throw new IllegalStateException("Not done yet!");
}
public String[] getPath() {
return new String[] {

View File

@ -0,0 +1,60 @@
/* ====================================================================
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.hpbf;
import java.io.File;
import java.io.FileInputStream;
import junit.framework.TestCase;
public class TestHPBFDocument extends TestCase {
private String dir;
protected void setUp() throws Exception {
dir = System.getProperty("HPBF.testdata.path");
}
public void testOpen() throws Exception {
File f = new File(dir, "Sample.pub");
HPBFDocument doc = new HPBFDocument(
new FileInputStream(f)
);
assertNotNull(doc);
}
public void testBits() throws Exception {
File f = new File(dir, "Sample.pub");
HPBFDocument doc = new HPBFDocument(
new FileInputStream(f)
);
assertNotNull(doc.getMainContents());
assertNotNull(doc.getQuillContents());
assertNotNull(doc.getEscherStm());
assertNotNull(doc.getEscherDelayStm());
assertTrue(doc.getMainContents().getData().length > 0);
assertTrue(doc.getQuillContents().getData().length > 0);
assertTrue(doc.getEscherStm().getData().length > 0);
assertTrue(doc.getEscherDelayStm().getData().length == 0);
}
// TODO
// public void testWrite() throws Exception {
// }
}