]> source.dussan.org Git - poi.git/commitdiff
POIXMLPropertiesTextExtractor support for extracting custom OOXML properties as text
authorNick Burch <nick@apache.org>
Wed, 11 Jan 2012 15:51:10 +0000 (15:51 +0000)
committerNick Burch <nick@apache.org>
Wed, 11 Jan 2012 15:51:10 +0000 (15:51 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1230106 13f79535-47bb-0310-9956-ffa450edef68

src/documentation/content/xdocs/status.xml
src/ooxml/java/org/apache/poi/POIXMLPropertiesTextExtractor.java
src/ooxml/testcases/org/apache/poi/TestXMLPropertiesTextExtractor.java

index 38c7666b965e9d0a906f63776e5c9c5518912410..3a1e1999fe512af108e8663bd27863f7685b1ce5 100644 (file)
@@ -34,6 +34,7 @@
 
     <changes>
         <release version="3.8-beta6" date="2012-??-??">
+           <action dev="poi-developers" type="add">POIXMLPropertiesTextExtractor support for extracting custom OOXML properties as text</action>
            <action dev="poi-developers" type="fix">52449 - Support writing XWPF documents with glossaries (Glossaries are not yet supported, but can now be written out again without changes)</action>
            <action dev="poi-developers" type="fix">52446 - Handle files which have been truncated by a few bytes in NPropertyTable</action>
            <action dev="poi-developers" type="fix">52438 - Update CellDateFormatter to handle times without seconds</action>
index 22ff8902c3f4b42f1901cf275f4a3b98d57e24fa..05528f6285e1b981b19e210765fa89a620e87fcf 100644 (file)
@@ -20,6 +20,7 @@ package org.apache.poi;
 import org.apache.poi.openxml4j.opc.internal.PackagePropertiesPart;
 import org.openxmlformats.schemas.officeDocument.x2006.customProperties.CTProperty;
 
+import java.math.BigDecimal;
 import java.util.Date;
 import java.util.List;
 
@@ -131,12 +132,42 @@ public class POIXMLPropertiesTextExtractor extends POIXMLTextExtractor {
                        props = getDocument().getProperties().getCustomProperties().getUnderlyingProperties();
 
                List<CTProperty> properties = props.getPropertyList();
-               for(int i = 0; i<properties.size(); i++) {
-                       // TODO - finish off
+               for(CTProperty property : properties) {
                        String val = "(not implemented!)";
-
+                       
+                       if (property.isSetLpwstr()) {
+                          val = property.getLpwstr(); 
+                       }
+                       else if (property.isSetFiletime()) {
+                          val = property.getFiletime().toString(); 
+                       }
+                       else if (property.isSetDate()) {
+                          val = property.getDate().toString(); 
+                       }
+                       else if (property.isSetDecimal()) {
+                          BigDecimal d = property.getDecimal();
+                          if (d == null) {
+                             val = null;
+                          } else {
+                             val = d.toPlainString();
+                          }
+                       }
+                       else if (property.isSetBool()) {
+                          val = Boolean.toString( property.getBool() );
+                       }
+                       else if (property.isSetInt()) {
+                          val = Integer.toString( property.getInt() ); 
+                       }
+                       else if (property.isSetLpstr()) {
+                          val = property.getLpstr(); 
+                       }
+                       else if (property.isSetI4()) {
+                          /* Number in Excel for example.... Why i4 ? Ask microsoft. */ 
+                          val = Integer.toString(property.getI4()); 
+                       }
+                       
                        text.append(
-                                       properties.get(i).getName() +
+                                       property.getName() +
                                        " = " + val + "\n"
                        );
                }
index b19477f04cec7c617466ef82d5a94b3aa6a8d7e1..8ad2f78c4b5d41011302ce659a0073dee1a0b243 100644 (file)
@@ -84,8 +84,21 @@ public final class TestXMLPropertiesTextExtractor extends TestCase {
                assertTrue(eText.contains("Company = Mera"));
        }
 
-       public void testCustom() {
-               // TODO!
+       public void testCustom() throws Exception {
+      OPCPackage pkg = OPCPackage.open(
+                _ssSamples.openResourceAsStream("ExcelWithAttachments.xlsm")
+      );
+      XSSFWorkbook wb = new XSSFWorkbook(pkg);
+
+      POIXMLPropertiesTextExtractor ext = new POIXMLPropertiesTextExtractor(wb);
+      ext.getText();
+
+      // Now check
+      String text = ext.getText();
+      String cText = ext.getCustomPropertiesText();
+      
+      assertTrue(text.contains("description = another value"));
+      assertTrue(cText.contains("description = another value"));
        }
        
        /**