KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > poifs > dev > POIFSViewEngine


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.dev;
20
21 import java.io.*;
22
23 import java.util.*;
24
25 /**
26  * This class contains methods used to inspect POIFSViewable objects
27  *
28  * @author Marc Johnson (mjohnson at apache dot org)
29  */

30
31 public class POIFSViewEngine
32 {
33     private static final String JavaDoc _EOL = System.getProperty("line.separator");
34
35     /**
36      * Inspect an object that may be viewable, and drill down if told
37      * to
38      *
39      * @param viewable the object to be viewed
40      * @param drilldown if true, and the object implements
41      * POIFSViewable, inspect the objects' contents
42      * (recursively)
43      * @param indentLevel how far in to indent each string
44      * @param indentString string to use for indenting
45      *
46      * @return a List of Strings holding the content
47      */

48
49     public static List inspectViewable(final Object JavaDoc viewable,
50                                        final boolean drilldown,
51                                        final int indentLevel,
52                                        final String JavaDoc indentString)
53     {
54         List objects = new ArrayList();
55
56         if (viewable instanceof POIFSViewable)
57         {
58             POIFSViewable inspected = ( POIFSViewable ) viewable;
59
60             objects.add(indent(indentLevel, indentString,
61                                inspected.getShortDescription()));
62             if (drilldown)
63             {
64                 if (inspected.preferArray())
65                 {
66                     Object JavaDoc[] data = inspected.getViewableArray();
67
68                     for (int j = 0; j < data.length; j++)
69                     {
70                         objects.addAll(inspectViewable(data[ j ], drilldown,
71                                                        indentLevel + 1,
72                                                        indentString));
73                     }
74                 }
75                 else
76                 {
77                     Iterator iter = inspected.getViewableIterator();
78
79                     while (iter.hasNext())
80                     {
81                         objects.addAll(inspectViewable(iter.next(),
82                                                        drilldown,
83                                                        indentLevel + 1,
84                                                        indentString));
85                     }
86                 }
87             }
88         }
89         else
90         {
91             objects.add(indent(indentLevel, indentString,
92                                viewable.toString()));
93         }
94         return objects;
95     }
96
97     private static String JavaDoc indent(final int indentLevel,
98                                  final String JavaDoc indentString, final String JavaDoc data)
99     {
100         StringBuffer JavaDoc finalBuffer = new StringBuffer JavaDoc();
101         StringBuffer JavaDoc indentPrefix = new StringBuffer JavaDoc();
102
103         for (int j = 0; j < indentLevel; j++)
104         {
105             indentPrefix.append(indentString);
106         }
107         LineNumberReader reader =
108             new LineNumberReader(new StringReader(data));
109
110         try
111         {
112             String JavaDoc line = reader.readLine();
113
114             while (line != null)
115             {
116                 finalBuffer.append(indentPrefix).append(line).append(_EOL);
117                 line = reader.readLine();
118             }
119         }
120         catch (IOException e)
121         {
122             finalBuffer.append(indentPrefix).append(e.getMessage())
123                 .append(_EOL);
124         }
125         return finalBuffer.toString();
126     }
127 } // end public class POIFSViewEngine
128

129
Popular Tags