Bug 21775

git-svn-id: https://svn.apache.org/repos/asf/jakarta/poi/trunk@353247 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Rainer Klute 2003-07-26 06:57:33 +00:00
parent 6182b10170
commit f970fa93d8
6 changed files with 295 additions and 231 deletions

View File

@ -58,13 +58,21 @@
package org.apache.poi.contrib.poibrowser;
import java.awt.*;
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.util.Iterator;
import java.util.List;
import javax.swing.*;
import javax.swing.tree.*;
import org.apache.poi.hpsf.*;
import org.apache.poi.hpsf.wellknown.*;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import org.apache.poi.hpsf.Property;
import org.apache.poi.hpsf.PropertySet;
import org.apache.poi.hpsf.Section;
import org.apache.poi.hpsf.SummaryInformation;
/**
* <p>Renders a {@link PropertySetDescriptor} by more or less dumping
@ -148,7 +156,8 @@ public class PropertySetDescriptorRenderer extends DocumentDescriptorRenderer
for (Iterator i = sections.iterator(); i.hasNext();)
{
Section s = (Section) i.next();
b.append(toString(s, "Section " + count++));
String d = toString(s, "Section " + count++);
b.append(d);
}
return b.toString();
}
@ -184,8 +193,10 @@ public class PropertySetDescriptorRenderer extends DocumentDescriptorRenderer
b.append(' ');
b.append("0x" + Codec.hexEncode(b2, 4, b2.length - 4));
}
else
else if (value != null)
b.append(value.toString());
else
b.append("null");
}
return b.toString();
}

View File

@ -62,7 +62,6 @@
*/
package org.apache.poi.hpsf;
import java.util.*;
import org.apache.poi.util.LittleEndian;
/**
@ -85,6 +84,7 @@ public class TypeReader
* starts
* @param length The length of the variant including the variant
* type field
* @param type The variant type to read
* @return A Java object that corresponds best to the variant
* field. For example, a VT_I4 is returned as a {@link Long}, a
* VT_LPSTR as a {@link String}.
@ -92,15 +92,25 @@ public class TypeReader
* @see Variant
*/
public static Object read(final byte[] src, int offset, int length,
final int type)
final int type)
{
/*
* FIXME: Support reading more types and clean up this code!
*/
Object value;
length = length - LittleEndian.INT_SIZE;
/*
* FIXME: Support reading more types and clean up this code!
*/
Object value;
length = length - LittleEndian.INT_SIZE;
switch (type)
{
{
case Variant.VT_EMPTY:
{
/*
* FIXME: The value returned by this case relies on the
* assumption that the value VT_EMPTY denotes consists of zero
* bytes. I'd be glad if some could confirm or correct this.
*/
value = null;
break;
}
case Variant.VT_I2:
{
/*
@ -137,11 +147,11 @@ public class TypeReader
* Read a byte string. In Java it is represented as a
* String object. The 0x00 bytes at the end must be
* stripped.
*
* FIXME: Reading an 8-bit string should pay attention
* to the codepage. Currently the byte making out the
* property's value are interpreted according to the
* platform's default character set.
*
* FIXME: Reading an 8-bit string should pay attention
* to the codepage. Currently the byte making out the
* property's value are interpreted according to the
* platform's default character set.
*/
final int first = offset + LittleEndian.INT_SIZE;
long last = first + LittleEndian.getUInt(src, offset) - 1;
@ -149,7 +159,7 @@ public class TypeReader
while (src[(int) last] == 0 && first <= last)
last--;
value = new String(src, (int) first, (int) (last - first + 1));
break;
break;
}
case Variant.VT_LPWSTR:
{
@ -160,27 +170,27 @@ public class TypeReader
*/
final int first = offset + LittleEndian.INT_SIZE;
long last = first + LittleEndian.getUInt(src, offset) - 1;
long l = last - first;
long l = last - first;
offset += LittleEndian.INT_SIZE;
StringBuffer b = new StringBuffer((int) (last - first));
for (int i = 0; i <= l; i++)
{
final int i1 = offset + (i * 2);
final int i2 = i1 + 1;
b.append((char) ((src[i2] << 8) + src[i1]));
}
/* Strip 0x00 characters from the end of the string: */
while (b.charAt(b.length() - 1) == 0x00)
b.setLength(b.length() - 1);
value = b.toString();
break;
StringBuffer b = new StringBuffer((int) (last - first));
for (int i = 0; i <= l; i++)
{
final int i1 = offset + (i * 2);
final int i2 = i1 + 1;
b.append((char) ((src[i2] << 8) + src[i1]));
}
/* Strip 0x00 characters from the end of the string: */
while (b.charAt(b.length() - 1) == 0x00)
b.setLength(b.length() - 1);
value = b.toString();
break;
}
case Variant.VT_CF:
{
final byte[] v = new byte[length];
for (int i = 0; i < length; i++)
v[i] = src[(int) (offset + i)];
value = v;
value = v;
break;
}
case Variant.VT_BOOL:
@ -190,24 +200,24 @@ public class TypeReader
* src[offset + 3] contain the DWord for VT_BOOL, so
* skip it, we don't need it.
*/
final int first = offset + LittleEndian.INT_SIZE;
// final int first = offset + LittleEndian.INT_SIZE;
long bool = LittleEndian.getUInt(src, offset);
if (bool != 0)
value = new Boolean(true);
else
value = new Boolean(false);
break;
break;
}
default:
{
final byte[] v = new byte[length];
for (int i = 0; i < length; i++)
v[i] = src[(int) (offset + i)];
value = v;
value = v;
break;
}
}
return value;
return value;
}
}

