Package org.apache.poi.poifs.filesystem

Examples of org.apache.poi.poifs.filesystem.NPOIFSFileSystem


    @SuppressWarnings("static-access")
    @Test
    public void testUserFile() throws Exception {
        Biff8EncryptionKey.setCurrentUserPassword("abc");
        NPOIFSFileSystem fs = new NPOIFSFileSystem(samples.getSampleFile("xor-encryption-abc.xls"), true);
        HSSFWorkbook hwb = new HSSFWorkbook(fs.getRoot(), true);
       
        HSSFSheet sh = hwb.getSheetAt(0);
        assertEquals(1.0, sh.getRow(0).getCell(0).getNumericCellValue(), 0.0);
        assertEquals(2.0, sh.getRow(1).getCell(0).getNumericCellValue(), 0.0);
        assertEquals(3.0, sh.getRow(2).getCell(0).getNumericCellValue(), 0.0);

        fs.close();
    }
View Full Code Here


    );
      ext = new PublisherTextExtractor(docOPOIFS);
      assertEquals( SAMPLE_TEXT, ext.getText() );

      // And with NPOIFS
      NPOIFSFileSystem fs = new NPOIFSFileSystem(sample);
    HPBFDocument docNPOIFS = new HPBFDocument(
            fs
      );
    ext = new PublisherTextExtractor(docNPOIFS);
    assertEquals( SAMPLE_TEXT, ext.getText() );

   
    // Now a simpler file
    ext = new PublisherTextExtractor(
          new FileInputStream(simple)
    );
      assertEquals( SIMPLE_TEXT, ext.getText() );
      fs.close();
  }
View Full Code Here

    @SuppressWarnings("resource")
    public void testDifferentPOIFS() throws Exception {
       // Open the two filesystems
       DirectoryNode[] files = new DirectoryNode[2];
       files[0] = (new POIFSFileSystem(slTests.openResourceAsStream("basic_test_ppt_file.ppt"))).getRoot();
       NPOIFSFileSystem npoifsFileSystem = new NPOIFSFileSystem(slTests.getFile("basic_test_ppt_file.ppt"));
       files[1] = npoifsFileSystem.getRoot();
      
       // Open directly
       for(DirectoryNode dir : files) {
          PowerPointExtractor extractor = new PowerPointExtractor(dir);
          assertEquals(expectText, extractor.getText());
       }

       // Open via a HWPFDocument
       for(DirectoryNode dir : files) {
          HSLFSlideShow slideshow = new HSLFSlideShow(dir);
          PowerPointExtractor extractor = new PowerPointExtractor(slideshow);
          assertEquals(expectText, extractor.getText());
       }
      
       npoifsFileSystem.close();
    }
