public static final Type List = new Type(2000,null);
public static final Type FontCollection = new Type(2005,FontCollection.class);
public static final Type BookmarkCollection = new Type(2019,null);
+ public static final Type SoundCollection = new Type(2020,SoundCollection.class);
public static final Type SoundCollAtom = new Type(2021,null);
- public static final Type Sound = new Type(2022,null);
- public static final Type SoundData = new Type(2023,null);
+ public static final Type Sound = new Type(2022,Sound.class);
+ public static final Type SoundData = new Type(2023,SoundData.class);
public static final Type BookmarkSeedAtom = new Type(2025,null);
public static final Type ColorSchemeAtom = new Type(2032,ColorSchemeAtom.class);
public static final Type ExObjRefAtom = new Type(3009,null);
--- /dev/null
+/* ====================================================================\r
+ Licensed to the Apache Software Foundation (ASF) under one or more\r
+ contributor license agreements. See the NOTICE file distributed with\r
+ this work for additional information regarding copyright ownership.\r
+ The ASF licenses this file to You under the Apache License, Version 2.0\r
+ (the "License"); you may not use this file except in compliance with\r
+ the License. You may obtain a copy of the License at\r
+\r
+ http://www.apache.org/licenses/LICENSE-2.0\r
+\r
+ Unless required by applicable law or agreed to in writing, software\r
+ distributed under the License is distributed on an "AS IS" BASIS,\r
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ See the License for the specific language governing permissions and\r
+ limitations under the License.\r
+==================================================================== */\r
+package org.apache.poi.hslf.record;\r
+\r
+import org.apache.poi.util.POILogger;\r
+\r
+import java.io.OutputStream;\r
+import java.io.IOException;\r
+\r
+/**\r
+ * A container holding information about a sound. It contains:\r
+ * <p>\r
+ * <li>1. CString (4026), Instance 0: Name of sound (e.g. "crash")\r
+ * <li>2. CString (4026), Instance 1: Type of sound (e.g. ".wav")\r
+ * <li>3. CString (4026), Instance 2: Reference id of sound in sound collection\r
+ * <li>4. CString (4026), Instance 3, optional: Built-in id of sound, for sounds we ship. This is the id that?s in the reg file.\r
+ * <li>5. SoundData (2023), optional\r
+ * </p>\r
+ *\r
+ * @author Yegor Kozlov\r
+ */\r
+public class Sound extends RecordContainer {\r
+ /**\r
+ * Record header data.\r
+ */\r
+ private byte[] _header;\r
+\r
+ // Links to our more interesting children\r
+ private CString _name;\r
+ private CString _type;\r
+ private SoundData _data;\r
+\r
+\r
+ /**\r
+ * Set things up, and find our more interesting children\r
+ *\r
+ * @param source the source data as a byte array.\r
+ * @param start the start offset into the byte array.\r
+ * @param len the length of the slice in the byte array.\r
+ */\r
+ protected Sound(byte[] source, int start, int len) {\r
+ // Grab the header\r
+ _header = new byte[8];\r
+ System.arraycopy(source,start,_header,0,8);\r
+\r
+ // Find our children\r
+ _children = Record.findChildRecords(source,start+8,len-8);\r
+ findInterestingChildren();\r
+ }\r
+\r
+ private void findInterestingChildren() {\r
+ // First child should be the ExHyperlinkAtom\r
+ if(_children[0] instanceof CString) {\r
+ _name = (CString)_children[0];\r
+ } else {\r
+ logger.log(POILogger.ERROR, "First child record wasn't a CString, was of type " + _children[0].getRecordType());\r
+ }\r
+\r
+ // Second child should be the ExOleObjAtom\r
+ if (_children[1] instanceof CString) {\r
+ _type = (CString)_children[1];\r
+ } else {\r
+ logger.log(POILogger.ERROR, "Second child record wasn't a CString, was of type " + _children[1].getRecordType());\r
+ }\r
+\r
+ for (int i = 2; i < _children.length; i++) {\r
+ if(_children[i] instanceof SoundData){\r
+ _data = (SoundData)_children[i];\r
+ break;\r
+ }\r
+ }\r
+\r
+ }\r
+\r
+ /**\r
+ * Returns the type (held as a little endian in bytes 3 and 4)\r
+ * that this class handles.\r
+ *\r
+ * @return the record type.\r
+ */\r
+ public long getRecordType() {\r
+ return RecordTypes.Sound.typeID;\r
+ }\r
+\r
+ /**\r
+ * Have the contents printer out into an OutputStream, used when\r
+ * writing a file back out to disk.\r
+ *\r
+ * @param out the output stream.\r
+ * @throws java.io.IOException if there was an error writing to the stream.\r
+ */\r
+ public void writeOut(OutputStream out) throws IOException {\r
+ writeOut(_header[0],_header[1],getRecordType(),_children,out);\r
+ }\r
+\r
+ /**\r
+ * Name of the sound (e.g. "crash")\r
+ *\r
+ * @return name of the sound\r
+ */\r
+ public String getSoundName(){\r
+ return _name.getText();\r
+ }\r
+\r
+ /**\r
+ * Type of the sound (e.g. ".wav")\r
+ *\r
+ * @return type of the sound\r
+ */\r
+ public String getSoundType(){\r
+ return _type.getText();\r
+ }\r
+\r
+ /**\r
+ * The sound data\r
+ *\r
+ * @return the sound data.\r
+ */\r
+ public byte[] getSoundData(){\r
+ return _data == null ? null : _data.getData();\r
+ }\r
+}\r
--- /dev/null
+/* ====================================================================\r
+ Licensed to the Apache Software Foundation (ASF) under one or more\r
+ contributor license agreements. See the NOTICE file distributed with\r
+ this work for additional information regarding copyright ownership.\r
+ The ASF licenses this file to You under the Apache License, Version 2.0\r
+ (the "License"); you may not use this file except in compliance with\r
+ the License. You may obtain a copy of the License at\r
+\r
+ http://www.apache.org/licenses/LICENSE-2.0\r
+\r
+ Unless required by applicable law or agreed to in writing, software\r
+ distributed under the License is distributed on an "AS IS" BASIS,\r
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ See the License for the specific language governing permissions and\r
+ limitations under the License.\r
+==================================================================== */\r
+package org.apache.poi.hslf.record;\r
+\r
+import org.apache.poi.util.POILogger;\r
+\r
+import java.io.OutputStream;\r
+import java.io.IOException;\r
+\r
+/**\r
+ * Is a container for all sound related atoms and containers. It contains:\r
+ *<li>1. SoundCollAtom (2021)\r
+ *<li>2. Sound (2022), for each sound, if any\r
+ *\r
+ * @author Yegor Kozlov\r
+ */\r
+public class SoundCollection extends RecordContainer {\r
+ /**\r
+ * Record header data.\r
+ */\r
+ private byte[] _header;\r
+\r
+ /**\r
+ * Set things up, and find our more interesting children\r
+ *\r
+ * @param source the source data as a byte array.\r
+ * @param start the start offset into the byte array.\r
+ * @param len the length of the slice in the byte array.\r
+ */\r
+ protected SoundCollection(byte[] source, int start, int len) {\r
+ // Grab the header\r
+ _header = new byte[8];\r
+ System.arraycopy(source,start,_header,0,8);\r
+\r
+ // Find our children\r
+ _children = Record.findChildRecords(source,start+8,len-8);\r
+ }\r
+\r
+ /**\r
+ * Returns the type (held as a little endian in bytes 3 and 4)\r
+ * that this class handles.\r
+ *\r
+ * @return the record type.\r
+ */\r
+ public long getRecordType() {\r
+ return RecordTypes.SoundCollection.typeID;\r
+ }\r
+\r
+ /**\r
+ * Have the contents printer out into an OutputStream, used when\r
+ * writing a file back out to disk.\r
+ *\r
+ * @param out the output stream.\r
+ * @throws java.io.IOException if there was an error writing to the stream.\r
+ */\r
+ public void writeOut(OutputStream out) throws IOException {\r
+ writeOut(_header[0],_header[1],getRecordType(),_children,out);\r
+ }\r
+}\r
--- /dev/null
+/* ====================================================================\r
+ Licensed to the Apache Software Foundation (ASF) under one or more\r
+ contributor license agreements. See the NOTICE file distributed with\r
+ this work for additional information regarding copyright ownership.\r
+ The ASF licenses this file to You under the Apache License, Version 2.0\r
+ (the "License"); you may not use this file except in compliance with\r
+ the License. You may obtain a copy of the License at\r
+\r
+ http://www.apache.org/licenses/LICENSE-2.0\r
+\r
+ Unless required by applicable law or agreed to in writing, software\r
+ distributed under the License is distributed on an "AS IS" BASIS,\r
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ See the License for the specific language governing permissions and\r
+ limitations under the License.\r
+==================================================================== */\r
+\r
+package org.apache.poi.hslf.record;\r
+\r
+import java.io.ByteArrayInputStream;\r
+import java.io.IOException;\r
+import java.io.InputStream;\r
+import java.io.OutputStream;\r
+import java.util.zip.InflaterInputStream;\r
+\r
+import org.apache.poi.util.LittleEndian;\r
+\r
+/**\r
+ * Storage for embedded sounds.\r
+ *\r
+ * @author Yegor Kozlov\r
+ */\r
+public class SoundData extends RecordAtom {\r
+\r
+ /**\r
+ * Record header.\r
+ */\r
+ private byte[] _header;\r
+\r
+ /**\r
+ * Record data.\r
+ */\r
+ private byte[] _data;\r
+\r
+ /**\r
+ * Constructs a new empty sound container.\r
+ */\r
+ protected SoundData() {\r
+ _header = new byte[8];\r
+ _data = new byte[0];\r
+\r
+ LittleEndian.putShort(_header, 2, (short)getRecordType());\r
+ LittleEndian.putInt(_header, 4, _data.length);\r
+ }\r
+\r
+ /**\r
+ * Constructs the link related atom record from its\r
+ * source data.\r
+ *\r
+ * @param source the source data as a byte array.\r
+ * @param start the start offset into the byte array.\r
+ * @param len the length of the slice in the byte array.\r
+ */\r
+ protected SoundData(byte[] source, int start, int len) {\r
+ // Get the header.\r
+ _header = new byte[8];\r
+ System.arraycopy(source,start,_header,0,8);\r
+\r
+ // Get the record data.\r
+ _data = new byte[len-8];\r
+ System.arraycopy(source,start+8,_data,0,len-8);\r
+ }\r
+\r
+ /**\r
+ * Returns the sound data.\r
+ *\r
+ * @return the sound data \r
+ */\r
+ public byte[] getData() {\r
+ return _data;\r
+ }\r
+\r
+ /**\r
+ * Gets the record type.\r
+ *\r
+ * @return the record type.\r
+ */\r
+ public long getRecordType() {\r
+ return RecordTypes.SoundData.typeID;\r
+ }\r
+\r
+ /**\r
+ * Write the contents of the record back, so it can be written\r
+ * to disk.\r
+ *\r
+ * @param out the output stream to write to.\r
+ * @throws java.io.IOException if an error occurs.\r
+ */\r
+ public void writeOut(OutputStream out) throws IOException {\r
+ out.write(_header);\r
+ out.write(_data);\r
+ }\r
+}\r
public ObjectData[] getEmbeddedObjects() {
return _hslfSlideShow.getEmbeddedObjects();
}
+
+ /**
+ * Returns the data of all the embedded sounds in the SlideShow
+ */
+ public SoundData[] getSoundData() {
+ return SoundData.find(_documentRecord);
+ }
+
/**
* Return the current page size
*/
--- /dev/null
+/* ====================================================================\r
+ Licensed to the Apache Software Foundation (ASF) under one or more\r
+ contributor license agreements. See the NOTICE file distributed with\r
+ this work for additional information regarding copyright ownership.\r
+ The ASF licenses this file to You under the Apache License, Version 2.0\r
+ (the "License"); you may not use this file except in compliance with\r
+ the License. You may obtain a copy of the License at\r
+\r
+ http://www.apache.org/licenses/LICENSE-2.0\r
+\r
+ Unless required by applicable law or agreed to in writing, software\r
+ distributed under the License is distributed on an "AS IS" BASIS,\r
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ See the License for the specific language governing permissions and\r
+ limitations under the License.\r
+==================================================================== */\r
+package org.apache.poi.hslf.usermodel;\r
+\r
+import org.apache.poi.hslf.record.*;\r
+\r
+import java.util.ArrayList;\r
+\r
+/**\r
+ * A class that represents sound data embedded in a slide show.\r
+ *\r
+ * @author Yegor Kozlov\r
+ */\r
+public class SoundData {\r
+ /**\r
+ * The record that contains the object data.\r
+ */\r
+ private Sound _container;\r
+\r
+ /**\r
+ * Creates the object data wrapping the record that contains the sound data.\r
+ *\r
+ * @param container the record that contains the sound data.\r
+ */\r
+ public SoundData(Sound container) {\r
+ this._container = container;\r
+ }\r
+\r
+ /**\r
+ * Name of the sound (e.g. "crash")\r
+ *\r
+ * @return name of the sound\r
+ */\r
+ public String getSoundName(){\r
+ return _container.getSoundName();\r
+ }\r
+\r
+ /**\r
+ * Type of the sound (e.g. ".wav")\r
+ *\r
+ * @return type of the sound\r
+ */\r
+ public String getSoundType(){\r
+ return _container.getSoundType();\r
+ }\r
+\r
+ /**\r
+ * Gets an input stream which returns the binary of the sound data.\r
+ *\r
+ * @return the input stream which will contain the binary of the sound data.\r
+ */\r
+ public byte[] getData() {\r
+ return _container.getSoundData();\r
+ }\r
+\r
+ /**\r
+ * Find all sound records in the supplied Document records\r
+ *\r
+ * @param document the document to find in\r
+ * @return the array with the sound data\r
+ */\r
+ public static SoundData[] find(Document document){\r
+ ArrayList lst = new ArrayList();\r
+ Record[] ch = document.getChildRecords();\r
+ for (int i = 0; i < ch.length; i++) {\r
+ if(ch[i].getRecordType() == RecordTypes.SoundCollection.typeID){\r
+ RecordContainer col = (RecordContainer)ch[i];\r
+ Record[] sr = col.getChildRecords();\r
+ for (int j = 0; j < sr.length; j++) {\r
+ if(sr[j] instanceof Sound){\r
+ lst.add(new SoundData((Sound)sr[j]));\r
+ }\r
+ }\r
+ }\r
+\r
+ }\r
+ return (SoundData[])lst.toArray(new SoundData[lst.size()]);\r
+ }\r
+}\r
--- /dev/null
+\r
+/* ====================================================================\r
+ Licensed to the Apache Software Foundation (ASF) under one or more\r
+ contributor license agreements. See the NOTICE file distributed with\r
+ this work for additional information regarding copyright ownership.\r
+ The ASF licenses this file to You under the Apache License, Version 2.0\r
+ (the "License"); you may not use this file except in compliance with\r
+ the License. You may obtain a copy of the License at\r
+\r
+ http://www.apache.org/licenses/LICENSE-2.0\r
+\r
+ Unless required by applicable law or agreed to in writing, software\r
+ distributed under the License is distributed on an "AS IS" BASIS,\r
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ See the License for the specific language governing permissions and\r
+ limitations under the License.\r
+==================================================================== */\r
+ \r
+\r
+\r
+package org.apache.poi.hslf.record;\r
+\r
+\r
+import junit.framework.TestCase;\r
+import java.io.ByteArrayOutputStream;\r
+import java.io.File;\r
+import java.io.FileInputStream;\r
+import java.text.SimpleDateFormat;\r
+import java.util.ArrayList;\r
+import java.util.Date;\r
+import java.util.Arrays;\r
+\r
+import org.apache.poi.hslf.HSLFSlideShow;\r
+import org.apache.poi.hslf.usermodel.SlideShow;\r
+\r
+/**\r
+ * Tests Sound-related records: SoundCollection(2020), Sound(2022) and SoundData(2023)).\r
+ *\r
+ * @author Yegor Kozlov\r
+ */\r
+public class TestSound extends TestCase {\r
+ public void testRealFile() throws Exception {\r
+ String cwd = System.getProperty("HSLF.testdata.path");\r
+ FileInputStream is = new FileInputStream(new File(cwd, "sound.ppt"));\r
+ SlideShow ppt = new SlideShow(is);\r
+ is.close();\r
+\r
+ // Get the document\r
+ Document doc = ppt.getDocumentRecord();\r
+ SoundCollection soundCollection = null;\r
+ Record[] doc_ch = doc.getChildRecords();\r
+ for (int i = 0; i < doc_ch.length; i++) {\r
+ if(doc_ch[i] instanceof SoundCollection){\r
+ soundCollection = (SoundCollection)doc_ch[i];\r
+ break;\r
+ }\r
+ }\r
+ assertNotNull(soundCollection);\r
+\r
+ Sound sound = null;\r
+ Record[] sound_ch = soundCollection.getChildRecords();\r
+ int k = 0;\r
+ for (int i = 0; i < sound_ch.length; i++) {\r
+ if(sound_ch[i] instanceof Sound){\r
+ sound = (Sound)sound_ch[i];\r
+ k++;\r
+ }\r
+ }\r
+ assertNotNull(sound);\r
+ assertEquals(1, k);\r
+\r
+ assertEquals("ringin.wav", sound.getSoundName());\r
+ assertEquals(".WAV", sound.getSoundType());\r
+ assertNotNull(sound.getSoundData());\r
+\r
+ File f = new File(cwd, "ringin.wav");\r
+ int length = (int)f.length();\r
+ byte[] ref_data = new byte[length];\r
+ is = new FileInputStream(f);\r
+ is.read(ref_data);\r
+ is.close();\r
+\r
+ assertTrue(Arrays.equals(ref_data, sound.getSoundData()));\r
+\r
+ }\r
+}\r
--- /dev/null
+/* ====================================================================\r
+ Licensed to the Apache Software Foundation (ASF) under one or more\r
+ contributor license agreements. See the NOTICE file distributed with\r
+ this work for additional information regarding copyright ownership.\r
+ The ASF licenses this file to You under the Apache License, Version 2.0\r
+ (the "License"); you may not use this file except in compliance with\r
+ the License. You may obtain a copy of the License at\r
+\r
+ http://www.apache.org/licenses/LICENSE-2.0\r
+\r
+ Unless required by applicable law or agreed to in writing, software\r
+ distributed under the License is distributed on an "AS IS" BASIS,\r
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ See the License for the specific language governing permissions and\r
+ limitations under the License.\r
+==================================================================== */\r
+\r
+package org.apache.poi.hslf.usermodel;\r
+\r
+import org.apache.poi.hslf.*;\r
+import org.apache.poi.hslf.exceptions.HSLFException;\r
+import org.apache.poi.hslf.blip.*;\r
+import org.apache.poi.hslf.model.*;\r
+import junit.framework.TestCase;\r
+\r
+import java.io.*;\r
+import java.util.Arrays;\r
+\r
+/**\r
+ * Test reading sound data from a ppt\r
+ *\r
+ * @author Yegor Kozlov\r
+ */\r
+public class TestSoundData extends TestCase{\r
+\r
+ protected File cwd;\r
+\r
+ public void setUp() throws Exception {\r
+ cwd = new File(System.getProperty("HSLF.testdata.path"));\r
+ }\r
+\r
+ /**\r
+ * Read a reference sound file from disk and compare it from the data extracted from the slide show\r
+ */ \r
+ public void testSounds() throws Exception {\r
+ //read the reference sound file\r
+ File f = new File(cwd, "ringin.wav");\r
+ int length = (int)f.length();\r
+ byte[] ref_data = new byte[length];\r
+ FileInputStream is = new FileInputStream(f);\r
+ is.read(ref_data);\r
+ is.close();\r
+\r
+ is = new FileInputStream(new File(cwd, "sound.ppt"));\r
+ SlideShow ppt = new SlideShow(is);\r
+ is.close();\r
+\r
+ SoundData[] sound = ppt.getSoundData();\r
+ assertEquals("Expected 1 sound", 1, sound.length);\r
+\r
+ assertTrue(Arrays.equals(ref_data, sound[0].getData()));\r
+ }\r
+}\r