git-svn-id: https://svn.apache.org/repos/asf/jakarta/poi/trunk@353563 13f79535-47bb-0310-9956-ffa450edef68tags/PERF_BEFORE_MERGE
package org.apache.poi.contrib.poibrowser; | package org.apache.poi.contrib.poibrowser; | ||||
import java.awt.event.*; | |||||
import java.io.*; | |||||
import javax.swing.*; | |||||
import javax.swing.tree.*; | |||||
import org.apache.poi.poifs.eventfilesystem.*; | |||||
import java.awt.event.WindowAdapter; | |||||
import java.awt.event.WindowEvent; | |||||
import java.io.FileInputStream; | |||||
import java.io.IOException; | |||||
import javax.swing.JFrame; | |||||
import javax.swing.JScrollPane; | |||||
import javax.swing.JTree; | |||||
import javax.swing.tree.DefaultMutableTreeNode; | |||||
import javax.swing.tree.DefaultTreeModel; | |||||
import javax.swing.tree.MutableTreeNode; | |||||
import org.apache.poi.poifs.eventfilesystem.POIFSReader; | |||||
/** | /** | ||||
* <p>The main class of the POI Browser. It shows the structure of POI | * <p>The main class of the POI Browser. It shows the structure of POI |
package org.apache.poi.contrib.poibrowser; | package org.apache.poi.contrib.poibrowser; | ||||
import java.io.*; | |||||
import org.apache.poi.hpsf.*; | |||||
import org.apache.poi.poifs.filesystem.*; | |||||
import java.io.IOException; | |||||
import java.io.UnsupportedEncodingException; | |||||
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.UnexpectedPropertySetTypeException; | |||||
import org.apache.poi.poifs.filesystem.DocumentInputStream; | |||||
import org.apache.poi.poifs.filesystem.POIFSDocumentPath; | |||||
/** | /** | ||||
* <p>Describes the most important (whatever that is) features of a | * <p>Describes the most important (whatever that is) features of a | ||||
final POIFSDocumentPath path, | final POIFSDocumentPath path, | ||||
final DocumentInputStream stream, | final DocumentInputStream stream, | ||||
final int nrOfBytesToDump) | final int nrOfBytesToDump) | ||||
throws NoPropertySetStreamException, MarkUnsupportedException, | |||||
UnexpectedPropertySetTypeException, IOException | |||||
throws UnexpectedPropertySetTypeException, NoPropertySetStreamException, | |||||
MarkUnsupportedException, UnsupportedEncodingException, | |||||
IOException | |||||
{ | { | ||||
super(name, path, stream, nrOfBytesToDump); | super(name, path, stream, nrOfBytesToDump); | ||||
propertySet = PropertySetFactory.create(stream); | propertySet = PropertySetFactory.create(stream); |
<li> | <li> | ||||
The <link href="#sec4">fourth section</link> tells you how to write | The <link href="#sec4">fourth section</link> tells you how to write | ||||
property set streams. Writing is still rudimentary in HPSF. You have to | |||||
understand the <link href="#sec3">third section</link> before you should | |||||
think about writing properties. Check the Javadoc API documentation to | |||||
find out about the details! | |||||
property set streams. At this time HPSF provides low-level methods only | |||||
for writing properties. Therefore you have to understand the <link | |||||
href="#sec3">third section</link> before you should think about writing | |||||
properties. Check the Javadoc API documentation to find out about the | |||||
details! <strong>Please note:</strong> HPSF's writing functionality is | |||||
<strong>not</strong> present in POI releases up to and including 2.5. In | |||||
order to write properties you have to download a later POI release (when | |||||
available) or retrieve the POI development version from the CVS | |||||
repository. | |||||
</li> | </li> | ||||
</ol> | </ol> | ||||
codepage number is illegal, an UnsupportedEncodingException will be | codepage number is illegal, an UnsupportedEncodingException will be | ||||
thrown.</p> | thrown.</p> | ||||
<p>There are two exceptions to the rule that a character encoding's name | |||||
is derived from the codepage number by prepending the string "cp" to | |||||
it:</p> | |||||
<p>There are some exceptions to the rule saying that a character | |||||
encoding's name is derived from the codepage number by prepending the | |||||
string "cp" to it:</p> | |||||
<dl> | <dl> | ||||
<dt>Codepage 932</dt> | |||||
<dd>is mapped to the character encoding "SJIS".</dd> | |||||
<dt>Codepage 1200</dt> | <dt>Codepage 1200</dt> | ||||
<dd>is mapped to the character encoding "UTF-16".</dd> | <dd>is mapped to the character encoding "UTF-16".</dd> | ||||
<dt>Codepage 65001</dt> | <dt>Codepage 65001</dt> | ||||
<dd>is mapped to the character encoding "UTF-8".</dd> | <dd>is mapped to the character encoding "UTF-8".</dd> | ||||
</dl> | </dl> | ||||
<p>Probably there will be a need to add more mappings between codepage | |||||
numbers and character encoding names. They should be added to the method | |||||
<code>codepageToEncoding</code> in the class | |||||
<code>org.apache.poi.hpsf.VariantSupport</code>. The HPSF author will | |||||
appreciate any advices for mappings to be added.</p> | |||||
</section> | </section> | ||||
</section> | </section> | ||||
<section><title>Overview of Writing Properties</title> | <section><title>Overview of Writing Properties</title> | ||||
<p>Writing properties is possible at a low level only at the moment. You | <p>Writing properties is possible at a low level only at the moment. You | ||||
have to deal with property IDs and variant types to write | |||||
have to deal with things like property IDs and variant types to write | |||||
properties. There are no convenience classes or convenience methods for | properties. There are no convenience classes or convenience methods for | ||||
dealing with summary information and document summary information streams | dealing with summary information and document summary information streams | ||||
yet. Therefore you should have read <link href="#sec3">section 3</link> | yet. Therefore you should have read <link href="#sec3">section 3</link> | ||||
<code>MutableProperty</code>, and some helper classes. The "mutable" | <code>MutableProperty</code>, and some helper classes. The "mutable" | ||||
classes extend their respective superclasses <code>PropertySet</code>, | classes extend their respective superclasses <code>PropertySet</code>, | ||||
<code>Section</code>, and <code>Property</code> and provide "set" and | <code>Section</code>, and <code>Property</code> and provide "set" and | ||||
"write" methods.</p> | |||||
"write" methods, following the <link | |||||
href="http://en.wikipedia.org/wiki/Decorator_pattern">Decorator | |||||
pattern</link>.</p> | |||||
<p>When you are going to write a property set stream your application has | <p>When you are going to write a property set stream your application has | ||||
to perform the following steps:</p> | to perform the following steps:</p> | ||||
<source>package org.apache.poi.hpsf.examples; | <source>package org.apache.poi.hpsf.examples; | ||||
import java.io.*; | |||||
import org.apache.poi.hpsf.*; | |||||
import org.apache.poi.hpsf.wellknown.*; | |||||
import org.apache.poi.poifs.filesystem.*; | |||||
import java.io.FileOutputStream; | |||||
import java.io.IOException; | |||||
import java.io.InputStream; | |||||
import org.apache.poi.hpsf.MutableProperty; | |||||
import org.apache.poi.hpsf.MutablePropertySet; | |||||
import org.apache.poi.hpsf.MutableSection; | |||||
import org.apache.poi.hpsf.SummaryInformation; | |||||
import org.apache.poi.hpsf.Variant; | |||||
import org.apache.poi.hpsf.WritingNotSupportedException; | |||||
import org.apache.poi.hpsf.wellknown.PropertyIDMap; | |||||
import org.apache.poi.hpsf.wellknown.SectionIDMap; | |||||
import org.apache.poi.poifs.filesystem.POIFSFileSystem; | |||||
/** | |||||
* <p>This class is a simple sample application showing how to create a property | |||||
* set and write it to disk.</p> | |||||
* | |||||
* @author Rainer Klute | |||||
* @since 2003-09-12 | |||||
*/ | |||||
public class WriteTitle | public class WriteTitle | ||||
{ | { | ||||
/** | |||||
* <p>Runs the example program.</p> | |||||
* | |||||
* @param args Command-line arguments. The first and only command-line | |||||
* argument is the name of the POI file system to create. | |||||
* @throws IOException if any I/O exception occurs. | |||||
* @throws WritingNotSupportedException if HPSF does not (yet) support | |||||
* writing a certain property type. | |||||
*/ | |||||
public static void main(final String[] args) | public static void main(final String[] args) | ||||
throws WritingNotSupportedException, IOException | throws WritingNotSupportedException, IOException | ||||
{ | { | ||||
} | } | ||||
final String fileName = args[0]; | final String fileName = args[0]; | ||||
final POIFSFileSystem poiFs = new POIFSFileSystem(); | |||||
/* Create a mutable property set. Initially it contains a single section | /* Create a mutable property set. Initially it contains a single section | ||||
* with no properties. */ | * with no properties. */ | ||||
p.setType(Variant.VT_LPWSTR); | p.setType(Variant.VT_LPWSTR); | ||||
p.setValue("Sample title"); | p.setValue("Sample title"); | ||||
/* Place the property into the section. */ | |||||
ms.setProperty(p); | |||||
/* Create the POI file system the property set is to be written to. */ | |||||
final POIFSFileSystem poiFs = new POIFSFileSystem(); | |||||
/* For writing the property set into a POI file system it has to be | /* For writing the property set into a POI file system it has to be | ||||
* handed over to the POIFS.createDocument() method as an input stream | * handed over to the POIFS.createDocument() method as an input stream | ||||
* which produces the bytes making out the property set stream. */ | * which produces the bytes making out the property set stream. */ | ||||
}</source> | }</source> | ||||
<p>The applications first checks that there is exactly a single argument | |||||
on the command line. If this is true, the application stores it in the | |||||
<p>The application first checks that there is exactly one single argument | |||||
on the command line: the name of the file to write. If this single | |||||
argument is present, the application stores it in the | |||||
<code>fileName</code> variable. It will be used in the end when the POI | <code>fileName</code> variable. It will be used in the end when the POI | ||||
file system is written to a disk file.</p> | file system is written to a disk file.</p> | ||||
/* ==================================================================== | /* ==================================================================== | ||||
Copyright 2002-2004 Apache Software Foundation | Copyright 2002-2004 Apache Software Foundation | ||||
import java.io.IOException; | import java.io.IOException; | ||||
import java.io.InputStream; | import java.io.InputStream; | ||||
import java.io.OutputStream; | import java.io.OutputStream; | ||||
import java.io.UnsupportedEncodingException; | |||||
import java.util.HashMap; | import java.util.HashMap; | ||||
import java.util.Iterator; | import java.util.Iterator; | ||||
import java.util.Map; | import java.util.Map; | ||||
* create a property set from a POI document stream that is not a property | * create a property set from a POI document stream that is not a property | ||||
* set stream. | * set stream. | ||||
* @exception IOException if any I/O exception occurs. | * @exception IOException if any I/O exception occurs. | ||||
* @exception UnsupportedEncodingException if a character encoding is not | |||||
* supported. | |||||
*/ | */ | ||||
public static void main(final String[] args) | public static void main(final String[] args) | ||||
throws MarkUnsupportedException, NoPropertySetStreamException, IOException | |||||
throws NoPropertySetStreamException, MarkUnsupportedException, | |||||
UnsupportedEncodingException, IOException | |||||
{ | { | ||||
String originalFileName = null; | String originalFileName = null; | ||||
String copyFileName = null; | String copyFileName = null; | ||||
private static boolean equal(final DirectoryEntry d1, | private static boolean equal(final DirectoryEntry d1, | ||||
final DirectoryEntry d2, | final DirectoryEntry d2, | ||||
final StringBuffer msg) | final StringBuffer msg) | ||||
throws MarkUnsupportedException, NoPropertySetStreamException, IOException | |||||
throws NoPropertySetStreamException, MarkUnsupportedException, | |||||
UnsupportedEncodingException, IOException | |||||
{ | { | ||||
boolean equal = true; | boolean equal = true; | ||||
/* Iterate over d1 and compare each entry with its counterpart in d2. */ | /* Iterate over d1 and compare each entry with its counterpart in d2. */ | ||||
*/ | */ | ||||
private static boolean equal(final DocumentEntry d1, final DocumentEntry d2, | private static boolean equal(final DocumentEntry d1, final DocumentEntry d2, | ||||
final StringBuffer msg) | final StringBuffer msg) | ||||
throws MarkUnsupportedException, NoPropertySetStreamException, IOException | |||||
throws NoPropertySetStreamException, MarkUnsupportedException, | |||||
UnsupportedEncodingException, IOException | |||||
{ | { | ||||
boolean equal = true; | boolean equal = true; | ||||
final DocumentInputStream dis1 = new DocumentInputStream(d1); | final DocumentInputStream dis1 = new DocumentInputStream(d1); |
/* ==================================================================== | /* ==================================================================== | ||||
Copyright 2002-2004 Apache Software Foundation | Copyright 2002-2004 Apache Software Foundation | ||||
import java.io.FileNotFoundException; | import java.io.FileNotFoundException; | ||||
import java.io.IOException; | import java.io.IOException; | ||||
import java.io.InputStream; | import java.io.InputStream; | ||||
import java.io.UnsupportedEncodingException; | |||||
import java.util.List; | import java.util.List; | ||||
import junit.framework.Assert; | import junit.framework.Assert; | ||||
* NoPropertySetStreamException} will be thrown when trying to | * NoPropertySetStreamException} will be thrown when trying to | ||||
* create a {@link PropertySet}.</p> | * create a {@link PropertySet}.</p> | ||||
* | * | ||||
* @exception IOException if an I/O exception occurs | |||||
* @exception IOException if an I/O exception occurs. | |||||
* | |||||
* @exception UnsupportedEncodingException if a character encoding is not | |||||
* supported. | |||||
*/ | */ | ||||
public void testCreatePropertySets() throws IOException | |||||
public void testCreatePropertySets() | |||||
throws UnsupportedEncodingException, IOException | |||||
{ | { | ||||
Class[] expected = new Class[] | Class[] expected = new Class[] | ||||
{ | { |
import java.io.FileNotFoundException; | import java.io.FileNotFoundException; | ||||
import java.io.IOException; | import java.io.IOException; | ||||
import java.io.InputStream; | import java.io.InputStream; | ||||
import java.io.UnsupportedEncodingException; | |||||
import junit.framework.Assert; | import junit.framework.Assert; | ||||
import junit.framework.TestCase; | import junit.framework.TestCase; | ||||
* NoPropertySetStreamException} will be thrown when trying to | * NoPropertySetStreamException} will be thrown when trying to | ||||
* create a {@link PropertySet}.</p> | * create a {@link PropertySet}.</p> | ||||
* | * | ||||
* @exception IOException if an I/O exception occurs | |||||
* @exception IOException if an I/O exception occurs. | |||||
* | |||||
* @exception UnsupportedEncodingException if a character encoding is not | |||||
* supported. | |||||
*/ | */ | ||||
public void testCreatePropertySets() throws IOException | |||||
public void testCreatePropertySets() | |||||
throws UnsupportedEncodingException, IOException | |||||
{ | { | ||||
Class[] expected = new Class[] | Class[] expected = new Class[] | ||||
{ | { |
import junit.framework.Assert; | import junit.framework.Assert; | ||||
import junit.framework.TestCase; | import junit.framework.TestCase; | ||||
import org.apache.poi.hpsf.Constants; | |||||
import org.apache.poi.hpsf.HPSFException; | import org.apache.poi.hpsf.HPSFException; | ||||
import org.apache.poi.hpsf.PropertySet; | import org.apache.poi.hpsf.PropertySet; | ||||
import org.apache.poi.hpsf.PropertySetFactory; | import org.apache.poi.hpsf.PropertySetFactory; | ||||
Assert.assertEquals(ps.getSectionCount(), 2); | Assert.assertEquals(ps.getSectionCount(), 2); | ||||
Section s = (Section) ps.getSections().get(1); | Section s = (Section) ps.getSections().get(1); | ||||
Assert.assertEquals(s.getProperty(1), | Assert.assertEquals(s.getProperty(1), | ||||
new Integer(1200)); | |||||
new Integer(Constants.CP_UTF16)); | |||||
Assert.assertEquals(s.getProperty(2), | Assert.assertEquals(s.getProperty(2), | ||||
new Long(4198897018L)); | new Long(4198897018L)); | ||||
Assert.assertEquals(s.getProperty(3), | Assert.assertEquals(s.getProperty(3), |