View Full Code Here

         * For brevity to boundary check is performed on the command-line
         * arguments. */
        File summaryFile = new File(args[0]);

        /* Open the POI filesystem. */
        NPOIFSFileSystem poifs = new NPOIFSFileSystem(summaryFile, false);

        /* Read the summary information. */
        DirectoryEntry dir = poifs.getRoot();
        SummaryInformation si;
        try
        {
            si = (SummaryInformation)PropertySetFactory.create(
                    dir, SummaryInformation.DEFAULT_STREAM_NAME);
        }
        catch (FileNotFoundException ex)
        {
            // There is no summary information yet. We have to create a new one
            si = PropertySetFactory.newSummaryInformation();
        }

        /* Change the author to "Rainer Klute". Any former author value will
         * be lost. If there has been no author yet, it will be created. */
        si.setAuthor("Rainer Klute");
        System.out.println("Author changed to " + si.getAuthor() + ".");


        /* Handling the document summary information is analogous to handling
         * the summary information. An additional feature, however, are the
         * custom properties. */

        /* Read the document summary information. */
        DocumentSummaryInformation dsi;
        try
        {
            dsi = (DocumentSummaryInformation)PropertySetFactory.create(
                    dir, DocumentSummaryInformation.DEFAULT_STREAM_NAME);
        }
        catch (FileNotFoundException ex)
        {
            /* There is no document summary information yet. We have to create a
             * new one. */
            dsi = PropertySetFactory.newDocumentSummaryInformation();
        }

        /* Change the category to "POI example". Any former category value will
         * be lost. If there has been no category yet, it will be created. */
        dsi.setCategory("POI example");
        System.out.println("Category changed to " + dsi.getCategory() + ".");

        /* Read the custom properties. If there are no custom properties yet,
         * the application has to create a new CustomProperties object. It will
         * serve as a container for custom properties. */
        CustomProperties customProperties = dsi.getCustomProperties();
        if (customProperties == null)
            customProperties = new CustomProperties();

        /* Insert some custom properties into the container. */
        customProperties.put("Key 1", "Value 1");
        customProperties.put("Schl\u00fcssel 2", "Wert 2");
        customProperties.put("Sample Number", new Integer(12345));
        customProperties.put("Sample Boolean", Boolean.TRUE);
        customProperties.put("Sample Date", new Date());

        /* Read a custom property. */
        Object value = customProperties.get("Sample Number");
        System.out.println("Custom Sample Number is now " + value);

        /* Write the custom properties back to the document summary
         * information. */
        dsi.setCustomProperties(customProperties);

        /* Write the summary information and the document summary information
         * to the POI filesystem. */
        si.write(dir, SummaryInformation.DEFAULT_STREAM_NAME);
        dsi.write(dir, DocumentSummaryInformation.DEFAULT_STREAM_NAME);

        /* Write the POI filesystem back to the original file. Please note that
         * in production code you should take care when write directly to the
         * origin, to make sure you don't loose things on error */
        poifs.writeFilesystem();
        poifs.close();
    }
View Full Code Here

       POIDataSamples docTests = POIDataSamples.getDocumentInstance();
      
       // Open the two filesystems
       DirectoryNode[] files = new DirectoryNode[2];
       files[0] = (new POIFSFileSystem(docTests.openResourceAsStream("test2.doc"))).getRoot();
       NPOIFSFileSystem npoifsFileSystem = new NPOIFSFileSystem(docTests.getFile("test2.doc"));
       files[1] = npoifsFileSystem.getRoot();
      
       // Open directly
       for(DirectoryNode dir : files) {
          WordExtractor extractor = new WordExtractor(dir);
          assertEquals(p_text1_block, extractor.getText());
       }

       // Open via a HWPFDocument
       for(DirectoryNode dir : files) {
          HWPFDocument doc = new HWPFDocument(dir);
          WordExtractor extractor = new WordExtractor(doc);
          assertEquals(p_text1_block, extractor.getText());
       }
      
       npoifsFileSystem.close();
    }
