📄 testwritewellknown.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.basic;import java.io.File;import java.io.FileFilter;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.OutputStream;import java.util.Calendar;import java.util.Date;import java.util.GregorianCalendar;import java.util.HashMap;import java.util.Iterator;import java.util.Map;import java.util.Map.Entry;import junit.framework.TestCase;import org.apache.poi.hpsf.CustomProperties;import org.apache.poi.hpsf.CustomProperty;import org.apache.poi.hpsf.DocumentSummaryInformation;import org.apache.poi.hpsf.MarkUnsupportedException;import org.apache.poi.hpsf.MutableProperty;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.UnexpectedPropertySetTypeException;import org.apache.poi.hpsf.Variant;import org.apache.poi.hpsf.VariantSupport;import org.apache.poi.hpsf.WritingNotSupportedException;import org.apache.poi.hpsf.wellknown.SectionIDMap;import org.apache.poi.poifs.filesystem.DirectoryEntry;import org.apache.poi.poifs.filesystem.DocumentEntry;import org.apache.poi.poifs.filesystem.DocumentInputStream;import org.apache.poi.poifs.filesystem.POIFSFileSystem;/** * <p>Tests HPSF's high-level writing functionality for the well-known property * set "SummaryInformation" and "DocumentSummaryInformation".</p> * * @author Rainer Klute * <a href="mailto:klute@rainer-klute.de">klute@rainer-klute.de</a> * @since 2006-02-01 * @version $Id: TestWriteWellKnown.java 489730 2006-12-22 19:18:16Z bayard $ */public class TestWriteWellKnown extends TestCase{ private static final String POI_FS = "TestWriteWellKnown.doc"; /** * <p>Constructor</p> * * @param name the test case's name */ public TestWriteWellKnown(final String name) { super(name); } /** * @see TestCase#setUp() */ public void setUp() { VariantSupport.setLogUnsupportedTypes(false); } /** * <p>This test method checks whether DocumentSummary information streams * can be read. This is done by opening all "Test*" files in the directrory * pointed to by the "HPSF.testdata.path" system property, trying to extract * the document summary information stream in the root directory and calling * its get... methods.</p> * @throws IOException * @throws FileNotFoundException * @throws MarkUnsupportedException * @throws NoPropertySetStreamException * @throws UnexpectedPropertySetTypeException */ public void testReadDocumentSummaryInformation() throws FileNotFoundException, IOException, NoPropertySetStreamException, MarkUnsupportedException, UnexpectedPropertySetTypeException { final String dataDirName = System.getProperty("HPSF.testdata.path"); final File dataDir = new File(dataDirName); final File[] docs = dataDir.listFiles(new FileFilter() { public boolean accept(final File file) { return file.isFile() && file.getName().startsWith("Test"); }}); for (int i = 0; i < docs.length; i++) { final File doc = docs[i]; System.out.println("Reading file " + doc); /* Read a test document <em>doc</em> into a POI filesystem. */ final POIFSFileSystem poifs = new POIFSFileSystem(new FileInputStream(doc)); final DirectoryEntry dir = poifs.getRoot(); DocumentEntry dsiEntry = null; try { dsiEntry = (DocumentEntry) dir.getEntry(DocumentSummaryInformation.DEFAULT_STREAM_NAME); } catch (FileNotFoundException ex) { /* * A missing document summary information stream is not an error * and therefore silently ignored here. */ } /* * If there is a document summry information stream, read it from * the POI filesystem. */ if (dsiEntry != null) { final DocumentInputStream dis = new DocumentInputStream(dsiEntry); final PropertySet ps = new PropertySet(dis); final DocumentSummaryInformation dsi = new DocumentSummaryInformation(ps); /* Execute the get... methods. */ dsi.getByteCount(); dsi.getByteOrder(); dsi.getCategory(); dsi.getCompany(); dsi.getCustomProperties(); // FIXME dsi.getDocparts(); // FIXME dsi.getHeadingPair(); dsi.getHiddenCount(); dsi.getLineCount(); dsi.getLinksDirty(); dsi.getManager(); dsi.getMMClipCount(); dsi.getNoteCount(); dsi.getParCount(); dsi.getPresentationFormat(); dsi.getScale(); dsi.getSlideCount(); } } } /** * <p>This test method test the writing of properties in the well-known * property set streams "SummaryInformation" and * "DocumentSummaryInformation" by performing the following steps:</p> * * <ol> * * <li><p>Read a test document <em>doc1</em> into a POI filesystem.</p></li> * * <li><p>Read the summary information stream and the document summary * information stream from the POI filesystem.</p></li> * * <li><p>Write all properties supported by HPSF to the summary * information (e.g. author, edit date, application name) and to the * document summary information (e.g. company, manager).</p></li> * * <li><p>Write the summary information stream and the document summary * information stream to the POI filesystem.</p></li> * * <li><p>Write the POI filesystem to a (temporary) file <em>doc2</em> * and close the latter.</p></li> * * <li><p>Open <em>doc2</em> for reading and check summary information * and document summary information. All properties written before must be * found in the property streams of <em>doc2</em> and have the correct * values.</p></li> * * <li><p>Remove all properties supported by HPSF from the summary * information (e.g. author, edit date, application name) and from the * document summary information (e.g. company, manager).</p></li> * * <li><p>Write the summary information stream and the document summary * information stream to the POI filesystem.</p></li> * * <li><p>Write the POI filesystem to a (temporary) file <em>doc3</em> * and close the latter.</p></li> * * <li><p>Open <em>doc3</em> for reading and check summary information * and document summary information. All properties removed before must not * be found in the property streams of <em>doc3</em>.</p></li> </ol> * * @throws IOException if some I/O error occurred. * @throws MarkUnsupportedException * @throws NoPropertySetStreamException * @throws UnexpectedPropertySetTypeException * @throws WritingNotSupportedException */ public void testWriteWellKnown() throws IOException, NoPropertySetStreamException, MarkUnsupportedException, UnexpectedPropertySetTypeException, WritingNotSupportedException { final String dataDirName = System.getProperty("HPSF.testdata.path"); final File dataDir = new File(dataDirName); final File doc1 = new File(dataDir, POI_FS); /* Read a test document <em>doc1</em> into a POI filesystem. */ POIFSFileSystem poifs = new POIFSFileSystem(new FileInputStream(doc1)); DirectoryEntry dir = poifs.getRoot(); DocumentEntry siEntry = (DocumentEntry) dir.getEntry(SummaryInformation.DEFAULT_STREAM_NAME); DocumentEntry dsiEntry = (DocumentEntry) dir.getEntry(DocumentSummaryInformation.DEFAULT_STREAM_NAME); /* * Read the summary information stream and the document summary * information stream from the POI filesystem. * * Please note that the result consists of SummaryInformation and * DocumentSummaryInformation instances which are in memory only. To * make them permanent they have to be written to a POI filesystem * explicitly (overwriting the former contents). Then the POI filesystem * should be saved to a file. */ DocumentInputStream dis = new DocumentInputStream(siEntry); PropertySet ps = new PropertySet(dis); SummaryInformation si = new SummaryInformation(ps); dis = new DocumentInputStream(dsiEntry); ps = new PropertySet(dis); DocumentSummaryInformation dsi = new DocumentSummaryInformation(ps); /* * Write all properties supported by HPSF to the summary information * (e.g. author, edit date, application name) and to the document * summary information (e.g. company, manager). */ Calendar cal = new GregorianCalendar(); cal.set(2000, 6, 6, 6, 6, 6); final long time1 = cal.getTimeInMillis(); cal.set(2001, 7, 7, 7, 7, 7); final long time2 = cal.getTimeInMillis(); cal.set(2002, 8, 8, 8, 8, 8); final long time3 = cal.getTimeInMillis(); int nr = 4711; final String P_APPLICATION_NAME = "ApplicationName"; final String P_AUTHOR = "Author"; final int P_CHAR_COUNT = ++nr; final String P_COMMENTS = "Comments"; final Date P_CREATE_DATE_TIME = new Date(time1); final long P_EDIT_TIME = ++nr * 1000 * 10; final String P_KEYWORDS = "Keywords"; final String P_LAST_AUTHOR = "LastAuthor"; final Date P_LAST_PRINTED = new Date(time2); final Date P_LAST_SAVE_DATE_TIME = new Date(time3); final int P_PAGE_COUNT = ++nr; final String P_REV_NUMBER = "RevNumber"; final int P_SECURITY = 1; final String P_SUBJECT = "Subject"; final String P_TEMPLATE = "Template"; // FIXME (byte array properties not yet implemented): final byte[] P_THUMBNAIL = new byte[123]; final String P_TITLE = "Title"; final int P_WORD_COUNT = ++nr; final int P_BYTE_COUNT = ++nr; final String P_CATEGORY = "Category"; final String P_COMPANY = "Company"; // FIXME (byte array properties not yet implemented): final byte[] P_DOCPARTS = new byte[123]; // FIXME (byte array properties not yet implemented): final byte[] P_HEADING_PAIR = new byte[123]; final int P_HIDDEN_COUNT = ++nr; final int P_LINE_COUNT = ++nr; final boolean P_LINKS_DIRTY = true; final String P_MANAGER = "Manager"; final int P_MM_CLIP_COUNT = ++nr; final int P_NOTE_COUNT = ++nr; final int P_PAR_COUNT = ++nr; final String P_PRESENTATION_FORMAT = "PresentationFormat"; final boolean P_SCALE = false; final int P_SLIDE_COUNT = ++nr; final Date now = new Date(); final Integer POSITIVE_INTEGER = new Integer(2222); final Long POSITIVE_LONG = new Long(3333); final Double POSITIVE_DOUBLE = new Double(4444); final Integer NEGATIVE_INTEGER = new Integer(2222); final Long NEGATIVE_LONG = new Long(3333); final Double NEGATIVE_DOUBLE = new Double(4444); final Integer MAX_INTEGER = new Integer(Integer.MAX_VALUE); final Integer MIN_INTEGER = new Integer(Integer.MIN_VALUE); final Long MAX_LONG = new Long(Long.MAX_VALUE); final Long MIN_LONG = new Long(Long.MIN_VALUE); final Double MAX_DOUBLE = new Double(Double.MAX_VALUE); final Double MIN_DOUBLE = new Double(Double.MIN_VALUE); si.setApplicationName(P_APPLICATION_NAME); si.setAuthor(P_AUTHOR); si.setCharCount(P_CHAR_COUNT); si.setComments(P_COMMENTS); si.setCreateDateTime(P_CREATE_DATE_TIME); si.setEditTime(P_EDIT_TIME); si.setKeywords(P_KEYWORDS); si.setLastAuthor(P_LAST_AUTHOR); si.setLastPrinted(P_LAST_PRINTED); si.setLastSaveDateTime(P_LAST_SAVE_DATE_TIME); si.setPageCount(P_PAGE_COUNT); si.setRevNumber(P_REV_NUMBER); si.setSecurity(P_SECURITY); si.setSubject(P_SUBJECT); si.setTemplate(P_TEMPLATE); // FIXME (byte array properties not yet implemented): si.setThumbnail(P_THUMBNAIL); si.setTitle(P_TITLE); si.setWordCount(P_WORD_COUNT); dsi.setByteCount(P_BYTE_COUNT); dsi.setCategory(P_CATEGORY); dsi.setCompany(P_COMPANY); // FIXME (byte array properties not yet implemented): dsi.setDocparts(P_DOCPARTS); // FIXME (byte array properties not yet implemented): dsi.setHeadingPair(P_HEADING_PAIR); dsi.setHiddenCount(P_HIDDEN_COUNT); dsi.setLineCount(P_LINE_COUNT); dsi.setLinksDirty(P_LINKS_DIRTY); dsi.setManager(P_MANAGER); dsi.setMMClipCount(P_MM_CLIP_COUNT); dsi.setNoteCount(P_NOTE_COUNT); dsi.setParCount(P_PAR_COUNT); dsi.setPresentationFormat(P_PRESENTATION_FORMAT); dsi.setScale(P_SCALE); dsi.setSlideCount(P_SLIDE_COUNT); CustomProperties customProperties = dsi.getCustomProperties(); if (customProperties == null) customProperties = new CustomProperties(); customProperties.put("Schl黶sel
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -