POIXMLproperties: created, creator, modified, lastprinted, identifier, revision + test

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@796239 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Paolo Mottadelli 2009-07-21 10:23:05 +00:00
parent 76752c5db7
commit 15e434d684
3 changed files with 123 additions and 25 deletions

View File

@ -18,16 +18,22 @@ package org.apache.poi;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import java.util.Map; import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map;
import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.opc.*; import org.apache.poi.openxml4j.exceptions.OpenXML4JException;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.openxml4j.opc.PackagePart;
import org.apache.poi.openxml4j.opc.PackagePartName;
import org.apache.poi.openxml4j.opc.PackageRelationshipCollection;
import org.apache.poi.openxml4j.opc.PackagingURIHelper;
import org.apache.poi.openxml4j.opc.TargetMode;
import org.apache.poi.openxml4j.opc.internal.PackagePropertiesPart; import org.apache.poi.openxml4j.opc.internal.PackagePropertiesPart;
import org.apache.poi.openxml4j.util.Nullable;
import org.apache.xmlbeans.XmlException; import org.apache.xmlbeans.XmlException;
import org.apache.xmlbeans.XmlOptions; import org.apache.xmlbeans.XmlOptions;
import org.openxmlformats.schemas.officeDocument.x2006.extendedProperties.PropertiesDocument;
/** /**
* Wrapper around the two different kinds of OOXML properties * Wrapper around the two different kinds of OOXML properties
@ -159,11 +165,32 @@ public class POIXMLProperties {
this.part = part; this.part = part;
} }
public void setTitle(String title) { public String getCategory() {
part.setTitleProperty(title); return part.getCategoryProperty().getValue();
} }
public String getTitle() { public void setCategory(String category) {
return part.getTitleProperty().getValue(); part.setCategoryProperty(category);
}
public String getContentStatus() {
return part.getContentStatusProperty().getValue();
}
public void setContentStatus(String contentStatus) {
part.setContentStatusProperty(contentStatus);
}
public String getContentType() {
return part.getContentTypeProperty().getValue();
}
public void setContentType(String contentType) {
part.setContentTypeProperty(contentType);
}
public Date getCreated() {
return part.getCreatedProperty().getValue();
}
public void setCreated(Nullable<Date> date) {
part.setCreatedProperty(date);
}
public void setCreated(String date) {
part.setCreatedProperty(date);
} }
public String getCreator() { public String getCreator() {
return part.getCreatorProperty().getValue(); return part.getCreatorProperty().getValue();
@ -171,12 +198,58 @@ public class POIXMLProperties {
public void setCreator(String creator) { public void setCreator(String creator) {
part.setCreatorProperty(creator); part.setCreatorProperty(creator);
} }
public String getDescription() {
return part.getDescriptionProperty().getValue();
}
public void setDescription(String description) {
part.setDescriptionProperty(description);
}
public String getIdentifier() {
return part.getIdentifierProperty().getValue();
}
public void setIdentifier(String identifier) {
part.setIdentifierProperty(identifier);
}
public Date getLastPrinted() {
return part.getLastPrintedProperty().getValue();
}
public void setLastPrinted(Nullable<Date> date) {
part.setLastPrintedProperty(date);
}
public void setLastPrinted(String date) {
part.setLastPrintedProperty(date);
}
public Date getModified() {
return part.getModifiedProperty().getValue();
}
public void setModified(Nullable<Date> date) {
part.setModifiedProperty(date);
}
public void setModified(String date) {
part.setModifiedProperty(date);
}
public String getSubject() { public String getSubject() {
return part.getSubjectProperty().getValue(); return part.getSubjectProperty().getValue();
} }
public void setSubjectProperty(String subject) { public void setSubjectProperty(String subject) {
part.setSubjectProperty(subject); part.setSubjectProperty(subject);
} }
public void setTitle(String title) {
part.setTitleProperty(title);
}
public String getTitle() {
return part.getTitleProperty().getValue();
}
public String getRevision() {
return part.getRevisionProperty().getValue();
}
public void setRevision(String revision) {
try {
new Long(revision);
part.setRevisionProperty(revision);
}
catch (NumberFormatException e) {}
}
public PackagePropertiesPart getUnderlyingProperties() { public PackagePropertiesPart getUnderlyingProperties() {
return part; return part;

View File

@ -19,10 +19,12 @@
package org.apache.poi; package org.apache.poi;
import java.io.File; import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import junit.framework.TestCase; import junit.framework.TestCase;
import org.apache.poi.openxml4j.opc.OPCPackage; import org.apache.poi.POIXMLProperties.CoreProperties;
import org.apache.poi.xssf.XSSFTestDataSamples; import org.apache.poi.xssf.XSSFTestDataSamples;
import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.apache.poi.xwpf.usermodel.XWPFDocument;
@ -31,6 +33,23 @@ import org.apache.poi.xwpf.usermodel.XWPFDocument;
* Test setting extended and custom OOXML properties * Test setting extended and custom OOXML properties
*/ */
public class TestPOIXMLProperties extends TestCase { public class TestPOIXMLProperties extends TestCase {
POIXMLProperties props;
CoreProperties coreProperties;
public void setUp() throws Exception{
File sampleFile = new File(
System.getProperty("HWPF.testdata.path") +
File.separator + "documentProperties.docx"
);
assertTrue(sampleFile.exists());
XWPFDocument sampleDoc;
sampleDoc = new XWPFDocument(
POIXMLDocument.openPackage(sampleFile.toString())
);
props = sampleDoc.getProperties();
coreProperties = props.getCoreProperties();
assertNotNull(props);
}
public void testWorkbookExtendedProperties() throws Exception { public void testWorkbookExtendedProperties() throws Exception {
XSSFWorkbook workbook = new XSSFWorkbook(); XSSFWorkbook workbook = new XSSFWorkbook();
@ -137,23 +156,29 @@ public class TestPOIXMLProperties extends TestCase {
} }
public void testDocumentProperties() throws Exception { public void testDocumentProperties() {
File sampleFile = new File( String category = coreProperties.getCategory();
System.getProperty("HWPF.testdata.path") + assertEquals("test", category);
File.separator + "documentProperties.docx" String contentStatus = "Draft";
); coreProperties.setContentStatus(contentStatus);
assertTrue(sampleFile.exists()); assertEquals("Draft", contentStatus);
XWPFDocument sampleDoc; Date created = coreProperties.getCreated();
sampleDoc = new XWPFDocument( SimpleDateFormat formatter = new SimpleDateFormat("EEE, MMM d, ''yy");
POIXMLDocument.openPackage(sampleFile.toString()) assertEquals("Mon, Jul 20, '09", formatter.format(created));
); String creator = coreProperties.getCreator();
POIXMLProperties props = sampleDoc.getProperties();
assertNotNull(props);
String title = props.getCoreProperties().getTitle();
assertEquals("Hello World", title);
String creator = props.getCoreProperties().getCreator();
assertEquals("Paolo Mottadelli", creator); assertEquals("Paolo Mottadelli", creator);
String subject = props.getCoreProperties().getSubject(); String subject = coreProperties.getSubject();
assertEquals("Greetings", subject); assertEquals("Greetings", subject);
String title = coreProperties.getTitle();
assertEquals("Hello World", title);
}
public void testGetSetRevision() {
String revision = coreProperties.getRevision();
assertTrue("Revision number is 1", new Integer(coreProperties.getRevision()).intValue() > 1);
coreProperties.setRevision("20");
assertEquals("20", coreProperties.getRevision());
coreProperties.setRevision("20xx");
assertEquals("20", coreProperties.getRevision());
} }
} }