View Full Code Here

     *  without needing to stream in + out the whole kitchen sink
     */
    @Test
    public void inPlaceNPOIFSWrite() throws Exception {
        try {
            NPOIFSFileSystem fs = null;
            DirectoryEntry root = null;
            DocumentNode sinfDoc = null;
            DocumentNode dinfDoc = null;
            SummaryInformation sinf = null;
            DocumentSummaryInformation dinf = null;
           
            // We need to work on a File for in-place changes, so create a temp one
            final File copy = TempFile.createTempFile("Test-HPSF", "ole2");
            copy.deleteOnExit();
           
            // Copy a test file over to our temp location
            InputStream inp = _samples.openResourceAsStream("TestShiftJIS.doc");
            FileOutputStream out = new FileOutputStream(copy);
            IOUtils.copy(inp, out);
            inp.close();
            out.close();
           
           
            // Open the copy in read/write mode
            fs = new NPOIFSFileSystem(copy, false);
            root = fs.getRoot();
           
           
            // Read the properties in there
            sinfDoc = (DocumentNode)root.getEntry(SummaryInformation.DEFAULT_STREAM_NAME);
            dinfDoc = (DocumentNode)root.getEntry(DocumentSummaryInformation.DEFAULT_STREAM_NAME);
   
            sinf = (SummaryInformation)PropertySetFactory.create(new NDocumentInputStream(sinfDoc));
            assertEquals(131077, sinf.getOSVersion());
           
            dinf = (DocumentSummaryInformation)PropertySetFactory.create(new NDocumentInputStream(dinfDoc));
            assertEquals(131077, dinf.getOSVersion());
           
           
            // Check they start as we expect
            assertEquals("Reiichiro Hori", sinf.getAuthor());
            assertEquals("Microsoft Word 9.0", sinf.getApplicationName());
            assertEquals("\u7b2c1\u7ae0", sinf.getTitle());
           
            assertEquals("", dinf.getCompany());
            assertEquals(null, dinf.getManager());
           
           
            // Do an in-place replace via an InputStream
            new NPOIFSDocument(sinfDoc).replaceContents(sinf.toInputStream());
            new NPOIFSDocument(dinfDoc).replaceContents(dinf.toInputStream());
           
           
            // Check it didn't get changed
            sinfDoc = (DocumentNode)root.getEntry(SummaryInformation.DEFAULT_STREAM_NAME);
            dinfDoc = (DocumentNode)root.getEntry(DocumentSummaryInformation.DEFAULT_STREAM_NAME);
           
            sinf = (SummaryInformation)PropertySetFactory.create(new NDocumentInputStream(sinfDoc));
            assertEquals(131077, sinf.getOSVersion());
           
            dinf = (DocumentSummaryInformation)PropertySetFactory.create(new NDocumentInputStream(dinfDoc));
            assertEquals(131077, dinf.getOSVersion());
   
           
            // Start again!
            fs.close();
            inp = _samples.openResourceAsStream("TestShiftJIS.doc");
            out = new FileOutputStream(copy);
            IOUtils.copy(inp, out);
            inp.close();
            out.close();
           
            fs = new NPOIFSFileSystem(copy, false);
            root = fs.getRoot();
           
            // Read the properties in once more
            sinfDoc = (DocumentNode)root.getEntry(SummaryInformation.DEFAULT_STREAM_NAME);
            dinfDoc = (DocumentNode)root.getEntry(DocumentSummaryInformation.DEFAULT_STREAM_NAME);
   
            sinf = (SummaryInformation)PropertySetFactory.create(new NDocumentInputStream(sinfDoc));
            assertEquals(131077, sinf.getOSVersion());
           
            dinf = (DocumentSummaryInformation)PropertySetFactory.create(new NDocumentInputStream(dinfDoc));
            assertEquals(131077, dinf.getOSVersion());
           
           
            // Have them write themselves in-place with no changes, as an OutputStream
            sinf.write(new NDocumentOutputStream(sinfDoc));
            dinf.write(new NDocumentOutputStream(dinfDoc));
           
            // And also write to some bytes for checking
            ByteArrayOutputStream sinfBytes = new ByteArrayOutputStream();
            sinf.write(sinfBytes);
            ByteArrayOutputStream dinfBytes = new ByteArrayOutputStream();
            dinf.write(dinfBytes);
           
           
            // Check that the filesystem can give us back the same bytes
            sinfDoc = (DocumentNode)root.getEntry(SummaryInformation.DEFAULT_STREAM_NAME);
            dinfDoc = (DocumentNode)root.getEntry(DocumentSummaryInformation.DEFAULT_STREAM_NAME);
   
            byte[] sinfData = IOUtils.toByteArray(new NDocumentInputStream(sinfDoc));
            byte[] dinfData = IOUtils.toByteArray(new NDocumentInputStream(dinfDoc));
            assertThat(sinfBytes.toByteArray(), equalTo(sinfData));
            assertThat(dinfBytes.toByteArray(), equalTo(dinfData));
   
           
            // Read back in as-is
            sinf = (SummaryInformation)PropertySetFactory.create(new NDocumentInputStream(sinfDoc));
            assertEquals(131077, sinf.getOSVersion());
           
            dinf = (DocumentSummaryInformation)PropertySetFactory.create(new NDocumentInputStream(dinfDoc));
            assertEquals(131077, dinf.getOSVersion());
           
            assertEquals("Reiichiro Hori", sinf.getAuthor());
            assertEquals("Microsoft Word 9.0", sinf.getApplicationName());
            assertEquals("\u7b2c1\u7ae0", sinf.getTitle());
           
            assertEquals("", dinf.getCompany());
            assertEquals(null, dinf.getManager());
           
   
            // Now alter a few of them
            sinf.setAuthor("Changed Author");
            sinf.setTitle("Le titre \u00e9tait chang\u00e9");
            dinf.setManager("Changed Manager");
           
           
            // Save this into the filesystem
            sinf.write(new NDocumentOutputStream(sinfDoc));
            dinf.write(new NDocumentOutputStream(dinfDoc));
           
           
            // Read them back in again
            sinfDoc = (DocumentNode)root.getEntry(SummaryInformation.DEFAULT_STREAM_NAME);
            sinf = (SummaryInformation)PropertySetFactory.create(new NDocumentInputStream(sinfDoc));
            assertEquals(131077, sinf.getOSVersion());
           
            dinfDoc = (DocumentNode)root.getEntry(DocumentSummaryInformation.DEFAULT_STREAM_NAME);
            dinf = (DocumentSummaryInformation)PropertySetFactory.create(new NDocumentInputStream(dinfDoc));
            assertEquals(131077, dinf.getOSVersion());
   
            assertEquals("Changed Author", sinf.getAuthor());
            assertEquals("Microsoft Word 9.0", sinf.getApplicationName());
            assertEquals("Le titre \u00e9tait chang\u00e9", sinf.getTitle());
           
            assertEquals("", dinf.getCompany());
            assertEquals("Changed Manager", dinf.getManager());
   
           
            // Close the whole filesystem, and open it once more
            fs.writeFilesystem();
            fs.close();
           
            fs = new NPOIFSFileSystem(copy);
            root = fs.getRoot();
           
            // Re-check on load
            sinfDoc = (DocumentNode)root.getEntry(SummaryInformation.DEFAULT_STREAM_NAME);
            sinf = (SummaryInformation)PropertySetFactory.create(new NDocumentInputStream(sinfDoc));
            assertEquals(131077, sinf.getOSVersion());
           
            dinfDoc = (DocumentNode)root.getEntry(DocumentSummaryInformation.DEFAULT_STREAM_NAME);
            dinf = (DocumentSummaryInformation)PropertySetFactory.create(new NDocumentInputStream(dinfDoc));
            assertEquals(131077, dinf.getOSVersion());
   
            assertEquals("Changed Author", sinf.getAuthor());
            assertEquals("Microsoft Word 9.0", sinf.getApplicationName());
            assertEquals("Le titre \u00e9tait chang\u00e9", sinf.getTitle());
           
            assertEquals("", dinf.getCompany());
            assertEquals("Changed Manager", dinf.getManager());
           
           
            // Tidy up
            fs.close();
            copy.delete();
        } catch (FileNotFoundException e) {
            // On Windows this might always fail, as the nio classes
            // leave memory mapped buffers active, even when the corresponding channel is closed
            // The buffers are closed on garbage-collection (but System.gc() can't be forced)
View Full Code Here

     */
    public void test51671() throws Exception
    {
        InputStream is = POIDataSamples.getDocumentInstance()
                .openResourceAsStream( "empty.doc" );
        NPOIFSFileSystem npoifsFileSystem = new NPOIFSFileSystem( is );
        HWPFDocument hwpfDocument = new HWPFDocument(
                npoifsFileSystem.getRoot() );
        hwpfDocument.write( new ByteArrayOutputStream() );
    }
View Full Code Here

    @Test
    public void differentPOIFS() throws Exception {
       // Open the two filesystems
       DirectoryNode[] files = new DirectoryNode[2];
       files[0] = (new POIFSFileSystem(HSSFTestDataSamples.openSampleFileStream("Simple.xls"))).getRoot();
       NPOIFSFileSystem npoifsFileSystem = new NPOIFSFileSystem(HSSFTestDataSamples.getSampleFile("Simple.xls"));
       try {
           files[1] = npoifsFileSystem.getRoot();
          
           // Open without preserving nodes
           for(DirectoryNode dir : files) {
              HSSFWorkbook workbook = new HSSFWorkbook(dir, false);
              HSSFSheet sheet = workbook.getSheetAt(0);
              HSSFCell cell = sheet.getRow(0).getCell(0);
              assertEquals("replaceMe", cell .getRichStringCellValue().getString());
           }
   
           // Now re-check with preserving
           for(DirectoryNode dir : files) {
              HSSFWorkbook workbook = new HSSFWorkbook(dir, true);
              HSSFSheet sheet = workbook.getSheetAt(0);
              HSSFCell cell = sheet.getRow(0).getCell(0);
              assertEquals("replaceMe", cell .getRichStringCellValue().getString());
           }
       } finally {
           npoifsFileSystem.close();
       }
    }
View Full Code Here

    @Test
    public void wordDocEmbeddedInXls() throws IOException {
       // Open the two filesystems
       DirectoryNode[] files = new DirectoryNode[2];
       files[0] = (new POIFSFileSystem(HSSFTestDataSamples.openSampleFileStream("WithEmbeddedObjects.xls"))).getRoot();
       NPOIFSFileSystem npoifsFileSystem = new NPOIFSFileSystem(HSSFTestDataSamples.getSampleFile("WithEmbeddedObjects.xls"));
       try {
           files[1] = npoifsFileSystem.getRoot();
          
           // Check the embedded parts
           for(DirectoryNode root : files) {
              HSSFWorkbook hw = new HSSFWorkbook(root, true);
              List<HSSFObjectData> objects = hw.getAllEmbeddedObjects();
              boolean found = false;
              for (int i = 0; i < objects.size(); i++) {
                 HSSFObjectData embeddedObject = objects.get(i);
                 if (embeddedObject.hasDirectoryEntry()) {
                    DirectoryEntry dir = embeddedObject.getDirectory();
                    if (dir instanceof DirectoryNode) {
                       DirectoryNode dNode = (DirectoryNode)dir;
                       if (hasEntry(dNode,"WordDocument")) {
                          found = true;
                       }
                    }
                 }
              }
              assertTrue(found);
           }
       } finally {
           npoifsFileSystem.close();
       }
    }
View Full Code Here

     */
    @Test
    public void writeWorkbookFromNPOIFS() throws IOException {
       InputStream is = HSSFTestDataSamples.openSampleFileStream("WithEmbeddedObjects.xls");
       try {
           NPOIFSFileSystem fs = new NPOIFSFileSystem(is);
           try {
               // Start as NPOIFS
               HSSFWorkbook wb = new HSSFWorkbook(fs.getRoot(), true);
               assertEquals(3, wb.getNumberOfSheets());
               assertEquals("Root xls", wb.getSheetAt(0).getRow(0).getCell(0).getStringCellValue());

               // Will switch to POIFS
               wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
               assertEquals(3, wb.getNumberOfSheets());
               assertEquals("Root xls", wb.getSheetAt(0).getRow(0).getCell(0).getStringCellValue());
           } finally {
             fs.close();
           }
       } finally {
         is.close();
       }
    }
View Full Code Here

TOP

Related Classes of org.apache.poi.poifs.filesystem.NPOIFSFileSystem

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.