View File

@ -53,7 +53,8 @@
*/
package org.apache.poi.hpsf.basic;
import org.apache.poi.poifs.filesystem.*;
import org.apache.poi.poifs.filesystem.POIFSDocumentPath;
@ -73,32 +74,32 @@ public class POIFile
public void setName(final String name)
{
this.name = name;
this.name = name;
}
public String getName()
{
return name;
return name;
}
public void setPath(final POIFSDocumentPath path)
{
this.path = path;
this.path = path;
}
public POIFSDocumentPath getPath()
{
return path;
return path;
}
public void setBytes(final byte[] bytes)
{
this.bytes = bytes;
this.bytes = bytes;
}
public byte[] getBytes()
{
return bytes;
return bytes;
}
}

View File

@ -54,10 +54,23 @@
package org.apache.poi.hpsf.basic;
import java.io.*;
import java.util.*;
import junit.framework.*;
import org.apache.poi.hpsf.*;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import junit.framework.Assert;
import junit.framework.TestCase;
import org.apache.poi.hpsf.DocumentSummaryInformation;
import org.apache.poi.hpsf.HPSFException;
import org.apache.poi.hpsf.MarkUnsupportedException;
import org.apache.poi.hpsf.NoPropertySetStreamException;
import org.apache.poi.hpsf.PropertySet;
import org.apache.poi.hpsf.PropertySetFactory;
import org.apache.poi.hpsf.SummaryInformation;
import org.apache.poi.hpsf.UnexpectedPropertySetTypeException;
@ -71,37 +84,37 @@ import org.apache.poi.hpsf.*;
public class TestBasic extends TestCase
{
final static String POI_FS = "TestGermanWord90.doc";
final static String[] POI_FILES = new String[]
{
"\005SummaryInformation",
"\005DocumentSummaryInformation",
"WordDocument",
"\001CompObj",
"1Table"
};
final static int BYTE_ORDER = 0xfffe;
final static int FORMAT = 0x0000;
final static int OS_VERSION = 0x00020A04;
final static byte[] CLASS_ID =
{
(byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
(byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
(byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
(byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00
};
final static int[] SECTION_COUNT =
static final String POI_FS = "TestGermanWord90.doc";
static final String[] POI_FILES = new String[]
{
"\005SummaryInformation",
"\005DocumentSummaryInformation",
"WordDocument",
"\001CompObj",
"1Table"
};
static final int BYTE_ORDER = 0xfffe;
static final int FORMAT = 0x0000;
static final int OS_VERSION = 0x00020A04;
static final byte[] CLASS_ID =
{
(byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
(byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
(byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
(byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00
};
static final int[] SECTION_COUNT =
{1, 2};
final static boolean[] IS_SUMMARY_INFORMATION =
static final boolean[] IS_SUMMARY_INFORMATION =
{true, false};
final static boolean[] IS_DOCUMENT_SUMMARY_INFORMATION =
{false, true};
static final boolean[] IS_DOCUMENT_SUMMARY_INFORMATION =
{false, true};
POIFile[] poiFiles;
public TestBasic(String name)
public TestBasic(final String name)
{
super(name);
}
@ -113,11 +126,11 @@ public class TestBasic extends TestCase
*/
public void setUp() throws FileNotFoundException, IOException
{
final File dataDir =
new File(System.getProperty("HPSF.testdata.path"));
final File data = new File(dataDir, POI_FS);
final File dataDir =
new File(System.getProperty("HPSF.testdata.path"));
final File data = new File(dataDir, POI_FS);
poiFiles = Util.readPOIFiles(data);
poiFiles = Util.readPOIFiles(data);
}
@ -128,9 +141,9 @@ public class TestBasic extends TestCase
*/
public void testReadFiles() throws IOException
{
String[] expected = POI_FILES;
for (int i = 0; i < expected.length; i++)
Assert.assertEquals(poiFiles[i].getName(), expected[i]);
String[] expected = POI_FILES;
for (int i = 0; i < expected.length; i++)
Assert.assertEquals(poiFiles[i].getName(), expected[i]);
}
@ -146,37 +159,37 @@ public class TestBasic extends TestCase
*/
public void testCreatePropertySets() throws IOException
{
Class[] expected = new Class[]
{
SummaryInformation.class,
DocumentSummaryInformation.class,
NoPropertySetStreamException.class,
NoPropertySetStreamException.class,
NoPropertySetStreamException.class
};
for (int i = 0; i < expected.length; i++)
{
InputStream in = new ByteArrayInputStream(poiFiles[i].getBytes());
Object o;
try
{
o = PropertySetFactory.create(in);
}
catch (NoPropertySetStreamException ex)
{
o = ex;
}
catch (UnexpectedPropertySetTypeException ex)
{
o = ex;
}
catch (MarkUnsupportedException ex)
{
o = ex;
}
in.close();
Assert.assertEquals(o.getClass(), expected[i]);
}
Class[] expected = new Class[]
{
SummaryInformation.class,
DocumentSummaryInformation.class,
NoPropertySetStreamException.class,
NoPropertySetStreamException.class,
NoPropertySetStreamException.class
};
for (int i = 0; i < expected.length; i++)
{
InputStream in = new ByteArrayInputStream(poiFiles[i].getBytes());
Object o;
try
{
o = PropertySetFactory.create(in);
}
catch (NoPropertySetStreamException ex)
{
o = ex;
}
catch (UnexpectedPropertySetTypeException ex)
{
o = ex;
}
catch (MarkUnsupportedException ex)
{
o = ex;
}
in.close();
Assert.assertEquals(o.getClass(), expected[i]);
}
}
@ -188,25 +201,24 @@ public class TestBasic extends TestCase
*/
public void testPropertySetMethods() throws IOException, HPSFException
{
String[] expected = POI_FILES;
/* Loop over the two property sets. */
for (int i = 0; i < 2; i++)
{
byte[] b = poiFiles[i].getBytes();
PropertySet ps =
PropertySetFactory.create(new ByteArrayInputStream(b));
Assert.assertEquals(ps.getByteOrder(), BYTE_ORDER);
Assert.assertEquals(ps.getFormat(), FORMAT);
Assert.assertEquals(ps.getOSVersion(), OS_VERSION);
Assert.assertEquals(new String(ps.getClassID().getBytes()),
new String(CLASS_ID));
Assert.assertEquals(ps.getSectionCount(), SECTION_COUNT[i]);
Assert.assertEquals(ps.isSummaryInformation(),
IS_SUMMARY_INFORMATION[i]);
Assert.assertEquals(ps.isDocumentSummaryInformation(),
IS_DOCUMENT_SUMMARY_INFORMATION[i]);
}
/* Loop over the two property sets. */
for (int i = 0; i < 2; i++)
{
byte[] b = poiFiles[i].getBytes();
PropertySet ps =
PropertySetFactory.create(new ByteArrayInputStream(b));
Assert.assertEquals(ps.getByteOrder(), BYTE_ORDER);
Assert.assertEquals(ps.getFormat(), FORMAT);
Assert.assertEquals(ps.getOSVersion(), OS_VERSION);
Assert.assertEquals(new String(ps.getClassID().getBytes()),
new String(CLASS_ID));
Assert.assertEquals(ps.getSectionCount(), SECTION_COUNT[i]);
Assert.assertEquals(ps.isSummaryInformation(),
IS_SUMMARY_INFORMATION[i]);
Assert.assertEquals(ps.isDocumentSummaryInformation(),
IS_DOCUMENT_SUMMARY_INFORMATION[i]);
}
}
@ -214,11 +226,11 @@ public class TestBasic extends TestCase
/**
* <p>Runs the test cases stand-alone.</p>
*/
public static void main(String[] args) throws Throwable
public static void main(final String[] args) throws Throwable
{
System.setProperty("HPSF.testdata.path",
"./src/testcases/org/apache/poi/hpsf/data");
junit.textui.TestRunner.run(TestBasic.class);
System.setProperty("HPSF.testdata.path",
"./src/testcases/org/apache/poi/hpsf/data");
junit.textui.TestRunner.run(TestBasic.class);
}
}

View File

@ -54,10 +54,18 @@
package org.apache.poi.hpsf.basic;
import java.io.*;
import java.util.*;
import junit.framework.*;
import org.apache.poi.hpsf.*;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import junit.framework.Assert;
import junit.framework.TestCase;
import org.apache.poi.hpsf.HPSFException;
import org.apache.poi.hpsf.PropertySet;
import org.apache.poi.hpsf.PropertySetFactory;
import org.apache.poi.hpsf.Section;
@ -72,17 +80,22 @@ import org.apache.poi.hpsf.*;
public class TestUnicode extends TestCase
{
final static String POI_FS = "TestUnicode.xls";
final static String[] POI_FILES = new String[]
{
"\005DocumentSummaryInformation",
};
static final String POI_FS = "TestUnicode.xls";
static final String[] POI_FILES = new String[]
{
"\005DocumentSummaryInformation",
};
File data;
POIFile[] poiFiles;
public TestUnicode(String name)
/**
* <p>Constructor</p>
*
* @param name the test case's name
*/
public TestUnicode(final String name)
{
super(name);
}
@ -92,11 +105,11 @@ public class TestUnicode extends TestCase
/**
* <p>Read a the test file from the "data" directory.</p>
*/
public void setUp() throws FileNotFoundException, IOException
protected void setUp() throws FileNotFoundException, IOException
{
final File dataDir =
new File(System.getProperty("HPSF.testdata.path"));
data = new File(dataDir, POI_FS);
final File dataDir =
new File(System.getProperty("HPSF.testdata.path"));
data = new File(dataDir, POI_FS);
}
@ -108,23 +121,23 @@ public class TestUnicode extends TestCase
*/
public void testPropertySetMethods() throws IOException, HPSFException
{
POIFile poiFile = Util.readPOIFiles(data, POI_FILES)[0];
byte[] b = poiFile.getBytes();
PropertySet ps =
PropertySetFactory.create(new ByteArrayInputStream(b));
Assert.assertTrue(ps.isDocumentSummaryInformation());
Assert.assertEquals(ps.getSectionCount(), 2);
Section s = (Section) ps.getSections().get(1);
Assert.assertEquals(s.getProperty(1),
new Integer(1200));
Assert.assertEquals(s.getProperty(2),
new Long(4198897018l));
Assert.assertEquals(s.getProperty(3),
"MCon_Info zu Office bei Schreiner");
Assert.assertEquals(s.getProperty(4),
"petrovitsch@schreiner-online.de");
Assert.assertEquals(s.getProperty(5),
"Petrovitsch, Wilhelm");
POIFile poiFile = Util.readPOIFiles(data, POI_FILES)[0];
byte[] b = poiFile.getBytes();
PropertySet ps =
PropertySetFactory.create(new ByteArrayInputStream(b));
Assert.assertTrue(ps.isDocumentSummaryInformation());
Assert.assertEquals(ps.getSectionCount(), 2);
Section s = (Section) ps.getSections().get(1);
Assert.assertEquals(s.getProperty(1),
new Integer(1200));
Assert.assertEquals(s.getProperty(2),
new Long(4198897018L));
Assert.assertEquals(s.getProperty(3),
"MCon_Info zu Office bei Schreiner");
Assert.assertEquals(s.getProperty(4),
"petrovitsch@schreiner-online.de");
Assert.assertEquals(s.getProperty(5),
"Petrovitsch, Wilhelm");
}
@ -132,10 +145,10 @@ public class TestUnicode extends TestCase
/**
* <p>Runs the test cases stand-alone.</p>
*/
public static void main(String[] args)
public static void main(final String[] args)
{
System.setProperty("HPSF.testdata.path",
"./src/testcases/org/apache/poi/hpsf/data");
System.setProperty("HPSF.testdata.path",
"./src/testcases/org/apache/poi/hpsf/data");
junit.textui.TestRunner.run(TestUnicode.class);
}

View File

@ -54,9 +54,24 @@
package org.apache.poi.hpsf.basic;
import java.io.*;
import java.util.*;
import org.apache.poi.poifs.eventfilesystem.*;
import java.io.ByteArrayOutputStream;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Properties;
import org.apache.poi.poifs.eventfilesystem.POIFSReader;
import org.apache.poi.poifs.eventfilesystem.POIFSReaderEvent;
import org.apache.poi.poifs.eventfilesystem.POIFSReaderListener;
@ -73,6 +88,9 @@ public class Util
/**
* <p>Reads bytes from an input stream and writes them to an
* output stream until end of file is encountered.</p>
*
* @param in the input stream to read from
* @param out the output stream to write to
*/
public static void copy(final InputStream in, final OutputStream out)
throws IOException
@ -88,8 +106,8 @@ public class Util
read = in.read(b, 0, BUF_SIZE);
if (read > 0)
out.write(b, 0, read);
else
eof = true;
else
eof = true;
}
catch (EOFException ex)
{
@ -106,16 +124,16 @@ public class Util
* into memory and thus does not cope well with large POI
* filessystems.</p>
*
* @param file The name of the POI filesystem as seen by the
* @param poiFs The name of the POI filesystem as seen by the
* operating system. (This is the "filename".)
*
* @return The POI files. The elements are ordered in the same way
* as the files in the POI filesystem.
*/
public static POIFile[] readPOIFiles(final File poiFs)
throws FileNotFoundException, IOException
throws FileNotFoundException, IOException
{
return readPOIFiles(poiFs, null);
return readPOIFiles(poiFs, null);
}
@ -126,7 +144,7 @@ public class Util
* files into memory and thus does not cope well with large POI
* filessystems.</p>
*
* @param file The name of the POI filesystem as seen by the
* @param poiFs The name of the POI filesystem as seen by the
* operating system. (This is the "filename".)
*
* @param poiFiles The names of the POI files to be read.
@ -135,50 +153,49 @@ public class Util
* as the files in the POI filesystem.
*/
public static POIFile[] readPOIFiles(final File poiFs,
final String[] poiFiles)
throws FileNotFoundException, IOException
final String[] poiFiles)
throws FileNotFoundException, IOException
{
final List files = new ArrayList();
POIFSReader r = new POIFSReader();
POIFSReaderListener pfl = new POIFSReaderListener()
{
public void processPOIFSReaderEvent(POIFSReaderEvent event)
{
try
{
POIFile f = new POIFile();
f.setName(event.getName());
f.setPath(event.getPath());
InputStream in = event.getStream();
ByteArrayOutputStream out =
new ByteArrayOutputStream();
Util.copy(in, out);
out.close();
f.setBytes(out.toByteArray());
files.add(f);
}
catch (IOException ex)
{
ex.printStackTrace();
throw new RuntimeException(ex.getMessage());
}
}
};
if (poiFiles == null)
/* Register the listener for all POI files. */
r.registerListener(pfl);
else
/* Register the listener for the specified POI files
* only. */
for (int i = 0; i < poiFiles.length; i++)
r.registerListener(pfl, poiFiles[i]);
final List files = new ArrayList();
POIFSReader r = new POIFSReader();
POIFSReaderListener pfl = new POIFSReaderListener()
{
public void processPOIFSReaderEvent(final POIFSReaderEvent event)
{
try
{
POIFile f = new POIFile();
f.setName(event.getName());
f.setPath(event.getPath());
InputStream in = event.getStream();
ByteArrayOutputStream out = new ByteArrayOutputStream();
Util.copy(in, out);
out.close();
f.setBytes(out.toByteArray());
files.add(f);
}
catch (IOException ex)
{
ex.printStackTrace();
throw new RuntimeException(ex.getMessage());
}
}
};
if (poiFiles == null)
/* Register the listener for all POI files. */
r.registerListener(pfl);
else
/* Register the listener for the specified POI files
* only. */
for (int i = 0; i < poiFiles.length; i++)
r.registerListener(pfl, poiFiles[i]);
/* Read the POI filesystem. */
r.read(new FileInputStream(poiFs));
POIFile[] result = new POIFile[files.size()];
for (int i = 0; i < result.length; i++)
result[i] = (POIFile) files.get(i);
return result;
/* Read the POI filesystem. */
r.read(new FileInputStream(poiFs));
POIFile[] result = new POIFile[files.size()];
for (int i = 0; i < result.length; i++)
result[i] = (POIFile) files.get(i);
return result;
}
@ -188,19 +205,19 @@ public class Util
*/
public static void printSystemProperties()
{
Properties p = System.getProperties();
List names = new LinkedList();
for (Iterator i = p.keySet().iterator(); i.hasNext();)
names.add(i.next());
Collections.sort(names);
for (Iterator i = names.iterator(); i.hasNext();)
final Properties p = System.getProperties();
final List names = new LinkedList();
for (Iterator i = p.keySet().iterator(); i.hasNext();)
names.add(i.next());
Collections.sort(names);
for (final Iterator i = names.iterator(); i.hasNext();)
{
String name = (String) i.next();
String value = (String) p.get(name);
System.out.println(name + ": " + value);
}
System.out.println("Current directory: " +
System.getProperty("user.dir"));
String name = (String) i.next();
String value = (String) p.get(name);
System.out.println(name + ": " + value);
}
System.out.println("Current directory: " +
System.getProperty("user.dir"));
}
}