Search in sources :

Example 6 with CustomProperties

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

the class TestWriteWellKnown method testGetCustomerProperties.

/**
     * Tests reading custom properties from a section including reading
     * custom properties which are not pure.
     */
@Test
public void testGetCustomerProperties() {
    final int ID_1 = 2;
    final int ID_2 = 3;
    final String NAME_1 = "Schlüssel ä";
    final String VALUE_1 = "Wert 1";
    final Map<Long, String> dictionary = new HashMap<Long, String>();
    DocumentSummaryInformation dsi = PropertySetFactory.newDocumentSummaryInformation();
    CustomProperties cps;
    MutableSection s;
    /* A document summary information set stream by default does have custom properties. */
    cps = dsi.getCustomProperties();
    assertNull(cps);
    /* Test an empty custom properties set. */
    s = new MutableSection();
    s.setFormatID(SectionIDMap.DOCUMENT_SUMMARY_INFORMATION_ID[1]);
    // s.setCodepage(CodePageUtil.CP_UNICODE);
    dsi.addSection(s);
    cps = dsi.getCustomProperties();
    assertEquals(0, cps.size());
    /* Add a custom property. */
    MutableProperty p = new MutableProperty();
    p.setID(ID_1);
    p.setType(Variant.VT_LPWSTR);
    p.setValue(VALUE_1);
    s.setProperty(p);
    dictionary.put(Long.valueOf(ID_1), NAME_1);
    s.setDictionary(dictionary);
    cps = dsi.getCustomProperties();
    assertEquals(1, cps.size());
    assertTrue(cps.isPure());
    /* Add another custom property. */
    s.setProperty(ID_2, Variant.VT_LPWSTR, VALUE_1);
    dictionary.put(Long.valueOf(ID_2), NAME_1);
    s.setDictionary(dictionary);
    cps = dsi.getCustomProperties();
    assertEquals(1, cps.size());
    assertFalse(cps.isPure());
}
Also used : MutableProperty(org.apache.poi.hpsf.MutableProperty) HashMap(java.util.HashMap) MutableSection(org.apache.poi.hpsf.MutableSection) DocumentSummaryInformation(org.apache.poi.hpsf.DocumentSummaryInformation) CustomProperties(org.apache.poi.hpsf.CustomProperties) Test(org.junit.Test)

Example 7 with CustomProperties

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

the class TestWriteWellKnown method write2ndFile.

/*
     * 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.
     */
