Search in sources :

Example 1 with CustomProperties

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

the class ModifyDocumentSummaryInformation method main.

/**
     * <p>Main method - see class description.</p>
     *
     * @param args The command-line parameters.
     * @throws IOException
     * @throws MarkUnsupportedException
     * @throws NoPropertySetStreamException
     * @throws UnexpectedPropertySetTypeException
     * @throws WritingNotSupportedException
     */
public static void main(final String[] args) throws IOException, NoPropertySetStreamException, MarkUnsupportedException, UnexpectedPropertySetTypeException, WritingNotSupportedException {
    /* Read the name of the POI filesystem to modify from the command line.
         * For brevity to boundary check is performed on the command-line
         * arguments. */
    File summaryFile = new File(args[0]);
    /* Open the POI filesystem. */
    NPOIFSFileSystem poifs = new NPOIFSFileSystem(summaryFile, false);
    /* Read the summary information. */
    DirectoryEntry dir = poifs.getRoot();
    SummaryInformation si;
    try {
        si = (SummaryInformation) PropertySetFactory.create(dir, SummaryInformation.DEFAULT_STREAM_NAME);
    } catch (FileNotFoundException ex) {
        // There is no summary information yet. We have to create a new one
        si = PropertySetFactory.newSummaryInformation();
    }
    /* Change the author to "Rainer Klute". Any former author value will
         * be lost. If there has been no author yet, it will be created. */
    si.setAuthor("Rainer Klute");
    System.out.println("Author changed to " + si.getAuthor() + ".");
    /* Handling the document summary information is analogous to handling
         * the summary information. An additional feature, however, are the
         * custom properties. */
    /* Read the document summary information. */
    DocumentSummaryInformation dsi;
    try {
        dsi = (DocumentSummaryInformation) PropertySetFactory.create(dir, DocumentSummaryInformation.DEFAULT_STREAM_NAME);
    } catch (FileNotFoundException ex) {
        /* There is no document summary information yet. We have to create a
             * new one. */
        dsi = PropertySetFactory.newDocumentSummaryInformation();
    }
    /* Change the category to "POI example". Any former category value will
         * be lost. If there has been no category yet, it will be created. */
    dsi.setCategory("POI example");
    System.out.println("Category changed to " + dsi.getCategory() + ".");
    /* 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 customProperties = dsi.getCustomProperties();
    if (customProperties == null)
        customProperties = new CustomProperties();
    /* Insert some custom properties into the container. */
    customProperties.put("Key 1", "Value 1");
    customProperties.put("Schlüssel 2", "Wert 2");
    customProperties.put("Sample Number", new Integer(12345));
    customProperties.put("Sample Boolean", Boolean.TRUE);
    customProperties.put("Sample Date", new Date());
    /* Read a custom property. */
    Object value = customProperties.get("Sample Number");
    System.out.println("Custom Sample Number is now " + value);
    /* Write the custom properties back to the document summary
         * information. */
    dsi.setCustomProperties(customProperties);
    /* Write the summary information and the document summary information
         * to the POI filesystem. */
    si.write(dir, SummaryInformation.DEFAULT_STREAM_NAME);
    dsi.write(dir, DocumentSummaryInformation.DEFAULT_STREAM_NAME);
    /* Write the POI filesystem back to the original file. Please note that
         * in production code you should take care when write directly to the 
         * origin, to make sure you don't loose things on error */
    poifs.writeFilesystem();
    poifs.close();
}
Also used : NPOIFSFileSystem(org.apache.poi.poifs.filesystem.NPOIFSFileSystem) SummaryInformation(org.apache.poi.hpsf.SummaryInformation) DocumentSummaryInformation(org.apache.poi.hpsf.DocumentSummaryInformation) FileNotFoundException(java.io.FileNotFoundException) DocumentSummaryInformation(org.apache.poi.hpsf.DocumentSummaryInformation) DirectoryEntry(org.apache.poi.poifs.filesystem.DirectoryEntry) File(java.io.File) Date(java.util.Date) CustomProperties(org.apache.poi.hpsf.CustomProperties)

Example 2 with CustomProperties

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

the class TestMetaDataIPI method testConvAndExistence.

/**
	 * Tests conversion in custom fields and errors
	 */
