You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@poi.apache.org by Praveen Mathew <pr...@in.ibm.com> on 2003/06/01 02:16:26 UTC

Re: HPSF (Was: Re: HDF)

Hi all,
             With the help of you all I could make a simple Word Writer
which reads main, table & summary streams from exixting Doc files & writes
them to a different Doc file.
Ryan it was "1Table" ..."0Table" gave me an invalid entry error.
             Now If we can create these streams seperately instead of
reading from existing Docs....its Done!!!

For dealing with summary stream, i have followed the instructions Rainer
gave.
But I think we can do it in another way too.
may be we can use createDocumentInputstream() and createDocument() methods
with the string name "SummaryInformation"..

Here is the code...

import java.util.ArrayList;
import java.io.*;
import java.util.List;
import java.util.TreeSet;


import org.apache.poi.hdf.model.hdftypes.*;
import org.apache.poi.hdf.event.HDFLowLevelParsingListener;
import org.apache.poi.hdf.model.util.BTreeSet;
import org.apache.poi.hdf.model.util.ParsingState;

import org.apache.poi.poifs.eventfilesystem.*;

import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.poifs.filesystem.POIFSDocument;
import org.apache.poi.poifs.filesystem.DocumentEntry;
import org.apache.poi.poifs.filesystem.DirectoryEntry;
import org.apache.poi.util.LittleEndian;




/**
 * WordWriter reads specicfic streams from existing Doc file
   & writes them to a new Doc file
 * @author  praveen
 */
public class WordWriter
{

 /** OLE stuff*/
    private POIFSFileSystem _filesystemRead;
    private POIFSFileSystem _filesystemWrite;

    /** main document stream buffer*/
    byte[] _mainDocument;
    /** table stream buffer*/
    byte[] _tableBuffer;
    /** summary stream buffer*/
    static byte[] _summaryBuffer;

    //InputStream istream;
    String outfile;
    ByteArrayInputStream baisMs, baisTs, baisSs;


