001: /* ====================================================================
002: Licensed to the Apache Software Foundation (ASF) under one or more
003: contributor license agreements. See the NOTICE file distributed with
004: this work for additional information regarding copyright ownership.
005: The ASF licenses this file to You under the Apache License, Version 2.0
006: (the "License"); you may not use this file except in compliance with
007: the License. You may obtain a copy of the License at
008:
009: http://www.apache.org/licenses/LICENSE-2.0
010:
011: Unless required by applicable law or agreed to in writing, software
012: distributed under the License is distributed on an "AS IS" BASIS,
013: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: See the License for the specific language governing permissions and
015: limitations under the License.
016: ==================================================================== */
017:
018: package org.apache.poi.poifs.dev;
019:
020: import java.io.*;
021:
022: import java.util.*;
023:
024: /**
025: * This class contains methods used to inspect POIFSViewable objects
026: *
027: * @author Marc Johnson (mjohnson at apache dot org)
028: */
029:
030: public class POIFSViewEngine {
031: private static final String _EOL = System
032: .getProperty("line.separator");
033:
034: /**
035: * Inspect an object that may be viewable, and drill down if told
036: * to
037: *
038: * @param viewable the object to be viewed
039: * @param drilldown if true, and the object implements
040: * POIFSViewable, inspect the objects' contents
041: * (recursively)
042: * @param indentLevel how far in to indent each string
043: * @param indentString string to use for indenting
044: *
045: * @return a List of Strings holding the content
046: */
047:
048: public static List inspectViewable(final Object viewable,
049: final boolean drilldown, final int indentLevel,
050: final String indentString) {
051: List objects = new ArrayList();
052:
053: if (viewable instanceof POIFSViewable) {
054: POIFSViewable inspected = (POIFSViewable) viewable;
055:
056: objects.add(indent(indentLevel, indentString, inspected
057: .getShortDescription()));
058: if (drilldown) {
059: if (inspected.preferArray()) {
060: Object[] data = inspected.getViewableArray();
061:
062: for (int j = 0; j < data.length; j++) {
063: objects.addAll(inspectViewable(data[j],
064: drilldown, indentLevel + 1,
065: indentString));
066: }
067: } else {
068: Iterator iter = inspected.getViewableIterator();
069:
070: while (iter.hasNext()) {
071: objects.addAll(inspectViewable(iter.next(),
072: drilldown, indentLevel + 1,
073: indentString));
074: }
075: }
076: }
077: } else {
078: objects.add(indent(indentLevel, indentString, viewable
079: .toString()));
080: }
081: return objects;
082: }
083:
084: private static String indent(final int indentLevel,
085: final String indentString, final String data) {
086: StringBuffer finalBuffer = new StringBuffer();
087: StringBuffer indentPrefix = new StringBuffer();
088:
089: for (int j = 0; j < indentLevel; j++) {
090: indentPrefix.append(indentString);
091: }
092: LineNumberReader reader = new LineNumberReader(
093: new StringReader(data));
094:
095: try {
096: String line = reader.readLine();
097:
098: while (line != null) {
099: finalBuffer.append(indentPrefix).append(line).append(
100: _EOL);
101: line = reader.readLine();
102: }
103: } catch (IOException e) {
104: finalBuffer.append(indentPrefix).append(e.getMessage())
105: .append(_EOL);
106: }
107: return finalBuffer.toString();
108: }
109: } // end public class POIFSViewEngine
|