You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TestOle10Native.java 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.poifs.filesystem;
  16. import static org.hamcrest.core.IsEqual.equalTo;
  17. import static org.junit.Assert.assertEquals;
  18. import static org.junit.Assert.assertThat;
  19. import java.io.ByteArrayOutputStream;
  20. import java.io.File;
  21. import java.io.IOException;
  22. import java.io.InputStream;
  23. import java.util.ArrayList;
  24. import java.util.Iterator;
  25. import java.util.List;
  26. import org.apache.poi.POIDataSamples;
  27. import org.apache.poi.util.IOUtils;
  28. import org.junit.Test;
  29. public class TestOle10Native {
  30. private static final POIDataSamples dataSamples = POIDataSamples.getPOIFSInstance();
  31. @Test
  32. public void testOleNative() throws IOException, Ole10NativeException {
  33. POIFSFileSystem fs = new POIFSFileSystem(dataSamples.openResourceAsStream("oleObject1.bin"));
  34. Ole10Native ole = Ole10Native.createFromEmbeddedOleObject(fs);
  35. assertEquals("File1.svg", ole.getLabel());
  36. assertEquals("D:\\Documents and Settings\\rsc\\My Documents\\file1.svg", ole.getCommand());
  37. }
  38. @Test
  39. public void testFiles() throws IOException, Ole10NativeException {
  40. File files[] = {
  41. // bug 51891
  42. POIDataSamples.getPOIFSInstance().getFile("multimedia.doc"),
  43. // tika bug 1072
  44. POIDataSamples.getPOIFSInstance().getFile("20-Force-on-a-current-S00.doc"),
  45. // other files containing ole10native records ...
  46. POIDataSamples.getDocumentInstance().getFile("Bug53380_3.doc"),
  47. POIDataSamples.getDocumentInstance().getFile("Bug47731.doc")
  48. };
  49. for (File f : files) {
  50. NPOIFSFileSystem fs = new NPOIFSFileSystem(f, true);
  51. List<Entry> entries = new ArrayList<Entry>();
  52. findOle10(entries, fs.getRoot(), "/", "");
  53. for (Entry e : entries) {
  54. ByteArrayOutputStream bosExp = new ByteArrayOutputStream();
  55. InputStream is = ((DirectoryNode)e.getParent()).createDocumentInputStream(e);
  56. IOUtils.copy(is,bosExp);
  57. is.close();
  58. Ole10Native ole = Ole10Native.createFromEmbeddedOleObject((DirectoryNode)e.getParent());
  59. ByteArrayOutputStream bosAct = new ByteArrayOutputStream();
  60. ole.writeOut(bosAct);
  61. assertThat(bosExp.toByteArray(), equalTo(bosAct.toByteArray()));
  62. }
  63. fs.close();
  64. }
  65. }
  66. /*
  67. void searchOle10Files() throws Exception {
  68. File dir = new File("test-data/document");
  69. for (File file : dir.listFiles(new FileFilter(){
  70. public boolean accept(File pathname) {
  71. return pathname.getName().endsWith("doc");
  72. }
  73. })) {
  74. NPOIFSFileSystem fs = new NPOIFSFileSystem(file, true);
  75. findOle10(null, fs.getRoot(), "/", file.getName());
  76. fs.close();
  77. }
  78. }*/
  79. void findOle10(List<Entry> entries, DirectoryNode dn, String path, String filename) {
  80. Iterator<Entry> iter = dn.getEntries();
  81. while (iter.hasNext()) {
  82. Entry e = iter.next();
  83. if (Ole10Native.OLE10_NATIVE.equals(e.getName())) {
  84. if (entries != null) entries.add(e);
  85. // System.out.println(filename+" : "+path);
  86. } else if (e.isDirectoryEntry()) {
  87. findOle10(entries, (DirectoryNode)e, path+e.getName()+"/", filename);
  88. }
  89. }
  90. }
  91. }