KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > contrib > poibrowser > TreeReaderListener


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.contrib.poibrowser;
20
21 import java.io.IOException JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.Map JavaDoc;
24
25 import javax.swing.tree.DefaultMutableTreeNode JavaDoc;
26 import javax.swing.tree.MutableTreeNode JavaDoc;
27
28 import org.apache.poi.hpsf.HPSFException;
29 import org.apache.poi.poifs.eventfilesystem.POIFSReaderEvent;
30 import org.apache.poi.poifs.eventfilesystem.POIFSReaderListener;
31 import org.apache.poi.poifs.filesystem.DocumentInputStream;
32 import org.apache.poi.poifs.filesystem.POIFSDocumentPath;
33
34 /**
35  * <p>Organizes document information in a tree model in order to be
36  * e.g. displayed in a Swing {@link javax.swing.JTree}. An instance of this
37  * class is created with a root tree node ({@link MutableTreeNode}) and
38  * registered as a {@link POIFSReaderListener} with a {@link
39  * org.apache.poi.poifs.eventfilesystem.POIFSReader}. While the latter processes
40  * a POI filesystem it calls this class' {@link #processPOIFSReaderEvent} for
41  * each document it has been registered for. This method appends the document it
42  * processes at the appropriate position into the tree rooted at the
43  * above mentioned root tree node.</p>
44  *
45  * <p>The root tree node should be the root tree node of a {@link
46  * javax.swing.tree.TreeModel}.</p>
47  *
48  * <p>A top-level element in the tree model, i.e. an immediate child
49  * node of the root node, describes a POI filesystem as such. It is
50  * suggested to use the file's name (as seen by the operating system)
51  * but it could be any other string.</p>
52  *
53  * <p>The value of a tree node is a {@link DocumentDescriptor}. Unlike
54  * a {@link org.apache.poi.poifs.filesystem.POIFSDocument} which may be as heavy
55  * as many megabytes, an instance of {@link DocumentDescriptor} is a
56  * light-weight object and contains only some meta-information about a
57  * document.</p>
58  *
59  * @author Rainer Klute <a
60  * HREF="mailto:klute@rainer-klute.de">&lt;klute@rainer-klute.de&gt;</a>
61  * @version $Id: TreeReaderListener.java,v 1.6 2004/04/09 13:05:08 glens Exp $
62  * @since 2002-01-24
63  */

