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.hpsf.basic;
019:
020: import java.io.ByteArrayOutputStream;
021: import java.io.EOFException;
022: import java.io.File;
023: import java.io.FileInputStream;
024: import java.io.FileNotFoundException;
025: import java.io.IOException;
026: import java.io.InputStream;
027: import java.io.OutputStream;
028: import java.util.ArrayList;
029: import java.util.Collections;
030: import java.util.Iterator;
031: import java.util.LinkedList;
032: import java.util.List;
033: import java.util.Properties;
034:
035: import org.apache.poi.hpsf.PropertySet;
036: import org.apache.poi.poifs.eventfilesystem.POIFSReader;
037: import org.apache.poi.poifs.eventfilesystem.POIFSReaderEvent;
038: import org.apache.poi.poifs.eventfilesystem.POIFSReaderListener;
039:
040: /**
041: * <p>Static utility methods needed by the HPSF test cases.</p>
042: *
043: * @author Rainer Klute (klute@rainer-klute.de)
044: * @since 2002-07-20
045: * @version $Id: Util.java 489730 2006-12-22 19:18:16Z bayard $
046: */
047: public class Util {
048:
049: /**
050: * <p>Reads bytes from an input stream and writes them to an
051: * output stream until end of file is encountered.</p>
052: *
053: * @param in the input stream to read from
054: *
055: * @param out the output stream to write to
056: *
057: * @exception IOException if an I/O exception occurs
058: */
059: public static void copy(final InputStream in, final OutputStream out)
060: throws IOException {
061: final int BUF_SIZE = 1000;
062: byte[] b = new byte[BUF_SIZE];
063: int read;
064: boolean eof = false;
065: while (!eof) {
066: try {
067: read = in.read(b, 0, BUF_SIZE);
068: if (read > 0)
069: out.write(b, 0, read);
070: else
071: eof = true;
072: } catch (EOFException ex) {
073: eof = true;
074: }
075: }
076: }
077:
078: /**
079: * <p>Reads all files from a POI filesystem and returns them as an
080: * array of {@link POIFile} instances. This method loads all files
081: * into memory and thus does not cope well with large POI
082: * filessystems.</p>
083: *
084: * @param poiFs The name of the POI filesystem as seen by the
085: * operating system. (This is the "filename".)
086: *
087: * @return The POI files. The elements are ordered in the same way
088: * as the files in the POI filesystem.
089: *
090: * @exception FileNotFoundException if the file containing the POI
091: * filesystem does not exist
092: *
093: * @exception IOException if an I/O exception occurs
094: */
095: public static POIFile[] readPOIFiles(final File poiFs)
096: throws FileNotFoundException, IOException {
097: return readPOIFiles(poiFs, null);
098: }
099:
100: /**
101: * <p>Reads a set of files from a POI filesystem and returns them
102: * as an array of {@link POIFile} instances. This method loads all
103: * files into memory and thus does not cope well with large POI
104: * filessystems.</p>
105: *
106: * @param poiFs The name of the POI filesystem as seen by the
107: * operating system. (This is the "filename".)
108: *
109: * @param poiFiles The names of the POI files to be read.
110: *
111: * @return The POI files. The elements are ordered in the same way
112: * as the files in the POI filesystem.
113: *
114: * @exception FileNotFoundException if the file containing the POI
115: * filesystem does not exist
116: *
117: * @exception IOException if an I/O exception occurs
118: */
119: public static POIFile[] readPOIFiles(final File poiFs,
120: final String[] poiFiles) throws FileNotFoundException,
121: IOException {
122: final List files = new ArrayList();
123: POIFSReader r = new POIFSReader();
124: POIFSReaderListener pfl = new POIFSReaderListener() {
125: public void processPOIFSReaderEvent(
126: final POIFSReaderEvent event) {
127: try {
128: final POIFile f = new POIFile();
129: f.setName(event.getName());
130: f.setPath(event.getPath());
131: final InputStream in = event.getStream();
132: final ByteArrayOutputStream out = new ByteArrayOutputStream();
133: Util.copy(in, out);
134: out.close();
135: f.setBytes(out.toByteArray());
136: files.add(f);
137: } catch (IOException ex) {
138: ex.printStackTrace();
139: throw new RuntimeException(ex.getMessage());
140: }
141: }
142: };
143: if (poiFiles == null)
144: /* Register the listener for all POI files. */
145: r.registerListener(pfl);
146: else
147: /* Register the listener for the specified POI files
148: * only. */
149: for (int i = 0; i < poiFiles.length; i++)
150: r.registerListener(pfl, poiFiles[i]);
151:
152: /* Read the POI filesystem. */
153: r.read(new FileInputStream(poiFs));
154: POIFile[] result = new POIFile[files.size()];
155: for (int i = 0; i < result.length; i++)
156: result[i] = (POIFile) files.get(i);
157: return result;
158: }
159:
160: /**
161: * <p>Read all files from a POI filesystem which are property set streams
162: * and returns them as an array of {@link org.apache.poi.hpsf.PropertySet}
163: * instances.</p>
164: *
165: * @param poiFs The name of the POI filesystem as seen by the
166: * operating system. (This is the "filename".)
167: *
168: * @return The property sets. The elements are ordered in the same way
169: * as the files in the POI filesystem.
170: *
171: * @exception FileNotFoundException if the file containing the POI
172: * filesystem does not exist
173: *
174: * @exception IOException if an I/O exception occurs
175: */
176: public static POIFile[] readPropertySets(final File poiFs)
177: throws FileNotFoundException, IOException {
178: final List files = new ArrayList(7);
179: final POIFSReader r = new POIFSReader();
180: POIFSReaderListener pfl = new POIFSReaderListener() {
181: public void processPOIFSReaderEvent(
182: final POIFSReaderEvent event) {
183: try {
184: final POIFile f = new POIFile();
185: f.setName(event.getName());
186: f.setPath(event.getPath());
187: final InputStream in = event.getStream();
188: if (PropertySet.isPropertySetStream(in)) {
189: final ByteArrayOutputStream out = new ByteArrayOutputStream();
190: Util.copy(in, out);
191: out.close();
192: f.setBytes(out.toByteArray());
193: files.add(f);
194: }
195: } catch (Exception ex) {
196: ex.printStackTrace();
197: throw new RuntimeException(ex.getMessage());
198: }
199: }
200: };
201:
202: /* Register the listener for all POI files. */
203: r.registerListener(pfl);
204:
205: /* Read the POI filesystem. */
206: r.read(new FileInputStream(poiFs));
207: POIFile[] result = new POIFile[files.size()];
208: for (int i = 0; i < result.length; i++)
209: result[i] = (POIFile) files.get(i);
210: return result;
211: }
212:
213: /**
214: * <p>Prints the system properties to System.out.</p>
215: */
216: public static void printSystemProperties() {
217: final Properties p = System.getProperties();
218: final List names = new LinkedList();
219: for (Iterator i = p.keySet().iterator(); i.hasNext();)
220: names.add(i.next());
221: Collections.sort(names);
222: for (final Iterator i = names.iterator(); i.hasNext();) {
223: String name = (String) i.next();
224: String value = (String) p.get(name);
225: System.out.println(name + ": " + value);
226: }
227: System.out.println("Current directory: "
228: + System.getProperty("user.dir"));
229: }
230:
231: }
|