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;
public class HPBFDocument extends POIDocument {
private MainContents mainContents;
private QuillContents quillContents;
+ private EscherStm escherStm;
+ private EscherDelayStm escherDelayStm;
/**
* Opens a new publisher document
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,
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);
}
}
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/");
}
--- /dev/null
+/* ====================================================================
+ 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"
+ };
+ }
+}
--- /dev/null
+/* ====================================================================
+ 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;
+ }
+ }
+}
--- /dev/null
+/* ====================================================================
+ 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"
+ };
+ }
+}
==================================================================== */
package org.apache.poi.hpbf.model;
+import java.io.ByteArrayInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
}
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.
public String[] getPath() {
return new String[] { "Contents" };
}
+
+ protected void generateData() {
+ // We don't parse the contents, so
+ // nothing will have changed
+ }
}
// 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[] {
--- /dev/null
+/* ====================================================================
+ 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 {
+// }
+}