📄 writeauthorandtitle.java
字号:
/* ==================================================================== Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.==================================================================== */ package org.apache.poi.hpsf.examples;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.util.HashMap;import java.util.Map;import org.apache.poi.hpsf.HPSFRuntimeException;import org.apache.poi.hpsf.MarkUnsupportedException;import org.apache.poi.hpsf.MutablePropertySet;import org.apache.poi.hpsf.MutableSection;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.Util;import org.apache.poi.hpsf.Variant;import org.apache.poi.hpsf.WritingNotSupportedException;import org.apache.poi.hpsf.wellknown.PropertyIDMap;import org.apache.poi.poifs.eventfilesystem.POIFSReader;import org.apache.poi.poifs.eventfilesystem.POIFSReaderEvent;import org.apache.poi.poifs.eventfilesystem.POIFSReaderListener;import org.apache.poi.poifs.filesystem.DirectoryEntry;import org.apache.poi.poifs.filesystem.DocumentInputStream;import org.apache.poi.poifs.filesystem.POIFSDocumentPath;import org.apache.poi.poifs.filesystem.POIFSFileSystem;/** * <p>This class is a sample application which shows how to write or modify the * author and title property of an OLE 2 document. This could be done in two * different ways:</p> * * <ul> * * <li><p>The first approach is to open the OLE 2 file as a POI filesystem * (see class {@link POIFSFileSystem}), read the summary information property * set (see classes {@link SummaryInformation} and {@link PropertySet}), write * the author and title properties into it and write the property set back into * the POI filesystem.</p></li> * * <li><p>The second approach does not modify the original POI filesystem, but * instead creates a new one. All documents from the original POIFS are copied * to the destination POIFS, except for the summary information stream. The * latter is modified by setting the author and title property before writing * it to the destination POIFS. It there are several summary information streams * in the original POIFS - e.g. in subordinate directories - they are modified * just the same.</p></li> * * </ul> * * <p>This sample application takes the second approach. It expects the name of * the existing POI filesystem's name as its first command-line parameter and * the name of the output POIFS as the second command-line argument. The * program then works as described above: It copies nearly all documents * unmodified from the input POI filesystem to the output POI filesystem. If it * encounters a summary information stream it reads its properties. Then it sets * the "author" and "title" properties to new values and writes the modified * summary information stream into the output file.</p> * * <p>Further explanations can be found in the HPSF HOW-TO.</p> * * @author Rainer Klute <a * href="mailto:klute@rainer-klute.de"><klute@rainer-klute.de></a> * @version $Id: WriteAuthorAndTitle.java 489730 2006-12-22 19:18:16Z bayard $ * @since 2003-09-01 */public class WriteAuthorAndTitle{ /** * <p>Runs the example program.</p> * * @param args Command-line arguments. The first command-line argument must * be the name of a POI filesystem to read. * @throws IOException if any I/O exception occurs. */ public static void main(final String[] args) throws IOException { /* Check whether we have exactly two command-line arguments. */ if (args.length != 2) { System.err.println("Usage: " + WriteAuthorAndTitle.class.getName() + " originPOIFS destinationPOIFS"); System.exit(1); } /* Read the names of the origin and destination POI filesystems. */ final String srcName = args[0]; final String dstName = args[1]; /* Read the origin POIFS using the eventing API. The real work is done * in the class ModifySICopyTheRest which is registered here as a * POIFSReader. */ final POIFSReader r = new POIFSReader(); final ModifySICopyTheRest msrl = new ModifySICopyTheRest(dstName); r.registerListener(msrl); r.read(new FileInputStream(srcName)); /* Write the new POIFS to disk. */ msrl.close(); } /** * <p>This class does all the work. As its name implies it modifies a * summary information property set and copies everything else unmodified * to the destination POI filesystem. Since an instance of it is registered * as a {@link POIFSReader} its method {@link * #processPOIFSReaderEvent(POIFSReaderEvent)} is called for each document * in the origin POIFS.</p> */ static class ModifySICopyTheRest implements POIFSReaderListener { String dstName; OutputStream out; POIFSFileSystem poiFs; /** * <p>The constructor of a {@link ModifySICopyTheRest} instance creates * the target POIFS. It also stores the name of the file the POIFS will * be written to once it is complete.</p> * * @param dstName The name of the disk file the destination POIFS is to * be written to. */ public ModifySICopyTheRest(final String dstName) { this.dstName = dstName; poiFs = new POIFSFileSystem(); } /** * <p>The method is called by POI's eventing API for each file in the * origin POIFS.</p> */ public void processPOIFSReaderEvent(final POIFSReaderEvent event) { /* The following declarations are shortcuts for accessing the * "event" object. */ final POIFSDocumentPath path = event.getPath(); final String name = event.getName(); final DocumentInputStream stream = event.getStream(); Throwable t = null; try { /* Find out whether the current document is a property set * stream or not. */ if (PropertySet.isPropertySetStream(stream)) { /* Yes, the current document is a property set stream. * Let's create a PropertySet instance from it. */ PropertySet ps = null; try { ps = PropertySetFactory.create(stream); } catch (NoPropertySetStreamException ex) { /* This exception will not be thrown because we already * checked above. */ } /* Now we know that we really have a property set. The next * step is to find out whether it is a summary information * or not. */ if (ps.isSummaryInformation()) /* Yes, it is a summary information. We will modify it * and write the result to the destination POIFS. */ editSI(poiFs, path, name, ps); else /* No, it is not a summary information. We don't care * about its internals and copy it unmodified to the * destination POIFS. */ copy(poiFs, path, name, ps); } else /* No, the current document is not a property set stream. We * copy it unmodified to the destination POIFS. */ copy(poiFs, event.getPath(), event.getName(), stream); } catch (MarkUnsupportedException ex) { t = ex; } catch (IOException ex) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -