Search in sources :

Example 11 with CustomProperties

use of org.apache.poi.hpsf.CustomProperties in project poi by apache.

the class TestWriteWellKnown method testWriteWellKnown.

/**
     * <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
     */
@Test
public void testWriteWellKnown() throws Exception {
    POIDataSamples _samples = POIDataSamples.getHPSFInstance();
    final File doc1 = TempFile.createTempFile("POI_HPSF_Test1.", ".tmp");
    final File doc2 = TempFile.createTempFile("POI_HPSF_Test2.", ".tmp");
    final File doc3 = TempFile.createTempFile("POI_HPSF_Test3.", ".tmp");
    FileInputStream fis = new FileInputStream(_samples.getFile(POI_FS));
    FileOutputStream fos = new FileOutputStream(doc1);
    IOUtils.copy(fis, fos);
    fos.close();
    fis.close();
    CustomProperties cps1 = write1stFile(doc1, doc2);
    CustomProperties cps2 = write2ndFile(doc2, doc3);
    write3rdFile(doc3, null);
    assertEquals(cps1, cps2);
}
Also used : FileOutputStream(java.io.FileOutputStream) POIDataSamples(org.apache.poi.POIDataSamples) TempFile(org.apache.poi.util.TempFile) File(java.io.File) FileInputStream(java.io.FileInputStream) CustomProperties(org.apache.poi.hpsf.CustomProperties) Test(org.junit.Test)

Example 12 with CustomProperties

use of org.apache.poi.hpsf.CustomProperties in project poi by apache.

the class TestMetaDataIPI method testTwo.

/**
	 * Test very long input in each of the fields (approx 30-60KB each)
	 */
@Test
public void testTwo() throws Exception {
    String company = elongate("company");
    String manager = elongate("manager");
    String category = elongate("category");
    String title = elongate("title");
    String author = elongate("author");
    String comments = elongate("comments");
    String keywords = elongate("keywords");
    String subject = elongate("subject");
    String p1 = elongate("p1");
    String p2 = elongate("p2");
    String k1 = elongate("k1");
    String k2 = elongate("k2");
    dsi.setCompany(company);
    dsi.setManager(manager);
    dsi.setCategory(category);
    si.setTitle(title);
    si.setAuthor(author);
    si.setComments(comments);
    si.setKeywords(keywords);
    si.setSubject(subject);
    CustomProperties customProperties = dsi.getCustomProperties();
    if (customProperties == null) {
        customProperties = new CustomProperties();
    }
    /* Insert some custom properties into the container. */
    customProperties.put(k1, p1);
    customProperties.put(k2, p2);
    customProperties.put("Sample Number", new Integer(12345));
    customProperties.put("Sample Boolean", Boolean.TRUE);
    Date date = new Date();
    customProperties.put("Sample Date", date);
    dsi.setCustomProperties(customProperties);
    closeAndReOpen();
    assertNotNull(dsi);
    assertNotNull(si);
    /*
		 * Change the category to "POI example". Any former category value will
		 * be lost. If there has been no category yet, it will be created.
		 */
    assertEquals("Category", category, dsi.getCategory());
    assertEquals("Company", company, dsi.getCompany());
    assertEquals("Manager", manager, dsi.getManager());
    assertEquals("", author, si.getAuthor());
    assertEquals("", title, si.getTitle());
    assertEquals("", comments, si.getComments());
    assertEquals("", keywords, si.getKeywords());
    assertEquals("", subject, si.getSubject());
    /*
		 * Read the custom properties. If there are no custom properties yet,
		 * the application has to create a new CustomProperties object. It will
		 * serve as a container for custom properties.
		 */
    customProperties = dsi.getCustomProperties();
    if (customProperties == null) {
        fail();
    }
    /* Insert some custom properties into the container. */
    String a1 = (String) customProperties.get(k1);
    assertEquals("Key1", p1, a1);
    String a2 = (String) customProperties.get(k2);
    assertEquals("Schlüssel2", p2, a2);
    Integer a3 = (Integer) customProperties.get("Sample Number");
    assertEquals("Sample Number", new Integer(12345), a3);
    Boolean a4 = (Boolean) customProperties.get("Sample Boolean");
    assertEquals("Sample Boolean", Boolean.TRUE, a4);
    Date a5 = (Date) customProperties.get("Sample Date");
    assertEquals("Custom Date:", date, a5);
}
Also used : Date(java.util.Date) CustomProperties(org.apache.poi.hpsf.CustomProperties) Test(org.junit.Test)

Example 13 with CustomProperties

use of org.apache.poi.hpsf.CustomProperties in project poi by apache.

the class TestMetaDataIPI method testThree.

/**
	 * Tests with strange characters in keys and data (Umlaute etc.)
	 */
@Test
public void testThree() throws Exception {
    String company = strangize("company");
    String manager = strangize("manager");
    String category = strangize("category");
    String title = strangize("title");
    String author = strangize("author");
    String comments = strangize("comments");
    String keywords = strangize("keywords");
    String subject = strangize("subject");
    String p1 = strangize("p1");
    String p2 = strangize("p2");
    String k1 = strangize("k1");
    String k2 = strangize("k2");
    dsi.setCompany(company);
    dsi.setManager(manager);
    dsi.setCategory(category);
    si.setTitle(title);
    si.setAuthor(author);
    si.setComments(comments);
    si.setKeywords(keywords);
    si.setSubject(subject);
    CustomProperties customProperties = dsi.getCustomProperties();
    if (customProperties == null) {
        customProperties = new CustomProperties();
    }
    /* Insert some custom properties into the container. */
    customProperties.put(k1, p1);
    customProperties.put(k2, p2);
    customProperties.put("Sample Number", new Integer(12345));
    customProperties.put("Sample Boolean", Boolean.FALSE);
    Date date = new Date(0);
    customProperties.put("Sample Date", date);
    dsi.setCustomProperties(customProperties);
    closeAndReOpen();
    assertNotNull(dsi);
    assertNotNull(si);
    /*
		 * Change the category to "POI example". Any former category value will
		 * be lost. If there has been no category yet, it will be created.
		 */
    assertEquals("Category", category, dsi.getCategory());
    assertEquals("Company", company, dsi.getCompany());
    assertEquals("Manager", manager, dsi.getManager());
    assertEquals("", author, si.getAuthor());
    assertEquals("", title, si.getTitle());
    assertEquals("", comments, si.getComments());
    assertEquals("", keywords, si.getKeywords());
    assertEquals("", subject, si.getSubject());
    /*
		 * Read the custom properties. If there are no custom properties yet,
		 * the application has to create a new CustomProperties object. It will
		 * serve as a container for custom properties.
		 */
    customProperties = dsi.getCustomProperties();
    if (customProperties == null) {
        fail();
    }
    /* Insert some custom properties into the container. */
    // System.out.println(k1);
    String a1 = (String) customProperties.get(k1);
    assertEquals("Key1", p1, a1);
    String a2 = (String) customProperties.get(k2);
    assertEquals("Schlüssel2", p2, a2);
    Integer a3 = (Integer) customProperties.get("Sample Number");
    assertEquals("Sample Number", new Integer(12345), a3);
    Boolean a4 = (Boolean) customProperties.get("Sample Boolean");
    assertEquals("Sample Boolean", Boolean.FALSE, a4);
    Date a5 = (Date) customProperties.get("Sample Date");
    assertEquals("Custom Date:", date, a5);
}
Also used : Date(java.util.Date) CustomProperties(org.apache.poi.hpsf.CustomProperties) Test(org.junit.Test)

Aggregations

CustomProperties (org.apache.poi.hpsf.CustomProperties)13 Test (org.junit.Test)9 Date (java.util.Date)6 DocumentSummaryInformation (org.apache.poi.hpsf.DocumentSummaryInformation)6 NPOIFSFileSystem (org.apache.poi.poifs.filesystem.NPOIFSFileSystem)4 FileOutputStream (java.io.FileOutputStream)3 SummaryInformation (org.apache.poi.hpsf.SummaryInformation)3 File (java.io.File)2 CustomProperty (org.apache.poi.hpsf.CustomProperty)2 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 OutputStream (java.io.OutputStream)1 HashMap (java.util.HashMap)1 POIDataSamples (org.apache.poi.POIDataSamples)1 MutableProperty (org.apache.poi.hpsf.MutableProperty)1 MutableSection (org.apache.poi.hpsf.MutableSection)1 DirectoryEntry (org.apache.poi.poifs.filesystem.DirectoryEntry)1 TempFile (org.apache.poi.util.TempFile)1