@Test
public void testConvAndExistence() throws Exception {
    CustomProperties customProperties = dsi.getCustomProperties();
    if (customProperties == null) {
        customProperties = new CustomProperties();
    }
    /* Insert some custom properties into the container. */
    customProperties.put("int", new Integer(12345));
    customProperties.put("negint", new Integer(-12345));
    customProperties.put("long", new Long(12345));
    customProperties.put("neglong", new Long(-12345));
    customProperties.put("boolean", Boolean.TRUE);
    customProperties.put("string", "a String");
    // customProperties.put("float", new Float(12345.0)); is not valid
    // customProperties.put("negfloat", new Float(-12345.1)); is not valid
    customProperties.put("double", new Double(12345.2));
    customProperties.put("negdouble", new Double(-12345.3));
    // customProperties.put("char", new Character('a')); is not valid
    Date date = new Date();
    customProperties.put("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.
		 */
    assertNull(dsi.getCategory());
    assertNull(dsi.getCompany());
    assertNull(dsi.getManager());
    assertNull(si.getAuthor());
    assertNull(si.getTitle());
    assertNull(si.getComments());
    assertNull(si.getKeywords());
    assertNull(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. */
    Integer a3 = (Integer) customProperties.get("int");
    assertEquals("int", new Integer(12345), a3);
    a3 = (Integer) customProperties.get("negint");
    assertEquals("negint", new Integer(-12345), a3);
    Long al = (Long) customProperties.get("neglong");
    assertEquals("neglong", new Long(-12345), al);
    al = (Long) customProperties.get("long");
    assertEquals("long", new Long(12345), al);
    Boolean a4 = (Boolean) customProperties.get("boolean");
    assertEquals("boolean", Boolean.TRUE, a4);
    Date a5 = (Date) customProperties.get("date");
    assertEquals("Custom Date:", date, a5);
    Double d = (Double) customProperties.get("double");
    assertEquals("int", new Double(12345.2), d);
    d = (Double) customProperties.get("negdouble");
    assertEquals("string", new Double(-12345.3), d);
    String s = (String) customProperties.get("string");
    assertEquals("sring", "a String", s);
    assertTrue(customProperties.get("string") instanceof String);
    assertTrue(customProperties.get("boolean") instanceof Boolean);
    assertTrue(customProperties.get("int") instanceof Integer);
    assertTrue(customProperties.get("negint") instanceof Integer);
    assertTrue(customProperties.get("long") instanceof Long);
    assertTrue(customProperties.get("neglong") instanceof Long);
    assertTrue(customProperties.get("double") instanceof Double);
    assertTrue(customProperties.get("negdouble") instanceof Double);
    assertTrue(customProperties.get("date") instanceof Date);
}
Also used : Date(java.util.Date) CustomProperties(org.apache.poi.hpsf.CustomProperties) Test(org.junit.Test)

Example 3 with CustomProperties

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

the class TestMetaDataIPI method testOne.

/**
	 * Sets the most important information in DocumentSummaryInformation and Summary Information and rereads it
	 */
@Test
public void testOne() throws Exception {
    // DocumentSummaryInformation
    dsi.setCompany("xxxCompanyxxx");
    dsi.setManager("xxxManagerxxx");
    dsi.setCategory("xxxCategoryxxx");
    // SummaryInformation
    si.setTitle("xxxTitlexxx");
    si.setAuthor("xxxAuthorxxx");
    si.setComments("xxxCommentsxxx");
    si.setKeywords("xxxKeyWordsxxx");
    si.setSubject("xxxSubjectxxx");
    // Custom Properties (in DocumentSummaryInformation
    CustomProperties customProperties = dsi.getCustomProperties();
    if (customProperties == null) {
        customProperties = new CustomProperties();
    }
    /* Insert some custom properties into the container. */
    customProperties.put("Key1", "Value1");
    customProperties.put("Schlüssel2", "Wert2");
    customProperties.put("Sample Integer", new Integer(12345));
    customProperties.put("Sample Boolean", Boolean.TRUE);
    Date date = new Date();
    customProperties.put("Sample Date", date);
    customProperties.put("Sample Double", new Double(-1.0001));
    customProperties.put("Sample Negative Integer", new Integer(-100000));
    dsi.setCustomProperties(customProperties);
    // start reading
    closeAndReOpen();
    // testing
    assertNotNull(dsi);
    assertNotNull(si);
    assertEquals("Category", "xxxCategoryxxx", dsi.getCategory());
    assertEquals("Company", "xxxCompanyxxx", dsi.getCompany());
    assertEquals("Manager", "xxxManagerxxx", dsi.getManager());
    assertEquals("", "xxxAuthorxxx", si.getAuthor());
    assertEquals("", "xxxTitlexxx", si.getTitle());
    assertEquals("", "xxxCommentsxxx", si.getComments());
    assertEquals("", "xxxKeyWordsxxx", si.getKeywords());
    assertEquals("", "xxxSubjectxxx", 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();
    assertNotNull(customProperties);
    /* Insert some custom properties into the container. */
    String a1 = (String) customProperties.get("Key1");
    assertEquals("Key1", "Value1", a1);
    String a2 = (String) customProperties.get("Schlüssel2");
    assertEquals("Schlüssel2", "Wert2", a2);
    Integer a3 = (Integer) customProperties.get("Sample Integer");
    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);
    Double a6 = (Double) customProperties.get("Sample Double");
    assertEquals("Custom Float", new Double(-1.0001), a6);
    Integer a7 = (Integer) customProperties.get("Sample Negative Integer");
    assertEquals("Neg", new Integer(-100000), a7);
}
Also used : Date(java.util.Date) CustomProperties(org.apache.poi.hpsf.CustomProperties) Test(org.junit.Test)

Example 4 with CustomProperties

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

the class TestReadAllFiles method readCustomPropertiesFromFiles.

/**
     * <p>Tests the simplified custom properties by reading them from the
     * available test files.</p>
     *
     * @throws Throwable if anything goes wrong.
     */
@Test
public void readCustomPropertiesFromFiles() throws Exception {
    /* Read a test document <em>doc</em> into a POI filesystem. */
    NPOIFSFileSystem poifs = new NPOIFSFileSystem(file);
    try {
        /*
             * If there is a document summry information stream, read it from
             * the POI filesystem, else create a new one.
             */
        DocumentSummaryInformation dsi = TestWriteWellKnown.getDocumentSummaryInformation(poifs);
        if (dsi == null) {
            dsi = PropertySetFactory.newDocumentSummaryInformation();
        }
        final CustomProperties cps = dsi.getCustomProperties();
        if (cps == null) {
            /* The document does not have custom properties. */
            return;
        }
        for (CustomProperty cp : cps.properties()) {
            assertNotNull(cp.getName());
            assertNotNull(cp.getValue());
        }
    } finally {
        poifs.close();
    }
}
Also used : NPOIFSFileSystem(org.apache.poi.poifs.filesystem.NPOIFSFileSystem) DocumentSummaryInformation(org.apache.poi.hpsf.DocumentSummaryInformation) CustomProperty(org.apache.poi.hpsf.CustomProperty) CustomProperties(org.apache.poi.hpsf.CustomProperties) Test(org.junit.Test)

Example 5 with CustomProperties

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

the class TestWriteWellKnown method testCustomerProperties.

/**
     * Tests basic custom property features.
     */
@Test
public void testCustomerProperties() {
    final String KEY = "Schlüssel ä";
    final String VALUE_1 = "Wert 1";
    final String VALUE_2 = "Wert 2";
    CustomProperty cp;
    CustomProperties cps = new CustomProperties();
    assertEquals(0, cps.size());
    /* After adding a custom property the size must be 1 and it must be
         * possible to extract the custom property from the map. */
    cps.put(KEY, VALUE_1);
    assertEquals(1, cps.size());
    Object v1 = cps.get(KEY);
    assertEquals(VALUE_1, v1);
    /* After adding a custom property with the same name the size must still
         * be one. */
    cps.put(KEY, VALUE_2);
    assertEquals(1, cps.size());
    Object v2 = cps.get(KEY);
    assertEquals(VALUE_2, v2);
    /* Removing the custom property must return the remove property and
         * reduce the size to 0. */
    cp = (CustomProperty) cps.remove(KEY);
    assertEquals(KEY, cp.getName());
    assertEquals(VALUE_2, cp.getValue());
    assertEquals(0, cps.size());
}
Also used : CustomProperty(org.apache.poi.hpsf.CustomProperty) 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