      public static void main(String args[])
    {
      try
      {
        WordWriter w = new WordWriter(new FileInputStream("c:
\\test.doc"),args[0]);
        w.readMainStream();
        w.readTableStream();
        w.readSummaryStream();
        w.writeWordStream();
      }
      catch(Throwable t)
      {
        t.printStackTrace();
      }
    }

    static class MyPOIFSReaderListener implements POIFSReaderListener
      {
          public void processPOIFSReaderEvent(POIFSReaderEvent event)
          {
                  try
                  {
                  _summaryBuffer = new byte[event.getStream().available()];
                        event.getStream().read(_summaryBuffer);
                  }
                  catch(Throwable t)
                  {
                        t.printStackTrace();
                  }
            }
      }

    public WordWriter(InputStream istream,String outfilename) throws
IOException
      {
            _filesystemRead = new POIFSFileSystem(istream);
            outfile = new String(outfilename);

      }

      public void readMainStream() throws IOException
      {


            DocumentEntry headerProps =
                  (DocumentEntry)_filesystemRead.getRoot().getEntry
("WordDocument");

            _mainDocument = new byte[headerProps.getSize()];
            //_mainDocument = new byte
[_filesystemRead.createDocumentInputStream("WordDocument").available()];
            _filesystemRead.createDocumentInputStream("WordDocument").read
(_mainDocument);

      }

      public void readTableStream() throws IOException
      {
            String tablename = null;

            tablename ="1Table";

            DocumentEntry tableEntry = (DocumentEntry)
_filesystemRead.getRoot().getEntry(tablename);

            //load the table stream into a buffer
            int size = tableEntry.getSize();
            _tableBuffer = new byte[size];
            //_tableBuffer = new byte
[_filesystemRead.createDocumentInputStream(tablename).available()];
        _filesystemRead.createDocumentInputStream(tablename).read
(_tableBuffer);
      }

      public void readSummaryStream() throws IOException
      {

            POIFSReader r = new POIFSReader();
            r.registerListener(new MyPOIFSReaderListener(),
                                       "\005SummaryInformation");
            r.read(new FileInputStream("c:\\art.doc"));
      }

      public void writeWordStream() throws IOException
      {
            baisMs = new ByteArrayInputStream(_mainDocument);
            baisTs = new ByteArrayInputStream(_tableBuffer);
            baisSs = new ByteArrayInputStream(_summaryBuffer);

            _filesystemWrite = new POIFSFileSystem();

            DirectoryEntry directory = _filesystemWrite.getRoot();

            directory.createDocument("WordDocument", baisMs);
            directory.createDocument("1Table", baisTs);
            System.out.println(outfile);

            FileOutputStream ostream = new FileOutputStream(outfile);

            _filesystemWrite.writeFilesystem(ostream);
            //_filesystemRead.writeFilesystem(ostream);

            baisMs.close();
            baisTs.close();
            ostream.close();
      }
}


Thanks & Regards
Praveen




                                                                                                           
                      "Ryan Ackley"                                                                        
                      <sackley@cfl.rr.c        To:       "POI Developers List"                             
                      om>                       <po...@jakarta.apache.org>                               
                                               cc:                                                         
                      05/30/03 07:27 PM        Subject:  Re: HPSF (Was: Re: HDF)                           
                      Please respond to                                                                    
                      "POI Developers                                                                      
                      List"                                                                                
                                                                                                           
                                                                                                           



I am not sure I completely understand you. You would name the main stream
"WordDocument", and the table stream either "0Table" or "1Table". You have
to put the one you plan on using in the FIB. It defaults to "0Table".  To
create 2 streams, you simply call createDocument twice.

Ryan

----- Original Message -----
From: "Praveen Mathew" <pr...@in.ibm.com>
To: "POI Developers List" <po...@jakarta.apache.org>
Sent: Friday, May 30, 2003 9:44 AM
Subject: Re: HPSF (Was: Re: HDF)



Hi,

       After thinking on the lines of Rainer's suggestion , I have made a
plan. Please tell me if this will work?

According to HDFObjectFactory.java  it seems that the main stream is
collected in _mainDocument
and the table stream is collected in _tableBuffer. and all the data
structures created ( FIB, TextPiece, ChpX etc..) are created from these
streams.

I think to make a PoifsFilesystem object & read both these streams in. from
an existing Doc file.
now create a different PoifsFilesystem object and using createDocument()
method create two
entries for mainstream & tablestream  created using the first
PoifsFilesystem  object.
Then write the 2nd  PoifsFilesystem object to disk using writeFilesystem().

My doubt here is using the 2nd PoifsFilesystem  object how will I create 2
entries for the streams
in the root directory??
what should be the string name i have to give to createDocument() method
for 1. mainstream
      2. tablestream

please do help...................


Thanks & Regards
Praveen






                      Rainer Klute
                      <klute@rainer-klu        To:       "POI Developers
List"
                      te.de>
<po...@jakarta.apache.org>
                                               cc:
                      05/30/03 03:37 PM        Subject:  Re: HPSF (Was: Re:
HDF)
                      Please respond to
                      "POI Developers
                      List"





>Thanks Rainer for your detailed explanation for Summarystream!!

Well, I wouldn't call it detailed, but I hope you can get along
with it.

Best regards
Rainer Klute
                           Rainer Klute IT-Consulting GmbH
  Dipl.-Inform.
  Rainer Klute             E-Mail:  klute@rainer-klute.de
  Körner Grund 24          Telefon: +49 172 2324824
D-44143 Dortmund           Telefax: +49 231 5349423


---------------------------------------------------------------------
To unsubscribe, e-mail: poi-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: poi-dev-help@jakarta.apache.org






---------------------------------------------------------------------
To unsubscribe, e-mail: poi-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: poi-dev-help@jakarta.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: poi-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: poi-dev-help@jakarta.apache.org






Re: HPSF (Was: Re: HDF)

Posted by Ryan Ackley <sa...@cfl.rr.com>.
> Now If we can create these streams seperately instead of
> reading from existing Docs....its Done!!!

Wow, this is a real breakthough

Ryan

----- Original Message ----- 
From: "Praveen Mathew" <pr...@in.ibm.com>
To: "POI Developers List" <po...@jakarta.apache.org>
Sent: Saturday, May 31, 2003 8:16 PM
Subject: Re: HPSF (Was: Re: HDF)



Hi all,
             With the help of you all I could make a simple Word Writer
which reads main, table & summary streams from exixting Doc files & writes
them to a different Doc file.
Ryan it was "1Table" ..."0Table" gave me an invalid entry error.
             Now If we can create these streams seperately instead of
reading from existing Docs....its Done!!!

For dealing with summary stream, i have followed the instructions Rainer
gave.
But I think we can do it in another way too.
may be we can use createDocumentInputstream() and createDocument() methods
with the string name "SummaryInformation"..

Here is the code...

import java.util.ArrayList;
import java.io.*;
import java.util.List;
import java.util.TreeSet;


import org.apache.poi.hdf.model.hdftypes.*;
import org.apache.poi.hdf.event.HDFLowLevelParsingListener;
import org.apache.poi.hdf.model.util.BTreeSet;
import org.apache.poi.hdf.model.util.ParsingState;

import org.apache.poi.poifs.eventfilesystem.*;

import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.poifs.filesystem.POIFSDocument;
import org.apache.poi.poifs.filesystem.DocumentEntry;
import org.apache.poi.poifs.filesystem.DirectoryEntry;
import org.apache.poi.util.LittleEndian;




/**
 * WordWriter reads specicfic streams from existing Doc file
   & writes them to a new Doc file
 * @author  praveen
 */
public class WordWriter
{

 /** OLE stuff*/
    private POIFSFileSystem _filesystemRead;
    private POIFSFileSystem _filesystemWrite;

    /** main document stream buffer*/
    byte[] _mainDocument;
    /** table stream buffer*/
    byte[] _tableBuffer;
    /** summary stream buffer*/
    static byte[] _summaryBuffer;

    //InputStream istream;
    String outfile;
    ByteArrayInputStream baisMs, baisTs, baisSs;


      public static void main(String args[])
    {
      try
      {
        WordWriter w = new WordWriter(new FileInputStream("c:
\\test.doc"),args[0]);
        w.readMainStream();
        w.readTableStream();
        w.readSummaryStream();
        w.writeWordStream();
      }
      catch(Throwable t)
      {
        t.printStackTrace();
      }
    }

    static class MyPOIFSReaderListener implements POIFSReaderListener
      {
          public void processPOIFSReaderEvent(POIFSReaderEvent event)
          {
                  try
                  {
                  _summaryBuffer = new byte[event.getStream().available()];
                        event.getStream().read(_summaryBuffer);
                  }
                  catch(Throwable t)
                  {
                        t.printStackTrace();
                  }
            }
      }

    public WordWriter(InputStream istream,String outfilename) throws
IOException
      {
            _filesystemRead = new POIFSFileSystem(istream);
            outfile = new String(outfilename);

      }

      public void readMainStream() throws IOException
      {


            DocumentEntry headerProps =
                  (DocumentEntry)_filesystemRead.getRoot().getEntry
("WordDocument");

            _mainDocument = new byte[headerProps.getSize()];
            //_mainDocument = new byte
[_filesystemRead.createDocumentInputStream("WordDocument").available()];
            _filesystemRead.createDocumentInputStream("WordDocument").read
(_mainDocument);

      }

      public void readTableStream() throws IOException
      {
            String tablename = null;

            tablename ="1Table";

            DocumentEntry tableEntry = (DocumentEntry)
_filesystemRead.getRoot().getEntry(tablename);

            //load the table stream into a buffer
            int size = tableEntry.getSize();
            _tableBuffer = new byte[size];
            //_tableBuffer = new byte
[_filesystemRead.createDocumentInputStream(tablename).available()];
        _filesystemRead.createDocumentInputStream(tablename).read
(_tableBuffer);
      }

      public void readSummaryStream() throws IOException
      {

            POIFSReader r = new POIFSReader();
            r.registerListener(new MyPOIFSReaderListener(),
                                       "\005SummaryInformation");
            r.read(new FileInputStream("c:\\art.doc"));
      }

      public void writeWordStream() throws IOException
      {
            baisMs = new ByteArrayInputStream(_mainDocument);
            baisTs = new ByteArrayInputStream(_tableBuffer);
            baisSs = new ByteArrayInputStream(_summaryBuffer);

            _filesystemWrite = new POIFSFileSystem();

            DirectoryEntry directory = _filesystemWrite.getRoot();

            directory.createDocument("WordDocument", baisMs);
            directory.createDocument("1Table", baisTs);
            System.out.println(outfile);

            FileOutputStream ostream = new FileOutputStream(outfile);

            _filesystemWrite.writeFilesystem(ostream);
            //_filesystemRead.writeFilesystem(ostream);

            baisMs.close();
            baisTs.close();
            ostream.close();
      }
}


Thanks & Regards
Praveen





                      "Ryan Ackley"
                      <sackley@cfl.rr.c        To:       "POI Developers
List"
                      om>                       <po...@jakarta.apache.org>
                                               cc:
                      05/30/03 07:27 PM        Subject:  Re: HPSF (Was: Re:
HDF)
                      Please respond to
                      "POI Developers
                      List"





I am not sure I completely understand you. You would name the main stream
"WordDocument", and the table stream either "0Table" or "1Table". You have
to put the one you plan on using in the FIB. It defaults to "0Table".  To
create 2 streams, you simply call createDocument twice.

Ryan

----- Original Message -----
From: "Praveen Mathew" <pr...@in.ibm.com>
To: "POI Developers List" <po...@jakarta.apache.org>
Sent: Friday, May 30, 2003 9:44 AM
Subject: Re: HPSF (Was: Re: HDF)



Hi,

       After thinking on the lines of Rainer's suggestion , I have made a
plan. Please tell me if this will work?

According to HDFObjectFactory.java  it seems that the main stream is
collected in _mainDocument
and the table stream is collected in _tableBuffer. and all the data
structures created ( FIB, TextPiece, ChpX etc..) are created from these
streams.

I think to make a PoifsFilesystem object & read both these streams in. from
an existing Doc file.
now create a different PoifsFilesystem object and using createDocument()
method create two
entries for mainstream & tablestream  created using the first
PoifsFilesystem  object.
Then write the 2nd  PoifsFilesystem object to disk using writeFilesystem().

My doubt here is using the 2nd PoifsFilesystem  object how will I create 2
entries for the streams
in the root directory??
what should be the string name i have to give to createDocument() method
for 1. mainstream
      2. tablestream

please do help...................


Thanks & Regards
Praveen






                      Rainer Klute
                      <klute@rainer-klu        To:       "POI Developers
List"
                      te.de>
<po...@jakarta.apache.org>
                                               cc:
                      05/30/03 03:37 PM        Subject:  Re: HPSF (Was: Re:
HDF)
                      Please respond to
                      "POI Developers
                      List"





>Thanks Rainer for your detailed explanation for Summarystream!!

Well, I wouldn't call it detailed, but I hope you can get along
with it.

Best regards
Rainer Klute
                           Rainer Klute IT-Consulting GmbH
  Dipl.-Inform.
  Rainer Klute             E-Mail:  klute@rainer-klute.de
  Körner Grund 24          Telefon: +49 172 2324824
D-44143 Dortmund           Telefax: +49 231 5349423


---------------------------------------------------------------------
To unsubscribe, e-mail: poi-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: poi-dev-help@jakarta.apache.org






---------------------------------------------------------------------
To unsubscribe, e-mail: poi-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: poi-dev-help@jakarta.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: poi-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: poi-dev-help@jakarta.apache.org






---------------------------------------------------------------------
To unsubscribe, e-mail: poi-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: poi-dev-help@jakarta.apache.org