64 public class TreeReaderListener implements POIFSReaderListener
65 {
66
67     /**
68      * <p>The tree's root node. POI filesystems get attached to this
69      * node as children.</p>
70      */

71     protected MutableTreeNode JavaDoc rootNode;
72
73     /**
74      * <p>Maps filenames and POI document paths to their associated
75      * tree nodes.</p>
76      */

77     protected Map JavaDoc pathToNode;
78
79     /**
80      * <p>The name of the file this {@link TreeReaderListener}
81      * processes. It is used to identify a top-level element in the
82      * tree. Alternatively any other string can be used. It is just a
83      * label which should identify a POI filesystem.</p>
84      */

85     protected String JavaDoc filename;
86
87
88
89     /**
90      * <p>Creates a {@link TreeReaderListener} which should then be
91      * registered with a
92      * {@link org.apache.poi.poifs.eventfilesystem.POIFSReader}.</p>
93      *
94      * @param filename The name of the POI filesystem, i.e. the name
95      * of the file the POI filesystem resides in. Alternatively any
96      * other string can be used.
97      *
98      * @param rootNode All document information will be attached as
99      * descendands to this tree node.
100      */

101     public TreeReaderListener(final String JavaDoc filename,
102                               final MutableTreeNode JavaDoc rootNode)
103     {
104         this.filename = filename;
105         this.rootNode = rootNode;
106         pathToNode = new HashMap JavaDoc(15); // Should be a reasonable guess.
107
}
108
109
110
111     /** <p>The number of bytes to dump.</p> */
112     private int nrOfBytes = 50;
113
114     public void setNrOfBytes(final int nrOfBytes)
115     {
116         this.nrOfBytes = nrOfBytes;
117     }
118
119     public int getNrOfBytes()
120     {
121         return nrOfBytes;
122     }
123
124
125
126     /**
127      * <p>A document in the POI filesystem has been opened for
128      * reading. This method retrieves properties of the document and
129      * adds them to a tree model.</p>
130      */

131     public void processPOIFSReaderEvent(final POIFSReaderEvent event)
132     {
133         DocumentDescriptor d;
134         final DocumentInputStream is = event.getStream();
135         if (!is.markSupported())
136             throw new UnsupportedOperationException JavaDoc(is.getClass().getName() +
137                 " does not support mark().");
138
139         /* Try do handle this document as a property set. We receive
140          * an exception if is no property set and handle it as a
141          * document of some other format. We are not concerned about
142          * that document's details. */

143         try
144         {
145             d = new PropertySetDescriptor(event.getName(), event.getPath(),
146                                           is, nrOfBytes);
147         }
148         catch (HPSFException ex)
149         {
150             d = new DocumentDescriptor(event.getName(), event.getPath(),
151                                        is, nrOfBytes);
152         }
153         catch (Throwable JavaDoc t)
154         {
155             System.err.println
156                 ("Unexpected exception while processing " +
157                 event.getName() + " in " + event.getPath().toString());
158             t.printStackTrace(System.err);
159             throw new RuntimeException JavaDoc(t.getMessage());
160         }
161
162         try
163         {
164             is.close();
165         }
166         catch (IOException JavaDoc ex)
167         {
168             System.err.println
169                 ("Unexpected exception while closing " +
170                 event.getName() + " in " + event.getPath().toString());
171             ex.printStackTrace(System.err);
172         }
173
174         final MutableTreeNode JavaDoc parentNode = getNode(d.path, filename, rootNode);
175         final MutableTreeNode JavaDoc nameNode = new DefaultMutableTreeNode JavaDoc(d.name);
176         parentNode.insert(nameNode, 0);
177         final MutableTreeNode JavaDoc dNode = new DefaultMutableTreeNode JavaDoc(d);
178         nameNode.insert(dNode, 0);
179     }
180
181
182
183     /**
184      * <p>Locates the parent node for a document entry in the tree
185      * model. If the parent node does not yet exist it will be
186      * created, too. This is done recursively, if needed.</p>
187      *
188      * @param path The tree node for this path is located.
189      *
190      * @param fsName The name of the POI filesystem. This is just a
191      * string which is displayed in the tree at the top lovel.
192      *
193      * @param root The root node.
194      */

195     private MutableTreeNode JavaDoc getNode(final POIFSDocumentPath path,
196                                     final String JavaDoc fsName,
197                                     final MutableTreeNode JavaDoc root)
198     {
199         MutableTreeNode JavaDoc n = (MutableTreeNode JavaDoc) pathToNode.get(path);
200         if (n != null)
201             /* Node found in map, just return it. */
202             return n;
203         if (path.length() == 0)
204         {
205             /* This is the root path of the POI filesystem. Its tree
206              * node is resp. must be located below the tree node of
207              * the POI filesystem itself. This is a tree node with the
208              * POI filesystem's name (this the operating system file's
209              * name) as its key it the path-to-node map. */

210             n = (MutableTreeNode JavaDoc) pathToNode.get(fsName);
211             if (n == null)
212             {
213                 /* A tree node for the POI filesystem does not yet
214                  * exist. */

215                 n = new DefaultMutableTreeNode JavaDoc(fsName);
216                 pathToNode.put(fsName, n);
217                 root.insert(n, 0);
218             }
219             return n;
220         }
221         else
222         {
223             /* The path is somewhere down in the POI filesystem's
224              * hierarchy. We need the tree node of this path's parent
225              * and attach our new node to it. */

226             final String JavaDoc name = path.getComponent(path.length() - 1);
227             final POIFSDocumentPath parentPath = path.getParent();
228             final MutableTreeNode JavaDoc parentNode =
229                 getNode(parentPath, fsName, root);
230             n = new DefaultMutableTreeNode JavaDoc(name);
231             pathToNode.put(path, n);
232             parentNode.insert(n, 0);
233             return n;
234         }
235     }
236
237 }
238
Popular Tags