KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > poifs > filesystem > ReaderWriter


1
2 /* ====================================================================
3    Copyright 2002-2004 Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16 ==================================================================== */

17         
18
19 package org.apache.poi.poifs.filesystem;
20
21 import java.util.*;
22
23 import java.io.*;
24
25 import org.apache.poi.poifs.eventfilesystem.POIFSReader;
26 import org.apache.poi.poifs.eventfilesystem.POIFSReaderEvent;
27 import org.apache.poi.poifs.eventfilesystem.POIFSReaderListener;
28
29 /**
30  * Test (Proof of concept) program that employs the
31  * POIFSReaderListener and POIFSWriterListener interfaces
32  *
33  * @author Marc Johnson (mjohnson at apache dot org)
34  */

35
36 public class ReaderWriter
37     implements POIFSReaderListener, POIFSWriterListener
38 {
39     private POIFSFileSystem filesystem;
40     private DirectoryEntry root;
41
42     // keys are DocumentDescriptors, values are byte[]s
43
private Map dataMap;
44
45     /**
46      * Constructor ReaderWriter
47      *
48      *
49      * @param filesystem
50      *
51      */

52
53     ReaderWriter(final POIFSFileSystem filesystem)
54     {
55         this.filesystem = filesystem;
56         root = this.filesystem.getRoot();
57         dataMap = new HashMap();
58     }
59
60     /**
61      * Method main
62      *
63      *
64      * @param args
65      *
66      * @exception IOException
67      *
68      */

69
70     public static void main(String JavaDoc [] args)
71         throws IOException
72     {
73         if (args.length != 2)
74         {
75             System.err.println(
76                 "two arguments required: one input file name and one output file name");
77         }
78         else
79         {
80             POIFSReader reader = new POIFSReader();
81             POIFSFileSystem filesystem = new POIFSFileSystem();
82
83             reader.registerListener(new ReaderWriter(filesystem));
84             FileInputStream istream = new FileInputStream(args[ 0 ]);
85
86             reader.read(istream);
87             istream.close();
88             FileOutputStream ostream = new FileOutputStream(args[ 1 ]);
89
90             filesystem.writeFilesystem(ostream);
91             ostream.close();
92         }
93     }
94
95     /* ********** START implementation of POIFSReaderListener ********** */
96
97     /**
98      * Process a POIFSReaderEvent that this listener had registered
99      * for
100      *
101      * @param event the POIFSReaderEvent
102      */

103
104     public void processPOIFSReaderEvent(final POIFSReaderEvent event)
105     {
106         DocumentInputStream istream = event.getStream();
107         POIFSDocumentPath path = event.getPath();
108         String JavaDoc name = event.getName();
109
110         try
111         {
112             int size = istream.available();
113             byte[] data = new byte[ istream.available() ];
114
115             istream.read(data);
116             DocumentDescriptor descriptor = new DocumentDescriptor(path,
117                                                 name);
118
119             System.out.println("adding document: " + descriptor + " (" + size
120                                + " bytes)");
121             dataMap.put(descriptor, data);
122             int pathLength = path.length();
123             DirectoryEntry entry = root;
124
125             for (int k = 0; k < path.length(); k++)
126             {
127                 String JavaDoc componentName = path.getComponent(k);
128                 Entry nextEntry = null;
129
130                 try
131                 {
132                     nextEntry = entry.getEntry(componentName);
133                 }
134                 catch (FileNotFoundException ignored)
135                 {
136                     try
137                     {
138                         nextEntry = entry.createDirectory(componentName);
139                     }
140                     catch (IOException e)
141                     {
142                         System.out.println("Unable to create directory");
143                         e.printStackTrace();
144                         System.exit(1);
145                     }
146                 }
147                 entry = ( DirectoryEntry ) nextEntry;
148             }
149             entry.createDocument(name, size, this);
150         }
151         catch (IOException ignored)
152         {
153         }
154     }
155
156     /* ********** END implementation of POIFSReaderListener ********** */
157     /* ********** START implementation of POIFSWriterListener ********** */
158
159     /**
160      * Process a POIFSWriterEvent that this listener had registered
161      * for
162      *
163      * @param event the POIFSWriterEvent
164      */

165
166     public void processPOIFSWriterEvent(final POIFSWriterEvent event)
167     {
168         try
169         {
170             DocumentDescriptor descriptor =
171                 new DocumentDescriptor(event.getPath(), event.getName());
172
173             System.out.println("looking up document: " + descriptor + " ("
174                                + event.getLimit() + " bytes)");
175             event.getStream().write(( byte [] ) dataMap.get(descriptor));
176         }
177         catch (IOException e)
178         {
179             System.out.println("Unable to write document");
180             e.printStackTrace();
181             System.exit(1);
182         }
183     }
184
185     /* ********** END implementation of POIFSWriterListener ********** */
186 } // end public class ReaderWriter
187

188
Popular Tags