private static CustomProperties write2ndFile(File fileIn, File fileOut) throws Exception {
    NPOIFSFileSystem poifs = new NPOIFSFileSystem(fileIn, false);
    SummaryInformation si = getSummaryInformation(poifs);
    DocumentSummaryInformation dsi = getDocumentSummaryInformation(poifs);
    assertEquals(P_APPLICATION_NAME, si.getApplicationName());
    assertEquals(P_AUTHOR, si.getAuthor());
    assertEquals(P_CHAR_COUNT, si.getCharCount());
    assertEquals(P_COMMENTS, si.getComments());
    assertEquals(P_CREATE_DATE_TIME, si.getCreateDateTime());
    assertEquals(P_EDIT_TIME, si.getEditTime());
    assertEquals(P_KEYWORDS, si.getKeywords());
    assertEquals(P_LAST_AUTHOR, si.getLastAuthor());
    assertEquals(P_LAST_PRINTED, si.getLastPrinted());
    assertEquals(P_LAST_SAVE_DATE_TIME, si.getLastSaveDateTime());
    assertEquals(P_PAGE_COUNT, si.getPageCount());
    assertEquals(P_REV_NUMBER, si.getRevNumber());
    assertEquals(P_SECURITY, si.getSecurity());
    assertEquals(P_SUBJECT, si.getSubject());
    assertEquals(P_TEMPLATE, si.getTemplate());
    // FIXME (byte array properties not yet implemented): assertEquals(P_THUMBNAIL, si.getThumbnail());
    assertEquals(P_TITLE, si.getTitle());
    assertEquals(P_WORD_COUNT, si.getWordCount());
    assertEquals(P_BYTE_COUNT, dsi.getByteCount());
    assertEquals(P_CATEGORY, dsi.getCategory());
    assertEquals(P_COMPANY, dsi.getCompany());
    // FIXME (byte array properties not yet implemented): assertEquals(P_, dsi.getDocparts());
    // FIXME (byte array properties not yet implemented): assertEquals(P_, dsi.getHeadingPair());
    assertEquals(P_HIDDEN_COUNT, dsi.getHiddenCount());
    assertEquals(P_LINE_COUNT, dsi.getLineCount());
    assertEquals(P_LINKS_DIRTY, dsi.getLinksDirty());
    assertEquals(P_MANAGER, dsi.getManager());
    assertEquals(P_MM_CLIP_COUNT, dsi.getMMClipCount());
    assertEquals(P_NOTE_COUNT, dsi.getNoteCount());
    assertEquals(P_PAR_COUNT, dsi.getParCount());
    assertEquals(P_PRESENTATION_FORMAT, dsi.getPresentationFormat());
    assertEquals(P_SCALE, dsi.getScale());
    assertEquals(P_SLIDE_COUNT, dsi.getSlideCount());
    final CustomProperties cps = dsi.getCustomProperties();
    assertNotNull(cps);
    assertNull(cps.get("No value available"));
    assertEquals("Wert ä", cps.get("Schlüssel ä"));
    assertEquals("Wert äö", cps.get("Schlüssel äö"));
    assertEquals("Wert äöü", cps.get("Schlüssel äöü"));
    assertEquals("Wert äöüÖ", cps.get("Schlüssel äöüÖ"));
    assertEquals(POSITIVE_INTEGER, cps.get("positive_Integer"));
    assertEquals(POSITIVE_LONG, cps.get("positive_Long"));
    assertEquals(POSITIVE_DOUBLE, cps.get("positive_Double"));
    assertEquals(NEGATIVE_INTEGER, cps.get("negative_Integer"));
    assertEquals(NEGATIVE_LONG, cps.get("negative_Long"));
    assertEquals(NEGATIVE_DOUBLE, cps.get("negative_Double"));
    assertEquals(Boolean.TRUE, cps.get("Boolean"));
    assertEquals(now, cps.get("Date"));
    assertEquals(MAX_INTEGER, cps.get("max_Integer"));
    assertEquals(MIN_INTEGER, cps.get("min_Integer"));
    assertEquals(MAX_LONG, cps.get("max_Long"));
    assertEquals(MIN_LONG, cps.get("min_Long"));
    assertEquals(MAX_DOUBLE, cps.get("max_Double"));
    assertEquals(MIN_DOUBLE, cps.get("min_Double"));
    /* 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). */
    si.removeApplicationName();
    si.removeAuthor();
    si.removeCharCount();
    si.removeComments();
    si.removeCreateDateTime();
    si.removeEditTime();
    si.removeKeywords();
    si.removeLastAuthor();
    si.removeLastPrinted();
    si.removeLastSaveDateTime();
    si.removePageCount();
    si.removeRevNumber();
    si.removeSecurity();
    si.removeSubject();
    si.removeTemplate();
    si.removeThumbnail();
    si.removeTitle();
    si.removeWordCount();
    dsi.removeByteCount();
    dsi.removeCategory();
    dsi.removeCompany();
    dsi.removeCustomProperties();
    dsi.removeDocparts();
    dsi.removeHeadingPair();
    dsi.removeHiddenCount();
    dsi.removeLineCount();
    dsi.removeLinksDirty();
    dsi.removeManager();
    dsi.removeMMClipCount();
    dsi.removeNoteCount();
    dsi.removeParCount();
    dsi.removePresentationFormat();
    dsi.removeScale();
    dsi.removeSlideCount();
    // Write the summary information stream and the document summary
    // information stream to the POI filesystem.
    si.write(poifs.getRoot(), SummaryInformation.DEFAULT_STREAM_NAME);
    dsi.write(poifs.getRoot(), DocumentSummaryInformation.DEFAULT_STREAM_NAME);
    // Write the POI filesystem to a (temporary) file doc3 and close the latter.
    FileOutputStream out = new FileOutputStream(fileOut);
    poifs.writeFilesystem(out);
    out.close();
    poifs.close();
    return cps;
}
Also used : NPOIFSFileSystem(org.apache.poi.poifs.filesystem.NPOIFSFileSystem) SummaryInformation(org.apache.poi.hpsf.SummaryInformation) DocumentSummaryInformation(org.apache.poi.hpsf.DocumentSummaryInformation) FileOutputStream(java.io.FileOutputStream) DocumentSummaryInformation(org.apache.poi.hpsf.DocumentSummaryInformation) CustomProperties(org.apache.poi.hpsf.CustomProperties)

Example 8 with CustomProperties

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

the class HPSFPropertiesExtractor method getDocumentSummaryInformationText.

