Author: nick Date: Mon Aug 4 15:48:39 2008 New Revision: 682533 URL: http://svn.apache.org/viewvc?rev=682533&view=rev Log: Fix bug #45543 - Optionally extract comment text with PowerPointExtractor, and initial hslf model support for comments
Added: poi/trunk/src/scratchpad/src/org/apache/poi/hslf/model/Comment.java (with props) poi/trunk/src/scratchpad/testcases/org/apache/poi/hslf/data/45543.ppt (with props) poi/trunk/src/scratchpad/testcases/org/apache/poi/hslf/data/WithComments.ppt (with props) Modified: poi/trunk/src/documentation/content/xdocs/changes.xml poi/trunk/src/documentation/content/xdocs/status.xml poi/trunk/src/scratchpad/src/org/apache/poi/hslf/extractor/PowerPointExtractor.java poi/trunk/src/scratchpad/src/org/apache/poi/hslf/model/Slide.java poi/trunk/src/scratchpad/src/org/apache/poi/hslf/record/RecordContainer.java poi/trunk/src/scratchpad/testcases/org/apache/poi/hslf/extractor/TextExtractor.java Modified: poi/trunk/src/documentation/content/xdocs/changes.xml URL: http://svn.apache.org/viewvc/poi/trunk/src/documentation/content/xdocs/changes.xml?rev=682533&r1=682532&r2=682533&view=diff ============================================================================== --- poi/trunk/src/documentation/content/xdocs/changes.xml (original) +++ poi/trunk/src/documentation/content/xdocs/changes.xml Mon Aug 4 15:48:39 2008 @@ -37,6 +37,7 @@ <!-- Don't forget to update status.xml too! --> <release version="3.1.1-alpha1" date="2008-??-??"> + <action dev="POI-DEVELOPERS" type="add">45543 - Optionally extract comment text with PowerPointExtractor, and initial hslf model support for comments</action> <action dev="POI-DEVELOPERS" type="fix">45538 - Include excel headers and footers in the output of ExcelExtractor</action> <action dev="POI-DEVELOPERS" type="fix">44894 - refactor duplicate logic from EventRecordFactory to RecordFactory</action> <action dev="POI-DEVELOPERS" type="add">Support for Headers / Footers in HSLF</action> Modified: poi/trunk/src/documentation/content/xdocs/status.xml URL: http://svn.apache.org/viewvc/poi/trunk/src/documentation/content/xdocs/status.xml?rev=682533&r1=682532&r2=682533&view=diff ============================================================================== --- poi/trunk/src/documentation/content/xdocs/status.xml (original) +++ poi/trunk/src/documentation/content/xdocs/status.xml Mon Aug 4 15:48:39 2008 @@ -34,6 +34,7 @@ <!-- Don't forget to update changes.xml too! --> <changes> <release version="3.1.1-alpha1" date="2008-??-??"> + <action dev="POI-DEVELOPERS" type="add">45543 - Optionally extract comment text with PowerPointExtractor, and initial hslf model support for comments</action> <action dev="POI-DEVELOPERS" type="fix">45538 - Include excel headers and footers in the output of ExcelExtractor</action> <action dev="POI-DEVELOPERS" type="fix">44894 - refactor duplicate logic from EventRecordFactory to RecordFactory</action> <action dev="POI-DEVELOPERS" type="add">Support for Headers / Footers in HSLF</action> Modified: poi/trunk/src/scratchpad/src/org/apache/poi/hslf/extractor/PowerPointExtractor.java URL: http://svn.apache.org/viewvc/poi/trunk/src/scratchpad/src/org/apache/poi/hslf/extractor/PowerPointExtractor.java?rev=682533&r1=682532&r2=682533&view=diff ============================================================================== --- poi/trunk/src/scratchpad/src/org/apache/poi/hslf/extractor/PowerPointExtractor.java (original) +++ poi/trunk/src/scratchpad/src/org/apache/poi/hslf/extractor/PowerPointExtractor.java Mon Aug 4 15:48:39 2008 @@ -27,6 +27,8 @@ import org.apache.poi.poifs.filesystem.POIFSFileSystem; import org.apache.poi.hslf.*; import org.apache.poi.hslf.model.*; +import org.apache.poi.hslf.record.Comment2000; +import org.apache.poi.hslf.record.Record; import org.apache.poi.hslf.usermodel.*; /** @@ -44,6 +46,7 @@ private boolean slidesByDefault = true; private boolean notesByDefault = false; + private boolean commentsByDefault = false; /** * Basic extractor. Returns all the text, and optionally all the notes @@ -57,16 +60,20 @@ } boolean notes = false; + boolean comments = false; String file; if(args.length > 1) { notes = true; file = args[1]; + if(args.length > 2) { + comments = true; + } } else { file = args[0]; } PowerPointExtractor ppe = new PowerPointExtractor(file); - System.out.println(ppe.getText(true,notes)); + System.out.println(ppe.getText(true,notes,comments)); ppe.close(); } @@ -127,6 +134,13 @@ public void setNotesByDefault(boolean notesByDefault) { this.notesByDefault = notesByDefault; } + /** + * Should a call to getText() return comments text? + * Default is no + */ + public void setCommentsByDefault(boolean commentsByDefault) { + this.commentsByDefault = commentsByDefault; + } /** * Fetches all the slide text from the slideshow, @@ -135,7 +149,7 @@ * to change this */ public String getText() { - return getText(slidesByDefault,notesByDefault); + return getText(slidesByDefault,notesByDefault,commentsByDefault); } /** @@ -153,6 +167,9 @@ * @param getNoteText fetch note text */ public String getText(boolean getSlideText, boolean getNoteText) { + return getText(getSlideText, getNoteText, commentsByDefault); + } + public String getText(boolean getSlideText, boolean getNoteText, boolean getCommentText) { StringBuffer ret = new StringBuffer(); if(getSlideText) { @@ -169,6 +186,18 @@ } } } + + if(getCommentText) { + Comment[] comments = slide.getComments(); + for(int j=0; j<comments.length; j++) { + ret.append( + comments[j].getAuthor() + + " - " + + comments[j].getText() + + "\n" + ); + } + } } if(getNoteText) { ret.append("\n"); Added: poi/trunk/src/scratchpad/src/org/apache/poi/hslf/model/Comment.java URL: http://svn.apache.org/viewvc/poi/trunk/src/scratchpad/src/org/apache/poi/hslf/model/Comment.java?rev=682533&view=auto ============================================================================== --- poi/trunk/src/scratchpad/src/org/apache/poi/hslf/model/Comment.java (added) +++ poi/trunk/src/scratchpad/src/org/apache/poi/hslf/model/Comment.java Mon Aug 4 15:48:39 2008 @@ -0,0 +1,54 @@ +package org.apache.poi.hslf.model; + +import org.apache.poi.hslf.record.Comment2000; + +public class Comment { + private Comment2000 comment2000; + + public Comment(Comment2000 comment2000) { + this.comment2000 = comment2000; + } + + protected Comment2000 getComment2000() { + return comment2000; + } + + /** + * Get the Author of this comment + */ + public String getAuthor() { + return comment2000.getAuthor(); + } + /** + * Set the Author of this comment + */ + public void setAuthor(String author) { + comment2000.setAuthor(author); + } + + /** + * Get the Author's Initials of this comment + */ + public String getAuthorInitials() { + return comment2000.getAuthorInitials(); + } + /** + * Set the Author's Initials of this comment + */ + public void setAuthorInitials(String initials) { + comment2000.setAuthorInitials(initials); + } + + /** + * Get the text of this comment + */ + public String getText() { + return comment2000.getText(); + } + /** + * Set the text of this comment + */ + public void setText(String text) { + comment2000.setText(text); + } +} Propchange: poi/trunk/src/scratchpad/src/org/apache/poi/hslf/model/Comment.java ------------------------------------------------------------------------------ svn:eol-style = native Modified: poi/trunk/src/scratchpad/src/org/apache/poi/hslf/model/Slide.java URL: http://svn.apache.org/viewvc/poi/trunk/src/scratchpad/src/org/apache/poi/hslf/model/Slide.java?rev=682533&r1=682532&r2=682533&view=diff ============================================================================== --- poi/trunk/src/scratchpad/src/org/apache/poi/hslf/model/Slide.java (original) +++ poi/trunk/src/scratchpad/src/org/apache/poi/hslf/model/Slide.java Mon Aug 4 15:48:39 2008 @@ -361,6 +361,59 @@ } return super.getColorScheme(); } + + /** + * Get the comment(s) for this slide. + * Note - for now, only works on PPT 2000 and + * PPT 2003 files. Doesn't work for PPT 97 + * ones, as they do their comments oddly. + */ + public Comment[] getComments() { + // If there are any, they're in + // ProgTags -> ProgBinaryTag -> BinaryTagData + RecordContainer progTags = (RecordContainer) + getSheetContainer().findFirstOfType( + RecordTypes.ProgTags.typeID + ); + if(progTags != null) { + RecordContainer progBinaryTag = (RecordContainer) + progTags.findFirstOfType( + RecordTypes.ProgBinaryTag.typeID + ); + if(progBinaryTag != null) { + RecordContainer binaryTags = (RecordContainer) + progBinaryTag.findFirstOfType( + RecordTypes.BinaryTagData.typeID + ); + if(binaryTags != null) { + // This is where they'll be + int count = 0; + for(int i=0; i<binaryTags.getChildRecords().length; i++) { + if(binaryTags.getChildRecords()[i] instanceof Comment2000) { + count++; + } + } + + // Now build + Comment[] comments = new Comment[count]; + count = 0; + for(int i=0; i<binaryTags.getChildRecords().length; i++) { + if(binaryTags.getChildRecords()[i] instanceof Comment2000) { + comments[i] = new Comment( + (Comment2000)binaryTags.getChildRecords()[i] + ); + count++; + } + } + + return comments; + } + } + } + + // None found + return new Comment[0]; + } public void draw(Graphics2D graphics){ MasterSheet master = getMasterSheet(); Modified: poi/trunk/src/scratchpad/src/org/apache/poi/hslf/record/RecordContainer.java URL: http://svn.apache.org/viewvc/poi/trunk/src/scratchpad/src/org/apache/poi/hslf/record/RecordContainer.java?rev=682533&r1=682532&r2=682533&view=diff ============================================================================== --- poi/trunk/src/scratchpad/src/org/apache/poi/hslf/record/RecordContainer.java (original) +++ poi/trunk/src/scratchpad/src/org/apache/poi/hslf/record/RecordContainer.java Mon Aug 4 15:48:39 2008 @@ -123,6 +123,20 @@ } + /** + * Finds the first child record of the given type, + * or null if none of the child records are of the + * given type. Does not descend. + */ + public Record findFirstOfType(long type) { + for(int i=0; i<_children.length; i++) { + if(_children[i].getRecordType() == type) { + return _children[i]; + } + } + return null; + } + /* =============================================================== * External Move Methods * =============================================================== Added: poi/trunk/src/scratchpad/testcases/org/apache/poi/hslf/data/45543.ppt URL: http://svn.apache.org/viewvc/poi/trunk/src/scratchpad/testcases/org/apache/poi/hslf/data/45543.ppt?rev=682533&view=auto ============================================================================== Binary file - no diff available. Propchange: poi/trunk/src/scratchpad/testcases/org/apache/poi/hslf/data/45543.ppt ------------------------------------------------------------------------------ svn:mime-type = application/octet-stream Added: poi/trunk/src/scratchpad/testcases/org/apache/poi/hslf/data/WithComments.ppt URL: http://svn.apache.org/viewvc/poi/trunk/src/scratchpad/testcases/org/apache/poi/hslf/data/WithComments.ppt?rev=682533&view=auto ============================================================================== Binary file - no diff available. Propchange: poi/trunk/src/scratchpad/testcases/org/apache/poi/hslf/data/WithComments.ppt ------------------------------------------------------------------------------ svn:executable = * Propchange: poi/trunk/src/scratchpad/testcases/org/apache/poi/hslf/data/WithComments.ppt ------------------------------------------------------------------------------ svn:mime-type = application/octet-stream Modified: poi/trunk/src/scratchpad/testcases/org/apache/poi/hslf/extractor/TextExtractor.java URL: http://svn.apache.org/viewvc/poi/trunk/src/scratchpad/testcases/org/apache/poi/hslf/extractor/TextExtractor.java?rev=682533&r1=682532&r2=682533&view=diff ============================================================================== --- poi/trunk/src/scratchpad/testcases/org/apache/poi/hslf/extractor/TextExtractor.java (original) +++ poi/trunk/src/scratchpad/testcases/org/apache/poi/hslf/extractor/TextExtractor.java Mon Aug 4 15:48:39 2008 @@ -216,4 +216,36 @@ ppe.getText(true, false) ); } + + /** + * From bug #45543 + */ + public void testWithComments() throws Exception { + String filename; + + // New file + filename = dirname + "/WithComments.ppt"; + ppe = new PowerPointExtractor(filename); + + String text = ppe.getText(); + assertFalse("Comments not in by default", text.contains("This is a test comment")); + + ppe.setCommentsByDefault(true); + + text = ppe.getText(); + assertTrue("Unable to find expected word in text\n" + text, text.contains("This is a test comment")); + + + // And another file + filename = dirname + "/45543.ppt"; + ppe = new PowerPointExtractor(filename); + + text = ppe.getText(); + assertFalse("Comments not in by default", text.contains("testdoc")); + + ppe.setCommentsByDefault(true); + + text = ppe.getText(); + assertTrue("Unable to find expected word in text\n" + text, text.contains("testdoc")); + } } --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]