public String getDocumentSummaryInformationText() {
    if (document == null) {
        // event based extractor does not have a document
        return "";
    }
    DocumentSummaryInformation dsi = document.getDocumentSummaryInformation();
    StringBuilder text = new StringBuilder();
    // Normal properties
    text.append(getPropertiesText(dsi));
    // Now custom ones
    CustomProperties cps = dsi == null ? null : dsi.getCustomProperties();
    if (cps != null) {
        for (String key : cps.nameSet()) {
            String val = getPropertyValueText(cps.get(key));
            text.append(key).append(" = ").append(val).append("\n");
        }
    }
    // All done
    return text.toString();
}
Also used : DocumentSummaryInformation(org.apache.poi.hpsf.DocumentSummaryInformation) CustomProperties(org.apache.poi.hpsf.CustomProperties)

Example 9 with CustomProperties

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

the class TestMetaDataIPI method testUnicode.

/**
	 * Unicode test
	 */
@Test
public void testUnicode() throws Exception {
    String company = strangizeU("company");
    String manager = strangizeU("manager");
    String category = strangizeU("category");
    String title = strangizeU("title");
    String author = strangizeU("author");
    String comments = strangizeU("comments");
    String keywords = strangizeU("keywords");
    String subject = strangizeU("subject");
    String p1 = strangizeU("p1");
    String p2 = strangizeU("p2");
    String k1 = strangizeU("k1");
    String k2 = strangizeU("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 = 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();
    assertNotNull(customProperties);
    /* 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.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 10 with CustomProperties

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

the class TestWriteWellKnown method write1stFile.

/*
     * 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).
     */
private static CustomProperties write1stFile(File fileIn, File fileOut) throws Exception {
    /* Read a test document <em>doc1</em> into a POI filesystem. */
    NPOIFSFileSystem poifs = new NPOIFSFileSystem(fileIn, false);
    /*
         * 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.
         */
    SummaryInformation si = getSummaryInformation(poifs);
    DocumentSummaryInformation dsi = getDocumentSummaryInformation(poifs);
    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 cps = dsi.getCustomProperties();
    assertNull(cps);
    cps = new CustomProperties();
    cps.put("Schlüssel ä", "Wert ä");
    cps.put("Schlüssel äö", "Wert äö");
    cps.put("Schlüssel äöü", "Wert äöü");
    cps.put("Schlüssel äöüÖ", "Wert äöüÖ");
    cps.put("positive_Integer", POSITIVE_INTEGER);
    cps.put("positive_Long", POSITIVE_LONG);
    cps.put("positive_Double", POSITIVE_DOUBLE);
    cps.put("negative_Integer", NEGATIVE_INTEGER);
    cps.put("negative_Long", NEGATIVE_LONG);
    cps.put("negative_Double", NEGATIVE_DOUBLE);
    cps.put("Boolean", Boolean.TRUE);
    cps.put("Date", now);
    cps.put("max_Integer", MAX_INTEGER);
    cps.put("min_Integer", MIN_INTEGER);
    cps.put("max_Long", MAX_LONG);
    cps.put("min_Long", MIN_LONG);
    cps.put("max_Double", MAX_DOUBLE);
    cps.put("min_Double", MIN_DOUBLE);
    // Check the keys went in
    assertTrue(cps.containsKey("Schlüssel ä"));
    assertTrue(cps.containsKey("Boolean"));
    // Check the values went in
    assertEquals("Wert ä", cps.get("Schlüssel ä"));
    assertEquals(Boolean.TRUE, cps.get("Boolean"));
    assertTrue(cps.containsValue(Boolean.TRUE));
    assertTrue(cps.containsValue("Wert ä"));
    // Check that things that aren't in aren't in
    assertFalse(cps.containsKey("False Boolean"));
    assertFalse(cps.containsValue(Boolean.FALSE));
    // Save as our custom properties
    dsi.setCustomProperties(cps);
    /* Write the summary information stream and the document summary
         * information stream to the POI filesystem. */
    si.write(poifs.getRoot(), SummaryInformation.DEFAULT_STREAM_NAME);
    dsi.write(poifs.getRoot(), DocumentSummaryInformation.DEFAULT_STREAM_NAME);
    /* Write the POI filesystem to a (temporary) file <em>doc2</em>
         * and close the latter. */
    OutputStream out = new FileOutputStream(fileOut);
    poifs.writeFilesystem(out);
    out.close();
    poifs.close();
    return cps;
}
Also used : NPOIFSFileSystem(org.apache.poi.poifs.filesystem.NPOIFSFileSystem) SummaryInformation(org.apache.poi.hpsf.SummaryInformation) DocumentSummaryInformation(org.apache.poi.hpsf.DocumentSummaryInformation) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) DocumentSummaryInformation(org.apache.poi.hpsf.DocumentSummaryInformation) CustomProperties(org.apache.poi.hpsf.CustomProperties)

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